@automerge/automerge-repo 1.0.6 → 1.0.8
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/.eslintrc +1 -1
- package/dist/DocHandle.d.ts +8 -8
- package/dist/DocHandle.d.ts.map +1 -1
- package/dist/DocHandle.js +4 -8
- package/dist/DocUrl.d.ts.map +1 -1
- package/dist/Repo.d.ts +6 -3
- package/dist/Repo.d.ts.map +1 -1
- package/dist/Repo.js +20 -19
- package/dist/helpers/cbor.d.ts +2 -2
- package/dist/helpers/cbor.d.ts.map +1 -1
- package/dist/helpers/cbor.js +1 -1
- package/dist/helpers/debounce.d.ts +14 -0
- package/dist/helpers/debounce.d.ts.map +1 -0
- package/dist/helpers/debounce.js +21 -0
- package/dist/helpers/pause.d.ts.map +1 -1
- package/dist/helpers/pause.js +3 -1
- package/dist/helpers/tests/network-adapter-tests.d.ts.map +1 -1
- package/dist/helpers/tests/network-adapter-tests.js +2 -2
- package/dist/helpers/throttle.d.ts +28 -0
- package/dist/helpers/throttle.d.ts.map +1 -0
- package/dist/helpers/throttle.js +39 -0
- package/dist/index.d.ts +11 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/network/NetworkAdapter.d.ts +3 -3
- package/dist/network/NetworkAdapter.d.ts.map +1 -1
- package/dist/network/NetworkSubsystem.d.ts +2 -2
- package/dist/network/NetworkSubsystem.d.ts.map +1 -1
- package/dist/network/NetworkSubsystem.js +30 -18
- package/dist/network/messages.d.ts +38 -68
- package/dist/network/messages.d.ts.map +1 -1
- package/dist/network/messages.js +13 -21
- package/dist/storage/StorageSubsystem.d.ts +1 -1
- package/dist/storage/StorageSubsystem.d.ts.map +1 -1
- package/dist/storage/StorageSubsystem.js +9 -9
- package/dist/synchronizer/CollectionSynchronizer.d.ts +3 -3
- package/dist/synchronizer/CollectionSynchronizer.d.ts.map +1 -1
- package/dist/synchronizer/CollectionSynchronizer.js +3 -3
- package/dist/synchronizer/DocSynchronizer.d.ts +4 -3
- package/dist/synchronizer/DocSynchronizer.d.ts.map +1 -1
- package/dist/synchronizer/DocSynchronizer.js +25 -34
- package/dist/synchronizer/Synchronizer.d.ts +2 -2
- package/dist/synchronizer/Synchronizer.d.ts.map +1 -1
- package/dist/types.d.ts +5 -1
- package/dist/types.d.ts.map +1 -1
- package/fuzz/fuzz.ts +8 -6
- package/fuzz/tsconfig.json +8 -0
- package/package.json +5 -13
- package/src/DocHandle.ts +12 -15
- package/src/DocUrl.ts +3 -1
- package/src/Repo.ts +36 -29
- package/src/helpers/cbor.ts +4 -4
- package/src/helpers/debounce.ts +25 -0
- package/src/helpers/headsAreSame.ts +1 -1
- package/src/helpers/pause.ts +7 -2
- package/src/helpers/tests/network-adapter-tests.ts +3 -3
- package/src/helpers/throttle.ts +43 -0
- package/src/helpers/withTimeout.ts +2 -2
- package/src/index.ts +36 -29
- package/src/network/NetworkAdapter.ts +7 -3
- package/src/network/NetworkSubsystem.ts +31 -23
- package/src/network/messages.ts +88 -151
- package/src/storage/StorageSubsystem.ts +12 -12
- package/src/synchronizer/CollectionSynchronizer.ts +7 -16
- package/src/synchronizer/DocSynchronizer.ts +42 -53
- package/src/synchronizer/Synchronizer.ts +2 -2
- package/src/types.ts +8 -3
- package/test/CollectionSynchronizer.test.ts +58 -53
- package/test/DocHandle.test.ts +35 -36
- package/test/DocSynchronizer.test.ts +5 -8
- package/test/Network.test.ts +1 -0
- package/test/Repo.test.ts +229 -158
- package/test/StorageSubsystem.test.ts +6 -9
- package/test/helpers/DummyNetworkAdapter.ts +9 -4
- package/test/helpers/DummyStorageAdapter.ts +4 -2
- package/test/tsconfig.json +8 -0
- package/typedoc.json +3 -3
- package/.mocharc.json +0 -5
- package/dist/EphemeralData.d.ts +0 -20
- package/dist/EphemeralData.d.ts.map +0 -1
- package/dist/EphemeralData.js +0 -1
- package/src/EphemeralData.ts +0 -17
package/.eslintrc
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
],
|
|
11
11
|
"ignorePatterns": ["dist/**", "test/**", "node_modules/**"],
|
|
12
12
|
"parser": "@typescript-eslint/parser",
|
|
13
|
-
"plugins": ["@typescript-eslint"
|
|
13
|
+
"plugins": ["@typescript-eslint"],
|
|
14
14
|
"parserOptions": {
|
|
15
15
|
"project": "./tsconfig.json",
|
|
16
16
|
"ecmaVersion": "latest",
|
package/dist/DocHandle.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as A from "@automerge/automerge/next";
|
|
2
2
|
import { EventEmitter } from "eventemitter3";
|
|
3
3
|
import { StateValue } from "xstate";
|
|
4
|
-
import type { DocumentId, PeerId
|
|
4
|
+
import type { AutomergeUrl, DocumentId, PeerId } from "./types.js";
|
|
5
5
|
/** DocHandle is a wrapper around a single Automerge document that lets us
|
|
6
6
|
* listen for changes and notify the network and storage of new changes.
|
|
7
7
|
*
|
|
@@ -110,7 +110,7 @@ export declare class DocHandle<T>//
|
|
|
110
110
|
* a user could have multiple tabs open and would appear as multiple PeerIds.
|
|
111
111
|
* every message source must have a unique PeerId.
|
|
112
112
|
*/
|
|
113
|
-
broadcast(message:
|
|
113
|
+
broadcast(message: unknown): void;
|
|
114
114
|
}
|
|
115
115
|
/** @hidden */
|
|
116
116
|
export interface DocHandleOptions {
|
|
@@ -140,13 +140,13 @@ export interface DocHandleChangePayload<T> {
|
|
|
140
140
|
/** Information about the change */
|
|
141
141
|
patchInfo: A.PatchInfo<T>;
|
|
142
142
|
}
|
|
143
|
-
export interface DocHandleEphemeralMessagePayload {
|
|
144
|
-
handle: DocHandle<
|
|
143
|
+
export interface DocHandleEphemeralMessagePayload<T> {
|
|
144
|
+
handle: DocHandle<T>;
|
|
145
145
|
senderId: PeerId;
|
|
146
146
|
message: unknown;
|
|
147
147
|
}
|
|
148
|
-
export interface DocHandleOutboundEphemeralMessagePayload {
|
|
149
|
-
handle: DocHandle<
|
|
148
|
+
export interface DocHandleOutboundEphemeralMessagePayload<T> {
|
|
149
|
+
handle: DocHandle<T>;
|
|
150
150
|
data: Uint8Array;
|
|
151
151
|
}
|
|
152
152
|
export interface DocHandleEvents<T> {
|
|
@@ -154,8 +154,8 @@ export interface DocHandleEvents<T> {
|
|
|
154
154
|
change: (payload: DocHandleChangePayload<T>) => void;
|
|
155
155
|
delete: (payload: DocHandleDeletePayload<T>) => void;
|
|
156
156
|
unavailable: (payload: DocHandleDeletePayload<T>) => void;
|
|
157
|
-
"ephemeral-message": (payload: DocHandleEphemeralMessagePayload) => void;
|
|
158
|
-
"ephemeral-message-outbound": (payload: DocHandleOutboundEphemeralMessagePayload) => void;
|
|
157
|
+
"ephemeral-message": (payload: DocHandleEphemeralMessagePayload<T>) => void;
|
|
158
|
+
"ephemeral-message-outbound": (payload: DocHandleOutboundEphemeralMessagePayload<T>) => void;
|
|
159
159
|
}
|
|
160
160
|
/**
|
|
161
161
|
* The state of a document handle
|
package/dist/DocHandle.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DocHandle.d.ts","sourceRoot":"","sources":["../src/DocHandle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,2BAA2B,CAAA;AAE9C,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EASL,UAAU,EAEX,MAAM,QAAQ,CAAA;
|
|
1
|
+
{"version":3,"file":"DocHandle.d.ts","sourceRoot":"","sources":["../src/DocHandle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,2BAA2B,CAAA;AAE9C,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EASL,UAAU,EAEX,MAAM,QAAQ,CAAA;AAMf,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAElE;;;;;;;;;;;KAWK;AACL,qBAAa,SAAS,CAAC,CAAC,CAAE,EAAE;AAC1B,SAAQ,YAAY,CAAC,eA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I,EAAE,UAAU,CAAA;CACjB;AAED,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,eAAe,EAAE,CAAC,OAAO,EAAE,6BAA6B,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IACpE,MAAM,EAAE,CAAC,OAAO,EAAE,sBAAsB,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IACpD,MAAM,EAAE,CAAC,OAAO,EAAE,sBAAsB,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IACpD,WAAW,EAAE,CAAC,OAAO,EAAE,sBAAsB,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IACzD,mBAAmB,EAAE,CAAC,OAAO,EAAE,gCAAgC,CAAC,CAAC,CAAC,KAAK,IAAI,CAAA;IAC3E,4BAA4B,EAAE,CAC5B,OAAO,EAAE,wCAAwC,CAAC,CAAC,CAAC,KACjD,IAAI,CAAA;CACV;AAMD;;;;GAIG;AACH,eAAO,MAAM,WAAW;IACtB,kEAAkE;;IAElE,mDAAmD;;IAEnD,sDAAsD;;IAEtD,6EAA6E;;IAE7E,gCAAgC;;IAEhC,qEAAqE;;IAErE,kDAAkD;;IAElD,4EAA4E;;CAEpE,CAAA;AACV,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,OAAO,WAAW,CAAC,CAAA;AAkBxE,eAAO,MAAM,KAAK;;;;;;;;;;;CAWR,CAAA;AA8CV,eAAO,MACL,IAAI,UACJ,OAAO,aACP,gBAAgB,qBAChB,UAAU,gBACV,KAAK,WACL,MAAM,YACN,OAAO,aACP,WAAW,eACE,CAAA"}
|
package/dist/DocHandle.js
CHANGED
|
@@ -3,11 +3,10 @@ import debug from "debug";
|
|
|
3
3
|
import { EventEmitter } from "eventemitter3";
|
|
4
4
|
import { assign, createMachine, interpret, } from "xstate";
|
|
5
5
|
import { waitFor } from "xstate/lib/waitFor.js";
|
|
6
|
-
import { headsAreSame } from "./helpers/headsAreSame.js";
|
|
7
|
-
import { pause } from "./helpers/pause.js";
|
|
8
|
-
import { TimeoutError, withTimeout } from "./helpers/withTimeout.js";
|
|
9
6
|
import { stringifyAutomergeUrl } from "./DocUrl.js";
|
|
10
7
|
import { encode } from "./helpers/cbor.js";
|
|
8
|
+
import { headsAreSame } from "./helpers/headsAreSame.js";
|
|
9
|
+
import { withTimeout } from "./helpers/withTimeout.js";
|
|
11
10
|
/** DocHandle is a wrapper around a single Automerge document that lets us
|
|
12
11
|
* listen for changes and notify the network and storage of new changes.
|
|
13
12
|
*
|
|
@@ -238,16 +237,13 @@ export class DocHandle//
|
|
|
238
237
|
* @param {awaitStates=[READY]} optional states to wait for, such as "LOADING". mostly for internal use.
|
|
239
238
|
*/
|
|
240
239
|
async doc(awaitStates = [READY, UNAVAILABLE]) {
|
|
241
|
-
await pause(); // yield one tick because reasons
|
|
242
240
|
try {
|
|
243
241
|
// wait for the document to enter one of the desired states
|
|
244
242
|
await this.#statePromise(awaitStates);
|
|
245
243
|
}
|
|
246
244
|
catch (error) {
|
|
247
|
-
if (
|
|
248
|
-
|
|
249
|
-
else
|
|
250
|
-
throw error;
|
|
245
|
+
// if we timed out (or the load has already failed), return undefined
|
|
246
|
+
return undefined;
|
|
251
247
|
}
|
|
252
248
|
// Return the document
|
|
253
249
|
return !this.isUnavailable() ? this.#doc : undefined;
|
package/dist/DocUrl.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DocUrl.d.ts","sourceRoot":"","sources":["../src/DocUrl.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,UAAU,EAChB,MAAM,YAAY,CAAA;AAInB,eAAO,MAAM,SAAS,eAAe,CAAA;AAErC;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,QAAS,YAAY;;;CAIlD,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB;
|
|
1
|
+
{"version":3,"file":"DocUrl.d.ts","sourceRoot":"","sources":["../src/DocUrl.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,UAAU,EAChB,MAAM,YAAY,CAAA;AAInB,eAAO,MAAM,SAAS,eAAe,CAAA;AAErC;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,QAAS,YAAY;;;CAIlD,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB;gBAGpB,UAAU,GAAG,gBAAgB;MACvC,YAQH,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,QAAS,MAAM,wBAK9C,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,QAAO,YAGpC,CAAA;AAEJ,eAAO,MAAM,kBAAkB,UACtB,UAAU,KAChB,gBAAgB,GAAG,SACyC,CAAA;AAE/D,eAAO,MAAM,kBAAkB,UAAW,gBAAgB,KAAG,UACtB,CAAA;AAEvC,eAAO,MAAM,eAAe,QAAS,MAAM,KAAG,YAAY,GAAG,SAM5D,CAAA"}
|
package/dist/Repo.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
import { EventEmitter } from "eventemitter3";
|
|
2
|
+
import { DocHandle } from "./DocHandle.js";
|
|
1
3
|
import { NetworkAdapter } from "./network/NetworkAdapter.js";
|
|
2
4
|
import { NetworkSubsystem } from "./network/NetworkSubsystem.js";
|
|
3
5
|
import { StorageAdapter } from "./storage/StorageAdapter.js";
|
|
4
6
|
import { StorageSubsystem } from "./storage/StorageSubsystem.js";
|
|
5
|
-
import {
|
|
6
|
-
import { DocHandle } from "./DocHandle.js";
|
|
7
|
-
import { EventEmitter } from "eventemitter3";
|
|
7
|
+
import { DocumentId, PeerId, type AutomergeUrl } from "./types.js";
|
|
8
8
|
/** A Repo is a collection of documents with networking, syncing, and storage capabilities. */
|
|
9
9
|
/** The `Repo` is the main entry point of this library
|
|
10
10
|
*
|
|
@@ -19,6 +19,9 @@ export declare class Repo extends EventEmitter<RepoEvents> {
|
|
|
19
19
|
networkSubsystem: NetworkSubsystem;
|
|
20
20
|
/** @hidden */
|
|
21
21
|
storageSubsystem?: StorageSubsystem;
|
|
22
|
+
/** The debounce rate is adjustable on the repo. */
|
|
23
|
+
/** @hidden */
|
|
24
|
+
saveDebounceRate: number;
|
|
22
25
|
/** By default, we share generously with all peers. */
|
|
23
26
|
/** @hidden */
|
|
24
27
|
sharePolicy: SharePolicy;
|
package/dist/Repo.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Repo.d.ts","sourceRoot":"","sources":["../src/Repo.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Repo.d.ts","sourceRoot":"","sources":["../src/Repo.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,SAAS,EAAiC,MAAM,gBAAgB,CAAA;AAQzE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAEhE,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAA;AAElE,8FAA8F;AAC9F;;;;;;GAMG;AACH,qBAAa,IAAK,SAAQ,YAAY,CAAC,UAAU,CAAC;;IAGhD,cAAc;IACd,gBAAgB,EAAE,gBAAgB,CAAA;IAClC,cAAc;IACd,gBAAgB,CAAC,EAAE,gBAAgB,CAAA;IAEnC,mDAAmD;IACnD,cAAc;IACd,gBAAgB,SAAM;IAItB,sDAAsD;IACtD,cAAc;IACd,WAAW,EAAE,WAAW,CAAmB;gBAE/B,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,EAAE,UAA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,11 +1,12 @@
|
|
|
1
|
+
import { next as Automerge } from "@automerge/automerge";
|
|
1
2
|
import debug from "debug";
|
|
3
|
+
import { EventEmitter } from "eventemitter3";
|
|
4
|
+
import { DocHandle } from "./DocHandle.js";
|
|
5
|
+
import { generateAutomergeUrl, isValidAutomergeUrl, parseAutomergeUrl, parseLegacyUUID, } from "./DocUrl.js";
|
|
6
|
+
import { throttle } from "./helpers/throttle.js";
|
|
2
7
|
import { NetworkSubsystem } from "./network/NetworkSubsystem.js";
|
|
3
8
|
import { StorageSubsystem } from "./storage/StorageSubsystem.js";
|
|
4
9
|
import { CollectionSynchronizer } from "./synchronizer/CollectionSynchronizer.js";
|
|
5
|
-
import { parseAutomergeUrl, generateAutomergeUrl, isValidAutomergeUrl, parseLegacyUUID, } from "./DocUrl.js";
|
|
6
|
-
import { DocHandle } from "./DocHandle.js";
|
|
7
|
-
import { EventEmitter } from "eventemitter3";
|
|
8
|
-
import { next as Automerge } from "@automerge/automerge";
|
|
9
10
|
/** A Repo is a collection of documents with networking, syncing, and storage capabilities. */
|
|
10
11
|
/** The `Repo` is the main entry point of this library
|
|
11
12
|
*
|
|
@@ -20,6 +21,9 @@ export class Repo extends EventEmitter {
|
|
|
20
21
|
networkSubsystem;
|
|
21
22
|
/** @hidden */
|
|
22
23
|
storageSubsystem;
|
|
24
|
+
/** The debounce rate is adjustable on the repo. */
|
|
25
|
+
/** @hidden */
|
|
26
|
+
saveDebounceRate = 100;
|
|
23
27
|
#handleCache = {};
|
|
24
28
|
/** By default, we share generously with all peers. */
|
|
25
29
|
/** @hidden */
|
|
@@ -33,10 +37,11 @@ export class Repo extends EventEmitter {
|
|
|
33
37
|
// up a document by ID. We listen for it in order to wire up storage and network synchronization.
|
|
34
38
|
this.on("document", async ({ handle, isNew }) => {
|
|
35
39
|
if (storageSubsystem) {
|
|
36
|
-
// Save when the document changes
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
+
// Save when the document changes, but no more often than saveDebounceRate.
|
|
41
|
+
const saveFn = ({ handle, doc, }) => {
|
|
42
|
+
void storageSubsystem.saveDoc(handle.documentId, doc);
|
|
43
|
+
};
|
|
44
|
+
const debouncedSaveFn = handle.on("heads-changed", throttle(saveFn, this.saveDebounceRate));
|
|
40
45
|
if (isNew) {
|
|
41
46
|
// this is a new document, immediately save it
|
|
42
47
|
await storageSubsystem.saveDoc(handle.documentId, handle.docSync());
|
|
@@ -84,9 +89,9 @@ export class Repo extends EventEmitter {
|
|
|
84
89
|
// SYNCHRONIZER
|
|
85
90
|
// The synchronizer uses the network subsystem to keep documents in sync with peers.
|
|
86
91
|
const synchronizer = new CollectionSynchronizer(this);
|
|
87
|
-
// When the synchronizer emits
|
|
92
|
+
// When the synchronizer emits messages, send them to peers
|
|
88
93
|
synchronizer.on("message", message => {
|
|
89
|
-
this.#log(`sending
|
|
94
|
+
this.#log(`sending ${message.type} message to ${message.targetId}`);
|
|
90
95
|
networkSubsystem.send(message);
|
|
91
96
|
});
|
|
92
97
|
// STORAGE
|
|
@@ -182,7 +187,7 @@ export class Repo extends EventEmitter {
|
|
|
182
187
|
throw new Error("Cloned handle doesn't have a document.");
|
|
183
188
|
}
|
|
184
189
|
const handle = this.create();
|
|
185
|
-
handle.update((
|
|
190
|
+
handle.update(() => {
|
|
186
191
|
// we replace the document with the new cloned one
|
|
187
192
|
return Automerge.clone(sourceDoc);
|
|
188
193
|
});
|
|
@@ -196,7 +201,7 @@ export class Repo extends EventEmitter {
|
|
|
196
201
|
/** The documentId of the handle to retrieve */
|
|
197
202
|
automergeUrl) {
|
|
198
203
|
if (!isValidAutomergeUrl(automergeUrl)) {
|
|
199
|
-
|
|
204
|
+
const maybeAutomergeUrl = parseLegacyUUID(automergeUrl);
|
|
200
205
|
if (maybeAutomergeUrl) {
|
|
201
206
|
console.warn("Legacy UUID document ID detected, converting to AutomergeUrl. This will be removed in a future version.");
|
|
202
207
|
automergeUrl = maybeAutomergeUrl;
|
|
@@ -225,15 +230,11 @@ export class Repo extends EventEmitter {
|
|
|
225
230
|
delete(
|
|
226
231
|
/** The documentId of the handle to delete */
|
|
227
232
|
id) {
|
|
228
|
-
if (isValidAutomergeUrl(id))
|
|
229
|
-
;
|
|
230
|
-
({ documentId: id } = parseAutomergeUrl(id));
|
|
231
|
-
}
|
|
233
|
+
if (isValidAutomergeUrl(id))
|
|
234
|
+
id = parseAutomergeUrl(id).documentId;
|
|
232
235
|
const handle = this.#getHandle(id, false);
|
|
233
236
|
handle.delete();
|
|
234
237
|
delete this.#handleCache[id];
|
|
235
|
-
this.emit("delete-document", {
|
|
236
|
-
documentId: id,
|
|
237
|
-
});
|
|
238
|
+
this.emit("delete-document", { documentId: id });
|
|
238
239
|
}
|
|
239
240
|
}
|
package/dist/helpers/cbor.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
-
export declare function encode(obj:
|
|
3
|
-
export declare function decode(buf: Buffer | Uint8Array):
|
|
2
|
+
export declare function encode(obj: unknown): Buffer;
|
|
3
|
+
export declare function decode<T = unknown>(buf: Buffer | Uint8Array): T;
|
|
4
4
|
//# sourceMappingURL=cbor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cbor.d.ts","sourceRoot":"","sources":["../../src/helpers/cbor.ts"],"names":[],"mappings":";AAEA,wBAAgB,MAAM,CAAC,GAAG,EAAE,
|
|
1
|
+
{"version":3,"file":"cbor.d.ts","sourceRoot":"","sources":["../../src/helpers/cbor.ts"],"names":[],"mappings":";AAEA,wBAAgB,MAAM,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAG3C;AAED,wBAAgB,MAAM,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,CAAC,CAE/D"}
|
package/dist/helpers/cbor.js
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** throttle( callback, rate )
|
|
2
|
+
* Returns a throttle function with a build in debounce timer that runs after `wait` ms.
|
|
3
|
+
*
|
|
4
|
+
* Note that the args go inside the parameter and you should be careful not to
|
|
5
|
+
* recreate the function on each usage. (In React, see useMemo().)
|
|
6
|
+
*
|
|
7
|
+
*
|
|
8
|
+
* Example usage:
|
|
9
|
+
* const callback = throttle((ev) => { doSomethingExpensiveOrOccasional() }, 100)
|
|
10
|
+
* target.addEventListener('frequent-event', callback);
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
export declare const throttle: <F extends (...args: Parameters<F>) => ReturnType<F>>(fn: F, rate: number) => (...args: Parameters<F>) => void;
|
|
14
|
+
//# sourceMappingURL=debounce.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debounce.d.ts","sourceRoot":"","sources":["../../src/helpers/debounce.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,eAAO,MAAM,QAAQ,qEAEb,MAAM,qCASb,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** throttle( callback, rate )
|
|
2
|
+
* Returns a throttle function with a build in debounce timer that runs after `wait` ms.
|
|
3
|
+
*
|
|
4
|
+
* Note that the args go inside the parameter and you should be careful not to
|
|
5
|
+
* recreate the function on each usage. (In React, see useMemo().)
|
|
6
|
+
*
|
|
7
|
+
*
|
|
8
|
+
* Example usage:
|
|
9
|
+
* const callback = throttle((ev) => { doSomethingExpensiveOrOccasional() }, 100)
|
|
10
|
+
* target.addEventListener('frequent-event', callback);
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
export const throttle = (fn, rate) => {
|
|
14
|
+
let timeout;
|
|
15
|
+
return function (...args) {
|
|
16
|
+
clearTimeout(timeout);
|
|
17
|
+
timeout = setTimeout(() => {
|
|
18
|
+
fn.apply(null, args);
|
|
19
|
+
}, rate);
|
|
20
|
+
};
|
|
21
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pause.d.ts","sourceRoot":"","sources":["../../src/helpers/pause.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,KAAK,+BAC4C,CAAA;AAE9D,wBAAgB,eAAe,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"pause.d.ts","sourceRoot":"","sources":["../../src/helpers/pause.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,KAAK,+BAC4C,CAAA;AAE9D,wBAAgB,eAAe,CAAC,CAAC,EAC/B,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EACnB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,CAAC,CAAC,CAOZ"}
|
package/dist/helpers/pause.js
CHANGED
|
@@ -2,6 +2,8 @@ export const pause = (t = 0) => new Promise(resolve => setTimeout(() => resolve(
|
|
|
2
2
|
export function rejectOnTimeout(promise, millis) {
|
|
3
3
|
return Promise.race([
|
|
4
4
|
promise,
|
|
5
|
-
pause(millis).then(() => {
|
|
5
|
+
pause(millis).then(() => {
|
|
6
|
+
throw new Error("timeout exceeded");
|
|
7
|
+
}),
|
|
6
8
|
]);
|
|
7
9
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"network-adapter-tests.d.ts","sourceRoot":"","sources":["../../../src/helpers/tests/network-adapter-tests.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"network-adapter-tests.d.ts","sourceRoot":"","sources":["../../../src/helpers/tests/network-adapter-tests.ts"],"names":[],"mappings":"AAEA,OAAO,EAAgB,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAIlE;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CA8HrE;AAID,KAAK,OAAO,GAAG,cAAc,GAAG,cAAc,EAAE,CAAA;AAEhD,MAAM,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC;IAClC,QAAQ,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,CAAA;IACrC,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAA;CACtB,CAAC,CAAA"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import assert from "assert";
|
|
2
|
+
import { describe, it } from "vitest";
|
|
1
3
|
import { Repo } from "../../index.js";
|
|
2
4
|
import { eventPromise, eventPromises } from "../eventPromise.js";
|
|
3
|
-
import { assert } from "chai";
|
|
4
|
-
import { describe, it } from "mocha";
|
|
5
5
|
import { pause } from "../pause.js";
|
|
6
6
|
/**
|
|
7
7
|
* Runs a series of tests against a set of three peers, each represented by one or more instantiated
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Throttle
|
|
2
|
+
* Returns a function with a built in throttle timer that runs after `delay` ms.
|
|
3
|
+
*
|
|
4
|
+
* This function differs from a conventional `throttle` in that it ensures the final
|
|
5
|
+
* call will also execute and delays sending the first one until `delay` ms to allow
|
|
6
|
+
* additional work to accumulate.
|
|
7
|
+
*
|
|
8
|
+
* Here's a diagram:
|
|
9
|
+
*
|
|
10
|
+
* calls +----++++++-----++----
|
|
11
|
+
* dlay ^--v ^--v^--v ^--v
|
|
12
|
+
* execs ---+----+---+------+--
|
|
13
|
+
*
|
|
14
|
+
* The goal in this design is to create batches of changes without flooding
|
|
15
|
+
* communication or storage systems while still feeling responsive.
|
|
16
|
+
* (By default we communicate at 10hz / every 100ms.)
|
|
17
|
+
*
|
|
18
|
+
* Note that the args go inside the parameter and you should be careful not to
|
|
19
|
+
* recreate the function on each usage. (In React, see useMemo().)
|
|
20
|
+
*
|
|
21
|
+
*
|
|
22
|
+
* Example usage:
|
|
23
|
+
* const callback = debounce((ev) => { doSomethingExpensiveOrOccasional() }, 100)
|
|
24
|
+
* target.addEventListener('frequent-event', callback);
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
export declare const throttle: <F extends (...args: Parameters<F>) => ReturnType<F>>(fn: F, delay: number) => (...args: Parameters<F>) => void;
|
|
28
|
+
//# sourceMappingURL=throttle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.d.ts","sourceRoot":"","sources":["../../src/helpers/throttle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,eAAO,MAAM,QAAQ,sEAEZ,MAAM,qCAad,CAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Throttle
|
|
2
|
+
* Returns a function with a built in throttle timer that runs after `delay` ms.
|
|
3
|
+
*
|
|
4
|
+
* This function differs from a conventional `throttle` in that it ensures the final
|
|
5
|
+
* call will also execute and delays sending the first one until `delay` ms to allow
|
|
6
|
+
* additional work to accumulate.
|
|
7
|
+
*
|
|
8
|
+
* Here's a diagram:
|
|
9
|
+
*
|
|
10
|
+
* calls +----++++++-----++----
|
|
11
|
+
* dlay ^--v ^--v^--v ^--v
|
|
12
|
+
* execs ---+----+---+------+--
|
|
13
|
+
*
|
|
14
|
+
* The goal in this design is to create batches of changes without flooding
|
|
15
|
+
* communication or storage systems while still feeling responsive.
|
|
16
|
+
* (By default we communicate at 10hz / every 100ms.)
|
|
17
|
+
*
|
|
18
|
+
* Note that the args go inside the parameter and you should be careful not to
|
|
19
|
+
* recreate the function on each usage. (In React, see useMemo().)
|
|
20
|
+
*
|
|
21
|
+
*
|
|
22
|
+
* Example usage:
|
|
23
|
+
* const callback = debounce((ev) => { doSomethingExpensiveOrOccasional() }, 100)
|
|
24
|
+
* target.addEventListener('frequent-event', callback);
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
export const throttle = (fn, delay) => {
|
|
28
|
+
let lastCall = Date.now();
|
|
29
|
+
let wait;
|
|
30
|
+
let timeout;
|
|
31
|
+
return function (...args) {
|
|
32
|
+
wait = lastCall + delay - Date.now();
|
|
33
|
+
clearTimeout(timeout);
|
|
34
|
+
timeout = setTimeout(() => {
|
|
35
|
+
fn.apply(null, args);
|
|
36
|
+
lastCall = Date.now();
|
|
37
|
+
}, wait);
|
|
38
|
+
};
|
|
39
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -25,16 +25,18 @@
|
|
|
25
25
|
* const handle = repo.create
|
|
26
26
|
* ```
|
|
27
27
|
*/
|
|
28
|
-
export { DocHandle
|
|
29
|
-
export
|
|
28
|
+
export { DocHandle } from "./DocHandle.js";
|
|
29
|
+
export { isValidAutomergeUrl, parseAutomergeUrl, stringifyAutomergeUrl, } from "./DocUrl.js";
|
|
30
|
+
export { Repo } from "./Repo.js";
|
|
30
31
|
export { NetworkAdapter } from "./network/NetworkAdapter.js";
|
|
31
|
-
export
|
|
32
|
-
export
|
|
33
|
-
export { isValidMessage } from "./network/messages.js";
|
|
34
|
-
export { Repo, type SharePolicy, type RepoConfig, type RepoEvents, type DeleteDocumentPayload, type DocumentPayload } from "./Repo.js";
|
|
35
|
-
export { StorageAdapter, type StorageKey } from "./storage/StorageAdapter.js";
|
|
36
|
-
export { parseAutomergeUrl, isValidAutomergeUrl, stringifyAutomergeUrl as generateAutomergeUrl, } from "./DocUrl.js";
|
|
37
|
-
export * from "./types.js";
|
|
32
|
+
export { isValidRepoMessage } from "./network/messages.js";
|
|
33
|
+
export { StorageAdapter } from "./storage/StorageAdapter.js";
|
|
38
34
|
/** @hidden **/
|
|
39
35
|
export * as cbor from "./helpers/cbor.js";
|
|
36
|
+
export type { DocHandleChangePayload, DocHandleDeletePayload, DocHandleEncodedChangePayload, DocHandleEphemeralMessagePayload, DocHandleEvents, DocHandleOptions, DocHandleOutboundEphemeralMessagePayload, HandleState, } from "./DocHandle.js";
|
|
37
|
+
export type { DeleteDocumentPayload, DocumentPayload, RepoConfig, RepoEvents, SharePolicy, } from "./Repo.js";
|
|
38
|
+
export type { NetworkAdapterEvents, OpenPayload, PeerCandidatePayload, PeerDisconnectedPayload, } from "./network/NetworkAdapter.js";
|
|
39
|
+
export type { ArriveMessage, DocumentUnavailableMessage, EphemeralMessage, Message, RepoMessage, RequestMessage, SyncMessage, WelcomeMessage, } from "./network/messages.js";
|
|
40
|
+
export type { StorageKey } from "./storage/StorageAdapter.js";
|
|
41
|
+
export * from "./types.js";
|
|
40
42
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAE,SAAS,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,qBAAqB,GACtB,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAC1D,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAE5D,eAAe;AACf,OAAO,KAAK,IAAI,MAAM,mBAAmB,CAAA;AAIzC,YAAY,EACV,sBAAsB,EACtB,sBAAsB,EACtB,6BAA6B,EAC7B,gCAAgC,EAChC,eAAe,EACf,gBAAgB,EAChB,wCAAwC,EACxC,WAAW,GACZ,MAAM,gBAAgB,CAAA;AACvB,YAAY,EACV,qBAAqB,EACrB,eAAe,EACf,UAAU,EACV,UAAU,EACV,WAAW,GACZ,MAAM,WAAW,CAAA;AAClB,YAAY,EACV,oBAAoB,EACpB,WAAW,EACX,oBAAoB,EACpB,uBAAuB,GACxB,MAAM,6BAA6B,CAAA;AACpC,YAAY,EACV,aAAa,EACb,0BAA0B,EAC1B,gBAAgB,EAChB,OAAO,EACP,WAAW,EACX,cAAc,EACd,WAAW,EACX,cAAc,GACf,MAAM,uBAAuB,CAAA;AAC9B,YAAY,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAA;AAC7D,cAAc,YAAY,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
* ```
|
|
27
27
|
*/
|
|
28
28
|
export { DocHandle } from "./DocHandle.js";
|
|
29
|
-
export {
|
|
30
|
-
export { isValidMessage } from "./network/messages.js";
|
|
29
|
+
export { isValidAutomergeUrl, parseAutomergeUrl, stringifyAutomergeUrl, } from "./DocUrl.js";
|
|
31
30
|
export { Repo } from "./Repo.js";
|
|
31
|
+
export { NetworkAdapter } from "./network/NetworkAdapter.js";
|
|
32
|
+
export { isValidRepoMessage } from "./network/messages.js";
|
|
32
33
|
export { StorageAdapter } from "./storage/StorageAdapter.js";
|
|
33
|
-
export { parseAutomergeUrl, isValidAutomergeUrl, stringifyAutomergeUrl as generateAutomergeUrl, } from "./DocUrl.js";
|
|
34
|
-
export * from "./types.js";
|
|
35
34
|
/** @hidden **/
|
|
36
35
|
export * as cbor from "./helpers/cbor.js";
|
|
36
|
+
export * from "./types.js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from "eventemitter3";
|
|
2
2
|
import { PeerId } from "../types.js";
|
|
3
|
-
import {
|
|
3
|
+
import { RepoMessage } from "./messages.js";
|
|
4
4
|
/** An interface representing some way to connect to other peers
|
|
5
5
|
*
|
|
6
6
|
* @remarks
|
|
@@ -19,7 +19,7 @@ export declare abstract class NetworkAdapter extends EventEmitter<NetworkAdapter
|
|
|
19
19
|
*
|
|
20
20
|
* @argument message - the message to send
|
|
21
21
|
*/
|
|
22
|
-
abstract send(message:
|
|
22
|
+
abstract send(message: RepoMessage): void;
|
|
23
23
|
/** Called by the {@link Repo} to disconnect from the network */
|
|
24
24
|
abstract disconnect(): void;
|
|
25
25
|
}
|
|
@@ -33,7 +33,7 @@ export interface NetworkAdapterEvents {
|
|
|
33
33
|
/** Emitted when the network adapter learns that a peer has disconnected */
|
|
34
34
|
"peer-disconnected": (payload: PeerDisconnectedPayload) => void;
|
|
35
35
|
/** Emitted when the network adapter receives a message from a peer */
|
|
36
|
-
message: (payload:
|
|
36
|
+
message: (payload: RepoMessage) => void;
|
|
37
37
|
}
|
|
38
38
|
export interface OpenPayload {
|
|
39
39
|
network: NetworkAdapter;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NetworkAdapter.d.ts","sourceRoot":"","sources":["../../src/network/NetworkAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"NetworkAdapter.d.ts","sourceRoot":"","sources":["../../src/network/NetworkAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAE3C;;;;;;GAMG;AACH,8BAAsB,cAAe,SAAQ,YAAY,CAAC,oBAAoB,CAAC;IAC7E,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAEtC;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,IAAI;IAEzC,gEAAgE;IAChE,QAAQ,CAAC,UAAU,IAAI,IAAI;CAC5B;AAID,MAAM,WAAW,oBAAoB;IACnC,mDAAmD;IACnD,KAAK,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,IAAI,CAAA;IAErC,yCAAyC;IACzC,KAAK,EAAE,MAAM,IAAI,CAAA;IAEjB,+DAA+D;IAC/D,gBAAgB,EAAE,CAAC,OAAO,EAAE,oBAAoB,KAAK,IAAI,CAAA;IAEzD,2EAA2E;IAC3E,mBAAmB,EAAE,CAAC,OAAO,EAAE,uBAAuB,KAAK,IAAI,CAAA;IAE/D,sEAAsE;IACtE,OAAO,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,IAAI,CAAA;CACxC;AAED,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,cAAc,CAAA;CACxB;AAED,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,uBAAuB;IACtC,MAAM,EAAE,MAAM,CAAA;CACf"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { EventEmitter } from "eventemitter3";
|
|
2
2
|
import { PeerId } from "../types.js";
|
|
3
3
|
import { NetworkAdapter, PeerDisconnectedPayload } from "./NetworkAdapter.js";
|
|
4
|
-
import {
|
|
4
|
+
import { MessageContents, RepoMessage } from "./messages.js";
|
|
5
5
|
export declare class NetworkSubsystem extends EventEmitter<NetworkSubsystemEvents> {
|
|
6
6
|
#private;
|
|
7
7
|
peerId: PeerId;
|
|
@@ -14,7 +14,7 @@ export declare class NetworkSubsystem extends EventEmitter<NetworkSubsystemEvent
|
|
|
14
14
|
export interface NetworkSubsystemEvents {
|
|
15
15
|
peer: (payload: PeerPayload) => void;
|
|
16
16
|
"peer-disconnected": (payload: PeerDisconnectedPayload) => void;
|
|
17
|
-
message: (payload:
|
|
17
|
+
message: (payload: RepoMessage) => void;
|
|
18
18
|
ready: () => void;
|
|
19
19
|
}
|
|
20
20
|
export interface PeerPayload {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NetworkSubsystem.d.ts","sourceRoot":"","sources":["../../src/network/NetworkSubsystem.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"NetworkSubsystem.d.ts","sourceRoot":"","sources":["../../src/network/NetworkSubsystem.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,MAAM,EAAa,MAAM,aAAa,CAAA;AAC/C,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAA;AAC7E,OAAO,EAEL,eAAe,EACf,WAAW,EAGZ,MAAM,eAAe,CAAA;AAOtB,qBAAa,gBAAiB,SAAQ,YAAY,CAAC,sBAAsB,CAAC;;IAUzB,MAAM;gBAAzC,QAAQ,EAAE,cAAc,EAAE,EAAS,MAAM,SAAiB;IAMtE,iBAAiB,CAAC,cAAc,EAAE,cAAc;IAsEhD,IAAI,CAAC,OAAO,EAAE,eAAe;IAsC7B,OAAO,gBAEN;IAED,SAAS,sBAUR;CACF;AAQD,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,IAAI,CAAA;IACpC,mBAAmB,EAAE,CAAC,OAAO,EAAE,uBAAuB,KAAK,IAAI,CAAA;IAC/D,OAAO,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,IAAI,CAAA;IACvC,KAAK,EAAE,MAAM,IAAI,CAAA;CAClB;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAA;CACf"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { EventEmitter } from "eventemitter3";
|
|
2
|
-
import { isEphemeralMessage, isValidMessage, } from "./messages.js";
|
|
3
1
|
import debug from "debug";
|
|
2
|
+
import { EventEmitter } from "eventemitter3";
|
|
3
|
+
import { isEphemeralMessage, isValidRepoMessage, } from "./messages.js";
|
|
4
4
|
const getEphemeralMessageSource = (message) => `${message.senderId}:${message.sessionId}`;
|
|
5
5
|
export class NetworkSubsystem extends EventEmitter {
|
|
6
6
|
peerId;
|
|
@@ -41,7 +41,7 @@ export class NetworkSubsystem extends EventEmitter {
|
|
|
41
41
|
this.emit("peer-disconnected", { peerId });
|
|
42
42
|
});
|
|
43
43
|
networkAdapter.on("message", msg => {
|
|
44
|
-
if (!
|
|
44
|
+
if (!isValidRepoMessage(msg)) {
|
|
45
45
|
this.#log(`invalid message: ${JSON.stringify(msg)}`);
|
|
46
46
|
return;
|
|
47
47
|
}
|
|
@@ -73,24 +73,36 @@ export class NetworkSubsystem extends EventEmitter {
|
|
|
73
73
|
this.#log(`Tried to send message but peer not found: ${message.targetId}`);
|
|
74
74
|
return;
|
|
75
75
|
}
|
|
76
|
-
this
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
76
|
+
/** Messages come in without a senderId and other required information; this is where we make
|
|
77
|
+
* sure they have everything they need.
|
|
78
|
+
*/
|
|
79
|
+
const prepareMessage = (message) => {
|
|
80
|
+
if (message.type === "ephemeral") {
|
|
81
|
+
if ("count" in message) {
|
|
82
|
+
// existing ephemeral message from another peer; pass on without changes
|
|
83
|
+
return message;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
// new ephemeral message from us; add our senderId as well as a counter and session id
|
|
87
|
+
return {
|
|
88
|
+
...message,
|
|
89
|
+
count: ++this.#count,
|
|
90
|
+
sessionId: this.#sessionId,
|
|
91
|
+
senderId: this.peerId,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
// other message type; just add our senderId
|
|
97
|
+
return {
|
|
81
98
|
...message,
|
|
82
|
-
count: ++this.#count,
|
|
83
|
-
sessionId: this.#sessionId,
|
|
84
99
|
senderId: this.peerId,
|
|
85
100
|
};
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
this.#log("Sync message", outbound);
|
|
92
|
-
peer.send(outbound);
|
|
93
|
-
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const outbound = prepareMessage(message);
|
|
104
|
+
this.#log("sending message", outbound);
|
|
105
|
+
peer.send(outbound);
|
|
94
106
|
}
|
|
95
107
|
isReady = () => {
|
|
96
108
|
return this.#readyAdapterCount === this.#adapters.length;
|