From 830ffb6e57dbdd3c195bbd34fc1392165a44e027 Mon Sep 17 00:00:00 2001 From: HerbCaudill Date: Sun, 29 Oct 2023 12:33:19 +0100 Subject: [PATCH 1/4] add AuthMessage type --- .../automerge-repo/src/network/messages.ts | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/packages/automerge-repo/src/network/messages.ts b/packages/automerge-repo/src/network/messages.ts index 96db4434b..eea6dfc17 100644 --- a/packages/automerge-repo/src/network/messages.ts +++ b/packages/automerge-repo/src/network/messages.ts @@ -87,26 +87,18 @@ export type RequestMessage = { documentId: DocumentId } -/** Notify the network that we have arrived so everyone knows our peer ID */ -export type ArriveMessage = { - type: "arrive" +/** (anticipating work in progress) */ +export type AuthMessage = { + type: "auth" /** The peer ID of the sender of this message */ senderId: PeerId - /** Arrive messages don't have a targetId */ - targetId: never -} - -/** Respond to an arriving peer with our peer ID */ -export type WelcomeMessage = { - type: "welcome" - - /** The peer ID of the recipient sender this message */ - senderId: PeerId - /** The peer ID of the recipient of this message */ targetId: PeerId + + /** The payload of the auth message (up to the specific auth provider) */ + payload: TPayload } /** These are message types that a {@link NetworkAdapter} surfaces to a {@link Repo}. */ From 9edcd0c467492e6b72473a9ad2c098b838a62247 Mon Sep 17 00:00:00 2001 From: HerbCaudill Date: Sun, 29 Oct 2023 12:59:36 +0100 Subject: [PATCH 2/4] remove adapter-specific types from automerge-repo --- .../src/index.ts | 28 +++++- .../src/index.ts | 95 ++++++++++++------- .../src/messages.ts | 6 +- packages/automerge-repo/src/index.ts | 2 - .../src/network/NetworkAdapter.ts | 6 +- .../automerge-repo/src/network/messages.ts | 9 +- 6 files changed, 95 insertions(+), 51 deletions(-) diff --git a/packages/automerge-repo-network-broadcastchannel/src/index.ts b/packages/automerge-repo-network-broadcastchannel/src/index.ts index 24c437f00..415b91337 100644 --- a/packages/automerge-repo-network-broadcastchannel/src/index.ts +++ b/packages/automerge-repo-network-broadcastchannel/src/index.ts @@ -41,7 +41,7 @@ export class BroadcastChannelNetworkAdapter extends NetworkAdapter { this.#broadcastChannel.addEventListener( "message", - (e: { data: Message }) => { + (e: { data: BroadcastChannelMessage }) => { const message = e.data if ("targetId" in message && message.targetId !== this.peerId) { return @@ -87,7 +87,7 @@ export class BroadcastChannelNetworkAdapter extends NetworkAdapter { this.emit("peer-candidate", { peerId }) } - send(message: RepoMessage) { + send(message: Message) { if ("data" in message) { this.#broadcastChannel.postMessage({ ...message, @@ -106,3 +106,27 @@ export class BroadcastChannelNetworkAdapter extends NetworkAdapter { throw new Error("Unimplemented: leave on BroadcastChannelNetworkAdapter") } } + +/** Notify the network that we have arrived so everyone knows our peer ID */ +type ArriveMessage = { + type: "arrive" + + /** The peer ID of the sender of this message */ + senderId: PeerId + + /** Arrive messages don't have a targetId */ + targetId: never +} + +/** Respond to an arriving peer with our peer ID */ +type WelcomeMessage = { + type: "welcome" + + /** The peer ID of the recipient sender this message */ + senderId: PeerId + + /** The peer ID of the recipient of this message */ + targetId: PeerId +} + +type BroadcastChannelMessage = ArriveMessage | WelcomeMessage | Message diff --git a/packages/automerge-repo-network-messagechannel/src/index.ts b/packages/automerge-repo-network-messagechannel/src/index.ts index 2a1a9d113..7fc2389f7 100644 --- a/packages/automerge-repo-network-messagechannel/src/index.ts +++ b/packages/automerge-repo-network-messagechannel/src/index.ts @@ -41,42 +41,45 @@ export class MessageChannelNetworkAdapter extends NetworkAdapter { log("messageport connecting") this.peerId = peerId this.messagePortRef.start() - this.messagePortRef.addListener("message", (e: { data: Message }) => { - log("message port received", e.data) - - const message = e.data - if ("targetId" in message && message.targetId !== this.peerId) { - throw new Error( - "MessagePortNetwork should never receive messages for a different peer." - ) - } - - const { senderId, type } = message - - switch (type) { - case "arrive": - this.messagePortRef.postMessage({ - senderId: this.peerId, - targetId: senderId, - type: "welcome", - }) - this.announceConnection(senderId) - break - case "welcome": - this.announceConnection(senderId) - break - default: - if (!("data" in message)) { - this.emit("message", message) - } else { - this.emit("message", { - ...message, - data: new Uint8Array(message.data), + this.messagePortRef.addListener( + "message", + (e: { data: MessageChannelMessage }) => { + log("message port received", e.data) + + const message = e.data + if ("targetId" in message && message.targetId !== this.peerId) { + throw new Error( + "MessagePortNetwork should never receive messages for a different peer." + ) + } + + const { senderId, type } = message + + switch (type) { + case "arrive": + this.messagePortRef.postMessage({ + senderId: this.peerId, + targetId: senderId, + type: "welcome", }) - } - break + this.announceConnection(senderId) + break + case "welcome": + this.announceConnection(senderId) + break + default: + if (!("data" in message)) { + this.emit("message", message) + } else { + this.emit("message", { + ...message, + data: new Uint8Array(message.data), + }) + } + break + } } - }) + ) this.messagePortRef.addListener("close", () => { this.emit("close") @@ -142,3 +145,27 @@ export interface MessageChannelNetworkAdapterConfig { */ useWeakRef?: boolean } + +/** Notify the network that we have arrived so everyone knows our peer ID */ +type ArriveMessage = { + type: "arrive" + + /** The peer ID of the sender of this message */ + senderId: PeerId + + /** Arrive messages don't have a targetId */ + targetId: never +} + +/** Respond to an arriving peer with our peer ID */ +type WelcomeMessage = { + type: "welcome" + + /** The peer ID of the recipient sender this message */ + senderId: PeerId + + /** The peer ID of the recipient of this message */ + targetId: PeerId +} + +type MessageChannelMessage = ArriveMessage | WelcomeMessage | Message diff --git a/packages/automerge-repo-network-websocket/src/messages.ts b/packages/automerge-repo-network-websocket/src/messages.ts index a7fec5114..aa1ae1765 100644 --- a/packages/automerge-repo-network-websocket/src/messages.ts +++ b/packages/automerge-repo-network-websocket/src/messages.ts @@ -1,4 +1,4 @@ -import { type RepoMessage, type PeerId } from "@automerge/automerge-repo" +import { Message, type PeerId } from "@automerge/automerge-repo" import { ProtocolVersion } from "./protocolVersion.js" /** The sender is disconnecting */ @@ -42,7 +42,7 @@ export type ErrorMessage = { // join/leave /** A message from the client to the server */ -export type FromClientMessage = JoinMessage | LeaveMessage | RepoMessage +export type FromClientMessage = JoinMessage | LeaveMessage | Message /** A message from the server to the client */ -export type FromServerMessage = PeerMessage | ErrorMessage | RepoMessage +export type FromServerMessage = PeerMessage | ErrorMessage | Message diff --git a/packages/automerge-repo/src/index.ts b/packages/automerge-repo/src/index.ts index 18da18dc4..b66f57655 100644 --- a/packages/automerge-repo/src/index.ts +++ b/packages/automerge-repo/src/index.ts @@ -66,14 +66,12 @@ export type { PeerDisconnectedPayload, } from "./network/NetworkAdapter.js" export type { - ArriveMessage, DocumentUnavailableMessage, EphemeralMessage, Message, RepoMessage, RequestMessage, SyncMessage, - WelcomeMessage, } from "./network/messages.js" export type { StorageKey } from "./storage/StorageAdapter.js" export * from "./types.js" diff --git a/packages/automerge-repo/src/network/NetworkAdapter.ts b/packages/automerge-repo/src/network/NetworkAdapter.ts index de914deef..56d0783bd 100644 --- a/packages/automerge-repo/src/network/NetworkAdapter.ts +++ b/packages/automerge-repo/src/network/NetworkAdapter.ts @@ -1,6 +1,6 @@ import { EventEmitter } from "eventemitter3" import { PeerId } from "../types.js" -import { RepoMessage } from "./messages.js" +import { Message } from "./messages.js" /** An interface representing some way to connect to other peers * @@ -22,7 +22,7 @@ export abstract class NetworkAdapter extends EventEmitter * * @argument message - the message to send */ - abstract send(message: RepoMessage): void + abstract send(message: Message): void /** Called by the {@link Repo} to disconnect from the network */ abstract disconnect(): void @@ -44,7 +44,7 @@ export interface NetworkAdapterEvents { "peer-disconnected": (payload: PeerDisconnectedPayload) => void /** Emitted when the network adapter receives a message from a peer */ - message: (payload: RepoMessage) => void + message: (payload: Message) => void } export interface OpenPayload { diff --git a/packages/automerge-repo/src/network/messages.ts b/packages/automerge-repo/src/network/messages.ts index eea6dfc17..74a528d3d 100644 --- a/packages/automerge-repo/src/network/messages.ts +++ b/packages/automerge-repo/src/network/messages.ts @@ -108,13 +108,8 @@ export type RepoMessage = | RequestMessage | DocumentUnavailableMessage -/** These are all the message types that a {@link NetworkAdapter} might see. - * - * @remarks - * It is not _required_ that a {@link NetworkAdapter} use these types: They are free to use - * whatever message type makes sense for their transport. However, this type is a useful default. - * */ -export type Message = RepoMessage | ArriveMessage | WelcomeMessage +/** These are all the message types that a {@link NetworkAdapter} might see. */ +export type Message = RepoMessage | AuthMessage /** * The contents of a message, without the sender ID or other properties added by the {@link NetworkSubsystem}) From cde81179159bd0e10ae704ef16769de32ea21d8b Mon Sep 17 00:00:00 2001 From: HerbCaudill Date: Sun, 29 Oct 2023 12:59:45 +0100 Subject: [PATCH 3/4] unused import --- .../automerge-repo-network-broadcastchannel/src/index.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/automerge-repo-network-broadcastchannel/src/index.ts b/packages/automerge-repo-network-broadcastchannel/src/index.ts index 415b91337..08ff017dc 100644 --- a/packages/automerge-repo-network-broadcastchannel/src/index.ts +++ b/packages/automerge-repo-network-broadcastchannel/src/index.ts @@ -14,12 +14,7 @@ * */ -import { - Message, - NetworkAdapter, - PeerId, - RepoMessage, -} from "@automerge/automerge-repo" +import { Message, NetworkAdapter, PeerId } from "@automerge/automerge-repo" export type BroadcastChannelNetworkAdapterOptions = { channelName: string From abe5e110272e10389de1980f16ce744041c356ab Mon Sep 17 00:00:00 2001 From: HerbCaudill Date: Sun, 29 Oct 2023 13:05:17 +0100 Subject: [PATCH 4/4] network-websocket/messages: import type {} --- packages/automerge-repo-network-websocket/src/messages.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/automerge-repo-network-websocket/src/messages.ts b/packages/automerge-repo-network-websocket/src/messages.ts index aa1ae1765..5cd31650e 100644 --- a/packages/automerge-repo-network-websocket/src/messages.ts +++ b/packages/automerge-repo-network-websocket/src/messages.ts @@ -1,5 +1,5 @@ -import { Message, type PeerId } from "@automerge/automerge-repo" -import { ProtocolVersion } from "./protocolVersion.js" +import type { Message, PeerId } from "@automerge/automerge-repo" +import type { ProtocolVersion } from "./protocolVersion.js" /** The sender is disconnecting */ export type LeaveMessage = {