@ebay-dt/bonfire-core 0.1.0-alpha.0 → 0.1.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -6
- package/dist/agent.d.ts +48 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +96 -0
- package/dist/agent.js.map +1 -0
- package/dist/auth-model.d.ts.map +1 -1
- package/dist/auth-model.js.map +1 -1
- package/dist/config.d.ts +2 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +15 -1
- package/dist/config.js.map +1 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/internal/types.d.ts.map +1 -1
- package/dist/internal/types.js.map +1 -1
- package/dist/internal/utils.d.ts.map +1 -1
- package/dist/internal/utils.js.map +1 -1
- package/dist/lifecycle-input.d.ts +10 -5
- package/dist/lifecycle-input.d.ts.map +1 -1
- package/dist/lifecycle-input.js +49 -25
- package/dist/lifecycle-input.js.map +1 -1
- package/dist/lifecycle.d.ts +23 -1
- package/dist/lifecycle.d.ts.map +1 -1
- package/dist/lifecycle.js +13 -0
- package/dist/lifecycle.js.map +1 -1
- package/dist/prototype-catalog.d.ts +4 -0
- package/dist/prototype-catalog.d.ts.map +1 -1
- package/dist/prototype-catalog.js.map +1 -1
- package/dist/prototype-metadata.d.ts +6 -1
- package/dist/prototype-metadata.d.ts.map +1 -1
- package/dist/prototype-metadata.js +17 -1
- package/dist/prototype-metadata.js.map +1 -1
- package/dist/prototype-ref.d.ts.map +1 -1
- package/dist/prototype-ref.js.map +1 -1
- package/dist/theme.d.ts +43 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +210 -0
- package/dist/theme.js.map +1 -0
- package/package.json +2 -2
package/dist/lifecycle-input.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { asRecord, deepFreeze, findUnknownKey, readRequiredString, } from "./internal/utils.js";
|
|
2
2
|
import { definePrototypeRef, PrototypeRefError, slugifyPrototypeTitle, } from "./prototype-ref.js";
|
|
3
|
+
import { parsePrototypeTheme, ThemeValidationError, } from "./theme.js";
|
|
3
4
|
export const PROTOTYPE_TITLE_MAX_CODE_POINTS = 200;
|
|
4
5
|
export const PROTOTYPE_DESCRIPTION_MAX_CODE_POINTS = 2_000;
|
|
5
6
|
export class LifecycleInputError extends Error {
|
|
@@ -48,26 +49,55 @@ function parseDescription(record) {
|
|
|
48
49
|
}
|
|
49
50
|
return description.length === 0 ? null : description;
|
|
50
51
|
}
|
|
51
|
-
function
|
|
52
|
-
if (!Object.hasOwn(record,
|
|
53
|
-
throw new LifecycleInputError(
|
|
52
|
+
function parseRef(record, key) {
|
|
53
|
+
if (!Object.hasOwn(record, key)) {
|
|
54
|
+
throw new LifecycleInputError(key);
|
|
54
55
|
}
|
|
55
56
|
try {
|
|
56
|
-
return definePrototypeRef(record
|
|
57
|
+
return definePrototypeRef(record[key]);
|
|
57
58
|
}
|
|
58
59
|
catch (error) {
|
|
59
60
|
if (error instanceof PrototypeRefError) {
|
|
60
|
-
throw new LifecycleInputError(
|
|
61
|
+
throw new LifecycleInputError(key);
|
|
62
|
+
}
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function parseSource(record) {
|
|
67
|
+
return parseRef(record, "source");
|
|
68
|
+
}
|
|
69
|
+
function parseThemeField(record, themeRegistry) {
|
|
70
|
+
if (!Object.hasOwn(record, "theme") || record.theme === undefined) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
if (!themeRegistry) {
|
|
74
|
+
// Theme input has nothing to validate against when the instance has no
|
|
75
|
+
// configured registry; reject rather than silently ignoring it.
|
|
76
|
+
throw new LifecycleInputError("theme");
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
// `theme` is already the normalized `PrototypeTheme` shape (the same
|
|
80
|
+
// shape stored in prototype metadata). Callers that offer a simplified
|
|
81
|
+
// authoring UI expand a `PrototypeThemeSelection` into this shape with
|
|
82
|
+
// `parsePrototypeThemeSelection` before submitting it, keeping this
|
|
83
|
+
// parse idempotent for callers that re-validate already-parsed input.
|
|
84
|
+
return parsePrototypeTheme(record.theme, "theme", themeRegistry);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
if (error instanceof ThemeValidationError) {
|
|
88
|
+
throw new LifecycleInputError(error.path);
|
|
61
89
|
}
|
|
62
90
|
throw error;
|
|
63
91
|
}
|
|
64
92
|
}
|
|
65
|
-
export function parseCreatePrototypeInput(value) {
|
|
93
|
+
export function parseCreatePrototypeInput(value, themeRegistry) {
|
|
66
94
|
const record = requireRecord(value, "createPrototypeInput");
|
|
67
|
-
rejectUnknownKey(record, ["title", "description"], "createPrototypeInput");
|
|
95
|
+
rejectUnknownKey(record, ["title", "description", "theme"], "createPrototypeInput");
|
|
96
|
+
const theme = parseThemeField(record, themeRegistry);
|
|
68
97
|
return deepFreeze({
|
|
69
98
|
title: parseTitle(record),
|
|
70
99
|
description: parseDescription(record),
|
|
100
|
+
...(theme === undefined ? {} : { theme }),
|
|
71
101
|
});
|
|
72
102
|
}
|
|
73
103
|
export function parseDuplicatePrototypeInput(value) {
|
|
@@ -79,26 +109,20 @@ export function parseDuplicatePrototypeInput(value) {
|
|
|
79
109
|
description: parseDescription(record),
|
|
80
110
|
});
|
|
81
111
|
}
|
|
112
|
+
export function parseEditPrototypeInput(value, themeRegistry) {
|
|
113
|
+
const record = requireRecord(value, "editPrototypeInput");
|
|
114
|
+
rejectUnknownKey(record, ["ref", "title", "description", "theme"], "editPrototypeInput");
|
|
115
|
+
const theme = parseThemeField(record, themeRegistry);
|
|
116
|
+
return deepFreeze({
|
|
117
|
+
ref: parseRef(record, "ref"),
|
|
118
|
+
title: parseTitle(record),
|
|
119
|
+
description: parseDescription(record),
|
|
120
|
+
...(theme === undefined ? {} : { theme }),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
82
123
|
export function parsePrototypeCommandInput(value) {
|
|
83
124
|
const record = requireRecord(value, "prototypeCommandInput");
|
|
84
125
|
rejectUnknownKey(record, ["ref"], "prototypeCommandInput");
|
|
85
|
-
|
|
86
|
-
throw new LifecycleInputError("ref");
|
|
87
|
-
}
|
|
88
|
-
try {
|
|
89
|
-
return deepFreeze({ ref: definePrototypeRef(record.ref) });
|
|
90
|
-
}
|
|
91
|
-
catch (error) {
|
|
92
|
-
if (error instanceof PrototypeRefError) {
|
|
93
|
-
throw new LifecycleInputError("ref");
|
|
94
|
-
}
|
|
95
|
-
throw error;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
export function parseSavePrototypeInput(value) {
|
|
99
|
-
return parsePrototypeCommandInput(value);
|
|
100
|
-
}
|
|
101
|
-
export function parseDiscardPrototypeInput(value) {
|
|
102
|
-
return parsePrototypeCommandInput(value);
|
|
126
|
+
return deepFreeze({ ref: parseRef(record, "ref") });
|
|
103
127
|
}
|
|
104
128
|
//# sourceMappingURL=lifecycle-input.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lifecycle-input.js","sourceRoot":"","sources":["../src/lifecycle-input.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,UAAU,EACV,cAAc,EACd,kBAAkB,GACnB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,qBAAqB,GAEtB,MAAM,oBAAoB,CAAC;AAE5B,MAAM,CAAC,MAAM,+BAA+B,GAAG,GAAG,CAAC;AACnD,MAAM,CAAC,MAAM,qCAAqC,GAAG,KAAK,CAAC;AAoB3D,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IACnC,IAAI,GAAG,eAAwB,CAAC;IAChC,IAAI,CAAS;IAEtB,YAAY,IAAY;QACtB,KAAK,CAAC,yBAAyB,CAAC,CAAC;QACjC,IA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AAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC;KAC1C,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,0BAA0B,CACxC,KAAc;IAEd,MAAM,MAAM,GAAG,aAAa,CAAC,KAAK,EAAE,uBAAuB,CAAC,CAAC;IAC7D,gBAAgB,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,EAAE,uBAAuB,CAAC,CAAC;IAE3D,OAAO,UAAU,CAAC,EAAE,GAAG,EAAE,QAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC,CAAC;AACtD,CAAC","sourcesContent":["import {\n asRecord,\n deepFreeze,\n findUnknownKey,\n readRequiredString,\n} from \"./internal/utils.js\";\nimport {\n definePrototypeRef,\n PrototypeRefError,\n slugifyPrototypeTitle,\n type PrototypeRef,\n} from \"./prototype-ref.js\";\nimport {\n parsePrototypeTheme,\n ThemeValidationError,\n type PrototypeTheme,\n type ThemeRegistry,\n} from \"./theme.js\";\n\nexport const PROTOTYPE_TITLE_MAX_CODE_POINTS = 200;\nexport const PROTOTYPE_DESCRIPTION_MAX_CODE_POINTS = 2_000;\n\nexport type CreatePrototypeInput = {\n title: string;\n description: string | null;\n theme?: PrototypeTheme;\n};\n\nexport type DuplicatePrototypeInput = {\n source: PrototypeRef;\n title: string;\n description: string | null;\n};\n\nexport type EditPrototypeInput = {\n ref: PrototypeRef;\n title: string;\n description: string | null;\n theme?: PrototypeTheme;\n};\n\nexport type PrototypeCommandInput = {\n ref: PrototypeRef;\n};\n\nexport class LifecycleInputError extends Error {\n readonly code = \"invalid-input\" as const;\n readonly path: string;\n\n constructor(path: string) {\n super(\"Invalid lifecycle input\");\n this.name = \"LifecycleInputError\";\n this.path = path;\n }\n}\n\nfunction requireRecord(value: unknown, path: string) {\n const record = asRecord(value);\n if (record === null) throw new LifecycleInputError(path);\n return record;\n}\n\nfunction rejectUnknownKey(\n record: Record<string, unknown>,\n allowedKeys: readonly string[],\n path: string,\n) {\n if (findUnknownKey(record, allowedKeys)) {\n throw new LifecycleInputError(path);\n }\n}\n\nfunction parseTitle(record: Record<string, unknown>) {\n const result = readRequiredString(record, \"title\");\n if (result.status !== \"valid\") {\n throw new LifecycleInputError(\"title\");\n }\n\n const title = result.value.trim();\n if (\n title.length === 0 ||\n Array.from(title).length > PROTOTYPE_TITLE_MAX_CODE_POINTS ||\n slugifyPrototypeTitle(title) === null\n ) {\n throw new LifecycleInputError(\"title\");\n }\n\n return title;\n}\n\nfunction parseDescription(record: Record<string, unknown>) {\n if (!Object.hasOwn(record, \"description\") || record.description === null) {\n return null;\n }\n\n if (typeof record.description !== \"string\") {\n throw new LifecycleInputError(\"description\");\n }\n\n const description = record.description.trim();\n if (Array.from(description).length > PROTOTYPE_DESCRIPTION_MAX_CODE_POINTS) {\n throw new LifecycleInputError(\"description\");\n }\n\n return description.length === 0 ? null : description;\n}\n\nfunction parseRef(\n record: Record<string, unknown>,\n key: \"ref\" | \"source\",\n): PrototypeRef {\n if (!Object.hasOwn(record, key)) {\n throw new LifecycleInputError(key);\n }\n\n try {\n return definePrototypeRef(record[key]);\n } catch (error) {\n if (error instanceof PrototypeRefError) {\n throw new LifecycleInputError(key);\n }\n throw error;\n }\n}\n\nfunction parseSource(record: Record<string, unknown>): PrototypeRef {\n return parseRef(record, \"source\");\n}\n\nfunction parseThemeField(\n record: Record<string, unknown>,\n themeRegistry: ThemeRegistry | undefined,\n): PrototypeTheme | undefined {\n if (!Object.hasOwn(record, \"theme\") || record.theme === undefined) {\n return undefined;\n }\n\n if (!themeRegistry) {\n // Theme input has nothing to validate against when the instance has no\n // configured registry; reject rather than silently ignoring it.\n throw new LifecycleInputError(\"theme\");\n }\n\n try {\n // `theme` is already the normalized `PrototypeTheme` shape (the same\n // shape stored in prototype metadata). Callers that offer a simplified\n // authoring UI expand a `PrototypeThemeSelection` into this shape with\n // `parsePrototypeThemeSelection` before submitting it, keeping this\n // parse idempotent for callers that re-validate already-parsed input.\n return parsePrototypeTheme(record.theme, \"theme\", themeRegistry);\n } catch (error) {\n if (error instanceof ThemeValidationError) {\n throw new LifecycleInputError(error.path);\n }\n throw error;\n }\n}\n\nexport function parseCreatePrototypeInput(\n value: unknown,\n themeRegistry?: ThemeRegistry,\n): CreatePrototypeInput {\n const record = requireRecord(value, \"createPrototypeInput\");\n rejectUnknownKey(\n record,\n [\"title\", \"description\", \"theme\"],\n \"createPrototypeInput\",\n );\n const theme = parseThemeField(record, themeRegistry);\n\n return deepFreeze({\n title: parseTitle(record),\n description: parseDescription(record),\n ...(theme === undefined ? {} : { theme }),\n });\n}\n\nexport function parseDuplicatePrototypeInput(\n value: unknown,\n): DuplicatePrototypeInput {\n const record = requireRecord(value, \"duplicatePrototypeInput\");\n rejectUnknownKey(\n record,\n [\"source\", \"title\", \"description\"],\n \"duplicatePrototypeInput\",\n );\n\n return deepFreeze({\n source: parseSource(record),\n title: parseTitle(record),\n description: parseDescription(record),\n });\n}\n\nexport function parseEditPrototypeInput(\n value: unknown,\n themeRegistry?: ThemeRegistry,\n): EditPrototypeInput {\n const record = requireRecord(value, \"editPrototypeInput\");\n rejectUnknownKey(\n record,\n [\"ref\", \"title\", \"description\", \"theme\"],\n \"editPrototypeInput\",\n );\n const theme = parseThemeField(record, themeRegistry);\n\n return deepFreeze({\n ref: parseRef(record, \"ref\"),\n title: parseTitle(record),\n description: parseDescription(record),\n ...(theme === undefined ? {} : { theme }),\n });\n}\n\nexport function parsePrototypeCommandInput(\n value: unknown,\n): PrototypeCommandInput {\n const record = requireRecord(value, \"prototypeCommandInput\");\n rejectUnknownKey(record, [\"ref\"], \"prototypeCommandInput\");\n\n return deepFreeze({ ref: parseRef(record, \"ref\") });\n}\n"]}
|
package/dist/lifecycle.d.ts
CHANGED
|
@@ -5,7 +5,11 @@ export declare class AutomaticBranchSwitchSettingError extends Error {
|
|
|
5
5
|
constructor();
|
|
6
6
|
}
|
|
7
7
|
export declare function parseAutomaticBranchSwitchSetting(value: unknown): AutomaticBranchSwitchMode;
|
|
8
|
-
export
|
|
8
|
+
export declare const DEPLOYMENT_READY_STATES: readonly ["BLOCKED", "BUILDING", "CANCELED", "ERROR", "INITIALIZING", "QUEUED", "READY"];
|
|
9
|
+
export type DeploymentReadyState = (typeof DEPLOYMENT_READY_STATES)[number];
|
|
10
|
+
export declare function isDeploymentReadyState(value: unknown): value is DeploymentReadyState;
|
|
11
|
+
export type DeploymentWarningCode = "metadata-persistence-failed" | "primary-merge-failed";
|
|
12
|
+
export type LifecycleErrorCode = "unauthenticated" | "forbidden" | "invalid-input" | "not-found" | "already-exists" | "repository-busy" | "working-tree-conflict" | "filesystem-failure" | "git-failure" | "deployment-failure" | "deployment-unavailable" | "timeout" | "unexpected-failure";
|
|
9
13
|
export type LifecycleError = {
|
|
10
14
|
code: LifecycleErrorCode;
|
|
11
15
|
};
|
|
@@ -28,15 +32,33 @@ export type LifecyclePrototypeValue = {
|
|
|
28
32
|
};
|
|
29
33
|
export type CreatePrototypeValue = LifecyclePrototypeValue;
|
|
30
34
|
export type DuplicatePrototypeValue = LifecyclePrototypeValue;
|
|
35
|
+
export type EditPrototypeValue = {
|
|
36
|
+
edited: true;
|
|
37
|
+
ref: PrototypeRef;
|
|
38
|
+
};
|
|
39
|
+
export type DeletePrototypeValue = {
|
|
40
|
+
deleted: true;
|
|
41
|
+
ref: PrototypeRef;
|
|
42
|
+
};
|
|
31
43
|
export type SavePrototypeValue = {
|
|
32
44
|
saved: true;
|
|
33
45
|
};
|
|
34
46
|
export type DiscardPrototypeValue = {
|
|
35
47
|
discarded: true;
|
|
36
48
|
};
|
|
49
|
+
export type DeploymentValue = {
|
|
50
|
+
deployed: true;
|
|
51
|
+
ref: PrototypeRef;
|
|
52
|
+
previewUrl: string;
|
|
53
|
+
deploymentId: string;
|
|
54
|
+
readyState: DeploymentReadyState;
|
|
55
|
+
warning: DeploymentWarningCode | undefined;
|
|
56
|
+
};
|
|
37
57
|
export type DirtyObservationStatus = "clean" | "dirty";
|
|
38
58
|
export type DirtyObservationValue = {
|
|
39
59
|
status: DirtyObservationStatus;
|
|
60
|
+
/** Existing deployment preview, when one has been persisted. */
|
|
61
|
+
previewUrl?: string | null;
|
|
40
62
|
};
|
|
41
63
|
export declare function lifecycleSuccess<Value>(value: Value): LifecycleResult<Value>;
|
|
42
64
|
export declare function lifecycleFailure(code: LifecycleErrorCode): LifecycleResult<never>;
|
package/dist/lifecycle.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lifecycle.d.ts","sourceRoot":"","sources":["../src/lifecycle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,MAAM,MAAM,yBAAyB,GAAG,SAAS,GAAG,UAAU,CAAC;AAE/D,qBAAa,iCAAkC,SAAQ,KAAK;IAC1D,QAAQ,CAAC,IAAI,EAAG,eAAe,CAAU;IAEzC,cAGC;CACF;AAED,wBAAgB,iCAAiC,CAC/C,KAAK,EAAE,OAAO,GACb,yBAAyB,CAI3B;AAED,MAAM,MAAM,kBAAkB,GAC1B,iBAAiB,GACjB,WAAW,GACX,eAAe,GACf,WAAW,GACX,gBAAgB,GAChB,iBAAiB,GACjB,uBAAuB,GACvB,oBAAoB,GACpB,aAAa,GACb,SAAS,GACT,oBAAoB,CAAC;AAEzB,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,kBAAkB,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,eAAe,CAAC,KAAK,IAC/B;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,KAAK,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,cAAc,CAAA;CAAE,CAAC;AAEpE,MAAM,MAAM,qBAAqB,GAAG,QAAQ,GAAG,YAAY,CAAC;AAC5D,MAAM,MAAM,uBAAuB,GAAG,OAAO,GAAG,UAAU,CAAC;AAE3D,MAAM,MAAM,sBAAsB,GAAG;IACnC,M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,QAAQ,EAAE,IAAI,CAAC;IACf,GAAG,EAAE,YAAY,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,oBAAoB,CAAC;IACjC,OAAO,EAAE,qBAAqB,GAAG,SAAS,CAAC;CAC5C,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG,OAAO,GAAG,OAAO,CAAC;AAEvD,MAAM,MAAM,qBAAqB,GAAG;IAClC,MAAM,EAAE,sBAAsB,CAAC;IAC/B,gEAAgE;IAChE,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B,CAAC;AAEF,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,GAAG,eAAe,CAAC,KAAK,CAAC,CAE5E;AAED,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,kBAAkB,GACvB,eAAe,CAAC,KAAK,CAAC,CAExB;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EACtC,MAAM,EAAE,eAAe,CAAC,KAAK,CAAC,GAC7B,MAAM,IAAI;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,KAAK,CAAA;CAAE,CAEtC;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EACtC,MAAM,EAAE,eAAe,CAAC,KAAK,CAAC,GAC7B,MAAM,IAAI;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,cAAc,CAAA;CAAE,CAEhD","sourcesContent":["import type { PrototypeRef } from \"./prototype-ref.js\";\n\nexport type AutomaticBranchSwitchMode = \"enabled\" | \"disabled\";\n\nexport class AutomaticBranchSwitchSettingError extends Error {\n readonly code = \"invalid-input\" as const;\n\n constructor() {\n super(\"Invalid automatic branch-switch setting\");\n this.name = \"AutomaticBranchSwitchSettingError\";\n }\n}\n\nexport function parseAutomaticBranchSwitchSetting(\n value: unknown,\n): AutomaticBranchSwitchMode {\n if (value === undefined || value === \"false\") return \"enabled\";\n if (value === \"true\") return \"disabled\";\n throw new AutomaticBranchSwitchSettingError();\n}\n\nexport const DEPLOYMENT_READY_STATES = Object.freeze([\n \"BLOCKED\",\n \"BUILDING\",\n \"CANCELED\",\n \"ERROR\",\n \"INITIALIZING\",\n \"QUEUED\",\n \"READY\",\n] as const);\n\nexport type DeploymentReadyState = (typeof DEPLOYMENT_READY_STATES)[number];\n\nexport function isDeploymentReadyState(\n value: unknown,\n): value is DeploymentReadyState {\n return (\n typeof value === \"string\" &&\n (DEPLOYMENT_READY_STATES as readonly string[]).includes(value)\n );\n}\n\nexport type DeploymentWarningCode =\n \"metadata-persistence-failed\" | \"primary-merge-failed\";\n\nexport type LifecycleErrorCode =\n | \"unauthenticated\"\n | \"forbidden\"\n | \"invalid-input\"\n | \"not-found\"\n | \"already-exists\"\n | \"repository-busy\"\n | \"working-tree-conflict\"\n | \"filesystem-failure\"\n | \"git-failure\"\n | \"deployment-failure\"\n | \"deployment-unavailable\"\n | \"timeout\"\n | \"unexpected-failure\";\n\nexport type LifecycleError = {\n code: LifecycleErrorCode;\n};\n\nexport type LifecycleResult<Value> =\n { ok: true; value: Value } | { ok: false; error: LifecycleError };\n\nexport type LifecycleRemoteStatus = \"pushed\" | \"local-only\";\nexport type BranchPreparationStatus = \"ready\" | \"disabled\";\n\nexport type BranchPreparationValue = {\n status: BranchPreparationStatus;\n};\n\nexport type LifecyclePrototypeValue = {\n ref: PrototypeRef;\n url: string;\n remoteStatus: LifecycleRemoteStatus;\n};\n\nexport type CreatePrototypeValue = LifecyclePrototypeValue;\nexport type DuplicatePrototypeValue = LifecyclePrototypeValue;\n\nexport type EditPrototypeValue = {\n edited: true;\n ref: PrototypeRef;\n};\n\nexport type DeletePrototypeValue = {\n deleted: true;\n ref: PrototypeRef;\n};\n\nexport type SavePrototypeValue = {\n saved: true;\n};\n\nexport type DiscardPrototypeValue = {\n discarded: true;\n};\n\nexport type DeploymentValue = {\n deployed: true;\n ref: PrototypeRef;\n previewUrl: string;\n deploymentId: string;\n readyState: DeploymentReadyState;\n warning: DeploymentWarningCode | undefined;\n};\n\nexport type DirtyObservationStatus = \"clean\" | \"dirty\";\n\nexport type DirtyObservationValue = {\n status: DirtyObservationStatus;\n /** Existing deployment preview, when one has been persisted. */\n previewUrl?: string | null;\n};\n\nexport function lifecycleSuccess<Value>(value: Value): LifecycleResult<Value> {\n return { ok: true, value };\n}\n\nexport function lifecycleFailure(\n code: LifecycleErrorCode,\n): LifecycleResult<never> {\n return { ok: false, error: { code } };\n}\n\nexport function isLifecycleSuccess<Value>(\n result: LifecycleResult<Value>,\n): result is { ok: true; value: Value } {\n return result.ok;\n}\n\nexport function isLifecycleFailure<Value>(\n result: LifecycleResult<Value>,\n): result is { ok: false; error: LifecycleError } {\n return !result.ok;\n}\n"]}
|
package/dist/lifecycle.js
CHANGED
|
@@ -12,6 +12,19 @@ export function parseAutomaticBranchSwitchSetting(value) {
|
|
|
12
12
|
return "disabled";
|
|
13
13
|
throw new AutomaticBranchSwitchSettingError();
|
|
14
14
|
}
|
|
15
|
+
export const DEPLOYMENT_READY_STATES = Object.freeze([
|
|
16
|
+
"BLOCKED",
|
|
17
|
+
"BUILDING",
|
|
18
|
+
"CANCELED",
|
|
19
|
+
"ERROR",
|
|
20
|
+
"INITIALIZING",
|
|
21
|
+
"QUEUED",
|
|
22
|
+
"READY",
|
|
23
|
+
]);
|
|
24
|
+
export function isDeploymentReadyState(value) {
|
|
25
|
+
return (typeof value === "string" &&
|
|
26
|
+
DEPLOYMENT_READY_STATES.includes(value));
|
|
27
|
+
}
|
|
15
28
|
export function lifecycleSuccess(value) {
|
|
16
29
|
return { ok: true, value };
|
|
17
30
|
}
|
package/dist/lifecycle.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lifecycle.js","sourceRoot":"","sources":["../src/lifecycle.ts"],"names":[],"mappings":"AAIA,MAAM,OAAO,iCAAkC,SAAQ,KAAK;IACjD,IAAI,GAAG,eAAwB,CAAC;IAE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MAAM,UAAU,gBAAgB,CAC9B,IAAwB;IAExB,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,EAAE,CAAC;AACxC,CAAC;AAED,MAAM,UAAU,kBAAkB,CAChC,MAA8B;IAE9B,OAAO,MAAM,CAAC,EAAE,CAAC;AACnB,CAAC;AAED,MAAM,UAAU,kBAAkB,CAChC,MAA8B;IAE9B,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;AACpB,CAAC","sourcesContent":["import type { PrototypeRef } from \"./prototype-ref.js\";\n\nexport type AutomaticBranchSwitchMode = \"enabled\" | \"disabled\";\n\nexport class AutomaticBranchSwitchSettingError extends Error {\n readonly code = \"invalid-input\" as const;\n\n constructor() {\n super(\"Invalid automatic branch-switch setting\");\n this.name = \"AutomaticBranchSwitchSettingError\";\n }\n}\n\nexport function parseAutomaticBranchSwitchSetting(\n value: unknown,\n): AutomaticBranchSwitchMode {\n if (value === undefined || value === \"false\") return \"enabled\";\n if (value === \"true\") return \"disabled\";\n throw new AutomaticBranchSwitchSettingError();\n}\n\nexport const DEPLOYMENT_READY_STATES = Object.freeze([\n \"BLOCKED\",\n \"BUILDING\",\n \"CANCELED\",\n \"ERROR\",\n \"INITIALIZING\",\n \"QUEUED\",\n \"READY\",\n] as const);\n\nexport type DeploymentReadyState = (typeof DEPLOYMENT_READY_STATES)[number];\n\nexport function isDeploymentReadyState(\n value: unknown,\n): value is DeploymentReadyState {\n return (\n typeof value === \"string\" &&\n (DEPLOYMENT_READY_STATES as readonly string[]).includes(value)\n );\n}\n\nexport type DeploymentWarningCode =\n \"metadata-persistence-failed\" | \"primary-merge-failed\";\n\nexport type LifecycleErrorCode =\n | \"unauthenticated\"\n | \"forbidden\"\n | \"invalid-input\"\n | \"not-found\"\n | \"already-exists\"\n | \"repository-busy\"\n | \"working-tree-conflict\"\n | \"filesystem-failure\"\n | \"git-failure\"\n | \"deployment-failure\"\n | \"deployment-unavailable\"\n | \"timeout\"\n | \"unexpected-failure\";\n\nexport type LifecycleError = {\n code: LifecycleErrorCode;\n};\n\nexport type LifecycleResult<Value> =\n { ok: true; value: Value } | { ok: false; error: LifecycleError };\n\nexport type LifecycleRemoteStatus = \"pushed\" | \"local-only\";\nexport type BranchPreparationStatus = \"ready\" | \"disabled\";\n\nexport type BranchPreparationValue = {\n status: BranchPreparationStatus;\n};\n\nexport type LifecyclePrototypeValue = {\n ref: PrototypeRef;\n url: string;\n remoteStatus: LifecycleRemoteStatus;\n};\n\nexport type CreatePrototypeValue = LifecyclePrototypeValue;\nexport type DuplicatePrototypeValue = LifecyclePrototypeValue;\n\nexport type EditPrototypeValue = {\n edited: true;\n ref: PrototypeRef;\n};\n\nexport type DeletePrototypeValue = {\n deleted: true;\n ref: PrototypeRef;\n};\n\nexport type SavePrototypeValue = {\n saved: true;\n};\n\nexport type DiscardPrototypeValue = {\n discarded: true;\n};\n\nexport type DeploymentValue = {\n deployed: true;\n ref: PrototypeRef;\n previewUrl: string;\n deploymentId: string;\n readyState: DeploymentReadyState;\n warning: DeploymentWarningCode | undefined;\n};\n\nexport type DirtyObservationStatus = \"clean\" | \"dirty\";\n\nexport type DirtyObservationValue = {\n status: DirtyObservationStatus;\n /** Existing deployment preview, when one has been persisted. */\n previewUrl?: string | null;\n};\n\nexport function lifecycleSuccess<Value>(value: Value): LifecycleResult<Value> {\n return { ok: true, value };\n}\n\nexport function lifecycleFailure(\n code: LifecycleErrorCode,\n): LifecycleResult<never> {\n return { ok: false, error: { code } };\n}\n\nexport function isLifecycleSuccess<Value>(\n result: LifecycleResult<Value>,\n): result is { ok: true; value: Value } {\n return result.ok;\n}\n\nexport function isLifecycleFailure<Value>(\n result: LifecycleResult<Value>,\n): result is { ok: false; error: LifecycleError } {\n return !result.ok;\n}\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { PrototypeSummary } from "./prototype-metadata.js";
|
|
2
|
+
import type { ThemeRegistry } from "./theme.js";
|
|
2
3
|
export type PrototypeSort = "recent" | "alphabetical" | "deployed";
|
|
3
4
|
export type PrototypeAuthorSelection = "all" | "mine" | string;
|
|
4
5
|
export type PrototypeCatalogQuery = {
|
|
@@ -15,6 +16,9 @@ export type PrototypeDashboardModel = {
|
|
|
15
16
|
authorOptions: readonly PrototypeAuthorOption[];
|
|
16
17
|
showMine: boolean;
|
|
17
18
|
summaries: readonly PrototypeSummary[];
|
|
19
|
+
/** Present only when the instance has configured theme definitions; used
|
|
20
|
+
* to render theme-authoring controls in create/edit dialogs. */
|
|
21
|
+
themeRegistry?: ThemeRegistry;
|
|
18
22
|
};
|
|
19
23
|
export declare function filterPrototypes(summaries: readonly PrototypeSummary[], author: PrototypeAuthorSelection, currentLogin?: string | null): PrototypeSummary[];
|
|
20
24
|
export declare function sortPrototypes(summaries: readonly PrototypeSummary[], sort: PrototypeSort): PrototypeSummary[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prototype-catalog.d.ts","sourceRoot":"","sources":["../src/prototype-catalog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"prototype-catalog.d.ts","sourceRoot":"","sources":["../src/prototype-catalog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAChE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,cAAc,GAAG,UAAU,CAAC;AACnE,MAAM,MAAM,wBAAwB,GAAG,KAAK,GAAG,MAAM,GAAG,MAAM,CAAC;AAE/D,MAAM,MAAM,qBAAqB,GAAG;IAClC,MAAM,EAAE,wBAAwB,CAAC;IACjC,IAAI,EAAE,aAAa,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG;IACpC,eAAe,EAAE,MAAM,CAAC;IACxB,KAAK,EAAE,qBAAqB,CAAC;IAC7B,aAAa,EAAE,SAAS,qBAAqB,EAAE,CAAC;IAChD,QAAQ,EAAE,OAAO,CAAC;IAClB,SAAS,EAAE,SAAS,gBAAgB,EAAE,CAAC;IACvC;oEACgE;IAChE,aAAa,CAAC,EAAE,aAAa,CAAC;CAC/B,CAAC;AAgBF,wBAAgB,gBAAgB,CAC9B,SAAS,EAAE,SAAS,gBAAgB,EAAE,EACtC,MAAM,EAAE,wBAAwB,EAChC,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,GAC3B,gBAAgB,EAAE,CAQpB;AAED,wBAAgB,cAAc,CAC5B,SAAS,EAAE,SAAS,gBAAgB,EAAE,EACtC,IAAI,EAAE,aAAa,GAClB,gBAAgB,EAAE,CAgCpB;AAED,wBAAgB,yBAAyB,CACvC,SAAS,EAAE,SAAS,gBAAgB,EAAE,EACtC,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,GAC3B,qBAAqB,EAAE,CAkBzB","sourcesContent":["import type { PrototypeSummary } from \"./prototype-metadata.js\";\nimport type { ThemeRegistry } from \"./theme.js\";\n\nexport type PrototypeSort = \"recent\" | \"alphabetical\" | \"deployed\";\nexport type PrototypeAuthorSelection = \"all\" | \"mine\" | string;\n\nexport type PrototypeCatalogQuery = {\n author: PrototypeAuthorSelection;\n sort: PrototypeSort;\n};\n\nexport type PrototypeAuthorOption = {\n login: string;\n name: string | null;\n};\n\nexport type PrototypeDashboardModel = {\n applicationName: string;\n query: PrototypeCatalogQuery;\n authorOptions: readonly PrototypeAuthorOption[];\n showMine: boolean;\n summaries: readonly PrototypeSummary[];\n /** Present only when the instance has configured theme definitions; used\n * to render theme-authoring controls in create/edit dialogs. */\n themeRegistry?: ThemeRegistry;\n};\n\nfunction compareStrings(left: string, right: string) {\n if (left < right) return -1;\n if (left > right) return 1;\n return 0;\n}\n\nfunction compareNewest(left: string, right: string) {\n return compareStrings(right, left);\n}\n\nfunction compareByUrl(left: PrototypeSummary, right: PrototypeSummary) {\n return compareStrings(left.url, right.url);\n}\n\nexport function filterPrototypes(\n summaries: readonly PrototypeSummary[],\n author: PrototypeAuthorSelection,\n currentLogin?: string | null,\n): PrototypeSummary[] {\n if (author === \"all\") return [...summaries];\n if (author === \"mine\") {\n if (!currentLogin) return [];\n return summaries.filter((summary) => summary.authorLogin === currentLogin);\n }\n\n return summaries.filter((summary) => summary.authorLogin === author);\n}\n\nexport function sortPrototypes(\n summaries: readonly PrototypeSummary[],\n sort: PrototypeSort,\n): PrototypeSummary[] {\n const result = [...summaries];\n\n result.sort((left, right) => {\n if (sort === \"alphabetical\") {\n return (\n compareStrings(left.title, right.title) || compareByUrl(left, right)\n );\n }\n\n if (sort === \"deployed\") {\n const leftDeployed = left.previewUrl !== null;\n const rightDeployed = right.previewUrl !== null;\n if (leftDeployed !== rightDeployed) return leftDeployed ? -1 : 1;\n\n if (leftDeployed) {\n return (\n compareNewest(left.updatedAt, right.updatedAt) ||\n compareByUrl(left, right)\n );\n }\n\n return compareByUrl(left, right);\n }\n\n return (\n compareNewest(left.createdAt, right.createdAt) ||\n compareByUrl(left, right)\n );\n });\n\n return result;\n}\n\nexport function getPrototypeAuthorOptions(\n summaries: readonly PrototypeSummary[],\n currentLogin?: string | null,\n): PrototypeAuthorOption[] {\n const options = new Map<string, PrototypeSummary>();\n\n for (const summary of summaries) {\n if (summary.authorLogin === currentLogin) continue;\n\n const existing = options.get(summary.authorLogin);\n if (existing === undefined || compareByUrl(summary, existing) < 0) {\n options.set(summary.authorLogin, summary);\n }\n }\n\n return [...options.values()]\n .sort((left, right) => compareStrings(left.authorLogin, right.authorLogin))\n .map((summary) => ({\n login: summary.authorLogin,\n name: summary.authorName,\n }));\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prototype-catalog.js","sourceRoot":"","sources":["../src/prototype-catalog.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"prototype-catalog.js","sourceRoot":"","sources":["../src/prototype-catalog.ts"],"names":[],"mappings":"AA2BA,SAAS,cAAc,CAAC,IAAY,EAAE,KAAa;IACjD,IAAI,IAAI,GAAG,KAAK;QAAE,OAAO,CAAC,CAAC,CAAC;IAC5B,IAAI,IAAI,GAAG,KAAK;QAAE,OAAO,CAAC,CAAC;IAC3B,OAAO,CAAC,CAAC;AACX,CAAC;AAED,SAAS,aAAa,CAAC,IAAY,EAAE,KAAa;IAChD,OAAO,cAAc,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;AACrC,CAAC;AAED,SAAS,YAAY,CAAC,IAAsB,EAAE,KAAuB;IACnE,OAAO,cAAc,CAAC,IAAI,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;AAC7C,CAAC;AAED,MAAM,UAAU,gBAAgB,CAC9B,SAAsC,EACtC,MAAgC,EAChC,YAA4B;IAE5B,IAAI,MAAM,KAAK,KAAK;QAAE,OAAO,CAAC,GAAG,SAAS,CAAC,CAAC;IAC5C,IAAI,MAAM,KAAK,MAAM,EAAE,CAAC;QACtB,IAAI,CAAC,YAAY;YAAE,OAAO,EAAE,CAAC;QAC7B,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,WAAW,KAAK,YAAY,CAAC,CAAC;IAC7E,CAAC;IAED,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,WAAW,KAAK,MAAM,CAAC,CAAC;AACvE,CAAC;AAED,MAAM,UAAU,cAAc,CAC5B,SAAsC,EACtC,IAAmB;IAEnB,MAAM,MAAM,GAAG,CAAC,GAAG,SAAS,CAAC,CAAC;IAE9B,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;QAC1B,IAAI,IAAI,KAAK,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GAAG,CAAC,EAAE,CAAC;YAClE,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;QAC5C,CAAC;IACH,CAAC;IAED,OAAO,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;SACzB,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,cAAc,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC;SAC1E,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;QACjB,KAAK,EAAE,OAAO,CAAC,WAAW;QAC1B,IAAI,EAAE,OAAO,CAAC,UAAU;KACzB,CAAC,CAAC,CAAC;AACR,CAAC","sourcesContent":["import type { PrototypeSummary } from \"./prototype-metadata.js\";\nimport type { ThemeRegistry } from \"./theme.js\";\n\nexport type PrototypeSort = \"recent\" | \"alphabetical\" | \"deployed\";\nexport type PrototypeAuthorSelection = \"all\" | \"mine\" | string;\n\nexport type PrototypeCatalogQuery = {\n author: PrototypeAuthorSelection;\n sort: PrototypeSort;\n};\n\nexport type PrototypeAuthorOption = {\n login: string;\n name: string | null;\n};\n\nexport type PrototypeDashboardModel = {\n applicationName: string;\n query: PrototypeCatalogQuery;\n authorOptions: readonly PrototypeAuthorOption[];\n showMine: boolean;\n summaries: readonly PrototypeSummary[];\n /** Present only when the instance has configured theme definitions; used\n * to render theme-authoring controls in create/edit dialogs. */\n themeRegistry?: ThemeRegistry;\n};\n\nfunction compareStrings(left: string, right: string) {\n if (left < right) return -1;\n if (left > right) return 1;\n return 0;\n}\n\nfunction compareNewest(left: string, right: string) {\n return compareStrings(right, left);\n}\n\nfunction compareByUrl(left: PrototypeSummary, right: PrototypeSummary) {\n return compareStrings(left.url, right.url);\n}\n\nexport function filterPrototypes(\n summaries: readonly PrototypeSummary[],\n author: PrototypeAuthorSelection,\n currentLogin?: string | null,\n): PrototypeSummary[] {\n if (author === \"all\") return [...summaries];\n if (author === \"mine\") {\n if (!currentLogin) return [];\n return summaries.filter((summary) => summary.authorLogin === currentLogin);\n }\n\n return summaries.filter((summary) => summary.authorLogin === author);\n}\n\nexport function sortPrototypes(\n summaries: readonly PrototypeSummary[],\n sort: PrototypeSort,\n): PrototypeSummary[] {\n const result = [...summaries];\n\n result.sort((left, right) => {\n if (sort === \"alphabetical\") {\n return (\n compareStrings(left.title, right.title) || compareByUrl(left, right)\n );\n }\n\n if (sort === \"deployed\") {\n const leftDeployed = left.previewUrl !== null;\n const rightDeployed = right.previewUrl !== null;\n if (leftDeployed !== rightDeployed) return leftDeployed ? -1 : 1;\n\n if (leftDeployed) {\n return (\n compareNewest(left.updatedAt, right.updatedAt) ||\n compareByUrl(left, right)\n );\n }\n\n return compareByUrl(left, right);\n }\n\n return (\n compareNewest(left.createdAt, right.createdAt) ||\n compareByUrl(left, right)\n );\n });\n\n return result;\n}\n\nexport function getPrototypeAuthorOptions(\n summaries: readonly PrototypeSummary[],\n currentLogin?: string | null,\n): PrototypeAuthorOption[] {\n const options = new Map<string, PrototypeSummary>();\n\n for (const summary of summaries) {\n if (summary.authorLogin === currentLogin) continue;\n\n const existing = options.get(summary.authorLogin);\n if (existing === undefined || compareByUrl(summary, existing) < 0) {\n options.set(summary.authorLogin, summary);\n }\n }\n\n return [...options.values()]\n .sort((left, right) => compareStrings(left.authorLogin, right.authorLogin))\n .map((summary) => ({\n login: summary.authorLogin,\n name: summary.authorName,\n }));\n}\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type PrototypeRef } from "./prototype-ref.js";
|
|
2
|
+
import { type PrototypeTheme, type ThemeRegistry } from "./theme.js";
|
|
2
3
|
export type PrototypeMetadata = {
|
|
3
4
|
title: string;
|
|
4
5
|
description: string | null;
|
|
@@ -7,6 +8,7 @@ export type PrototypeMetadata = {
|
|
|
7
8
|
createdAt: string;
|
|
8
9
|
updatedAt: string;
|
|
9
10
|
previewUrl: string | null;
|
|
11
|
+
theme?: PrototypeTheme;
|
|
10
12
|
};
|
|
11
13
|
export type PrototypeSummary = PrototypeMetadata & {
|
|
12
14
|
ref: PrototypeRef;
|
|
@@ -16,6 +18,9 @@ export declare class PrototypeMetadataError extends Error {
|
|
|
16
18
|
readonly path: string;
|
|
17
19
|
constructor(path: string, message: string);
|
|
18
20
|
}
|
|
19
|
-
export
|
|
21
|
+
export type PrototypeMetadataParseOptions = {
|
|
22
|
+
themeRegistry?: ThemeRegistry;
|
|
23
|
+
};
|
|
24
|
+
export declare function parsePrototypeMetadata(value: unknown, options?: PrototypeMetadataParseOptions): PrototypeMetadata;
|
|
20
25
|
export declare function definePrototypeSummary(metadata: PrototypeMetadata, ref: PrototypeRef): PrototypeSummary;
|
|
21
26
|
//# sourceMappingURL=prototype-metadata.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prototype-metadata.d.ts","sourceRoot":"","sources":["../src/prototype-metadata.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,YAAY,EAClB,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"prototype-metadata.d.ts","sourceRoot":"","sources":["../src/prototype-metadata.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,KAAK,YAAY,EAClB,MAAM,oBAAoB,CAAC;AAK5B,OAAO,EAGL,KAAK,cAAc,EACnB,KAAK,aAAa,EACnB,MAAM,YAAY,CAAC;AAGpB,MAAM,MAAM,iBAAiB,GAAG;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,KAAK,CAAC,EAAE,cAAc,CAAC;CACxB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,iBAAiB,GAAG;IACjD,GAAG,EAAE,YAAY,CAAC;IAClB,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,qBAAa,sBAAuB,SAAQ,KAAK;IAC/C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,YAAY,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAIxC;CACF;AA6JD,MAAM,MAAM,6BAA6B,GAAG;IAC1C,aAAa,CAAC,EAAE,aAAa,CAAC;CAC/B,CAAC;AAsBF,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,OAAO,EACd,OAAO,GAAE,6BAAkC,GAC1C,iBAAiB,CAuBnB;AAED,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,iBAAiB,EAC3B,GAAG,EAAE,YAAY,GAChB,gBAAgB,CAelB","sourcesContent":["import { asRecord, deepFreeze, readRequiredString } from \"./internal/utils.js\";\nimport {\n definePrototypeRef,\n getPrototypeUrl,\n PrototypeRefError,\n type PrototypeRef,\n} from \"./prototype-ref.js\";\nimport {\n PROTOTYPE_DESCRIPTION_MAX_CODE_POINTS,\n PROTOTYPE_TITLE_MAX_CODE_POINTS,\n} from \"./lifecycle-input.js\";\nimport {\n parsePrototypeTheme,\n ThemeValidationError,\n type PrototypeTheme,\n type ThemeRegistry,\n} from \"./theme.js\";\nconst VALIDATION_SLUG = \"prototype\";\n\nexport type PrototypeMetadata = {\n title: string;\n description: string | null;\n authorLogin: string;\n authorName: string | null;\n createdAt: string;\n updatedAt: string;\n previewUrl: string | null;\n theme?: PrototypeTheme;\n};\n\nexport type PrototypeSummary = PrototypeMetadata & {\n ref: PrototypeRef;\n url: string;\n};\n\nexport class PrototypeMetadataError extends Error {\n readonly path: string;\n\n constructor(path: string, message: string) {\n super(`Invalid prototype metadata at ${path}: ${message}`);\n this.name = \"PrototypeMetadataError\";\n this.path = path;\n }\n}\n\nfunction codePointLength(value: string) {\n return Array.from(value).length;\n}\n\nfunction requiredMetadataRecord(value: unknown) {\n const record = asRecord(value);\n if (record === null) {\n throw new PrototypeMetadataError(\"metadata\", \"must be an object\");\n }\n\n return record;\n}\n\nfunction parseTitle(record: Record<string, unknown>) {\n const result = readRequiredString(record, \"title\");\n if (result.status === \"missing\") {\n throw new PrototypeMetadataError(\"metadata.title\", \"is required\");\n }\n if (result.status === \"invalid\") {\n throw new PrototypeMetadataError(\"metadata.title\", \"must be a string\");\n }\n\n const title = result.value.trim();\n if (title.length === 0) {\n throw new PrototypeMetadataError(\"metadata.title\", \"must not be empty\");\n }\n if (codePointLength(title) > PROTOTYPE_TITLE_MAX_CODE_POINTS) {\n throw new PrototypeMetadataError(\n \"metadata.title\",\n `must be at most ${PROTOTYPE_TITLE_MAX_CODE_POINTS} Unicode code points`,\n );\n }\n\n return title;\n}\n\nfunction parseNullableText(\n record: Record<string, unknown>,\n key: \"description\" | \"authorName\",\n maximumCodePoints: number,\n) {\n if (!Object.hasOwn(record, key)) return null;\n\n const value = record[key];\n if (value === null) return null;\n if (typeof value !== \"string\") {\n throw new PrototypeMetadataError(\n `metadata.${key}`,\n \"must be a string or null\",\n );\n }\n\n const normalized = value.trim();\n if (normalized.length === 0) return null;\n if (codePointLength(normalized) > maximumCodePoints) {\n throw new PrototypeMetadataError(\n `metadata.${key}`,\n `must be at most ${maximumCodePoints} Unicode code points`,\n );\n }\n\n return normalized;\n}\n\nfunction parseAuthorLogin(record: Record<string, unknown>) {\n const result = readRequiredString(record, \"authorLogin\");\n if (result.status === \"missing\") {\n throw new PrototypeMetadataError(\"metadata.authorLogin\", \"is required\");\n }\n if (result.status === \"invalid\") {\n throw new PrototypeMetadataError(\n \"metadata.authorLogin\",\n \"must be a string\",\n );\n }\n\n try {\n return definePrototypeRef({\n login: result.value,\n slug: VALIDATION_SLUG,\n }).login;\n } catch (error) {\n if (error instanceof PrototypeRefError) {\n throw new PrototypeMetadataError(\n \"metadata.authorLogin\",\n \"must be a valid login segment\",\n );\n }\n throw error;\n }\n}\n\nfunction parseTimestamp(\n record: Record<string, unknown>,\n key: \"createdAt\" | \"updatedAt\",\n) {\n const result = readRequiredString(record, key);\n if (result.status === \"missing\") {\n throw new PrototypeMetadataError(`metadata.${key}`, \"is required\");\n }\n if (result.status === \"invalid\") {\n throw new PrototypeMetadataError(`metadata.${key}`, \"must be a string\");\n }\n\n try {\n if (new Date(result.value).toISOString() !== result.value) {\n throw new Error(\"noncanonical timestamp\");\n }\n } catch {\n throw new PrototypeMetadataError(\n `metadata.${key}`,\n \"must be a canonical ISO-8601 timestamp\",\n );\n }\n\n return result.value;\n}\n\nfunction parsePreviewUrl(record: Record<string, unknown>) {\n if (!Object.hasOwn(record, \"previewUrl\")) return null;\n\n const value = record.previewUrl;\n if (value === null) return null;\n if (typeof value !== \"string\") {\n throw new PrototypeMetadataError(\n \"metadata.previewUrl\",\n \"must be a string or null\",\n );\n }\n if (value.length === 0 || value !== value.trim() || /\\s/u.test(value)) {\n throw new PrototypeMetadataError(\n \"metadata.previewUrl\",\n \"must be an absolute HTTPS URL without credentials\",\n );\n }\n\n try {\n const url = new URL(value);\n if (\n url.protocol !== \"https:\" ||\n url.username.length > 0 ||\n url.password.length > 0\n ) {\n throw new Error(\"unsupported preview URL\");\n }\n } catch {\n throw new PrototypeMetadataError(\n \"metadata.previewUrl\",\n \"must be an absolute HTTPS URL without credentials\",\n );\n }\n\n return value;\n}\n\nexport type PrototypeMetadataParseOptions = {\n themeRegistry?: ThemeRegistry;\n};\n\nfunction parseMetadataTheme(\n record: Record<string, unknown>,\n options: PrototypeMetadataParseOptions,\n) {\n if (!Object.hasOwn(record, \"theme\")) return undefined;\n\n try {\n return parsePrototypeTheme(\n record.theme,\n \"metadata.theme\",\n options.themeRegistry,\n );\n } catch (error) {\n if (error instanceof ThemeValidationError) {\n throw new PrototypeMetadataError(error.path, error.reason);\n }\n throw error;\n }\n}\n\nexport function parsePrototypeMetadata(\n value: unknown,\n options: PrototypeMetadataParseOptions = {},\n): PrototypeMetadata {\n const record = requiredMetadataRecord(value);\n const theme = parseMetadataTheme(record, options);\n const metadata = {\n title: parseTitle(record),\n description: parseNullableText(\n record,\n \"description\",\n PROTOTYPE_DESCRIPTION_MAX_CODE_POINTS,\n ),\n authorLogin: parseAuthorLogin(record),\n authorName: parseNullableText(\n record,\n \"authorName\",\n PROTOTYPE_TITLE_MAX_CODE_POINTS,\n ),\n createdAt: parseTimestamp(record, \"createdAt\"),\n updatedAt: parseTimestamp(record, \"updatedAt\"),\n previewUrl: parsePreviewUrl(record),\n ...(theme === undefined ? {} : { theme }),\n } satisfies PrototypeMetadata;\n\n return deepFreeze(metadata);\n}\n\nexport function definePrototypeSummary(\n metadata: PrototypeMetadata,\n ref: PrototypeRef,\n): PrototypeSummary {\n const normalizedMetadata = parsePrototypeMetadata(metadata);\n const normalizedRef = definePrototypeRef(ref);\n if (normalizedMetadata.authorLogin !== normalizedRef.login) {\n throw new PrototypeMetadataError(\n \"metadata.authorLogin\",\n \"must match ref.login\",\n );\n }\n\n return deepFreeze({\n ...normalizedMetadata,\n ref: normalizedRef,\n url: getPrototypeUrl(normalizedRef),\n });\n}\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { asRecord, deepFreeze, readRequiredString } from "./internal/utils.js";
|
|
2
2
|
import { definePrototypeRef, getPrototypeUrl, PrototypeRefError, } from "./prototype-ref.js";
|
|
3
3
|
import { PROTOTYPE_DESCRIPTION_MAX_CODE_POINTS, PROTOTYPE_TITLE_MAX_CODE_POINTS, } from "./lifecycle-input.js";
|
|
4
|
+
import { parsePrototypeTheme, ThemeValidationError, } from "./theme.js";
|
|
4
5
|
const VALIDATION_SLUG = "prototype";
|
|
5
6
|
export class PrototypeMetadataError extends Error {
|
|
6
7
|
path;
|
|
@@ -118,8 +119,22 @@ function parsePreviewUrl(record) {
|
|
|
118
119
|
}
|
|
119
120
|
return value;
|
|
120
121
|
}
|
|
121
|
-
|
|
122
|
+
function parseMetadataTheme(record, options) {
|
|
123
|
+
if (!Object.hasOwn(record, "theme"))
|
|
124
|
+
return undefined;
|
|
125
|
+
try {
|
|
126
|
+
return parsePrototypeTheme(record.theme, "metadata.theme", options.themeRegistry);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
if (error instanceof ThemeValidationError) {
|
|
130
|
+
throw new PrototypeMetadataError(error.path, error.reason);
|
|
131
|
+
}
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
export function parsePrototypeMetadata(value, options = {}) {
|
|
122
136
|
const record = requiredMetadataRecord(value);
|
|
137
|
+
const theme = parseMetadataTheme(record, options);
|
|
123
138
|
const metadata = {
|
|
124
139
|
title: parseTitle(record),
|
|
125
140
|
description: parseNullableText(record, "description", PROTOTYPE_DESCRIPTION_MAX_CODE_POINTS),
|
|
@@ -128,6 +143,7 @@ export function parsePrototypeMetadata(value) {
|
|
|
128
143
|
createdAt: parseTimestamp(record, "createdAt"),
|
|
129
144
|
updatedAt: parseTimestamp(record, "updatedAt"),
|
|
130
145
|
previewUrl: parsePreviewUrl(record),
|
|
146
|
+
...(theme === undefined ? {} : { theme }),
|
|
131
147
|
};
|
|
132
148
|
return deepFreeze(metadata);
|
|
133
149
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prototype-metadata.js","sourceRoot":"","sources":["../src/prototype-metadata.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC/E,OAAO,EACL,kBAAkB,EAClB,eAAe,EACf,iBAAiB,GAElB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,qCAAqC,EACrC,+BAA+B,GAChC,MAAM,sBAAsB,CAAC;AAC9B,MAAM,eAAe,GAAG,WAAW,CAAC;AAiBpC,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IACtC,IAAI,CAAS;IAEtB,YAAY,IAAY,EAAE,OAAe;QACvC,KAAK,CAAC,iCAAiC,IAAI,KAAK,OAAO,E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OAAO,CAAC,CAAC;IAClD,MAAM,QAAQ,GAAG;QACf,KAAK,EAAE,UAAU,CAAC,MAAM,CAAC;QACzB,WAAW,EAAE,iBAAiB,CAC5B,MAAM,EACN,aAAa,EACb,qCAAqC,CACtC;QACD,WAAW,EAAE,gBAAgB,CAAC,MAAM,CAAC;QACrC,UAAU,EAAE,iBAAiB,CAC3B,MAAM,EACN,YAAY,EACZ,+BAA+B,CAChC;QACD,SAAS,EAAE,cAAc,CAAC,MAAM,EAAE,WAAW,CAAC;QAC9C,SAAS,EAAE,cAAc,CAAC,MAAM,EAAE,WAAW,CAAC;QAC9C,UAAU,EAAE,eAAe,CAAC,MAAM,CAAC;QACnC,GAAG,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC;KACd,CAAC;IAE9B,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC;AAC9B,CAAC;AAED,MAAM,UAAU,sBAAsB,CACpC,QAA2B,EAC3B,GAAiB;IAEjB,MAAM,kBAAkB,GAAG,sBAAsB,CAAC,QAAQ,CAAC,CAAC;IAC5D,MAAM,aAAa,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAC;IAC9C,IAAI,kBAAkB,CAAC,WAAW,KAAK,aAAa,CAAC,KAAK,EAAE,CAAC;QAC3D,MAAM,IAAI,sBAAsB,CAC9B,sBAAsB,EACtB,sBAAsB,CACvB,CAAC;IACJ,CAAC;IAED,OAAO,UAAU,CAAC;QAChB,GAAG,kBAAkB;QACrB,GAAG,EAAE,aAAa;QAClB,GAAG,EAAE,eAAe,CAAC,aAAa,CAAC;KACpC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { asRecord, deepFreeze, readRequiredString } from \"./internal/utils.js\";\nimport {\n definePrototypeRef,\n getPrototypeUrl,\n PrototypeRefError,\n type PrototypeRef,\n} from \"./prototype-ref.js\";\nimport {\n PROTOTYPE_DESCRIPTION_MAX_CODE_POINTS,\n PROTOTYPE_TITLE_MAX_CODE_POINTS,\n} from \"./lifecycle-input.js\";\nimport {\n parsePrototypeTheme,\n ThemeValidationError,\n type PrototypeTheme,\n type ThemeRegistry,\n} from \"./theme.js\";\nconst VALIDATION_SLUG = \"prototype\";\n\nexport type PrototypeMetadata = {\n title: string;\n description: string | null;\n authorLogin: string;\n authorName: string | null;\n createdAt: string;\n updatedAt: string;\n previewUrl: string | null;\n theme?: PrototypeTheme;\n};\n\nexport type PrototypeSummary = PrototypeMetadata & {\n ref: PrototypeRef;\n url: string;\n};\n\nexport class PrototypeMetadataError extends Error {\n readonly path: string;\n\n constructor(path: string, message: string) {\n super(`Invalid prototype metadata at ${path}: ${message}`);\n this.name = \"PrototypeMetadataError\";\n this.path = path;\n }\n}\n\nfunction codePointLength(value: string) {\n return Array.from(value).length;\n}\n\nfunction requiredMetadataRecord(value: unknown) {\n const record = asRecord(value);\n if (record === null) {\n throw new PrototypeMetadataError(\"metadata\", \"must be an object\");\n }\n\n return record;\n}\n\nfunction parseTitle(record: Record<string, unknown>) {\n const result = readRequiredString(record, \"title\");\n if (result.status === \"missing\") {\n throw new PrototypeMetadataError(\"metadata.title\", \"is required\");\n }\n if (result.status === \"invalid\") {\n throw new PrototypeMetadataError(\"metadata.title\", \"must be a string\");\n }\n\n const title = result.value.trim();\n if (title.length === 0) {\n throw new PrototypeMetadataError(\"metadata.title\", \"must not be empty\");\n }\n if (codePointLength(title) > PROTOTYPE_TITLE_MAX_CODE_POINTS) {\n throw new PrototypeMetadataError(\n \"metadata.title\",\n `must be at most ${PROTOTYPE_TITLE_MAX_CODE_POINTS} Unicode code points`,\n );\n }\n\n return title;\n}\n\nfunction parseNullableText(\n record: Record<string, unknown>,\n key: \"description\" | \"authorName\",\n maximumCodePoints: number,\n) {\n if (!Object.hasOwn(record, key)) return null;\n\n const value = record[key];\n if (value === null) return null;\n if (typeof value !== \"string\") {\n throw new PrototypeMetadataError(\n `metadata.${key}`,\n \"must be a string or null\",\n );\n }\n\n const normalized = value.trim();\n if (normalized.length === 0) return null;\n if (codePointLength(normalized) > maximumCodePoints) {\n throw new PrototypeMetadataError(\n `metadata.${key}`,\n `must be at most ${maximumCodePoints} Unicode code points`,\n );\n }\n\n return normalized;\n}\n\nfunction parseAuthorLogin(record: Record<string, unknown>) {\n const result = readRequiredString(record, \"authorLogin\");\n if (result.status === \"missing\") {\n throw new PrototypeMetadataError(\"metadata.authorLogin\", \"is required\");\n }\n if (result.status === \"invalid\") {\n throw new PrototypeMetadataError(\n \"metadata.authorLogin\",\n \"must be a string\",\n );\n }\n\n try {\n return definePrototypeRef({\n login: result.value,\n slug: VALIDATION_SLUG,\n }).login;\n } catch (error) {\n if (error instanceof PrototypeRefError) {\n throw new PrototypeMetadataError(\n \"metadata.authorLogin\",\n \"must be a valid login segment\",\n );\n }\n throw error;\n }\n}\n\nfunction parseTimestamp(\n record: Record<string, unknown>,\n key: \"createdAt\" | \"updatedAt\",\n) {\n const result = readRequiredString(record, key);\n if (result.status === \"missing\") {\n throw new PrototypeMetadataError(`metadata.${key}`, \"is required\");\n }\n if (result.status === \"invalid\") {\n throw new PrototypeMetadataError(`metadata.${key}`, \"must be a string\");\n }\n\n try {\n if (new Date(result.value).toISOString() !== result.value) {\n throw new Error(\"noncanonical timestamp\");\n }\n } catch {\n throw new PrototypeMetadataError(\n `metadata.${key}`,\n \"must be a canonical ISO-8601 timestamp\",\n );\n }\n\n return result.value;\n}\n\nfunction parsePreviewUrl(record: Record<string, unknown>) {\n if (!Object.hasOwn(record, \"previewUrl\")) return null;\n\n const value = record.previewUrl;\n if (value === null) return null;\n if (typeof value !== \"string\") {\n throw new PrototypeMetadataError(\n \"metadata.previewUrl\",\n \"must be a string or null\",\n );\n }\n if (value.length === 0 || value !== value.trim() || /\\s/u.test(value)) {\n throw new PrototypeMetadataError(\n \"metadata.previewUrl\",\n \"must be an absolute HTTPS URL without credentials\",\n );\n }\n\n try {\n const url = new URL(value);\n if (\n url.protocol !== \"https:\" ||\n url.username.length > 0 ||\n url.password.length > 0\n ) {\n throw new Error(\"unsupported preview URL\");\n }\n } catch {\n throw new PrototypeMetadataError(\n \"metadata.previewUrl\",\n \"must be an absolute HTTPS URL without credentials\",\n );\n }\n\n return value;\n}\n\nexport type PrototypeMetadataParseOptions = {\n themeRegistry?: ThemeRegistry;\n};\n\nfunction parseMetadataTheme(\n record: Record<string, unknown>,\n options: PrototypeMetadataParseOptions,\n) {\n if (!Object.hasOwn(record, \"theme\")) return undefined;\n\n try {\n return parsePrototypeTheme(\n record.theme,\n \"metadata.theme\",\n options.themeRegistry,\n );\n } catch (error) {\n if (error instanceof ThemeValidationError) {\n throw new PrototypeMetadataError(error.path, error.reason);\n }\n throw error;\n }\n}\n\nexport function parsePrototypeMetadata(\n value: unknown,\n options: PrototypeMetadataParseOptions = {},\n): PrototypeMetadata {\n const record = requiredMetadataRecord(value);\n const theme = parseMetadataTheme(record, options);\n const metadata = {\n title: parseTitle(record),\n description: parseNullableText(\n record,\n \"description\",\n PROTOTYPE_DESCRIPTION_MAX_CODE_POINTS,\n ),\n authorLogin: parseAuthorLogin(record),\n authorName: parseNullableText(\n record,\n \"authorName\",\n PROTOTYPE_TITLE_MAX_CODE_POINTS,\n ),\n createdAt: parseTimestamp(record, \"createdAt\"),\n updatedAt: parseTimestamp(record, \"updatedAt\"),\n previewUrl: parsePreviewUrl(record),\n ...(theme === undefined ? {} : { theme }),\n } satisfies PrototypeMetadata;\n\n return deepFreeze(metadata);\n}\n\nexport function definePrototypeSummary(\n metadata: PrototypeMetadata,\n ref: PrototypeRef,\n): PrototypeSummary {\n const normalizedMetadata = parsePrototypeMetadata(metadata);\n const normalizedRef = definePrototypeRef(ref);\n if (normalizedMetadata.authorLogin !== normalizedRef.login) {\n throw new PrototypeMetadataError(\n \"metadata.authorLogin\",\n \"must match ref.login\",\n );\n }\n\n return deepFreeze({\n ...normalizedMetadata,\n ref: normalizedRef,\n url: getPrototypeUrl(normalizedRef),\n });\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prototype-ref.d.ts","sourceRoot":"","sources":["../src/prototype-ref.ts"],"names":[],"mappings":"AAoBA,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,YAAY,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAIxC;CACF;AAgCD,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,YAAY,CAiC/D;AAED,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAiClE;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,YAAY,GAAG,MAAM,CAGzD;AAgBD,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI,CA2BxE"}
|
|
1
|
+
{"version":3,"file":"prototype-ref.d.ts","sourceRoot":"","sources":["../src/prototype-ref.ts"],"names":[],"mappings":"AAoBA,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,YAAY,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAIxC;CACF;AAgCD,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,YAAY,CAiC/D;AAED,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAiClE;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,YAAY,GAAG,MAAM,CAGzD;AAgBD,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI,CA2BxE","sourcesContent":["import {\n asRecord,\n findUnknownKey,\n readRequiredString,\n} from \"./internal/utils.js\";\n\nconst LOGIN_PATTERN = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/;\nconst SLUG_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;\nconst MAX_LOGIN_LENGTH = 39;\nconst MAX_SLUG_LENGTH = 63;\nconst RESERVED_TOP_LEVEL_ROUTE_SEGMENTS = new Set([\n \"api\",\n \"auth\",\n \"login\",\n \"_next\",\n]);\nconst APOSTROPHE_PATTERN = /[\\u0027\\u2018\\u2019\\u02BC]/gu;\nconst COMBINING_MARK_PATTERN = /\\p{M}/gu;\nconst UNSUPPORTED_SLUG_RUN_PATTERN = /[^a-z0-9-]+/g;\n\nexport type PrototypeRef = {\n login: string;\n slug: string;\n};\n\nexport class PrototypeRefError extends Error {\n readonly path: string;\n\n constructor(path: string, message: string) {\n super(`Invalid prototype reference at ${path}: ${message}`);\n this.name = \"PrototypeRefError\";\n this.path = path;\n }\n}\n\nfunction validateLogin(login: string) {\n if (\n login.length === 0 ||\n login.length > MAX_LOGIN_LENGTH ||\n !LOGIN_PATTERN.test(login)\n ) {\n throw new PrototypeRefError(\n \"prototypeRef.login\",\n `must be an alphanumeric GitHub-style segment of at most ${MAX_LOGIN_LENGTH} characters with no leading or trailing hyphen`,\n );\n }\n\n return login;\n}\n\nfunction validateSlug(slug: string) {\n if (\n slug.length === 0 ||\n slug.length > MAX_SLUG_LENGTH ||\n !SLUG_PATTERN.test(slug)\n ) {\n throw new PrototypeRefError(\n \"prototypeRef.slug\",\n `must be lowercase kebab-case of at most ${MAX_SLUG_LENGTH} characters`,\n );\n }\n\n return slug;\n}\n\nexport function definePrototypeRef(value: unknown): PrototypeRef {\n const record = asRecord(value);\n if (record === null) {\n throw new PrototypeRefError(\"prototypeRef\", \"must be an object\");\n }\n\n const unknownKey = findUnknownKey(record, [\"login\", \"slug\"]);\n if (unknownKey) {\n throw new PrototypeRefError(\n `prototypeRef.${unknownKey}`,\n \"is not supported\",\n );\n }\n\n const loginResult = readRequiredString(record, \"login\");\n if (loginResult.status === \"missing\") {\n throw new PrototypeRefError(\"prototypeRef.login\", \"is required\");\n }\n if (loginResult.status === \"invalid\") {\n throw new PrototypeRefError(\"prototypeRef.login\", \"must be a string\");\n }\n\n const slugResult = readRequiredString(record, \"slug\");\n if (slugResult.status === \"missing\") {\n throw new PrototypeRefError(\"prototypeRef.slug\", \"is required\");\n }\n if (slugResult.status === \"invalid\") {\n throw new PrototypeRefError(\"prototypeRef.slug\", \"must be a string\");\n }\n\n const login = validateLogin(loginResult.value);\n const slug = validateSlug(slugResult.value);\n return Object.freeze({ login, slug });\n}\n\nexport function slugifyPrototypeTitle(title: string): string | null {\n if (typeof title !== \"string\") {\n throw new PrototypeRefError(\"title\", \"must be a string\");\n }\n\n const normalized = title\n .normalize(\"NFKD\")\n .replace(COMBINING_MARK_PATTERN, \"\")\n .replace(APOSTROPHE_PATTERN, \"\")\n .replaceAll(\"&\", \"and\")\n .toLowerCase()\n .replace(UNSUPPORTED_SLUG_RUN_PATTERN, \"-\")\n .replace(/-+/g, \"-\")\n .replace(/^-|-$/g, \"\");\n\n const tokens = normalized.split(\"-\").filter(Boolean);\n if (tokens.length === 0) return null;\n\n let slug = \"\";\n for (const token of tokens) {\n const candidate = slug.length === 0 ? token : `${slug}-${token}`;\n if (candidate.length <= MAX_SLUG_LENGTH) {\n slug = candidate;\n continue;\n }\n\n if (slug.length > 0) break;\n\n slug = token.slice(0, MAX_SLUG_LENGTH);\n break;\n }\n\n return slug.length === 0 ? null : slug;\n}\n\nexport function getPrototypeUrl(ref: PrototypeRef): string {\n const normalized = definePrototypeRef(ref);\n return `/${normalized.login}/${normalized.slug}`;\n}\n\nfunction hasUnsafePathInput(pathname: string) {\n const hasControlCharacter = [...pathname].some((character) => {\n const codePoint = character.codePointAt(0) ?? 0;\n return codePoint < 0x20 || codePoint === 0x7f;\n });\n\n return (\n pathname.includes(\"//\") ||\n /[?#%\\\\]/u.test(pathname) ||\n /\\s/u.test(pathname) ||\n hasControlCharacter\n );\n}\n\nexport function parsePrototypePath(pathname: string): PrototypeRef | null {\n if (typeof pathname !== \"string\" || !pathname.startsWith(\"/\")) return null;\n if (hasUnsafePathInput(pathname)) return null;\n\n const parts = pathname.slice(1).split(\"/\");\n const [login, slug] = parts;\n if (\n parts.length < 2 ||\n login === undefined ||\n login === \"\" ||\n slug === \"\" ||\n slug === undefined\n ) {\n return null;\n }\n\n if (parts.some((part) => part === \".\" || part === \"..\")) return null;\n if (RESERVED_TOP_LEVEL_ROUTE_SEGMENTS.has(login.toLowerCase())) {\n return null;\n }\n\n try {\n return definePrototypeRef({ login, slug });\n } catch (error) {\n if (error instanceof PrototypeRefError) return null;\n throw error;\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prototype-ref.js","sourceRoot":"","sources":["../src/prototype-ref.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,cAAc,EACd,kBAAkB,GACnB,MAAM,qBAAqB,CAAC;AAE7B,MAAM,aAAa,GAAG,iDAAiD,CAAC;AACxE,MAAM,YAAY,GAAG,4BAA4B,CAAC;AAClD,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAC5B,MAAM,eAAe,GAAG,EAAE,CAAC;AAC3B,MAAM,iCAAiC,GAAG,IAAI,GAAG,CAAC;IAChD,KAAK;IACL,MAAM;IACN,OAAO;IACP,OAAO;CACR,CAAC,CAAC;AACH,MAAM,kBAAkB,GAAG,8BAA8B,CAAC;AAC1D,MAAM,sBAAsB,GAAG,SAAS,CAAC;AACzC,MAAM,4BAA4B,GAAG,cAAc,CAAC;AAOpD,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IACjC,IAAI,CAAS;IAEtB,YAAY,IAAY,EAAE,OAAe;QACvC,KAAK,CAAC,kCAAkC,IAAI,KAAK,OAAO,EAAE,CAAC,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AC,GAAG,CAAC,CAAC;IAC3C,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,GAAG,KAAK,CAAC;IAC5B,IACE,KAAK,CAAC,MAAM,GAAG,CAAC;QAChB,KAAK,KAAK,SAAS;QACnB,KAAK,KAAK,EAAE;QACZ,IAAI,KAAK,EAAE;QACX,IAAI,KAAK,SAAS,EAClB,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,GAAG,IAAI,IAAI,KAAK,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IACrE,IAAI,iCAAiC,CAAC,GAAG,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,EAAE,CAAC;QAC/D,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,CAAC;QACH,OAAO,kBAAkB,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAC7C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,iBAAiB;YAAE,OAAO,IAAI,CAAC;QACpD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import {\n asRecord,\n findUnknownKey,\n readRequiredString,\n} from \"./internal/utils.js\";\n\nconst LOGIN_PATTERN = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/;\nconst SLUG_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;\nconst MAX_LOGIN_LENGTH = 39;\nconst MAX_SLUG_LENGTH = 63;\nconst RESERVED_TOP_LEVEL_ROUTE_SEGMENTS = new Set([\n \"api\",\n \"auth\",\n \"login\",\n \"_next\",\n]);\nconst APOSTROPHE_PATTERN = /[\\u0027\\u2018\\u2019\\u02BC]/gu;\nconst COMBINING_MARK_PATTERN = /\\p{M}/gu;\nconst UNSUPPORTED_SLUG_RUN_PATTERN = /[^a-z0-9-]+/g;\n\nexport type PrototypeRef = {\n login: string;\n slug: string;\n};\n\nexport class PrototypeRefError extends Error {\n readonly path: string;\n\n constructor(path: string, message: string) {\n super(`Invalid prototype reference at ${path}: ${message}`);\n this.name = \"PrototypeRefError\";\n this.path = path;\n }\n}\n\nfunction validateLogin(login: string) {\n if (\n login.length === 0 ||\n login.length > MAX_LOGIN_LENGTH ||\n !LOGIN_PATTERN.test(login)\n ) {\n throw new PrototypeRefError(\n \"prototypeRef.login\",\n `must be an alphanumeric GitHub-style segment of at most ${MAX_LOGIN_LENGTH} characters with no leading or trailing hyphen`,\n );\n }\n\n return login;\n}\n\nfunction validateSlug(slug: string) {\n if (\n slug.length === 0 ||\n slug.length > MAX_SLUG_LENGTH ||\n !SLUG_PATTERN.test(slug)\n ) {\n throw new PrototypeRefError(\n \"prototypeRef.slug\",\n `must be lowercase kebab-case of at most ${MAX_SLUG_LENGTH} characters`,\n );\n }\n\n return slug;\n}\n\nexport function definePrototypeRef(value: unknown): PrototypeRef {\n const record = asRecord(value);\n if (record === null) {\n throw new PrototypeRefError(\"prototypeRef\", \"must be an object\");\n }\n\n const unknownKey = findUnknownKey(record, [\"login\", \"slug\"]);\n if (unknownKey) {\n throw new PrototypeRefError(\n `prototypeRef.${unknownKey}`,\n \"is not supported\",\n );\n }\n\n const loginResult = readRequiredString(record, \"login\");\n if (loginResult.status === \"missing\") {\n throw new PrototypeRefError(\"prototypeRef.login\", \"is required\");\n }\n if (loginResult.status === \"invalid\") {\n throw new PrototypeRefError(\"prototypeRef.login\", \"must be a string\");\n }\n\n const slugResult = readRequiredString(record, \"slug\");\n if (slugResult.status === \"missing\") {\n throw new PrototypeRefError(\"prototypeRef.slug\", \"is required\");\n }\n if (slugResult.status === \"invalid\") {\n throw new PrototypeRefError(\"prototypeRef.slug\", \"must be a string\");\n }\n\n const login = validateLogin(loginResult.value);\n const slug = validateSlug(slugResult.value);\n return Object.freeze({ login, slug });\n}\n\nexport function slugifyPrototypeTitle(title: string): string | null {\n if (typeof title !== \"string\") {\n throw new PrototypeRefError(\"title\", \"must be a string\");\n }\n\n const normalized = title\n .normalize(\"NFKD\")\n .replace(COMBINING_MARK_PATTERN, \"\")\n .replace(APOSTROPHE_PATTERN, \"\")\n .replaceAll(\"&\", \"and\")\n .toLowerCase()\n .replace(UNSUPPORTED_SLUG_RUN_PATTERN, \"-\")\n .replace(/-+/g, \"-\")\n .replace(/^-|-$/g, \"\");\n\n const tokens = normalized.split(\"-\").filter(Boolean);\n if (tokens.length === 0) return null;\n\n let slug = \"\";\n for (const token of tokens) {\n const candidate = slug.length === 0 ? token : `${slug}-${token}`;\n if (candidate.length <= MAX_SLUG_LENGTH) {\n slug = candidate;\n continue;\n }\n\n if (slug.length > 0) break;\n\n slug = token.slice(0, MAX_SLUG_LENGTH);\n break;\n }\n\n return slug.length === 0 ? null : slug;\n}\n\nexport function getPrototypeUrl(ref: PrototypeRef): string {\n const normalized = definePrototypeRef(ref);\n return `/${normalized.login}/${normalized.slug}`;\n}\n\nfunction hasUnsafePathInput(pathname: string) {\n const hasControlCharacter = [...pathname].some((character) => {\n const codePoint = character.codePointAt(0) ?? 0;\n return codePoint < 0x20 || codePoint === 0x7f;\n });\n\n return (\n pathname.includes(\"//\") ||\n /[?#%\\\\]/u.test(pathname) ||\n /\\s/u.test(pathname) ||\n hasControlCharacter\n );\n}\n\nexport function parsePrototypePath(pathname: string): PrototypeRef | null {\n if (typeof pathname !== \"string\" || !pathname.startsWith(\"/\")) return null;\n if (hasUnsafePathInput(pathname)) return null;\n\n const parts = pathname.slice(1).split(\"/\");\n const [login, slug] = parts;\n if (\n parts.length < 2 ||\n login === undefined ||\n login === \"\" ||\n slug === \"\" ||\n slug === undefined\n ) {\n return null;\n }\n\n if (parts.some((part) => part === \".\" || part === \"..\")) return null;\n if (RESERVED_TOP_LEVEL_ROUTE_SEGMENTS.has(login.toLowerCase())) {\n return null;\n }\n\n try {\n return definePrototypeRef({ login, slug });\n } catch (error) {\n if (error instanceof PrototypeRefError) return null;\n throw error;\n }\n}\n"]}
|
package/dist/theme.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export declare const COLOR_SCHEMES: readonly ["light", "dark", "system"];
|
|
2
|
+
export type ColorScheme = (typeof COLOR_SCHEMES)[number];
|
|
3
|
+
export type ThemeDefinition = {
|
|
4
|
+
id: string;
|
|
5
|
+
label: string;
|
|
6
|
+
allowedColorSchemes: readonly ColorScheme[];
|
|
7
|
+
};
|
|
8
|
+
export type ThemeRegistry = {
|
|
9
|
+
definitions: readonly ThemeDefinition[];
|
|
10
|
+
defaultTheme: string;
|
|
11
|
+
defaultColorScheme: ColorScheme;
|
|
12
|
+
};
|
|
13
|
+
export type PrototypeTheme = {
|
|
14
|
+
defaultTheme: string;
|
|
15
|
+
allowedThemes: readonly string[];
|
|
16
|
+
defaultColorScheme: ColorScheme;
|
|
17
|
+
allowedColorSchemes: readonly ColorScheme[];
|
|
18
|
+
};
|
|
19
|
+
export declare class ThemeValidationError extends Error {
|
|
20
|
+
readonly path: string;
|
|
21
|
+
constructor(path: string, message: string);
|
|
22
|
+
readonly reason: string;
|
|
23
|
+
}
|
|
24
|
+
export declare function parseThemeRegistry(value: unknown, path?: string): ThemeRegistry;
|
|
25
|
+
export declare function createDefaultPrototypeTheme(registry: ThemeRegistry): PrototypeTheme;
|
|
26
|
+
export type PrototypeThemeSelection = {
|
|
27
|
+
defaultTheme: string;
|
|
28
|
+
allowThemeSwitching: boolean;
|
|
29
|
+
defaultColorScheme: ColorScheme;
|
|
30
|
+
allowColorSchemeSwitching: boolean;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Expands an author-facing selection (a chosen default plus two
|
|
34
|
+
* switching-permission flags) into a fully validated `PrototypeTheme`
|
|
35
|
+
* policy. This is the shape a create/edit authoring UI submits; it is
|
|
36
|
+
* simpler than hand-authoring `allowedThemes`/`allowedColorSchemes` arrays
|
|
37
|
+
* directly, since "allow switching" always means every registered theme (or
|
|
38
|
+
* every color scheme supported by the allowed themes) rather than an
|
|
39
|
+
* arbitrary subset.
|
|
40
|
+
*/
|
|
41
|
+
export declare function parsePrototypeThemeSelection(value: unknown, registry: ThemeRegistry, path?: string): PrototypeTheme;
|
|
42
|
+
export declare function parsePrototypeTheme(value: unknown, path?: string, registry?: ThemeRegistry): PrototypeTheme;
|
|
43
|
+
//# sourceMappingURL=theme.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme.d.ts","sourceRoot":"","sources":["../src/theme.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,aAAa,YAAI,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAU,CAAC;AAElE,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC;AAEzD,MAAM,MAAM,eAAe,GAAG;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,mBAAmB,EAAE,SAAS,WAAW,EAAE,CAAC;CAC7C,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,WAAW,EAAE,SAAS,eAAe,EAAE,CAAC;IACxC,YAAY,EAAE,MAAM,CAAC;IACrB,kBAAkB,EAAE,WAAW,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,SAAS,MAAM,EAAE,CAAC;IACjC,kBAAkB,EAAE,WAAW,CAAC;IAChC,mBAAmB,EAAE,SAAS,WAAW,EAAE,CAAC;CAC7C,CAAC;AAKF,qBAAa,oBAAqB,SAAQ,KAAK;IAC7C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,YAAY,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAKxC;IAED,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAyGD,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,OAAO,EACd,IAAI,SAAW,GACd,aAAa,CA8Df;AAeD,wBAAgB,2BAA2B,CACzC,QAAQ,EAAE,aAAa,GACtB,cAAc,CAmBhB;AAED,MAAM,MAAM,uBAAuB,GAAG;IACpC,YAAY,EAAE,MAAM,CAAC;IACrB,mBAAmB,EAAE,OAAO,CAAC;IAC7B,kBAAkB,EAAE,WAAW,CAAC;IAChC,yBAAyB,EAAE,OAAO,CAAC;CACpC,CAAC;AAUF;;;;;;;;GAQG;AACH,wBAAgB,4BAA4B,CAC1C,KAAK,EAAE,OAAO,EACd,QAAQ,EAAE,aAAa,EACvB,IAAI,SAAU,GACb,cAAc,CAqDhB;AAED,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,OAAO,EACd,IAAI,SAAU,EACd,QAAQ,CAAC,EAAE,aAAa,GACvB,cAAc,CAsFhB","sourcesContent":["import { asRecord, deepFreeze, findUnknownKey } from \"./internal/utils.js\";\n\nexport const COLOR_SCHEMES = [\"light\", \"dark\", \"system\"] as const;\n\nexport type ColorScheme = (typeof COLOR_SCHEMES)[number];\n\nexport type ThemeDefinition = {\n id: string;\n label: string;\n allowedColorSchemes: readonly ColorScheme[];\n};\n\nexport type ThemeRegistry = {\n definitions: readonly ThemeDefinition[];\n defaultTheme: string;\n defaultColorScheme: ColorScheme;\n};\n\nexport type PrototypeTheme = {\n defaultTheme: string;\n allowedThemes: readonly string[];\n defaultColorScheme: ColorScheme;\n allowedColorSchemes: readonly ColorScheme[];\n};\n\nconst THEME_ID_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;\nconst MAX_THEME_ID_LENGTH = 63;\n\nexport class ThemeValidationError extends Error {\n readonly path: string;\n\n constructor(path: string, message: string) {\n super(`Invalid theme configuration at ${path}: ${message}`);\n this.name = \"ThemeValidationError\";\n this.path = path;\n this.reason = message;\n }\n\n readonly reason: string;\n}\n\nfunction themeRecord(value: unknown, path: string) {\n const record = asRecord(value);\n if (record === null) {\n throw new ThemeValidationError(path, \"must be an object\");\n }\n\n return record;\n}\n\nfunction rejectUnknownKeys(\n record: Record<string, unknown>,\n allowedKeys: readonly string[],\n path: string,\n) {\n const unknownKey = findUnknownKey(record, allowedKeys);\n if (unknownKey) {\n throw new ThemeValidationError(`${path}.${unknownKey}`, \"is not supported\");\n }\n}\n\nfunction parseThemeId(value: unknown, path: string) {\n if (typeof value !== \"string\") {\n throw new ThemeValidationError(path, \"must be a string\");\n }\n if (\n value.length === 0 ||\n value.length > MAX_THEME_ID_LENGTH ||\n !THEME_ID_PATTERN.test(value)\n ) {\n throw new ThemeValidationError(\n path,\n \"must contain lowercase letters, numbers, and single hyphens between segments\",\n );\n }\n\n return value;\n}\n\nfunction parseLabel(value: unknown, path: string) {\n if (typeof value !== \"string\") {\n throw new ThemeValidationError(path, \"must be a string\");\n }\n\n const label = value.trim();\n if (label.length === 0) {\n throw new ThemeValidationError(path, \"must not be empty\");\n }\n\n return label;\n}\n\nfunction parseColorScheme(value: unknown, path: string): ColorScheme {\n if (\n typeof value !== \"string\" ||\n !(COLOR_SCHEMES as readonly string[]).includes(value)\n ) {\n throw new ThemeValidationError(\n path,\n `must be one of ${COLOR_SCHEMES.join(\", \")}`,\n );\n }\n\n return value as ColorScheme;\n}\n\nfunction parseColorSchemes(value: unknown, path: string) {\n if (!Array.isArray(value) || value.length === 0) {\n throw new ThemeValidationError(path, \"must be a non-empty array\");\n }\n\n const schemes = value.map((scheme, index) =>\n parseColorScheme(scheme, `${path}[${index}]`),\n );\n if (new Set(schemes).size !== schemes.length) {\n throw new ThemeValidationError(path, \"must not contain duplicate values\");\n }\n\n return schemes;\n}\n\nfunction parseThemeDefinition(\n value: unknown,\n index: number,\n definitionsPath: string,\n): ThemeDefinition {\n const path = `${definitionsPath}[${index}]`;\n const record = themeRecord(value, path);\n rejectUnknownKeys(record, [\"id\", \"label\", \"allowedColorSchemes\"], path);\n\n return {\n id: parseThemeId(record.id, `${path}.id`),\n label: parseLabel(record.label, `${path}.label`),\n allowedColorSchemes: parseColorSchemes(\n record.allowedColorSchemes,\n `${path}.allowedColorSchemes`,\n ),\n };\n}\n\nfunction findTheme(definitions: readonly ThemeDefinition[], id: string) {\n return definitions.find((definition) => definition.id === id);\n}\n\nexport function parseThemeRegistry(\n value: unknown,\n path = \"themes\",\n): ThemeRegistry {\n const record = themeRecord(value, path);\n rejectUnknownKeys(\n record,\n [\"definitions\", \"defaultTheme\", \"defaultColorScheme\"],\n path,\n );\n\n if (!Array.isArray(record.definitions) || record.definitions.length === 0) {\n throw new ThemeValidationError(\n `${path}.definitions`,\n \"must be a non-empty array\",\n );\n }\n\n const definitions = record.definitions.map((definition, index) => {\n const parsed = parseThemeDefinition(\n definition,\n index,\n `${path}.definitions`,\n );\n return parsed;\n });\n\n if (\n new Set(definitions.map((definition) => definition.id)).size !==\n definitions.length\n ) {\n throw new ThemeValidationError(\n `${path}.definitions`,\n \"must not contain duplicate theme IDs\",\n );\n }\n\n const defaultTheme = parseThemeId(\n record.defaultTheme,\n `${path}.defaultTheme`,\n );\n const defaultDefinition = findTheme(definitions, defaultTheme);\n if (!defaultDefinition) {\n throw new ThemeValidationError(\n `${path}.defaultTheme`,\n \"must reference a defined theme\",\n );\n }\n\n const defaultColorScheme = parseColorScheme(\n record.defaultColorScheme,\n `${path}.defaultColorScheme`,\n );\n if (!defaultDefinition.allowedColorSchemes.includes(defaultColorScheme)) {\n throw new ThemeValidationError(\n `${path}.defaultColorScheme`,\n `must be allowed by the default theme (${defaultTheme})`,\n );\n }\n\n return deepFreeze({\n definitions,\n defaultTheme,\n defaultColorScheme,\n });\n}\n\nfunction parseThemeIds(value: unknown, path: string) {\n if (!Array.isArray(value) || value.length === 0) {\n throw new ThemeValidationError(path, \"must be a non-empty array\");\n }\n\n const ids = value.map((id, index) => parseThemeId(id, `${path}[${index}]`));\n if (new Set(ids).size !== ids.length) {\n throw new ThemeValidationError(path, \"must not contain duplicate values\");\n }\n\n return ids;\n}\n\nexport function createDefaultPrototypeTheme(\n registry: ThemeRegistry,\n): PrototypeTheme {\n const allowedColorSchemes = [\n ...new Set(\n registry.definitions.flatMap(\n (definition) => definition.allowedColorSchemes,\n ),\n ),\n ];\n\n return parsePrototypeTheme(\n {\n defaultTheme: registry.defaultTheme,\n allowedThemes: registry.definitions.map((definition) => definition.id),\n defaultColorScheme: registry.defaultColorScheme,\n allowedColorSchemes,\n },\n \"theme\",\n registry,\n );\n}\n\nexport type PrototypeThemeSelection = {\n defaultTheme: string;\n allowThemeSwitching: boolean;\n defaultColorScheme: ColorScheme;\n allowColorSchemeSwitching: boolean;\n};\n\nfunction parseBoolean(value: unknown, path: string): boolean {\n if (typeof value !== \"boolean\") {\n throw new ThemeValidationError(path, \"must be a boolean\");\n }\n\n return value;\n}\n\n/**\n * Expands an author-facing selection (a chosen default plus two\n * switching-permission flags) into a fully validated `PrototypeTheme`\n * policy. This is the shape a create/edit authoring UI submits; it is\n * simpler than hand-authoring `allowedThemes`/`allowedColorSchemes` arrays\n * directly, since \"allow switching\" always means every registered theme (or\n * every color scheme supported by the allowed themes) rather than an\n * arbitrary subset.\n */\nexport function parsePrototypeThemeSelection(\n value: unknown,\n registry: ThemeRegistry,\n path = \"theme\",\n): PrototypeTheme {\n const record = themeRecord(value, path);\n rejectUnknownKeys(\n record,\n [\n \"defaultTheme\",\n \"allowThemeSwitching\",\n \"defaultColorScheme\",\n \"allowColorSchemeSwitching\",\n ],\n path,\n );\n\n const defaultTheme = parseThemeId(\n record.defaultTheme,\n `${path}.defaultTheme`,\n );\n const allowThemeSwitching = parseBoolean(\n record.allowThemeSwitching,\n `${path}.allowThemeSwitching`,\n );\n const defaultColorScheme = parseColorScheme(\n record.defaultColorScheme,\n `${path}.defaultColorScheme`,\n );\n const allowColorSchemeSwitching = parseBoolean(\n record.allowColorSchemeSwitching,\n `${path}.allowColorSchemeSwitching`,\n );\n\n const allowedThemes = allowThemeSwitching\n ? registry.definitions.map((definition) => definition.id)\n : [defaultTheme];\n\n const allowedDefinitions = registry.definitions.filter((definition) =>\n allowedThemes.includes(definition.id),\n );\n\n const allowedColorSchemes = allowColorSchemeSwitching\n ? [\n ...new Set(\n allowedDefinitions.flatMap(\n (definition) => definition.allowedColorSchemes,\n ),\n ),\n ]\n : [defaultColorScheme];\n\n return parsePrototypeTheme(\n { defaultTheme, allowedThemes, defaultColorScheme, allowedColorSchemes },\n path,\n registry,\n );\n}\n\nexport function parsePrototypeTheme(\n value: unknown,\n path = \"theme\",\n registry?: ThemeRegistry,\n): PrototypeTheme {\n const record = themeRecord(value, path);\n rejectUnknownKeys(\n record,\n [\n \"defaultTheme\",\n \"allowedThemes\",\n \"defaultColorScheme\",\n \"allowedColorSchemes\",\n ],\n path,\n );\n\n const defaultTheme = parseThemeId(\n record.defaultTheme,\n `${path}.defaultTheme`,\n );\n const allowedThemes = parseThemeIds(\n record.allowedThemes,\n `${path}.allowedThemes`,\n );\n if (!allowedThemes.includes(defaultTheme)) {\n throw new ThemeValidationError(\n `${path}.defaultTheme`,\n \"must be included in allowedThemes\",\n );\n }\n\n const defaultColorScheme = parseColorScheme(\n record.defaultColorScheme,\n `${path}.defaultColorScheme`,\n );\n const allowedColorSchemes = parseColorSchemes(\n record.allowedColorSchemes,\n `${path}.allowedColorSchemes`,\n );\n if (!allowedColorSchemes.includes(defaultColorScheme)) {\n throw new ThemeValidationError(\n `${path}.defaultColorScheme`,\n \"must be included in allowedColorSchemes\",\n );\n }\n\n if (registry) {\n for (const theme of allowedThemes) {\n const definition = findTheme(registry.definitions, theme);\n if (!definition) {\n throw new ThemeValidationError(\n `${path}.allowedThemes`,\n `must reference defined theme IDs; ${theme} is not registered`,\n );\n }\n\n if (\n !allowedColorSchemes.some((scheme) =>\n definition.allowedColorSchemes.includes(scheme),\n )\n ) {\n throw new ThemeValidationError(\n `${path}.allowedColorSchemes`,\n `must include a scheme supported by theme ${theme}`,\n );\n }\n }\n\n const defaultDefinition = findTheme(registry.definitions, defaultTheme);\n if (!defaultDefinition) {\n throw new ThemeValidationError(\n `${path}.defaultTheme`,\n \"must reference a defined theme\",\n );\n }\n if (!defaultDefinition.allowedColorSchemes.includes(defaultColorScheme)) {\n throw new ThemeValidationError(\n `${path}.defaultColorScheme`,\n `must be allowed by theme ${defaultTheme}`,\n );\n }\n }\n\n return deepFreeze({\n defaultTheme,\n allowedThemes,\n defaultColorScheme,\n allowedColorSchemes,\n });\n}\n"]}
|