@flanksource/page-runtime 0.0.0-stage → 0.1.0
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/LICENSE +17 -0
- package/README.md +494 -2
- package/dist/build/README.md +96 -0
- package/dist/build/cjs-export-names.mjs +24 -0
- package/dist/build/dependencies.mjs +279 -0
- package/dist/build/dependencies.mjs.map +1 -0
- package/dist/build/dependency-cache-Zg-FGasE.mjs +2130 -0
- package/dist/build/dependency-cache-Zg-FGasE.mjs.map +1 -0
- package/dist/build/index.mjs +42 -0
- package/dist/build/index.mjs.map +1 -0
- package/dist/build/package.json +19 -0
- package/dist/build/types/activity-schema.d.ts +62 -0
- package/dist/build/types/build/barrels.d.ts +70 -0
- package/dist/build/types/build/base.d.ts +2 -0
- package/dist/build/types/build/browser-runtime-exports.d.ts +19 -0
- package/dist/build/types/build/check-fragment.d.ts +5 -0
- package/dist/build/types/build/commonjs-exports.d.ts +5 -0
- package/dist/build/types/build/convert-commonjs.d.ts +12 -0
- package/dist/build/types/build/dependencies.d.ts +6 -0
- package/dist/build/types/build/dependency-barrel-index.d.ts +5 -0
- package/dist/build/types/build/dependency-barrels.d.ts +7 -0
- package/dist/build/types/build/dependency-cache.d.ts +7 -0
- package/dist/build/types/build/dependency-optional.d.ts +4 -0
- package/dist/build/types/build/dependency-plugin.d.ts +8 -0
- package/dist/build/types/build/dependency-runtime.d.ts +20 -0
- package/dist/build/types/build/dependency-workers.d.ts +8 -0
- package/dist/build/types/build/errors.d.ts +9 -0
- package/dist/build/types/build/fragment.d.ts +13 -0
- package/dist/build/types/build/index.d.ts +7 -0
- package/dist/build/types/build/library-runtime.d.ts +21 -0
- package/dist/build/types/build/module-utils.d.ts +3 -0
- package/dist/build/types/build/recipe-dependencies.d.ts +2 -0
- package/dist/build/types/build/runtime-entries.d.ts +4 -0
- package/dist/build/types/build/runtime-layer.d.ts +17 -0
- package/dist/build/types/build/trace.d.ts +140 -0
- package/dist/build/types/design-schema.d.ts +98 -0
- package/dist/build/types/host/agent-source.d.ts +2 -0
- package/dist/build/types/host/agent-validation.d.ts +3 -0
- package/dist/build/types/host/design-validation.d.ts +2 -0
- package/dist/build/types/host/message-values.d.ts +8 -0
- package/dist/build/types/host/review/validation.d.ts +5 -0
- package/dist/build/types/host/validation.d.ts +6 -0
- package/dist/build/types/link/page-sources.d.ts +31 -0
- package/dist/build/types/link/types.d.ts +62 -0
- package/dist/build/types/link/validation.d.ts +9 -0
- package/dist/build/types/markers.d.ts +10 -0
- package/dist/build/types/review-schema.d.ts +60 -0
- package/dist/build/types/schema.d.ts +319 -0
- package/dist/build/types/sw/barrels.d.ts +9 -0
- package/dist/build/types/sw/compiler-plugins.d.ts +6 -0
- package/dist/build/types/sw/composition.d.ts +8 -0
- package/dist/build/types/sw/module-transform.d.ts +68 -0
- package/dist/build/types/sw/profile.d.ts +7 -0
- package/dist/build/types/sw/source-provider.d.ts +7 -0
- package/dist/build/types/sw/versions.d.ts +10 -0
- package/dist/node/catalog.mjs +1 -0
- package/dist/node/chunks/publication-RchcQvxD.mjs +229 -0
- package/dist/node/chunks/publication-RchcQvxD.mjs.map +1 -0
- package/dist/node/chunks/schema-CaRTfg2x.mjs +12 -0
- package/dist/node/chunks/schema-CaRTfg2x.mjs.map +1 -0
- package/dist/node/dev.mjs +410 -0
- package/dist/node/dev.mjs.map +1 -0
- package/dist/node/publication/cli.mjs +13 -0
- package/dist/node/publication/cli.mjs.map +1 -0
- package/dist/node/publication.mjs +1 -0
- package/dist/node/schema.mjs +1 -0
- package/dist/node/styles.mjs +49 -0
- package/dist/node/styles.mjs.map +1 -0
- package/dist/static/boot.js +40938 -0
- package/dist/static/boot.js.map +1 -0
- package/dist/static/bootstrap.html +1 -0
- package/dist/static/devtools-hook.js +118 -0
- package/dist/static/document-bundle.js +1 -0
- package/dist/static/document.js +2621 -0
- package/dist/static/editing.js +37 -0
- package/dist/static/export-compiler.js +81521 -0
- package/dist/static/export-compiler.js.map +1 -0
- package/dist/static/page-agent.js +17193 -0
- package/dist/static/page-agent.js.map +1 -0
- package/dist/static/sw.js +86112 -0
- package/dist/static/sw.js.map +1 -0
- package/package.json +110 -3
- package/scripts/build-builder.ts +27 -0
- package/scripts/build-library-runtime.ts +9 -0
- package/scripts/build-node.ts +26 -0
- package/scripts/build-static.ts +86 -0
- package/scripts/build.ts +7 -0
- package/scripts/check-fragment.ts +12 -0
- package/scripts/local-workspaces.ts +55 -0
- package/scripts/react-externals.ts +18 -0
- package/scripts/release-version.ts +47 -0
- package/scripts/run-package-build.ts +8 -0
- package/scripts/serve-packages.ts +8 -0
- package/src/activity-schema.ts +92 -0
- package/src/build/README.md +96 -0
- package/src/build/barrels.ts +306 -0
- package/src/build/base.ts +2 -0
- package/src/build/browser-runtime-exports.ts +52 -0
- package/src/build/check-fragment.ts +45 -0
- package/src/build/cjs-export-names.mjs +24 -0
- package/src/build/commonjs-exports.ts +24 -0
- package/src/build/convert-commonjs.ts +183 -0
- package/src/build/dependencies.ts +6 -0
- package/src/build/dependency-barrel-index.ts +48 -0
- package/src/build/dependency-barrels.ts +80 -0
- package/src/build/dependency-cache.ts +40 -0
- package/src/build/dependency-optional.ts +15 -0
- package/src/build/dependency-plugin.ts +58 -0
- package/src/build/dependency-runtime.ts +182 -0
- package/src/build/dependency-workers.ts +41 -0
- package/src/build/errors.ts +9 -0
- package/src/build/fragment.ts +89 -0
- package/src/build/index.ts +7 -0
- package/src/build/library-runtime.ts +101 -0
- package/src/build/module-utils.ts +11 -0
- package/src/build/recipe-dependencies.ts +14 -0
- package/src/build/runtime-entries.ts +10 -0
- package/src/build/runtime-layer.ts +99 -0
- package/src/build/trace.ts +431 -0
- package/src/design-schema.ts +25 -0
- package/src/dev/README.md +78 -0
- package/src/dev/catalog.ts +79 -0
- package/src/dev/cli.ts +25 -0
- package/src/dev/index.ts +118 -0
- package/src/dev/runtime-origin.ts +63 -0
- package/src/editing.ts +25 -0
- package/src/form/entry-source.ts +68 -0
- package/src/form/form-page.ts +63 -0
- package/src/form/index.ts +2 -0
- package/src/form/source-update.ts +17 -0
- package/src/frame/activity.browser.ts +34 -0
- package/src/frame/activity.ts +52 -0
- package/src/frame/agent/README.md +47 -0
- package/src/frame/agent/agent.browser.ts +127 -0
- package/src/frame/agent/design.ts +62 -0
- package/src/frame/agent/frame-layout.ts +54 -0
- package/src/frame/agent/index.ts +151 -0
- package/src/frame/agent/overlay.ts +104 -0
- package/src/frame/agent/owner.ts +61 -0
- package/src/frame/agent/provider.tsx +14 -0
- package/src/frame/agent/react-override.ts +55 -0
- package/src/frame/agent/review.browser.ts +65 -0
- package/src/frame/agent/review.ts +108 -0
- package/src/frame/agent/testing/callsite.tsx +5 -0
- package/src/frame/agent/testing/client.ts +99 -0
- package/src/frame/agent/testing/library.ts +3 -0
- package/src/frame/agent/testing/modal.tsx +5 -0
- package/src/frame/agent/testing/review-client.ts +43 -0
- package/src/frame/agent/testing/review-server.ts +31 -0
- package/src/frame/agent/testing/server.ts +72 -0
- package/src/frame/agent/testing/vocabulary-host.ts +20 -0
- package/src/frame/agent/testing/vocabulary.tsx +10 -0
- package/src/frame/agent/vitest.browser.config.ts +6 -0
- package/src/frame/agent/vocabulary.browser.ts +30 -0
- package/src/frame/agent/vocabulary.ts +13 -0
- package/src/frame/agent.ts +3 -0
- package/src/frame/boot-styles.ts +50 -0
- package/src/frame/boot.ts +269 -0
- package/src/frame/bridge.ts +27 -0
- package/src/frame/devtools-hook.ts +1 -0
- package/src/frame/document-assets.ts +76 -0
- package/src/frame/document-entry.ts +68 -0
- package/src/frame/document-modules.ts +113 -0
- package/src/frame/document-mount.ts +32 -0
- package/src/frame/entry.ts +4 -0
- package/src/frame/export-compiler.ts +18 -0
- package/src/frame/export-page.ts +61 -0
- package/src/frame/export.ts +89 -0
- package/src/frame/index.ts +8 -0
- package/src/frame/local-runtime.browser.ts +36 -0
- package/src/frame/mount-page.ts +36 -0
- package/src/frame/page-agent.ts +2 -0
- package/src/frame/readiness.ts +51 -0
- package/src/frame/render-job.ts +101 -0
- package/src/frame/review-integration.browser.ts +48 -0
- package/src/frame/runtime.browser.ts +90 -0
- package/src/frame/tailwind/README.md +49 -0
- package/src/frame/tailwind/boot-styles.browser.ts +18 -0
- package/src/frame/tailwind/cache.ts +20 -0
- package/src/frame/tailwind/load.ts +75 -0
- package/src/frame/tailwind/recipe.ts +76 -0
- package/src/frame/tailwind/runtime.ts +113 -0
- package/src/frame/tailwind/store.ts +55 -0
- package/src/frame/tailwind/stylesheets.ts +42 -0
- package/src/frame/tailwind/tailwind.browser.ts +71 -0
- package/src/frame/tailwind/testing/client.ts +133 -0
- package/src/frame/tailwind/testing/compare.ts +91 -0
- package/src/frame/tailwind/testing/pages.ts +14 -0
- package/src/frame/tailwind/testing/server.ts +89 -0
- package/src/frame/tailwind/vitest.browser.config.ts +7 -0
- package/src/frame/testing/boot-layout.ts +3 -0
- package/src/frame/testing/boot-page.ts +3 -0
- package/src/frame/testing/host.ts +78 -0
- package/src/frame/testing/local-host.tsx +33 -0
- package/src/frame/testing/server.ts +135 -0
- package/src/host/BuildLog.tsx +28 -0
- package/src/host/DEBUG_CONSOLE.md +13 -0
- package/src/host/PageForm.tsx +63 -0
- package/src/host/PageFrame.tsx +49 -0
- package/src/host/RuntimeActivity.tsx +77 -0
- package/src/host/RuntimeDebugPanel.tsx +18 -0
- package/src/host/activity-state.ts +69 -0
- package/src/host/agent-source.ts +14 -0
- package/src/host/agent-validation.ts +73 -0
- package/src/host/bridge.ts +56 -0
- package/src/host/build-log.ts +29 -0
- package/src/host/clear-cache.ts +66 -0
- package/src/host/debug-runtime.tsx +17 -0
- package/src/host/design-validation.ts +96 -0
- package/src/host/index.ts +12 -0
- package/src/host/link/index.ts +66 -0
- package/src/host/link/transport.ts +68 -0
- package/src/host/link/watch.ts +62 -0
- package/src/host/message-values.ts +55 -0
- package/src/host/origin.ts +7 -0
- package/src/host/review/README.md +32 -0
- package/src/host/review/index.ts +43 -0
- package/src/host/review/validation.ts +55 -0
- package/src/host/runtime-diagnostics.ts +41 -0
- package/src/host/validation.ts +148 -0
- package/src/link/README.md +115 -0
- package/src/link/barrels.ts +5 -0
- package/src/link/build-cli.ts +12 -0
- package/src/link/build.ts +99 -0
- package/src/link/commonjs.ts +39 -0
- package/src/link/examples/server.ts +18 -0
- package/src/link/index.ts +6 -0
- package/src/link/link.browser.ts +66 -0
- package/src/link/local-relay.browser.ts +22 -0
- package/src/link/overrides.ts +69 -0
- package/src/link/page-sources.ts +69 -0
- package/src/link/source.ts +25 -0
- package/src/link/storage.ts +32 -0
- package/src/link/testing/client.ts +96 -0
- package/src/link/testing/folder.ts +19 -0
- package/src/link/testing/local-client.ts +30 -0
- package/src/link/testing/local-server.ts +26 -0
- package/src/link/testing/local-worker.ts +17 -0
- package/src/link/testing/server.ts +131 -0
- package/src/link/testing/worker.ts +32 -0
- package/src/link/testing-cache.ts +20 -0
- package/src/link/testing-fixtures.ts +16 -0
- package/src/link/types.ts +27 -0
- package/src/link/validation.ts +44 -0
- package/src/link/vitest.browser.config.ts +3 -0
- package/src/markers.ts +18 -0
- package/src/publication/build.ts +27 -0
- package/src/publication/fixtures/build.mjs +2 -0
- package/src/publication/index.ts +3 -0
- package/src/publication/lock.ts +32 -0
- package/src/publication/manifest.ts +14 -0
- package/src/publication/publish.ts +80 -0
- package/src/publication/runtime.ts +28 -0
- package/src/publication/schema.ts +13 -0
- package/src/review-schema.ts +17 -0
- package/src/schema.ts +175 -0
- package/src/storage/db.ts +64 -0
- package/src/styles.ts +40 -0
- package/src/sw/activity.ts +66 -0
- package/src/sw/barrels.ts +44 -0
- package/src/sw/candidates.ts +14 -0
- package/src/sw/compile.ts +92 -0
- package/src/sw/compiler-columns.ts +44 -0
- package/src/sw/compiler-internals.d.ts +8 -0
- package/src/sw/compiler-plugins.ts +18 -0
- package/src/sw/compiler.ts +93 -0
- package/src/sw/composition.ts +86 -0
- package/src/sw/content-types.ts +28 -0
- package/src/sw/converted.browser.ts +31 -0
- package/src/sw/converted.ts +64 -0
- package/src/sw/editable-sites.ts +42 -0
- package/src/sw/errors.ts +30 -0
- package/src/sw/esm-sh-fixtures/browser-client.ts +18 -0
- package/src/sw/esm-sh-fixtures/browser-server.ts +104 -0
- package/src/sw/esm-sh-fixtures/fixtures/copy-to-clipboard-entry.headers +15 -0
- package/src/sw/esm-sh-fixtures/fixtures/copy-to-clipboard-entry.mjs.txt +3 -0
- package/src/sw/esm-sh-fixtures/fixtures/copy-to-clipboard-module.headers +13 -0
- package/src/sw/esm-sh-fixtures/fixtures/copy-to-clipboard-module.mjs.txt +142 -0
- package/src/sw/esm-sh-fixtures/fixtures/react-copy-to-clipboard-entry.headers +15 -0
- package/src/sw/esm-sh-fixtures/fixtures/react-copy-to-clipboard-entry.mjs.txt +3 -0
- package/src/sw/esm-sh-fixtures/fixtures/react-copy-to-clipboard-module.headers +11 -0
- package/src/sw/esm-sh-fixtures/fixtures/react-copy-to-clipboard-module.mjs.txt +222 -0
- package/src/sw/esm-sh-fixtures/fixtures/toggle-selection-entry.headers +14 -0
- package/src/sw/esm-sh-fixtures/fixtures/toggle-selection-entry.mjs.txt +3 -0
- package/src/sw/esm-sh-fixtures/fixtures/toggle-selection-module.headers +11 -0
- package/src/sw/esm-sh-fixtures/fixtures/toggle-selection-module.mjs.txt +76 -0
- package/src/sw/esm-sh-fixtures/fixtures.ts +54 -0
- package/src/sw/esm-sh-fixtures/redact.ts +1 -0
- package/src/sw/index.ts +7 -0
- package/src/sw/integrity.ts +10 -0
- package/src/sw/legacy-storage.ts +18 -0
- package/src/sw/link-activity.ts +21 -0
- package/src/sw/link-cache.ts +19 -0
- package/src/sw/link-compiler.ts +120 -0
- package/src/sw/link-folder.ts +29 -0
- package/src/sw/link-relay.ts +40 -0
- package/src/sw/link-route.ts +239 -0
- package/src/sw/link-source.ts +41 -0
- package/src/sw/markers.ts +128 -0
- package/src/sw/module-transform.ts +364 -0
- package/src/sw/page-clients.ts +45 -0
- package/src/sw/profile.ts +90 -0
- package/src/sw/resolver.ts +224 -0
- package/src/sw/router.ts +360 -0
- package/src/sw/runtime-exports.ts +39 -0
- package/src/sw/runtime-module.ts +13 -0
- package/src/sw/source-hash.ts +37 -0
- package/src/sw/source-provider.ts +20 -0
- package/src/sw/versions.ts +96 -0
- package/src/sw/worker.ts +91 -0
- package/static/README.md +15 -0
- package/static/profile.example.json +15 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const PAGE_AGENT_URL = "/__runtime/page-agent.js";
|
|
2
|
+
/** The MDX compiler the frame loads only to export a page with library compiler plugins. */
|
|
3
|
+
export const EXPORT_COMPILER_URL = "/__runtime/export-compiler.js";
|
|
4
|
+
/** Static runtime modules the worker serves with the page's import map applied, by their runtime URL. */
|
|
5
|
+
export const RUNTIME_MODULES: Record<string, string> = { "/__runtime/editing.js": "editing.js", [PAGE_AGENT_URL]: "page-agent.js", [EXPORT_COMPILER_URL]: "export-compiler.js" };
|
|
6
|
+
|
|
7
|
+
export async function runtimeModuleSource(options: { origin: string; fetch: typeof fetch; file: string }): Promise<string> {
|
|
8
|
+
// Called unbound: the worker's native fetch throws "Illegal invocation" when `this` is the options object.
|
|
9
|
+
const fetcher = options.fetch;
|
|
10
|
+
const response = await fetcher(`${options.origin}/${options.file}`);
|
|
11
|
+
if (!response.ok) throw new Error(`${options.file}: HTTP ${response.status}`);
|
|
12
|
+
return response.text();
|
|
13
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const K = [
|
|
2
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
3
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
4
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
5
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
6
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
7
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
8
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
9
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
10
|
+
];
|
|
11
|
+
const rotate = (word: number, bits: number) => (word >>> bits) | (word << (32 - bits));
|
|
12
|
+
|
|
13
|
+
// Synchronous SHA-256 keeps the compiler's marker pass usable by both the browser and the write-side tooling.
|
|
14
|
+
export function sourceHash(source: string): string {
|
|
15
|
+
const input = new TextEncoder().encode(source);
|
|
16
|
+
const bytes = new Uint8Array(Math.ceil((input.length + 9) / 64) * 64);
|
|
17
|
+
bytes.set(input); bytes[input.length] = 0x80;
|
|
18
|
+
const view = new DataView(bytes.buffer);
|
|
19
|
+
view.setBigUint64(bytes.length - 8, BigInt(input.length) * 8n);
|
|
20
|
+
const state = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
|
|
21
|
+
const words = new Uint32Array(64);
|
|
22
|
+
for (let offset = 0; offset < bytes.length; offset += 64) {
|
|
23
|
+
for (let index = 0; index < 16; index++) words[index] = view.getUint32(offset + index * 4);
|
|
24
|
+
for (let index = 16; index < 64; index++) {
|
|
25
|
+
const a = words[index - 15]!; const b = words[index - 2]!;
|
|
26
|
+
words[index] = words[index - 16]! + (rotate(a, 7) ^ rotate(a, 18) ^ (a >>> 3)) + words[index - 7]! + (rotate(b, 17) ^ rotate(b, 19) ^ (b >>> 10));
|
|
27
|
+
}
|
|
28
|
+
let [a, b, c, d, e, f, g, h] = state as [number, number, number, number, number, number, number, number];
|
|
29
|
+
for (let index = 0; index < 64; index++) {
|
|
30
|
+
const first = (h + (rotate(e, 6) ^ rotate(e, 11) ^ rotate(e, 25)) + ((e & f) ^ (~e & g)) + K[index]! + words[index]!) | 0;
|
|
31
|
+
const second = ((rotate(a, 2) ^ rotate(a, 13) ^ rotate(a, 22)) + ((a & b) ^ (a & c) ^ (b & c))) | 0;
|
|
32
|
+
h = g; g = f; f = e; e = (d + first) | 0; d = c; c = b; b = a; a = (first + second) | 0;
|
|
33
|
+
}
|
|
34
|
+
[a, b, c, d, e, f, g, h].forEach((word, index) => { state[index] = (state[index]! + word) >>> 0; });
|
|
35
|
+
}
|
|
36
|
+
return state.map((word) => word.toString(16).padStart(8, "0")).join("").slice(0, 12);
|
|
37
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { SourceProvider } from "../schema.ts";
|
|
2
|
+
import { validateSourcePath } from "../host/validation.ts";
|
|
3
|
+
|
|
4
|
+
export function validateSourceProvider(provider: SourceProvider): void {
|
|
5
|
+
const url = new URL(provider.url);
|
|
6
|
+
if (!["http:", "https:"].includes(url.protocol) || url.username || url.password || url.search || url.hash) throw new Error("source provider requires an HTTP origin and path");
|
|
7
|
+
if (!Array.isArray(provider.paths)) throw new Error("source provider requires a path index");
|
|
8
|
+
for (const path of provider.paths) validateSourcePath(path);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function fetchSource(options: { provider: SourceProvider; path: string; fetch: typeof fetch }): Promise<Response> {
|
|
12
|
+
validateSourceProvider(options.provider);
|
|
13
|
+
validateSourcePath(options.path);
|
|
14
|
+
if (!options.provider.paths.includes(options.path)) throw new Error(`source ${options.path} is outside the approved path index`);
|
|
15
|
+
const url = `${options.provider.url.replace(/\/$/, "")}/${options.path.split("/").map(encodeURIComponent).join("/")}`;
|
|
16
|
+
const fetcher = options.fetch;
|
|
17
|
+
const response = await fetcher(url, { credentials: "omit", redirect: "error" });
|
|
18
|
+
if (!response.ok) throw new Error(`source ${options.path}: HTTP ${response.status}`);
|
|
19
|
+
return response;
|
|
20
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
type Version = { parts: number[]; pre: string[] };
|
|
2
|
+
|
|
3
|
+
function version(text: string): Version {
|
|
4
|
+
const match = /^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([\w.-]+))?(?:\+[\w.-]+)?$/.exec(text);
|
|
5
|
+
if (!match) throw new Error(`invalid semantic version ${text}`);
|
|
6
|
+
return { parts: [Number(match[1]), Number(match[2]), Number(match[3])], pre: match[4]?.split(".") ?? [] };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function compare(a: Version, b: Version): number {
|
|
10
|
+
for (let i = 0; i < 3; i++) if (a.parts[i] !== b.parts[i]) return a.parts[i]! - b.parts[i]!;
|
|
11
|
+
if (!a.pre.length || !b.pre.length) return Number(!a.pre.length) - Number(!b.pre.length);
|
|
12
|
+
for (let i = 0; i < Math.max(a.pre.length, b.pre.length); i++) {
|
|
13
|
+
const x = a.pre[i]; const y = b.pre[i];
|
|
14
|
+
if (x === y) continue;
|
|
15
|
+
if (x === undefined || y === undefined) return x === undefined ? -1 : 1;
|
|
16
|
+
if (/^\d+$/.test(x) && /^\d+$/.test(y)) return Number(x) - Number(y);
|
|
17
|
+
if (/^\d+$/.test(x) !== /^\d+$/.test(y)) return /^\d+$/.test(x) ? -1 : 1;
|
|
18
|
+
return x < y ? -1 : 1;
|
|
19
|
+
}
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function compareVersions(a: string, b: string): number { return compare(version(a), version(b)); }
|
|
24
|
+
|
|
25
|
+
function partial(text: string): { v: Version; count: number } {
|
|
26
|
+
const match = /^v?(\d+|[xX*])(?:\.(\d+|[xX*]))?(?:\.(\d+|[xX*]))?(-[\w.-]+)?(?:\+[\w.-]+)?$/.exec(text);
|
|
27
|
+
if (!match) throw new Error(`invalid semantic range ${text}`);
|
|
28
|
+
const parts = match.slice(1, 4);
|
|
29
|
+
let count = parts.findIndex((p) => p === undefined || /[xX*]/.test(p));
|
|
30
|
+
if (count === -1) count = 3;
|
|
31
|
+
if (parts.slice(count).some((p) => p !== undefined && !/[xX*]/.test(p))) throw new Error(`invalid semantic range ${text}`);
|
|
32
|
+
return { v: version(`${parts.map((p, i) => i < count ? p : "0").join(".")}${match[4] ?? ""}`), count };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function upper(v: Version, index: number): Version {
|
|
36
|
+
return { parts: v.parts.map((n, i) => i < index ? n : i === index ? n + 1 : 0), pre: ["0"] };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function comparator(actual: Version, token: string): boolean {
|
|
40
|
+
const match = /^(\^|~|>=|<=|>|<|=)?(.+)$/.exec(token);
|
|
41
|
+
if (!match?.[2]) throw new Error(`invalid semantic range ${token}`);
|
|
42
|
+
const op = match[1] ?? "=";
|
|
43
|
+
const { v, count } = partial(match[2]);
|
|
44
|
+
if (!count) return op !== ">" && op !== "<";
|
|
45
|
+
const c = compare(actual, v);
|
|
46
|
+
if (op === "^") {
|
|
47
|
+
const first = v.parts.findIndex((n, i) => n > 0 || i === count - 1);
|
|
48
|
+
return c >= 0 && compare(actual, upper(v, first)) < 0;
|
|
49
|
+
}
|
|
50
|
+
if (op === "~") return c >= 0 && compare(actual, upper(v, Math.min(count - 1, 1))) < 0;
|
|
51
|
+
if (op === "=") return count === 3 ? c === 0 : c >= 0 && compare(actual, upper(v, count - 1)) < 0;
|
|
52
|
+
if (op === ">=") return c >= 0;
|
|
53
|
+
if (op === "<") return c < 0;
|
|
54
|
+
if (op === ">") return count === 3 ? c > 0 : compare(actual, upper(v, count - 1)) >= 0;
|
|
55
|
+
return count === 3 ? c <= 0 : compare(actual, upper(v, count - 1)) < 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function rangeTokens(branch: string): string[] {
|
|
59
|
+
const words = branch.trim().split(/\s+/);
|
|
60
|
+
const expanded: string[] = [];
|
|
61
|
+
for (let i = 0; i < words.length; i += 1) {
|
|
62
|
+
const lo = words[i]!;
|
|
63
|
+
const hi = words[i + 2];
|
|
64
|
+
if (words[i + 1] === "-" && hi !== undefined) {
|
|
65
|
+
const end = partial(hi);
|
|
66
|
+
expanded.push(`>=${partial(lo).v.parts.join(".")}`, end.count === 3 ? `<=${hi}` : `<${upper(end.v, end.count - 1).parts.join(".")}-0`);
|
|
67
|
+
i += 2;
|
|
68
|
+
} else expanded.push(lo);
|
|
69
|
+
}
|
|
70
|
+
const normalized = expanded.join(" ").replace(/(\^|~|>=|<=|>|<|=)\s+/g, "$1");
|
|
71
|
+
return normalized ? normalized.split(/\s+/) : ["*"];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function validateVersionRange(range: string): void {
|
|
75
|
+
if (!range.trim()) throw new Error("empty semantic version range");
|
|
76
|
+
for (const branch of range.split("||")) for (const token of rangeTokens(branch)) comparator(version("0.0.0"), token);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function satisfiesVersion(text: string, range: string): boolean {
|
|
80
|
+
const actual = version(text);
|
|
81
|
+
return range.split("||").some((branch) => {
|
|
82
|
+
const tokens = rangeTokens(branch);
|
|
83
|
+
if (actual.pre.length && !tokens.some((token) => {
|
|
84
|
+
const target = partial(token.replace(/^(\^|~|>=|<=|>|<|=)/, "")).v;
|
|
85
|
+
return target.pre.length && target.parts.every((n, i) => n === actual.parts[i]);
|
|
86
|
+
})) return false;
|
|
87
|
+
return tokens.every((token) => comparator(actual, token));
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function chooseVersion(options: { name: string; range: string; times: Record<string, string>; ageDays: number; now: number }): string {
|
|
92
|
+
const eligible = Object.entries(options.times).filter(([v, date]) => /^v?\d+\.\d+\.\d+/.test(v) && satisfiesVersion(v, options.range) && Date.parse(date) <= options.now - options.ageDays * 86_400_000);
|
|
93
|
+
eligible.sort(([a], [b]) => compareVersions(b, a));
|
|
94
|
+
if (!eligible[0]) throw new Error(`${options.name}: no version matches ${options.range} with minimum release age ${options.ageDays} days`);
|
|
95
|
+
return eligible[0][0];
|
|
96
|
+
}
|
package/src/sw/worker.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { compile } from "@mdx-js/mdx";
|
|
2
|
+
import { BASE_PLUGINS } from "./compiler.ts";
|
|
3
|
+
import { createRouter, type RuntimeRouter } from "./router.ts";
|
|
4
|
+
import { validateProfile } from "./profile.ts";
|
|
5
|
+
import { runtimeError } from "./errors.ts";
|
|
6
|
+
import { createActivityBatcher } from "./activity.ts";
|
|
7
|
+
import { createPageClients } from "./page-clients.ts";
|
|
8
|
+
import { cleanupLegacyStorage } from "./legacy-storage.ts";
|
|
9
|
+
import type { WorkerActivity } from "../activity-schema.ts";
|
|
10
|
+
|
|
11
|
+
type LifetimeEvent = Event & { waitUntil(promise: Promise<unknown>): void };
|
|
12
|
+
type WorkerEvent = LifetimeEvent & { request: Request; clientId: string; resultingClientId: string; respondWith(response: Promise<Response>): void };
|
|
13
|
+
type WorkerMessage = LifetimeEvent & { data: unknown; source: { id: string; url: string } | null; ports: MessagePort[] };
|
|
14
|
+
type WorkerScope = {
|
|
15
|
+
location: Location; skipWaiting(): Promise<void>;
|
|
16
|
+
clients: { claim(): Promise<void>; get(id: string): Promise<{ id: string; url: string; postMessage(value: unknown): void } | undefined>; matchAll(options: { type: "all"; includeUncontrolled: boolean }): Promise<{ id: string }[]> };
|
|
17
|
+
addEventListener(type: "install" | "activate", listener: (e: LifetimeEvent) => void): void;
|
|
18
|
+
addEventListener(type: "fetch", listener: (e: WorkerEvent) => void): void;
|
|
19
|
+
addEventListener(type: "message", listener: (e: WorkerMessage) => void): void;
|
|
20
|
+
};
|
|
21
|
+
declare const __RUNTIME_VERSION__: string;
|
|
22
|
+
const scope = self as unknown as WorkerScope;
|
|
23
|
+
const activityRequests = new WeakMap<Request, WorkerEvent>();
|
|
24
|
+
const activityClients = new Map<string, { load: string; navigationStart: number }>();
|
|
25
|
+
const earlyActivity = new Map<string, WorkerActivity[]>();
|
|
26
|
+
const activity = createActivityBatcher({ send: async (id, records) => {
|
|
27
|
+
const client = await scope.clients.get(id);
|
|
28
|
+
if (!client) { activityClients.delete(id); earlyActivity.delete(id); return; }
|
|
29
|
+
const subscription = activityClients.get(id);
|
|
30
|
+
if (subscription) client.postMessage({ type: "activity", ...subscription, records, timeline: [], tailwind: [] });
|
|
31
|
+
else earlyActivity.set(id, [...(earlyActivity.get(id) ?? []), ...records]);
|
|
32
|
+
} });
|
|
33
|
+
const pageClients = createPageClients({ origin: scope.location.origin, clients: scope.clients });
|
|
34
|
+
let router: Promise<RuntimeRouter> | undefined;
|
|
35
|
+
function load(): Promise<RuntimeRouter> {
|
|
36
|
+
router ??= (async () => {
|
|
37
|
+
const response = await fetch("/profile.json", { cache: "no-store" });
|
|
38
|
+
if (!response.ok) throw new Error(`profile.json: HTTP ${response.status}`);
|
|
39
|
+
return createRouter({ origin: scope.location.origin, version: __RUNTIME_VERSION__, profile: validateProfile(await response.json()), plugins: BASE_PLUGINS, caches, onActivity: ({ request, record }) => {
|
|
40
|
+
const event = activityRequests.get(request);
|
|
41
|
+
if (!event) throw new Error("worker activity request context is missing");
|
|
42
|
+
const id = event.resultingClientId || event.clientId;
|
|
43
|
+
if (id) event.waitUntil(activity.push({ client: id, record }));
|
|
44
|
+
} });
|
|
45
|
+
})();
|
|
46
|
+
return router;
|
|
47
|
+
}
|
|
48
|
+
// MDX is statically bundled: service workers cannot load more compiler code after install.
|
|
49
|
+
void compile;
|
|
50
|
+
scope.addEventListener("install", (event) => event.waitUntil(Promise.all([load(), scope.skipWaiting()])));
|
|
51
|
+
scope.addEventListener("activate", (event) => event.waitUntil(Promise.all([scope.clients.claim(), cleanupLegacyStorage({ caches, origin: scope.location.origin, version: __RUNTIME_VERSION__ })])));
|
|
52
|
+
scope.addEventListener("message", (event) => {
|
|
53
|
+
if (!event.source || new URL(event.source.url).origin !== scope.location.origin) return;
|
|
54
|
+
event.waitUntil((async () => {
|
|
55
|
+
const port = event.ports[0];
|
|
56
|
+
if (!port) throw new Error("runtime worker message requires reply port");
|
|
57
|
+
try {
|
|
58
|
+
const value = event.data as { page?: unknown; message?: unknown };
|
|
59
|
+
if (value?.message === "subscribe-activity") {
|
|
60
|
+
const subscription = event.data as { load: unknown; navigationStart: unknown };
|
|
61
|
+
if (typeof subscription.load !== "string" || subscription.load.length > 256 || typeof subscription.navigationStart !== "number" || !Number.isFinite(subscription.navigationStart) || subscription.navigationStart < 0) throw new Error("invalid activity subscription");
|
|
62
|
+
const id = event.source!.id;
|
|
63
|
+
for (const previous of new Set([...earlyActivity.keys(), ...activityClients.keys()])) if (previous !== id && !await scope.clients.get(previous)) { earlyActivity.delete(previous); activityClients.delete(previous); }
|
|
64
|
+
activityClients.set(id, { load: subscription.load, navigationStart: subscription.navigationStart });
|
|
65
|
+
const records = earlyActivity.get(id) ?? []; earlyActivity.delete(id);
|
|
66
|
+
await Promise.all(records.map((record) => activity.push({ client: id, record })));
|
|
67
|
+
port.postMessage({ ok: true }); return;
|
|
68
|
+
}
|
|
69
|
+
const loaded = await load();
|
|
70
|
+
const exportMessage = value?.message as { type?: unknown } | undefined;
|
|
71
|
+
if (typeof value?.page === "string" && exportMessage?.type === "export-module") await loaded.exportModule(value.page, value.message);
|
|
72
|
+
else if (typeof value?.page === "string") await loaded.update(value.page, value.message);
|
|
73
|
+
else {
|
|
74
|
+
port.postMessage({ ok: true, result: await loaded.message(value?.message) });
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
port.postMessage({ ok: true });
|
|
78
|
+
} catch (error) { port.postMessage({ ok: false, error: runtimeError({ error, phase: "resolve", file: new URL(event.source!.url).pathname }) }); }
|
|
79
|
+
})());
|
|
80
|
+
});
|
|
81
|
+
scope.addEventListener("fetch", (event) => {
|
|
82
|
+
const url = new URL(event.request.url);
|
|
83
|
+
if (url.origin === scope.location.origin && !/^\/(frame|src|npm|esm|link|__runtime)\//.test(url.pathname) && !["/boot.js", "/devtools-hook.js", "/page-agent.js", "/profile.json"].includes(url.pathname)) return;
|
|
84
|
+
event.respondWith((async () => {
|
|
85
|
+
const { request, client, owner } = await pageClients.request(event);
|
|
86
|
+
activityRequests.set(request, event);
|
|
87
|
+
const response = await (await load()).route(request, { onError: (error) => client?.postMessage({ type: "error", error }) });
|
|
88
|
+
event.waitUntil(pageClients.served({ event, owner, response }));
|
|
89
|
+
return response;
|
|
90
|
+
})());
|
|
91
|
+
});
|
package/static/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Runtime static hosting
|
|
2
|
+
|
|
3
|
+
Build with `pnpm --filter @flanksource/page-runtime build`. The worker compiles live pages with the base MDX plugins only. `export-compiler.js` is loaded only for an HTML or PDF export and runs the plugins libraries declare (see `src/build/README.md`). Add `--dev` to bundle every `@flanksource/*` dependency from a sibling checkout of this workspace instead of its installed release; the build logs which checkout each package came from. An import that resolves nowhere fails the build.
|
|
4
|
+
|
|
5
|
+
Serve `dist/static/` over HTTPS (localhost works for development). Serve `bootstrap.html` for `/frame/*` on the first visit, and make `/sw.js`, `/boot.js`, `/page-agent.js`, `/export-compiler.js`, `/devtools-hook.js` and `/profile.json` available from the origin root. Use a separate runtime origin from the host. The first visit registers the worker and reloads once. `profile.json` is required and validated; configure it for the deployment using the example below. Profile changes require a worker upgrade/reload.
|
|
6
|
+
|
|
7
|
+
Frames get a strict Content Security Policy: `script-src 'self'` plus a per-document nonce, with no `eval` and no WebAssembly. `csp.add` appends sources to any directive. To let page code compile WebAssembly, opt in with `"csp": { "add": {}, "wasm": true }`, which adds `'wasm-unsafe-eval'` to `script-src`. It allows WebAssembly compilation only, not JS `eval`, and it is off when omitted. The runtime itself never needs it: the worker is not governed by the frame policy, and the export compiler uses an asm.js lexer.
|
|
8
|
+
|
|
9
|
+
The host embeds `PageFrame` from `@flanksource/page-runtime/host`, passing `runtimeOrigin`, `open: { page, manifest, files, lock? }`, `theme`, `data` and `onError`. The host can retain the bridge with `onBridge`; send `sources` to update buffers and reload, or request `{ type: "export", format: "html" }` / `{ type: "export", format: "print" }`. The frame's `connecting` status requests initial configuration, `booting` requests data and theme, and `ready` permits exports. Sessions are fresh for each embedding.
|
|
10
|
+
|
|
11
|
+
For local development only, `origin="same-origin-local"` explicitly permits `runtimeOrigin={location.origin}` on localhost, 127.0.0.1 or ::1. Both frame and host enforce the opt-in; the default remains cross-origin-only. Exact origin, source-window and session validation is unchanged. The worker registers with scope `/frame/`: it controls runtime frame documents and handles their `/src/`, `/npm/`, `/link/` and `/__runtime/` fetches, while the host document and its own requests stay outside its scope. Reserve `/frame/` for runtime documents. This local mode shares the host realm's origin authority and is not a production isolation boundary; P7 remains deferred.
|
|
12
|
+
|
|
13
|
+
`PageLock` is serializable. The frame emits a validated `lock` message; a source provider should persist it beside writable sources, hash-page URLs or browser state, and supply it in `open.lock`. Supplying a lock bypasses registry resolution. The worker persists opened pages in versioned Cache Storage and reuses their locks while the folder manifest is unchanged. The package origin uses the unpkg layout (`/<name>@<exact-version>/<file>`), including `?meta` file listings for additional ESM packages. Libraries publish `runtime/importmap.json` and `runtime/barrels.json`; named imports are pruned only where the package's `sideEffects` declaration permits skipping the barrel.
|
|
14
|
+
|
|
15
|
+
P3 can install `window.__PAGE_RUNTIME_STYLES__ = { prepare }` before page boot. `prepare({ config, candidates })` is awaited before React mounts. The worker extracts candidates from every page module and exposes `/__runtime/candidates?page=<page>`. P5 can extend the validated bridge catalog for agent messages; P6 owns linked-library routing. CSP additions and network patterns are explicit profile seams; P7 hardening remains separate.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"runtimeDomain": "pages.example.com",
|
|
3
|
+
"embedders": ["https://app.example.com"],
|
|
4
|
+
"packageOrigin": "https://unpkg.com",
|
|
5
|
+
"registry": "https://registry.npmjs.org",
|
|
6
|
+
"managed": { "@flanksource/clicky-ui": "^0.3", "@flanksource/facet": "^0.1.74" },
|
|
7
|
+
"policy": { "dependencies": "strict", "allow": ["date-fns", "d3-*", "zod", "react-copy-to-clipboard"] },
|
|
8
|
+
"convert": { "origin": "https://esm.internal.example" },
|
|
9
|
+
"minReleaseAgeDays": 3,
|
|
10
|
+
"network": { "allow": ["https://api.example.com/*"] },
|
|
11
|
+
"csp": { "add": {} },
|
|
12
|
+
"links": "deny",
|
|
13
|
+
"sources": ["host", "opfs", "folder", "url", "hash"],
|
|
14
|
+
"readinessTimeoutMs": 15000
|
|
15
|
+
}
|