@inkandswitch/patchwork-bootloader 0.4.2 → 0.4.4

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/src/types.ts CHANGED
@@ -161,7 +161,28 @@ export interface HandoffResponseMessage {
161
161
  response: HandoffResponse;
162
162
  }
163
163
 
164
- export type HandoffReplyMessage = HandoffCachedMessage | HandoffResponseMessage;
164
+ /**
165
+ * Automerge worker → service worker: fail the request as a network error
166
+ * rather than serving any response at all.
167
+ *
168
+ * "Heads haven't arrived yet" is not a 404 — the document may well exist, so
169
+ * a status implying it doesn't is a lie any HTTP cache is entitled to store.
170
+ * A network error is the honest answer and isn't storable as a response.
171
+ *
172
+ * This does *not* help with `import()`: the ES module map memoizes failed
173
+ * fetches, network errors included, so a retry needs a distinct URL either
174
+ * way (see `importModule` in patchwork-filesystem).
175
+ */
176
+ export interface HandoffAbortMessage {
177
+ id: string;
178
+ type: "abort";
179
+ reason: string;
180
+ }
181
+
182
+ export type HandoffReplyMessage =
183
+ | HandoffCachedMessage
184
+ | HandoffResponseMessage
185
+ | HandoffAbortMessage;
165
186
 
166
187
  /**
167
188
  * Automerge worker → world: broadcast once on startup so the service worker
@@ -5,6 +5,7 @@ import type {
5
5
  } from "./patchwork-plugin.js";
6
6
  import { readFileSync } from "node:fs";
7
7
  import { createRequire } from "node:module";
8
+ import { fileURLToPath } from "node:url";
8
9
  const require = createRequire(import.meta.url);
9
10
 
10
11
  /**
@@ -19,8 +20,35 @@ export const builtins = externals.reduce(
19
20
  );
20
21
 
21
22
  /**
22
- * merge the importmap option with our builtins
23
+ * pretend the import came from inside this package, so node_modules resolution
24
+ * walks up from *our* directory and finds our copy of each external. that's why
25
+ * a consuming site never has to install them or agree with us about versions.
23
26
  */
27
+ const self = fileURLToPath(import.meta.url);
28
+
29
+ /**
30
+ * resolve an external from our node_modules rather than the site's.
31
+ *
32
+ * this goes through rollup's resolver rather than import.meta.resolve or
33
+ * require.resolve because those apply node's conditions: subduction (and
34
+ * others) would hand us `dist/esm/node.js`, which imports node:path and blows
35
+ * up at bundle time. rollup applies the browser conditions vite configured.
36
+ */
37
+ async function resolveExternal(
38
+ this: import("rollup").PluginContext,
39
+ name: string
40
+ ) {
41
+ const resolved = await this.resolve(name, self, { skipSelf: true });
42
+ if (!resolved) {
43
+ throw new Error(
44
+ `@patchwork/vite: couldn't resolve the external "${name}" from ` +
45
+ `@inkandswitch/patchwork-bootloader. it should be one of the ` +
46
+ `bootloader's own dependencies.`
47
+ );
48
+ }
49
+ return resolved.id;
50
+ }
51
+
24
52
  function createImportMap(options?: PatchworkVitePluginOptions) {
25
53
  const importmap: ImportMap = structuredClone(
26
54
  options?.importmap ?? { imports: {}, scopes: {} }
@@ -40,12 +68,12 @@ export function importmap(options?: PatchworkVitePluginOptions): Plugin {
40
68
  this.emitFile({
41
69
  type: "chunk",
42
70
  fileName: fileName.slice(1),
43
- id,
71
+ id: await resolveExternal.call(this, id),
44
72
  preserveSignature: "strict",
45
73
  });
46
74
  }
47
75
 
48
- // Emit automerge, keyhive, and subduction wasm so the service worker can fetch them
76
+ // Emitted so the service worker can fetch them
49
77
  const automergeWasmPath = require.resolve(
50
78
  "@automerge/automerge/automerge.wasm"
51
79
  );
@@ -63,7 +91,6 @@ export function importmap(options?: PatchworkVitePluginOptions): Plugin {
63
91
  source: readFileSync(keyhiveWasmPath),
64
92
  });
65
93
 
66
- // Emit subduction wasm so the service worker can fetch it
67
94
  const subdWasmPath =
68
95
  require.resolve("@automerge/automerge-subduction/wasm");
69
96
  this.emitFile({
@@ -72,8 +99,14 @@ export function importmap(options?: PatchworkVitePluginOptions): Plugin {
72
99
  source: readFileSync(subdWasmPath),
73
100
  });
74
101
  },
75
- resolveId(id) {
76
- if (id in importmap.imports && !(id in builtins)) {
102
+ async resolveId(id) {
103
+ if (id in builtins) {
104
+ // point the site's own imports at the same copy we emit as a chunk,
105
+ // otherwise rollup bundles a second one out of the site's node_modules
106
+ // and you end up with two automerges racing to init the same wasm
107
+ return resolveExternal.call(this, id);
108
+ }
109
+ if (id in importmap.imports) {
77
110
  return { id: importmap.imports[id], external: true };
78
111
  }
79
112
  },