@relate/contrib-web 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 (191) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +242 -2
  3. package/conformance/example-site/blank.html +2 -0
  4. package/conformance/example-site/broken.html +35 -0
  5. package/conformance/example-site/index.html +35 -0
  6. package/conformance/example-site/notes.html +35 -0
  7. package/conformance/example-site/page-1.html +35 -0
  8. package/conformance/example-site/page-2.html +35 -0
  9. package/conformance/example-site/public/descriptor-b.json +16 -0
  10. package/conformance/example-site/public/descriptor-broken.json +16 -0
  11. package/conformance/example-site/public/descriptor-mismatch.json +16 -0
  12. package/conformance/example-site/public/descriptor.json +16 -0
  13. package/conformance/example-site/public/graph-b.json +14408 -0
  14. package/conformance/example-site/public/graph.json +10808 -0
  15. package/conformance/example-site/rel-page.ts +147 -0
  16. package/conformance/example-site/vite.config.ts +23 -0
  17. package/conformance/make-example.ts +22 -0
  18. package/conformance/run.ts +152 -0
  19. package/conformance/scenarios.ts +285 -0
  20. package/dist/application/options.d.ts +9 -0
  21. package/dist/application/options.js +64 -0
  22. package/dist/application/options.js.map +1 -0
  23. package/dist/application/ranges.d.ts +7 -0
  24. package/dist/application/ranges.js +14 -0
  25. package/dist/application/ranges.js.map +1 -0
  26. package/dist/application/storage.d.ts +34 -0
  27. package/dist/application/storage.js +60 -0
  28. package/dist/application/storage.js.map +1 -0
  29. package/dist/application/types.d.ts +49 -0
  30. package/dist/application/types.js +2 -0
  31. package/dist/application/types.js.map +1 -0
  32. package/dist/build/canonical.d.ts +5 -0
  33. package/dist/build/canonical.js +26 -0
  34. package/dist/build/canonical.js.map +1 -0
  35. package/dist/build/descriptor.d.ts +16 -0
  36. package/dist/build/descriptor.js +13 -0
  37. package/dist/build/descriptor.js.map +1 -0
  38. package/dist/build/index.d.ts +6 -0
  39. package/dist/build/index.js +6 -0
  40. package/dist/build/index.js.map +1 -0
  41. package/dist/build/inline.d.ts +16 -0
  42. package/dist/build/inline.js +44 -0
  43. package/dist/build/inline.js.map +1 -0
  44. package/dist/build/prepare.d.ts +11 -0
  45. package/dist/build/prepare.js +25 -0
  46. package/dist/build/prepare.js.map +1 -0
  47. package/dist/catalog/api.d.ts +30 -0
  48. package/dist/catalog/api.js +56 -0
  49. package/dist/catalog/api.js.map +1 -0
  50. package/dist/catalog/catalog.d.ts +51 -0
  51. package/dist/catalog/catalog.js +104 -0
  52. package/dist/catalog/catalog.js.map +1 -0
  53. package/dist/catalog/directory.d.ts +30 -0
  54. package/dist/catalog/directory.js +116 -0
  55. package/dist/catalog/directory.js.map +1 -0
  56. package/dist/catalog/file.d.ts +12 -0
  57. package/dist/catalog/file.js +84 -0
  58. package/dist/catalog/file.js.map +1 -0
  59. package/dist/catalog/index.d.ts +10 -0
  60. package/dist/catalog/index.js +12 -0
  61. package/dist/catalog/index.js.map +1 -0
  62. package/dist/catalog/memory-storage.d.ts +16 -0
  63. package/dist/catalog/memory-storage.js +56 -0
  64. package/dist/catalog/memory-storage.js.map +1 -0
  65. package/dist/catalog/opfs-storage.d.ts +10 -0
  66. package/dist/catalog/opfs-storage.js +71 -0
  67. package/dist/catalog/opfs-storage.js.map +1 -0
  68. package/dist/catalog/remove.d.ts +21 -0
  69. package/dist/catalog/remove.js +85 -0
  70. package/dist/catalog/remove.js.map +1 -0
  71. package/dist/catalog/storage.d.ts +11 -0
  72. package/dist/catalog/storage.js +4 -0
  73. package/dist/catalog/storage.js.map +1 -0
  74. package/dist/catalog/types.d.ts +68 -0
  75. package/dist/catalog/types.js +4 -0
  76. package/dist/catalog/types.js.map +1 -0
  77. package/dist/client/application.d.ts +32 -0
  78. package/dist/client/application.js +99 -0
  79. package/dist/client/application.js.map +1 -0
  80. package/dist/client/client.d.ts +2 -0
  81. package/dist/client/client.js +445 -0
  82. package/dist/client/client.js.map +1 -0
  83. package/dist/client/diagnostic.d.ts +6 -0
  84. package/dist/client/diagnostic.js +35 -0
  85. package/dist/client/diagnostic.js.map +1 -0
  86. package/dist/client/events.d.ts +27 -0
  87. package/dist/client/events.js +31 -0
  88. package/dist/client/events.js.map +1 -0
  89. package/dist/client/failure.d.ts +13 -0
  90. package/dist/client/failure.js +40 -0
  91. package/dist/client/failure.js.map +1 -0
  92. package/dist/client/scope.d.ts +15 -0
  93. package/dist/client/scope.js +39 -0
  94. package/dist/client/scope.js.map +1 -0
  95. package/dist/client/status.d.ts +14 -0
  96. package/dist/client/status.js +34 -0
  97. package/dist/client/status.js.map +1 -0
  98. package/dist/client/subscriptions.d.ts +31 -0
  99. package/dist/client/subscriptions.js +64 -0
  100. package/dist/client/subscriptions.js.map +1 -0
  101. package/dist/client/types.d.ts +82 -0
  102. package/dist/client/types.js +2 -0
  103. package/dist/client/types.js.map +1 -0
  104. package/dist/config.d.ts +30 -0
  105. package/dist/config.js +28 -0
  106. package/dist/config.js.map +1 -0
  107. package/dist/conformance/run.d.ts +61 -0
  108. package/dist/conformance/run.js +121 -0
  109. package/dist/conformance/run.js.map +1 -0
  110. package/dist/conformance/scenarios.d.ts +25 -0
  111. package/dist/conformance/scenarios.js +257 -0
  112. package/dist/conformance/scenarios.js.map +1 -0
  113. package/dist/data/descriptor.d.ts +20 -0
  114. package/dist/data/descriptor.js +75 -0
  115. package/dist/data/descriptor.js.map +1 -0
  116. package/dist/data/types.d.ts +41 -0
  117. package/dist/data/types.js +3 -0
  118. package/dist/data/types.js.map +1 -0
  119. package/dist/data/validate.d.ts +22 -0
  120. package/dist/data/validate.js +118 -0
  121. package/dist/data/validate.js.map +1 -0
  122. package/dist/errors.d.ts +9 -0
  123. package/dist/errors.js +14 -0
  124. package/dist/errors.js.map +1 -0
  125. package/dist/index.d.ts +17 -0
  126. package/dist/index.js +8 -0
  127. package/dist/index.js.map +1 -0
  128. package/dist/profiles/index.d.ts +5 -0
  129. package/dist/profiles/index.js +9 -0
  130. package/dist/profiles/index.js.map +1 -0
  131. package/dist/profiles/per-page.d.ts +2 -0
  132. package/dist/profiles/per-page.js +11 -0
  133. package/dist/profiles/per-page.js.map +1 -0
  134. package/dist/profiles/persistent.d.ts +5 -0
  135. package/dist/profiles/persistent.js +14 -0
  136. package/dist/profiles/persistent.js.map +1 -0
  137. package/dist/validate.d.ts +5 -0
  138. package/dist/validate.js +4 -0
  139. package/dist/validate.js.map +1 -0
  140. package/dist/worker/application/classify.d.ts +30 -0
  141. package/dist/worker/application/classify.js +57 -0
  142. package/dist/worker/application/classify.js.map +1 -0
  143. package/dist/worker/application/init.d.ts +12 -0
  144. package/dist/worker/application/init.js +64 -0
  145. package/dist/worker/application/init.js.map +1 -0
  146. package/dist/worker/application/lifecycle.d.ts +29 -0
  147. package/dist/worker/application/lifecycle.js +86 -0
  148. package/dist/worker/application/lifecycle.js.map +1 -0
  149. package/dist/worker/application/losses.d.ts +11 -0
  150. package/dist/worker/application/losses.js +61 -0
  151. package/dist/worker/application/losses.js.map +1 -0
  152. package/dist/worker/application/migrate.d.ts +16 -0
  153. package/dist/worker/application/migrate.js +54 -0
  154. package/dist/worker/application/migrate.js.map +1 -0
  155. package/dist/worker/application/open-store.d.ts +31 -0
  156. package/dist/worker/application/open-store.js +60 -0
  157. package/dist/worker/application/open-store.js.map +1 -0
  158. package/dist/worker/application/seed.d.ts +14 -0
  159. package/dist/worker/application/seed.js +50 -0
  160. package/dist/worker/application/seed.js.map +1 -0
  161. package/dist/worker/application/transfer.d.ts +31 -0
  162. package/dist/worker/application/transfer.js +134 -0
  163. package/dist/worker/application/transfer.js.map +1 -0
  164. package/dist/worker/cache.d.ts +15 -0
  165. package/dist/worker/cache.js +45 -0
  166. package/dist/worker/cache.js.map +1 -0
  167. package/dist/worker/encode.d.ts +1 -0
  168. package/dist/worker/encode.js +43 -0
  169. package/dist/worker/encode.js.map +1 -0
  170. package/dist/worker/engine.d.ts +35 -0
  171. package/dist/worker/engine.js +99 -0
  172. package/dist/worker/engine.js.map +1 -0
  173. package/dist/worker/legacy-copy.d.ts +2 -0
  174. package/dist/worker/legacy-copy.js +19 -0
  175. package/dist/worker/legacy-copy.js.map +1 -0
  176. package/dist/worker/loader.d.ts +40 -0
  177. package/dist/worker/loader.js +96 -0
  178. package/dist/worker/loader.js.map +1 -0
  179. package/dist/worker/protocol.d.ts +121 -0
  180. package/dist/worker/protocol.js +2 -0
  181. package/dist/worker/protocol.js.map +1 -0
  182. package/dist/worker/store.d.ts +12 -0
  183. package/dist/worker/store.js +39 -0
  184. package/dist/worker/store.js.map +1 -0
  185. package/dist/worker/sweep.d.ts +8 -0
  186. package/dist/worker/sweep.js +21 -0
  187. package/dist/worker/sweep.js.map +1 -0
  188. package/dist/worker/worker.d.ts +1 -0
  189. package/dist/worker/worker.js +198 -0
  190. package/dist/worker/worker.js.map +1 -0
  191. package/package.json +81 -3
