@taggor/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.
Files changed (144) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +49 -2
  3. package/dist/.tsbuildinfo +1 -0
  4. package/dist/compat/before-compat.d.ts +5 -0
  5. package/dist/compat/before-compat.d.ts.map +1 -0
  6. package/dist/compat/before-compat.js +15 -0
  7. package/dist/compat/before-compat.js.map +1 -0
  8. package/dist/compat/index.d.ts +90 -0
  9. package/dist/compat/index.d.ts.map +1 -0
  10. package/dist/compat/index.js +67 -0
  11. package/dist/compat/index.js.map +1 -0
  12. package/dist/compat/scope.d.ts +81 -0
  13. package/dist/compat/scope.d.ts.map +1 -0
  14. package/dist/compat/scope.js +107 -0
  15. package/dist/compat/scope.js.map +1 -0
  16. package/dist/element/attributes.d.ts +103 -0
  17. package/dist/element/attributes.d.ts.map +1 -0
  18. package/dist/element/attributes.js +153 -0
  19. package/dist/element/attributes.js.map +1 -0
  20. package/dist/element/component-controller.d.ts +98 -0
  21. package/dist/element/component-controller.d.ts.map +1 -0
  22. package/dist/element/component-controller.js +472 -0
  23. package/dist/element/component-controller.js.map +1 -0
  24. package/dist/element/contract-gate.d.ts +29 -0
  25. package/dist/element/contract-gate.d.ts.map +1 -0
  26. package/dist/element/contract-gate.js +37 -0
  27. package/dist/element/contract-gate.js.map +1 -0
  28. package/dist/element/create-component-element.d.ts +93 -0
  29. package/dist/element/create-component-element.d.ts.map +1 -0
  30. package/dist/element/create-component-element.js +302 -0
  31. package/dist/element/create-component-element.js.map +1 -0
  32. package/dist/element/define-element.d.ts +43 -0
  33. package/dist/element/define-element.d.ts.map +1 -0
  34. package/dist/element/define-element.js +61 -0
  35. package/dist/element/define-element.js.map +1 -0
  36. package/dist/element/diagnostics.d.ts +35 -0
  37. package/dist/element/diagnostics.d.ts.map +1 -0
  38. package/dist/element/diagnostics.js +52 -0
  39. package/dist/element/diagnostics.js.map +1 -0
  40. package/dist/element/error-boundary.d.ts +41 -0
  41. package/dist/element/error-boundary.d.ts.map +1 -0
  42. package/dist/element/error-boundary.js +36 -0
  43. package/dist/element/error-boundary.js.map +1 -0
  44. package/dist/element/error-report.d.ts +9 -0
  45. package/dist/element/error-report.d.ts.map +1 -0
  46. package/dist/element/error-report.js +51 -0
  47. package/dist/element/error-report.js.map +1 -0
  48. package/dist/element/events.d.ts +69 -0
  49. package/dist/element/events.d.ts.map +1 -0
  50. package/dist/element/events.js +115 -0
  51. package/dist/element/events.js.map +1 -0
  52. package/dist/element/handler-guard.d.ts +54 -0
  53. package/dist/element/handler-guard.d.ts.map +1 -0
  54. package/dist/element/handler-guard.js +162 -0
  55. package/dist/element/handler-guard.js.map +1 -0
  56. package/dist/element/index.d.ts +9 -0
  57. package/dist/element/index.d.ts.map +1 -0
  58. package/dist/element/index.js +9 -0
  59. package/dist/element/index.js.map +1 -0
  60. package/dist/element/props-script.d.ts +37 -0
  61. package/dist/element/props-script.d.ts.map +1 -0
  62. package/dist/element/props-script.js +63 -0
  63. package/dist/element/props-script.js.map +1 -0
  64. package/dist/element/styles.d.ts +45 -0
  65. package/dist/element/styles.d.ts.map +1 -0
  66. package/dist/element/styles.js +66 -0
  67. package/dist/element/styles.js.map +1 -0
  68. package/dist/element/types.d.ts +148 -0
  69. package/dist/element/types.d.ts.map +1 -0
  70. package/dist/element/types.js +3 -0
  71. package/dist/element/types.js.map +1 -0
  72. package/dist/host/api.d.ts +40 -0
  73. package/dist/host/api.d.ts.map +1 -0
  74. package/dist/host/api.js +144 -0
  75. package/dist/host/api.js.map +1 -0
  76. package/dist/host/barrier.d.ts +27 -0
  77. package/dist/host/barrier.d.ts.map +1 -0
  78. package/dist/host/barrier.js +47 -0
  79. package/dist/host/barrier.js.map +1 -0
  80. package/dist/host/index.d.ts +11 -0
  81. package/dist/host/index.d.ts.map +1 -0
  82. package/dist/host/index.js +9 -0
  83. package/dist/host/index.js.map +1 -0
  84. package/dist/host/registry.d.ts +21 -0
  85. package/dist/host/registry.d.ts.map +1 -0
  86. package/dist/host/registry.js +29 -0
  87. package/dist/host/registry.js.map +1 -0
  88. package/dist/host/types.d.ts +54 -0
  89. package/dist/host/types.d.ts.map +1 -0
  90. package/dist/host/types.js +11 -0
  91. package/dist/host/types.js.map +1 -0
  92. package/dist/index.d.ts +13 -0
  93. package/dist/index.d.ts.map +1 -0
  94. package/dist/index.js +12 -0
  95. package/dist/index.js.map +1 -0
  96. package/dist/loader/config.d.ts +63 -0
  97. package/dist/loader/config.d.ts.map +1 -0
  98. package/dist/loader/config.js +115 -0
  99. package/dist/loader/config.js.map +1 -0
  100. package/dist/loader/entry.d.ts +2 -0
  101. package/dist/loader/entry.d.ts.map +1 -0
  102. package/dist/loader/entry.js +37 -0
  103. package/dist/loader/entry.js.map +1 -0
  104. package/dist/loader/lazy-transport.d.ts +26 -0
  105. package/dist/loader/lazy-transport.d.ts.map +1 -0
  106. package/dist/loader/lazy-transport.js +59 -0
  107. package/dist/loader/lazy-transport.js.map +1 -0
  108. package/dist/loader/loader-code.d.ts +2 -0
  109. package/dist/loader/loader-code.js +2 -0
  110. package/dist/loader/loading.d.ts +24 -0
  111. package/dist/loader/loading.d.ts.map +1 -0
  112. package/dist/loader/loading.js +29 -0
  113. package/dist/loader/loading.js.map +1 -0
  114. package/dist/loader/page-state.d.ts +66 -0
  115. package/dist/loader/page-state.d.ts.map +1 -0
  116. package/dist/loader/page-state.js +73 -0
  117. package/dist/loader/page-state.js.map +1 -0
  118. package/dist/loader/resilient-import.d.ts +32 -0
  119. package/dist/loader/resilient-import.d.ts.map +1 -0
  120. package/dist/loader/resilient-import.js +42 -0
  121. package/dist/loader/resilient-import.js.map +1 -0
  122. package/dist/loader/run.d.ts +45 -0
  123. package/dist/loader/run.d.ts.map +1 -0
  124. package/dist/loader/run.js +217 -0
  125. package/dist/loader/run.js.map +1 -0
  126. package/dist/loader/script-url.d.ts +50 -0
  127. package/dist/loader/script-url.d.ts.map +1 -0
  128. package/dist/loader/script-url.js +71 -0
  129. package/dist/loader/script-url.js.map +1 -0
  130. package/dist/loader/script.d.ts +31 -0
  131. package/dist/loader/script.d.ts.map +1 -0
  132. package/dist/loader/script.js +40 -0
  133. package/dist/loader/script.js.map +1 -0
  134. package/dist/server/render-fragment.d.ts +57 -0
  135. package/dist/server/render-fragment.d.ts.map +1 -0
  136. package/dist/server/render-fragment.js +182 -0
  137. package/dist/server/render-fragment.js.map +1 -0
  138. package/dist/server/ssr-code.d.ts +4 -0
  139. package/dist/server/ssr-code.js +3 -0
  140. package/dist/server/worker.d.ts +37 -0
  141. package/dist/server/worker.d.ts.map +1 -0
  142. package/dist/server/worker.js +71 -0
  143. package/dist/server/worker.js.map +1 -0
  144. package/package.json +86 -3
