nerdbank-streams 2.10.37-alpha → 2.10.66

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.
Files changed (38) hide show
  1. package/js/AcceptanceParameters.d.ts +4 -4
  2. package/js/AcceptanceParameters.js +2 -2
  3. package/js/Channel.d.ts +71 -70
  4. package/js/Channel.js +219 -219
  5. package/js/Channel.js.map +1 -1
  6. package/js/ChannelOptions.d.ts +4 -4
  7. package/js/ChannelOptions.js +2 -2
  8. package/js/ControlCode.d.ts +30 -30
  9. package/js/ControlCode.js +34 -34
  10. package/js/ControlCode.js.map +1 -1
  11. package/js/Deferred.d.ts +39 -39
  12. package/js/Deferred.js +67 -67
  13. package/js/FrameHeader.d.ts +20 -20
  14. package/js/FrameHeader.js +39 -39
  15. package/js/FullDuplexStream.d.ts +10 -10
  16. package/js/FullDuplexStream.js +31 -31
  17. package/js/IChannelOfferEventArgs.d.ts +17 -17
  18. package/js/IChannelOfferEventArgs.js +2 -2
  19. package/js/IDisposableObservable.d.ts +4 -4
  20. package/js/IDisposableObservable.js +2 -2
  21. package/js/MultiplexingStream.d.ts +220 -218
  22. package/js/MultiplexingStream.js +624 -610
  23. package/js/MultiplexingStream.js.map +1 -1
  24. package/js/MultiplexingStreamFormatters.d.ts +90 -89
  25. package/js/MultiplexingStreamFormatters.js +302 -302
  26. package/js/MultiplexingStreamOptions.d.ts +18 -18
  27. package/js/MultiplexingStreamOptions.js +2 -2
  28. package/js/OfferParameters.d.ts +6 -6
  29. package/js/OfferParameters.js +2 -2
  30. package/js/QualifiedChannelId.d.ts +25 -25
  31. package/js/QualifiedChannelId.js +38 -38
  32. package/js/QualifiedChannelId.js.map +1 -1
  33. package/js/Utilities.d.ts +12 -11
  34. package/js/Utilities.js +154 -154
  35. package/js/index.d.ts +8 -8
  36. package/js/index.js +15 -15
  37. package/package.json +18 -18
  38. package/src/MultiplexingStream.ts +18 -1
