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
|
@@ -1,395 +1,395 @@
|
|
|
1
|
-
import { OfferParameters } from './OfferParameters'
|
|
2
|
-
import { AcceptanceParameters } from './AcceptanceParameters'
|
|
3
|
-
import { MultiplexingStream } from './MultiplexingStream'
|
|
4
|
-
import { getBufferFrom, writeAsync } from './Utilities'
|
|
5
|
-
import CancellationToken from 'cancellationtoken'
|
|
6
|
-
import * as msgpack from 'msgpack-lite'
|
|
7
|
-
import { Deferred } from './Deferred'
|
|
8
|
-
import { FrameHeader } from './FrameHeader'
|
|
9
|
-
import { ControlCode } from './ControlCode'
|
|
10
|
-
import { ChannelSource } from './QualifiedChannelId'
|
|
11
|
-
|
|
12
|
-
export interface Version {
|
|
13
|
-
major: number
|
|
14
|
-
minor: number
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface HandshakeResult {
|
|
18
|
-
isOdd?: boolean
|
|
19
|
-
protocolVersion: Version
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export abstract class MultiplexingStreamFormatter {
|
|
23
|
-
isOdd?: boolean
|
|
24
|
-
|
|
25
|
-
abstract writeHandshakeAsync(): Promise<Buffer | null>
|
|
26
|
-
abstract readHandshakeAsync(writeHandshakeResult: Buffer | null, cancellationToken: CancellationToken): Promise<HandshakeResult>
|
|
27
|
-
|
|
28
|
-
abstract writeFrameAsync(header: FrameHeader, payload?: Buffer): Promise<void>
|
|
29
|
-
abstract readFrameAsync(cancellationToken: CancellationToken): Promise<{ header: FrameHeader; payload: Buffer } | null>
|
|
30
|
-
|
|
31
|
-
abstract serializeOfferParameters(offer: OfferParameters): Buffer
|
|
32
|
-
abstract deserializeOfferParameters(payload: Buffer): OfferParameters
|
|
33
|
-
|
|
34
|
-
abstract serializeAcceptanceParameters(acceptance: AcceptanceParameters): Buffer
|
|
35
|
-
abstract deserializeAcceptanceParameters(payload: Buffer): AcceptanceParameters
|
|
36
|
-
|
|
37
|
-
abstract serializeContentProcessed(bytesProcessed: number): Buffer
|
|
38
|
-
abstract deserializeContentProcessed(payload: Buffer): number
|
|
39
|
-
|
|
40
|
-
abstract end(): void
|
|
41
|
-
|
|
42
|
-
protected static getIsOddRandomData(): Buffer {
|
|
43
|
-
const size = 16
|
|
44
|
-
const buffer = Buffer.alloc(size)
|
|
45
|
-
|
|
46
|
-
for (let i = 0; i < size; i++) {
|
|
47
|
-
buffer[i] = Math.floor(Math.random() * 256)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return buffer
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
protected static isOdd(localRandom: Buffer, remoteRandom: Buffer): boolean {
|
|
54
|
-
let isOdd: boolean | undefined
|
|
55
|
-
for (let i = 0; i < localRandom.length; i++) {
|
|
56
|
-
const sent = localRandom[i]
|
|
57
|
-
const recv = remoteRandom[i]
|
|
58
|
-
if (sent > recv) {
|
|
59
|
-
isOdd = true
|
|
60
|
-
break
|
|
61
|
-
} else if (sent < recv) {
|
|
62
|
-
isOdd = false
|
|
63
|
-
break
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (isOdd === undefined) {
|
|
68
|
-
throw new Error('Unable to determine even/odd party.')
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return isOdd
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
protected createFrameHeader(code: ControlCode, id: number | undefined): FrameHeader {
|
|
75
|
-
if (!id) {
|
|
76
|
-
return new FrameHeader(code)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const channelIsOdd = id % 2 === 1
|
|
80
|
-
|
|
81
|
-
// Remember that this is from the remote sender's point of view.
|
|
82
|
-
const source = channelIsOdd === this.isOdd ? ChannelSource.Remote : ChannelSource.Local
|
|
83
|
-
return new FrameHeader(code, { id, source })
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// tslint:disable-next-line: max-classes-per-file
|
|
88
|
-
export class MultiplexingStreamV1Formatter extends MultiplexingStreamFormatter {
|
|
89
|
-
/**
|
|
90
|
-
* The magic number to send at the start of communication when using v1 of the protocol.
|
|
91
|
-
*/
|
|
92
|
-
private static readonly protocolMagicNumber = Buffer.from([0x2f, 0xdf, 0x1d, 0x50])
|
|
93
|
-
|
|
94
|
-
constructor(private readonly stream: NodeJS.ReadWriteStream) {
|
|
95
|
-
super()
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
end() {
|
|
99
|
-
this.stream.end()
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
async writeHandshakeAsync(): Promise<Buffer> {
|
|
103
|
-
const randomSendBuffer = MultiplexingStreamFormatter.getIsOddRandomData()
|
|
104
|
-
const sendBuffer = Buffer.concat([MultiplexingStreamV1Formatter.protocolMagicNumber, randomSendBuffer])
|
|
105
|
-
await writeAsync(this.stream, sendBuffer)
|
|
106
|
-
return randomSendBuffer
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
async readHandshakeAsync(writeHandshakeResult: Buffer, cancellationToken: CancellationToken): Promise<HandshakeResult> {
|
|
110
|
-
const localRandomBuffer = writeHandshakeResult as Buffer
|
|
111
|
-
const recvBuffer = await getBufferFrom(this.stream, MultiplexingStreamV1Formatter.protocolMagicNumber.length + 16, false, cancellationToken)
|
|
112
|
-
|
|
113
|
-
for (let i = 0; i < MultiplexingStreamV1Formatter.protocolMagicNumber.length; i++) {
|
|
114
|
-
const expected = MultiplexingStreamV1Formatter.protocolMagicNumber[i]
|
|
115
|
-
const actual = recvBuffer.readUInt8(i)
|
|
116
|
-
if (expected !== actual) {
|
|
117
|
-
throw new Error(`Protocol magic number mismatch. Expected ${expected} but was ${actual}.`)
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const isOdd = MultiplexingStreamFormatter.isOdd(localRandomBuffer, recvBuffer.slice(MultiplexingStreamV1Formatter.protocolMagicNumber.length))
|
|
122
|
-
|
|
123
|
-
return { isOdd, protocolVersion: { major: 1, minor: 0 } }
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
async writeFrameAsync(header: FrameHeader, payload?: Buffer): Promise<void> {
|
|
127
|
-
const headerBuffer = Buffer.alloc(7)
|
|
128
|
-
headerBuffer.writeInt8(header.code, 0)
|
|
129
|
-
headerBuffer.writeUInt32BE(header.channel?.id || 0, 1)
|
|
130
|
-
headerBuffer.writeUInt16BE(payload?.length || 0, 5)
|
|
131
|
-
await writeAsync(this.stream, headerBuffer)
|
|
132
|
-
if (payload && payload.length > 0) {
|
|
133
|
-
await writeAsync(this.stream, payload)
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
async readFrameAsync(cancellationToken: CancellationToken): Promise<{ header: FrameHeader; payload: Buffer } | null> {
|
|
138
|
-
if (this.isOdd === undefined) {
|
|
139
|
-
throw new Error('isOdd must be set first.')
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const headerBuffer = await getBufferFrom(this.stream, 7, true, cancellationToken)
|
|
143
|
-
if (headerBuffer === null) {
|
|
144
|
-
return null
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const header = this.createFrameHeader(headerBuffer.readInt8(0), headerBuffer.readUInt32BE(1))
|
|
148
|
-
const payloadLength = headerBuffer.readUInt16BE(5)
|
|
149
|
-
const payload = await getBufferFrom(this.stream, payloadLength)
|
|
150
|
-
return { header, payload }
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
serializeOfferParameters(offer: OfferParameters): Buffer {
|
|
154
|
-
const payload = Buffer.from(offer.name, MultiplexingStream.ControlFrameEncoding)
|
|
155
|
-
if (payload.length > MultiplexingStream.framePayloadMaxLength) {
|
|
156
|
-
throw new Error('Name is too long.')
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return payload
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
deserializeOfferParameters(payload: Buffer): OfferParameters {
|
|
163
|
-
return {
|
|
164
|
-
name: payload.toString(MultiplexingStream.ControlFrameEncoding),
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
serializeAcceptanceParameters(_: AcceptanceParameters): Buffer {
|
|
169
|
-
return Buffer.from([])
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
deserializeAcceptanceParameters(_: Buffer): AcceptanceParameters {
|
|
173
|
-
return {}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
serializeContentProcessed(bytesProcessed: number): Buffer {
|
|
177
|
-
throw new Error('Not supported in the V1 protocol.')
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
deserializeContentProcessed(payload: Buffer): number {
|
|
181
|
-
throw new Error('Not supported in the V1 protocol.')
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// tslint:disable-next-line: max-classes-per-file
|
|
186
|
-
export class MultiplexingStreamV2Formatter extends MultiplexingStreamFormatter {
|
|
187
|
-
private static readonly protocolVersion: Version = { major: 2, minor: 0 }
|
|
188
|
-
private readonly reader: msgpack.DecodeStream
|
|
189
|
-
protected readonly writer: NodeJS.WritableStream
|
|
190
|
-
|
|
191
|
-
constructor(stream: NodeJS.ReadWriteStream) {
|
|
192
|
-
super()
|
|
193
|
-
this.reader = msgpack.createDecodeStream()
|
|
194
|
-
stream.pipe(this.reader)
|
|
195
|
-
|
|
196
|
-
this.writer = stream
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
end() {
|
|
200
|
-
this.writer.end()
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
async writeHandshakeAsync(): Promise<Buffer | null> {
|
|
204
|
-
const randomData = MultiplexingStreamFormatter.getIsOddRandomData()
|
|
205
|
-
const msgpackObject = [[MultiplexingStreamV2Formatter.protocolVersion.major, MultiplexingStreamV2Formatter.protocolVersion.minor], randomData]
|
|
206
|
-
await writeAsync(this.writer, msgpack.encode(msgpackObject))
|
|
207
|
-
return randomData
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
async readHandshakeAsync(writeHandshakeResult: Buffer | null, cancellationToken: CancellationToken): Promise<HandshakeResult> {
|
|
211
|
-
if (!writeHandshakeResult) {
|
|
212
|
-
throw new Error('Provide the result of writeHandshakeAsync as a first argument.')
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const handshake = await this.readMessagePackAsync(cancellationToken)
|
|
216
|
-
if (handshake === null) {
|
|
217
|
-
throw new Error('No data received during handshake.')
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
return {
|
|
221
|
-
isOdd: MultiplexingStreamFormatter.isOdd(writeHandshakeResult, handshake[1]),
|
|
222
|
-
protocolVersion: { major: handshake[0][0], minor: handshake[0][1] },
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
async writeFrameAsync(header: FrameHeader, payload?: Buffer): Promise<void> {
|
|
227
|
-
const msgpackObject: any[] = [header.code]
|
|
228
|
-
if (header.channel?.id) {
|
|
229
|
-
msgpackObject.push(header.channel.id)
|
|
230
|
-
if (payload && payload.length > 0) {
|
|
231
|
-
msgpackObject.push(payload)
|
|
232
|
-
}
|
|
233
|
-
} else if (payload && payload.length > 0) {
|
|
234
|
-
throw new Error('A frame may not contain payload without a channel ID.')
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
await writeAsync(this.writer, msgpack.encode(msgpackObject))
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
async readFrameAsync(cancellationToken: CancellationToken): Promise<{ header: FrameHeader; payload: Buffer } | null> {
|
|
241
|
-
if (this.isOdd === undefined) {
|
|
242
|
-
throw new Error('isOdd must be set first.')
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
const msgpackObject = (await this.readMessagePackAsync(cancellationToken)) as [ControlCode, number, Buffer] | null
|
|
246
|
-
if (msgpackObject === null) {
|
|
247
|
-
return null
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
const header = this.createFrameHeader(msgpackObject[0], msgpackObject[1])
|
|
251
|
-
return {
|
|
252
|
-
header,
|
|
253
|
-
payload: msgpackObject[2] || Buffer.from([]),
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
serializeOfferParameters(offer: OfferParameters): Buffer {
|
|
258
|
-
const payload: any[] = [offer.name]
|
|
259
|
-
if (offer.remoteWindowSize) {
|
|
260
|
-
payload.push(offer.remoteWindowSize)
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
return msgpack.encode(payload)
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
deserializeOfferParameters(payload: Buffer): OfferParameters {
|
|
267
|
-
const msgpackObject = msgpack.decode(payload)
|
|
268
|
-
return {
|
|
269
|
-
name: msgpackObject[0],
|
|
270
|
-
remoteWindowSize: msgpackObject[1],
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
serializeAcceptanceParameters(acceptance: AcceptanceParameters): Buffer {
|
|
275
|
-
const payload: any[] = []
|
|
276
|
-
if (acceptance.remoteWindowSize) {
|
|
277
|
-
payload.push(acceptance.remoteWindowSize)
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
return msgpack.encode(payload)
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
deserializeAcceptanceParameters(payload: Buffer): AcceptanceParameters {
|
|
284
|
-
const msgpackObject = msgpack.decode(payload)
|
|
285
|
-
return {
|
|
286
|
-
remoteWindowSize: msgpackObject[0],
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
serializeContentProcessed(bytesProcessed: number): Buffer {
|
|
291
|
-
return msgpack.encode([bytesProcessed])
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
deserializeContentProcessed(payload: Buffer): number {
|
|
295
|
-
return msgpack.decode(payload)[0]
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
serializeException(error: Error | null): Buffer {
|
|
299
|
-
// If the error doesn't exist then return an empty buffer.
|
|
300
|
-
if (!error) {
|
|
301
|
-
return Buffer.alloc(0)
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
const errorMsg: string = `${error.name}: ${error.message}`
|
|
305
|
-
const payload: any[] = [errorMsg]
|
|
306
|
-
return msgpack.encode(payload)
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
deserializeException(payload: Buffer): Error | null {
|
|
310
|
-
// If the payload is empty then return null.
|
|
311
|
-
if (payload.length === 0) {
|
|
312
|
-
return null
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// Make sure that the message pack object contains a message
|
|
316
|
-
const msgpackObject = msgpack.decode(payload)
|
|
317
|
-
if (!msgpackObject || msgpackObject.length === 0) {
|
|
318
|
-
return null
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// Get error message and return the error to the remote side
|
|
322
|
-
let errorMsg: string = msgpack.decode(payload)[0]
|
|
323
|
-
errorMsg = `Received error from remote side: ${errorMsg}`
|
|
324
|
-
return new Error(errorMsg)
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
protected async readMessagePackAsync(cancellationToken: CancellationToken): Promise<{} | [] | null> {
|
|
328
|
-
const streamEnded = new Deferred<void>()
|
|
329
|
-
while (true) {
|
|
330
|
-
const readObject = this.reader.read()
|
|
331
|
-
if (readObject === null) {
|
|
332
|
-
const bytesAvailable = new Deferred<void>()
|
|
333
|
-
const bytesAvailableCallback = bytesAvailable.resolve.bind(bytesAvailable)
|
|
334
|
-
const streamEndedCallback = streamEnded.resolve.bind(streamEnded)
|
|
335
|
-
|
|
336
|
-
this.reader.once('readable', bytesAvailableCallback)
|
|
337
|
-
this.reader.once('end', streamEndedCallback)
|
|
338
|
-
const endPromise = Promise.race([bytesAvailable.promise, streamEnded.promise])
|
|
339
|
-
await (cancellationToken ? cancellationToken.racePromise(endPromise) : endPromise)
|
|
340
|
-
|
|
341
|
-
this.reader.removeListener('readable', bytesAvailableCallback)
|
|
342
|
-
this.reader.removeListener('end', streamEndedCallback)
|
|
343
|
-
|
|
344
|
-
if (bytesAvailable.isCompleted) {
|
|
345
|
-
continue
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
return null
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
return readObject
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
// tslint:disable-next-line: max-classes-per-file
|
|
357
|
-
export class MultiplexingStreamV3Formatter extends MultiplexingStreamV2Formatter {
|
|
358
|
-
private static readonly protocolV3Version: Version = { major: 2, minor: 0 }
|
|
359
|
-
|
|
360
|
-
writeHandshakeAsync(): Promise<null> {
|
|
361
|
-
return Promise.resolve(null)
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
readHandshakeAsync(): Promise<HandshakeResult> {
|
|
365
|
-
return Promise.resolve({ protocolVersion: MultiplexingStreamV3Formatter.protocolV3Version })
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
async writeFrameAsync(header: FrameHeader, payload?: Buffer): Promise<void> {
|
|
369
|
-
const msgpackObject: any[] = [header.code]
|
|
370
|
-
if (header.channel) {
|
|
371
|
-
msgpackObject.push(header.channel.id)
|
|
372
|
-
msgpackObject.push(header.channel.source)
|
|
373
|
-
if (payload && payload.length > 0) {
|
|
374
|
-
msgpackObject.push(payload)
|
|
375
|
-
}
|
|
376
|
-
} else if (payload && payload.length > 0) {
|
|
377
|
-
throw new Error('A frame may not contain payload without a channel ID.')
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
await writeAsync(this.writer, msgpack.encode(msgpackObject))
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
async readFrameAsync(cancellationToken: CancellationToken): Promise<{ header: FrameHeader; payload: Buffer } | null> {
|
|
384
|
-
const msgpackObject = (await this.readMessagePackAsync(cancellationToken)) as [ControlCode, number, ChannelSource, Buffer] | null
|
|
385
|
-
if (msgpackObject === null) {
|
|
386
|
-
return null
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
const header = new FrameHeader(msgpackObject[0], msgpackObject.length > 1 ? { id: msgpackObject[1], source: msgpackObject[2] } : undefined)
|
|
390
|
-
return {
|
|
391
|
-
header,
|
|
392
|
-
payload: msgpackObject[3] || Buffer.from([]),
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
}
|
|
1
|
+
import { OfferParameters } from './OfferParameters'
|
|
2
|
+
import { AcceptanceParameters } from './AcceptanceParameters'
|
|
3
|
+
import { MultiplexingStream } from './MultiplexingStream'
|
|
4
|
+
import { getBufferFrom, writeAsync } from './Utilities'
|
|
5
|
+
import CancellationToken from 'cancellationtoken'
|
|
6
|
+
import * as msgpack from 'msgpack-lite'
|
|
7
|
+
import { Deferred } from './Deferred'
|
|
8
|
+
import { FrameHeader } from './FrameHeader'
|
|
9
|
+
import { ControlCode } from './ControlCode'
|
|
10
|
+
import { ChannelSource } from './QualifiedChannelId'
|
|
11
|
+
|
|
12
|
+
export interface Version {
|
|
13
|
+
major: number
|
|
14
|
+
minor: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface HandshakeResult {
|
|
18
|
+
isOdd?: boolean
|
|
19
|
+
protocolVersion: Version
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export abstract class MultiplexingStreamFormatter {
|
|
23
|
+
isOdd?: boolean
|
|
24
|
+
|
|
25
|
+
abstract writeHandshakeAsync(): Promise<Buffer | null>
|
|
26
|
+
abstract readHandshakeAsync(writeHandshakeResult: Buffer | null, cancellationToken: CancellationToken): Promise<HandshakeResult>
|
|
27
|
+
|
|
28
|
+
abstract writeFrameAsync(header: FrameHeader, payload?: Buffer): Promise<void>
|
|
29
|
+
abstract readFrameAsync(cancellationToken: CancellationToken): Promise<{ header: FrameHeader; payload: Buffer } | null>
|
|
30
|
+
|
|
31
|
+
abstract serializeOfferParameters(offer: OfferParameters): Buffer
|
|
32
|
+
abstract deserializeOfferParameters(payload: Buffer): OfferParameters
|
|
33
|
+
|
|
34
|
+
abstract serializeAcceptanceParameters(acceptance: AcceptanceParameters): Buffer
|
|
35
|
+
abstract deserializeAcceptanceParameters(payload: Buffer): AcceptanceParameters
|
|
36
|
+
|
|
37
|
+
abstract serializeContentProcessed(bytesProcessed: number): Buffer
|
|
38
|
+
abstract deserializeContentProcessed(payload: Buffer): number
|
|
39
|
+
|
|
40
|
+
abstract end(): void
|
|
41
|
+
|
|
42
|
+
protected static getIsOddRandomData(): Buffer {
|
|
43
|
+
const size = 16
|
|
44
|
+
const buffer = Buffer.alloc(size)
|
|
45
|
+
|
|
46
|
+
for (let i = 0; i < size; i++) {
|
|
47
|
+
buffer[i] = Math.floor(Math.random() * 256)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return buffer
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected static isOdd(localRandom: Buffer, remoteRandom: Buffer): boolean {
|
|
54
|
+
let isOdd: boolean | undefined
|
|
55
|
+
for (let i = 0; i < localRandom.length; i++) {
|
|
56
|
+
const sent = localRandom[i]
|
|
57
|
+
const recv = remoteRandom[i]
|
|
58
|
+
if (sent > recv) {
|
|
59
|
+
isOdd = true
|
|
60
|
+
break
|
|
61
|
+
} else if (sent < recv) {
|
|
62
|
+
isOdd = false
|
|
63
|
+
break
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (isOdd === undefined) {
|
|
68
|
+
throw new Error('Unable to determine even/odd party.')
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return isOdd
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
protected createFrameHeader(code: ControlCode, id: number | undefined): FrameHeader {
|
|
75
|
+
if (!id) {
|
|
76
|
+
return new FrameHeader(code)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const channelIsOdd = id % 2 === 1
|
|
80
|
+
|
|
81
|
+
// Remember that this is from the remote sender's point of view.
|
|
82
|
+
const source = channelIsOdd === this.isOdd ? ChannelSource.Remote : ChannelSource.Local
|
|
83
|
+
return new FrameHeader(code, { id, source })
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// tslint:disable-next-line: max-classes-per-file
|
|
88
|
+
export class MultiplexingStreamV1Formatter extends MultiplexingStreamFormatter {
|
|
89
|
+
/**
|
|
90
|
+
* The magic number to send at the start of communication when using v1 of the protocol.
|
|
91
|
+
*/
|
|
92
|
+
private static readonly protocolMagicNumber = Buffer.from([0x2f, 0xdf, 0x1d, 0x50])
|
|
93
|
+
|
|
94
|
+
constructor(private readonly stream: NodeJS.ReadWriteStream) {
|
|
95
|
+
super()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
end() {
|
|
99
|
+
this.stream.end()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async writeHandshakeAsync(): Promise<Buffer> {
|
|
103
|
+
const randomSendBuffer = MultiplexingStreamFormatter.getIsOddRandomData()
|
|
104
|
+
const sendBuffer = Buffer.concat([MultiplexingStreamV1Formatter.protocolMagicNumber, randomSendBuffer])
|
|
105
|
+
await writeAsync(this.stream, sendBuffer)
|
|
106
|
+
return randomSendBuffer
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async readHandshakeAsync(writeHandshakeResult: Buffer, cancellationToken: CancellationToken): Promise<HandshakeResult> {
|
|
110
|
+
const localRandomBuffer = writeHandshakeResult as Buffer
|
|
111
|
+
const recvBuffer = await getBufferFrom(this.stream, MultiplexingStreamV1Formatter.protocolMagicNumber.length + 16, false, cancellationToken)
|
|
112
|
+
|
|
113
|
+
for (let i = 0; i < MultiplexingStreamV1Formatter.protocolMagicNumber.length; i++) {
|
|
114
|
+
const expected = MultiplexingStreamV1Formatter.protocolMagicNumber[i]
|
|
115
|
+
const actual = recvBuffer.readUInt8(i)
|
|
116
|
+
if (expected !== actual) {
|
|
117
|
+
throw new Error(`Protocol magic number mismatch. Expected ${expected} but was ${actual}.`)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const isOdd = MultiplexingStreamFormatter.isOdd(localRandomBuffer, recvBuffer.slice(MultiplexingStreamV1Formatter.protocolMagicNumber.length))
|
|
122
|
+
|
|
123
|
+
return { isOdd, protocolVersion: { major: 1, minor: 0 } }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async writeFrameAsync(header: FrameHeader, payload?: Buffer): Promise<void> {
|
|
127
|
+
const headerBuffer = Buffer.alloc(7)
|
|
128
|
+
headerBuffer.writeInt8(header.code, 0)
|
|
129
|
+
headerBuffer.writeUInt32BE(header.channel?.id || 0, 1)
|
|
130
|
+
headerBuffer.writeUInt16BE(payload?.length || 0, 5)
|
|
131
|
+
await writeAsync(this.stream, headerBuffer)
|
|
132
|
+
if (payload && payload.length > 0) {
|
|
133
|
+
await writeAsync(this.stream, payload)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async readFrameAsync(cancellationToken: CancellationToken): Promise<{ header: FrameHeader; payload: Buffer } | null> {
|
|
138
|
+
if (this.isOdd === undefined) {
|
|
139
|
+
throw new Error('isOdd must be set first.')
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const headerBuffer = await getBufferFrom(this.stream, 7, true, cancellationToken)
|
|
143
|
+
if (headerBuffer === null) {
|
|
144
|
+
return null
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const header = this.createFrameHeader(headerBuffer.readInt8(0), headerBuffer.readUInt32BE(1))
|
|
148
|
+
const payloadLength = headerBuffer.readUInt16BE(5)
|
|
149
|
+
const payload = await getBufferFrom(this.stream, payloadLength)
|
|
150
|
+
return { header, payload }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
serializeOfferParameters(offer: OfferParameters): Buffer {
|
|
154
|
+
const payload = Buffer.from(offer.name, MultiplexingStream.ControlFrameEncoding)
|
|
155
|
+
if (payload.length > MultiplexingStream.framePayloadMaxLength) {
|
|
156
|
+
throw new Error('Name is too long.')
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return payload
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
deserializeOfferParameters(payload: Buffer): OfferParameters {
|
|
163
|
+
return {
|
|
164
|
+
name: payload.toString(MultiplexingStream.ControlFrameEncoding),
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
serializeAcceptanceParameters(_: AcceptanceParameters): Buffer {
|
|
169
|
+
return Buffer.from([])
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
deserializeAcceptanceParameters(_: Buffer): AcceptanceParameters {
|
|
173
|
+
return {}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
serializeContentProcessed(bytesProcessed: number): Buffer {
|
|
177
|
+
throw new Error('Not supported in the V1 protocol.')
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
deserializeContentProcessed(payload: Buffer): number {
|
|
181
|
+
throw new Error('Not supported in the V1 protocol.')
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// tslint:disable-next-line: max-classes-per-file
|
|
186
|
+
export class MultiplexingStreamV2Formatter extends MultiplexingStreamFormatter {
|
|
187
|
+
private static readonly protocolVersion: Version = { major: 2, minor: 0 }
|
|
188
|
+
private readonly reader: msgpack.DecodeStream
|
|
189
|
+
protected readonly writer: NodeJS.WritableStream
|
|
190
|
+
|
|
191
|
+
constructor(stream: NodeJS.ReadWriteStream) {
|
|
192
|
+
super()
|
|
193
|
+
this.reader = msgpack.createDecodeStream()
|
|
194
|
+
stream.pipe(this.reader)
|
|
195
|
+
|
|
196
|
+
this.writer = stream
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
end() {
|
|
200
|
+
this.writer.end()
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async writeHandshakeAsync(): Promise<Buffer | null> {
|
|
204
|
+
const randomData = MultiplexingStreamFormatter.getIsOddRandomData()
|
|
205
|
+
const msgpackObject = [[MultiplexingStreamV2Formatter.protocolVersion.major, MultiplexingStreamV2Formatter.protocolVersion.minor], randomData]
|
|
206
|
+
await writeAsync(this.writer, msgpack.encode(msgpackObject))
|
|
207
|
+
return randomData
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async readHandshakeAsync(writeHandshakeResult: Buffer | null, cancellationToken: CancellationToken): Promise<HandshakeResult> {
|
|
211
|
+
if (!writeHandshakeResult) {
|
|
212
|
+
throw new Error('Provide the result of writeHandshakeAsync as a first argument.')
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const handshake = await this.readMessagePackAsync(cancellationToken)
|
|
216
|
+
if (handshake === null) {
|
|
217
|
+
throw new Error('No data received during handshake.')
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
isOdd: MultiplexingStreamFormatter.isOdd(writeHandshakeResult, handshake[1]),
|
|
222
|
+
protocolVersion: { major: handshake[0][0], minor: handshake[0][1] },
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async writeFrameAsync(header: FrameHeader, payload?: Buffer): Promise<void> {
|
|
227
|
+
const msgpackObject: any[] = [header.code]
|
|
228
|
+
if (header.channel?.id) {
|
|
229
|
+
msgpackObject.push(header.channel.id)
|
|
230
|
+
if (payload && payload.length > 0) {
|
|
231
|
+
msgpackObject.push(payload)
|
|
232
|
+
}
|
|
233
|
+
} else if (payload && payload.length > 0) {
|
|
234
|
+
throw new Error('A frame may not contain payload without a channel ID.')
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
await writeAsync(this.writer, msgpack.encode(msgpackObject))
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async readFrameAsync(cancellationToken: CancellationToken): Promise<{ header: FrameHeader; payload: Buffer } | null> {
|
|
241
|
+
if (this.isOdd === undefined) {
|
|
242
|
+
throw new Error('isOdd must be set first.')
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const msgpackObject = (await this.readMessagePackAsync(cancellationToken)) as [ControlCode, number, Buffer] | null
|
|
246
|
+
if (msgpackObject === null) {
|
|
247
|
+
return null
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const header = this.createFrameHeader(msgpackObject[0], msgpackObject[1])
|
|
251
|
+
return {
|
|
252
|
+
header,
|
|
253
|
+
payload: msgpackObject[2] || Buffer.from([]),
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
serializeOfferParameters(offer: OfferParameters): Buffer {
|
|
258
|
+
const payload: any[] = [offer.name]
|
|
259
|
+
if (offer.remoteWindowSize) {
|
|
260
|
+
payload.push(offer.remoteWindowSize)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return msgpack.encode(payload)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
deserializeOfferParameters(payload: Buffer): OfferParameters {
|
|
267
|
+
const msgpackObject = msgpack.decode(payload)
|
|
268
|
+
return {
|
|
269
|
+
name: msgpackObject[0],
|
|
270
|
+
remoteWindowSize: msgpackObject[1],
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
serializeAcceptanceParameters(acceptance: AcceptanceParameters): Buffer {
|
|
275
|
+
const payload: any[] = []
|
|
276
|
+
if (acceptance.remoteWindowSize) {
|
|
277
|
+
payload.push(acceptance.remoteWindowSize)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return msgpack.encode(payload)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
deserializeAcceptanceParameters(payload: Buffer): AcceptanceParameters {
|
|
284
|
+
const msgpackObject = msgpack.decode(payload)
|
|
285
|
+
return {
|
|
286
|
+
remoteWindowSize: msgpackObject[0],
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
serializeContentProcessed(bytesProcessed: number): Buffer {
|
|
291
|
+
return msgpack.encode([bytesProcessed])
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
deserializeContentProcessed(payload: Buffer): number {
|
|
295
|
+
return msgpack.decode(payload)[0]
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
serializeException(error: Error | null): Buffer {
|
|
299
|
+
// If the error doesn't exist then return an empty buffer.
|
|
300
|
+
if (!error) {
|
|
301
|
+
return Buffer.alloc(0)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const errorMsg: string = `${error.name}: ${error.message}`
|
|
305
|
+
const payload: any[] = [errorMsg]
|
|
306
|
+
return msgpack.encode(payload)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
deserializeException(payload: Buffer): Error | null {
|
|
310
|
+
// If the payload is empty then return null.
|
|
311
|
+
if (payload.length === 0) {
|
|
312
|
+
return null
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Make sure that the message pack object contains a message
|
|
316
|
+
const msgpackObject = msgpack.decode(payload)
|
|
317
|
+
if (!msgpackObject || msgpackObject.length === 0) {
|
|
318
|
+
return null
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Get error message and return the error to the remote side
|
|
322
|
+
let errorMsg: string = msgpack.decode(payload)[0]
|
|
323
|
+
errorMsg = `Received error from remote side: ${errorMsg}`
|
|
324
|
+
return new Error(errorMsg)
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
protected async readMessagePackAsync(cancellationToken: CancellationToken): Promise<{} | [] | null> {
|
|
328
|
+
const streamEnded = new Deferred<void>()
|
|
329
|
+
while (true) {
|
|
330
|
+
const readObject = this.reader.read()
|
|
331
|
+
if (readObject === null) {
|
|
332
|
+
const bytesAvailable = new Deferred<void>()
|
|
333
|
+
const bytesAvailableCallback = bytesAvailable.resolve.bind(bytesAvailable)
|
|
334
|
+
const streamEndedCallback = streamEnded.resolve.bind(streamEnded)
|
|
335
|
+
|
|
336
|
+
this.reader.once('readable', bytesAvailableCallback)
|
|
337
|
+
this.reader.once('end', streamEndedCallback)
|
|
338
|
+
const endPromise = Promise.race([bytesAvailable.promise, streamEnded.promise])
|
|
339
|
+
await (cancellationToken ? cancellationToken.racePromise(endPromise) : endPromise)
|
|
340
|
+
|
|
341
|
+
this.reader.removeListener('readable', bytesAvailableCallback)
|
|
342
|
+
this.reader.removeListener('end', streamEndedCallback)
|
|
343
|
+
|
|
344
|
+
if (bytesAvailable.isCompleted) {
|
|
345
|
+
continue
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return null
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return readObject
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// tslint:disable-next-line: max-classes-per-file
|
|
357
|
+
export class MultiplexingStreamV3Formatter extends MultiplexingStreamV2Formatter {
|
|
358
|
+
private static readonly protocolV3Version: Version = { major: 2, minor: 0 }
|
|
359
|
+
|
|
360
|
+
writeHandshakeAsync(): Promise<null> {
|
|
361
|
+
return Promise.resolve(null)
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
readHandshakeAsync(): Promise<HandshakeResult> {
|
|
365
|
+
return Promise.resolve({ protocolVersion: MultiplexingStreamV3Formatter.protocolV3Version })
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async writeFrameAsync(header: FrameHeader, payload?: Buffer): Promise<void> {
|
|
369
|
+
const msgpackObject: any[] = [header.code]
|
|
370
|
+
if (header.channel) {
|
|
371
|
+
msgpackObject.push(header.channel.id)
|
|
372
|
+
msgpackObject.push(header.channel.source)
|
|
373
|
+
if (payload && payload.length > 0) {
|
|
374
|
+
msgpackObject.push(payload)
|
|
375
|
+
}
|
|
376
|
+
} else if (payload && payload.length > 0) {
|
|
377
|
+
throw new Error('A frame may not contain payload without a channel ID.')
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
await writeAsync(this.writer, msgpack.encode(msgpackObject))
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
async readFrameAsync(cancellationToken: CancellationToken): Promise<{ header: FrameHeader; payload: Buffer } | null> {
|
|
384
|
+
const msgpackObject = (await this.readMessagePackAsync(cancellationToken)) as [ControlCode, number, ChannelSource, Buffer] | null
|
|
385
|
+
if (msgpackObject === null) {
|
|
386
|
+
return null
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const header = new FrameHeader(msgpackObject[0], msgpackObject.length > 1 ? { id: msgpackObject[1], source: msgpackObject[2] } : undefined)
|
|
390
|
+
return {
|
|
391
|
+
header,
|
|
392
|
+
payload: msgpackObject[3] || Buffer.from([]),
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|