@@ -0,0 +1,66 @@
1
+ /**
2
+ * What every loader on one page shares (TAG-610). A host page can run the snippet more than once
3
+ * (a cache plugin that duplicates scripts, a theme that injects it twice) and can carry the
4
+ * snippets of two sites (two agencies). Each run is a separate closure of the same code, so the
5
+ * runs meet in one record kept on the page's global under `Symbol.for("taggor.loader")`: a symbol
6
+ * key is invisible to `Object.keys`, `for in` and `getOwnPropertyNames`, so the page's only added
7
+ * name stays `taggor` (golden rule 2, `e2e/tests/loader.e2e.ts`).
8
+ *
9
+ * The record holds: the site keys already running (a second run of the same key does nothing),
10
+ * the version that claimed each tag (the first definition of a tag wins and another version of it
11
+ * is skipped with one warning), the one initial-load barrier and runtime reference behind
12
+ * `window.taggor` (so `ready()` and `components()` cover every site), and the Trusted Types policy
13
+ * state (`script-url.ts`: one `taggor` policy per page, never a duplicate). The page can read and
14
+ * replace it like any global; a value that is not shaped like the record is replaced by a fresh
15
+ * one, and a global that refuses the property gets a private record (the loader still works).
16
+ */
17
+ import type { ComponentElementOptions } from "../element/create-component-element.js";
18
+ import type { MountableComponent } from "../element/types.js";
19
+ import type { InitialLoadBarrier } from "../host/barrier.js";
20
+ import type { LiveComponents } from "../host/registry.js";
21
+ import type { PolicyState } from "./script-url.js";
22
+ /** The parts of the runtime module the loader calls. */
23
+ export interface RuntimeModule {
24
+ readonly createComponentElement: (definition: MountableComponent, options?: ComponentElementOptions) => unknown;
25
+ readonly liveComponents: LiveComponents;
26
+ }
27
+ /** The record shared by every loader of the page. */
28
+ export interface PageState {
29
+ /** Site keys whose loader runs on this page. */
30
+ readonly sites: Set<string>;
31
+ /** Tag → site key and version that claimed it. */
32
+ readonly tags: Map<string, string>;
33
+ readonly barrier: InitialLoadBarrier;
34
+ /** The runtime once any loader imported it. */
35
+ runtime: RuntimeModule | undefined;
36
+ /** The Trusted Types policy of the page's loaders. */
37
+ readonly policy: PolicyState;
38
+ }
39
+ /**
40
+ * The page's shared record, created by the first loader.
41
+ *
42
+ * Inputs: the global object the record lives on (default `globalThis`). Output: the `PageState`.
43
+ * Errors: never throws. Side effects: defines one non-enumerable symbol property on `target` the
44
+ * first time (or when the page replaced it with something else).
45
+ */
46
+ export declare function pageState(target?: object): PageState;
47
+ /**
48
+ * Claims a site key for the calling loader.
49
+ *
50
+ * Output: `true` for the first loader of the key, `false` for a repeated run (it must do nothing).
51
+ * An embed without a site key is never treated as a repeat. Errors: none. Side effects: records the key.
52
+ */
53
+ export declare function claimSite(state: PageState, siteKey: string | undefined): boolean;
54
+ /** How a loader stands on a tag. */
55
+ export type TagClaim = "mine" | "same" | "other";
56
+ /**
57
+ * Claims `tag` for `siteKey` at `version`: `"mine"` for the first claim, `"same"` when the same
58
+ * site already holds that exact version, `"other"` otherwise (another version, or another site:
59
+ * the first definition wins and the caller warns). Another site is never "same", even at the same
60
+ * version: its component would reuse the first site's definition and data transport, so its
61
+ * visitors' submissions would go to the first site's workspace. That is the page-level tag
62
+ * collision of ADR 0018 and cannot be fixed on the client; the warning makes it visible.
63
+ * Errors: none. Side effects: records the first claim.
64
+ */
65
+ export declare function claimTag(state: PageState, tag: string, version: string, siteKey: string | undefined): TagClaim;
66
+ //# sourceMappingURL=page-state.d.ts.map
@@ -0,0 +1 @@
1
+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AAO,CAAC;AAEjD;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CACtB,KAAK,EAAE,SAAS,EAChB,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,GAAG,SAAS,GAC1B,QAAQ,CAQV"}
@@ -0,0 +1,73 @@
1
+ import { createInitialLoadBarrier } from "../host/barrier.js";
2
+ const KEY = Symbol.for("taggor.loader");
3
+ function create() {
4
+ return {
5
+ sites: new Set(),
6
+ tags: new Map(),
7
+ barrier: createInitialLoadBarrier(),
8
+ runtime: undefined,
9
+ policy: {},
10
+ };
11
+ }
12
+ function isState(value) {
13
+ const state = value;
14
+ return (typeof state === "object" &&
15
+ state !== null &&
16
+ state.sites instanceof Set &&
17
+ state.tags instanceof Map &&
18
+ typeof state.barrier === "object" &&
19
+ typeof state.policy === "object");
20
+ }
21
+ /**
22
+ * The page's shared record, created by the first loader.
23
+ *
24
+ * Inputs: the global object the record lives on (default `globalThis`). Output: the `PageState`.
25
+ * Errors: never throws. Side effects: defines one non-enumerable symbol property on `target` the
26
+ * first time (or when the page replaced it with something else).
27
+ */
28
+ export function pageState(target = globalThis) {
29
+ try {
30
+ const found = target[KEY];
31
+ if (isState(found))
32
+ return found;
33
+ const state = create();
34
+ Object.defineProperty(target, KEY, { value: state, configurable: true });
35
+ return state;
36
+ }
37
+ catch {
38
+ return create();
39
+ }
40
+ }
41
+ /**
42
+ * Claims a site key for the calling loader.
43
+ *
44
+ * Output: `true` for the first loader of the key, `false` for a repeated run (it must do nothing).
45
+ * An embed without a site key is never treated as a repeat. Errors: none. Side effects: records the key.
46
+ */
47
+ export function claimSite(state, siteKey) {
48
+ if (siteKey === undefined)
49
+ return true;
50
+ if (state.sites.has(siteKey))
51
+ return false;
52
+ state.sites.add(siteKey);
53
+ return true;
54
+ }
55
+ /**
56
+ * Claims `tag` for `siteKey` at `version`: `"mine"` for the first claim, `"same"` when the same
57
+ * site already holds that exact version, `"other"` otherwise (another version, or another site:
58
+ * the first definition wins and the caller warns). Another site is never "same", even at the same
59
+ * version: its component would reuse the first site's definition and data transport, so its
60
+ * visitors' submissions would go to the first site's workspace. That is the page-level tag
61
+ * collision of ADR 0018 and cannot be fixed on the client; the warning makes it visible.
62
+ * Errors: none. Side effects: records the first claim.
63
+ */
64
+ export function claimTag(state, tag, version, siteKey) {
65
+ const claim = `${siteKey ?? ""}\n${version}`;
66
+ const held = state.tags.get(tag);
67
+ if (held === undefined) {
68
+ state.tags.set(tag, claim);
69
+ return "mine";
70
+ }
71
+ return held === claim ? "same" : "other";
72
+ }
73
+ //# sourceMappingURL=page-state.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page-state.js","sourceRoot":"","sources":["../../src/loader/page-state.ts"],"names":[],"mappings":"AAmBA,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AA0B9D,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;AAExC,SAAS,MAAM;IACb,OAAO;QACL,KAAK,EAAE,IAAI,GAAG,EAAE;QAChB,IAAI,EAAE,IAAI,GAAG,EAAE;QACf,OAAO,EAAE,wBAAwB,EAAE;QACnC,OAAO,EAAE,SAAS;QAClB,MAAM,EAAE,EAAE;KACX,CAAC;AACJ,CAAC;AAED,SAAS,OAAO,CAAC,KAAc;IAC7B,MAAM,KAAK,GAAG,KAAkC,CAAC;IACjD,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,KAAK,CAAC,KAAK,YAAY,GAAG;QAC1B,KAAK,CAAC,IAAI,YAAY,GAAG;QACzB,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ;QACjC,OAAO,KAAK,CAAC,MAAM,KAAK,QAAQ,CACjC,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CAAC,SAAiB,UAAU;IACnD,IAAI,CAAC;QACH,MAAM,KAAK,GAAI,MAAkC,CAAC,GAAG,CAAC,CAAC;QACvD,IAAI,OAAO,CAAC,KAAK,CAAC;YAAE,OAAO,KAAK,CAAC;QACjC,MAAM,KAAK,GAAG,MAAM,EAAE,CAAC;QACvB,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC,CAAC;QACzE,OAAO,KAAK,CAAC;IACf,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,MAAM,EAAE,CAAC;IAClB,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,KAAgB,EAAE,OAA2B;IACrE,IAAI,OAAO,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IACvC,IAAI,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC;QAAE,OAAO,KAAK,CAAC;IAC3C,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACzB,OAAO,IAAI,CAAC;AACd,CAAC;AAKD;;;;;;;;GAQG;AACH,MAAM,UAAU,QAAQ,CACtB,KAAgB,EAChB,GAAW,EACX,OAAe,EACf,OAA2B;IAE3B,MAAM,KAAK,GAAG,GAAG,OAAO,IAAI,EAAE,KAAK,OAAO,EAAE,CAAC;IAC7C,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACjC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;QACvB,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QAC3B,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,OAAO,IAAI,KAAK,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The time limit and the single retry around the loader's module imports (TAG-307). A dynamic
3
+ * `import()` cannot be aborted and a hung CDN connection never settles it, so each attempt races a
4
+ * timer; a failure is tried once more after a short backoff. The retry only refetches on a
5
+ * network or HTTP failure (404, 5xx, refused); a hung connection re-enters the same module map
6
+ * fetch, and a throwing or truncated (parse error) module returns the cached error. No query
7
+ * cache-busting: it would break the `moduleUrl` origin/path rule and URL immutability. Golden rule 2: the result of a failed component is a rejection the caller already
8
+ * handles; nothing here reaches the host page.
9
+ */
10
+ import type { TrustedScriptUrl } from "./script-url.js";
11
+ /** What `import()` takes: a plain URL or, under Trusted Types, a `TrustedScriptURL` (TAG-310). */
12
+ export type ModuleUrl = string | TrustedScriptUrl;
13
+ /** How long one attempt may take before it is abandoned. */
14
+ export declare const IMPORT_TIMEOUT_MS = 10000;
15
+ /** The wait between the failed attempt and the one retry. */
16
+ export declare const RETRY_BACKOFF_MS = 1000;
17
+ /** The timer functions the module uses (injected so tests run on fake timers). */
18
+ export interface Timers {
19
+ readonly setTimeout: (callback: () => void, ms: number) => unknown;
20
+ readonly clearTimeout: (handle: never) => void;
21
+ }
22
+ /**
23
+ * Wraps an `import(url)` function.
24
+ *
25
+ * Inputs: the raw import and the timers. Output: a function with the same signature that resolves
26
+ * with the first successful attempt, or rejects with the error of the second attempt (`Error
27
+ * ("timeout")` when it was abandoned). At most two attempts, 10 s each, `RETRY_BACKOFF_MS`
28
+ * apart. Errors: only the rejection above; late results of an abandoned attempt are swallowed.
29
+ * Side effects: the imports and one timer per attempt (cleared when the attempt settles).
30
+ */
31
+ export declare function resilientImport(importModule: (url: ModuleUrl) => Promise<unknown>, timers: Timers): (url: ModuleUrl) => Promise<unknown>;
32
+ //# sourceMappingURL=resilient-import.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resilient-import.d.ts","sourceRoot":"","sources":["../../src/loader/resilient-import.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,kGAAkG;AAClG,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,gBAAgB,CAAC;AAElD,4DAA4D;AAC5D,eAAO,MAAM,iBAAiB,QAAS,CAAC;AAExC,6DAA6D;AAC7D,eAAO,MAAM,gBAAgB,OAAQ,CAAC;AAEtC,kFAAkF;AAClF,MAAM,WAAW,MAAM;IACrB,QAAQ,CAAC,UAAU,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,EAAE,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC;IACnE,QAAQ,CAAC,YAAY,EAAE,CAAC,MAAM,EAAE,KAAK,KAAK,IAAI,CAAC;CAChD;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAC7B,YAAY,EAAE,CAAC,GAAG,EAAE,SAAS,KAAK,OAAO,CAAC,OAAO,CAAC,EAClD,MAAM,EAAE,MAAM,GACb,CAAC,GAAG,EAAE,SAAS,KAAK,OAAO,CAAC,OAAO,CAAC,CA2BtC"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The time limit and the single retry around the loader's module imports (TAG-307). A dynamic
3
+ * `import()` cannot be aborted and a hung CDN connection never settles it, so each attempt races a
4
+ * timer; a failure is tried once more after a short backoff. The retry only refetches on a
5
+ * network or HTTP failure (404, 5xx, refused); a hung connection re-enters the same module map
6
+ * fetch, and a throwing or truncated (parse error) module returns the cached error. No query
7
+ * cache-busting: it would break the `moduleUrl` origin/path rule and URL immutability. Golden rule 2: the result of a failed component is a rejection the caller already
8
+ * handles; nothing here reaches the host page.
9
+ */
10
+ /** How long one attempt may take before it is abandoned. */
11
+ export const IMPORT_TIMEOUT_MS = 10_000;
12
+ /** The wait between the failed attempt and the one retry. */
13
+ export const RETRY_BACKOFF_MS = 1_000;
14
+ /**
15
+ * Wraps an `import(url)` function.
16
+ *
17
+ * Inputs: the raw import and the timers. Output: a function with the same signature that resolves
18
+ * with the first successful attempt, or rejects with the error of the second attempt (`Error
19
+ * ("timeout")` when it was abandoned). At most two attempts, 10 s each, `RETRY_BACKOFF_MS`
20
+ * apart. Errors: only the rejection above; late results of an abandoned attempt are swallowed.
21
+ * Side effects: the imports and one timer per attempt (cleared when the attempt settles).
22
+ */
23
+ export function resilientImport(importModule, timers) {
24
+ const attempt = (url) => new Promise((resolve, reject) => {
25
+ const timer = timers.setTimeout(() => {
26
+ reject(new Error("timeout"));
27
+ }, IMPORT_TIMEOUT_MS);
28
+ const done = () => {
29
+ timers.clearTimeout(timer);
30
+ };
31
+ // Both handlers are attached, so an abandoned attempt that fails late is still handled.
32
+ importModule(url).then((module) => {
33
+ done();
34
+ resolve(module);
35
+ }, (error) => {
36
+ done();
37
+ reject(error instanceof Error ? error : new Error("import failed"));
38
+ });
39
+ });
40
+ return (url) => attempt(url).catch(() => new Promise((resolve) => timers.setTimeout(resolve, RETRY_BACKOFF_MS)).then(() => attempt(url)));
41
+ }
42
+ //# sourceMappingURL=resilient-import.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resilient-import.js","sourceRoot":"","sources":["../../src/loader/resilient-import.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAOH,4DAA4D;AAC5D,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC;AAExC,6DAA6D;AAC7D,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC;AAQtC;;;;;;;;GAQG;AACH,MAAM,UAAU,eAAe,CAC7B,YAAkD,EAClD,MAAc;IAEd,MAAM,OAAO,GAAG,CAAC,GAAc,EAAE,EAAE,CACjC,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACvC,MAAM,KAAK,GAAG,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE;YACnC,MAAM,CAAC,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;QAC/B,CAAC,EAAE,iBAAiB,CAAC,CAAC;QACtB,MAAM,IAAI,GAAG,GAAG,EAAE;YAChB,MAAM,CAAC,YAAY,CAAC,KAAc,CAAC,CAAC;QACtC,CAAC,CAAC;QACF,wFAAwF;QACxF,YAAY,CAAC,GAAG,CAAC,CAAC,IAAI,CACpB,CAAC,MAAM,EAAE,EAAE;YACT,IAAI,EAAE,CAAC;YACP,OAAO,CAAC,MAAM,CAAC,CAAC;QAClB,CAAC,EACD,CAAC,KAAc,EAAE,EAAE;YACjB,IAAI,EAAE,CAAC;YACP,MAAM,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC;QACtE,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;IACL,OAAO,CAAC,GAAG,EAAE,EAAE,CACb,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CACtB,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CACrF,OAAO,CAAC,GAAG,CAAC,CACb,CACF,CAAC;AACN,CAAC"}
@@ -0,0 +1,45 @@
1
+ import type { RuntimeDiagnostics } from "../element/types.js";
2
+ import { type ModuleUrl, type Timers } from "./resilient-import.js";
3
+ import type { TrustedScriptUrl } from "./script-url.js";
4
+ export type { RuntimeModule } from "./page-state.js";
5
+ /** What the loader needs from the page and the platform. */
6
+ export interface LoaderPorts {
7
+ readonly document: Pick<Document, "readyState" | "addEventListener" | "querySelectorAll"> & {
8
+ readonly documentElement: Node;
9
+ };
10
+ /** `import(url)`; rejects when the module does not load. */
11
+ readonly importModule: (url: ModuleUrl) => Promise<unknown>;
12
+ /**
13
+ * Turns a module URL into what `importModule` receives, given the delivery origin (TAG-310:
14
+ * the `taggor` Trusted Types policy of `script-url.ts`). Throws for a URL outside the origin,
15
+ * which skips that module. Default: the URL itself.
16
+ */
17
+ readonly scriptUrl?: (url: string, origin: string) => string | TrustedScriptUrl;
18
+ /** A `MutationObserver` factory. */
19
+ readonly observe: (callback: () => void, target: Node) => {
20
+ disconnect(): void;
21
+ };
22
+ /** Where warnings go (default: silent; debug mode, TAG-315, passes the console). */
23
+ readonly diagnostics?: RuntimeDiagnostics | undefined;
24
+ /** The page's CSP nonce, read from the loader's own script tag. */
25
+ readonly nonce?: string | undefined;
26
+ /** Where `window.taggor` is installed (default `globalThis`). */
27
+ readonly target?: object;
28
+ /**
29
+ * Sends the render signal of a mounted component (TAG-816): `navigator.sendBeacon` in the
30
+ * browser. Absent: nothing is sent.
31
+ */
32
+ readonly beacon?: (url: URL, body: string) => void;
33
+ /** The timers of the download time limit and retry backoff (TAG-307; default the page's). */
34
+ readonly timers?: Timers;
35
+ }
36
+ /**
37
+ * Runs the loader.
38
+ *
39
+ * Inputs: the embedded configuration (unchecked) and the ports. Output: none; asynchronous work
40
+ * continues after it returns. Errors: never throws or rejects. Side effects: defines
41
+ * `window.taggor` (unless the page has its own), reads the DOM, downloads modules, defines custom
42
+ * elements, and holds one `MutationObserver` until every manifest tag was handled.
43
+ */
44
+ export declare function startLoader(raw: unknown, rawPorts: LoaderPorts): void;
45
+ //# sourceMappingURL=run.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../../src/loader/run.ts"],"names":[],"mappings":"AA4BA,OAAO,KAAK,EAAsB,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAGlF,OAAO,EAAmB,KAAK,SAAS,EAAE,KAAK,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAErF,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,YAAY,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAErD,4DAA4D;AAC5D,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE,YAAY,GAAG,kBAAkB,GAAG,kBAAkB,CAAC,GAAG;QAC1F,QAAQ,CAAC,eAAe,EAAE,IAAI,CAAC;KAChC,CAAC;IACF,4DAA4D;IAC5D,QAAQ,CAAC,YAAY,EAAE,CAAC,GAAG,EAAE,SAAS,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAC5D;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,MAAM,GAAG,gBAAgB,CAAC;IAChF,oCAAoC;IACpC,QAAQ,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,EAAE,MAAM,EAAE,IAAI,KAAK;QAAE,UAAU,IAAI,IAAI,CAAA;KAAE,CAAC;IACjF,oFAAoF;IACpF,QAAQ,CAAC,WAAW,CAAC,EAAE,kBAAkB,GAAG,SAAS,CAAC;IACtD,mEAAmE;IACnE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,iEAAiE;IACjE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IACnD,6FAA6F;IAC7F,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B;AAwED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,WAAW,GAAG,IAAI,CAsHrE"}
@@ -0,0 +1,217 @@
1
+ /**
2
+ * The site loader's logic (TAG-301): what runs on the host page when `/s/<siteKey>.js` is
3
+ * executed. Written against injected ports (`LoaderPorts`) so it is tested in happy-dom without a
4
+ * network; `entry.ts` wires the browser's own.
5
+ *
6
+ * Order of events: install `window.taggor` at once (it exists before anything is downloaded, ADR
7
+ * 0023); wait for the parser to finish (no `import()` while the document is still loading: the
8
+ * Firefox import map bug of docs/testing/compatibility.md §4.1); look in the DOM for the tags of
9
+ * the manifest only; download the runtime once and the bundle of each present tag only; register
10
+ * it with `createComponentElement`; keep one `MutationObserver` for tags a single-page app inserts
11
+ * later, until every tag of the manifest was handled.
12
+ *
13
+ * TAG-709: a component whose manifest declares `data` gets a lazy data transport (`lazy-transport.ts`)
14
+ * as `host.transport`; the data module is imported on its first submission, not before.
15
+ *
16
+ * TAG-307: every import has a 10 s limit and one retry after a backoff (`resilient-import.ts`); a
17
+ * component whose bundle never arrives, answers an error or throws on evaluation is skipped, its tag
18
+ * stays as the page wrote it (the fallback), and the other components are not affected.
19
+ *
20
+ * TAG-816: once a component is registered the loader reports it with one `beacon` (`sendBeacon` to
21
+ * `/e/<siteKey>` of the delivery origin, body: the published version hash), the browser half of the
22
+ * render count; the edge ingest is `apps/edge` `ingest-event.ts`.
23
+ *
24
+ * Golden rule 2: everything is inside `try`, failures are silent (`window.taggor` carries the
25
+ * diagnostics port, which only debug mode prints), and a bundle that does not load or does not
26
+ * match its published manifest only costs that component.
27
+ */
28
+ import { createHostApi, installHostApi } from "../host/api.js";
29
+ import { readLoaderConfig } from "./config.js";
30
+ import { createLazyTransport } from "./lazy-transport.js";
31
+ import { resilientImport } from "./resilient-import.js";
32
+ import { claimSite, claimTag, pageState } from "./page-state.js";
33
+ /**
34
+ * The page's own timers, wrapped: `setTimeout` and `clearTimeout` throw "Illegal invocation" in a
35
+ * browser when called as methods of another object (found by the TAG-307 chaos suite).
36
+ */
37
+ const pageTimers = {
38
+ setTimeout: (callback, ms) => setTimeout(callback, ms),
39
+ clearTimeout: (handle) => {
40
+ clearTimeout(handle);
41
+ },
42
+ };
43
+ /** The module a bundle exports: `export default defineTaggorComponent(...)`, untrusted until checked. */
44
+ function definitionOf(module) {
45
+ const definition = module?.default;
46
+ return definition ?? undefined;
47
+ }
48
+ /** The lazy transport of the site's data module; the import happens on first use. */
49
+ function dataTransport(data, importModule, csp) {
50
+ return createLazyTransport(async () => {
51
+ const module = (await importModule(data.moduleUrl));
52
+ return module.createDataTransport({
53
+ baseUrl: data.baseUrl,
54
+ siteKey: data.siteKey,
55
+ turnstileSiteKey: data.turnstileSiteKey,
56
+ // TAG-310: the Turnstile script the data module injects needs the nonce and a trusted URL.
57
+ nonce: csp.nonce,
58
+ scriptUrl: csp.scriptUrl,
59
+ });
60
+ });
61
+ }
62
+ /** The one warning of a tag two snippets publish in different versions (the first one stays). */
63
+ function warnConflict(diagnostics, tag) {
64
+ try {
65
+ diagnostics?.warn({
66
+ code: "tag-collision",
67
+ tag,
68
+ message: `<${tag}> is already loaded by another snippet; the first stays.`,
69
+ });
70
+ }
71
+ catch {
72
+ // A diagnostics sink that throws is not the page's problem.
73
+ }
74
+ }
75
+ /**
76
+ * Whether a bundle's own manifest agrees with the published one (TAG-417: the extracted manifest
77
+ * describes the Node branch, which can differ from the browser's). The published manifest names
78
+ * the tag and the contract version; any other tag or version is a divergence and the component is not
79
+ * registered. Props and events are not compared: the published manifest does not carry them yet.
80
+ */
81
+ function agrees(definition, entry) {
82
+ const manifest = definition?.manifest;
83
+ return (manifest?.tag === entry.tag &&
84
+ (manifest.contractVersion === undefined || manifest.contractVersion === entry.contractVersion));
85
+ }
86
+ /**
87
+ * Runs the loader.
88
+ *
89
+ * Inputs: the embedded configuration (unchecked) and the ports. Output: none; asynchronous work
90
+ * continues after it returns. Errors: never throws or rejects. Side effects: defines
91
+ * `window.taggor` (unless the page has its own), reads the DOM, downloads modules, defines custom
92
+ * elements, and holds one `MutationObserver` until every manifest tag was handled.
93
+ */
94
+ export function startLoader(raw, rawPorts) {
95
+ try {
96
+ const ports = {
97
+ ...rawPorts,
98
+ importModule: resilientImport(rawPorts.importModule, rawPorts.timers ?? pageTimers),
99
+ };
100
+ // TAG-610: shared by every loader of the page, so `window.taggor` covers all sites.
101
+ const state = pageState(ports.target);
102
+ const { barrier } = state;
103
+ installHostApi(createHostApi({
104
+ live: () => state.runtime?.liveComponents,
105
+ barrier,
106
+ ...(ports.diagnostics === undefined ? {} : { diagnostics: ports.diagnostics }),
107
+ }), ports.target);
108
+ const config = readLoaderConfig(raw);
109
+ if (config === undefined) {
110
+ // Another loader may still announce its tags; only a lone loader opens the barrier.
111
+ if (state.sites.size === 0)
112
+ barrier.expect([]);
113
+ return;
114
+ }
115
+ // TAG-610: the same site's snippet again (cache plugin, theme) does nothing.
116
+ if (!claimSite(state, config.siteKey))
117
+ return;
118
+ const toUrl = rawPorts.scriptUrl ?? ((url) => url);
119
+ // Async so a URL the policy refuses is a rejection like any failed download.
120
+ const importUrl = async (url) => ports.importModule(toUrl(url, new URL(config.runtimeUrl).origin));
121
+ const pending = new Map(config.components);
122
+ let runtimeLoad;
123
+ const loadRuntime = () => (runtimeLoad ??= importUrl(config.runtimeUrl).then((module) => {
124
+ state.runtime = module;
125
+ return state.runtime;
126
+ }));
127
+ const mount = async (entry) => {
128
+ try {
129
+ // TAG-335: `idle` waits in the lazy `loading.js`; a release without it (an older ring) or a
130
+ // failed download means "eager", never a skipped component.
131
+ if (entry.loading === "idle") {
132
+ await importUrl(new URL("loading.js", config.runtimeUrl).href).then((module) => module.idle(), () => undefined);
133
+ }
134
+ const [loaded, bundle] = await Promise.all([loadRuntime(), importUrl(entry.bundleUrl)]);
135
+ const definition = definitionOf(bundle);
136
+ if (definition !== undefined && agrees(definition, entry)) {
137
+ loaded.createComponentElement(definition, {
138
+ contractVersion: entry.contractVersion,
139
+ version: entry.version,
140
+ origin: config.runtimeUrl,
141
+ siteDefaults: entry.defaults,
142
+ ...(config.servedOn === undefined ? {} : { servedOn: config.servedOn }),
143
+ ...(ports.diagnostics === undefined ? {} : { diagnostics: ports.diagnostics }),
144
+ ...(ports.nonce ? { styleNonce: ports.nonce } : {}),
145
+ ...(config.data !== undefined
146
+ ? { transport: dataTransport(config.data, importUrl, ports) }
147
+ : {}),
148
+ });
149
+ if (config.siteKey)
150
+ ports.beacon?.(new URL(`/e/${config.siteKey}`, config.runtimeUrl), entry.version);
151
+ }
152
+ }
153
+ catch {
154
+ // Network, a bundle that throws on import or a broken runtime: this component is skipped.
155
+ }
156
+ finally {
157
+ barrier.settle(entry.tag);
158
+ }
159
+ };
160
+ /**
161
+ * Takes the pending tags that are in the DOM now. One query per call (all pending tags in one
162
+ * selector), so a mutation batch costs one document scan however long the manifest is.
163
+ */
164
+ const take = () => {
165
+ const present = [];
166
+ if (pending.size === 0)
167
+ return present;
168
+ const found = ports.document.querySelectorAll([...pending.keys()].join(","));
169
+ for (const element of found) {
170
+ const entry = pending.get(element.localName);
171
+ if (entry === undefined)
172
+ continue;
173
+ pending.delete(element.localName);
174
+ const claim = claimTag(state, entry.tag, entry.version, config.siteKey);
175
+ if (claim === "mine")
176
+ present.push(entry);
177
+ else if (claim === "other")
178
+ warnConflict(ports.diagnostics, entry.tag);
179
+ }
180
+ return present;
181
+ };
182
+ const start = () => {
183
+ try {
184
+ const initial = take();
185
+ barrier.expect(initial.map((entry) => entry.tag));
186
+ for (const entry of initial)
187
+ void mount(entry);
188
+ if (pending.size === 0)
189
+ return;
190
+ const observer = ports.observe(() => {
191
+ try {
192
+ for (const entry of take())
193
+ void mount(entry);
194
+ if (pending.size === 0)
195
+ observer.disconnect();
196
+ }
197
+ catch {
198
+ // A hostile DOM: try again on the next mutation.
199
+ }
200
+ }, ports.document.documentElement);
201
+ }
202
+ catch {
203
+ barrier.expect([]);
204
+ }
205
+ };
206
+ if (ports.document.readyState === "loading") {
207
+ ports.document.addEventListener("DOMContentLoaded", start, { once: true });
208
+ }
209
+ else {
210
+ start();
211
+ }
212
+ }
213
+ catch {
214
+ // Golden rule 2: the host page never sees a loader failure.
215
+ }
216
+ }
217
+ //# sourceMappingURL=run.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run.js","sourceRoot":"","sources":["../../src/loader/run.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAE/D,OAAO,EAAE,gBAAgB,EAAyC,MAAM,aAAa,CAAC;AACtF,OAAO,E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C,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;gBAClD,KAAK,MAAM,KAAK,IAAI,OAAO;oBAAE,KAAK,KAAK,CAAC,KAAK,CAAC,CAAC;gBAC/C,IAAI,OAAO,CAAC,IAAI,KAAK,CAAC;oBAAE,OAAO;gBAC/B,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE;oBAClC,IAAI,CAAC;wBACH,KAAK,MAAM,KAAK,IAAI,IAAI,EAAE;4BAAE,KAAK,KAAK,CAAC,KAAK,CAAC,CAAC;wBAC9C,IAAI,OAAO,CAAC,IAAI,KAAK,CAAC;4BAAE,QAAQ,CAAC,UAAU,EAAE,CAAC;oBAChD,CAAC;oBAAC,MAAM,CAAC;wBACP,iDAAiD;oBACnD,CAAC;gBACH,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC;YACrC,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;YACrB,CAAC;QACH,CAAC,CAAC;QAEF,IAAI,KAAK,CAAC,QAAQ,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;YAC5C,KAAK,CAAC,QAAQ,CAAC,gBAAgB,CAAC,kBAAkB,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QAC7E,CAAC;aAAM,CAAC;YACN,KAAK,EAAE,CAAC;QACV,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,4DAA4D;IAC9D,CAAC;AACH,CAAC"}
@@ -0,0 +1,50 @@
1
+ /**
2
+ * The loader's Trusted Types policy for `import()` URLs (TAG-310). A page that sends
3
+ * `require-trusted-types-for 'script'` may also list the policies it allows (`trusted-types
4
+ * taggor`); the loader owns exactly one, named `taggor`, and uses it for nothing but the URLs it
5
+ * passes to `import()`. It never creates a `default` policy (that would reroute the host page's
6
+ * own sinks) and never registers a policy that returns its input unchecked: the policy refuses
7
+ * any URL that is not http(s) on the origin the edge embedded, before a `TrustedScriptURL`
8
+ * exists. Dynamic `import()` is not a Trusted Types sink in today's browsers, so this is
9
+ * defense in depth and forward compatibility; it is also what makes the loader work on a page
10
+ * that lists `trusted-types taggor` for it.
11
+ *
12
+ * Golden rule 2: when the page's CSP does not list `taggor`, `createPolicy` throws; the loader
13
+ * catches it, warns once on the console and imports the checked string (documented fallback,
14
+ * `docs/guias/csp-e-taggor.md`). Lives in the loader, not the runtime: the runtime bundle is
15
+ * 5 B under its ceiling (ADR 0020), the loader has room.
16
+ */
17
+ /** What `createScriptURL` returns: a `TrustedScriptURL`, which `import()` reads as its string. */
18
+ export interface TrustedScriptUrl {
19
+ toString(): string;
20
+ }
21
+ /** The slice of `window.trustedTypes` the loader uses. */
22
+ export interface PolicyFactory {
23
+ createPolicy(name: string, rules: {
24
+ createScriptURL(input: string, origin: string): string;
25
+ }): {
26
+ createScriptURL(input: string, origin: string): TrustedScriptUrl;
27
+ };
28
+ }
29
+ /**
30
+ * What the loaders of one page share about the policy (TAG-610): `createPolicy("taggor")` a
31
+ * second time throws on a page whose `trusted-types` lacks `'allow-duplicates'`, so the second
32
+ * snippet reuses the first one's policy (or its refusal, without warning again).
33
+ */
34
+ export interface PolicyState {
35
+ policy?: ReturnType<PolicyFactory["createPolicy"]>;
36
+ tried?: boolean;
37
+ }
38
+ /**
39
+ * Builds the URL maker of a loader.
40
+ *
41
+ * Inputs: `window.trustedTypes` (or `undefined` where the browser has none), a warning sink and
42
+ * the page's shared `PolicyState` (default: a private one, for a loader that runs alone).
43
+ * Output: a function `(url, origin)` returning the string to hand to `import()`, a
44
+ * `TrustedScriptURL` when the `taggor` policy exists. Errors: the function throws a `TypeError`
45
+ * for a URL that is not http(s) on `origin`; it never throws for a refused policy. Side effects:
46
+ * creates the `taggor` policy on the first call of the page's first loader (not at load, so a page without Trusted Types
47
+ * pays nothing), and calls `warn` once if the page's CSP refuses it.
48
+ */
49
+ export declare function createScriptUrls(factory: PolicyFactory | undefined, warn: (message: string) => void, state?: PolicyState): (url: string, origin: string) => string | TrustedScriptUrl;
50
+ //# sourceMappingURL=script-url.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"script-url.d.ts","sourceRoot":"","sources":["../../src/loader/script-url.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,kGAAkG;AAClG,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,IAAI,MAAM,CAAC;CACpB;AAED,0DAA0D;AAC1D,MAAM,WAAW,aAAa;IAC5B,YAAY,CACV,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE;QAAE,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,GAChE;QAAE,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,gBAAgB,CAAA;KAAE,CAAC;CACzE;AAED;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,EAAE,UAAU,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC,CAAC;IACnD,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAcD;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,aAAa,GAAG,SAAS,EAClC,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,EAC/B,KAAK,GAAE,WAAgB,GACtB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,MAAM,GAAG,gBAAgB,CA+B5D"}
@@ -0,0 +1,71 @@
1
+ /**
2
+ * The loader's Trusted Types policy for `import()` URLs (TAG-310). A page that sends
3
+ * `require-trusted-types-for 'script'` may also list the policies it allows (`trusted-types
4
+ * taggor`); the loader owns exactly one, named `taggor`, and uses it for nothing but the URLs it
5
+ * passes to `import()`. It never creates a `default` policy (that would reroute the host page's
6
+ * own sinks) and never registers a policy that returns its input unchecked: the policy refuses
7
+ * any URL that is not http(s) on the origin the edge embedded, before a `TrustedScriptURL`
8
+ * exists. Dynamic `import()` is not a Trusted Types sink in today's browsers, so this is
9
+ * defense in depth and forward compatibility; it is also what makes the loader work on a page
10
+ * that lists `trusted-types taggor` for it.
11
+ *
12
+ * Golden rule 2: when the page's CSP does not list `taggor`, `createPolicy` throws; the loader
13
+ * catches it, warns once on the console and imports the checked string (documented fallback,
14
+ * `docs/guias/csp-e-taggor.md`). Lives in the loader, not the runtime: the runtime bundle is
15
+ * 5 B under its ceiling (ADR 0020), the loader has room.
16
+ */
17
+ /** Origins the Turnstile script may come from (the data module passes its origin, TAG-709). */
18
+ const TURNSTILE_ORIGIN = "https://challenges.cloudflare.com";
19
+ /** `url` when it is http(s) on `origin`; otherwise throws. */
20
+ function checkedUrl(url, origin) {
21
+ const parsed = new URL(url);
22
+ if (parsed.origin !== origin || !parsed.protocol.startsWith("http")) {
23
+ throw new TypeError("module URL outside the loader origin");
24
+ }
25
+ return parsed.href;
26
+ }
27
+ /**
28
+ * Builds the URL maker of a loader.
29
+ *
30
+ * Inputs: `window.trustedTypes` (or `undefined` where the browser has none), a warning sink and
31
+ * the page's shared `PolicyState` (default: a private one, for a loader that runs alone).
32
+ * Output: a function `(url, origin)` returning the string to hand to `import()`, a
33
+ * `TrustedScriptURL` when the `taggor` policy exists. Errors: the function throws a `TypeError`
34
+ * for a URL that is not http(s) on `origin`; it never throws for a refused policy. Side effects:
35
+ * creates the `taggor` policy on the first call of the page's first loader (not at load, so a page without Trusted Types
36
+ * pays nothing), and calls `warn` once if the page's CSP refuses it.
37
+ */
38
+ export function createScriptUrls(factory, warn, state = {}) {
39
+ return (url, origin) => {
40
+ const checked = checkedUrl(url, origin);
41
+ if (state.tried !== true && factory !== undefined) {
42
+ state.tried = true;
43
+ try {
44
+ // L1 (TAG-610 review): the accepted origins are pinned here, in the closure; the origin a
45
+ // caller passes to `createScriptURL` is ignored, so a page script that reaches the shared
46
+ // policy cannot mint a TrustedScriptURL for another origin.
47
+ const allowed = [origin, TURNSTILE_ORIGIN];
48
+ state.policy = factory.createPolicy("taggor", {
49
+ createScriptURL: (input) => {
50
+ const { origin: from } = new URL(input);
51
+ return checkedUrl(input, allowed.includes(from) ? from : "");
52
+ },
53
+ });
54
+ }
55
+ catch {
56
+ warn("Taggor: the page's trusted-types directive does not list 'taggor'; loading without it.");
57
+ }
58
+ }
59
+ if (state.policy === undefined)
60
+ return checked;
61
+ try {
62
+ return state.policy.createScriptURL(checked, origin);
63
+ }
64
+ catch {
65
+ // A second loader on another delivery origin than the one the policy pinned: `import()` is
66
+ // not a Trusted Types sink, so the string, already checked against its own origin, is used.
67
+ return checked;
68
+ }
69
+ };
70
+ }
71
+ //# sourceMappingURL=script-url.js.map