@@ -1,303 +1,303 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MultiplexingStreamV3Formatter = exports.MultiplexingStreamV2Formatter = exports.MultiplexingStreamV1Formatter = exports.MultiplexingStreamFormatter = void 0;
4
- const MultiplexingStream_1 = require("./MultiplexingStream");
5
- const crypto_1 = require("crypto");
6
- const Utilities_1 = require("./Utilities");
7
- const msgpack = require("msgpack-lite");
8
- const Deferred_1 = require("./Deferred");
9
- const FrameHeader_1 = require("./FrameHeader");
10
- const QualifiedChannelId_1 = require("./QualifiedChannelId");
11
- class MultiplexingStreamFormatter {
12
- static getIsOddRandomData() {
13
- return (0, crypto_1.randomBytes)(16);
14
- }
15
- static isOdd(localRandom, remoteRandom) {
16
- let isOdd;
17
- for (let i = 0; i < localRandom.length; i++) {
18
- const sent = localRandom[i];
19
- const recv = remoteRandom[i];
20
- if (sent > recv) {
21
- isOdd = true;
22
- break;
23
- }
24
- else if (sent < recv) {
25
- isOdd = false;
26
- break;
27
- }
28
- }
29
- if (isOdd === undefined) {
30
- throw new Error('Unable to determine even/odd party.');
31
- }
32
- return isOdd;
33
- }
34
- createFrameHeader(code, id) {
35
- if (!id) {
36
- return new FrameHeader_1.FrameHeader(code);
37
- }
38
- const channelIsOdd = id % 2 === 1;
39
- // Remember that this is from the remote sender's point of view.
40
- const source = channelIsOdd === this.isOdd ? QualifiedChannelId_1.ChannelSource.Remote : QualifiedChannelId_1.ChannelSource.Local;
41
- return new FrameHeader_1.FrameHeader(code, { id, source });
42
- }
43
- }
44
- exports.MultiplexingStreamFormatter = MultiplexingStreamFormatter;
45
- // tslint:disable-next-line: max-classes-per-file
46
- class MultiplexingStreamV1Formatter extends MultiplexingStreamFormatter {
47
- constructor(stream) {
48
- super();
49
- this.stream = stream;
50
- }
51
- end() {
52
- this.stream.end();
53
- }
54
- async writeHandshakeAsync() {
55
- const randomSendBuffer = MultiplexingStreamFormatter.getIsOddRandomData();
56
- const sendBuffer = Buffer.concat([MultiplexingStreamV1Formatter.protocolMagicNumber, randomSendBuffer]);
57
- await (0, Utilities_1.writeAsync)(this.stream, sendBuffer);
58
- return randomSendBuffer;
59
- }
60
- async readHandshakeAsync(writeHandshakeResult, cancellationToken) {
61
- const localRandomBuffer = writeHandshakeResult;
62
- const recvBuffer = await (0, Utilities_1.getBufferFrom)(this.stream, MultiplexingStreamV1Formatter.protocolMagicNumber.length + 16, false, cancellationToken);
63
- for (let i = 0; i < MultiplexingStreamV1Formatter.protocolMagicNumber.length; i++) {
64
- const expected = MultiplexingStreamV1Formatter.protocolMagicNumber[i];
65
- const actual = recvBuffer.readUInt8(i);
66
- if (expected !== actual) {
67
- throw new Error(`Protocol magic number mismatch. Expected ${expected} but was ${actual}.`);
68
- }
69
- }
70
- const isOdd = MultiplexingStreamFormatter.isOdd(localRandomBuffer, recvBuffer.slice(MultiplexingStreamV1Formatter.protocolMagicNumber.length));
71
- return { isOdd, protocolVersion: { major: 1, minor: 0 } };
72
- }
73
- async writeFrameAsync(header, payload) {
74
- var _a;
75
- const headerBuffer = Buffer.alloc(7);
76
- headerBuffer.writeInt8(header.code, 0);
77
- headerBuffer.writeUInt32BE(((_a = header.channel) === null || _a === void 0 ? void 0 : _a.id) || 0, 1);
78
- headerBuffer.writeUInt16BE((payload === null || payload === void 0 ? void 0 : payload.length) || 0, 5);
79
- await (0, Utilities_1.writeAsync)(this.stream, headerBuffer);
80
- if (payload && payload.length > 0) {
81
- await (0, Utilities_1.writeAsync)(this.stream, payload);
82
- }
83
- }
84
- async readFrameAsync(cancellationToken) {
85
- if (this.isOdd === undefined) {
86
- throw new Error('isOdd must be set first.');
87
- }
88
- const headerBuffer = await (0, Utilities_1.getBufferFrom)(this.stream, 7, true, cancellationToken);
89
- if (headerBuffer === null) {
90
- return null;
91
- }
92
- const header = this.createFrameHeader(headerBuffer.readInt8(0), headerBuffer.readUInt32BE(1));
93
- const payloadLength = headerBuffer.readUInt16BE(5);
94
- const payload = await (0, Utilities_1.getBufferFrom)(this.stream, payloadLength);
95
- return { header, payload };
96
- }
97
- serializeOfferParameters(offer) {
98
- const payload = Buffer.from(offer.name, MultiplexingStream_1.MultiplexingStream.ControlFrameEncoding);
99
- if (payload.length > MultiplexingStream_1.MultiplexingStream.framePayloadMaxLength) {
100
- throw new Error('Name is too long.');
101
- }
102
- return payload;
103
- }
104
- deserializeOfferParameters(payload) {
105
- return {
106
- name: payload.toString(MultiplexingStream_1.MultiplexingStream.ControlFrameEncoding),
107
- };
108
- }
109
- serializeAcceptanceParameters(_) {
110
- return Buffer.from([]);
111
- }
112
- deserializeAcceptanceParameters(_) {
113
- return {};
114
- }
115
- serializeContentProcessed(bytesProcessed) {
116
- throw new Error('Not supported in the V1 protocol.');
117
- }
118
- deserializeContentProcessed(payload) {
119
- throw new Error('Not supported in the V1 protocol.');
120
- }
121
- }
122
- exports.MultiplexingStreamV1Formatter = MultiplexingStreamV1Formatter;
123
- /**
124
- * The magic number to send at the start of communication when using v1 of the protocol.
125
- */
126
- MultiplexingStreamV1Formatter.protocolMagicNumber = Buffer.from([0x2f, 0xdf, 0x1d, 0x50]);
127
- // tslint:disable-next-line: max-classes-per-file
128
- class MultiplexingStreamV2Formatter extends MultiplexingStreamFormatter {
129
- constructor(stream) {
130
- super();
131
- this.reader = msgpack.createDecodeStream();
132
- stream.pipe(this.reader);
133
- this.writer = stream;
134
- }
135
- end() {
136
- this.writer.end();
137
- }
138
- async writeHandshakeAsync() {
139
- const randomData = MultiplexingStreamFormatter.getIsOddRandomData();
140
- const msgpackObject = [[MultiplexingStreamV2Formatter.protocolVersion.major, MultiplexingStreamV2Formatter.protocolVersion.minor], randomData];
141
- await (0, Utilities_1.writeAsync)(this.writer, msgpack.encode(msgpackObject));
142
- return randomData;
143
- }
144
- async readHandshakeAsync(writeHandshakeResult, cancellationToken) {
145
- if (!writeHandshakeResult) {
146
- throw new Error('Provide the result of writeHandshakeAsync as a first argument.');
147
- }
148
- const handshake = await this.readMessagePackAsync(cancellationToken);
149
- if (handshake === null) {
150
- throw new Error('No data received during handshake.');
151
- }
152
- return {
153
- isOdd: MultiplexingStreamFormatter.isOdd(writeHandshakeResult, handshake[1]),
154
- protocolVersion: { major: handshake[0][0], minor: handshake[0][1] },
155
- };
156
- }
157
- async writeFrameAsync(header, payload) {
158
- var _a;
159
- const msgpackObject = [header.code];
160
- if ((_a = header.channel) === null || _a === void 0 ? void 0 : _a.id) {
161
- msgpackObject.push(header.channel.id);
162
- if (payload && payload.length > 0) {
163
- msgpackObject.push(payload);
164
- }
165
- }
166
- else if (payload && payload.length > 0) {
167
- throw new Error('A frame may not contain payload without a channel ID.');
168
- }
169
- await (0, Utilities_1.writeAsync)(this.writer, msgpack.encode(msgpackObject));
170
- }
171
- async readFrameAsync(cancellationToken) {
172
- if (this.isOdd === undefined) {
173
- throw new Error('isOdd must be set first.');
174
- }
175
- const msgpackObject = (await this.readMessagePackAsync(cancellationToken));
176
- if (msgpackObject === null) {
177
- return null;
178
- }
179
- const header = this.createFrameHeader(msgpackObject[0], msgpackObject[1]);
180
- return {
181
- header,
182
- payload: msgpackObject[2] || Buffer.from([]),
183
- };
184
- }
185
- serializeOfferParameters(offer) {
186
- const payload = [offer.name];
187
- if (offer.remoteWindowSize) {
188
- payload.push(offer.remoteWindowSize);
189
- }
190
- return msgpack.encode(payload);
191
- }
192
- deserializeOfferParameters(payload) {
193
- const msgpackObject = msgpack.decode(payload);
194
- return {
195
- name: msgpackObject[0],
196
- remoteWindowSize: msgpackObject[1],
197
- };
198
- }
199
- serializeAcceptanceParameters(acceptance) {
200
- const payload = [];
201
- if (acceptance.remoteWindowSize) {
202
- payload.push(acceptance.remoteWindowSize);
203
- }
204
- return msgpack.encode(payload);
205
- }
206
- deserializeAcceptanceParameters(payload) {
207
- const msgpackObject = msgpack.decode(payload);
208
- return {
209
- remoteWindowSize: msgpackObject[0],
210
- };
211
- }
212
- serializeContentProcessed(bytesProcessed) {
213
- return msgpack.encode([bytesProcessed]);
214
- }
215
- deserializeContentProcessed(payload) {
216
- return msgpack.decode(payload)[0];
217
- }
218
- serializeException(error) {
219
- // If the error doesn't exist then return an empty buffer.
220
- if (!error) {
221
- return Buffer.alloc(0);
222
- }
223
- const errorMsg = `${error.name}: ${error.message}`;
224
- const payload = [errorMsg];
225
- return msgpack.encode(payload);
226
- }
227
- deserializeException(payload) {
228
- // If the payload is empty then return null.
229
- if (payload.length === 0) {
230
- return null;
231
- }
232
- // Make sure that the message pack object contains a message
233
- const msgpackObject = msgpack.decode(payload);
234
- if (!msgpackObject || msgpackObject.length === 0) {
235
- return null;
236
- }
237
- // Get error message and return the error to the remote side
238
- let errorMsg = msgpack.decode(payload)[0];
239
- errorMsg = `Received error from remote side: ${errorMsg}`;
240
- return new Error(errorMsg);
241
- }
242
- async readMessagePackAsync(cancellationToken) {
243
- const streamEnded = new Deferred_1.Deferred();
244
- while (true) {
245
- const readObject = this.reader.read();
246
- if (readObject === null) {
247
- const bytesAvailable = new Deferred_1.Deferred();
248
- const bytesAvailableCallback = bytesAvailable.resolve.bind(bytesAvailable);
249
- const streamEndedCallback = streamEnded.resolve.bind(streamEnded);
250
- this.reader.once('readable', bytesAvailableCallback);
251
- this.reader.once('end', streamEndedCallback);
252
- const endPromise = Promise.race([bytesAvailable.promise, streamEnded.promise]);
253
- await (cancellationToken ? cancellationToken.racePromise(endPromise) : endPromise);
254
- this.reader.removeListener('readable', bytesAvailableCallback);
255
- this.reader.removeListener('end', streamEndedCallback);
256
- if (bytesAvailable.isCompleted) {
257
- continue;
258
- }
259
- return null;
260
- }
261
- return readObject;
262
- }
263
- }
264
- }
265
- exports.MultiplexingStreamV2Formatter = MultiplexingStreamV2Formatter;
266
- MultiplexingStreamV2Formatter.protocolVersion = { major: 2, minor: 0 };
267
- // tslint:disable-next-line: max-classes-per-file
268
- class MultiplexingStreamV3Formatter extends MultiplexingStreamV2Formatter {
269
- writeHandshakeAsync() {
270
- return Promise.resolve(null);
271
- }
272
- readHandshakeAsync() {
273
- return Promise.resolve({ protocolVersion: MultiplexingStreamV3Formatter.protocolV3Version });
274
- }
275
- async writeFrameAsync(header, payload) {
276
- const msgpackObject = [header.code];
277
- if (header.channel) {
278
- msgpackObject.push(header.channel.id);
279
- msgpackObject.push(header.channel.source);
280
- if (payload && payload.length > 0) {
281
- msgpackObject.push(payload);
282
- }
283
- }
284
- else if (payload && payload.length > 0) {
285
- throw new Error('A frame may not contain payload without a channel ID.');
286
- }
287
- await (0, Utilities_1.writeAsync)(this.writer, msgpack.encode(msgpackObject));
288
- }
289
- async readFrameAsync(cancellationToken) {
290
- const msgpackObject = (await this.readMessagePackAsync(cancellationToken));
291
- if (msgpackObject === null) {
292
- return null;
293
- }
294
- const header = new FrameHeader_1.FrameHeader(msgpackObject[0], msgpackObject.length > 1 ? { id: msgpackObject[1], source: msgpackObject[2] } : undefined);
295
- return {
296
- header,
297
- payload: msgpackObject[3] || Buffer.from([]),
298
- };
299
- }
300
- }
301
- exports.MultiplexingStreamV3Formatter = MultiplexingStreamV3Formatter;
302
- MultiplexingStreamV3Formatter.protocolV3Version = { major: 2, minor: 0 };
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MultiplexingStreamV3Formatter = exports.MultiplexingStreamV2Formatter = exports.MultiplexingStreamV1Formatter = exports.MultiplexingStreamFormatter = void 0;
4
+ const MultiplexingStream_1 = require("./MultiplexingStream");
5
+ const crypto_1 = require("crypto");
6
+ const Utilities_1 = require("./Utilities");
7
+ const msgpack = require("msgpack-lite");
8
+ const Deferred_1 = require("./Deferred");
9
+ const FrameHeader_1 = require("./FrameHeader");
10
+ const QualifiedChannelId_1 = require("./QualifiedChannelId");
11
+ class MultiplexingStreamFormatter {
12
+ static getIsOddRandomData() {
13
+ return (0, crypto_1.randomBytes)(16);
14
+ }
15
+ static isOdd(localRandom, remoteRandom) {
16
+ let isOdd;
17
+ for (let i = 0; i < localRandom.length; i++) {
18
+ const sent = localRandom[i];
19
+ const recv = remoteRandom[i];
20
+ if (sent > recv) {
21
+ isOdd = true;
22
+ break;
23
+ }
24
+ else if (sent < recv) {
25
+ isOdd = false;
26
+ break;
27
+ }
28
+ }
29
+ if (isOdd === undefined) {
30
+ throw new Error('Unable to determine even/odd party.');
31
+ }
32
+ return isOdd;
33
+ }
34
+ createFrameHeader(code, id) {
35
+ if (!id) {
36
+ return new FrameHeader_1.FrameHeader(code);
37
+ }
38
+ const channelIsOdd = id % 2 === 1;
39
+ // Remember that this is from the remote sender's point of view.
40
+ const source = channelIsOdd === this.isOdd ? QualifiedChannelId_1.ChannelSource.Remote : QualifiedChannelId_1.ChannelSource.Local;
41
+ return new FrameHeader_1.FrameHeader(code, { id, source });
42
+ }
43
+ }
44
+ exports.MultiplexingStreamFormatter = MultiplexingStreamFormatter;
45
+ // tslint:disable-next-line: max-classes-per-file
46
+ class MultiplexingStreamV1Formatter extends MultiplexingStreamFormatter {
47
+ constructor(stream) {
48
+ super();
49
+ this.stream = stream;
50
+ }
51
+ end() {
52
+ this.stream.end();
53
+ }
54
+ async writeHandshakeAsync() {
55
+ const randomSendBuffer = MultiplexingStreamFormatter.getIsOddRandomData();
56
+ const sendBuffer = Buffer.concat([MultiplexingStreamV1Formatter.protocolMagicNumber, randomSendBuffer]);
57
+ await (0, Utilities_1.writeAsync)(this.stream, sendBuffer);
58
+ return randomSendBuffer;
59
+ }
60
+ async readHandshakeAsync(writeHandshakeResult, cancellationToken) {
61
+ const localRandomBuffer = writeHandshakeResult;
62
+ const recvBuffer = await (0, Utilities_1.getBufferFrom)(this.stream, MultiplexingStreamV1Formatter.protocolMagicNumber.length + 16, false, cancellationToken);
63
+ for (let i = 0; i < MultiplexingStreamV1Formatter.protocolMagicNumber.length; i++) {
64
+ const expected = MultiplexingStreamV1Formatter.protocolMagicNumber[i];
65
+ const actual = recvBuffer.readUInt8(i);
66
+ if (expected !== actual) {
67
+ throw new Error(`Protocol magic number mismatch. Expected ${expected} but was ${actual}.`);
68
+ }
69
+ }
70
+ const isOdd = MultiplexingStreamFormatter.isOdd(localRandomBuffer, recvBuffer.slice(MultiplexingStreamV1Formatter.protocolMagicNumber.length));
71
+ return { isOdd, protocolVersion: { major: 1, minor: 0 } };
72
+ }
73
+ async writeFrameAsync(header, payload) {
74
+ var _a;
75
+ const headerBuffer = Buffer.alloc(7);
76
+ headerBuffer.writeInt8(header.code, 0);
77
+ headerBuffer.writeUInt32BE(((_a = header.channel) === null || _a === void 0 ? void 0 : _a.id) || 0, 1);
78
+ headerBuffer.writeUInt16BE((payload === null || payload === void 0 ? void 0 : payload.length) || 0, 5);
79
+ await (0, Utilities_1.writeAsync)(this.stream, headerBuffer);
80
+ if (payload && payload.length > 0) {
81
+ await (0, Utilities_1.writeAsync)(this.stream, payload);
82
+ }
83
+ }
84
+ async readFrameAsync(cancellationToken) {
85
+ if (this.isOdd === undefined) {
86
+ throw new Error('isOdd must be set first.');
87
+ }
88
+ const headerBuffer = await (0, Utilities_1.getBufferFrom)(this.stream, 7, true, cancellationToken);
89
+ if (headerBuffer === null) {
90
+ return null;
91
+ }
92
+ const header = this.createFrameHeader(headerBuffer.readInt8(0), headerBuffer.readUInt32BE(1));
93
+ const payloadLength = headerBuffer.readUInt16BE(5);
94
+ const payload = await (0, Utilities_1.getBufferFrom)(this.stream, payloadLength);
95
+ return { header, payload };
96
+ }
97
+ serializeOfferParameters(offer) {
98
+ const payload = Buffer.from(offer.name, MultiplexingStream_1.MultiplexingStream.ControlFrameEncoding);
99
+ if (payload.length > MultiplexingStream_1.MultiplexingStream.framePayloadMaxLength) {
100
+ throw new Error('Name is too long.');
101
+ }
102
+ return payload;
103
+ }
104
+ deserializeOfferParameters(payload) {
105
+ return {
106
+ name: payload.toString(MultiplexingStream_1.MultiplexingStream.ControlFrameEncoding),
107
+ };
108
+ }
109
+ serializeAcceptanceParameters(_) {
110
+ return Buffer.from([]);
111
+ }
112
+ deserializeAcceptanceParameters(_) {
113
+ return {};
114
+ }
115
+ serializeContentProcessed(bytesProcessed) {
116
+ throw new Error('Not supported in the V1 protocol.');
117
+ }
118
+ deserializeContentProcessed(payload) {
119
+ throw new Error('Not supported in the V1 protocol.');
120
+ }
121
+ }
122
+ exports.MultiplexingStreamV1Formatter = MultiplexingStreamV1Formatter;
123
+ /**
124
+ * The magic number to send at the start of communication when using v1 of the protocol.
125
+ */
126
+ MultiplexingStreamV1Formatter.protocolMagicNumber = Buffer.from([0x2f, 0xdf, 0x1d, 0x50]);
127
+ // tslint:disable-next-line: max-classes-per-file
128
+ class MultiplexingStreamV2Formatter extends MultiplexingStreamFormatter {
129
+ constructor(stream) {
130
+ super();
131
+ this.reader = msgpack.createDecodeStream();
132
+ stream.pipe(this.reader);
133
+ this.writer = stream;
134
+ }
135
+ end() {
136
+ this.writer.end();
137
+ }
138
+ async writeHandshakeAsync() {
139
+ const randomData = MultiplexingStreamFormatter.getIsOddRandomData();
140
+ const msgpackObject = [[MultiplexingStreamV2Formatter.protocolVersion.major, MultiplexingStreamV2Formatter.protocolVersion.minor], randomData];
141
+ await (0, Utilities_1.writeAsync)(this.writer, msgpack.encode(msgpackObject));
142
+ return randomData;
143
+ }
144
+ async readHandshakeAsync(writeHandshakeResult, cancellationToken) {
145
+ if (!writeHandshakeResult) {
146
+ throw new Error('Provide the result of writeHandshakeAsync as a first argument.');
147
+ }
148
+ const handshake = await this.readMessagePackAsync(cancellationToken);
149
+ if (handshake === null) {
150
+ throw new Error('No data received during handshake.');
151
+ }
152
+ return {
153
+ isOdd: MultiplexingStreamFormatter.isOdd(writeHandshakeResult, handshake[1]),
154
+ protocolVersion: { major: handshake[0][0], minor: handshake[0][1] },
155
+ };
156
+ }
157
+ async writeFrameAsync(header, payload) {
158
+ var _a;
159
+ const msgpackObject = [header.code];
160
+ if ((_a = header.channel) === null || _a === void 0 ? void 0 : _a.id) {
161
+ msgpackObject.push(header.channel.id);
162
+ if (payload && payload.length > 0) {
163
+ msgpackObject.push(payload);
164
+ }
165
+ }
166
+ else if (payload && payload.length > 0) {
167
+ throw new Error('A frame may not contain payload without a channel ID.');
168
+ }
169
+ await (0, Utilities_1.writeAsync)(this.writer, msgpack.encode(msgpackObject));
170
+ }
171
+ async readFrameAsync(cancellationToken) {
172
+ if (this.isOdd === undefined) {
173
+ throw new Error('isOdd must be set first.');
174
+ }
175
+ const msgpackObject = (await this.readMessagePackAsync(cancellationToken));
176
+ if (msgpackObject === null) {
177
+ return null;
178
+ }
179
+ const header = this.createFrameHeader(msgpackObject[0], msgpackObject[1]);
180
+ return {
181
+ header,
182
+ payload: msgpackObject[2] || Buffer.from([]),
183
+ };
184
+ }
185
+ serializeOfferParameters(offer) {
186
+ const payload = [offer.name];
187
+ if (offer.remoteWindowSize) {
188
+ payload.push(offer.remoteWindowSize);
189
+ }
190
+ return msgpack.encode(payload);
191
+ }
192
+ deserializeOfferParameters(payload) {
193
+ const msgpackObject = msgpack.decode(payload);
194
+ return {
195
+ name: msgpackObject[0],
196
+ remoteWindowSize: msgpackObject[1],
197
+ };
198
+ }
199
+ serializeAcceptanceParameters(acceptance) {
200
+ const payload = [];
201
+ if (acceptance.remoteWindowSize) {
202
+ payload.push(acceptance.remoteWindowSize);
203
+ }
204
+ return msgpack.encode(payload);
205
+ }
206
+ deserializeAcceptanceParameters(payload) {
207
+ const msgpackObject = msgpack.decode(payload);
208
+ return {
209
+ remoteWindowSize: msgpackObject[0],
210
+ };
211
+ }
212
+ serializeContentProcessed(bytesProcessed) {
213
+ return msgpack.encode([bytesProcessed]);
214
+ }
215
+ deserializeContentProcessed(payload) {
216
+ return msgpack.decode(payload)[0];
217
+ }
218
+ serializeException(error) {
219
+ // If the error doesn't exist then return an empty buffer.
220
+ if (!error) {
221
+ return Buffer.alloc(0);
222
+ }
223
+ const errorMsg = `${error.name}: ${error.message}`;
224
+ const payload = [errorMsg];
225
+ return msgpack.encode(payload);
226
+ }
227
+ deserializeException(payload) {
228
+ // If the payload is empty then return null.
229
+ if (payload.length === 0) {
230
+ return null;
231
+ }
232
+ // Make sure that the message pack object contains a message
233
+ const msgpackObject = msgpack.decode(payload);
234
+ if (!msgpackObject || msgpackObject.length === 0) {
235
+ return null;
236
+ }
237
+ // Get error message and return the error to the remote side
238
+ let errorMsg = msgpack.decode(payload)[0];
239
+ errorMsg = `Received error from remote side: ${errorMsg}`;
240
+ return new Error(errorMsg);
241
+ }
242
+ async readMessagePackAsync(cancellationToken) {
243
+ const streamEnded = new Deferred_1.Deferred();
244
+ while (true) {
245
+ const readObject = this.reader.read();
246
+ if (readObject === null) {
247
+ const bytesAvailable = new Deferred_1.Deferred();
248
+ const bytesAvailableCallback = bytesAvailable.resolve.bind(bytesAvailable);
249
+ const streamEndedCallback = streamEnded.resolve.bind(streamEnded);
250
+ this.reader.once('readable', bytesAvailableCallback);
251
+ this.reader.once('end', streamEndedCallback);
252
+ const endPromise = Promise.race([bytesAvailable.promise, streamEnded.promise]);
253
+ await (cancellationToken ? cancellationToken.racePromise(endPromise) : endPromise);
254
+ this.reader.removeListener('readable', bytesAvailableCallback);
255
+ this.reader.removeListener('end', streamEndedCallback);
256
+ if (bytesAvailable.isCompleted) {
257
+ continue;
258
+ }
259
+ return null;
260
+ }
261
+ return readObject;
262
+ }
263
+ }
264
+ }
265
+ exports.MultiplexingStreamV2Formatter = MultiplexingStreamV2Formatter;
266
+ MultiplexingStreamV2Formatter.protocolVersion = { major: 2, minor: 0 };
267
+ // tslint:disable-next-line: max-classes-per-file
268
+ class MultiplexingStreamV3Formatter extends MultiplexingStreamV2Formatter {
269
+ writeHandshakeAsync() {
270
+ return Promise.resolve(null);
271
+ }
272
+ readHandshakeAsync() {
273
+ return Promise.resolve({ protocolVersion: MultiplexingStreamV3Formatter.protocolV3Version });
274
+ }
275
+ async writeFrameAsync(header, payload) {
276
+ const msgpackObject = [header.code];
277
+ if (header.channel) {
278
+ msgpackObject.push(header.channel.id);
279
+ msgpackObject.push(header.channel.source);
280
+ if (payload && payload.length > 0) {
281
+ msgpackObject.push(payload);
282
+ }
283
+ }
284
+ else if (payload && payload.length > 0) {
285
+ throw new Error('A frame may not contain payload without a channel ID.');
286
+ }
287
+ await (0, Utilities_1.writeAsync)(this.writer, msgpack.encode(msgpackObject));
288
+ }
289
+ async readFrameAsync(cancellationToken) {
290
+ const msgpackObject = (await this.readMessagePackAsync(cancellationToken));
291
+ if (msgpackObject === null) {
292
+ return null;
293
+ }
294
+ const header = new FrameHeader_1.FrameHeader(msgpackObject[0], msgpackObject.length > 1 ? { id: msgpackObject[1], source: msgpackObject[2] } : undefined);
295
+ return {
296
+ header,
297
+ payload: msgpackObject[3] || Buffer.from([]),
298
+ };
299
+ }
300
+ }
301
+ exports.MultiplexingStreamV3Formatter = MultiplexingStreamV3Formatter;
302
+ MultiplexingStreamV3Formatter.protocolV3Version = { major: 2, minor: 0 };
303
303
  //# sourceMappingURL=MultiplexingStreamFormatters.js.map
