63 lines
3.0 KiB
JavaScript
63 lines
3.0 KiB
JavaScript
export const PROJECT_FORMAT_VERSION = 1;
|
|
export const PROJECT_MANIFEST = 'project.json';
|
|
|
|
export function parseProjectManifest(value) {
|
|
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
throw new TypeError('Project manifest must be an object');
|
|
}
|
|
if (value.formatVersion !== PROJECT_FORMAT_VERSION) {
|
|
throw new TypeError('Unsupported project format version');
|
|
}
|
|
if (typeof value.id !== 'string' || !/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/.test(value.id)) {
|
|
throw new TypeError('Invalid stable project identity');
|
|
}
|
|
if (typeof value.name !== 'string' || !value.name.trim() || value.name.length > 160) {
|
|
throw new TypeError('Invalid project name');
|
|
}
|
|
for (const key of ['createdAt', 'updatedAt']) {
|
|
if (typeof value[key] !== 'string' || !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?Z$/.test(value[key]) || !Number.isFinite(Date.parse(value[key])) || new Date(value[key]).toISOString().slice(0, 19) !== value[key].slice(0, 19)) {
|
|
throw new TypeError(`Invalid project ${key}`);
|
|
}
|
|
}
|
|
if (value.archived !== undefined && typeof value.archived !== 'boolean') {
|
|
throw new TypeError('Invalid project archive state');
|
|
}
|
|
// Return a copy preserving fields belonging to newer consumers of this version.
|
|
return structuredClone(value);
|
|
}
|
|
|
|
export function createProjectManifest({ id, name, now = new Date().toISOString() }) {
|
|
return parseProjectManifest({ formatVersion: PROJECT_FORMAT_VERSION, id, name, createdAt: now, updatedAt: now });
|
|
}
|
|
|
|
/** UUIDv8 for the first record of an extension that stores a collection by UUID. */
|
|
export async function defaultProjectRecordId(projectId, extensionId) {
|
|
const key = JSON.stringify(['isap.default-record.v1', projectId, extensionId]);
|
|
const bytes = new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(key)));
|
|
bytes[6] = (bytes[6] & 0x0f) | 0x80;
|
|
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
const hex = Array.from(bytes.subarray(0, 16), byte => byte.toString(16).padStart(2, '0')).join('');
|
|
return [hex.slice(0, 8), hex.slice(8, 12), hex.slice(12, 16), hex.slice(16, 20), hex.slice(20)].join('-');
|
|
}
|
|
|
|
export function validateArchivePath(path) {
|
|
if (typeof path !== 'string' || !path || path.length > 4096 || /[\\\u0000-\u001f\u007f:]/.test(path) || path.startsWith('/')) {
|
|
throw new TypeError('Unsafe project archive path');
|
|
}
|
|
const segments = path.split('/');
|
|
if (segments.some(segment => !segment || segment === '.' || segment === '..' || /[. ]$/.test(segment))) {
|
|
throw new TypeError('Unsafe project archive path');
|
|
}
|
|
if (segments.some(segment => /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(segment))) {
|
|
throw new TypeError('Unsafe project archive path');
|
|
}
|
|
return path;
|
|
}
|
|
|
|
export function extensionDataPath(extensionId, relativePath) {
|
|
if (typeof extensionId !== 'string' || !/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}$/.test(extensionId)) {
|
|
throw new TypeError('Invalid extension identity');
|
|
}
|
|
return validateArchivePath(`extensions/${extensionId}/${validateArchivePath(relativePath)}`);
|
|
}
|