@inkandswitch/patchwork 0.7.4 → 0.8.1
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/CHANGELOG.md +49 -0
- package/dist/head.js +1 -5
- package/dist/index.d.ts +5 -4
- package/dist/index.js +37 -56
- package/dist/repo.d.ts +9 -17
- package/dist/repo.js +27 -56
- package/dist/site-kit/index.d.ts +1 -1
- package/dist/site-kit/options.d.ts +24 -15
- package/dist/site-kit/sync-servers.d.ts +1 -0
- package/dist/site-kit/sync-servers.js +19 -17
- package/dist/types.d.ts +14 -3
- package/dist/vite/config-plugin.js +5 -5
- package/dist/vite/patches-plugin.d.ts +2 -0
- package/dist/vite/patches-plugin.js +149 -0
- package/dist/vite/patchwork-plugin.d.ts +2 -1
- package/dist/vite/patchwork-plugin.js +3 -0
- package/dist/vite/service-worker-plugin.js +2 -2
- package/package.json +5 -5
- package/src/index.ts +56 -48
- package/src/repo.ts +36 -72
- package/src/site-kit/index.ts +1 -0
- package/src/site-kit/options.ts +26 -11
- package/src/site-kit/sync-servers.ts +20 -17
- package/src/types.ts +15 -8
- package/src/vite/config-plugin.ts +7 -12
- package/src/vite/patches-plugin.ts +184 -0
- package/src/vite/patchwork-plugin.ts +4 -0
- package/src/vite/service-worker-plugin.ts +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,54 @@
|
|
|
1
1
|
# @inkandswitch/patchwork
|
|
2
2
|
|
|
3
|
+
## 0.8.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 5462610: Apply Patchwork's automerge-repo source patches at bundle time, so a site that installs `@inkandswitch/patchwork` gets them. They were only a pnpm patch, which exists in this repo's node_modules and nowhere else, so every consumer bundled an automerge-repo without the `mesh` subduction role or the `awaiting-reconnect` fix to `isConnecting()`.
|
|
8
|
+
|
|
9
|
+
The new `patches` plugin is part of `patchwork()` and also exported on its own. It rewrites the two files as they pass through rollup, and registers the same rewrite as an esbuild plugin for dep pre-bundling, which runs outside the plugin pipeline. It is pinned to one automerge-repo version and every anchor has to match, so bumping the dependency fails the build rather than quietly dropping the patches. A copy that already carries the edits — this repo's, via the pnpm patch — is left alone.
|
|
10
|
+
|
|
11
|
+
- 5462610: Move keyhive out of `syncServers` and into its own top-level site option: `keyhive?: boolean | { syncServer?, useIdFactory? }`. `keyhive: true` enables it against the `"subduction"` relay; the object form picks a different relay (or a custom `{url, contactCardJson, peerId}` identity) and turns individual behaviour off. `keyhive.useIdFactory: false` drops the `idFactory` ARK injects into the repo config, so document ids are generated the Repo's own way instead of derived from keyhive. It reaches both the tab repo and the automerge protocol handler worker through the `__SYNC_SERVER__` define.
|
|
12
|
+
|
|
13
|
+
`syncServers` is now just URLs — `subduction` and `classic`, no longer mutually exclusive with anything. Sites passing `syncServers: { keyhive: X }` should pass `keyhive: { syncServer: X }` instead; `syncServers.subduction` still overrides the URL a named relay implies.
|
|
14
|
+
|
|
15
|
+
- 5462610: Persist the Subduction signer so every context on an origin presents the same identity. The seed lives in the shared IndexedDB, so tabs and the automerge protocol handler worker adopt one signer instead of each minting a fresh `MemorySigner` on load. Generating it takes a Web Lock, so a cold profile opening two contexts at once still settles on one seed.
|
|
16
|
+
|
|
17
|
+
`@inkandswitch/patchwork-bootloader/signer` is a new export: `loadOrCreateSigner(storage)` returns the origin's `MemorySigner`, generating and storing a seed the first time.
|
|
18
|
+
|
|
19
|
+
- Updated dependencies [5462610]
|
|
20
|
+
- Updated dependencies [5462610]
|
|
21
|
+
- @inkandswitch/patchwork-bootloader@0.7.1
|
|
22
|
+
|
|
23
|
+
## 0.8.0
|
|
24
|
+
|
|
25
|
+
### Minor Changes
|
|
26
|
+
|
|
27
|
+
- 2e71745: Every Repo on the origin is its own Subduction node. A tab holds this origin's IndexedDB, keeps its own WebSocket to the sync server, and meets the other tabs over a BroadcastChannel carrying Subduction (`siblingAdapters()` in `@inkandswitch/patchwork-bootloader/siblings`, passed to `new Repo({ subductionAdapters })`). The automerge SharedWorker no longer sits between tabs and storage — it is one more such node, kept only to resolve `automerge:` URLs for the service worker, which can't own a Repo itself. The websocket proxy worker is gone with it.
|
|
28
|
+
|
|
29
|
+
Benchmarked against the shared-worker arrangement (`sites/bench`): boot and memory are a wash or better, cross-tab propagation matches, and two shared-worker failures go away — a `find()` racing a sibling's `create()` settled as unavailable, and edits made just before a tab closed were lost, since a storageless tab had nothing to flush to. Each tab flushing its own IndexedDB closes both.
|
|
30
|
+
|
|
31
|
+
Keyhive sites use the subduction-backed hive in both the tab and the worker, each talking to the sync server directly.
|
|
32
|
+
|
|
33
|
+
`patchwork.sw.subscribeSyncState(documentId, listener)` stays, now a filter over the tab's own Repo's `subduction-remote-heads` event, replaying the server's current heads from `handle.getSyncInfo()` on subscribe; `SyncStateDocMessage` is exported from `@inkandswitch/patchwork`. Removed from `setupServiceWorker()`'s result: `subscribeToRepoChannel`, `getRepoChannel`, `subscribeSyncState`. The `@patchwork/syncstate` BroadcastChannel and the other `SyncState*` message types are gone too; a tab's own Repo has what they carried — `repo.isSubductionConnected()` and the `subduction-connection` event for the link, `repo.connectedSubductionPeerIds()` for which peers are the server, and `patchwork.signerIdentity` for this tab's peer id. `createRepo` in `@inkandswitch/patchwork` takes no arguments.
|
|
34
|
+
|
|
35
|
+
`@inkandswitch/patchwork-bootloader` depends on `@automerge/automerge-repo-network-broadcastchannel`, which is also on the importmap.
|
|
36
|
+
|
|
37
|
+
That worker is renamed for what it now does: `@inkandswitch/patchwork-bootloader/automerge-worker` is now `@inkandswitch/patchwork-bootloader/automerge-protocol-handler-worker`, emitted as `automerge-protocol-handler-worker.js` (the `workerPath` option on `setupServiceWorker` still overrides it), and `getAutomergeWorker()` is now `getAutomergeProtocolHandlerWorker()`.
|
|
38
|
+
|
|
39
|
+
Inter-tab sync is Subduction, not classic automerge sync. `connectSiblings(repo, hive)` is replaced by `siblingAdapters()`, which returns the `subductionAdapters` entries for a Repo rather than mutating one after the fact, so it is passed to the `Repo` constructor. The frames on the siblings BroadcastChannel are Subduction transport frames authenticated by each node's own signer; the keyhive network adapter no longer wraps that channel, since keyhive material reaches siblings the same way it reaches the sync server. The one remaining classic-sync path is the opt-in classic sync server the protocol handler worker connects to on request.
|
|
40
|
+
|
|
41
|
+
Subduction's handshake has an initiator and a responder, and a BroadcastChannel is a mesh, so the siblings adapter is passed with `role: "mesh"`, added to the automerge-repo fork's `subductionAdapters` by this repo's pnpm patch: for each pair of peers on the adapter, the one whose peer id sorts lower initiates the handshake and the other accepts.
|
|
42
|
+
|
|
43
|
+
### Patch Changes
|
|
44
|
+
|
|
45
|
+
- 47bc4cf: `@automerge/automerge` goes to `3.4.1`, and `@automerge/automerge-repo-network-broadcastchannel` joins the automerge-repo family at `2.6.0-subduction.48`.
|
|
46
|
+
- Updated dependencies [47bc4cf]
|
|
47
|
+
- Updated dependencies [2e71745]
|
|
48
|
+
- @inkandswitch/patchwork-bootloader@0.7.0
|
|
49
|
+
- @inkandswitch/patchwork-filesystem@0.2.9
|
|
50
|
+
- @inkandswitch/patchwork-plugins@1.2.4
|
|
51
|
+
|
|
3
52
|
## 0.7.4
|
|
4
53
|
|
|
5
54
|
### Patch Changes
|
package/dist/head.js
CHANGED
|
@@ -6,11 +6,7 @@
|
|
|
6
6
|
"@automerge/automerge/slim",
|
|
7
7
|
"@automerge/automerge-repo",
|
|
8
8
|
"@automerge/automerge-repo/slim",
|
|
9
|
-
|
|
10
|
-
// proxy entry and donate its port to the automerge worker (Chrome can't
|
|
11
|
-
// spawn workers from inside a SharedWorker). See setup.ts/automerge-worker.ts.
|
|
12
|
-
"@automerge/automerge-repo/worker-port",
|
|
13
|
-
"@automerge/automerge-repo/subduction-websocket-worker-shared",
|
|
9
|
+
"@automerge/automerge-repo-network-broadcastchannel",
|
|
14
10
|
"@automerge/automerge-repo-network-messagechannel",
|
|
15
11
|
"@automerge/automerge-repo-network-websocket",
|
|
16
12
|
"@automerge/automerge-repo-storage-indexeddb",
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* One import for a Patchwork site.
|
|
3
3
|
*
|
|
4
|
-
* `setup(options)` constructs the Repo, wires up the
|
|
5
|
-
* loads plugins via the
|
|
4
|
+
* `setup(options)` constructs the Repo, wires up the
|
|
5
|
+
* automerge-protocol-handler-worker port, loads plugins via the
|
|
6
|
+
* ModuleWatcher, resolves the user's account document,
|
|
6
7
|
* installs the router, and resolves with the site's runtime API — `repo`,
|
|
7
8
|
* `create`, `open`, `find`, `packages`, `plugins`, `sw` — which is what a
|
|
8
9
|
* site assigns to `window.patchwork`.
|
|
@@ -15,7 +16,7 @@
|
|
|
15
16
|
* Pulls in DOM- and plugin-layer dependencies, so it is for a browser site's
|
|
16
17
|
* `main.ts` only. Non-UI consumers should import
|
|
17
18
|
* `@inkandswitch/patchwork-bootloader` directly, which does SW registration
|
|
18
|
-
* and the automerge-worker handoff and nothing else.
|
|
19
|
+
* and the automerge-protocol-handler-worker handoff and nothing else.
|
|
19
20
|
*/
|
|
20
21
|
import { Repo } from "@automerge/vanillajs/slim";
|
|
21
22
|
import type { AutomergeRepoKeyhiveBase as AutomergeRepoKeyhive } from "@automerge/automerge-repo-keyhive";
|
|
@@ -34,4 +35,4 @@ export default setup;
|
|
|
34
35
|
export { createRepo, initWasm } from "./repo.js";
|
|
35
36
|
export { createRouter } from "./router.js";
|
|
36
37
|
export { showLoadingAnimation, hideLoadingAnimation, showErrorScreen, } from "./loading.js";
|
|
37
|
-
export type { OpenOptions, Patchwork, PatchworkOptions, ServiceWorkerApi, SignerIdentity, } from "./types.js";
|
|
38
|
+
export type { OpenOptions, Patchwork, PatchworkOptions, ServiceWorkerApi, SignerIdentity, SyncStateDocMessage, } from "./types.js";
|
package/dist/index.js
CHANGED
|
@@ -1,23 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* One import for a Patchwork site.
|
|
3
|
-
*
|
|
4
|
-
* `setup(options)` constructs the Repo, wires up the automerge-worker port,
|
|
5
|
-
* loads plugins via the ModuleWatcher, resolves the user's account document,
|
|
6
|
-
* installs the router, and resolves with the site's runtime API — `repo`,
|
|
7
|
-
* `create`, `open`, `find`, `packages`, `plugins`, `sw` — which is what a
|
|
8
|
-
* site assigns to `window.patchwork`.
|
|
9
|
-
*
|
|
10
|
-
* Setup owns page-wide state — the `window.repo`/`window.Automerge`/
|
|
11
|
-
* `window.AutomergeRepo`/`window.hive` globals, custom-element registration,
|
|
12
|
-
* document listeners — so it may run only once per page; a second call
|
|
13
|
-
* throws.
|
|
14
|
-
*
|
|
15
|
-
* Pulls in DOM- and plugin-layer dependencies, so it is for a browser site's
|
|
16
|
-
* `main.ts` only. Non-UI consumers should import
|
|
17
|
-
* `@inkandswitch/patchwork-bootloader` directly, which does SW registration
|
|
18
|
-
* and the automerge-worker handoff and nothing else.
|
|
19
|
-
*/
|
|
20
|
-
import { MessageChannelNetworkAdapter, } from "@automerge/vanillajs/slim";
|
|
21
1
|
import * as Automerge from "@automerge/automerge/slim";
|
|
22
2
|
import * as AutomergeRepo from "@automerge/automerge-repo/slim";
|
|
23
3
|
import { ModuleWatcher } from "@inkandswitch/patchwork-filesystem";
|
|
@@ -28,7 +8,7 @@ import { createDocOfDatatype2, getRegistry, registerPlugins, resolveAccountHandl
|
|
|
28
8
|
import * as plugins from "@inkandswitch/patchwork-plugins";
|
|
29
9
|
import setupServiceWorker, { lifecycleLog, } from "@inkandswitch/patchwork-bootloader";
|
|
30
10
|
import debug from "debug";
|
|
31
|
-
import { createRepo,
|
|
11
|
+
import { createRepo, initWasm } from "./repo.js";
|
|
32
12
|
import { createRouter } from "./router.js";
|
|
33
13
|
import { createDefaultAccount } from "./createAccount.js";
|
|
34
14
|
const log = debug("patchwork:setup");
|
|
@@ -68,43 +48,13 @@ async function doSetup(options) {
|
|
|
68
48
|
let hive;
|
|
69
49
|
let repo;
|
|
70
50
|
let signerIdentity;
|
|
71
|
-
// Called with a fresh port when the automerge worker dies and is recreated.
|
|
72
|
-
// Assigned once the repo exists.
|
|
73
|
-
let onWorkerPortRenewed;
|
|
74
51
|
if (options.repo) {
|
|
75
52
|
log("using provided Repo");
|
|
76
53
|
repo = options.repo;
|
|
77
54
|
hive = options.hive;
|
|
78
55
|
}
|
|
79
56
|
else {
|
|
80
|
-
|
|
81
|
-
if (onWorkerPortRenewed)
|
|
82
|
-
onWorkerPortRenewed(port);
|
|
83
|
-
else {
|
|
84
|
-
console.warn("automerge worker port renewed before the repo existed; dropping it");
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
let workerAdapter = new MessageChannelNetworkAdapter(workerPort);
|
|
88
|
-
({ repo, hive, signerIdentity } = await createRepo(workerAdapter));
|
|
89
|
-
// The worker was recreated with cold state: wire the repo onto the fresh
|
|
90
|
-
// port and drop the adapter stranded on the dead one.
|
|
91
|
-
const bootHive = hive;
|
|
92
|
-
onWorkerPortRenewed = (port) => {
|
|
93
|
-
const fresh = new MessageChannelNetworkAdapter(port);
|
|
94
|
-
// Mirror the boot wiring: a keyhive repo talks to the worker through a
|
|
95
|
-
// keyhive adapter wrapped around the message channel.
|
|
96
|
-
const registered = bootHive
|
|
97
|
-
? bootHive.createKeyhiveNetworkAdapter(fresh, {
|
|
98
|
-
onlyShareWithSyncServer: false,
|
|
99
|
-
periodicallyRequestSync: false,
|
|
100
|
-
syncRequestInterval: 2000,
|
|
101
|
-
})
|
|
102
|
-
: fresh;
|
|
103
|
-
repo.networkSubsystem.addNetworkAdapter(registered);
|
|
104
|
-
removeAdapterFor(repo, workerAdapter, registered);
|
|
105
|
-
workerAdapter = fresh;
|
|
106
|
-
lifecycleLog("repo re-wired to the recreated automerge worker");
|
|
107
|
-
};
|
|
57
|
+
({ repo, hive, signerIdentity } = await createRepo());
|
|
108
58
|
}
|
|
109
59
|
// Dev-console / tool-runtime globals (e2e and loaded tools read these). The
|
|
110
60
|
// `window.patchwork` handle is deliberately not set here — the caller does
|
|
@@ -115,8 +65,6 @@ async function doSetup(options) {
|
|
|
115
65
|
AutomergeRepo;
|
|
116
66
|
if (hive)
|
|
117
67
|
window.hive = hive;
|
|
118
|
-
await repo.networkSubsystem.whenReady();
|
|
119
|
-
log("networkSubsystem ready");
|
|
120
68
|
hive?.networkAdapter?.syncKeyhive?.();
|
|
121
69
|
registerRepoProviderElement(repo);
|
|
122
70
|
const rootElementId = options.rootElementId ?? "root";
|
|
@@ -172,8 +120,7 @@ async function doSetup(options) {
|
|
|
172
120
|
plugins,
|
|
173
121
|
sw: {
|
|
174
122
|
connectClassicSync: sw.connectClassicSync,
|
|
175
|
-
|
|
176
|
-
subscribeSyncState: sw.subscribeSyncState,
|
|
123
|
+
subscribeSyncState: (documentId, listener) => subscribeSyncState(repo, documentId, listener),
|
|
177
124
|
},
|
|
178
125
|
async create(type, init) {
|
|
179
126
|
const datatype = await getRegistry("patchwork:datatype").load(type);
|
|
@@ -283,6 +230,40 @@ function installLifecycleLogging() {
|
|
|
283
230
|
window.addEventListener("offline", () => lifecycleLog("offline"), opts);
|
|
284
231
|
lifecycleLog("logging installed (visibilityState=%s, hasFocus=%s)", document.visibilityState, document.hasFocus());
|
|
285
232
|
}
|
|
233
|
+
// The tab's own Repo hears the server's heads directly, so this is a filter
|
|
234
|
+
// over its `subduction-remote-heads` event. The current value is replayed
|
|
235
|
+
// from the handle's sync info, when the server has reported any.
|
|
236
|
+
function subscribeSyncState(repo, documentId, listener) {
|
|
237
|
+
const onHeads = (payload) => {
|
|
238
|
+
if (payload.documentId !== documentId)
|
|
239
|
+
return;
|
|
240
|
+
listener({
|
|
241
|
+
type: "sync-state",
|
|
242
|
+
documentId,
|
|
243
|
+
storageId: payload.storageId,
|
|
244
|
+
heads: [...payload.heads],
|
|
245
|
+
timestamp: payload.timestamp,
|
|
246
|
+
});
|
|
247
|
+
};
|
|
248
|
+
repo.on("subduction-remote-heads", onHeads);
|
|
249
|
+
void (async () => {
|
|
250
|
+
const handle = repo.handles[documentId];
|
|
251
|
+
if (!handle)
|
|
252
|
+
return;
|
|
253
|
+
for (const storageId of await repo.connectedSubductionPeerIds()) {
|
|
254
|
+
const info = handle.getSyncInfo(storageId);
|
|
255
|
+
if (info) {
|
|
256
|
+
onHeads({
|
|
257
|
+
documentId,
|
|
258
|
+
storageId,
|
|
259
|
+
heads: info.lastHeads,
|
|
260
|
+
timestamp: info.lastSyncTimestamp,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
})();
|
|
265
|
+
return () => repo.off("subduction-remote-heads", onHeads);
|
|
266
|
+
}
|
|
286
267
|
// ── Named exports ────────────────────────────────────────────────────────
|
|
287
268
|
export { createRepo, initWasm } from "./repo.js";
|
|
288
269
|
export { createRouter } from "./router.js";
|
package/dist/repo.d.ts
CHANGED
|
@@ -1,23 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { type
|
|
3
|
-
import setupServiceWorker from "@inkandswitch/patchwork-bootloader";
|
|
1
|
+
import { Repo } from "@automerge/vanillajs/slim";
|
|
2
|
+
import { type AutomergeRepoKeyhive } from "@automerge/automerge-repo-keyhive";
|
|
4
3
|
import type { SignerIdentity } from "./types.js";
|
|
5
4
|
export declare function initWasm(): Promise<void>;
|
|
6
|
-
export
|
|
5
|
+
export type TabRepo = {
|
|
7
6
|
repo: Repo;
|
|
8
|
-
hive?:
|
|
7
|
+
hive?: AutomergeRepoKeyhive;
|
|
9
8
|
signerIdentity?: SignerIdentity;
|
|
10
|
-
}
|
|
9
|
+
};
|
|
11
10
|
/**
|
|
12
|
-
*
|
|
13
|
-
* every
|
|
14
|
-
*
|
|
15
|
-
* subscribeToRepoChannel is deliberately not awaited: it resolves only after
|
|
16
|
-
* the boot channel's port-ready handshake, which can take its full 30s timeout
|
|
17
|
-
* against a stranded worker connection. Boot blocks on the first *delivered*
|
|
18
|
-
* port instead — if the boot channel stalls, worker recovery hands the listener
|
|
19
|
-
* a good port long before that timeout.
|
|
11
|
+
* The tab's own node: this origin's IndexedDB, a socket to the sync server,
|
|
12
|
+
* and the siblings channel to every other Repo on the origin. Nothing is
|
|
13
|
+
* shared with other tabs except the database underneath.
|
|
20
14
|
*/
|
|
21
|
-
export declare function
|
|
22
|
-
/** Drop the adapter sitting on the dead worker port, leaving `keep` in place. */
|
|
23
|
-
export declare function removeAdapterFor(repo: Repo, stale: MessageChannelNetworkAdapter, keep: unknown): void;
|
|
15
|
+
export declare function createRepo(): Promise<TabRepo>;
|
package/dist/repo.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { initializeWasm, Repo
|
|
1
|
+
import { initializeWasm, Repo } from "@automerge/vanillajs/slim";
|
|
2
2
|
import { IndexedDBWorkerStorageAdapter } from "@automerge/automerge-repo-storage-indexeddb/IndexedDBWorkerStorageAdapter";
|
|
3
|
-
import {
|
|
3
|
+
import { siblingAdapters } from "@inkandswitch/patchwork-bootloader/siblings";
|
|
4
|
+
import { loadOrCreateSigner } from "@inkandswitch/patchwork-bootloader/signer";
|
|
5
|
+
import { initKeyhiveWasm, initializeAutomergeRepoKeyhive, } from "@automerge/automerge-repo-keyhive";
|
|
4
6
|
// eslint-disable-next-line
|
|
5
7
|
// @ts-ignore — initSync is a wasm-bindgen runtime helper not in the .d.ts
|
|
6
8
|
import { initSync as initSubductionSync } from "@automerge/automerge-subduction/slim";
|
|
7
|
-
import { MemorySigner } from "@automerge/automerge-subduction/slim";
|
|
8
9
|
import { keyhiveStorageName, storagePrefix, } from "@inkandswitch/patchwork-bootloader/storage";
|
|
9
10
|
import debug from "debug";
|
|
10
11
|
const log = debug("patchwork:setup:repo");
|
|
@@ -28,41 +29,49 @@ export function initWasm() {
|
|
|
28
29
|
}
|
|
29
30
|
return wasmReady;
|
|
30
31
|
}
|
|
31
|
-
|
|
32
|
+
/**
|
|
33
|
+
* The tab's own node: this origin's IndexedDB, a socket to the sync server,
|
|
34
|
+
* and the siblings channel to every other Repo on the origin. Nothing is
|
|
35
|
+
* shared with other tabs except the database underneath.
|
|
36
|
+
*/
|
|
37
|
+
export async function createRepo() {
|
|
32
38
|
if (syncServer.keyhive) {
|
|
33
39
|
log("setting up keyhive");
|
|
34
40
|
initKeyhiveWasm();
|
|
35
|
-
const { hive, repo } = await
|
|
36
|
-
|
|
41
|
+
const { hive, repo } = await initializeAutomergeRepoKeyhive({
|
|
42
|
+
// ARK injects an `idFactory` deriving document ids from keyhive. A site
|
|
43
|
+
// can opt out of it with `keyhive: { useIdFactory: false }`.
|
|
44
|
+
createRepo: ({ idFactory, ...repoConfig }) => new Repo(syncServer.useIdFactory === false
|
|
45
|
+
? repoConfig
|
|
46
|
+
: { ...repoConfig, idFactory }),
|
|
37
47
|
storage: new IndexedDBWorkerStorageAdapter(keyhiveStorageName),
|
|
38
48
|
peerIdSuffix: storagePrefix + Math.random().toString(36).slice(2),
|
|
39
|
-
networkAdapter: workerAdapter,
|
|
40
49
|
automaticArchiveIngestion: true,
|
|
41
50
|
cachingMode: "periodic",
|
|
42
|
-
onlyShareWithSyncServer: false,
|
|
43
51
|
// ARK selects the relay via `syncServer`, defaulting to "subduction".
|
|
44
52
|
syncServer: syncServer.keyhive,
|
|
45
53
|
repo: {
|
|
46
54
|
storage: new IndexedDBWorkerStorageAdapter(),
|
|
55
|
+
subductionWebsocketEndpoints: [syncServer.url],
|
|
56
|
+
subductionAdapters: siblingAdapters(),
|
|
47
57
|
enableRemoteHeadsGossiping: true,
|
|
48
58
|
},
|
|
49
59
|
});
|
|
50
60
|
log("keyhive setup complete");
|
|
51
61
|
return { repo, hive };
|
|
52
62
|
}
|
|
53
|
-
//
|
|
54
|
-
//
|
|
55
|
-
//
|
|
56
|
-
const
|
|
63
|
+
// The signer is explicit rather than the Repo's internal default so that
|
|
64
|
+
// every tab on this origin signs as the same peer, and so the identity the
|
|
65
|
+
// tab presents to the server can be shown on window.patchwork.
|
|
66
|
+
const storage = new IndexedDBWorkerStorageAdapter();
|
|
67
|
+
const signer = await loadOrCreateSigner(storage);
|
|
57
68
|
const repo = new Repo({
|
|
58
|
-
network: [workerAdapter],
|
|
59
|
-
storage: new IndexedDBWorkerStorageAdapter(),
|
|
60
69
|
signer,
|
|
61
|
-
|
|
62
|
-
return peerId.includes("automerge-worker");
|
|
63
|
-
},
|
|
64
|
-
enableRemoteHeadsGossiping: true,
|
|
70
|
+
storage,
|
|
65
71
|
peerId: `${storagePrefix}-tab-${crypto.randomUUID()}`,
|
|
72
|
+
subductionWebsocketEndpoints: [syncServer.url],
|
|
73
|
+
subductionAdapters: siblingAdapters(),
|
|
74
|
+
enableRemoteHeadsGossiping: true,
|
|
66
75
|
});
|
|
67
76
|
const signerIdentity = {
|
|
68
77
|
peerId: signer.peerId().toString(),
|
|
@@ -71,41 +80,3 @@ export async function createRepo(workerAdapter) {
|
|
|
71
80
|
log("repo created, tab subduction identity:", signerIdentity);
|
|
72
81
|
return { repo, signerIdentity };
|
|
73
82
|
}
|
|
74
|
-
/**
|
|
75
|
-
* Resolve with the first repo port the worker delivers, calling `onRenewed` for
|
|
76
|
-
* every later one.
|
|
77
|
-
*
|
|
78
|
-
* subscribeToRepoChannel is deliberately not awaited: it resolves only after
|
|
79
|
-
* the boot channel's port-ready handshake, which can take its full 30s timeout
|
|
80
|
-
* against a stranded worker connection. Boot blocks on the first *delivered*
|
|
81
|
-
* port instead — if the boot channel stalls, worker recovery hands the listener
|
|
82
|
-
* a good port long before that timeout.
|
|
83
|
-
*/
|
|
84
|
-
export function firstRepoPort(sw, onRenewed) {
|
|
85
|
-
return new Promise((resolve) => {
|
|
86
|
-
let seen = false;
|
|
87
|
-
void sw.subscribeToRepoChannel((port) => {
|
|
88
|
-
if (seen)
|
|
89
|
-
return onRenewed(port);
|
|
90
|
-
seen = true;
|
|
91
|
-
resolve(port);
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
/** Drop the adapter sitting on the dead worker port, leaving `keep` in place. */
|
|
96
|
-
export function removeAdapterFor(repo, stale, keep) {
|
|
97
|
-
for (const adapter of [...repo.networkSubsystem.adapters]) {
|
|
98
|
-
if (adapter === keep)
|
|
99
|
-
continue;
|
|
100
|
-
// The keyhive wrapper keeps the wrapped adapter on `.networkAdapter`.
|
|
101
|
-
const base = adapter.networkAdapter ?? adapter;
|
|
102
|
-
if (base !== stale)
|
|
103
|
-
continue;
|
|
104
|
-
try {
|
|
105
|
-
repo.networkSubsystem.removeNetworkAdapter(adapter);
|
|
106
|
-
}
|
|
107
|
-
catch (err) {
|
|
108
|
-
console.error("failed to remove stale worker network adapter", err);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
package/dist/site-kit/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* wires these into vite's plugin hooks (dev middleware, emitFile, virtual
|
|
8
8
|
* modules, config()).
|
|
9
9
|
*/
|
|
10
|
-
export type { PatchworkSiteOptions, PatchworkIconsOptions, PatchworkHtmlOptions, PatchworkNetlifyOptions, PatchworkKeyhiveSyncServer, PatchworkSyncServersOptions, } from "./options.js";
|
|
10
|
+
export type { PatchworkSiteOptions, PatchworkIconsOptions, PatchworkHtmlOptions, PatchworkNetlifyOptions, PatchworkKeyhiveSyncServer, PatchworkKeyhiveOptions, PatchworkSyncServersOptions, } from "./options.js";
|
|
11
11
|
export { resolveSyncServers, PRELOAD_WASM_ASSETS } from "./sync-servers.js";
|
|
12
12
|
export { getIcons, ICON_SPECS, type IconSpec } from "./icons.js";
|
|
13
13
|
export { buildHtml, escapeHtml } from "./html.js";
|
|
@@ -31,20 +31,28 @@ export interface PatchworkNetlifyOptions {
|
|
|
31
31
|
/** Cache-Control: immutable on /assets/*. Default true. */
|
|
32
32
|
immutableAssets?: boolean;
|
|
33
33
|
}
|
|
34
|
-
type PatchworkPrimarySyncServerOptions = {
|
|
35
|
-
subduction?: string;
|
|
36
|
-
keyhive?: never;
|
|
37
|
-
} | {
|
|
38
|
-
subduction?: never;
|
|
39
|
-
keyhive: PatchworkKeyhiveSyncServer;
|
|
40
|
-
};
|
|
41
34
|
export type PatchworkKeyhiveSyncServer = "keyhive" | "subduction" | ({
|
|
42
35
|
url: string;
|
|
43
36
|
} & SyncServerIdentity);
|
|
37
|
+
export interface PatchworkKeyhiveOptions {
|
|
38
|
+
/**
|
|
39
|
+
* Which relay ARK registers and grants access to. Default `"subduction"`.
|
|
40
|
+
* A custom identity also carries the WebSocket URL to reach it on.
|
|
41
|
+
*/
|
|
42
|
+
syncServer?: PatchworkKeyhiveSyncServer;
|
|
43
|
+
/**
|
|
44
|
+
* Use the `idFactory` ARK injects into the repo config, which derives
|
|
45
|
+
* document ids from keyhive. Default true. `false` drops it and lets the
|
|
46
|
+
* Repo generate ids its own way.
|
|
47
|
+
*/
|
|
48
|
+
useIdFactory?: boolean;
|
|
49
|
+
}
|
|
44
50
|
export type PatchworkSyncServersOptions = {
|
|
45
51
|
/** wss:// URL for the legacy automerge-repo sync-server channel (connected on demand via connectClassicSync). Default: wss://sync3.automerge.org. Pass false to skip its preconnect hint. */
|
|
46
52
|
classic?: string | false;
|
|
47
|
-
|
|
53
|
+
/** wss:// URL for the subduction channel. Default: wss://subduction.sync.inkandswitch.com. Overrides the URL a named `keyhive.syncServer` would otherwise imply. */
|
|
54
|
+
subduction?: string;
|
|
55
|
+
};
|
|
48
56
|
export declare const DEFAULT_TITLE = "Patchwork";
|
|
49
57
|
export interface PatchworkSiteOptions {
|
|
50
58
|
/**
|
|
@@ -76,12 +84,14 @@ export interface PatchworkSiteOptions {
|
|
|
76
84
|
/** manifest background_color */
|
|
77
85
|
backgroundColor?: string;
|
|
78
86
|
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
87
|
+
* Enables keyhive for this build. `true` takes every default; an object
|
|
88
|
+
* picks the relay and turns individual behaviour off. Omitted or `false`
|
|
89
|
+
* builds a plain subduction repo.
|
|
90
|
+
*/
|
|
91
|
+
keyhive?: boolean | PatchworkKeyhiveOptions;
|
|
92
|
+
/**
|
|
93
|
+
* The sync-server URLs for this build. Also emitted as connection hints;
|
|
94
|
+
* pass `false` to keep the URLs but skip the hints.
|
|
85
95
|
*/
|
|
86
96
|
syncServers?: false | PatchworkSyncServersOptions;
|
|
87
97
|
icons?: false | PatchworkIconsOptions;
|
|
@@ -89,4 +99,3 @@ export interface PatchworkSiteOptions {
|
|
|
89
99
|
manifest?: false | Record<string, unknown>;
|
|
90
100
|
netlify?: false | PatchworkNetlifyOptions;
|
|
91
101
|
}
|
|
92
|
-
export {};
|
|
@@ -8,6 +8,7 @@ export declare const DEFAULT_SYNC_SERVERS: {
|
|
|
8
8
|
export declare function resolvePrimarySyncServer(options: PatchworkSiteOptions): {
|
|
9
9
|
url: string;
|
|
10
10
|
keyhive?: SyncServerSelection;
|
|
11
|
+
useIdFactory?: boolean;
|
|
11
12
|
};
|
|
12
13
|
/**
|
|
13
14
|
* Resolves which sync-server origins are actually live for this build: the
|
|
@@ -1,30 +1,32 @@
|
|
|
1
1
|
// Mirrors core/bootloader/src/sync-config.ts's DEFAULT_CLASSIC_SYNC_SERVER
|
|
2
|
-
// and automerge-worker.ts's SUBDUCTION_SYNC_URL selection —
|
|
3
|
-
// plain constants (rather than importing those runtime modules)
|
|
4
|
-
// only needs the hostnames, not the browser-only logic that reads
|
|
2
|
+
// and automerge-protocol-handler-worker.ts's SUBDUCTION_SYNC_URL selection —
|
|
3
|
+
// kept here as plain constants (rather than importing those runtime modules)
|
|
4
|
+
// since this only needs the hostnames, not the browser-only logic that reads
|
|
5
|
+
// them.
|
|
5
6
|
export const DEFAULT_SYNC_SERVERS = {
|
|
6
7
|
classic: "wss://sync3.automerge.org",
|
|
7
8
|
subduction: "wss://subduction.sync.inkandswitch.com",
|
|
8
9
|
keyhive: "wss://keyhive.sync.automerge.org",
|
|
9
10
|
};
|
|
10
11
|
export function resolvePrimarySyncServer(options) {
|
|
11
|
-
const
|
|
12
|
-
if (
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const { url, ...identity } = servers.keyhive;
|
|
12
|
+
const subduction = (options.syncServers || undefined)?.subduction;
|
|
13
|
+
if (!options.keyhive) {
|
|
14
|
+
return { url: subduction ?? DEFAULT_SYNC_SERVERS.subduction };
|
|
15
|
+
}
|
|
16
|
+
const keyhive = options.keyhive === true ? {} : options.keyhive;
|
|
17
|
+
const useIdFactory = keyhive.useIdFactory ?? true;
|
|
18
|
+
const syncServer = keyhive.syncServer ?? "subduction";
|
|
19
|
+
if (typeof syncServer === "string") {
|
|
20
20
|
return {
|
|
21
|
-
keyhive:
|
|
22
|
-
url,
|
|
21
|
+
keyhive: syncServer,
|
|
22
|
+
url: subduction ?? DEFAULT_SYNC_SERVERS[syncServer],
|
|
23
|
+
useIdFactory,
|
|
23
24
|
};
|
|
24
25
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
};
|
|
26
|
+
// A custom relay carries the only URL it can be reached on, so it wins over
|
|
27
|
+
// `syncServers.subduction`.
|
|
28
|
+
const { url, ...identity } = syncServer;
|
|
29
|
+
return { keyhive: identity, url, useIdFactory };
|
|
28
30
|
}
|
|
29
31
|
function wsToHttpOrigin(wsUrl) {
|
|
30
32
|
return wsUrl.replace(/^ws/, "http");
|
package/dist/types.d.ts
CHANGED
|
@@ -2,16 +2,26 @@ import type { AutomergeUrl, DocHandle, Repo } from "@automerge/vanillajs/slim";
|
|
|
2
2
|
import type { AutomergeRepoKeyhiveBase as AutomergeRepoKeyhive } from "@automerge/automerge-repo-keyhive";
|
|
3
3
|
import type { ModuleWatcher, HasPatchworkMetadata } from "@inkandswitch/patchwork-filesystem";
|
|
4
4
|
import type { AccountCreator, AccountDoc } from "@inkandswitch/patchwork-plugins";
|
|
5
|
-
import type { ServiceWorkerRepoChannelListener, SyncStateDocMessage } from "@inkandswitch/patchwork-bootloader/types";
|
|
6
5
|
import type * as pluginsNS from "@inkandswitch/patchwork-plugins";
|
|
7
6
|
export type PluginsApi = typeof pluginsNS;
|
|
8
7
|
export type SignerIdentity = {
|
|
9
8
|
peerId: string;
|
|
10
9
|
verifyingKey: string;
|
|
11
10
|
};
|
|
11
|
+
/** The sync server's last-known heads for one document, as it reports them. */
|
|
12
|
+
export interface SyncStateDocMessage {
|
|
13
|
+
type: "sync-state";
|
|
14
|
+
documentId: string;
|
|
15
|
+
storageId: string;
|
|
16
|
+
heads: string[];
|
|
17
|
+
timestamp: number;
|
|
18
|
+
}
|
|
12
19
|
export interface ServiceWorkerApi {
|
|
13
20
|
connectClassicSync: (server?: string) => Promise<void>;
|
|
14
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Watch one document's heads at the sync server. Calls `listener` with
|
|
23
|
+
* what the server holds now, then on every update. Returns unsubscribe.
|
|
24
|
+
*/
|
|
15
25
|
subscribeSyncState: (documentId: string, listener: (update: SyncStateDocMessage) => void) => () => void;
|
|
16
26
|
}
|
|
17
27
|
export interface OpenOptions {
|
|
@@ -70,7 +80,8 @@ export interface PatchworkOptions {
|
|
|
70
80
|
/**
|
|
71
81
|
* Bring your own Repo. When provided, setup skips wasm initialization and
|
|
72
82
|
* repo creation entirely — you are responsible for having initialized
|
|
73
|
-
* automerge/subduction and wired the automerge-worker
|
|
83
|
+
* automerge/subduction and wired the automerge-protocol-handler-worker
|
|
84
|
+
* port yourself.
|
|
74
85
|
*/
|
|
75
86
|
repo?: Repo;
|
|
76
87
|
/** The keyhive instance accompanying a provided {@link PatchworkOptions.repo}. */
|
|
@@ -69,11 +69,11 @@ export function config(options = {}) {
|
|
|
69
69
|
},
|
|
70
70
|
};
|
|
71
71
|
},
|
|
72
|
-
// The shared automerge-worker's chunk imports bypass the
|
|
73
|
-
// worker, so offline boot needs the browser's HTTP cache to
|
|
74
|
-
// without revalidating. Content hashes make that safe; a new
|
|
75
|
-
// new URLs. Production gets this from the generated _headers
|
|
76
|
-
// preview doesn't read that, so mirror it here.
|
|
72
|
+
// The shared automerge-protocol-handler-worker's chunk imports bypass the
|
|
73
|
+
// page's service worker, so offline boot needs the browser's HTTP cache to
|
|
74
|
+
// serve them without revalidating. Content hashes make that safe; a new
|
|
75
|
+
// build gets new URLs. Production gets this from the generated _headers
|
|
76
|
+
// file — vite preview doesn't read that, so mirror it here.
|
|
77
77
|
configurePreviewServer(server) {
|
|
78
78
|
if (options.netlify === false ||
|
|
79
79
|
options.netlify?.immutableAssets === false) {
|