nerdbank-streams 2.10.37-alpha → 2.10.69
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/js/AcceptanceParameters.d.ts +4 -4
- package/js/AcceptanceParameters.js +2 -2
- package/js/Channel.d.ts +71 -70
- package/js/Channel.js +219 -219
- package/js/Channel.js.map +1 -1
- package/js/ChannelOptions.d.ts +4 -4
- package/js/ChannelOptions.js +2 -2
- package/js/ControlCode.d.ts +30 -30
- package/js/ControlCode.js +34 -34
- package/js/ControlCode.js.map +1 -1
- package/js/Deferred.d.ts +39 -39
- package/js/Deferred.js +67 -67
- package/js/FrameHeader.d.ts +20 -20
- package/js/FrameHeader.js +39 -39
- package/js/FullDuplexStream.d.ts +10 -10
- package/js/FullDuplexStream.js +31 -31
- package/js/IChannelOfferEventArgs.d.ts +17 -17
- package/js/IChannelOfferEventArgs.js +2 -2
- package/js/IDisposableObservable.d.ts +4 -4
- package/js/IDisposableObservable.js +2 -2
- package/js/MultiplexingStream.d.ts +220 -218
- package/js/MultiplexingStream.js +624 -610
- package/js/MultiplexingStream.js.map +1 -1
- package/js/MultiplexingStreamFormatters.d.ts +90 -89
- package/js/MultiplexingStreamFormatters.js +302 -302
- package/js/MultiplexingStreamOptions.d.ts +18 -18
- package/js/MultiplexingStreamOptions.js +2 -2
- package/js/OfferParameters.d.ts +6 -6
- package/js/OfferParameters.js +2 -2
- package/js/QualifiedChannelId.d.ts +25 -25
- package/js/QualifiedChannelId.js +38 -38
- package/js/QualifiedChannelId.js.map +1 -1
- package/js/Utilities.d.ts +12 -11
- package/js/Utilities.js +154 -154
- package/js/index.d.ts +8 -8
- package/js/index.js +15 -15
- package/package.json +18 -18
- package/src/MultiplexingStream.ts +18 -1
package/js/MultiplexingStream.js
CHANGED
|
@@ -1,611 +1,625 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/* TODO:
|
|
3
|
-
* Tracing
|
|
4
|
-
* Auto-terminate channels when both ends have finished writing (AutoCloseOnPipesClosureAsync)
|
|
5
|
-
*/
|
|
6
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.MultiplexingStreamClass = exports.MultiplexingStream = void 0;
|
|
8
|
-
const await_semaphore_1 = require("await-semaphore");
|
|
9
|
-
const cancellationtoken_1 = require("cancellationtoken");
|
|
10
|
-
const events_1 = require("events");
|
|
11
|
-
const Channel_1 = require("./Channel");
|
|
12
|
-
const ControlCode_1 = require("./ControlCode");
|
|
13
|
-
const Deferred_1 = require("./Deferred");
|
|
14
|
-
const FrameHeader_1 = require("./FrameHeader");
|
|
15
|
-
const MultiplexingStreamFormatters_1 = require("./MultiplexingStreamFormatters");
|
|
16
|
-
require("./MultiplexingStreamOptions");
|
|
17
|
-
const QualifiedChannelId_1 = require("./QualifiedChannelId");
|
|
18
|
-
const Utilities_1 = require("./Utilities");
|
|
19
|
-
const caught = require("caught");
|
|
20
|
-
class MultiplexingStream {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
*
|
|
78
|
-
* @
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Creates an anonymous channel that may be accepted by <see cref="AcceptChannel(int, ChannelOptions)"/>.
|
|
133
|
-
* Its existance must be communicated by other means (typically another, existing channel) to encourage acceptance.
|
|
134
|
-
* @param options A set of options that describe local treatment of this channel.
|
|
135
|
-
* @returns The anonymous channel.
|
|
136
|
-
* @description Note that while the channel is created immediately, any local write to that channel will be
|
|
137
|
-
* buffered locally until the remote party accepts the channel.
|
|
138
|
-
*/
|
|
139
|
-
createChannel(options) {
|
|
140
|
-
var _a;
|
|
141
|
-
const offerParameters = {
|
|
142
|
-
name: '',
|
|
143
|
-
remoteWindowSize: (_a = options === null || options === void 0 ? void 0 : options.channelReceivingWindowSize) !== null && _a !== void 0 ? _a : this.defaultChannelReceivingWindowSize,
|
|
144
|
-
};
|
|
145
|
-
const payload = this.formatter.serializeOfferParameters(offerParameters);
|
|
146
|
-
const channel = new Channel_1.ChannelClass(this, { id: this.getUnusedChannelId(), source: QualifiedChannelId_1.ChannelSource.Local }, offerParameters);
|
|
147
|
-
this.setOpenChannel(channel);
|
|
148
|
-
this.rejectOnFailure(this.sendFrameAsync(new FrameHeader_1.FrameHeader(ControlCode_1.ControlCode.offer, channel.qualifiedId), payload, this.disposalToken));
|
|
149
|
-
return channel;
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Accepts a channel with a specific ID.
|
|
153
|
-
* @param id The id of the channel to accept.
|
|
154
|
-
* @param options A set of options that describe local treatment of this channel.
|
|
155
|
-
* @description This method can be used to accept anonymous channels created with <see cref="CreateChannel"/>.
|
|
156
|
-
* Unlike <see cref="AcceptChannelAsync(string, ChannelOptions, CancellationToken)"/> which will await
|
|
157
|
-
* for a channel offer if a matching one has not been made yet, this method only accepts an offer
|
|
158
|
-
* for a channel that has already been made.
|
|
159
|
-
*/
|
|
160
|
-
acceptChannel(id, options) {
|
|
161
|
-
const channel = this.remotelyOfferedOpenChannels[id] || this.seededOpenChannels[id];
|
|
162
|
-
if (!channel) {
|
|
163
|
-
throw new Error('No channel with ID ' + id);
|
|
164
|
-
}
|
|
165
|
-
this.removeChannelFromOfferedQueue(channel);
|
|
166
|
-
this.acceptChannelOrThrow(channel, options);
|
|
167
|
-
return channel;
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Rejects an offer for the channel with a specified ID.
|
|
171
|
-
* @param id The ID of the channel whose offer should be rejected.
|
|
172
|
-
*/
|
|
173
|
-
rejectChannel(id) {
|
|
174
|
-
const channel = this.remotelyOfferedOpenChannels[id];
|
|
175
|
-
if (channel) {
|
|
176
|
-
(0, Utilities_1.removeFromQueue)(channel, this.channelsOfferedByThemByName[channel.name]);
|
|
177
|
-
}
|
|
178
|
-
else {
|
|
179
|
-
throw new Error('No channel with that ID found.');
|
|
180
|
-
}
|
|
181
|
-
// Rejecting a channel rejects a couple promises that we don't want the caller to have to observe
|
|
182
|
-
// separately since they are explicitly stating they want to take this action now.
|
|
183
|
-
caught(channel.acceptance);
|
|
184
|
-
caught(channel.completion);
|
|
185
|
-
channel.dispose();
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Offers a new, named channel to the remote party so they may accept it with
|
|
189
|
-
* {@link acceptChannelAsync}.
|
|
190
|
-
* @param name A name for the channel, which must be accepted on the remote end to complete creation.
|
|
191
|
-
* It need not be unique, and may be empty but must not be null.
|
|
192
|
-
* Any characters are allowed, and max length is determined by the maximum frame payload (based on UTF-8 encoding).
|
|
193
|
-
* @param options A set of options that describe local treatment of this channel.
|
|
194
|
-
* @param cancellationToken A cancellation token. Do NOT let this be a long-lived token
|
|
195
|
-
* or a memory leak will result since we add continuations to its promise.
|
|
196
|
-
* @returns A task that completes with the `Channel` if the offer is accepted on the remote end
|
|
197
|
-
* or faults with `MultiplexingProtocolException` if the remote end rejects the channel.
|
|
198
|
-
*/
|
|
199
|
-
async offerChannelAsync(name, options, cancellationToken = cancellationtoken_1.default.CONTINUE) {
|
|
200
|
-
var _a;
|
|
201
|
-
if (name === null || name === undefined) {
|
|
202
|
-
throw new Error('Name must be specified (but may be empty).');
|
|
203
|
-
}
|
|
204
|
-
cancellationToken.throwIfCancelled();
|
|
205
|
-
(0, Utilities_1.throwIfDisposed)(this);
|
|
206
|
-
const offerParameters = {
|
|
207
|
-
name,
|
|
208
|
-
remoteWindowSize: (_a = options === null || options === void 0 ? void 0 : options.channelReceivingWindowSize) !== null && _a !== void 0 ? _a : this.defaultChannelReceivingWindowSize,
|
|
209
|
-
};
|
|
210
|
-
const payload = this.formatter.serializeOfferParameters(offerParameters);
|
|
211
|
-
const channel = new Channel_1.ChannelClass(this, { id: this.getUnusedChannelId(), source: QualifiedChannelId_1.ChannelSource.Local }, offerParameters);
|
|
212
|
-
this.setOpenChannel(channel);
|
|
213
|
-
const header = new FrameHeader_1.FrameHeader(ControlCode_1.ControlCode.offer, channel.qualifiedId);
|
|
214
|
-
const unsubscribeFromCT = cancellationToken.onCancelled(reason => this.offerChannelCanceled(channel, reason));
|
|
215
|
-
try {
|
|
216
|
-
// We *will* recognize rejection of this promise. But just in case sendFrameAsync completes synchronously,
|
|
217
|
-
// we want to signify that we *will* catch it first to avoid node.js emitting warnings or crashing.
|
|
218
|
-
caught(channel.acceptance);
|
|
219
|
-
await this.sendFrameAsync(header, payload, cancellationToken);
|
|
220
|
-
await channel.acceptance;
|
|
221
|
-
return channel;
|
|
222
|
-
}
|
|
223
|
-
finally {
|
|
224
|
-
unsubscribeFromCT();
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* Accepts a channel that the remote end has attempted or may attempt to create.
|
|
229
|
-
* @param name The name of the channel to accept.
|
|
230
|
-
* @param options A set of options that describe local treatment of this channel.
|
|
231
|
-
* @param cancellationToken A token to indicate lost interest in accepting the channel.
|
|
232
|
-
* Do NOT let this be a long-lived token
|
|
233
|
-
* or a memory leak will result since we add continuations to its promise.
|
|
234
|
-
* @returns The `Channel`, after its offer has been received from the remote party and accepted.
|
|
235
|
-
* @description If multiple offers exist with the specified `name`, the first one received will be accepted.
|
|
236
|
-
*/
|
|
237
|
-
async acceptChannelAsync(name, options, cancellationToken = cancellationtoken_1.default.CONTINUE) {
|
|
238
|
-
if (name === null || name === undefined) {
|
|
239
|
-
throw new Error('Name must be specified (but may be empty).');
|
|
240
|
-
}
|
|
241
|
-
cancellationToken.throwIfCancelled();
|
|
242
|
-
(0, Utilities_1.throwIfDisposed)(this);
|
|
243
|
-
let channel;
|
|
244
|
-
let pendingAcceptChannel;
|
|
245
|
-
const channelsOfferedByThem = this.channelsOfferedByThemByName[name];
|
|
246
|
-
if (channelsOfferedByThem) {
|
|
247
|
-
while (channel === undefined && channelsOfferedByThem.length > 0) {
|
|
248
|
-
channel = channelsOfferedByThem.shift();
|
|
249
|
-
if (channel.isAccepted || channel.isRejectedOrCanceled) {
|
|
250
|
-
channel = undefined;
|
|
251
|
-
continue;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
if (channel === undefined) {
|
|
256
|
-
let acceptingChannels = this.acceptingChannels[name];
|
|
257
|
-
if (!acceptingChannels) {
|
|
258
|
-
this.acceptingChannels[name] = acceptingChannels = [];
|
|
259
|
-
}
|
|
260
|
-
pendingAcceptChannel = new Deferred_1.Deferred(options);
|
|
261
|
-
acceptingChannels.push(pendingAcceptChannel);
|
|
262
|
-
}
|
|
263
|
-
if (channel !== undefined) {
|
|
264
|
-
this.acceptChannelOrThrow(channel, options);
|
|
265
|
-
return channel;
|
|
266
|
-
}
|
|
267
|
-
else {
|
|
268
|
-
const unsubscribeFromCT = cancellationToken.onCancelled(reason => this.acceptChannelCanceled(pendingAcceptChannel, name, reason));
|
|
269
|
-
try {
|
|
270
|
-
return await pendingAcceptChannel.promise;
|
|
271
|
-
}
|
|
272
|
-
finally {
|
|
273
|
-
unsubscribeFromCT();
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Disposes the stream.
|
|
279
|
-
*/
|
|
280
|
-
dispose() {
|
|
281
|
-
this.
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
channel.
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
this.
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
break;
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
if (
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
/* TODO:
|
|
3
|
+
* Tracing
|
|
4
|
+
* Auto-terminate channels when both ends have finished writing (AutoCloseOnPipesClosureAsync)
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.MultiplexingStreamClass = exports.MultiplexingStream = void 0;
|
|
8
|
+
const await_semaphore_1 = require("await-semaphore");
|
|
9
|
+
const cancellationtoken_1 = require("cancellationtoken");
|
|
10
|
+
const events_1 = require("events");
|
|
11
|
+
const Channel_1 = require("./Channel");
|
|
12
|
+
const ControlCode_1 = require("./ControlCode");
|
|
13
|
+
const Deferred_1 = require("./Deferred");
|
|
14
|
+
const FrameHeader_1 = require("./FrameHeader");
|
|
15
|
+
const MultiplexingStreamFormatters_1 = require("./MultiplexingStreamFormatters");
|
|
16
|
+
require("./MultiplexingStreamOptions");
|
|
17
|
+
const QualifiedChannelId_1 = require("./QualifiedChannelId");
|
|
18
|
+
const Utilities_1 = require("./Utilities");
|
|
19
|
+
const caught = require("caught");
|
|
20
|
+
class MultiplexingStream {
|
|
21
|
+
get disposalToken() {
|
|
22
|
+
return this.disposalTokenSource.token;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Gets a promise that is resolved or rejected based on how this stream is disposed or fails.
|
|
26
|
+
*/
|
|
27
|
+
get completion() {
|
|
28
|
+
return this._completionSource.promise;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Gets a value indicating whether this instance has been disposed.
|
|
32
|
+
*/
|
|
33
|
+
get isDisposed() {
|
|
34
|
+
return this.disposalTokenSource.token.isCancelled;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Initializes a new instance of the `MultiplexingStream` class.
|
|
38
|
+
* @param stream The duplex stream to read and write to.
|
|
39
|
+
* Use `FullDuplexStream.Splice` if you have distinct input/output streams.
|
|
40
|
+
* @param options Options to customize the behavior of the stream.
|
|
41
|
+
* @param cancellationToken A token whose cancellation aborts the handshake with the remote end.
|
|
42
|
+
* @returns The multiplexing stream, once the handshake is complete.
|
|
43
|
+
*/
|
|
44
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
45
|
+
static async CreateAsync(stream, options, cancellationToken = cancellationtoken_1.default.CONTINUE) {
|
|
46
|
+
var _a;
|
|
47
|
+
if (!stream) {
|
|
48
|
+
throw new Error('stream must be specified.');
|
|
49
|
+
}
|
|
50
|
+
options = options || {};
|
|
51
|
+
(_a = options.protocolMajorVersion) !== null && _a !== void 0 ? _a : (options.protocolMajorVersion = 1);
|
|
52
|
+
options.defaultChannelReceivingWindowSize = options.defaultChannelReceivingWindowSize || MultiplexingStream.recommendedDefaultChannelReceivingWindowSize;
|
|
53
|
+
if (options.protocolMajorVersion < 3 && options.seededChannels && options.seededChannels.length > 0) {
|
|
54
|
+
throw new Error('Seeded channels require v3 of the protocol at least.');
|
|
55
|
+
}
|
|
56
|
+
// Send the protocol magic number, and a random 16-byte number to establish even/odd assignments.
|
|
57
|
+
const formatter = options.protocolMajorVersion === 1
|
|
58
|
+
? new MultiplexingStreamFormatters_1.MultiplexingStreamV1Formatter(stream)
|
|
59
|
+
: options.protocolMajorVersion === 2
|
|
60
|
+
? new MultiplexingStreamFormatters_1.MultiplexingStreamV2Formatter(stream)
|
|
61
|
+
: options.protocolMajorVersion === 3
|
|
62
|
+
? new MultiplexingStreamFormatters_1.MultiplexingStreamV3Formatter(stream)
|
|
63
|
+
: undefined;
|
|
64
|
+
if (!formatter) {
|
|
65
|
+
throw new Error(`Protocol major version ${options.protocolMajorVersion} is not supported.`);
|
|
66
|
+
}
|
|
67
|
+
const writeHandshakeData = await formatter.writeHandshakeAsync();
|
|
68
|
+
const handshakeResult = await formatter.readHandshakeAsync(writeHandshakeData, cancellationToken);
|
|
69
|
+
formatter.isOdd = handshakeResult.isOdd;
|
|
70
|
+
return new MultiplexingStreamClass(formatter, handshakeResult.isOdd, options);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Initializes a new instance of the `MultiplexingStream` class
|
|
74
|
+
* with protocolMajorVersion set to 3.
|
|
75
|
+
* @param stream The duplex stream to read and write to.
|
|
76
|
+
* Use `FullDuplexStream.Splice` if you have distinct input/output streams.
|
|
77
|
+
* @param options Options to customize the behavior of the stream.
|
|
78
|
+
* @returns The multiplexing stream.
|
|
79
|
+
*/
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
81
|
+
static Create(stream, options) {
|
|
82
|
+
var _a;
|
|
83
|
+
options !== null && options !== void 0 ? options : (options = { protocolMajorVersion: 3 });
|
|
84
|
+
(_a = options.protocolMajorVersion) !== null && _a !== void 0 ? _a : (options.protocolMajorVersion = 3);
|
|
85
|
+
const formatter = options.protocolMajorVersion === 3 ? new MultiplexingStreamFormatters_1.MultiplexingStreamV3Formatter(stream) : undefined;
|
|
86
|
+
if (!formatter) {
|
|
87
|
+
throw new Error(`Protocol major version ${options.protocolMajorVersion} is not supported. Try CreateAsync instead.`);
|
|
88
|
+
}
|
|
89
|
+
return new MultiplexingStreamClass(formatter, undefined, options);
|
|
90
|
+
}
|
|
91
|
+
constructor(formatter, isOdd, options) {
|
|
92
|
+
var _a, _b, _c;
|
|
93
|
+
this.formatter = formatter;
|
|
94
|
+
this.isOdd = isOdd;
|
|
95
|
+
this._completionSource = new Deferred_1.Deferred();
|
|
96
|
+
/**
|
|
97
|
+
* A dictionary of channels that were seeded, keyed by their ID.
|
|
98
|
+
*/
|
|
99
|
+
this.seededOpenChannels = {};
|
|
100
|
+
/**
|
|
101
|
+
* A dictionary of channels that were offered locally, keyed by their ID.
|
|
102
|
+
*/
|
|
103
|
+
this.locallyOfferedOpenChannels = {};
|
|
104
|
+
/**
|
|
105
|
+
* A dictionary of channels that were offered remotely, keyed by their ID.
|
|
106
|
+
*/
|
|
107
|
+
this.remotelyOfferedOpenChannels = {};
|
|
108
|
+
/**
|
|
109
|
+
* A map of channel names to queues of channels waiting for local acceptance.
|
|
110
|
+
*/
|
|
111
|
+
this.channelsOfferedByThemByName = {};
|
|
112
|
+
/**
|
|
113
|
+
* A map of channel names to queues of Deferred<Channel> from waiting accepters.
|
|
114
|
+
*/
|
|
115
|
+
this.acceptingChannels = {};
|
|
116
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
117
|
+
this.disposalTokenSource = cancellationtoken_1.default.create();
|
|
118
|
+
this.defaultChannelReceivingWindowSize = (_a = options.defaultChannelReceivingWindowSize) !== null && _a !== void 0 ? _a : MultiplexingStream.recommendedDefaultChannelReceivingWindowSize;
|
|
119
|
+
this.protocolMajorVersion = (_b = options.protocolMajorVersion) !== null && _b !== void 0 ? _b : 1;
|
|
120
|
+
if (options.seededChannels) {
|
|
121
|
+
for (let i = 0; i < options.seededChannels.length; i++) {
|
|
122
|
+
const channelOptions = options.seededChannels[i];
|
|
123
|
+
const id = { id: i, source: QualifiedChannelId_1.ChannelSource.Seeded };
|
|
124
|
+
this.setOpenChannel(new Channel_1.ChannelClass(this, id, {
|
|
125
|
+
name: '',
|
|
126
|
+
remoteWindowSize: (_c = channelOptions.channelReceivingWindowSize) !== null && _c !== void 0 ? _c : this.defaultChannelReceivingWindowSize,
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Creates an anonymous channel that may be accepted by <see cref="AcceptChannel(int, ChannelOptions)"/>.
|
|
133
|
+
* Its existance must be communicated by other means (typically another, existing channel) to encourage acceptance.
|
|
134
|
+
* @param options A set of options that describe local treatment of this channel.
|
|
135
|
+
* @returns The anonymous channel.
|
|
136
|
+
* @description Note that while the channel is created immediately, any local write to that channel will be
|
|
137
|
+
* buffered locally until the remote party accepts the channel.
|
|
138
|
+
*/
|
|
139
|
+
createChannel(options) {
|
|
140
|
+
var _a;
|
|
141
|
+
const offerParameters = {
|
|
142
|
+
name: '',
|
|
143
|
+
remoteWindowSize: (_a = options === null || options === void 0 ? void 0 : options.channelReceivingWindowSize) !== null && _a !== void 0 ? _a : this.defaultChannelReceivingWindowSize,
|
|
144
|
+
};
|
|
145
|
+
const payload = this.formatter.serializeOfferParameters(offerParameters);
|
|
146
|
+
const channel = new Channel_1.ChannelClass(this, { id: this.getUnusedChannelId(), source: QualifiedChannelId_1.ChannelSource.Local }, offerParameters);
|
|
147
|
+
this.setOpenChannel(channel);
|
|
148
|
+
this.rejectOnFailure(this.sendFrameAsync(new FrameHeader_1.FrameHeader(ControlCode_1.ControlCode.offer, channel.qualifiedId), payload, this.disposalToken));
|
|
149
|
+
return channel;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Accepts a channel with a specific ID.
|
|
153
|
+
* @param id The id of the channel to accept.
|
|
154
|
+
* @param options A set of options that describe local treatment of this channel.
|
|
155
|
+
* @description This method can be used to accept anonymous channels created with <see cref="CreateChannel"/>.
|
|
156
|
+
* Unlike <see cref="AcceptChannelAsync(string, ChannelOptions, CancellationToken)"/> which will await
|
|
157
|
+
* for a channel offer if a matching one has not been made yet, this method only accepts an offer
|
|
158
|
+
* for a channel that has already been made.
|
|
159
|
+
*/
|
|
160
|
+
acceptChannel(id, options) {
|
|
161
|
+
const channel = this.remotelyOfferedOpenChannels[id] || this.seededOpenChannels[id];
|
|
162
|
+
if (!channel) {
|
|
163
|
+
throw new Error('No channel with ID ' + id);
|
|
164
|
+
}
|
|
165
|
+
this.removeChannelFromOfferedQueue(channel);
|
|
166
|
+
this.acceptChannelOrThrow(channel, options);
|
|
167
|
+
return channel;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Rejects an offer for the channel with a specified ID.
|
|
171
|
+
* @param id The ID of the channel whose offer should be rejected.
|
|
172
|
+
*/
|
|
173
|
+
rejectChannel(id) {
|
|
174
|
+
const channel = this.remotelyOfferedOpenChannels[id];
|
|
175
|
+
if (channel) {
|
|
176
|
+
(0, Utilities_1.removeFromQueue)(channel, this.channelsOfferedByThemByName[channel.name]);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
throw new Error('No channel with that ID found.');
|
|
180
|
+
}
|
|
181
|
+
// Rejecting a channel rejects a couple promises that we don't want the caller to have to observe
|
|
182
|
+
// separately since they are explicitly stating they want to take this action now.
|
|
183
|
+
caught(channel.acceptance);
|
|
184
|
+
caught(channel.completion);
|
|
185
|
+
channel.dispose();
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Offers a new, named channel to the remote party so they may accept it with
|
|
189
|
+
* {@link acceptChannelAsync}.
|
|
190
|
+
* @param name A name for the channel, which must be accepted on the remote end to complete creation.
|
|
191
|
+
* It need not be unique, and may be empty but must not be null.
|
|
192
|
+
* Any characters are allowed, and max length is determined by the maximum frame payload (based on UTF-8 encoding).
|
|
193
|
+
* @param options A set of options that describe local treatment of this channel.
|
|
194
|
+
* @param cancellationToken A cancellation token. Do NOT let this be a long-lived token
|
|
195
|
+
* or a memory leak will result since we add continuations to its promise.
|
|
196
|
+
* @returns A task that completes with the `Channel` if the offer is accepted on the remote end
|
|
197
|
+
* or faults with `MultiplexingProtocolException` if the remote end rejects the channel.
|
|
198
|
+
*/
|
|
199
|
+
async offerChannelAsync(name, options, cancellationToken = cancellationtoken_1.default.CONTINUE) {
|
|
200
|
+
var _a;
|
|
201
|
+
if (name === null || name === undefined) {
|
|
202
|
+
throw new Error('Name must be specified (but may be empty).');
|
|
203
|
+
}
|
|
204
|
+
cancellationToken.throwIfCancelled();
|
|
205
|
+
(0, Utilities_1.throwIfDisposed)(this);
|
|
206
|
+
const offerParameters = {
|
|
207
|
+
name,
|
|
208
|
+
remoteWindowSize: (_a = options === null || options === void 0 ? void 0 : options.channelReceivingWindowSize) !== null && _a !== void 0 ? _a : this.defaultChannelReceivingWindowSize,
|
|
209
|
+
};
|
|
210
|
+
const payload = this.formatter.serializeOfferParameters(offerParameters);
|
|
211
|
+
const channel = new Channel_1.ChannelClass(this, { id: this.getUnusedChannelId(), source: QualifiedChannelId_1.ChannelSource.Local }, offerParameters);
|
|
212
|
+
this.setOpenChannel(channel);
|
|
213
|
+
const header = new FrameHeader_1.FrameHeader(ControlCode_1.ControlCode.offer, channel.qualifiedId);
|
|
214
|
+
const unsubscribeFromCT = cancellationToken.onCancelled(reason => this.offerChannelCanceled(channel, reason));
|
|
215
|
+
try {
|
|
216
|
+
// We *will* recognize rejection of this promise. But just in case sendFrameAsync completes synchronously,
|
|
217
|
+
// we want to signify that we *will* catch it first to avoid node.js emitting warnings or crashing.
|
|
218
|
+
caught(channel.acceptance);
|
|
219
|
+
await this.sendFrameAsync(header, payload, cancellationToken);
|
|
220
|
+
await channel.acceptance;
|
|
221
|
+
return channel;
|
|
222
|
+
}
|
|
223
|
+
finally {
|
|
224
|
+
unsubscribeFromCT();
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Accepts a channel that the remote end has attempted or may attempt to create.
|
|
229
|
+
* @param name The name of the channel to accept.
|
|
230
|
+
* @param options A set of options that describe local treatment of this channel.
|
|
231
|
+
* @param cancellationToken A token to indicate lost interest in accepting the channel.
|
|
232
|
+
* Do NOT let this be a long-lived token
|
|
233
|
+
* or a memory leak will result since we add continuations to its promise.
|
|
234
|
+
* @returns The `Channel`, after its offer has been received from the remote party and accepted.
|
|
235
|
+
* @description If multiple offers exist with the specified `name`, the first one received will be accepted.
|
|
236
|
+
*/
|
|
237
|
+
async acceptChannelAsync(name, options, cancellationToken = cancellationtoken_1.default.CONTINUE) {
|
|
238
|
+
if (name === null || name === undefined) {
|
|
239
|
+
throw new Error('Name must be specified (but may be empty).');
|
|
240
|
+
}
|
|
241
|
+
cancellationToken.throwIfCancelled();
|
|
242
|
+
(0, Utilities_1.throwIfDisposed)(this);
|
|
243
|
+
let channel;
|
|
244
|
+
let pendingAcceptChannel;
|
|
245
|
+
const channelsOfferedByThem = this.channelsOfferedByThemByName[name];
|
|
246
|
+
if (channelsOfferedByThem) {
|
|
247
|
+
while (channel === undefined && channelsOfferedByThem.length > 0) {
|
|
248
|
+
channel = channelsOfferedByThem.shift();
|
|
249
|
+
if (channel.isAccepted || channel.isRejectedOrCanceled) {
|
|
250
|
+
channel = undefined;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (channel === undefined) {
|
|
256
|
+
let acceptingChannels = this.acceptingChannels[name];
|
|
257
|
+
if (!acceptingChannels) {
|
|
258
|
+
this.acceptingChannels[name] = acceptingChannels = [];
|
|
259
|
+
}
|
|
260
|
+
pendingAcceptChannel = new Deferred_1.Deferred(options);
|
|
261
|
+
acceptingChannels.push(pendingAcceptChannel);
|
|
262
|
+
}
|
|
263
|
+
if (channel !== undefined) {
|
|
264
|
+
this.acceptChannelOrThrow(channel, options);
|
|
265
|
+
return channel;
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
const unsubscribeFromCT = cancellationToken.onCancelled(reason => this.acceptChannelCanceled(pendingAcceptChannel, name, reason));
|
|
269
|
+
try {
|
|
270
|
+
return await pendingAcceptChannel.promise;
|
|
271
|
+
}
|
|
272
|
+
finally {
|
|
273
|
+
unsubscribeFromCT();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Disposes the stream.
|
|
279
|
+
*/
|
|
280
|
+
dispose() {
|
|
281
|
+
this.disposeCore(false);
|
|
282
|
+
}
|
|
283
|
+
disposeCore(remoteDisconnect) {
|
|
284
|
+
this.disposalTokenSource.cancel();
|
|
285
|
+
this._completionSource.resolve();
|
|
286
|
+
this.formatter.end();
|
|
287
|
+
[this.locallyOfferedOpenChannels, this.remotelyOfferedOpenChannels].forEach(cb => {
|
|
288
|
+
for (const channelId in cb) {
|
|
289
|
+
if (cb.hasOwnProperty(channelId)) {
|
|
290
|
+
const channel = cb[channelId];
|
|
291
|
+
// Acceptance gets rejected when a channel is disposed.
|
|
292
|
+
// Avoid a node.js crash or test failure for unobserved channels (e.g. offers for channels from the other party that no one cared to receive on this side).
|
|
293
|
+
caught(channel.acceptance);
|
|
294
|
+
channel.dispose();
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
// Iterate over acceptingChannels to reject each promise
|
|
299
|
+
const errorMessage = remoteDisconnect ? 'The remote party has disconnected.' : 'The stream is being disposed locally.';
|
|
300
|
+
for (const name in this.acceptingChannels) {
|
|
301
|
+
if (this.acceptingChannels.hasOwnProperty(name)) {
|
|
302
|
+
const acceptingChannels = this.acceptingChannels[name];
|
|
303
|
+
for (const channelDeferral of acceptingChannels) {
|
|
304
|
+
channelDeferral.reject(new Error(errorMessage));
|
|
305
|
+
}
|
|
306
|
+
delete this.acceptingChannels[name];
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
on(event, listener) {
|
|
311
|
+
this.eventEmitter.on(event, listener);
|
|
312
|
+
}
|
|
313
|
+
off(event, listener) {
|
|
314
|
+
this.eventEmitter.off(event, listener);
|
|
315
|
+
}
|
|
316
|
+
once(event, listener) {
|
|
317
|
+
this.eventEmitter.once(event, listener);
|
|
318
|
+
}
|
|
319
|
+
raiseChannelOffered(id, name, isAccepted) {
|
|
320
|
+
const args = {
|
|
321
|
+
id,
|
|
322
|
+
isAccepted,
|
|
323
|
+
name,
|
|
324
|
+
};
|
|
325
|
+
try {
|
|
326
|
+
this.eventEmitter.emit('channelOffered', args);
|
|
327
|
+
}
|
|
328
|
+
catch (err) {
|
|
329
|
+
this._completionSource.reject(err);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
acceptChannelOrThrow(channel, options) {
|
|
333
|
+
var _a, _b;
|
|
334
|
+
if (channel.tryAcceptOffer(options)) {
|
|
335
|
+
const acceptanceParameters = {
|
|
336
|
+
remoteWindowSize: (_b = (_a = options === null || options === void 0 ? void 0 : options.channelReceivingWindowSize) !== null && _a !== void 0 ? _a : channel.localWindowSize) !== null && _b !== void 0 ? _b : this.defaultChannelReceivingWindowSize,
|
|
337
|
+
};
|
|
338
|
+
if (acceptanceParameters.remoteWindowSize < this.defaultChannelReceivingWindowSize) {
|
|
339
|
+
acceptanceParameters.remoteWindowSize = this.defaultChannelReceivingWindowSize;
|
|
340
|
+
}
|
|
341
|
+
if (channel.qualifiedId.source !== QualifiedChannelId_1.ChannelSource.Seeded) {
|
|
342
|
+
const payload = this.formatter.serializeAcceptanceParameters(acceptanceParameters);
|
|
343
|
+
this.rejectOnFailure(this.sendFrameAsync(new FrameHeader_1.FrameHeader(ControlCode_1.ControlCode.offerAccepted, channel.qualifiedId), payload, this.disposalToken));
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
else if (channel.isAccepted) {
|
|
347
|
+
throw new Error('Channel is already accepted.');
|
|
348
|
+
}
|
|
349
|
+
else if (channel.isRejectedOrCanceled) {
|
|
350
|
+
throw new Error('Channel is no longer available for acceptance.');
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
throw new Error('Channel could not be accepted.');
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Disposes this instance if the specified promise is rejected.
|
|
358
|
+
* @param promise The promise to check for failures.
|
|
359
|
+
*/
|
|
360
|
+
async rejectOnFailure(promise) {
|
|
361
|
+
try {
|
|
362
|
+
await promise;
|
|
363
|
+
}
|
|
364
|
+
catch (err) {
|
|
365
|
+
this._completionSource.reject(err);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
removeChannelFromOfferedQueue(channel) {
|
|
369
|
+
if (channel.name) {
|
|
370
|
+
(0, Utilities_1.removeFromQueue)(channel, this.channelsOfferedByThemByName[channel.name]);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
getOpenChannel(qualifiedId) {
|
|
374
|
+
return this.getChannelCollection(qualifiedId.source)[qualifiedId.id];
|
|
375
|
+
}
|
|
376
|
+
setOpenChannel(channel) {
|
|
377
|
+
this.getChannelCollection(channel.qualifiedId.source)[channel.qualifiedId.id] = channel;
|
|
378
|
+
}
|
|
379
|
+
deleteOpenChannel(qualifiedId) {
|
|
380
|
+
delete this.getChannelCollection(qualifiedId.source)[qualifiedId.id];
|
|
381
|
+
}
|
|
382
|
+
getChannelCollection(source) {
|
|
383
|
+
switch (source) {
|
|
384
|
+
case QualifiedChannelId_1.ChannelSource.Local:
|
|
385
|
+
return this.locallyOfferedOpenChannels;
|
|
386
|
+
case QualifiedChannelId_1.ChannelSource.Remote:
|
|
387
|
+
return this.remotelyOfferedOpenChannels;
|
|
388
|
+
case QualifiedChannelId_1.ChannelSource.Seeded:
|
|
389
|
+
return this.seededOpenChannels;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Cancels a prior call to acceptChannelAsync
|
|
394
|
+
* @param channel The promise of a channel to be canceled.
|
|
395
|
+
* @param name The name of the channel the caller was accepting.
|
|
396
|
+
* @param reason The reason for cancellation.
|
|
397
|
+
*/
|
|
398
|
+
acceptChannelCanceled(channel, name, reason) {
|
|
399
|
+
if (channel.reject(new cancellationtoken_1.default.CancellationError(reason))) {
|
|
400
|
+
(0, Utilities_1.removeFromQueue)(channel, this.acceptingChannels[name]);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Responds to cancellation of a prior call to offerChannelAsync.
|
|
405
|
+
* @param channel The channel previously offered.
|
|
406
|
+
*/
|
|
407
|
+
offerChannelCanceled(channel, reason) {
|
|
408
|
+
channel.tryCancelOffer(reason);
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Gets a unique number that can be used to represent a channel.
|
|
412
|
+
* @description The channel numbers increase by two in order to maintain odd or even numbers,
|
|
413
|
+
* since each party is allowed to create only one or the other.
|
|
414
|
+
*/
|
|
415
|
+
getUnusedChannelId() {
|
|
416
|
+
return (this.lastOfferedChannelId += this.isOdd !== undefined ? 2 : 1);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
exports.MultiplexingStream = MultiplexingStream;
|
|
420
|
+
/**
|
|
421
|
+
* The maximum length of a frame's payload.
|
|
422
|
+
*/
|
|
423
|
+
MultiplexingStream.framePayloadMaxLength = 20 * 1024;
|
|
424
|
+
MultiplexingStream.recommendedDefaultChannelReceivingWindowSize = 5 * MultiplexingStream.framePayloadMaxLength;
|
|
425
|
+
/**
|
|
426
|
+
* The options to use for channels we create in response to incoming offers.
|
|
427
|
+
* @description Whatever these settings are, they can be replaced when the channel is accepted.
|
|
428
|
+
*/
|
|
429
|
+
MultiplexingStream.defaultChannelOptions = {};
|
|
430
|
+
/**
|
|
431
|
+
* The encoding used for characters in control frames.
|
|
432
|
+
*/
|
|
433
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
434
|
+
MultiplexingStream.ControlFrameEncoding = 'utf-8';
|
|
435
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
436
|
+
class MultiplexingStreamClass extends MultiplexingStream {
|
|
437
|
+
constructor(formatter, isOdd, options) {
|
|
438
|
+
var _a, _b;
|
|
439
|
+
super(formatter, isOdd, options);
|
|
440
|
+
this.sendingSemaphore = new await_semaphore_1.Semaphore(1);
|
|
441
|
+
this.lastOfferedChannelId = isOdd ? -1 : 0; // the first channel created should be 1 or 2
|
|
442
|
+
this.lastOfferedChannelId += (_b = (_a = options.seededChannels) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0;
|
|
443
|
+
// Initiate reading from the transport stream. This will not end until the stream does, or we're disposed.
|
|
444
|
+
// If reading the stream fails, we'll dispose ourselves.
|
|
445
|
+
this.readFromStream(this.disposalToken).catch(err => this._completionSource.reject(err));
|
|
446
|
+
}
|
|
447
|
+
get backpressureSupportEnabled() {
|
|
448
|
+
return this.protocolMajorVersion > 1;
|
|
449
|
+
}
|
|
450
|
+
async sendFrameAsync(header, payload, cancellationToken = cancellationtoken_1.default.CONTINUE) {
|
|
451
|
+
if (!header) {
|
|
452
|
+
throw new Error('Header is required.');
|
|
453
|
+
}
|
|
454
|
+
await this.sendingSemaphore.use(async () => {
|
|
455
|
+
cancellationToken.throwIfCancelled();
|
|
456
|
+
(0, Utilities_1.throwIfDisposed)(this);
|
|
457
|
+
await this.formatter.writeFrameAsync(header, payload);
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Transmits a frame over the stream.
|
|
462
|
+
* @param code The op code for the channel.
|
|
463
|
+
* @param channelId The ID of the channel to receive the frame.
|
|
464
|
+
* @description The promise returned from this function is always resolved (not rejected)
|
|
465
|
+
* since it is anticipated that callers may not be awaiting its result.
|
|
466
|
+
*/
|
|
467
|
+
async sendFrame(code, channelId) {
|
|
468
|
+
try {
|
|
469
|
+
if (this._completionSource.isCompleted) {
|
|
470
|
+
// Any frames that come in after we're done are most likely frames just informing that channels are
|
|
471
|
+
// being terminated, which we do not need to communicate since the connection going down implies that.
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
const header = new FrameHeader_1.FrameHeader(code, channelId);
|
|
475
|
+
await this.sendFrameAsync(header);
|
|
476
|
+
}
|
|
477
|
+
catch (error) {
|
|
478
|
+
// We mustn't throw back to our caller. So report the failure by disposing with failure.
|
|
479
|
+
this._completionSource.reject(error);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
onChannelWritingCompleted(channel) {
|
|
483
|
+
// Only inform the remote side if this channel has not already been terminated.
|
|
484
|
+
if (!channel.isDisposed && this.getOpenChannel(channel.qualifiedId)) {
|
|
485
|
+
this.sendFrame(ControlCode_1.ControlCode.contentWritingCompleted, channel.qualifiedId);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
async onChannelDisposed(channel, error) {
|
|
489
|
+
if (!this._completionSource.isCompleted) {
|
|
490
|
+
try {
|
|
491
|
+
const payload = this.protocolMajorVersion > 1 && error ? this.formatter.serializeException(error) : Buffer.alloc(0);
|
|
492
|
+
const frameHeader = new FrameHeader_1.FrameHeader(ControlCode_1.ControlCode.channelTerminated, channel.qualifiedId);
|
|
493
|
+
await this.sendFrameAsync(frameHeader, payload);
|
|
494
|
+
}
|
|
495
|
+
catch (err) {
|
|
496
|
+
// Swallow exceptions thrown about channel disposal if the whole stream has been taken down.
|
|
497
|
+
if (this.isDisposed) {
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
throw err;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
localContentExamined(channel, bytesConsumed) {
|
|
505
|
+
const payload = this.formatter.serializeContentProcessed(bytesConsumed);
|
|
506
|
+
this.rejectOnFailure(this.sendFrameAsync(new FrameHeader_1.FrameHeader(ControlCode_1.ControlCode.contentProcessed, channel.qualifiedId), payload));
|
|
507
|
+
}
|
|
508
|
+
async readFromStream(cancellationToken) {
|
|
509
|
+
while (!this.isDisposed) {
|
|
510
|
+
const frame = await this.formatter.readFrameAsync(cancellationToken);
|
|
511
|
+
if (frame === null) {
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
frame.header.flipChannelPerspective();
|
|
515
|
+
switch (frame.header.code) {
|
|
516
|
+
case ControlCode_1.ControlCode.offer:
|
|
517
|
+
this.onOffer(frame.header.requiredChannel, frame.payload);
|
|
518
|
+
break;
|
|
519
|
+
case ControlCode_1.ControlCode.offerAccepted:
|
|
520
|
+
this.onOfferAccepted(frame.header.requiredChannel, frame.payload);
|
|
521
|
+
break;
|
|
522
|
+
case ControlCode_1.ControlCode.content:
|
|
523
|
+
this.onContent(frame.header.requiredChannel, frame.payload);
|
|
524
|
+
break;
|
|
525
|
+
case ControlCode_1.ControlCode.contentProcessed:
|
|
526
|
+
this.onContentProcessed(frame.header.requiredChannel, frame.payload);
|
|
527
|
+
break;
|
|
528
|
+
case ControlCode_1.ControlCode.contentWritingCompleted:
|
|
529
|
+
this.onContentWritingCompleted(frame.header.requiredChannel);
|
|
530
|
+
break;
|
|
531
|
+
case ControlCode_1.ControlCode.channelTerminated:
|
|
532
|
+
this.onChannelTerminated(frame.header.requiredChannel, frame.payload);
|
|
533
|
+
break;
|
|
534
|
+
default:
|
|
535
|
+
break;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
this.disposeCore(true);
|
|
539
|
+
}
|
|
540
|
+
onOffer(channelId, payload) {
|
|
541
|
+
const offerParameters = this.formatter.deserializeOfferParameters(payload);
|
|
542
|
+
const channel = new Channel_1.ChannelClass(this, channelId, offerParameters);
|
|
543
|
+
let acceptingChannelAlreadyPresent = false;
|
|
544
|
+
let options;
|
|
545
|
+
let acceptingChannels;
|
|
546
|
+
if ((acceptingChannels = this.acceptingChannels[offerParameters.name]) !== undefined) {
|
|
547
|
+
while (acceptingChannels.length > 0) {
|
|
548
|
+
const candidate = acceptingChannels.shift();
|
|
549
|
+
if (candidate.resolve(channel)) {
|
|
550
|
+
acceptingChannelAlreadyPresent = true;
|
|
551
|
+
options = candidate.state;
|
|
552
|
+
break;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
if (!acceptingChannelAlreadyPresent) {
|
|
557
|
+
if (offerParameters.name) {
|
|
558
|
+
let offeredChannels;
|
|
559
|
+
if (!(offeredChannels = this.channelsOfferedByThemByName[offerParameters.name])) {
|
|
560
|
+
this.channelsOfferedByThemByName[offerParameters.name] = offeredChannels = [];
|
|
561
|
+
}
|
|
562
|
+
offeredChannels.push(channel);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
this.setOpenChannel(channel);
|
|
566
|
+
if (acceptingChannelAlreadyPresent) {
|
|
567
|
+
this.acceptChannelOrThrow(channel, options);
|
|
568
|
+
}
|
|
569
|
+
this.raiseChannelOffered(channel.id, channel.name, acceptingChannelAlreadyPresent);
|
|
570
|
+
}
|
|
571
|
+
onOfferAccepted(channelId, payload) {
|
|
572
|
+
if (channelId.source !== QualifiedChannelId_1.ChannelSource.Local) {
|
|
573
|
+
throw new Error('Remote party tried to accept a channel that they offered.');
|
|
574
|
+
}
|
|
575
|
+
const acceptanceParameter = this.formatter.deserializeAcceptanceParameters(payload);
|
|
576
|
+
const channel = this.getOpenChannel(channelId);
|
|
577
|
+
if (!channel) {
|
|
578
|
+
throw new Error(`Unexpected channel created with ID ${QualifiedChannelId_1.QualifiedChannelId.toString(channelId)}`);
|
|
579
|
+
}
|
|
580
|
+
if (!channel.onAccepted(acceptanceParameter)) {
|
|
581
|
+
// This may be an acceptance of a channel that we canceled an offer for, and a race condition
|
|
582
|
+
// led to our cancellation notification crossing in transit with their acceptance notification.
|
|
583
|
+
// In this case, do nothing since we already sent a channel termination message, and the remote side
|
|
584
|
+
// should notice it soon.
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
onContent(channelId, payload) {
|
|
588
|
+
const channel = this.getOpenChannel(channelId);
|
|
589
|
+
if (!channel) {
|
|
590
|
+
throw new Error(`No channel with id ${channelId} found.`);
|
|
591
|
+
}
|
|
592
|
+
channel.onContent(payload);
|
|
593
|
+
}
|
|
594
|
+
onContentProcessed(channelId, payload) {
|
|
595
|
+
const channel = this.getOpenChannel(channelId);
|
|
596
|
+
if (!channel) {
|
|
597
|
+
throw new Error(`No channel with id ${channelId} found.`);
|
|
598
|
+
}
|
|
599
|
+
const bytesProcessed = this.formatter.deserializeContentProcessed(payload);
|
|
600
|
+
channel.onContentProcessed(bytesProcessed);
|
|
601
|
+
}
|
|
602
|
+
onContentWritingCompleted(channelId) {
|
|
603
|
+
const channel = this.getOpenChannel(channelId);
|
|
604
|
+
if (!channel) {
|
|
605
|
+
throw new Error(`No channel with id ${channelId} found.`);
|
|
606
|
+
}
|
|
607
|
+
channel.onContent(null); // signify that the remote is done writing.
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Occurs when the remote party has terminated a channel (including canceling an offer).
|
|
611
|
+
* @param channelId The ID of the terminated channel.
|
|
612
|
+
*/
|
|
613
|
+
onChannelTerminated(channelId, payload) {
|
|
614
|
+
const channel = this.getOpenChannel(channelId);
|
|
615
|
+
if (channel) {
|
|
616
|
+
this.deleteOpenChannel(channelId);
|
|
617
|
+
this.removeChannelFromOfferedQueue(channel);
|
|
618
|
+
// Extract the exception that we received from the remote side.
|
|
619
|
+
const remoteException = this.protocolMajorVersion > 1 ? this.formatter.deserializeException(payload) : null;
|
|
620
|
+
channel.dispose(remoteException);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
exports.MultiplexingStreamClass = MultiplexingStreamClass;
|
|
611
625
|
//# sourceMappingURL=MultiplexingStream.js.map
|