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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export interface AcceptanceParameters {
|
|
2
|
-
/** The maximum number of bytes that may be transmitted and not yet acknowledged as processed by the remote party. */
|
|
3
|
-
remoteWindowSize?: number;
|
|
4
|
-
}
|
|
1
|
+
export interface AcceptanceParameters {
|
|
2
|
+
/** The maximum number of bytes that may be transmitted and not yet acknowledged as processed by the remote party. */
|
|
3
|
+
remoteWindowSize?: number;
|
|
4
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
//# sourceMappingURL=AcceptanceParameters.js.map
|
package/js/Channel.d.ts
CHANGED
|
@@ -1,70 +1,71 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
private
|
|
46
|
-
private readonly
|
|
47
|
-
private readonly
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
*
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
get
|
|
60
|
-
get
|
|
61
|
-
get
|
|
62
|
-
get
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { AcceptanceParameters } from './AcceptanceParameters';
|
|
4
|
+
import { ChannelOptions } from './ChannelOptions';
|
|
5
|
+
import { IDisposableObservable } from './IDisposableObservable';
|
|
6
|
+
import { MultiplexingStreamClass } from './MultiplexingStream';
|
|
7
|
+
import { OfferParameters } from './OfferParameters';
|
|
8
|
+
import { QualifiedChannelId } from './QualifiedChannelId';
|
|
9
|
+
export declare abstract class Channel implements IDisposableObservable {
|
|
10
|
+
/**
|
|
11
|
+
* The id of the channel.
|
|
12
|
+
* @obsolete Use qualifiedId instead.
|
|
13
|
+
*/
|
|
14
|
+
get id(): number;
|
|
15
|
+
/**
|
|
16
|
+
* The party-qualified ID of the channel.
|
|
17
|
+
*/
|
|
18
|
+
readonly qualifiedId: QualifiedChannelId;
|
|
19
|
+
/**
|
|
20
|
+
* A read/write stream used to communicate over this channel.
|
|
21
|
+
*/
|
|
22
|
+
abstract stream: NodeJS.ReadWriteStream;
|
|
23
|
+
/**
|
|
24
|
+
* A promise that completes when this channel has been accepted/rejected by the remote party.
|
|
25
|
+
*/
|
|
26
|
+
abstract acceptance: Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* A promise that completes when this channel is closed.
|
|
29
|
+
*/
|
|
30
|
+
abstract completion: Promise<void>;
|
|
31
|
+
private _isDisposed;
|
|
32
|
+
constructor(id: QualifiedChannelId);
|
|
33
|
+
/**
|
|
34
|
+
* Gets a value indicating whether this channel has been disposed.
|
|
35
|
+
*/
|
|
36
|
+
get isDisposed(): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Closes this channel.
|
|
39
|
+
* @param error An optional error to send to the remote side, if this multiplexing stream is using protocol versions >= 2.
|
|
40
|
+
*/
|
|
41
|
+
dispose(error?: Error | null): void;
|
|
42
|
+
}
|
|
43
|
+
export declare class ChannelClass extends Channel {
|
|
44
|
+
readonly name: string;
|
|
45
|
+
private _duplex;
|
|
46
|
+
private readonly _multiplexingStream;
|
|
47
|
+
private readonly _acceptance;
|
|
48
|
+
private readonly _completion;
|
|
49
|
+
localWindowSize?: number;
|
|
50
|
+
private remoteWindowSize?;
|
|
51
|
+
/**
|
|
52
|
+
* The number of bytes transmitted from here but not yet acknowledged as processed from there,
|
|
53
|
+
* and thus occupying some portion of the full AcceptanceParameters.RemoteWindowSize.
|
|
54
|
+
*/
|
|
55
|
+
private remoteWindowFilled;
|
|
56
|
+
/** A signal which indicates when the <see cref="RemoteWindowRemaining"/> is non-zero. */
|
|
57
|
+
private remoteWindowHasCapacity;
|
|
58
|
+
constructor(multiplexingStream: MultiplexingStreamClass, id: QualifiedChannelId, offerParameters: OfferParameters);
|
|
59
|
+
get stream(): NodeJS.ReadWriteStream;
|
|
60
|
+
get acceptance(): Promise<void>;
|
|
61
|
+
get isAccepted(): boolean;
|
|
62
|
+
get isRejectedOrCanceled(): boolean;
|
|
63
|
+
get completion(): Promise<void>;
|
|
64
|
+
tryAcceptOffer(options?: ChannelOptions): boolean;
|
|
65
|
+
tryCancelOffer(reason: any): void;
|
|
66
|
+
onAccepted(acceptanceParameter: AcceptanceParameters): boolean;
|
|
67
|
+
onContent(buffer: Buffer | null): void;
|
|
68
|
+
onContentProcessed(bytesProcessed: number): void;
|
|
69
|
+
dispose(error?: Error | null): void;
|
|
70
|
+
private onTransmittingBytes;
|
|
71
|
+
}
|
package/js/Channel.js
CHANGED
|
@@ -1,220 +1,220 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ChannelClass = exports.Channel = void 0;
|
|
4
|
-
const cancellationtoken_1 = require("cancellationtoken");
|
|
5
|
-
const stream_1 = require("stream");
|
|
6
|
-
const ControlCode_1 = require("./ControlCode");
|
|
7
|
-
const Deferred_1 = require("./Deferred");
|
|
8
|
-
const FrameHeader_1 = require("./FrameHeader");
|
|
9
|
-
const MultiplexingStream_1 = require("./MultiplexingStream");
|
|
10
|
-
const QualifiedChannelId_1 = require("./QualifiedChannelId");
|
|
11
|
-
const caught = require("caught");
|
|
12
|
-
class Channel {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Gets a value indicating whether this channel has been disposed.
|
|
26
|
-
*/
|
|
27
|
-
get isDisposed() {
|
|
28
|
-
return this._isDisposed;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Closes this channel.
|
|
32
|
-
* @param error An optional error to send to the remote side, if this multiplexing stream is using protocol versions >= 2.
|
|
33
|
-
*/
|
|
34
|
-
dispose(error) {
|
|
35
|
-
// The interesting stuff is in the derived class.
|
|
36
|
-
this._isDisposed = true;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
exports.Channel = Channel;
|
|
40
|
-
// tslint:disable-next-line:max-classes-per-file
|
|
41
|
-
class ChannelClass extends Channel {
|
|
42
|
-
constructor(multiplexingStream, id, offerParameters) {
|
|
43
|
-
super(id);
|
|
44
|
-
this._acceptance = new Deferred_1.Deferred();
|
|
45
|
-
this._completion = new Deferred_1.Deferred();
|
|
46
|
-
/**
|
|
47
|
-
* The number of bytes transmitted from here but not yet acknowledged as processed from there,
|
|
48
|
-
* and thus occupying some portion of the full AcceptanceParameters.RemoteWindowSize.
|
|
49
|
-
*/
|
|
50
|
-
this.remoteWindowFilled = 0;
|
|
51
|
-
const self = this;
|
|
52
|
-
this.name = offerParameters.name;
|
|
53
|
-
switch (id.source) {
|
|
54
|
-
case QualifiedChannelId_1.ChannelSource.Local:
|
|
55
|
-
this.localWindowSize = offerParameters.remoteWindowSize;
|
|
56
|
-
break;
|
|
57
|
-
case QualifiedChannelId_1.ChannelSource.Remote:
|
|
58
|
-
this.remoteWindowSize = offerParameters.remoteWindowSize;
|
|
59
|
-
break;
|
|
60
|
-
case QualifiedChannelId_1.ChannelSource.Seeded:
|
|
61
|
-
this.remoteWindowSize = offerParameters.remoteWindowSize;
|
|
62
|
-
this.localWindowSize = offerParameters.remoteWindowSize;
|
|
63
|
-
break;
|
|
64
|
-
default:
|
|
65
|
-
throw new Error('Channel source not recognized.');
|
|
66
|
-
}
|
|
67
|
-
this._multiplexingStream = multiplexingStream;
|
|
68
|
-
this.remoteWindowHasCapacity = new Deferred_1.Deferred();
|
|
69
|
-
if (!this._multiplexingStream.backpressureSupportEnabled || this.remoteWindowSize) {
|
|
70
|
-
this.remoteWindowHasCapacity.resolve();
|
|
71
|
-
}
|
|
72
|
-
this._duplex = new stream_1.Duplex({
|
|
73
|
-
async write(chunk, _, callback) {
|
|
74
|
-
let error;
|
|
75
|
-
try {
|
|
76
|
-
let payload = Buffer.from(chunk);
|
|
77
|
-
while (payload.length > 0) {
|
|
78
|
-
// Never transmit more than one frame's worth at a time.
|
|
79
|
-
let bytesTransmitted = Math.min(payload.length, MultiplexingStream_1.MultiplexingStream.framePayloadMaxLength);
|
|
80
|
-
// Don't send more than will fit in the remote's receiving window size.
|
|
81
|
-
if (self._multiplexingStream.backpressureSupportEnabled) {
|
|
82
|
-
await self.remoteWindowHasCapacity.promise;
|
|
83
|
-
if (!self.remoteWindowSize) {
|
|
84
|
-
throw new Error('Remote window size unknown.');
|
|
85
|
-
}
|
|
86
|
-
bytesTransmitted = Math.min(self.remoteWindowSize - self.remoteWindowFilled, bytesTransmitted);
|
|
87
|
-
}
|
|
88
|
-
self.onTransmittingBytes(bytesTransmitted);
|
|
89
|
-
const header = new FrameHeader_1.FrameHeader(ControlCode_1.ControlCode.content, id);
|
|
90
|
-
await multiplexingStream.sendFrameAsync(header, payload.slice(0, bytesTransmitted));
|
|
91
|
-
payload = payload.slice(bytesTransmitted);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
catch (err) {
|
|
95
|
-
error = err;
|
|
96
|
-
}
|
|
97
|
-
if (callback) {
|
|
98
|
-
callback(error);
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
async final(cb) {
|
|
102
|
-
let error;
|
|
103
|
-
try {
|
|
104
|
-
await multiplexingStream.onChannelWritingCompleted(self);
|
|
105
|
-
}
|
|
106
|
-
catch (err) {
|
|
107
|
-
error = err;
|
|
108
|
-
}
|
|
109
|
-
if (cb) {
|
|
110
|
-
cb(error);
|
|
111
|
-
}
|
|
112
|
-
},
|
|
113
|
-
read() {
|
|
114
|
-
// Nothing to do here since data is pushed to us.
|
|
115
|
-
},
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
get stream() {
|
|
119
|
-
return this._duplex;
|
|
120
|
-
}
|
|
121
|
-
get acceptance() {
|
|
122
|
-
return this._acceptance.promise;
|
|
123
|
-
}
|
|
124
|
-
get isAccepted() {
|
|
125
|
-
return this._acceptance.isResolved;
|
|
126
|
-
}
|
|
127
|
-
get isRejectedOrCanceled() {
|
|
128
|
-
return this._acceptance.isRejected;
|
|
129
|
-
}
|
|
130
|
-
get completion() {
|
|
131
|
-
return this._completion.promise;
|
|
132
|
-
}
|
|
133
|
-
tryAcceptOffer(options) {
|
|
134
|
-
if (this._acceptance.resolve()) {
|
|
135
|
-
this.localWindowSize =
|
|
136
|
-
(options === null || options === void 0 ? void 0 : options.channelReceivingWindowSize) !== undefined
|
|
137
|
-
? Math.max(this._multiplexingStream.defaultChannelReceivingWindowSize, options === null || options === void 0 ? void 0 : options.channelReceivingWindowSize)
|
|
138
|
-
: this._multiplexingStream.defaultChannelReceivingWindowSize;
|
|
139
|
-
return true;
|
|
140
|
-
}
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
-
tryCancelOffer(reason) {
|
|
144
|
-
const cancellationReason = new cancellationtoken_1.default.CancellationError(reason);
|
|
145
|
-
this._acceptance.reject(cancellationReason);
|
|
146
|
-
this._completion.reject(cancellationReason);
|
|
147
|
-
// Also mark completion's promise rejections as 'caught' since we do not require
|
|
148
|
-
// or even expect it to be recognized by anyone else.
|
|
149
|
-
// The acceptance promise rejection is observed by the offer channel method.
|
|
150
|
-
caught(this._completion.promise);
|
|
151
|
-
// Inform the remote side that the offer is rescinded.
|
|
152
|
-
this.dispose();
|
|
153
|
-
}
|
|
154
|
-
onAccepted(acceptanceParameter) {
|
|
155
|
-
if (this._multiplexingStream.backpressureSupportEnabled) {
|
|
156
|
-
this.remoteWindowSize = acceptanceParameter.remoteWindowSize;
|
|
157
|
-
this.remoteWindowHasCapacity.resolve();
|
|
158
|
-
}
|
|
159
|
-
return this._acceptance.resolve();
|
|
160
|
-
}
|
|
161
|
-
onContent(buffer) {
|
|
162
|
-
this._duplex.push(buffer);
|
|
163
|
-
// We should find a way to detect when we *actually* share the received buffer with the Channel's user
|
|
164
|
-
// and only report consumption when they receive the buffer from us so that we effectively apply
|
|
165
|
-
// backpressure to the remote party based on our user's actual consumption rather than keep allocating memory.
|
|
166
|
-
if (this._multiplexingStream.backpressureSupportEnabled && buffer) {
|
|
167
|
-
this._multiplexingStream.localContentExamined(this, buffer.length);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
onContentProcessed(bytesProcessed) {
|
|
171
|
-
if (bytesProcessed < 0) {
|
|
172
|
-
throw new Error('A non-negative number is required.');
|
|
173
|
-
}
|
|
174
|
-
if (bytesProcessed > this.remoteWindowFilled) {
|
|
175
|
-
throw new Error('More bytes processed than we thought were in the window.');
|
|
176
|
-
}
|
|
177
|
-
if (this.remoteWindowSize === undefined) {
|
|
178
|
-
throw new Error("Unexpected content processed message given we don't know the remote window size.");
|
|
179
|
-
}
|
|
180
|
-
this.remoteWindowFilled -= bytesProcessed;
|
|
181
|
-
if (this.remoteWindowFilled < this.remoteWindowSize) {
|
|
182
|
-
this.remoteWindowHasCapacity.resolve();
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
dispose(error) {
|
|
186
|
-
if (!this.isDisposed) {
|
|
187
|
-
super.dispose();
|
|
188
|
-
if (this._acceptance.reject(new cancellationtoken_1.default.CancellationError('disposed'))) {
|
|
189
|
-
// Don't crash node due to an unnoticed rejection when dispose was explicitly called.
|
|
190
|
-
caught(this.acceptance);
|
|
191
|
-
}
|
|
192
|
-
// For the pipes, we Complete *our* ends, and leave the user's ends alone.
|
|
193
|
-
// The completion will propagate when it's ready to.
|
|
194
|
-
this._duplex.end();
|
|
195
|
-
this._duplex.push(null);
|
|
196
|
-
if (error) {
|
|
197
|
-
this._completion.reject(error);
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
this._completion.resolve();
|
|
201
|
-
}
|
|
202
|
-
// Send the notification, but we can't await the result of this.
|
|
203
|
-
caught(this._multiplexingStream.onChannelDisposed(this, error !== null && error !== void 0 ? error : null));
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
onTransmittingBytes(transmittedBytes) {
|
|
207
|
-
if (this._multiplexingStream.backpressureSupportEnabled) {
|
|
208
|
-
if (transmittedBytes < 0) {
|
|
209
|
-
throw new Error('Negative byte count transmitted.');
|
|
210
|
-
}
|
|
211
|
-
this.remoteWindowFilled += transmittedBytes;
|
|
212
|
-
if (this.remoteWindowFilled === this.remoteWindowSize) {
|
|
213
|
-
// Suspend writing.
|
|
214
|
-
this.remoteWindowHasCapacity = new Deferred_1.Deferred();
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
exports.ChannelClass = ChannelClass;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ChannelClass = exports.Channel = void 0;
|
|
4
|
+
const cancellationtoken_1 = require("cancellationtoken");
|
|
5
|
+
const stream_1 = require("stream");
|
|
6
|
+
const ControlCode_1 = require("./ControlCode");
|
|
7
|
+
const Deferred_1 = require("./Deferred");
|
|
8
|
+
const FrameHeader_1 = require("./FrameHeader");
|
|
9
|
+
const MultiplexingStream_1 = require("./MultiplexingStream");
|
|
10
|
+
const QualifiedChannelId_1 = require("./QualifiedChannelId");
|
|
11
|
+
const caught = require("caught");
|
|
12
|
+
class Channel {
|
|
13
|
+
/**
|
|
14
|
+
* The id of the channel.
|
|
15
|
+
* @obsolete Use qualifiedId instead.
|
|
16
|
+
*/
|
|
17
|
+
get id() {
|
|
18
|
+
return this.qualifiedId.id;
|
|
19
|
+
}
|
|
20
|
+
constructor(id) {
|
|
21
|
+
this._isDisposed = false;
|
|
22
|
+
this.qualifiedId = id;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Gets a value indicating whether this channel has been disposed.
|
|
26
|
+
*/
|
|
27
|
+
get isDisposed() {
|
|
28
|
+
return this._isDisposed;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Closes this channel.
|
|
32
|
+
* @param error An optional error to send to the remote side, if this multiplexing stream is using protocol versions >= 2.
|
|
33
|
+
*/
|
|
34
|
+
dispose(error) {
|
|
35
|
+
// The interesting stuff is in the derived class.
|
|
36
|
+
this._isDisposed = true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.Channel = Channel;
|
|
40
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
41
|
+
class ChannelClass extends Channel {
|
|
42
|
+
constructor(multiplexingStream, id, offerParameters) {
|
|
43
|
+
super(id);
|
|
44
|
+
this._acceptance = new Deferred_1.Deferred();
|
|
45
|
+
this._completion = new Deferred_1.Deferred();
|
|
46
|
+
/**
|
|
47
|
+
* The number of bytes transmitted from here but not yet acknowledged as processed from there,
|
|
48
|
+
* and thus occupying some portion of the full AcceptanceParameters.RemoteWindowSize.
|
|
49
|
+
*/
|
|
50
|
+
this.remoteWindowFilled = 0;
|
|
51
|
+
const self = this;
|
|
52
|
+
this.name = offerParameters.name;
|
|
53
|
+
switch (id.source) {
|
|
54
|
+
case QualifiedChannelId_1.ChannelSource.Local:
|
|
55
|
+
this.localWindowSize = offerParameters.remoteWindowSize;
|
|
56
|
+
break;
|
|
57
|
+
case QualifiedChannelId_1.ChannelSource.Remote:
|
|
58
|
+
this.remoteWindowSize = offerParameters.remoteWindowSize;
|
|
59
|
+
break;
|
|
60
|
+
case QualifiedChannelId_1.ChannelSource.Seeded:
|
|
61
|
+
this.remoteWindowSize = offerParameters.remoteWindowSize;
|
|
62
|
+
this.localWindowSize = offerParameters.remoteWindowSize;
|
|
63
|
+
break;
|
|
64
|
+
default:
|
|
65
|
+
throw new Error('Channel source not recognized.');
|
|
66
|
+
}
|
|
67
|
+
this._multiplexingStream = multiplexingStream;
|
|
68
|
+
this.remoteWindowHasCapacity = new Deferred_1.Deferred();
|
|
69
|
+
if (!this._multiplexingStream.backpressureSupportEnabled || this.remoteWindowSize) {
|
|
70
|
+
this.remoteWindowHasCapacity.resolve();
|
|
71
|
+
}
|
|
72
|
+
this._duplex = new stream_1.Duplex({
|
|
73
|
+
async write(chunk, _, callback) {
|
|
74
|
+
let error;
|
|
75
|
+
try {
|
|
76
|
+
let payload = Buffer.from(chunk);
|
|
77
|
+
while (payload.length > 0) {
|
|
78
|
+
// Never transmit more than one frame's worth at a time.
|
|
79
|
+
let bytesTransmitted = Math.min(payload.length, MultiplexingStream_1.MultiplexingStream.framePayloadMaxLength);
|
|
80
|
+
// Don't send more than will fit in the remote's receiving window size.
|
|
81
|
+
if (self._multiplexingStream.backpressureSupportEnabled) {
|
|
82
|
+
await self.remoteWindowHasCapacity.promise;
|
|
83
|
+
if (!self.remoteWindowSize) {
|
|
84
|
+
throw new Error('Remote window size unknown.');
|
|
85
|
+
}
|
|
86
|
+
bytesTransmitted = Math.min(self.remoteWindowSize - self.remoteWindowFilled, bytesTransmitted);
|
|
87
|
+
}
|
|
88
|
+
self.onTransmittingBytes(bytesTransmitted);
|
|
89
|
+
const header = new FrameHeader_1.FrameHeader(ControlCode_1.ControlCode.content, id);
|
|
90
|
+
await multiplexingStream.sendFrameAsync(header, payload.slice(0, bytesTransmitted));
|
|
91
|
+
payload = payload.slice(bytesTransmitted);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
error = err;
|
|
96
|
+
}
|
|
97
|
+
if (callback) {
|
|
98
|
+
callback(error);
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
async final(cb) {
|
|
102
|
+
let error;
|
|
103
|
+
try {
|
|
104
|
+
await multiplexingStream.onChannelWritingCompleted(self);
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
error = err;
|
|
108
|
+
}
|
|
109
|
+
if (cb) {
|
|
110
|
+
cb(error);
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
read() {
|
|
114
|
+
// Nothing to do here since data is pushed to us.
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
get stream() {
|
|
119
|
+
return this._duplex;
|
|
120
|
+
}
|
|
121
|
+
get acceptance() {
|
|
122
|
+
return this._acceptance.promise;
|
|
123
|
+
}
|
|
124
|
+
get isAccepted() {
|
|
125
|
+
return this._acceptance.isResolved;
|
|
126
|
+
}
|
|
127
|
+
get isRejectedOrCanceled() {
|
|
128
|
+
return this._acceptance.isRejected;
|
|
129
|
+
}
|
|
130
|
+
get completion() {
|
|
131
|
+
return this._completion.promise;
|
|
132
|
+
}
|
|
133
|
+
tryAcceptOffer(options) {
|
|
134
|
+
if (this._acceptance.resolve()) {
|
|
135
|
+
this.localWindowSize =
|
|
136
|
+
(options === null || options === void 0 ? void 0 : options.channelReceivingWindowSize) !== undefined
|
|
137
|
+
? Math.max(this._multiplexingStream.defaultChannelReceivingWindowSize, options === null || options === void 0 ? void 0 : options.channelReceivingWindowSize)
|
|
138
|
+
: this._multiplexingStream.defaultChannelReceivingWindowSize;
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
tryCancelOffer(reason) {
|
|
144
|
+
const cancellationReason = new cancellationtoken_1.default.CancellationError(reason);
|
|
145
|
+
this._acceptance.reject(cancellationReason);
|
|
146
|
+
this._completion.reject(cancellationReason);
|
|
147
|
+
// Also mark completion's promise rejections as 'caught' since we do not require
|
|
148
|
+
// or even expect it to be recognized by anyone else.
|
|
149
|
+
// The acceptance promise rejection is observed by the offer channel method.
|
|
150
|
+
caught(this._completion.promise);
|
|
151
|
+
// Inform the remote side that the offer is rescinded.
|
|
152
|
+
this.dispose();
|
|
153
|
+
}
|
|
154
|
+
onAccepted(acceptanceParameter) {
|
|
155
|
+
if (this._multiplexingStream.backpressureSupportEnabled) {
|
|
156
|
+
this.remoteWindowSize = acceptanceParameter.remoteWindowSize;
|
|
157
|
+
this.remoteWindowHasCapacity.resolve();
|
|
158
|
+
}
|
|
159
|
+
return this._acceptance.resolve();
|
|
160
|
+
}
|
|
161
|
+
onContent(buffer) {
|
|
162
|
+
this._duplex.push(buffer);
|
|
163
|
+
// We should find a way to detect when we *actually* share the received buffer with the Channel's user
|
|
164
|
+
// and only report consumption when they receive the buffer from us so that we effectively apply
|
|
165
|
+
// backpressure to the remote party based on our user's actual consumption rather than keep allocating memory.
|
|
166
|
+
if (this._multiplexingStream.backpressureSupportEnabled && buffer) {
|
|
167
|
+
this._multiplexingStream.localContentExamined(this, buffer.length);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
onContentProcessed(bytesProcessed) {
|
|
171
|
+
if (bytesProcessed < 0) {
|
|
172
|
+
throw new Error('A non-negative number is required.');
|
|
173
|
+
}
|
|
174
|
+
if (bytesProcessed > this.remoteWindowFilled) {
|
|
175
|
+
throw new Error('More bytes processed than we thought were in the window.');
|
|
176
|
+
}
|
|
177
|
+
if (this.remoteWindowSize === undefined) {
|
|
178
|
+
throw new Error("Unexpected content processed message given we don't know the remote window size.");
|
|
179
|
+
}
|
|
180
|
+
this.remoteWindowFilled -= bytesProcessed;
|
|
181
|
+
if (this.remoteWindowFilled < this.remoteWindowSize) {
|
|
182
|
+
this.remoteWindowHasCapacity.resolve();
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
dispose(error) {
|
|
186
|
+
if (!this.isDisposed) {
|
|
187
|
+
super.dispose();
|
|
188
|
+
if (this._acceptance.reject(new cancellationtoken_1.default.CancellationError('disposed'))) {
|
|
189
|
+
// Don't crash node due to an unnoticed rejection when dispose was explicitly called.
|
|
190
|
+
caught(this.acceptance);
|
|
191
|
+
}
|
|
192
|
+
// For the pipes, we Complete *our* ends, and leave the user's ends alone.
|
|
193
|
+
// The completion will propagate when it's ready to.
|
|
194
|
+
this._duplex.end();
|
|
195
|
+
this._duplex.push(null);
|
|
196
|
+
if (error) {
|
|
197
|
+
this._completion.reject(error);
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
this._completion.resolve();
|
|
201
|
+
}
|
|
202
|
+
// Send the notification, but we can't await the result of this.
|
|
203
|
+
caught(this._multiplexingStream.onChannelDisposed(this, error !== null && error !== void 0 ? error : null));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
onTransmittingBytes(transmittedBytes) {
|
|
207
|
+
if (this._multiplexingStream.backpressureSupportEnabled) {
|
|
208
|
+
if (transmittedBytes < 0) {
|
|
209
|
+
throw new Error('Negative byte count transmitted.');
|
|
210
|
+
}
|
|
211
|
+
this.remoteWindowFilled += transmittedBytes;
|
|
212
|
+
if (this.remoteWindowFilled === this.remoteWindowSize) {
|
|
213
|
+
// Suspend writing.
|
|
214
|
+
this.remoteWindowHasCapacity = new Deferred_1.Deferred();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
exports.ChannelClass = ChannelClass;
|
|
220
220
|
//# sourceMappingURL=Channel.js.map
|
package/js/Channel.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Channel.js","sourceRoot":"","sources":["../src/Channel.ts"],"names":[],"mappings":";;;AAAA,yDAAiD;AACjD,mCAA+B;AAG/B,+CAA2C;AAC3C,yCAAqC;AACrC,+CAA2C;AAE3C,6DAAkF;AAElF,6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A;aAC1B;YAED,gEAAgE;YAChE,MAAM,CAAC,IAAI,CAAC,mBAAmB,CAAC,iBAAiB,CAAC,IAAI,EAAE,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,IAAI,CAAC,CAAC,CAAA;SACvE;IACF,CAAC;IAEO,mBAAmB,CAAC,gBAAwB;QACnD,IAAI,IAAI,CAAC,mBAAmB,CAAC,0BAA0B,EAAE;YACxD,IAAI,gBAAgB,GAAG,CAAC,EAAE;gBACzB,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAA;aACnD;YAED,IAAI,CAAC,kBAAkB,IAAI,gBAAgB,CAAA;YAC3C,IAAI,IAAI,CAAC,kBAAkB,KAAK,IAAI,CAAC,gBAAgB,EAAE;gBACtD,mBAAmB;gBACnB,IAAI,CAAC,uBAAuB,GAAG,IAAI,mBAAQ,EAAQ,CAAA;aACnD;SACD;IACF,CAAC;CACD;AA3ND,oCA2NC"}
|
package/js/ChannelOptions.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export interface ChannelOptions {
|
|
2
|
-
/** The number of received bytes that may be buffered locally per channel (transmitted from the remote party but not yet processed). */
|
|
3
|
-
channelReceivingWindowSize?: number;
|
|
4
|
-
}
|
|
1
|
+
export interface ChannelOptions {
|
|
2
|
+
/** The number of received bytes that may be buffered locally per channel (transmitted from the remote party but not yet processed). */
|
|
3
|
+
channelReceivingWindowSize?: number;
|
|
4
|
+
}
|