@cloudcrane/app-runtime 0.2.0 → 0.3.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/README.md CHANGED
@@ -14,8 +14,15 @@ const { html, sha256, runtime } = compile(spec);
14
14
  - `compile(spec)` returns the page, its sha256 and the runtime it was built
15
15
  with (`@cloudcrane/app-runtime@0.2.0+<hash>`); it throws `CompileError`
16
16
  listing every problem when the spec doesn't validate.
17
+ - `compile(spec, { runtime: "shared" })` links the runtime instead of inlining
18
+ it: the page names `runtimeFile().path` (`/_cc/runtime/<version>-<hash>.js`)
19
+ with an integrity hash, so a host serves one copy to every spec app and a
20
+ browser runs nothing but those bytes. The page is then a few KB. Inline is
21
+ the default, for a page that stands alone.
17
22
  - Each runtime version is locked to one bundle hash (`runtime-lock.json`), so a
18
- version always draws apps the same way.
23
+ version always draws apps the same way. The runtime's version
24
+ (`runtimeVersion` in package.json) moves only when the bundle does; the
25
+ package can release without it.
19
26
  - A coded component runs in a frame that's sandboxed (scripts only, so it has
20
27
  no storage and nothing of the page's), in a closed shadow root, reached only
21
28
  through a `MessagePort`. Under CloudCrane's lockdown the lockdown makes the
@@ -8,9 +8,28 @@ export declare class CompileError extends Error {
8
8
  problem: string;
9
9
  }[]);
10
10
  }
11
+ /** Where a page that links the runtime finds it, and the hash its bytes must have. */
12
+ export type LinkedRuntime = {
13
+ path: string;
14
+ integrity: string;
15
+ };
11
16
  export type Compiled = {
12
17
  html: string;
13
18
  sha256: string;
14
19
  runtime: string;
20
+ linked?: LinkedRuntime;
21
+ };
22
+ /**
23
+ * How a page carries the runtime: inline (the default: the page stands alone,
24
+ * as blackbox preview and anyone hosting it themselves need) or shared (it
25
+ * links one file every spec app on a host shares, which the host serves from
26
+ * runtimeFile(); the integrity hash means a browser runs nothing else).
27
+ */
28
+ export type CompileOptions = {
29
+ runtime?: "inline" | "shared";
30
+ };
31
+ /** The runtime as the file a shared page links: its path (named by version and hash), its bytes and their integrity hash. */
32
+ export declare function runtimeFile(): LinkedRuntime & {
33
+ js: string;
15
34
  };
16
- export declare function compile(input: unknown): Compiled;
35
+ export declare function compile(input: unknown, options?: CompileOptions): Compiled;
@@ -15,6 +15,16 @@ export class CompileError extends Error {
15
15
  this.problems = problems;
16
16
  }
17
17
  }
18
+ let file;
19
+ /** The runtime as the file a shared page links: its path (named by version and hash), its bytes and their integrity hash. */
20
+ export function runtimeFile() {
21
+ file ??= {
22
+ path: `/_cc/runtime/${RUNTIME_VERSION}-${RUNTIME_SHA256.slice(0, 12)}.js`,
23
+ js: RUNTIME_JS,
24
+ integrity: `sha256-${createHash("sha256").update(RUNTIME_JS).digest("base64")}`,
25
+ };
26
+ return file;
27
+ }
18
28
  /** The page's base: the tokens set A2UI's variables; this lays out the page around the surface. */
19
29
  const BASE_CSS = "*{box-sizing:border-box}html,body{margin:0}body{background:var(--a2ui-color-background,#fff);color:var(--a2ui-color-on-background,#111);font-family:var(--a2ui-font-family,system-ui,sans-serif);-webkit-text-size-adjust:100%}#app{max-width:30rem;margin:0 auto;padding:max(16px,env(safe-area-inset-top)) 16px max(24px,env(safe-area-inset-bottom))}@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}";
20
30
  const escapeHtml = (s) => s.replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);
@@ -23,7 +33,10 @@ const scriptSafe = (json) => json
23
33
  .replace(/</g, "\\u003c")
