@dxos/teleport-extension-replicator 0.8.4-main.f9ba587 → 0.8.4-main.fffef41

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.
@@ -1,7 +1,7 @@
1
1
  import "@dxos/node-std/globals";
2
2
 
3
3
  // src/replicator-extension.ts
4
- import { asyncTimeout, DeferredTask, synchronized } from "@dxos/async";
4
+ import { DeferredTask, asyncTimeout, synchronized } from "@dxos/async";
5
5
  import { Context } from "@dxos/context";
6
6
  import { failUndefined } from "@dxos/debug";
7
7
  import { invariant } from "@dxos/invariant";
@@ -19,41 +19,41 @@ function _ts_decorate(decorators, target, key, desc) {
19
19
  }
20
20
  var __dxlog_file = "/__w/dxos/dxos/packages/core/mesh/teleport-extension-replicator/src/replicator-extension.ts";
21
21
  var ReplicatorExtension = class {
22
- constructor() {
23
- this._ctx = new Context({
24
- onError: (err) => {
25
- this._extensionContext?.close(err);
22
+ _ctx = new Context({
23
+ onError: (err) => {
24
+ this._extensionContext?.close(err);
25
+ }
26
+ }, {
27
+ F: __dxlog_file,
28
+ L: 31
29
+ });
30
+ _feeds = new ComplexMap(PublicKey.hash);
31
+ _streams = new ComplexMap(PublicKey.hash);
32
+ _rpc;
33
+ _extensionContext;
34
+ _options = {
35
+ upload: false
36
+ };
37
+ _updateTask = new DeferredTask(this._ctx, async () => {
38
+ try {
39
+ if (this._extensionContext.initiator === false) {
40
+ await this._rpc.rpc.ReplicatorService.updateFeeds({
41
+ feeds: Array.from(this._feeds.values()).map((feed) => ({
42
+ feedKey: feed.key,
43
+ download: true,
44
+ upload: this._options.upload
45
+ }))
46
+ });
47
+ } else if (this._extensionContext.initiator === true) {
48
+ await this._reevaluateFeeds();
26
49
  }
27
- }, {
28
- F: __dxlog_file,
29
- L: 30
30
- });
31
- this._feeds = new ComplexMap(PublicKey.hash);
32
- this._streams = new ComplexMap(PublicKey.hash);
33
- this._options = {
34
- upload: false
35
- };
36
- this._updateTask = new DeferredTask(this._ctx, async () => {
37
- try {
38
- if (this._extensionContext.initiator === false) {
39
- await this._rpc.rpc.ReplicatorService.updateFeeds({
40
- feeds: Array.from(this._feeds.values()).map((feed) => ({
41
- feedKey: feed.key,
42
- download: true,
43
- upload: this._options.upload
44
- }))
45
- });
46
- } else if (this._extensionContext.initiator === true) {
47
- await this._reevaluateFeeds();
48
- }
49
- } catch (err) {
50
- if (err instanceof RpcClosedError) {
51
- return;
52
- }
53
- throw err;
50
+ } catch (err) {
51
+ if (err instanceof RpcClosedError) {
52
+ return;
54
53
  }
55
- });
56
- }
54
+ throw err;
55
+ }
56
+ });
57
57
  get extensionInfo() {
58
58
  return {
59
59
  initiator: this._extensionContext?.initiator,
@@ -68,7 +68,7 @@ var ReplicatorExtension = class {
68
68
  options
69
69
  }, {
70
70
  F: __dxlog_file,
71
- L: 79,
71
+ L: 80,
72
72
  S: this,
73
73
  C: (f, a) => f(...a)
74
74
  });
@@ -83,7 +83,7 @@ var ReplicatorExtension = class {
83
83
  feedKey: feed.key
84
84
  }, {
85
85
  F: __dxlog_file,
86
- L: 88,
86
+ L: 89,
87
87
  S: this,
88
88
  C: (f, a) => f(...a)
89
89
  });
@@ -95,7 +95,7 @@ var ReplicatorExtension = class {
95
95
  this._extensionContext = context;
96
96
  log("open", void 0, {
97
97
  F: __dxlog_file,
98
- L: 96,
98
+ L: 97,
99
99
  S: this,
100
100
  C: (f, a) => f(...a)
101
101
  });
@@ -113,13 +113,13 @@ var ReplicatorExtension = class {
113
113
  feeds
114
114
  }, {
115
115
  F: __dxlog_file,
116
- L: 108,
116
+ L: 109,
117
117
  S: this,
118
118
  C: (f, a) => f(...a)
119
119
  });
120
120
  invariant(this._extensionContext.initiator === true, "Invalid call", {
121
121
  F: __dxlog_file,
122
- L: 109,
122
+ L: 110,
123
123
  S: this,
124
124
  A: [
125
125
  "this._extensionContext!.initiator === true",
@@ -133,13 +133,13 @@ var ReplicatorExtension = class {
133
133
  info
134
134
  }, {
135
135
  F: __dxlog_file,
136
- L: 113,
136
+ L: 114,
137
137
  S: this,
138
138
  C: (f, a) => f(...a)
139
139
  });
140
140
  invariant(this._extensionContext.initiator === false, "Invalid call", {
141
141
  F: __dxlog_file,
142
- L: 114,
142
+ L: 115,
143
143
  S: this,
144
144
  A: [
145
145
  "this._extensionContext!.initiator === false",
@@ -156,13 +156,13 @@ var ReplicatorExtension = class {
156
156
  info
157
157
  }, {
158
158
  F: __dxlog_file,
159
- L: 122,
159
+ L: 123,
160
160
  S: this,
161
161
  C: (f, a) => f(...a)
162
162
  });
163
163
  invariant(this._extensionContext.initiator === false, "Invalid call", {
164
164
  F: __dxlog_file,
165
- L: 124,
165
+ L: 125,
166
166
  S: this,
167
167
  A: [
168
168
  "this._extensionContext!.initiator === false",
@@ -186,7 +186,7 @@ var ReplicatorExtension = class {
186
186
  err
187
187
  }, {
188
188
  F: __dxlog_file,
189
- L: 141,
189
+ L: 142,
190
190
  S: this,
191
191
  C: (f, a) => f(...a)
192
192
  });
@@ -201,7 +201,7 @@ var ReplicatorExtension = class {
201
201
  err
202
202
  }, {
203
203
  F: __dxlog_file,
204
- L: 150,
204
+ L: 151,
205
205
  S: this,
206
206
  C: (f, a) => f(...a)
207
207
  });
@@ -214,7 +214,7 @@ var ReplicatorExtension = class {
214
214
  async _reevaluateFeeds() {
215
215
  log("_reevaluateFeeds", void 0, {
216
216
  F: __dxlog_file,
217
- L: 161,
217
+ L: 162,
218
218
  S: this,
219
219
  C: (f, a) => f(...a)
220
220
  });
@@ -228,7 +228,7 @@ var ReplicatorExtension = class {
228
228
  } catch (err) {
229
229
  log.catch(err, void 0, {
230
230
  F: __dxlog_file,
231
- L: 170,
231
+ L: 171,
232
232
  S: this,
233
233
  C: (f, a) => f(...a)
234
234
  });
@@ -254,13 +254,13 @@ var ReplicatorExtension = class {
254
254
  feedInfo
255
255
  }, {
256
256
  F: __dxlog_file,
257
- L: 191,
257
+ L: 192,
258
258
  S: this,
259
259
  C: (f, a) => f(...a)
260
260
  });
261
261
  invariant(this._extensionContext.initiator === true, "Invalid call", {
262
262
  F: __dxlog_file,
263
- L: 192,
263
+ L: 193,
264
264
  S: this,
265
265
  A: [
266
266
  "this._extensionContext!.initiator === true",
@@ -269,7 +269,7 @@ var ReplicatorExtension = class {
269
269
  });
270
270
  invariant(!this._streams.has(feedInfo.feedKey), `Replication already in progress for feed: ${feedInfo.feedKey}`, {
271
271
  F: __dxlog_file,
272
- L: 193,
272
+ L: 194,
273
273
  S: this,
274
274
  A: [
275
275
  "!this._streams.has(feedInfo.feedKey)",
@@ -291,7 +291,7 @@ var ReplicatorExtension = class {
291
291
  async _acceptReplication(feedInfo) {
292
292
  invariant(this._extensionContext.initiator === false, "Invalid call", {
293
293
  F: __dxlog_file,
294
- L: 208,
294
+ L: 209,
295
295
  S: this,
296
296
  A: [
297
297
  "this._extensionContext!.initiator === false",
@@ -311,13 +311,13 @@ var ReplicatorExtension = class {
311
311
  streamTag
312
312
  }, {
313
313
  F: __dxlog_file,
314
- L: 220,
314
+ L: 221,
315
315
  S: this,
316
316
  C: (f, a) => f(...a)
317
317
  });
318
318
  invariant(!this._streams.has(info.feedKey), `Replication already in progress for feed: ${info.feedKey}`, {
319
319
  F: __dxlog_file,
320
- L: 221,
320
+ L: 222,
321
321
  S: this,
322
322
  A: [
323
323
  "!this._streams.has(info.feedKey)",
@@ -344,7 +344,7 @@ var ReplicatorExtension = class {
344
344
  info
345
345
  }, {
346
346
  F: __dxlog_file,
347
- L: 257,
347
+ L: 258,
348
348
  S: this,
349
349
  C: (f, a) => f(...a)
350
350
  });
@@ -356,7 +356,7 @@ var ReplicatorExtension = class {
356
356
  info
357
357
  }, {
358
358
  F: __dxlog_file,
359
- L: 265,
359
+ L: 266,
360
360
  S: this,
361
361
  C: (f, a) => f(...a)
362
362
  });
@@ -368,7 +368,7 @@ var ReplicatorExtension = class {
368
368
  info
369
369
  }, {
370
370
  F: __dxlog_file,
371
- L: 271,
371
+ L: 272,
372
372
  S: this,
373
373
  C: (f, a) => f(...a)
374
374
  });
@@ -379,7 +379,7 @@ var ReplicatorExtension = class {
379
379
  count: replicationStreamErrors
380
380
  }, {
381
381
  F: __dxlog_file,
382
- L: 273,
382
+ L: 274,
383
383
  S: this,
384
384
  C: (f, a) => f(...a)
385
385
  });
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/replicator-extension.ts"],
4
- "sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport type { ProtocolStream } from 'hypercore-protocol';\nimport { type Duplex } from 'node:stream';\n\nimport { asyncTimeout, DeferredTask, synchronized } from '@dxos/async';\nimport { Context } from '@dxos/context';\nimport { failUndefined } from '@dxos/debug';\nimport { type FeedWrapper } from '@dxos/feed-store';\nimport { invariant } from '@dxos/invariant';\nimport { PublicKey } from '@dxos/keys';\nimport { log, logInfo } from '@dxos/log';\nimport { RpcClosedError, TimeoutError } from '@dxos/protocols';\nimport { schema } from '@dxos/protocols/proto';\nimport { type FeedInfo, type ReplicatorService } from '@dxos/protocols/proto/dxos/mesh/teleport/replicator';\nimport { createProtoRpcPeer, type ProtoRpcPeer } from '@dxos/rpc';\nimport { type ExtensionContext, type TeleportExtension } from '@dxos/teleport';\nimport { ComplexMap } from '@dxos/util';\n\nexport type ReplicationOptions = {\n upload: boolean;\n};\n\n/**\n * Manages replication between a set of feeds for a single teleport session.\n */\nexport class ReplicatorExtension implements TeleportExtension {\n private readonly _ctx = new Context({\n onError: (err) => {\n this._extensionContext?.close(err);\n },\n });\n\n private readonly _feeds = new ComplexMap<PublicKey, FeedWrapper<any>>(PublicKey.hash);\n private readonly _streams = new ComplexMap<PublicKey, ActiveStream>(PublicKey.hash);\n\n private _rpc?: ProtoRpcPeer<ServiceBundle>;\n private _extensionContext?: ExtensionContext;\n\n private _options: ReplicationOptions = {\n upload: false,\n };\n\n private readonly _updateTask = new DeferredTask(this._ctx, async () => {\n try {\n if (this._extensionContext!.initiator === false) {\n await this._rpc!.rpc.ReplicatorService.updateFeeds({\n feeds: Array.from(this._feeds.values()).map((feed) => ({\n feedKey: feed.key,\n download: true,\n upload: this._options.upload,\n })),\n });\n } else if (this._extensionContext!.initiator === true) {\n await this._reevaluateFeeds();\n }\n } catch (err) {\n if (err instanceof RpcClosedError) {\n return; // Some RPC requests might be pending while closing.\n }\n throw err;\n }\n });\n\n @logInfo\n private get extensionInfo() {\n return {\n initiator: this._extensionContext?.initiator,\n localPeerId: this._extensionContext?.localPeerId,\n remotePeerId: this._extensionContext?.remotePeerId,\n feeds: Array.from(this._feeds.keys()),\n };\n }\n\n setOptions(options: ReplicationOptions): this {\n this._options = options;\n log('setOptions', { options });\n if (this._extensionContext) {\n this._updateTask.schedule();\n }\n return this;\n }\n\n addFeed(feed: FeedWrapper<any>): void {\n this._feeds.set(feed.key, feed);\n log('addFeed', { feedKey: feed.key });\n if (this._extensionContext) {\n this._updateTask.schedule();\n }\n }\n\n async onOpen(context: ExtensionContext): Promise<void> {\n this._extensionContext = context;\n log('open');\n\n this._rpc = createProtoRpcPeer<ServiceBundle, ServiceBundle>({\n requested: {\n ReplicatorService: schema.getService('dxos.mesh.teleport.replicator.ReplicatorService'),\n },\n exposed: {\n ReplicatorService: schema.getService('dxos.mesh.teleport.replicator.ReplicatorService'),\n },\n handlers: {\n ReplicatorService: {\n updateFeeds: async ({ feeds }) => {\n log('received feed info', { feeds });\n invariant(this._extensionContext!.initiator === true, 'Invalid call');\n this._updateTask.schedule();\n },\n startReplication: async ({ info }) => {\n log('starting replication...', { info });\n invariant(this._extensionContext!.initiator === false, 'Invalid call');\n\n const streamTag = await this._acceptReplication(info);\n return {\n streamTag,\n };\n },\n stopReplication: async ({ info }) => {\n log('stopping replication...', { info });\n // TODO(dmaretskyi): Make sure any peer can stop replication.\n invariant(this._extensionContext!.initiator === false, 'Invalid call');\n\n await this._stopReplication(info.feedKey);\n },\n },\n },\n port: await context.createPort('rpc', {\n contentType: 'application/x-protobuf; messageType=\"dxos.rpc.Message\"',\n }),\n timeout: 10_000,\n });\n await this._rpc.open();\n\n this._updateTask.schedule();\n }\n\n async onClose(err?: Error | undefined): Promise<void> {\n log('close', { err });\n await this._ctx.dispose();\n await this._rpc?.close();\n for (const feedKey of this._streams.keys()) {\n await this._stopReplication(feedKey);\n }\n }\n\n async onAbort(err?: Error | undefined): Promise<void> {\n log('abort', { err });\n\n await this._ctx.dispose();\n await this._rpc?.abort();\n for (const feedKey of this._streams.keys()) {\n await this._stopReplication(feedKey);\n }\n }\n\n @synchronized\n private async _reevaluateFeeds(): Promise<void> {\n log('_reevaluateFeeds');\n for (const feedKey of this._feeds.keys()) {\n if (this._ctx.disposed) {\n return;\n }\n if (this._streams.has(feedKey) && this._options.upload !== this._streams.get(feedKey)?.info.upload) {\n try {\n await asyncTimeout(this._stopReplication(feedKey), 1000);\n } catch (err) {\n log.catch(err);\n }\n }\n\n if (this._ctx.disposed) {\n return;\n }\n if (!this._streams.has(feedKey)) {\n await this._initiateReplication({\n feedKey,\n download: true,\n upload: this._options.upload,\n });\n }\n }\n }\n\n /**\n * Try to initiate feed replication.\n */\n private async _initiateReplication(feedInfo: FeedInfo): Promise<void> {\n log('initiating replication', { feedInfo });\n invariant(this._extensionContext!.initiator === true, 'Invalid call');\n invariant(!this._streams.has(feedInfo.feedKey), `Replication already in progress for feed: ${feedInfo.feedKey}`);\n const { streamTag } = await this._rpc!.rpc.ReplicatorService.startReplication({ info: feedInfo });\n if (!streamTag) {\n return;\n }\n\n await this._replicateFeed(feedInfo, streamTag);\n }\n\n /**\n * Respond to a remote request to replicate a feed.\n * @returns A stream tag for the replication stream or `undefined` if we don't want to replicate.\n */\n @synchronized\n private async _acceptReplication(feedInfo: FeedInfo): Promise<string | undefined> {\n invariant(this._extensionContext!.initiator === false, 'Invalid call');\n\n if (!this._feeds.has(feedInfo.feedKey) || this._streams.has(feedInfo.feedKey)) {\n return undefined; // We don't have the feed or we are already replicating it.\n }\n\n const tag = `feed-${feedInfo.feedKey.toHex()}-${PublicKey.random().toHex().slice(0, 8)}`; // Generate a unique tag for the stream.\n await this._replicateFeed(feedInfo, tag);\n return tag;\n }\n\n private async _replicateFeed(info: FeedInfo, streamTag: string): Promise<void> {\n log('replicate', { info, streamTag });\n invariant(!this._streams.has(info.feedKey), `Replication already in progress for feed: ${info.feedKey}`);\n\n const feed = this._feeds.get(info.feedKey) ?? failUndefined();\n const networkStream = await this._extensionContext!.createStream(streamTag, {\n contentType: 'application/x-hypercore',\n });\n let replicationStreamErrors = 0;\n\n // https://github.com/holepunchto/hypercore/tree/v9.12.0#var-stream--feedreplicateisinitiator-options\n const replicationStream = feed.replicate(true, {\n live: true,\n upload: info.upload,\n download: info.download,\n noise: false,\n encrypted: false,\n maxRequests: 1024,\n });\n\n // Left for testing.\n // const debug = true;\n // if (debug) {\n // feed.on('sync', () => {\n // log.info('sync', { key: feed.key, length: feed.length });\n // });\n // feed.on('download', (index: number, data: any) => {\n // log.info('download', {\n // key: feed.key,\n // index,\n // length: feed.length,\n // data: data.length,\n // });\n // });\n // }\n\n replicationStream.on('error', (err) => {\n if (err instanceof TimeoutError) {\n log.info('replication stream timeout', { err, info });\n return;\n }\n // TODO(nf): use sentinel errors\n if (\n err?.message === 'Writable stream closed prematurely' ||\n err?.message === 'Cannot call write after a stream was destroyed'\n ) {\n log('replication stream closed', { err, info });\n return;\n }\n\n // TODO(nf): WARN on first error? log full info on some subsequent errors?\n if (replicationStreamErrors === 0) {\n log.info('replication stream error', { err, info });\n } else {\n log.info('replication stream error', { err, feedKey: info.feedKey, count: replicationStreamErrors });\n }\n replicationStreamErrors++;\n });\n\n this._streams.set(info.feedKey, {\n streamTag,\n networkStream,\n replicationStream,\n info,\n });\n\n networkStream.pipe(replicationStream as any).pipe(networkStream);\n }\n\n private async _stopReplication(feedKey: PublicKey): Promise<void> {\n const stream = this._streams.get(feedKey);\n if (!stream) {\n return;\n }\n\n stream.networkStream.destroy();\n this._streams.delete(feedKey);\n }\n}\n\ntype ServiceBundle = {\n ReplicatorService: ReplicatorService;\n};\n\ntype ActiveStream = {\n streamTag: string;\n networkStream: Duplex;\n replicationStream: ProtocolStream;\n info: FeedInfo;\n};\n"],
5
- "mappings": ";;;AAOA,SAASA,cAAcC,cAAcC,oBAAoB;AACzD,SAASC,eAAe;AACxB,SAASC,qBAAqB;AAE9B,SAASC,iBAAiB;AAC1B,SAASC,iBAAiB;AAC1B,SAASC,KAAKC,eAAe;AAC7B,SAASC,gBAAgBC,oBAAoB;AAC7C,SAASC,cAAc;AAEvB,SAASC,0BAA6C;AAEtD,SAASC,kBAAkB;;;;;;;;AASpB,IAAMC,sBAAN,MAAMA;EAAN;AACYC,gBAAO,IAAIZ,QAAQ;MAClCa,SAAS,CAACC,QAAAA;AACR,aAAKC,mBAAmBC,MAAMF,GAAAA;MAChC;IACF,GAAA;;;;AAEiBG,kBAAS,IAAIP,WAAwCP,UAAUe,IAAI;AACnEC,oBAAW,IAAIT,WAAoCP,UAAUe,IAAI;AAK1EE,oBAA+B;MACrCC,QAAQ;IACV;AAEiBC,uBAAc,IAAIxB,aAAa,KAAKc,MAAM,YAAA;AACzD,UAAI;AACF,YAAI,KAAKG,kBAAmBQ,cAAc,OAAO;AAC/C,gBAAM,KAAKC,KAAMC,IAAIC,kBAAkBC,YAAY;YACjDC,OAAOC,MAAMC,KAAK,KAAKb,OAAOc,OAAM,CAAA,EAAIC,IAAI,CAACC,UAAU;cACrDC,SAASD,KAAKE;cACdC,UAAU;cACVf,QAAQ,KAAKD,SAASC;YACxB,EAAA;UACF,CAAA;QACF,WAAW,KAAKN,kBAAmBQ,cAAc,MAAM;AACrD,gBAAM,KAAKc,iBAAgB;QAC7B;MACF,SAASvB,KAAK;AACZ,YAAIA,eAAeR,gBAAgB;AACjC;QACF;AACA,cAAMQ;MACR;IACF,CAAA;;EAEA,IACYwB,gBAAgB;AAC1B,WAAO;MACLf,WAAW,KAAKR,mBAAmBQ;MACnCgB,aAAa,KAAKxB,mBAAmBwB;MACrCC,cAAc,KAAKzB,mBAAmByB;MACtCZ,OA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pB,OAAO,KAAKjC,cAAAA;AAC9C,UAAM+E,gBAAgB,MAAM,KAAKjE,kBAAmBkE,aAAa1B,WAAW;MAC1EM,aAAa;IACf,CAAA;AACA,QAAIqB,0BAA0B;AAG9B,UAAMC,oBAAoBlD,KAAKmD,UAAU,MAAM;MAC7CC,MAAM;MACNhE,QAAQiC,KAAKjC;MACbe,UAAUkB,KAAKlB;MACfkD,OAAO;MACPC,WAAW;MACXC,aAAa;IACf,CAAA;AAkBAL,sBAAkBM,GAAG,SAAS,CAAC3E,QAAAA;AAC7B,UAAIA,eAAeP,cAAc;AAC/BH,YAAIkD,KAAK,8BAA8B;UAAExC;UAAKwC;QAAK,GAAA;;;;;;AACnD;MACF;AAEA,UACExC,KAAK4E,YAAY,wCACjB5E,KAAK4E,YAAY,kDACjB;AACAtF,YAAI,6BAA6B;UAAEU;UAAKwC;QAAK,GAAA;;;;;;AAC7C;MACF;AAGA,UAAI4B,4BAA4B,GAAG;AACjC9E,YAAIkD,KAAK,4BAA4B;UAAExC;UAAKwC;QAAK,GAAA;;;;;;MACnD,OAAO;AACLlD,YAAIkD,KAAK,4BAA4B;UAAExC;UAAKoB,SAASoB,KAAKpB;UAASyD,OAAOT;QAAwB,GAAA;;;;;;MACpG;AACAA;IACF,CAAA;AAEA,SAAK/D,SAAS2B,IAAIQ,KAAKpB,SAAS;MAC9BqB;MACAyB;MACAG;MACA7B;IACF,CAAA;AAEA0B,kBAAcY,KAAKT,iBAAAA,EAA0BS,KAAKZ,aAAAA;EACpD;EAEA,MAActB,iBAAiBxB,SAAmC;AAChE,UAAM2D,SAAS,KAAK1E,SAASmD,IAAIpC,OAAAA;AACjC,QAAI,CAAC2D,QAAQ;AACX;IACF;AAEAA,WAAOb,cAAcc,QAAO;AAC5B,SAAK3E,SAAS4E,OAAO7D,OAAAA;EACvB;AACF;;;;;;;;;;",
6
- "names": ["asyncTimeout", "DeferredTask", "synchronized", "Context", "failUndefined", "invariant", "PublicKey", "log", "logInfo", "RpcClosedError", "TimeoutError", "schema", "createProtoRpcPeer", "ComplexMap", "ReplicatorExtension", "_ctx", "onError", "err", "_extensionContext", "close", "_feeds", "hash", "_streams", "_options", "upload", "_updateTask", "initiator", "_rpc", "rpc", "ReplicatorService", "updateFeeds", "feeds", "Array", "from", "values", "map", "feed", "feedKey", "key", "download", "_reevaluateFeeds", "extensionInfo", "localPeerId", "remotePeerId", "keys", "setOptions", "options", "schedule", "addFeed", "set", "onOpen", "context", "requested", "getService", "exposed", "handlers", "startReplication", "info", "streamTag", "_acceptReplication", "stopReplication", "_stopReplication", "port", "createPort", "contentType", "timeout", "open", "onClose", "dispose", "onAbort", "abort", "disposed", "has", "get", "catch", "_initiateReplication", "feedInfo", "_replicateFeed", "undefined", "tag", "toHex", "random", "slice", "networkStream", "createStream", "replicationStreamErrors", "replicationStream", "replicate", "live", "noise", "encrypted", "maxRequests", "on", "message", "count", "pipe", "stream", "destroy", "delete"]
4
+ "sourcesContent": ["//\n// Copyright 2022 DXOS.org\n//\n\nimport { type Duplex } from 'node:stream';\n\nimport type { ProtocolStream } from 'hypercore-protocol';\n\nimport { DeferredTask, asyncTimeout, synchronized } from '@dxos/async';\nimport { Context } from '@dxos/context';\nimport { failUndefined } from '@dxos/debug';\nimport { type FeedWrapper } from '@dxos/feed-store';\nimport { invariant } from '@dxos/invariant';\nimport { PublicKey } from '@dxos/keys';\nimport { log, logInfo } from '@dxos/log';\nimport { RpcClosedError, TimeoutError } from '@dxos/protocols';\nimport { schema } from '@dxos/protocols/proto';\nimport { type FeedInfo, type ReplicatorService } from '@dxos/protocols/proto/dxos/mesh/teleport/replicator';\nimport { type ProtoRpcPeer, createProtoRpcPeer } from '@dxos/rpc';\nimport { type ExtensionContext, type TeleportExtension } from '@dxos/teleport';\nimport { ComplexMap } from '@dxos/util';\n\nexport type ReplicationOptions = {\n upload: boolean;\n};\n\n/**\n * Manages replication between a set of feeds for a single teleport session.\n */\nexport class ReplicatorExtension implements TeleportExtension {\n private readonly _ctx = new Context({\n onError: (err) => {\n this._extensionContext?.close(err);\n },\n });\n\n private readonly _feeds = new ComplexMap<PublicKey, FeedWrapper<any>>(PublicKey.hash);\n private readonly _streams = new ComplexMap<PublicKey, ActiveStream>(PublicKey.hash);\n\n private _rpc?: ProtoRpcPeer<ServiceBundle>;\n private _extensionContext?: ExtensionContext;\n\n private _options: ReplicationOptions = {\n upload: false,\n };\n\n private readonly _updateTask = new DeferredTask(this._ctx, async () => {\n try {\n if (this._extensionContext!.initiator === false) {\n await this._rpc!.rpc.ReplicatorService.updateFeeds({\n feeds: Array.from(this._feeds.values()).map((feed) => ({\n feedKey: feed.key,\n download: true,\n upload: this._options.upload,\n })),\n });\n } else if (this._extensionContext!.initiator === true) {\n await this._reevaluateFeeds();\n }\n } catch (err) {\n if (err instanceof RpcClosedError) {\n return; // Some RPC requests might be pending while closing.\n }\n throw err;\n }\n });\n\n @logInfo\n private get extensionInfo() {\n return {\n initiator: this._extensionContext?.initiator,\n localPeerId: this._extensionContext?.localPeerId,\n remotePeerId: this._extensionContext?.remotePeerId,\n feeds: Array.from(this._feeds.keys()),\n };\n }\n\n setOptions(options: ReplicationOptions): this {\n this._options = options;\n log('setOptions', { options });\n if (this._extensionContext) {\n this._updateTask.schedule();\n }\n return this;\n }\n\n addFeed(feed: FeedWrapper<any>): void {\n this._feeds.set(feed.key, feed);\n log('addFeed', { feedKey: feed.key });\n if (this._extensionContext) {\n this._updateTask.schedule();\n }\n }\n\n async onOpen(context: ExtensionContext): Promise<void> {\n this._extensionContext = context;\n log('open');\n\n this._rpc = createProtoRpcPeer<ServiceBundle, ServiceBundle>({\n requested: {\n ReplicatorService: schema.getService('dxos.mesh.teleport.replicator.ReplicatorService'),\n },\n exposed: {\n ReplicatorService: schema.getService('dxos.mesh.teleport.replicator.ReplicatorService'),\n },\n handlers: {\n ReplicatorService: {\n updateFeeds: async ({ feeds }) => {\n log('received feed info', { feeds });\n invariant(this._extensionContext!.initiator === true, 'Invalid call');\n this._updateTask.schedule();\n },\n startReplication: async ({ info }) => {\n log('starting replication...', { info });\n invariant(this._extensionContext!.initiator === false, 'Invalid call');\n\n const streamTag = await this._acceptReplication(info);\n return {\n streamTag,\n };\n },\n stopReplication: async ({ info }) => {\n log('stopping replication...', { info });\n // TODO(dmaretskyi): Make sure any peer can stop replication.\n invariant(this._extensionContext!.initiator === false, 'Invalid call');\n\n await this._stopReplication(info.feedKey);\n },\n },\n },\n port: await context.createPort('rpc', {\n contentType: 'application/x-protobuf; messageType=\"dxos.rpc.Message\"',\n }),\n timeout: 10_000,\n });\n await this._rpc.open();\n\n this._updateTask.schedule();\n }\n\n async onClose(err?: Error | undefined): Promise<void> {\n log('close', { err });\n await this._ctx.dispose();\n await this._rpc?.close();\n for (const feedKey of this._streams.keys()) {\n await this._stopReplication(feedKey);\n }\n }\n\n async onAbort(err?: Error | undefined): Promise<void> {\n log('abort', { err });\n\n await this._ctx.dispose();\n await this._rpc?.abort();\n for (const feedKey of this._streams.keys()) {\n await this._stopReplication(feedKey);\n }\n }\n\n @synchronized\n private async _reevaluateFeeds(): Promise<void> {\n log('_reevaluateFeeds');\n for (const feedKey of this._feeds.keys()) {\n if (this._ctx.disposed) {\n return;\n }\n if (this._streams.has(feedKey) && this._options.upload !== this._streams.get(feedKey)?.info.upload) {\n try {\n await asyncTimeout(this._stopReplication(feedKey), 1000);\n } catch (err) {\n log.catch(err);\n }\n }\n\n if (this._ctx.disposed) {\n return;\n }\n if (!this._streams.has(feedKey)) {\n await this._initiateReplication({\n feedKey,\n download: true,\n upload: this._options.upload,\n });\n }\n }\n }\n\n /**\n * Try to initiate feed replication.\n */\n private async _initiateReplication(feedInfo: FeedInfo): Promise<void> {\n log('initiating replication', { feedInfo });\n invariant(this._extensionContext!.initiator === true, 'Invalid call');\n invariant(!this._streams.has(feedInfo.feedKey), `Replication already in progress for feed: ${feedInfo.feedKey}`);\n const { streamTag } = await this._rpc!.rpc.ReplicatorService.startReplication({ info: feedInfo });\n if (!streamTag) {\n return;\n }\n\n await this._replicateFeed(feedInfo, streamTag);\n }\n\n /**\n * Respond to a remote request to replicate a feed.\n * @returns A stream tag for the replication stream or `undefined` if we don't want to replicate.\n */\n @synchronized\n private async _acceptReplication(feedInfo: FeedInfo): Promise<string | undefined> {\n invariant(this._extensionContext!.initiator === false, 'Invalid call');\n\n if (!this._feeds.has(feedInfo.feedKey) || this._streams.has(feedInfo.feedKey)) {\n return undefined; // We don't have the feed or we are already replicating it.\n }\n\n const tag = `feed-${feedInfo.feedKey.toHex()}-${PublicKey.random().toHex().slice(0, 8)}`; // Generate a unique tag for the stream.\n await this._replicateFeed(feedInfo, tag);\n return tag;\n }\n\n private async _replicateFeed(info: FeedInfo, streamTag: string): Promise<void> {\n log('replicate', { info, streamTag });\n invariant(!this._streams.has(info.feedKey), `Replication already in progress for feed: ${info.feedKey}`);\n\n const feed = this._feeds.get(info.feedKey) ?? failUndefined();\n const networkStream = await this._extensionContext!.createStream(streamTag, {\n contentType: 'application/x-hypercore',\n });\n let replicationStreamErrors = 0;\n\n // https://github.com/holepunchto/hypercore/tree/v9.12.0#var-stream--feedreplicateisinitiator-options\n const replicationStream = feed.replicate(true, {\n live: true,\n upload: info.upload,\n download: info.download,\n noise: false,\n encrypted: false,\n maxRequests: 1024,\n });\n\n // Left for testing.\n // const debug = true;\n // if (debug) {\n // feed.on('sync', () => {\n // log.info('sync', { key: feed.key, length: feed.length });\n // });\n // feed.on('download', (index: number, data: any) => {\n // log.info('download', {\n // key: feed.key,\n // index,\n // length: feed.length,\n // data: data.length,\n // });\n // });\n // }\n\n replicationStream.on('error', (err) => {\n if (err instanceof TimeoutError) {\n log.info('replication stream timeout', { err, info });\n return;\n }\n // TODO(nf): use sentinel errors\n if (\n err?.message === 'Writable stream closed prematurely' ||\n err?.message === 'Cannot call write after a stream was destroyed'\n ) {\n log('replication stream closed', { err, info });\n return;\n }\n\n // TODO(nf): WARN on first error? log full info on some subsequent errors?\n if (replicationStreamErrors === 0) {\n log.info('replication stream error', { err, info });\n } else {\n log.info('replication stream error', { err, feedKey: info.feedKey, count: replicationStreamErrors });\n }\n replicationStreamErrors++;\n });\n\n this._streams.set(info.feedKey, {\n streamTag,\n networkStream,\n replicationStream,\n info,\n });\n\n networkStream.pipe(replicationStream as any).pipe(networkStream);\n }\n\n private async _stopReplication(feedKey: PublicKey): Promise<void> {\n const stream = this._streams.get(feedKey);\n if (!stream) {\n return;\n }\n\n stream.networkStream.destroy();\n this._streams.delete(feedKey);\n }\n}\n\ntype ServiceBundle = {\n ReplicatorService: ReplicatorService;\n};\n\ntype ActiveStream = {\n streamTag: string;\n networkStream: Duplex;\n replicationStream: ProtocolStream;\n info: FeedInfo;\n};\n"],
5
+ "mappings": ";;;AAQA,SAASA,cAAcC,cAAcC,oBAAoB;AACzD,SAASC,eAAe;AACxB,SAASC,qBAAqB;AAE9B,SAASC,iBAAiB;AAC1B,SAASC,iBAAiB;AAC1B,SAASC,KAAKC,eAAe;AAC7B,SAASC,gBAAgBC,oBAAoB;AAC7C,SAASC,cAAc;AAEvB,SAA4BC,0BAA0B;AAEtD,SAASC,kBAAkB;;;;;;;;AASpB,IAAMC,sBAAN,MAAMA;EACMC,OAAO,IAAIZ,QAAQ;IAClCa,SAAS,CAACC,QAAAA;AACR,WAAKC,mBAAmBC,MAAMF,GAAAA;IAChC;EACF,GAAA;;;;EAEiBG,SAAS,IAAIP,WAAwCP,UAAUe,IAAI;EACnEC,WAAW,IAAIT,WAAoCP,UAAUe,IAAI;EAE1EE;EACAL;EAEAM,WAA+B;IACrCC,QAAQ;EACV;EAEiBC,cAAc,IAAI1B,aAAa,KAAKe,MAAM,YAAA;AACzD,QAAI;AACF,UAAI,KAAKG,kBAAmBS,cAAc,OAAO;AAC/C,cAAM,KAAKJ,KAAMK,IAAIC,kBAAkBC,YAAY;UACjDC,OAAOC,MAAMC,KAAK,KAAKb,OAAOc,OAAM,CAAA,EAAIC,IAAI,CAACC,UAAU;YACrDC,SAASD,KAAKE;YACdC,UAAU;YACVd,QAAQ,K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BAAAA,EAA0BS,KAAKZ,aAAAA;EACpD;EAEA,MAActB,iBAAiBxB,SAAmC;AAChE,UAAM2D,SAAS,KAAK1E,SAASmD,IAAIpC,OAAAA;AACjC,QAAI,CAAC2D,QAAQ;AACX;IACF;AAEAA,WAAOb,cAAcc,QAAO;AAC5B,SAAK3E,SAAS4E,OAAO7D,OAAAA;EACvB;AACF;;;;;;;;;;",
6
+ "names": ["DeferredTask", "asyncTimeout", "synchronized", "Context", "failUndefined", "invariant", "PublicKey", "log", "logInfo", "RpcClosedError", "TimeoutError", "schema", "createProtoRpcPeer", "ComplexMap", "ReplicatorExtension", "_ctx", "onError", "err", "_extensionContext", "close", "_feeds", "hash", "_streams", "_rpc", "_options", "upload", "_updateTask", "initiator", "rpc", "ReplicatorService", "updateFeeds", "feeds", "Array", "from", "values", "map", "feed", "feedKey", "key", "download", "_reevaluateFeeds", "extensionInfo", "localPeerId", "remotePeerId", "keys", "setOptions", "options", "schedule", "addFeed", "set", "onOpen", "context", "requested", "getService", "exposed", "handlers", "startReplication", "info", "streamTag", "_acceptReplication", "stopReplication", "_stopReplication", "port", "createPort", "contentType", "timeout", "open", "onClose", "dispose", "onAbort", "abort", "disposed", "has", "get", "catch", "_initiateReplication", "feedInfo", "_replicateFeed", "undefined", "tag", "toHex", "random", "slice", "networkStream", "createStream", "replicationStreamErrors", "replicationStream", "replicate", "live", "noise", "encrypted", "maxRequests", "on", "message", "count", "pipe", "stream", "destroy", "delete"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"src/replicator-extension.ts":{"bytes":38831,"imports":[{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/context","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/protocols","kind":"import-statement","external":true},{"path":"@dxos/protocols/proto","kind":"import-statement","external":true},{"path":"@dxos/rpc","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"src/index.ts":{"bytes":507,"imports":[{"path":"src/replicator-extension.ts","kind":"import-statement","original":"./replicator-extension"}],"format":"esm"}},"outputs":{"dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":16530},"dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/context","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/protocols","kind":"import-statement","external":true},{"path":"@dxos/protocols/proto","kind":"import-statement","external":true},{"path":"@dxos/rpc","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["ReplicatorExtension"],"entryPoint":"src/index.ts","inputs":{"src/replicator-extension.ts":{"bytesInOutput":11694},"src/index.ts":{"bytesInOutput":0}},"bytes":11828}}}
1
+ {"inputs":{"src/replicator-extension.ts":{"bytes":38835,"imports":[{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/context","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/protocols","kind":"import-statement","external":true},{"path":"@dxos/protocols/proto","kind":"import-statement","external":true},{"path":"@dxos/rpc","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"src/index.ts":{"bytes":507,"imports":[{"path":"src/replicator-extension.ts","kind":"import-statement","original":"./replicator-extension"}],"format":"esm"}},"outputs":{"dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":16531},"dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/context","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/keys","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/protocols","kind":"import-statement","external":true},{"path":"@dxos/protocols/proto","kind":"import-statement","external":true},{"path":"@dxos/rpc","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["ReplicatorExtension"],"entryPoint":"src/index.ts","inputs":{"src/replicator-extension.ts":{"bytesInOutput":11610},"src/index.ts":{"bytesInOutput":0}},"bytes":11744}}}
@@ -1,7 +1,7 @@
1
1
  import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
2
 
3
3
  // src/replicator-extension.ts
4
- import { asyncTimeout, DeferredTask, synchronized } from "@dxos/async";
4
+ import { DeferredTask, asyncTimeout, synchronized } from "@dxos/async";
5
5
  import { Context } from "@dxos/context";
6
6
  import { failUndefined } from "@dxos/debug";
7
7
  import { invariant } from "@dxos/invariant";
@@ -19,41 +19,41 @@ function _ts_decorate(decorators, target, key, desc) {
19
19
  }
20
20
  var __dxlog_file = "/__w/dxos/dxos/packages/core/mesh/teleport-extension-replicator/src/replicator-extension.ts";
21
21
  var ReplicatorExtension = class {
22
- constructor() {
23
- this._ctx = new Context({
24
- onError: (err) => {
25
- this._extensionContext?.close(err);
22
+ _ctx = new Context({
23
+ onError: (err) => {
24
+ this._extensionContext?.close(err);
25
+ }
26
+ }, {
27
+ F: __dxlog_file,
28
+ L: 31
29
+ });
30
+ _feeds = new ComplexMap(PublicKey.hash);
31
+ _streams = new ComplexMap(PublicKey.hash);
32
+ _rpc;
33
+ _extensionContext;
34
+ _options = {
35
+ upload: false
36
+ };
37
+ _updateTask = new DeferredTask(this._ctx, async () => {
38
+ try {
39
+ if (this._extensionContext.initiator === false) {
40
+ await this._rpc.rpc.ReplicatorService.updateFeeds({
41
+ feeds: Array.from(this._feeds.values()).map((feed) => ({
42
+ feedKey: feed.key,
43
+ download: true,
44
+ upload: this._options.upload
45
+ }))
46
+ });
47
+ } else if (this._extensionContext.initiator === true) {
48
+ await this._reevaluateFeeds();
26
49
  }
27
- }, {
28
- F: __dxlog_file,
29
- L: 30
30
- });
31
- this._feeds = new ComplexMap(PublicKey.hash);
32
- this._streams = new ComplexMap(PublicKey.hash);
33
- this._options = {
34
- upload: false
35
- };
36
- this._updateTask = new DeferredTask(this._ctx, async () => {
37
- try {
38
- if (this._extensionContext.initiator === false) {
39
- await this._rpc.rpc.ReplicatorService.updateFeeds({
40
- feeds: Array.from(this._feeds.values()).map((feed) => ({
41
- feedKey: feed.key,
42
- download: true,
43
- upload: this._options.upload
44
- }))
45
- });
46
- } else if (this._extensionContext.initiator === true) {
47
- await this._reevaluateFeeds();
48
- }
49
- } catch (err) {
50
- if (err instanceof RpcClosedError) {
51
- return;
52
- }
53
- throw err;
50
+ } catch (err) {
51
+ if (err instanceof RpcClosedError) {
52
+ return;
54
53
  }
55
- });
56
- }
54
+ throw err;
55
+ }
56
+ });
57
57
  get extensionInfo() {
58
58
  return {
59
59
  initiator: this._extensionContext?.initiator,
@@ -68,7 +68,7 @@ var ReplicatorExtension = class {
68
68
  options
69
69
  }, {
70
70
  F: __dxlog_file,
71
- L: 79,
71
+ L: 80,
72
72
  S: this,
73
73
  C: (f, a) => f(...a)
74
74
  });
@@ -83,7 +83,7 @@ var ReplicatorExtension = class {
83
83
  feedKey: feed.key
84
84
  }, {
85
85
  F: __dxlog_file,
86
- L: 88,
86
+ L: 89,
87
87
  S: this,
88
88
  C: (f, a) => f(...a)
89
89
  });
@@ -95,7 +95,7 @@ var ReplicatorExtension = class {
95
95
  this._extensionContext = context;
96
96
  log("open", void 0, {
97
97
  F: __dxlog_file,
98
- L: 96,
98
+ L: 97,
99
99
  S: this,
100
100
  C: (f, a) => f(...a)
101
101
  });
@@ -113,13 +113,13 @@ var ReplicatorExtension = class {
113
113
  feeds
114
114
  }, {
115
115
  F: __dxlog_file,
116
- L: 108,
116
+ L: 109,
117
117
  S: this,
118
118
  C: (f, a) => f(...a)
119
119
  });
120
120
  invariant(this._extensionContext.initiator === true, "Invalid call", {
121
121
  F: __dxlog_file,
122
- L: 109,
122
+ L: 110,
123
123
  S: this,
124
124
  A: [
125
125
  "this._extensionContext!.initiator === true",
@@ -133,13 +133,13 @@ var ReplicatorExtension = class {
133
133
  info
134
134
  }, {
135
135
  F: __dxlog_file,
136
- L: 113,
136
+ L: 114,
137
137
  S: this,
138
138
  C: (f, a) => f(...a)
139
139
  });
140
140
  invariant(this._extensionContext.initiator === false, "Invalid call", {
141
141
  F: __dxlog_file,
142
- L: 114,
142
+ L: 115,
143
143
  S: this,
144
144
  A: [
145
145
  "this._extensionContext!.initiator === false",
@@ -156,13 +156,13 @@ var ReplicatorExtension = class {
156
156
  info
157
157
  }, {
158
158
  F: __dxlog_file,
159
- L: 122,
159
+ L: 123,
160
160
  S: this,
161
161
  C: (f, a) => f(...a)
162
162
  });
163
163
  invariant(this._extensionContext.initiator === false, "Invalid call", {
164
164
  F: __dxlog_file,
165
- L: 124,
165
+ L: 125,
166
166
  S: this,
167
167
  A: [
168
168
  "this._extensionContext!.initiator === false",
@@ -186,7 +186,7 @@ var ReplicatorExtension = class {
186
186
  err
187
187
  }, {
188
188
  F: __dxlog_file,
189
- L: 141,
189
+ L: 142,
190
190
  S: this,
191
191
  C: (f, a) => f(...a)
192
192
  });
@@ -201,7 +201,7 @@ var ReplicatorExtension = class {
201
201
  err
202
202
  }, {
203
203
  F: __dxlog_file,
204
- L: 150,
204
+ L: 151,
205
205
  S: this,
206
206
  C: (f, a) => f(...a)
207
207
  });
@@ -214,7 +214,7 @@ var ReplicatorExtension = class {
214
214
  async _reevaluateFeeds() {
215
215
  log("_reevaluateFeeds", void 0, {
216
216
  F: __dxlog_file,
217
- L: 161,
217
+ L: 162,
218
218
  S: this,
219
219
  C: (f, a) => f(...a)
220
220
  });
@@ -228,7 +228,7 @@ var ReplicatorExtension = class {
228
228
  } catch (err) {
229
229
  log.catch(err, void 0, {
230
230
  F: __dxlog_file,
231
- L: 170,
231
+ L: 171,
232
232
  S: this,
233
233
  C: (f, a) => f(...a)
234
234
  });
@@ -254,13 +254,13 @@ var ReplicatorExtension = class {
254
254
  feedInfo
255
255
  }, {
256
256
  F: __dxlog_file,
257
- L: 191,
257
+ L: 192,
258
258
  S: this,
259
259
  C: (f, a) => f(...a)
260
260
  });
261
261
  invariant(this._extensionContext.initiator === true, "Invalid call", {
262
262
  F: __dxlog_file,
263
- L: 192,
263
+ L: 193,
264
264
  S: this,
265
265
  A: [
266
266
  "this._extensionContext!.initiator === true",
@@ -269,7 +269,7 @@ var ReplicatorExtension = class {
269
269
  });
270
270
  invariant(!this._streams.has(feedInfo.feedKey), `Replication already in progress for feed: ${feedInfo.feedKey}`, {
271
271
  F: __dxlog_file,
272
- L: 193,
272
+ L: 194,
273
273
  S: this,
274
274
  A: [
275
275
  "!this._streams.has(feedInfo.feedKey)",
@@ -291,7 +291,7 @@ var ReplicatorExtension = class {
291
291
  async _acceptReplication(feedInfo) {
292
292
  invariant(this._extensionContext.initiator === false, "Invalid call", {
293
293
  F: __dxlog_file,
294
- L: 208,
294
+ L: 209,
295
295
  S: this,
296
296
  A: [
297
297
  "this._extensionContext!.initiator === false",
@@ -311,13 +311,13 @@ var ReplicatorExtension = class {
311
311
  streamTag
312
312
  }, {
313
313
  F: __dxlog_file,
314
- L: 220,
314
+ L: 221,
315
315
  S: this,
316
316
  C: (f, a) => f(...a)
317
317
  });
318
318
  invariant(!this._streams.has(info.feedKey), `Replication already in progress for feed: ${info.feedKey}`, {
319
319
  F: __dxlog_file,
320
- L: 221,
320
+ L: 222,
321
321
  S: this,
322
322
  A: [
323
323
  "!this._streams.has(info.feedKey)",
@@ -344,7 +344,7 @@ var ReplicatorExtension = class {
344
344
  info
345
345
  }, {
346
346
  F: __dxlog_file,
347
- L: 257,
347
+ L: 258,
348
348
  S: this,
349
349
  C: (f, a) => f(...a)
350
350
  });
@@ -356,7 +356,7 @@ var ReplicatorExtension = class {
356
356
  info
357
357
  }, {
358
358
  F: __dxlog_file,
359
- L: 265,
359
+ L: 266,
360
360
  S: this,
361
361
  C: (f, a) => f(...a)
362
362
  });
@@ -368,7 +368,7 @@ var ReplicatorExtension = class {
368
368
  info
369
369
  }, {
370
370
  F: __dxlog_file,
371
- L: 271,
371
+ L: 272,
372
372
  S: this,
373
373
  C: (f, a) => f(...a)
374
374
  });
@@ -379,7 +379,7 @@ var ReplicatorExtension = class {
379
379
  count: replicationStreamErrors
380
380
  }, {
381
381
  F: __dxlog_file,
382
- L: 273,
382
+ L: 274,
383
383
  S: this,
384
384
  C: (f, a) => f(...a)
385
385
  });