@@ -1,18 +1,18 @@
1
- import { ChannelOptions } from './ChannelOptions';
2
- export interface MultiplexingStreamOptions {
3
- /**
4
- * The protocol version to be used.
5
- * @description 1 is the original version. 2 is a protocol breaking change and adds backpressure support. 3 is a protocol breaking change, eliminates the handshake packet and adds seeded channels support.
6
- */
7
- protocolMajorVersion?: number;
8
- /** The number of received bytes that may be buffered locally per channel (transmitted from the remote party but not yet processed). */
9
- defaultChannelReceivingWindowSize?: number;
10
- /**
11
- * A list of options for channels that are to be "seeded" into a new MultiplexingStream.
12
- * @description Seeded channels avoid the need for a round-trip for an offer/accept packet exchange.
13
- * Seeded channels are accessed within the MultiplexingStream instance by calling AcceptChannel(ulong, ChannelOptions?)
14
- * with the 0-based index into this list used as the channel ID.
15
- * They are only supported when ProtocolMajorVersion is at least 3.
16
- */
17
- seededChannels?: ChannelOptions[];
18
- }
1
+ import { ChannelOptions } from './ChannelOptions';
2
+ export interface MultiplexingStreamOptions {
3
+ /**
4
+ * The protocol version to be used.
5
+ * @description 1 is the original version. 2 is a protocol breaking change and adds backpressure support. 3 is a protocol breaking change, eliminates the handshake packet and adds seeded channels support.
6
+ */
7
+ protocolMajorVersion?: number;
8
+ /** The number of received bytes that may be buffered locally per channel (transmitted from the remote party but not yet processed). */
9
+ defaultChannelReceivingWindowSize?: number;
10
+ /**
11
+ * A list of options for channels that are to be "seeded" into a new MultiplexingStream.
12
+ * @description Seeded channels avoid the need for a round-trip for an offer/accept packet exchange.
13
+ * Seeded channels are accessed within the MultiplexingStream instance by calling AcceptChannel(ulong, ChannelOptions?)
14
+ * with the 0-based index into this list used as the channel ID.
15
+ * They are only supported when ProtocolMajorVersion is at least 3.
16
+ */
17
+ seededChannels?: ChannelOptions[];
18
+ }
@@ -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=MultiplexingStreamOptions.js.map