Repository navigation
Move Patchwork to ARK 0.8.0-alpha.1 #468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --- | ||
| "@inkandswitch/patchwork": minor | ||
| "@inkandswitch/patchwork-bootloader": minor | ||
| "@inkandswitch/patchwork-elements": patch | ||
| --- | ||
|
|
||
| Move to `@automerge/automerge-repo-keyhive` 0.8.0-alpha.1 and `@keyhive/keyhive` 0.6.0. The other exact pins move with them so the consumer's tree keeps a single copy of automerge-repo: `@automerge/automerge-repo` and its adapters, `@automerge/react` and `@automerge/vanillajs` to 2.6.0-subduction.49, and `@automerge/automerge-subduction` to 0.20.1. The shipped automerge-repo patch makes the same changes as before, rebased onto 2.6.0-subduction.49. A site applying the patch from `@inkandswitch/patchwork/dist/patches` should take the new one. | ||
|
|
||
| ARK no longer ships named sync servers, so a keyhive build finds its server's identity at startup. The tab and the automerge protocol handler worker fetch the server's contact card from `/keyhive/contact-card` on the `syncServers.subduction` host, and keep the last card fetched so a start without a network can use it. The server has to serve that endpoint. `keyhive.syncServer` and the `PatchworkKeyhiveSyncServer` type are gone; point `syncServers.subduction` at the server instead. `keyhive: true` and `keyhive: { useIdFactory }` are unchanged. | ||
|
|
||
| New in `@inkandswitch/patchwork-bootloader`: `loadSyncServerIdentity(url, storage)` from `@inkandswitch/patchwork-bootloader/keyhive-sync-server`, for a site that builds its own keyhive Repo. | ||
|
|
||
| The new keyhive can't read state written by 0.3, so keyhive state moves to a new `<prefix>-keyhive-0.8` database and the old one is left untouched. Existing installs start with fresh keyhive state on upgrade. | ||
|
|
||
| The keyhive access gates in `patchwork-elements` use ARK's `accessForDoc`, which replaces `bestAccessForDoc`. ARK no longer has public access, so a document opens only for its members. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import type { | ||
| PeerId, | ||
| StorageAdapterInterface, | ||
| } from "@automerge/automerge-repo/slim"; | ||
| import type { SyncServerIdentity } from "@automerge/automerge-repo-keyhive"; | ||
|
|
||
| const CONTACT_CARD_KEY = ["patchwork", "sync-server-contact-card"]; | ||
|
|
||
| /** | ||
| * The keyhive identity of the sync server at `url` (a ws:// or wss:// URL), | ||
| * which serves its contact card at `/keyhive/contact-card` on the same host. | ||
| * | ||
| * The card is fetched on every start so a server that has been redeployed | ||
| * with a new card is picked up. The last card fetched for each URL is kept | ||
| * in `storage` so a start without a network still has one. Fails only when | ||
| * no card can be fetched and none was saved for `url`. | ||
| * | ||
| * The card is trusted on first use. Nothing here checks it against a known | ||
| * identity so it is only as trustworthy as the connection it came over. | ||
| * | ||
| * Keyhive's wasm must be initialized before this is called. | ||
| */ | ||
| export async function loadSyncServerIdentity( | ||
| url: string, | ||
| storage: StorageAdapterInterface | ||
| ): Promise<SyncServerIdentity> { | ||
| const { ContactCard } = await import("@automerge/automerge-repo-keyhive"); | ||
| const key = [...CONTACT_CARD_KEY, url]; | ||
|
|
||
| // The server's keyhive identity and its subduction peer id are the same | ||
| // key. ARK identifies a peer by the base64 of that key. | ||
| const identity = (contactCard: Uint8Array): SyncServerIdentity => { | ||
| const id = ContactCard.fromBytes(contactCard).individualId.toBytes(); | ||
| const peerId = btoa(String.fromCharCode(...id)) as PeerId; | ||
| return { contactCard, peerId }; | ||
| }; | ||
|
|
||
| try { | ||
| const fetched = identity(await fetchContactCard(url)); | ||
| await storage.save(key, fetched.contactCard); | ||
| return fetched; | ||
| } catch (error) { | ||
| const saved = await storage.load(key); | ||
| if (!saved) { | ||
| throw new Error(`no keyhive contact card for ${url}`, { cause: error }); | ||
| } | ||
| console.warn( | ||
| `using the saved keyhive contact card for ${url}. Fetching it failed`, | ||
| error | ||
| ); | ||
| return identity(saved); | ||
| } | ||
| } | ||
|
|
||
| async function fetchContactCard(url: string): Promise<Uint8Array> { | ||
| const cardUrl = new URL("/keyhive/contact-card", url.replace(/^ws/, "http")); | ||
| const response = await fetch(cardUrl); | ||
| if (!response.ok) { | ||
| throw new Error(`GET ${cardUrl} responded with ${response.status}`); | ||
| } | ||
| return new Uint8Array(await response.arrayBuffer()); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cute