@@ -0,0 +1,85 @@
1
+ // Removing a graph (ADR-010 2.5, specs/007 contracts/catalog-api.md): intent first, then the directory, then the entry.
2
+ // Only a tab that does not hold the store can remove it, because the browser refuses to remove files another tab has open.
3
+ import { CatalogUnavailable } from "./catalog.js";
4
+ import { StoreHeldError } from "./directory.js";
5
+ import { splitStoreName } from "./file.js";
6
+ export class RemoveRefused extends Error {
7
+ reason;
8
+ constructor(reason, message) {
9
+ super(message);
10
+ this.name = "RemoveRefused";
11
+ this.reason = reason;
12
+ }
13
+ }
14
+ /** An entry as input again: the catalog stamps its own `updatedAt` on every write. */
15
+ const withoutTime = (entry) => {
16
+ const { updatedAt: _updatedAt, ...rest } = entry;
17
+ void _updatedAt;
18
+ return rest;
19
+ };
20
+ export async function performRemoval(catalog, directories, store, options) {
21
+ const parts = splitStoreName(store);
22
+ if (!parts)
23
+ throw new RemoveRefused("not-a-store", `"${store}" is not the name of a store the core keeps (app-<name> or rel-<id>)`);
24
+ if (options?.kind !== "application" && options?.kind !== "cache") {
25
+ throw new RemoveRefused("kind-mismatch", 'say what is being removed: { kind: "application" } or { kind: "cache" }');
26
+ }
27
+ // The stated kind must match the store's name before anything is read, so removing an application graph's data
28
+ // always takes an explicit "application", whatever the catalog holds or whether it can be read.
29
+ if (parts.kind !== options.kind) {
30
+ const named = parts.kind === "application" ? "an application graph" : "a cache";
31
+ throw new RemoveRefused("kind-mismatch", `${store} is named as ${named}; say so to remove it (kind: "${parts.kind}")`);
32
+ }
33
+ return directories.serialize(store, () => removeStore(catalog, directories, store, parts.kind));
34
+ }
35
+ async function removeStore(catalog, directories, store, kind) {
36
+ // Intent first. A catalog that cannot be used does not stop a removal the caller asked for.
37
+ let previous;
38
+ let written;
39
+ let malformed; // the stored text of an entry that is there but not valid
40
+ let usable = true;
41
+ try {
42
+ const result = await catalog.update(store, (current, raw) => {
43
+ previous = current;
44
+ if (!current) {
45
+ malformed = raw !== undefined ? JSON.stringify(raw) : undefined;
46
+ return undefined;
47
+ }
48
+ // `target` belongs to a migrating entry only; an entry that kept it would not be valid in the state `removing`.
49
+ const { target: _target, ...rest } = withoutTime(current);
50
+ void _target;
51
+ return { ...rest, state: "removing" };
52
+ });
53
+ written = result.changed ? (result.entry ?? undefined) : undefined;
54
+ }
55
+ catch (e) {
56
+ if (!(e instanceof CatalogUnavailable))
57
+ throw e;
58
+ usable = false;
59
+ }
60
+ const hadDirectory = await directories.exists(store);
61
+ try {
62
+ await directories.remove(store);
63
+ }
64
+ catch (e) {
65
+ if (usable && previous && written)
66
+ await restore(catalog, store, previous, written);
67
+ if (e instanceof StoreHeldError)
68
+ throw new RemoveRefused("locked", e.message);
69
+ throw e;
70
+ }
71
+ if (usable && written) {
72
+ await catalog.update(store, (current) => (current && current.state === "removing" && current.updatedAt === written.updatedAt ? null : undefined));
73
+ }
74
+ else if (usable && malformed !== undefined) {
75
+ // An entry that was not valid goes with its store, unless someone has written it since.
76
+ await catalog.update(store, (current, raw) => (current === undefined && raw !== undefined && JSON.stringify(raw) === malformed ? null : undefined));
77
+ }
78
+ const discarded = previous || malformed !== undefined ? (kind === "application" ? "application data" : "cache") : hadDirectory ? "unregistered directory" : "nothing";
79
+ return { store, kind, entry: previous ?? null, discarded };
80
+ }
81
+ /** Put the entry back as it was, but only if nobody has changed it since the removal wrote `removing`. */
82
+ async function restore(catalog, store, previous, written) {
83
+ await catalog.update(store, (current) => (current && current.state === "removing" && current.updatedAt === written.updatedAt ? withoutTime(previous) : undefined));
84
+ }
85
+ //# sourceMappingURL=remove.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"remove.js","sourceRoot":"","sources":["../../src/catalog/remove.ts"],"names":[],"mappings":"AAAA,wHAAwH;AACxH,2HAA2H;AAC3H,OAAO,EAAE,kBAAkB,EAAgB,MAAM,cAAc,CAAC;AAChE,OAAO,EAAE,cAAc,EAAyB,MAAM,gBAAgB,CAAC;AACvE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAK3C,MAAM,OAAO,aAAc,SAAQ,KAAK;IAC7B,MAAM,CAAgB;IAC/B,YAAY,MAAqB,EAAE,OAAe;QAChD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;QAC5B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AAgBD,sFAAsF;AACtF,MAAM,WAAW,GAAG,CAAC,KAAY,EAAc,EAAE;IAC/C,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,IAAI,EAAE,GAAG,KAA2C,CAAC;IACvF,KAAK,UAAU,CAAC;IAChB,OAAO,IAA6B,CAAC;AACvC,CAAC,CAAC;AAEF,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,OAAgB,EAAE,WAA6B,EAAE,KAAa,EAAE,OAAsB;IACzH,MAAM,KAAK,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;IACpC,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,aAAa,CAAC,aAAa,EAAE,IAAI,KAAK,sEAAsE,CAAC,CAAC;IACpI,IAAI,OAAO,EAAE,IAAI,KAAK,aAAa,IAAI,OAAO,EAAE,IAAI,KAAK,OAAO,EAAE,CAAC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OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,OAAO,KAAK,SAAS,IAAI,GAAG,KAAK,SAAS,IAAI,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC;IACtJ,CAAC;IAED,MAAM,SAAS,GACb,QAAQ,IAAI,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,aAAa,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,wBAAwB,CAAC,CAAC,CAAC,SAAS,CAAC;IACtJ,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,IAAI,IAAI,EAAE,SAAS,EAAE,CAAC;AAC7D,CAAC;AAED,0GAA0G;AAC1G,KAAK,UAAU,OAAO,CAAC,OAAgB,EAAE,KAAa,EAAE,QAAe,EAAE,OAAc;IACrF,MAAM,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,IAAI,OAAO,CAAC,KAAK,KAAK,UAAU,IAAI,OAAO,CAAC,SAAS,KAAK,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC;AACrK,CAAC"}
@@ -0,0 +1,11 @@
1
+ export type LockMode = "shared" | "exclusive";
2
+ export interface CatalogStorage {
3
+ /** The file's text, or null when there is no file. Callers hold a lock. */
4
+ read(): Promise<string | null>;
5
+ /** Replace the file whole. Callers hold the exclusive lock. */
6
+ replace(text: string): Promise<void>;
7
+ /** Delete the file; absence is not an error. Callers hold the exclusive lock. */
8
+ remove(): Promise<void>;
9
+ /** Run `fn` holding the catalog lock: shared for reading, exclusive for writing. */
10
+ withLock<T>(mode: LockMode, fn: () => Promise<T>): Promise<T>;
11
+ }
@@ -0,0 +1,4 @@
1
+ // The seam between the catalog and where its file lives (specs/007 research R1). The browser implementation keeps the
2
+ // file in the origin-private storage under a Web Lock; the in-memory one lets the lifecycle run in Node tests.
3
+ export {};
4
+ //# sourceMappingURL=storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.js","sourceRoot":"","sources":["../../src/catalog/storage.ts"],"names":[],"mappings":"AAAA,sHAAsH;AACtH,+GAA+G"}
@@ -0,0 +1,68 @@
1
+ export type ApplicationState = "ready" | "seeding" | "migrating" | "importing" | "resetting" | "removing";
2
+ export type CacheState = "ready" | "loading" | "removing";
3
+ export type State = ApplicationState | CacheState;
4
+ export declare const APPLICATION_STATES: readonly ApplicationState[];
5
+ export declare const CACHE_STATES: readonly CacheState[];
6
+ export interface ApplicationEntry {
7
+ kind: "application";
8
+ name: string;
9
+ state: ApplicationState;
10
+ /** The schema version as of the last completed seed or migration step. */
11
+ version: number;
12
+ /** The version being migrated to; present exactly when `state` is `migrating`. */
13
+ target?: number;
14
+ /** Whether the seed or import that last filled the store held at least one node. */
15
+ seeded: boolean;
16
+ /** ISO 8601 time of the last state change. */
17
+ updatedAt: string;
18
+ }
19
+ export interface CacheEntry {
20
+ kind: "cache";
21
+ name: string;
22
+ state: CacheState;
23
+ /** The descriptor's revision as of the last complete load; required when `state` is `ready`. */
24
+ revision?: string;
25
+ updatedAt: string;
26
+ }
27
+ export type Entry = ApplicationEntry | CacheEntry;
28
+ /** An entry as a caller supplies it: `updatedAt` is set by the catalog on every write. */
29
+ export type EntryInput = Omit<ApplicationEntry, "updatedAt"> | Omit<CacheEntry, "updatedAt">;
30
+ /** The file's parsed shape. Entries stay raw until used, so one malformed entry does not block the others. */
31
+ export interface CatalogFile {
32
+ format: number;
33
+ graphs: Record<string, unknown>;
34
+ }
35
+ export type ReadResult = {
36
+ status: "missing";
37
+ } | {
38
+ status: "unreadable";
39
+ message: string;
40
+ } | {
41
+ status: "newer";
42
+ format: number;
43
+ } | {
44
+ status: "ok";
45
+ file: CatalogFile;
46
+ };
47
+ export type EntryCheck = {
48
+ ok: true;
49
+ entry: Entry;
50
+ } | {
51
+ ok: false;
52
+ reason: string;
53
+ };
54
+ /** One store, one entry, or both (data-model.md, listing item). */
55
+ export interface ListingItem {
56
+ store: string;
57
+ kind?: "application" | "cache";
58
+ name?: string;
59
+ state?: State;
60
+ version?: number;
61
+ revision?: string;
62
+ registered: boolean;
63
+ present: boolean;
64
+ status: "ok" | "unregistered" | "missing";
65
+ /** Only when sizes were requested. */
66
+ size?: number;
67
+ modified?: number;
68
+ }
@@ -0,0 +1,4 @@
1
+ // The catalog's shapes (ADR-010 2.1, specs/007 data-model.md).
2
+ export const APPLICATION_STATES = ["ready", "seeding", "migrating", "importing", "resetting", "removing"];
3
+ export const CACHE_STATES = ["ready", "loading", "removing"];
4
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/catalog/types.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAM/D,MAAM,CAAC,MAAM,kBAAkB,GAAgC,CAAC,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,UAAU,CAAC,CAAC;AACvI,MAAM,CAAC,MAAM,YAAY,GAA0B,CAAC,OAAO,EAAE,SAAS,EAAE,UAAU,CAAC,CAAC"}
@@ -0,0 +1,32 @@
1
+ import { type NormalisedApplication } from "../application/options.js";
2
+ import { StorageHolder, storageEstimate } from "../application/storage.js";
3
+ import type { StorageStatus } from "../application/types.js";
4
+ import type { ApplicationInit } from "../worker/protocol.js";
5
+ import type { StorageMessage } from "../worker/protocol.js";
6
+ import type { ClientOptions } from "./types.js";
7
+ /** A problem with the seed found on the page: reported as the matching init failure. */
8
+ export declare class SeedProblem extends Error {
9
+ readonly reason: "worker-error" | "invalid-descriptor";
10
+ constructor(reason: "worker-error" | "invalid-descriptor", message: string);
11
+ }
12
+ export declare class ApplicationSupport {
13
+ readonly app: NormalisedApplication;
14
+ readonly holder: StorageHolder;
15
+ /** `reset` for the one start that `resetApplication()` asks for; every other start (a replaced worker) opens. */
16
+ intent: "open" | "reset";
17
+ /** The page has been hidden and its worker ended; a restore from the back-forward cache starts it again. */
18
+ released: boolean;
19
+ private listeners;
20
+ constructor(options: ClientOptions, onStorage: (status: StorageStatus) => void);
21
+ /** The application part of the init message; `seed` is resolved here, on the page. */
22
+ init(): Promise<ApplicationInit>;
23
+ /** The instance is ready: a reset, if one was asked for, has happened. Later starts open. */
24
+ confirmed(): void;
25
+ /** A storage report from the worker; the page adds what only it can ask the browser. */
26
+ onStorage(msg: StorageMessage): void;
27
+ /** Listen for the page leaving (`onHide`) and being restored from the back-forward cache (`onRestore`). */
28
+ watchPage(onHide: () => void, onRestore: () => void): void;
29
+ unwatchPage(): void;
30
+ requestPersistence: () => Promise<boolean>;
31
+ storageEstimate: typeof storageEstimate;
32
+ }
@@ -0,0 +1,99 @@
1
+ // The application-mode half of the client (ADR-009): resolving the seed into an init message, the storage status,
2
+ // the departing page releasing its store, and the calls that are only meaningful with an application graph.
3
+ import { validateApplication } from "../application/options.js";
4
+ import { readPersisted, requestPersistence, StorageHolder, storageEstimate } from "../application/storage.js";
5
+ import { validateDescriptor } from "../data/descriptor.js";
6
+ const absolute = (url, base) => new URL(String(url), base).href;
7
+ /** A problem with the seed found on the page: reported as the matching init failure. */
8
+ export class SeedProblem extends Error {
9
+ reason;
10
+ constructor(reason, message) {
11
+ super(message);
12
+ this.reason = reason;
13
+ }
14
+ }
15
+ /**
16
+ * Where the seed is. Inline data is checked here; a URL is passed on unresolved, because fetching it on every start would
17
+ * download a seed that a ready store never uses. The worker fetches it only when the store needs it.
18
+ */
19
+ function resolveSeed(seed) {
20
+ if (typeof seed === "string")
21
+ return { document: null, dataUrl: null, descriptor: null, url: absolute(seed, location.href) };
22
+ if (seed && typeof seed === "object" && "nodes" in seed) {
23
+ return { document: seed, dataUrl: null, descriptor: null, url: null };
24
+ }
25
+ const checked = validateDescriptor(seed);
26
+ if (!checked.ok)
27
+ throw new SeedProblem("invalid-descriptor", `${checked.path}: ${checked.message}`);
28
+ const descriptor = checked.descriptor;
29
+ return { document: null, dataUrl: absolute(descriptor.data.url, location.href), descriptor, url: null };
30
+ }
31
+ export class ApplicationSupport {
32
+ app;
33
+ holder;
34
+ /** `reset` for the one start that `resetApplication()` asks for; every other start (a replaced worker) opens. */
35
+ intent = "open";
36
+ /** The page has been hidden and its worker ended; a restore from the back-forward cache starts it again. */
37
+ released = false;
38
+ listeners = [];
39
+ constructor(options, onStorage) {
40
+ if (options.application && options.graph)
41
+ throw new Error("createClient takes exactly one of `graph` and `application`");
42
+ this.app = validateApplication(options.application);
43
+ this.holder = new StorageHolder(onStorage);
44
+ }
45
+ /** The application part of the init message; `seed` is resolved here, on the page. */
46
+ async init() {
47
+ const { app } = this;
48
+ // The reset intent stays until the instance is ready (the client calls `confirmed()`), so a start that fails after a
49
+ // reset was asked for is retried as a reset, not as an open that would pass for success.
50
+ const intent = this.intent;
51
+ return {
52
+ name: app.name,
53
+ version: app.version,
54
+ intent,
55
+ seed: resolveSeed(app.seed),
56
+ indexes: app.indexes,
57
+ migrations: app.migrations,
58
+ onUnavailable: app.onUnavailable,
59
+ onError: app.onError,
60
+ whenLocked: app.whenLocked,
61
+ };
62
+ }
63
+ /** The instance is ready: a reset, if one was asked for, has happened. Later starts open. */
64
+ confirmed() {
65
+ this.intent = "open";
66
+ }
67
+ /** A storage report from the worker; the page adds what only it can ask the browser. */
68
+ onStorage(msg) {
69
+ this.holder.apply({ kind: msg.kind, ...(msg.reason ? { reason: msg.reason } : {}) });
70
+ void readPersisted().then((p) => this.holder.setPersisted(p));
71
+ }
72
+ /** Listen for the page leaving (`onHide`) and being restored from the back-forward cache (`onRestore`). */
73
+ watchPage(onHide, onRestore) {
74
+ const target = globalThis;
75
+ if (!target.addEventListener)
76
+ return;
77
+ const hide = () => onHide();
78
+ const show = (e) => {
79
+ if (e.persisted)
80
+ onRestore();
81
+ };
82
+ this.listeners = [["pagehide", hide], ["pageshow", show]];
83
+ for (const [type, listener] of this.listeners)
84
+ target.addEventListener(type, listener);
85
+ }
86
+ unwatchPage() {
87
+ const target = globalThis;
88
+ for (const [type, listener] of this.listeners)
89
+ target.removeEventListener?.(type, listener);
90
+ this.listeners = [];
91
+ }
92
+ requestPersistence = async () => {
93
+ const granted = await requestPersistence();
94
+ this.holder.setPersisted((await readPersisted()) ?? granted);
95
+ return granted;
96
+ };
97
+ storageEstimate = storageEstimate;
98
+ }
99
+ //# sourceMappingURL=application.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"application.js","sourceRoot":"","sources":["../../src/client/application.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAClH,4GAA4G;AAC5G,OAAO,EAAE,mBAAmB,EAA8B,MAAM,2BAA2B,CAAC;AAC5F,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAE9G,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAM3D,MAAM,QAAQ,GAAG,CAAC,GAAiB,EAAE,IAAY,EAAU,EAAE,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC;AAE9F,wFAAwF;AACxF,MAAM,OAAO,WAAY,SAAQ,KAAK;IACf,MAAM;IAA3B,YAAqB,MAA6C,EAAE,OAAe;QACjF,KAAK,CAAC,OAAO,CAAC,CAAC;sBADI,MAAM;IAE3B,CAAC;CACF;AAED;;;GAGG;AACH,SAAS,WAAW,CAAC,IAAmC;IACtD,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,GAAG,EAAE,QAA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