@cloudcrane/app-runtime 0.2.0 → 0.5.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
@@ -12,10 +12,17 @@ const { html, sha256, runtime } = compile(spec);
12
12
  ```
13
13
 
14
14
  - `compile(spec)` returns the page, its sha256 and the runtime it was built
15
- with (`@cloudcrane/app-runtime@0.2.0+<hash>`); it throws `CompileError`
15
+ with (`@cloudcrane/app-runtime@0.4.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
@@ -32,6 +39,20 @@ const { html, sha256, runtime } = compile(spec);
32
39
  game.random(); // seeded by the page
33
40
  ```
34
41
 
42
+ The page kit runs an HTML page as it is, full screen. Its `localStorage`
43
+ works: what it holds is written into the frame when it opens, and each
44
+ change goes to the page, which keeps it under `<data.key>.coded.<id>` (at
45
+ most 4 MB), so it survives a reload and an update and the frame sees only
46
+ its own. Both kits reach the page through `window.__ccBus`.
47
+
48
+ A component's frame is allowed the device features the spec's `uses`
49
+ declares (its `allow`), and the page must allow them for frames too (in
50
+ Permissions-Policy, `geolocation=*` rather than `(self)`). Browsers give a
51
+ frame with no origin of its own no camera or microphone, so a page kit whose
52
+ spec declares either runs with the page's origin; it's then no more apart
53
+ from the page than an app written straight into it. A game kit always
54
+ stays apart.
55
+
35
56
  - `@cloudcrane/app-runtime/host`: what a host publishing spec apps needs in one
36
57
  import (validate, compile, derive the blueprint).
37
58
 
@@ -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,17 +33,21 @@ 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);
30
43
  if (spec.manifest.runtime !== RUNTIME_VERSION)
31
44
  throw new CompileError([{ at: "manifest.runtime", problem: `this compiler builds runtime ${RUNTIME_VERSION}` }]);
32
45
  // Only what drawing the app needs goes in the page: not the journeys, promises or tests.
33
- // Coded components go in only when there are some.
46
+ // Coded components go in only when there are some, and the device features they may use only when it uses some.
34
47
  const inPage = {
35
48
  module: spec.module,
36
49
  ...(Object.keys(spec.components).length ? { components: spec.components } : {}),
50
+ ...(spec.uses.length ? { uses: spec.uses } : {}),
37
51
  data: spec.data,
38
52
  logic: spec.logic,
39
53
  surface: spec.surface,
@@ -46,11 +60,14 @@ export function compile(input) {
46
60
  `<style>${tokensCss(spec.tokens)}${BASE_CSS}${DATESTRIP_CSS}${CODED_CSS}</style>` +
47
61
  '</head><body><main id="app"></main>' +
48
62
  `<script type="application/json" id="cc-spec">${scriptSafe(canonical(inPage))}</script>` +
49
- `<script type="module">${RUNTIME_JS}</script>` +
63
+ (mode === "shared"
64
+ ? `<script type="module" src="${runtimeFile().path}" integrity="${runtimeFile().integrity}"></script>`
65
+ : `<script type="module">${RUNTIME_JS}</script>`) +
50
66
  "</body></html>\n";
51
67
  return {
52
68
  html,
53
69
  sha256: createHash("sha256").update(html).digest("hex"),
54
70
  runtime: `@cloudcrane/app-runtime@${RUNTIME_VERSION}+${RUNTIME_SHA256.slice(0, 12)}`,
71
+ ...(mode === "shared" ? { linked: { path: runtimeFile().path, integrity: runtimeFile().integrity } } : {}),
55
72
  };
56
73
  }