24
34
  .replace(new RegExp(String.fromCharCode(0x2028), "g"), "\\u2028")
25
35
  .replace(new RegExp(String.fromCharCode(0x2029), "g"), "\\u2029");
26
- export function compile(input) {
36
+ export function compile(input, options = {}) {
37
+ const mode = options.runtime ?? "inline";
38
+ if (mode !== "inline" && mode !== "shared")
39
+ throw new TypeError("The runtime is inline or shared.");
27
40
  const { spec, problems } = validateSpec(input);
28
41
  if (!spec)
29
42
  throw new CompileError(problems);
@@ -46,11 +59,14 @@ export function compile(input) {
46
59
  `<style>${tokensCss(spec.tokens)}${BASE_CSS}${DATESTRIP_CSS}${CODED_CSS}</style>` +
47
60
  '</head><body><main id="app"></main>' +
48
61
  `<script type="application/json" id="cc-spec">${scriptSafe(canonical(inPage))}</script>` +
49
- `<script type="module">${RUNTIME_JS}</script>` +
62
+ (mode === "shared"
63
+ ? `<script type="module" src="${runtimeFile().path}" integrity="${runtimeFile().integrity}"></script>`
64
+ : `<script type="module">${RUNTIME_JS}</script>`) +
50
65
  "</body></html>\n";
51
66
  return {
52
67
  html,
53
68
  sha256: createHash("sha256").update(html).digest("hex"),
54
69
  runtime: `@cloudcrane/app-runtime@${RUNTIME_VERSION}+${RUNTIME_SHA256.slice(0, 12)}`,
70
+ ...(mode === "shared" ? { linked: { path: runtimeFile().path, integrity: runtimeFile().integrity } } : {}),
55
71
  };
56
72
  }
package/dist/host.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { validateSpec, leakProblems, deriveBlueprint, CLOUDCRANE_CATALOG_ID } from "@cloudcrane/app-spec";
2
- export { compile, CompileError } from "./compiler/compile.js";
2
+ export { compile, CompileError, runtimeFile } from "./compiler/compile.js";
3
3
  export { RUNTIME_JS, RUNTIME_SHA256, RUNTIME_VERSION } from "./compiler/runtime.generated.js";
package/dist/host.js CHANGED
@@ -3,5 +3,5 @@
3
3
  // blueprint. The runtime's code is exported so a host can check a built page
4
4
  // without it: the runtime is reviewed once per version and locked by its hash.
5
5
  export { validateSpec, leakProblems, deriveBlueprint, CLOUDCRANE_CATALOG_ID } from "@cloudcrane/app-spec";
6
- export { compile, CompileError } from "./compiler/compile.js";
6
+ export { compile, CompileError, runtimeFile } from "./compiler/compile.js";
7
7
  export { RUNTIME_JS, RUNTIME_SHA256, RUNTIME_VERSION } from "./compiler/runtime.generated.js";
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { compile, CompileError, type Compiled } from "./compiler/compile.js";
1
+ export { compile, CompileError, runtimeFile, type Compiled, type CompileOptions, type LinkedRuntime } from "./compiler/compile.js";
2
2
  export { RUNTIME_VERSION, RUNTIME_SHA256 } from "./compiler/runtime.generated.js";
3
3
  export { tokenVariables, tokensCss } from "./runtime/theme.js";
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  // Builds a CloudCrane app spec into one HTML page, the same bytes every time.
2
- export { compile, CompileError } from "./compiler/compile.js";
2
+ export { compile, CompileError, runtimeFile } from "./compiler/compile.js";
3
3
  export { RUNTIME_VERSION, RUNTIME_SHA256 } from "./compiler/runtime.generated.js";
4
4
  export { tokenVariables, tokensCss } from "./runtime/theme.js";
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@cloudcrane/app-runtime",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
+ "runtimeVersion": "0.2.0",
4
5
  "description": "Builds a CloudCrane app spec into one HTML page, the same bytes every time, and the runtime that draws it with A2UI's renderer.",
5
6
  "keywords": [
6
7
  "cloudcrane",