@dxos/teleport-extension-replicator 0.10.0 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { DeferredTask, asyncTimeout, synchronized } from "@dxos/async";
|
|
2
|
+
import { Context } from "@dxos/context";
|
|
3
|
+
import { failUndefined } from "@dxos/debug";
|
|
4
|
+
import { invariant } from "@dxos/invariant";
|
|
5
|
+
import { PublicKey } from "@dxos/keys";
|
|
6
|
+
import { log, logInfo } from "@dxos/log";
|
|
7
|
+
import { RpcClosedError, TimeoutError } from "@dxos/protocols";
|
|
8
|
+
import { schema } from "@dxos/protocols/proto";
|
|
9
|
+
import { createProtoRpcPeer } from "@dxos/rpc";
|
|
10
|
+
import { ComplexMap } from "@dxos/util";
|
|
11
|
+
//#region \0@oxc-project+runtime@0.139.0/helpers/esm/decorate.js
|
|
12
|
+
function __decorate(decorators, target, key, desc) {
|
|
13
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
14
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
15
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
16
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/replicator-extension.ts
|
|
20
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/core/mesh/teleport-extension-replicator/src/replicator-extension.ts";
|
|
21
|
+
/**
|
|
22
|
+
* Manages replication between a set of feeds for a single teleport session.
|
|
23
|
+
*/
|
|
24
|
+
var ReplicatorExtension = class {
|
|
25
|
+
_ctx = new Context({ onError: (err) => {
|
|
26
|
+
this._extensionContext?.close(err);
|
|
27
|
+
} }, {
|
|
28
|
+
"~LogMeta": "~LogMeta",
|
|
29
|
+
F: __dxlog_file,
|
|
30
|
+
L: 30
|
|
31
|
+
});
|
|
32
|
+
_feeds = new ComplexMap(PublicKey.hash);
|
|
33
|
+
_streams = new ComplexMap(PublicKey.hash);
|
|
34
|
+
_rpc;
|
|
35
|
+
_extensionContext;
|
|
36
|
+
_options = { upload: false };
|
|
37
|
+
_updateTask = new DeferredTask(this._ctx, async () => {
|
|
38
|
+
try {
|
|
39
|
+
if (this._extensionContext.initiator === false) await this._rpc.rpc.ReplicatorService.updateFeeds({ feeds: Array.from(this._feeds.values()).map((feed) => ({
|
|
40
|
+
feedKey: feed.key,
|
|
41
|
+
download: true,
|
|
42
|
+
upload: this._options.upload
|
|
43
|
+
})) });
|
|
44
|
+
else if (this._extensionContext.initiator === true) await this._reevaluateFeeds();
|
|
45
|
+
} catch (err) {
|
|
46
|
+
if (err instanceof RpcClosedError) return;
|
|
47
|
+
throw err;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
get extensionInfo() {
|
|
51
|
+
return {
|
|
52
|
+
initiator: this._extensionContext?.initiator,
|
|
53
|
+
localPeerId: this._extensionContext?.localPeerId,
|
|
54
|
+
remotePeerId: this._extensionContext?.remotePeerId,
|
|
55
|
+
feeds: Array.from(this._feeds.keys())
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
setOptions(options) {
|
|
59
|
+
this._options = options;
|
|
60
|
+
log("setOptions", { options }, {
|
|
61
|
+
"~LogMeta": "~LogMeta",
|
|
62
|
+
F: __dxlog_file,
|
|
63
|
+
L: 79,
|
|
64
|
+
S: this
|
|
65
|
+
});
|
|
66
|
+
if (this._extensionContext) this._updateTask.schedule();
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
addFeed(feed) {
|
|
70
|
+
this._feeds.set(feed.key, feed);
|
|
71
|
+
log("addFeed", { feedKey: feed.key }, {
|
|
72
|
+
"~LogMeta": "~LogMeta",
|
|
73
|
+
F: __dxlog_file,
|
|
74
|
+
L: 88,
|
|
75
|
+
S: this
|
|
76
|
+
});
|
|
77
|
+
if (this._extensionContext) this._updateTask.schedule();
|
|
78
|
+
}
|
|
79
|
+
async onOpen(context) {
|
|
80
|
+
this._extensionContext = context;
|
|
81
|
+
log("open", void 0, {
|
|
82
|
+
"~LogMeta": "~LogMeta",
|
|
83
|
+
F: __dxlog_file,
|
|
84
|
+
L: 96,
|
|
85
|
+
S: this
|
|
86
|
+
});
|
|
87
|
+
this._rpc = createProtoRpcPeer({
|
|
88
|
+
requested: { ReplicatorService: schema.getService("dxos.mesh.teleport.replicator.ReplicatorService") },
|
|
89
|
+
exposed: { ReplicatorService: schema.getService("dxos.mesh.teleport.replicator.ReplicatorService") },
|
|
90
|
+
handlers: { ReplicatorService: {
|
|
91
|
+
updateFeeds: async ({ feeds }) => {
|
|
92
|
+
log("received feed info", { feeds }, {
|
|
93
|
+
"~LogMeta": "~LogMeta",
|
|
94
|
+
F: __dxlog_file,
|
|
95
|
+
L: 108,
|
|
96
|
+
S: this
|
|
97
|
+
});
|
|
98
|
+
invariant(this._extensionContext.initiator === true, "Invalid call", {
|
|
99
|
+
"~LogMeta": "~LogMeta",
|
|
100
|
+
F: __dxlog_file,
|
|
101
|
+
L: 109,
|
|
102
|
+
S: this,
|
|
103
|
+
A: ["this._extensionContext!.initiator === true", "'Invalid call'"]
|
|
104
|
+
});
|
|
105
|
+
this._updateTask.schedule();
|
|
106
|
+
},
|
|
107
|
+
startReplication: async ({ info }) => {
|
|
108
|
+
log("starting replication...", { info }, {
|
|
109
|
+
"~LogMeta": "~LogMeta",
|
|
110
|
+
F: __dxlog_file,
|
|
111
|
+
L: 113,
|
|
112
|
+
S: this
|
|
113
|
+
});
|
|
114
|
+
invariant(this._extensionContext.initiator === false, "Invalid call", {
|
|
115
|
+
"~LogMeta": "~LogMeta",
|
|
116
|
+
F: __dxlog_file,
|
|
117
|
+
L: 114,
|
|
118
|
+
S: this,
|
|
119
|
+
A: ["this._extensionContext!.initiator === false", "'Invalid call'"]
|
|
120
|
+
});
|
|
121
|
+
return { streamTag: await this._acceptReplication(info) };
|
|
122
|
+
},
|
|
123
|
+
stopReplication: async ({ info }) => {
|
|
124
|
+
log("stopping replication...", { info }, {
|
|
125
|
+
"~LogMeta": "~LogMeta",
|
|
126
|
+
F: __dxlog_file,
|
|
127
|
+
L: 122,
|
|
128
|
+
S: this
|
|
129
|
+
});
|
|
130
|
+
invariant(this._extensionContext.initiator === false, "Invalid call", {
|
|
131
|
+
"~LogMeta": "~LogMeta",
|
|
132
|
+
F: __dxlog_file,
|
|
133
|
+
L: 124,
|
|
134
|
+
S: this,
|
|
135
|
+
A: ["this._extensionContext!.initiator === false", "'Invalid call'"]
|
|
136
|
+
});
|
|
137
|
+
await this._stopReplication(info.feedKey);
|
|
138
|
+
}
|
|
139
|
+
} },
|
|
140
|
+
port: await context.createPort("rpc", { contentType: "application/x-protobuf; messageType=\"dxos.rpc.Message\"" }),
|
|
141
|
+
timeout: 1e4
|
|
142
|
+
});
|
|
143
|
+
await this._rpc.open();
|
|
144
|
+
this._updateTask.schedule();
|
|
145
|
+
}
|
|
146
|
+
async onClose(err) {
|
|
147
|
+
log("close", { err }, {
|
|
148
|
+
"~LogMeta": "~LogMeta",
|
|
149
|
+
F: __dxlog_file,
|
|
150
|
+
L: 141,
|
|
151
|
+
S: this
|
|
152
|
+
});
|
|
153
|
+
await this._ctx.dispose();
|
|
154
|
+
await this._rpc?.close();
|
|
155
|
+
for (const feedKey of this._streams.keys()) await this._stopReplication(feedKey);
|
|
156
|
+
}
|
|
157
|
+
async onAbort(err) {
|
|
158
|
+
log("abort", { err }, {
|
|
159
|
+
"~LogMeta": "~LogMeta",
|
|
160
|
+
F: __dxlog_file,
|
|
161
|
+
L: 150,
|
|
162
|
+
S: this
|
|
163
|
+
});
|
|
164
|
+
await this._ctx.dispose();
|
|
165
|
+
await this._rpc?.abort();
|
|
166
|
+
for (const feedKey of this._streams.keys()) await this._stopReplication(feedKey);
|
|
167
|
+
}
|
|
168
|
+
async _reevaluateFeeds() {
|
|
169
|
+
log("_reevaluateFeeds", void 0, {
|
|
170
|
+
"~LogMeta": "~LogMeta",
|
|
171
|
+
F: __dxlog_file,
|
|
172
|
+
L: 161,
|
|
173
|
+
S: this
|
|
174
|
+
});
|
|
175
|
+
for (const feedKey of this._feeds.keys()) {
|
|
176
|
+
if (this._ctx.disposed) return;
|
|
177
|
+
if (this._streams.has(feedKey) && this._options.upload !== this._streams.get(feedKey)?.info.upload) try {
|
|
178
|
+
await asyncTimeout(this._stopReplication(feedKey), 1e3);
|
|
179
|
+
} catch (err) {
|
|
180
|
+
log.catch(err, void 0, {
|
|
181
|
+
"~LogMeta": "~LogMeta",
|
|
182
|
+
F: __dxlog_file,
|
|
183
|
+
L: 170,
|
|
184
|
+
S: this
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
if (this._ctx.disposed) return;
|
|
188
|
+
if (!this._streams.has(feedKey)) await this._initiateReplication({
|
|
189
|
+
feedKey,
|
|
190
|
+
download: true,
|
|
191
|
+
upload: this._options.upload
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Try to initiate feed replication.
|
|
197
|
+
*/
|
|
198
|
+
async _initiateReplication(feedInfo) {
|
|
199
|
+
log("initiating replication", { feedInfo }, {
|
|
200
|
+
"~LogMeta": "~LogMeta",
|
|
201
|
+
F: __dxlog_file,
|
|
202
|
+
L: 191,
|
|
203
|
+
S: this
|
|
204
|
+
});
|
|
205
|
+
invariant(this._extensionContext.initiator === true, "Invalid call", {
|
|
206
|
+
"~LogMeta": "~LogMeta",
|
|
207
|
+
F: __dxlog_file,
|
|
208
|
+
L: 192,
|
|
209
|
+
S: this,
|
|
210
|
+
A: ["this._extensionContext!.initiator === true", "'Invalid call'"]
|
|
211
|
+
});
|
|
212
|
+
invariant(!this._streams.has(feedInfo.feedKey), `Replication already in progress for feed: ${feedInfo.feedKey}`, {
|
|
213
|
+
"~LogMeta": "~LogMeta",
|
|
214
|
+
F: __dxlog_file,
|
|
215
|
+
L: 193,
|
|
216
|
+
S: this,
|
|
217
|
+
A: ["!this._streams.has(feedInfo.feedKey)", "`Replication already in progress for feed: ${feedInfo.feedKey}`"]
|
|
218
|
+
});
|
|
219
|
+
const { streamTag } = await this._rpc.rpc.ReplicatorService.startReplication({ info: feedInfo });
|
|
220
|
+
if (!streamTag) return;
|
|
221
|
+
await this._replicateFeed(feedInfo, streamTag);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Respond to a remote request to replicate a feed.
|
|
225
|
+
* @returns A stream tag for the replication stream or `undefined` if we don't want to replicate.
|
|
226
|
+
*/
|
|
227
|
+
async _acceptReplication(feedInfo) {
|
|
228
|
+
invariant(this._extensionContext.initiator === false, "Invalid call", {
|
|
229
|
+
"~LogMeta": "~LogMeta",
|
|
230
|
+
F: __dxlog_file,
|
|
231
|
+
L: 208,
|
|
232
|
+
S: this,
|
|
233
|
+
A: ["this._extensionContext!.initiator === false", "'Invalid call'"]
|
|
234
|
+
});
|
|
235
|
+
if (!this._feeds.has(feedInfo.feedKey) || this._streams.has(feedInfo.feedKey)) return;
|
|
236
|
+
const tag = `feed-${feedInfo.feedKey.toHex()}-${PublicKey.random().toHex().slice(0, 8)}`;
|
|
237
|
+
await this._replicateFeed(feedInfo, tag);
|
|
238
|
+
return tag;
|
|
239
|
+
}
|
|
240
|
+
async _replicateFeed(info, streamTag) {
|
|
241
|
+
log("replicate", {
|
|
242
|
+
info,
|
|
243
|
+
streamTag
|
|
244
|
+
}, {
|
|
245
|
+
"~LogMeta": "~LogMeta",
|
|
246
|
+
F: __dxlog_file,
|
|
247
|
+
L: 220,
|
|
248
|
+
S: this
|
|
249
|
+
});
|
|
250
|
+
invariant(!this._streams.has(info.feedKey), `Replication already in progress for feed: ${info.feedKey}`, {
|
|
251
|
+
"~LogMeta": "~LogMeta",
|
|
252
|
+
F: __dxlog_file,
|
|
253
|
+
L: 221,
|
|
254
|
+
S: this,
|
|
255
|
+
A: ["!this._streams.has(info.feedKey)", "`Replication already in progress for feed: ${info.feedKey}`"]
|
|
256
|
+
});
|
|
257
|
+
const feed = this._feeds.get(info.feedKey) ?? failUndefined();
|
|
258
|
+
const networkStream = await this._extensionContext.createStream(streamTag, { contentType: "application/x-hypercore" });
|
|
259
|
+
let replicationStreamErrors = 0;
|
|
260
|
+
const replicationStream = feed.replicate(true, {
|
|
261
|
+
live: true,
|
|
262
|
+
upload: info.upload,
|
|
263
|
+
download: info.download,
|
|
264
|
+
noise: false,
|
|
265
|
+
encrypted: false,
|
|
266
|
+
maxRequests: 1024
|
|
267
|
+
});
|
|
268
|
+
replicationStream.on("error", (err) => {
|
|
269
|
+
if (err instanceof TimeoutError) {
|
|
270
|
+
log.info("replication stream timeout", {
|
|
271
|
+
err,
|
|
272
|
+
info
|
|
273
|
+
}, {
|
|
274
|
+
"~LogMeta": "~LogMeta",
|
|
275
|
+
F: __dxlog_file,
|
|
276
|
+
L: 257,
|
|
277
|
+
S: this
|
|
278
|
+
});
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if (err?.message === "Writable stream closed prematurely" || err?.message === "Cannot call write after a stream was destroyed") {
|
|
282
|
+
log("replication stream closed", {
|
|
283
|
+
err,
|
|
284
|
+
info
|
|
285
|
+
}, {
|
|
286
|
+
"~LogMeta": "~LogMeta",
|
|
287
|
+
F: __dxlog_file,
|
|
288
|
+
L: 265,
|
|
289
|
+
S: this
|
|
290
|
+
});
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (replicationStreamErrors === 0) log.info("replication stream error", {
|
|
294
|
+
err,
|
|
295
|
+
info
|
|
296
|
+
}, {
|
|
297
|
+
"~LogMeta": "~LogMeta",
|
|
298
|
+
F: __dxlog_file,
|
|
299
|
+
L: 271,
|
|
300
|
+
S: this
|
|
301
|
+
});
|
|
302
|
+
else log.info("replication stream error", {
|
|
303
|
+
err,
|
|
304
|
+
feedKey: info.feedKey,
|
|
305
|
+
count: replicationStreamErrors
|
|
306
|
+
}, {
|
|
307
|
+
"~LogMeta": "~LogMeta",
|
|
308
|
+
F: __dxlog_file,
|
|
309
|
+
L: 273,
|
|
310
|
+
S: this
|
|
311
|
+
});
|
|
312
|
+
replicationStreamErrors++;
|
|
313
|
+
});
|
|
314
|
+
this._streams.set(info.feedKey, {
|
|
315
|
+
streamTag,
|
|
316
|
+
networkStream,
|
|
317
|
+
replicationStream,
|
|
318
|
+
info
|
|
319
|
+
});
|
|
320
|
+
networkStream.pipe(replicationStream).pipe(networkStream);
|
|
321
|
+
}
|
|
322
|
+
async _stopReplication(feedKey) {
|
|
323
|
+
const stream = this._streams.get(feedKey);
|
|
324
|
+
if (!stream) return;
|
|
325
|
+
stream.networkStream.destroy();
|
|
326
|
+
this._streams.delete(feedKey);
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
__decorate([logInfo], ReplicatorExtension.prototype, "extensionInfo", null);
|
|
330
|
+
__decorate([synchronized], ReplicatorExtension.prototype, "_reevaluateFeeds", null);
|
|
331
|
+
__decorate([synchronized], ReplicatorExtension.prototype, "_acceptReplication", null);
|
|
332
|
+
//#endregion
|
|
333
|
+
export { ReplicatorExtension };
|
|
334
|
+
|
|
335
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/replicator-extension.ts"],"sourcesContent":["//\n// Copyright 2022 DXOS.org\n//\n\nimport type { ProtocolStream } from 'hypercore-protocol';\nimport { type Duplex } from 'node:stream';\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA4BA,IAAa,sBAAb,MAA8D;CAC5D,OAAwB,IAAI,QAAQ,EAClC,UAAU,QAAQ;EAChB,KAAK,mBAAmB,MAAM,GAAG;CACnC,EACF,GAAA;EAAA,YAAA;EAAA,GAAA;EAAA,GAAA;CAAA,CAAC;CAED,SAA0B,IAAI,WAAwC,UAAU,IAAI;CACpF,WAA4B,IAAI,WAAoC,UAAU,IAAI;CAElF;CACA;CAEA,WAAuC,EACrC,QAAQ,MACV;CAEA,cAA+B,IAAI,aAAa,KAAK,MAAM,YAAY;EACrE,IAAI;GACF,IAAI,KAAK,kBAAmB,cAAc,OACxC,MAAM,KAAK,KAAM,IAAI,kBAAkB,YAAY,EACjD,OAAO,MAAM,KAAK,KAAK,OAAO,OAAO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