nerdbank-streams 2.11.86 → 2.12.74-alpha
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/README.md +22 -22
- package/js/Channel.d.ts +0 -2
- package/js/Deferred.d.ts +2 -2
- package/js/FullDuplexStream.d.ts +0 -2
- package/js/MultiplexingStream.d.ts +0 -2
- package/js/MultiplexingStreamFormatters.d.ts +0 -2
- package/js/Utilities.d.ts +0 -2
- package/js/Utilities.js +7 -8
- package/js/Utilities.js.map +1 -1
- package/package.json +67 -67
- package/src/AcceptanceParameters.ts +4 -4
- package/src/Channel.ts +295 -295
- package/src/ChannelOptions.ts +4 -4
- package/src/ControlCode.ts +35 -35
- package/src/Deferred.ts +75 -75
- package/src/FrameHeader.ts +47 -47
- package/src/FullDuplexStream.ts +32 -32
- package/src/IChannelOfferEventArgs.ts +19 -19
- package/src/IDisposableObservable.ts +4 -4
- package/src/MultiplexingStream.ts +761 -761
- package/src/MultiplexingStreamFormatters.ts +395 -395
- package/src/MultiplexingStreamOptions.ts +21 -21
- package/src/OfferParameters.ts +7 -7
- package/src/QualifiedChannelId.ts +44 -44
- package/src/Utilities.ts +183 -183
- package/src/index.ts +8 -8
package/src/Channel.ts
CHANGED
|
@@ -1,295 +1,295 @@
|
|
|
1
|
-
import CancellationToken from 'cancellationtoken'
|
|
2
|
-
import { Duplex } from 'stream'
|
|
3
|
-
import { AcceptanceParameters } from './AcceptanceParameters'
|
|
4
|
-
import { ChannelOptions } from './ChannelOptions'
|
|
5
|
-
import { ControlCode } from './ControlCode'
|
|
6
|
-
import { Deferred } from './Deferred'
|
|
7
|
-
import { FrameHeader } from './FrameHeader'
|
|
8
|
-
import { IDisposableObservable } from './IDisposableObservable'
|
|
9
|
-
import { MultiplexingStream, MultiplexingStreamClass } from './MultiplexingStream'
|
|
10
|
-
import { OfferParameters } from './OfferParameters'
|
|
11
|
-
import { ChannelSource, QualifiedChannelId } from './QualifiedChannelId'
|
|
12
|
-
import caught = require('caught')
|
|
13
|
-
|
|
14
|
-
export abstract class Channel implements IDisposableObservable {
|
|
15
|
-
/**
|
|
16
|
-
* The id of the channel.
|
|
17
|
-
* @obsolete Use qualifiedId instead.
|
|
18
|
-
*/
|
|
19
|
-
public get id(): number {
|
|
20
|
-
return this.qualifiedId.id
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* The party-qualified ID of the channel.
|
|
25
|
-
*/
|
|
26
|
-
public readonly qualifiedId: QualifiedChannelId
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* A read/write stream used to communicate over this channel.
|
|
30
|
-
*/
|
|
31
|
-
public abstract stream: NodeJS.ReadWriteStream
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* A promise that completes when this channel has been accepted/rejected by the remote party.
|
|
35
|
-
*/
|
|
36
|
-
public abstract acceptance: Promise<void>
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* A promise that completes when this channel is closed.
|
|
40
|
-
*/
|
|
41
|
-
public abstract completion: Promise<void>
|
|
42
|
-
|
|
43
|
-
private _isDisposed: boolean = false
|
|
44
|
-
|
|
45
|
-
constructor(id: QualifiedChannelId) {
|
|
46
|
-
this.qualifiedId = id
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Gets a value indicating whether this channel has been disposed.
|
|
51
|
-
*/
|
|
52
|
-
public get isDisposed(): boolean {
|
|
53
|
-
return this._isDisposed
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Closes this channel.
|
|
58
|
-
* @param error An optional error to send to the remote side, if this multiplexing stream is using protocol versions >= 2.
|
|
59
|
-
*/
|
|
60
|
-
public dispose(error?: Error | null): void {
|
|
61
|
-
// The interesting stuff is in the derived class.
|
|
62
|
-
this._isDisposed = true
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// tslint:disable-next-line:max-classes-per-file
|
|
67
|
-
export class ChannelClass extends Channel {
|
|
68
|
-
public readonly name: string
|
|
69
|
-
private _duplex: Duplex
|
|
70
|
-
private readonly _multiplexingStream: MultiplexingStreamClass
|
|
71
|
-
private readonly _acceptance = new Deferred<void>()
|
|
72
|
-
private readonly _completion = new Deferred<void>()
|
|
73
|
-
public localWindowSize?: number
|
|
74
|
-
private remoteWindowSize?: number
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* The number of bytes transmitted from here but not yet acknowledged as processed from there,
|
|
78
|
-
* and thus occupying some portion of the full AcceptanceParameters.RemoteWindowSize.
|
|
79
|
-
*/
|
|
80
|
-
private remoteWindowFilled: number = 0
|
|
81
|
-
|
|
82
|
-
/** A signal which indicates when the <see cref="RemoteWindowRemaining"/> is non-zero. */
|
|
83
|
-
private remoteWindowHasCapacity: Deferred<void>
|
|
84
|
-
|
|
85
|
-
constructor(multiplexingStream: MultiplexingStreamClass, id: QualifiedChannelId, offerParameters: OfferParameters) {
|
|
86
|
-
super(id)
|
|
87
|
-
const self = this
|
|
88
|
-
this.name = offerParameters.name
|
|
89
|
-
switch (id.source) {
|
|
90
|
-
case ChannelSource.Local:
|
|
91
|
-
this.localWindowSize = offerParameters.remoteWindowSize
|
|
92
|
-
break
|
|
93
|
-
case ChannelSource.Remote:
|
|
94
|
-
this.remoteWindowSize = offerParameters.remoteWindowSize
|
|
95
|
-
break
|
|
96
|
-
case ChannelSource.Seeded:
|
|
97
|
-
this.remoteWindowSize = offerParameters.remoteWindowSize
|
|
98
|
-
this.localWindowSize = offerParameters.remoteWindowSize
|
|
99
|
-
break
|
|
100
|
-
default:
|
|
101
|
-
throw new Error('Channel source not recognized.')
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
this._multiplexingStream = multiplexingStream
|
|
105
|
-
|
|
106
|
-
this.remoteWindowHasCapacity = new Deferred<void>()
|
|
107
|
-
if (!this._multiplexingStream.backpressureSupportEnabled || this.remoteWindowSize) {
|
|
108
|
-
this.remoteWindowHasCapacity.resolve()
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
this._duplex = new Duplex({
|
|
112
|
-
async write(chunk, _, callback) {
|
|
113
|
-
let error
|
|
114
|
-
try {
|
|
115
|
-
let payload = Buffer.from(chunk)
|
|
116
|
-
while (payload.length > 0) {
|
|
117
|
-
// Never transmit more than one frame's worth at a time.
|
|
118
|
-
let bytesTransmitted = Math.min(payload.length, MultiplexingStream.framePayloadMaxLength)
|
|
119
|
-
|
|
120
|
-
// Don't send more than will fit in the remote's receiving window size.
|
|
121
|
-
if (self._multiplexingStream.backpressureSupportEnabled) {
|
|
122
|
-
await self.remoteWindowHasCapacity.promise
|
|
123
|
-
if (!self.remoteWindowSize) {
|
|
124
|
-
throw new Error('Remote window size unknown.')
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
bytesTransmitted = Math.min(self.remoteWindowSize - self.remoteWindowFilled, bytesTransmitted)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
self.onTransmittingBytes(bytesTransmitted)
|
|
131
|
-
const header = new FrameHeader(ControlCode.content, id)
|
|
132
|
-
await multiplexingStream.sendFrameAsync(header, payload.slice(0, bytesTransmitted))
|
|
133
|
-
payload = payload.slice(bytesTransmitted)
|
|
134
|
-
}
|
|
135
|
-
} catch (err) {
|
|
136
|
-
error = err
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (callback) {
|
|
140
|
-
callback(error)
|
|
141
|
-
}
|
|
142
|
-
},
|
|
143
|
-
|
|
144
|
-
async final(cb?: (err?: any) => void) {
|
|
145
|
-
let error
|
|
146
|
-
try {
|
|
147
|
-
await multiplexingStream.onChannelWritingCompleted(self)
|
|
148
|
-
} catch (err) {
|
|
149
|
-
error = err
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (cb) {
|
|
153
|
-
cb(error)
|
|
154
|
-
}
|
|
155
|
-
},
|
|
156
|
-
|
|
157
|
-
read() {
|
|
158
|
-
// Nothing to do here since data is pushed to us.
|
|
159
|
-
},
|
|
160
|
-
})
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
public get stream(): NodeJS.ReadWriteStream {
|
|
164
|
-
return this._duplex
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
public get acceptance(): Promise<void> {
|
|
168
|
-
return this._acceptance.promise
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
public get isAccepted() {
|
|
172
|
-
return this._acceptance.isResolved
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
public get isRejectedOrCanceled() {
|
|
176
|
-
return this._acceptance.isRejected
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
public get completion(): Promise<void> {
|
|
180
|
-
return this._completion.promise
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
public tryAcceptOffer(options?: ChannelOptions): boolean {
|
|
184
|
-
if (this._acceptance.resolve()) {
|
|
185
|
-
this.localWindowSize =
|
|
186
|
-
options?.channelReceivingWindowSize !== undefined
|
|
187
|
-
? Math.max(this._multiplexingStream.defaultChannelReceivingWindowSize, options?.channelReceivingWindowSize)
|
|
188
|
-
: this._multiplexingStream.defaultChannelReceivingWindowSize
|
|
189
|
-
return true
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
return false
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
public tryCancelOffer(reason: any) {
|
|
196
|
-
const cancellationReason = new CancellationToken.CancellationError(reason)
|
|
197
|
-
this._acceptance.reject(cancellationReason)
|
|
198
|
-
this._completion.reject(cancellationReason)
|
|
199
|
-
|
|
200
|
-
// Also mark completion's promise rejections as 'caught' since we do not require
|
|
201
|
-
// or even expect it to be recognized by anyone else.
|
|
202
|
-
// The acceptance promise rejection is observed by the offer channel method.
|
|
203
|
-
caught(this._completion.promise)
|
|
204
|
-
|
|
205
|
-
// Inform the remote side that the offer is rescinded.
|
|
206
|
-
this.dispose()
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
public onAccepted(acceptanceParameter: AcceptanceParameters): boolean {
|
|
210
|
-
if (this._multiplexingStream.backpressureSupportEnabled) {
|
|
211
|
-
this.remoteWindowSize = acceptanceParameter.remoteWindowSize
|
|
212
|
-
this.remoteWindowHasCapacity.resolve()
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
return this._acceptance.resolve()
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
public onContent(buffer: Buffer | null) {
|
|
219
|
-
const priorReadableFlowing = this._duplex.readableFlowing
|
|
220
|
-
|
|
221
|
-
this._duplex.push(buffer)
|
|
222
|
-
|
|
223
|
-
// Large buffer pushes can switch a stream from flowing to non-flowing
|
|
224
|
-
// when it meets or exceeds the highWaterMark. We need to resume the stream
|
|
225
|
-
// in this case so that the user can continue to receive data.
|
|
226
|
-
if (priorReadableFlowing && this._duplex.readableFlowing === false) {
|
|
227
|
-
this._duplex.resume()
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// We should find a way to detect when we *actually* share the received buffer with the Channel's user
|
|
231
|
-
// and only report consumption when they receive the buffer from us so that we effectively apply
|
|
232
|
-
// backpressure to the remote party based on our user's actual consumption rather than continually allocating memory.
|
|
233
|
-
if (this._multiplexingStream.backpressureSupportEnabled && buffer) {
|
|
234
|
-
this._multiplexingStream.localContentExamined(this, buffer.length)
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
public onContentProcessed(bytesProcessed: number) {
|
|
239
|
-
if (bytesProcessed < 0) {
|
|
240
|
-
throw new Error('A non-negative number is required.')
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
if (bytesProcessed > this.remoteWindowFilled) {
|
|
244
|
-
throw new Error('More bytes processed than we thought were in the window.')
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
if (this.remoteWindowSize === undefined) {
|
|
248
|
-
throw new Error("Unexpected content processed message given we don't know the remote window size.")
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
this.remoteWindowFilled -= bytesProcessed
|
|
252
|
-
if (this.remoteWindowFilled < this.remoteWindowSize) {
|
|
253
|
-
this.remoteWindowHasCapacity.resolve()
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
public dispose(error?: Error | null): void {
|
|
258
|
-
if (!this.isDisposed) {
|
|
259
|
-
super.dispose()
|
|
260
|
-
|
|
261
|
-
if (this._acceptance.reject(new CancellationToken.CancellationError('disposed'))) {
|
|
262
|
-
// Don't crash node due to an unnoticed rejection when dispose was explicitly called.
|
|
263
|
-
caught(this.acceptance)
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// For the pipes, we Complete *our* ends, and leave the user's ends alone.
|
|
267
|
-
// The completion will propagate when it's ready to.
|
|
268
|
-
this._duplex.end()
|
|
269
|
-
this._duplex.push(null)
|
|
270
|
-
|
|
271
|
-
if (error) {
|
|
272
|
-
this._completion.reject(error)
|
|
273
|
-
} else {
|
|
274
|
-
this._completion.resolve()
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// Send the notification, but we can't await the result of this.
|
|
278
|
-
caught(this._multiplexingStream.onChannelDisposed(this, error ?? null))
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
private onTransmittingBytes(transmittedBytes: number): void {
|
|
283
|
-
if (this._multiplexingStream.backpressureSupportEnabled) {
|
|
284
|
-
if (transmittedBytes < 0) {
|
|
285
|
-
throw new Error('Negative byte count transmitted.')
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
this.remoteWindowFilled += transmittedBytes
|
|
289
|
-
if (this.remoteWindowFilled === this.remoteWindowSize) {
|
|
290
|
-
// Suspend writing.
|
|
291
|
-
this.remoteWindowHasCapacity = new Deferred<void>()
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
1
|
+
import CancellationToken from 'cancellationtoken'
|
|
2
|
+
import { Duplex } from 'stream'
|
|
3
|
+
import { AcceptanceParameters } from './AcceptanceParameters'
|
|
4
|
+
import { ChannelOptions } from './ChannelOptions'
|
|
5
|
+
import { ControlCode } from './ControlCode'
|
|
6
|
+
import { Deferred } from './Deferred'
|
|
7
|
+
import { FrameHeader } from './FrameHeader'
|
|
8
|
+
import { IDisposableObservable } from './IDisposableObservable'
|
|
9
|
+
import { MultiplexingStream, MultiplexingStreamClass } from './MultiplexingStream'
|
|
10
|
+
import { OfferParameters } from './OfferParameters'
|
|
11
|
+
import { ChannelSource, QualifiedChannelId } from './QualifiedChannelId'
|
|
12
|
+
import caught = require('caught')
|
|
13
|
+
|
|
14
|
+
export abstract class Channel implements IDisposableObservable {
|
|
15
|
+
/**
|
|
16
|
+
* The id of the channel.
|
|
17
|
+
* @obsolete Use qualifiedId instead.
|
|
18
|
+
*/
|
|
19
|
+
public get id(): number {
|
|
20
|
+
return this.qualifiedId.id
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The party-qualified ID of the channel.
|
|
25
|
+
*/
|
|
26
|
+
public readonly qualifiedId: QualifiedChannelId
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A read/write stream used to communicate over this channel.
|
|
30
|
+
*/
|
|
31
|
+
public abstract stream: NodeJS.ReadWriteStream
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A promise that completes when this channel has been accepted/rejected by the remote party.
|
|
35
|
+
*/
|
|
36
|
+
public abstract acceptance: Promise<void>
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A promise that completes when this channel is closed.
|
|
40
|
+
*/
|
|
41
|
+
public abstract completion: Promise<void>
|
|
42
|
+
|
|
43
|
+
private _isDisposed: boolean = false
|
|
44
|
+
|
|
45
|
+
constructor(id: QualifiedChannelId) {
|
|
46
|
+
this.qualifiedId = id
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Gets a value indicating whether this channel has been disposed.
|
|
51
|
+
*/
|
|
52
|
+
public get isDisposed(): boolean {
|
|
53
|
+
return this._isDisposed
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Closes this channel.
|
|
58
|
+
* @param error An optional error to send to the remote side, if this multiplexing stream is using protocol versions >= 2.
|
|
59
|
+
*/
|
|
60
|
+
public dispose(error?: Error | null): void {
|
|
61
|
+
// The interesting stuff is in the derived class.
|
|
62
|
+
this._isDisposed = true
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
67
|
+
export class ChannelClass extends Channel {
|
|
68
|
+
public readonly name: string
|
|
69
|
+
private _duplex: Duplex
|
|
70
|
+
private readonly _multiplexingStream: MultiplexingStreamClass
|
|
71
|
+
private readonly _acceptance = new Deferred<void>()
|
|
72
|
+
private readonly _completion = new Deferred<void>()
|
|
73
|
+
public localWindowSize?: number
|
|
74
|
+
private remoteWindowSize?: number
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The number of bytes transmitted from here but not yet acknowledged as processed from there,
|
|
78
|
+
* and thus occupying some portion of the full AcceptanceParameters.RemoteWindowSize.
|
|
79
|
+
*/
|
|
80
|
+
private remoteWindowFilled: number = 0
|
|
81
|
+
|
|
82
|
+
/** A signal which indicates when the <see cref="RemoteWindowRemaining"/> is non-zero. */
|
|
83
|
+
private remoteWindowHasCapacity: Deferred<void>
|
|
84
|
+
|
|
85
|
+
constructor(multiplexingStream: MultiplexingStreamClass, id: QualifiedChannelId, offerParameters: OfferParameters) {
|
|
86
|
+
super(id)
|
|
87
|
+
const self = this
|
|
88
|
+
this.name = offerParameters.name
|
|
89
|
+
switch (id.source) {
|
|
90
|
+
case ChannelSource.Local:
|
|
91
|
+
this.localWindowSize = offerParameters.remoteWindowSize
|
|
92
|
+
break
|
|
93
|
+
case ChannelSource.Remote:
|
|
94
|
+
this.remoteWindowSize = offerParameters.remoteWindowSize
|
|
95
|
+
break
|
|
96
|
+
case ChannelSource.Seeded:
|
|
97
|
+
this.remoteWindowSize = offerParameters.remoteWindowSize
|
|
98
|
+
this.localWindowSize = offerParameters.remoteWindowSize
|
|
99
|
+
break
|
|
100
|
+
default:
|
|
101
|
+
throw new Error('Channel source not recognized.')
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this._multiplexingStream = multiplexingStream
|
|
105
|
+
|
|
106
|
+
this.remoteWindowHasCapacity = new Deferred<void>()
|
|
107
|
+
if (!this._multiplexingStream.backpressureSupportEnabled || this.remoteWindowSize) {
|
|
108
|
+
this.remoteWindowHasCapacity.resolve()
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
this._duplex = new Duplex({
|
|
112
|
+
async write(chunk, _, callback) {
|
|
113
|
+
let error
|
|
114
|
+
try {
|
|
115
|
+
let payload = Buffer.from(chunk)
|
|
116
|
+
while (payload.length > 0) {
|
|
117
|
+
// Never transmit more than one frame's worth at a time.
|
|
118
|
+
let bytesTransmitted = Math.min(payload.length, MultiplexingStream.framePayloadMaxLength)
|
|
119
|
+
|
|
120
|
+
// Don't send more than will fit in the remote's receiving window size.
|
|
121
|
+
if (self._multiplexingStream.backpressureSupportEnabled) {
|
|
122
|
+
await self.remoteWindowHasCapacity.promise
|
|
123
|
+
if (!self.remoteWindowSize) {
|
|
124
|
+
throw new Error('Remote window size unknown.')
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
bytesTransmitted = Math.min(self.remoteWindowSize - self.remoteWindowFilled, bytesTransmitted)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
self.onTransmittingBytes(bytesTransmitted)
|
|
131
|
+
const header = new FrameHeader(ControlCode.content, id)
|
|
132
|
+
await multiplexingStream.sendFrameAsync(header, payload.slice(0, bytesTransmitted))
|
|
133
|
+
payload = payload.slice(bytesTransmitted)
|
|
134
|
+
}
|
|
135
|
+
} catch (err) {
|
|
136
|
+
error = err
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (callback) {
|
|
140
|
+
callback(error)
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
async final(cb?: (err?: any) => void) {
|
|
145
|
+
let error
|
|
146
|
+
try {
|
|
147
|
+
await multiplexingStream.onChannelWritingCompleted(self)
|
|
148
|
+
} catch (err) {
|
|
149
|
+
error = err
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (cb) {
|
|
153
|
+
cb(error)
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
read() {
|
|
158
|
+
// Nothing to do here since data is pushed to us.
|
|
159
|
+
},
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
public get stream(): NodeJS.ReadWriteStream {
|
|
164
|
+
return this._duplex
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
public get acceptance(): Promise<void> {
|
|
168
|
+
return this._acceptance.promise
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
public get isAccepted() {
|
|
172
|
+
return this._acceptance.isResolved
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
public get isRejectedOrCanceled() {
|
|
176
|
+
return this._acceptance.isRejected
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
public get completion(): Promise<void> {
|
|
180
|
+
return this._completion.promise
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
public tryAcceptOffer(options?: ChannelOptions): boolean {
|
|
184
|
+
if (this._acceptance.resolve()) {
|
|
185
|
+
this.localWindowSize =
|
|
186
|
+
options?.channelReceivingWindowSize !== undefined
|
|
187
|
+
? Math.max(this._multiplexingStream.defaultChannelReceivingWindowSize, options?.channelReceivingWindowSize)
|
|
188
|
+
: this._multiplexingStream.defaultChannelReceivingWindowSize
|
|
189
|
+
return true
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return false
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
public tryCancelOffer(reason: any) {
|
|
196
|
+
const cancellationReason = new CancellationToken.CancellationError(reason)
|
|
197
|
+
this._acceptance.reject(cancellationReason)
|
|
198
|
+
this._completion.reject(cancellationReason)
|
|
199
|
+
|
|
200
|
+
// Also mark completion's promise rejections as 'caught' since we do not require
|
|
201
|
+
// or even expect it to be recognized by anyone else.
|
|
202
|
+
// The acceptance promise rejection is observed by the offer channel method.
|
|
203
|
+
caught(this._completion.promise)
|
|
204
|
+
|
|
205
|
+
// Inform the remote side that the offer is rescinded.
|
|
206
|
+
this.dispose()
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
public onAccepted(acceptanceParameter: AcceptanceParameters): boolean {
|
|
210
|
+
if (this._multiplexingStream.backpressureSupportEnabled) {
|
|
211
|
+
this.remoteWindowSize = acceptanceParameter.remoteWindowSize
|
|
212
|
+
this.remoteWindowHasCapacity.resolve()
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return this._acceptance.resolve()
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
public onContent(buffer: Buffer | null) {
|
|
219
|
+
const priorReadableFlowing = this._duplex.readableFlowing
|
|
220
|
+
|
|
221
|
+
this._duplex.push(buffer)
|
|
222
|
+
|
|
223
|
+
// Large buffer pushes can switch a stream from flowing to non-flowing
|
|
224
|
+
// when it meets or exceeds the highWaterMark. We need to resume the stream
|
|
225
|
+
// in this case so that the user can continue to receive data.
|
|
226
|
+
if (priorReadableFlowing && this._duplex.readableFlowing === false) {
|
|
227
|
+
this._duplex.resume()
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// We should find a way to detect when we *actually* share the received buffer with the Channel's user
|
|
231
|
+
// and only report consumption when they receive the buffer from us so that we effectively apply
|
|
232
|
+
// backpressure to the remote party based on our user's actual consumption rather than continually allocating memory.
|
|
233
|
+
if (this._multiplexingStream.backpressureSupportEnabled && buffer) {
|
|
234
|
+
this._multiplexingStream.localContentExamined(this, buffer.length)
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
public onContentProcessed(bytesProcessed: number) {
|
|
239
|
+
if (bytesProcessed < 0) {
|
|
240
|
+
throw new Error('A non-negative number is required.')
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (bytesProcessed > this.remoteWindowFilled) {
|
|
244
|
+
throw new Error('More bytes processed than we thought were in the window.')
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (this.remoteWindowSize === undefined) {
|
|
248
|
+
throw new Error("Unexpected content processed message given we don't know the remote window size.")
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
this.remoteWindowFilled -= bytesProcessed
|
|
252
|
+
if (this.remoteWindowFilled < this.remoteWindowSize) {
|
|
253
|
+
this.remoteWindowHasCapacity.resolve()
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
public dispose(error?: Error | null): void {
|
|
258
|
+
if (!this.isDisposed) {
|
|
259
|
+
super.dispose()
|
|
260
|
+
|
|
261
|
+
if (this._acceptance.reject(new CancellationToken.CancellationError('disposed'))) {
|
|
262
|
+
// Don't crash node due to an unnoticed rejection when dispose was explicitly called.
|
|
263
|
+
caught(this.acceptance)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// For the pipes, we Complete *our* ends, and leave the user's ends alone.
|
|
267
|
+
// The completion will propagate when it's ready to.
|
|
268
|
+
this._duplex.end()
|
|
269
|
+
this._duplex.push(null)
|
|
270
|
+
|
|
271
|
+
if (error) {
|
|
272
|
+
this._completion.reject(error)
|
|
273
|
+
} else {
|
|
274
|
+
this._completion.resolve()
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Send the notification, but we can't await the result of this.
|
|
278
|
+
caught(this._multiplexingStream.onChannelDisposed(this, error ?? null))
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
private onTransmittingBytes(transmittedBytes: number): void {
|
|
283
|
+
if (this._multiplexingStream.backpressureSupportEnabled) {
|
|
284
|
+
if (transmittedBytes < 0) {
|
|
285
|
+
throw new Error('Negative byte count transmitted.')
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
this.remoteWindowFilled += transmittedBytes
|
|
289
|
+
if (this.remoteWindowFilled === this.remoteWindowSize) {
|
|
290
|
+
// Suspend writing.
|
|
291
|
+
this.remoteWindowHasCapacity = new Deferred<void>()
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
package/src/ChannelOptions.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
|
+
}
|
package/src/ControlCode.ts
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Signals what kind of frame is being transmitted.
|
|
3
|
-
*/
|
|
4
|
-
export enum ControlCode {
|
|
5
|
-
/**
|
|
6
|
-
* A channel is proposed to the remote party.
|
|
7
|
-
*/
|
|
8
|
-
offer,
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* A channel proposal has been accepted.
|
|
12
|
-
*/
|
|
13
|
-
offerAccepted,
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* The payload of the frame is a payload intended for channel consumption.
|
|
17
|
-
*/
|
|
18
|
-
content,
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Sent after all bytes have been transmitted on a given channel. Either or both sides may send this.
|
|
22
|
-
* A channel may be automatically closed when each side has both transmitted and received this message.
|
|
23
|
-
*/
|
|
24
|
-
contentWritingCompleted,
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Sent when a channel is closed, an incoming offer is rejected, or an outgoing offer is canceled.
|
|
28
|
-
*/
|
|
29
|
-
channelTerminated,
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Sent when a channel has finished processing data received from the remote party, allowing them to send more data.
|
|
33
|
-
*/
|
|
34
|
-
contentProcessed,
|
|
35
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Signals what kind of frame is being transmitted.
|
|
3
|
+
*/
|
|
4
|
+
export enum ControlCode {
|
|
5
|
+
/**
|
|
6
|
+
* A channel is proposed to the remote party.
|
|
7
|
+
*/
|
|
8
|
+
offer,
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A channel proposal has been accepted.
|
|
12
|
+
*/
|
|
13
|
+
offerAccepted,
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The payload of the frame is a payload intended for channel consumption.
|
|
17
|
+
*/
|
|
18
|
+
content,
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Sent after all bytes have been transmitted on a given channel. Either or both sides may send this.
|
|
22
|
+
* A channel may be automatically closed when each side has both transmitted and received this message.
|
|
23
|
+
*/
|
|
24
|
+
contentWritingCompleted,
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Sent when a channel is closed, an incoming offer is rejected, or an outgoing offer is canceled.
|
|
28
|
+
*/
|
|
29
|
+
channelTerminated,
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Sent when a channel has finished processing data received from the remote party, allowing them to send more data.
|
|
33
|
+
*/
|
|
34
|
+
contentProcessed,
|
|
35
|
+
}
|