nerdbank-streams 2.13.31 → 2.14.310-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/js/Channel.d.ts +2 -0
- package/js/Channel.js +17 -1
- package/js/Channel.js.map +1 -1
- package/js/ControlCode.d.ts +7 -1
- package/js/ControlCode.js +6 -0
- package/js/ControlCode.js.map +1 -1
- package/js/Deferred.d.ts +2 -2
- package/js/Deferred.js.map +1 -1
- package/js/FrameHeader.js.map +1 -1
- package/js/FullDuplexStream.d.ts +7 -0
- package/js/FullDuplexStream.js +77 -5
- package/js/FullDuplexStream.js.map +1 -1
- package/js/MultiplexingStream.d.ts +2 -0
- package/js/MultiplexingStream.js +22 -5
- package/js/MultiplexingStream.js.map +1 -1
- package/js/MultiplexingStreamFormatters.js +34 -1
- package/js/MultiplexingStreamFormatters.js.map +1 -1
- package/js/QualifiedChannelId.js +0 -3
- package/js/QualifiedChannelId.js.map +1 -1
- package/js/Utilities.d.ts +21 -1
- package/js/Utilities.js +200 -67
- package/js/Utilities.js.map +1 -1
- package/js/index.d.ts +1 -1
- package/js/index.js +3 -1
- package/js/index.js.map +1 -1
- package/package.json +22 -22
- package/src/Channel.ts +14 -0
- package/src/ControlCode.ts +7 -0
- package/src/FullDuplexStream.ts +90 -6
- package/src/MultiplexingStream.ts +26 -7
- package/src/QualifiedChannelId.ts +7 -7
- package/src/Utilities.ts +216 -73
- package/src/index.ts +1 -1
package/js/Utilities.d.ts
CHANGED
|
@@ -1,8 +1,28 @@
|
|
|
1
1
|
import CancellationToken from 'cancellationtoken';
|
|
2
|
+
import { Readable } from 'stream';
|
|
2
3
|
import { IDisposableObservable } from './IDisposableObservable';
|
|
3
4
|
export declare function writeAsync(stream: NodeJS.WritableStream, chunk: any): Promise<void>;
|
|
4
5
|
export declare function writeSubstream(stream: NodeJS.WritableStream): NodeJS.WritableStream;
|
|
5
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Reads the next chunk from a stream, asynchronously waiting for more to be read if necessary.
|
|
8
|
+
* @param stream The stream to read from.
|
|
9
|
+
* @param cancellationToken A token whose cancellation will result in immediate rejection of the returned promise.
|
|
10
|
+
* @returns The result of reading from the stream. This will be null if the end of the stream is reached before any more can be read.
|
|
11
|
+
*/
|
|
12
|
+
export declare function readAsync(stream: NodeJS.ReadableStream, cancellationToken?: CancellationToken): Promise<string | Buffer | null>;
|
|
13
|
+
/**
|
|
14
|
+
* Returns a readable stream that will read just a slice of some existing stream.
|
|
15
|
+
* @param stream The stream to read from.
|
|
16
|
+
* @param length The maximum number of bytes to read from the stream.
|
|
17
|
+
* @returns A stream that will read up to the given number of bytes, leaving the rest in the underlying stream.
|
|
18
|
+
*/
|
|
19
|
+
export declare function sliceStream(stream: NodeJS.ReadableStream, length: number): Readable;
|
|
20
|
+
/**
|
|
21
|
+
* Returns a readable stream that reads a substream that was written with {@link writeSubstream}.
|
|
22
|
+
* @param stream The stream to read the substream from.
|
|
23
|
+
* @returns A stream that ends when the substream ends, leaving the rest of `stream` available to other readers.
|
|
24
|
+
*/
|
|
25
|
+
export declare function readSubstream(stream: NodeJS.ReadableStream): Readable;
|
|
6
26
|
export declare function getBufferFrom(readable: NodeJS.ReadableStream, size: number, allowEndOfStream?: false, cancellationToken?: CancellationToken): Promise<Buffer>;
|
|
7
27
|
export declare function getBufferFrom(readable: NodeJS.ReadableStream, size: number, allowEndOfStream: true, cancellationToken?: CancellationToken): Promise<Buffer | null>;
|
|
8
28
|
export declare function throwIfDisposed(value: IDisposableObservable): void;
|
package/js/Utilities.js
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.writeAsync = writeAsync;
|
|
4
7
|
exports.writeSubstream = writeSubstream;
|
|
8
|
+
exports.readAsync = readAsync;
|
|
9
|
+
exports.sliceStream = sliceStream;
|
|
5
10
|
exports.readSubstream = readSubstream;
|
|
6
11
|
exports.getBufferFrom = getBufferFrom;
|
|
7
12
|
exports.throwIfDisposed = throwIfDisposed;
|
|
8
13
|
exports.requireInteger = requireInteger;
|
|
9
14
|
exports.removeFromQueue = removeFromQueue;
|
|
15
|
+
const cancellationtoken_1 = __importDefault(require("cancellationtoken"));
|
|
10
16
|
const stream_1 = require("stream");
|
|
11
|
-
const Deferred_1 = require("./Deferred");
|
|
12
17
|
async function writeAsync(stream, chunk) {
|
|
13
18
|
return new Promise((resolve, reject) => {
|
|
14
19
|
stream.write(chunk, (err) => {
|
|
@@ -41,88 +46,216 @@ function writeSubstream(stream) {
|
|
|
41
46
|
},
|
|
42
47
|
});
|
|
43
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Reads the next chunk from a stream, asynchronously waiting for more to be read if necessary.
|
|
51
|
+
* @param stream The stream to read from.
|
|
52
|
+
* @param cancellationToken A token whose cancellation will result in immediate rejection of the returned promise.
|
|
53
|
+
* @returns The result of reading from the stream. This will be null if the end of the stream is reached before any more can be read.
|
|
54
|
+
*/
|
|
55
|
+
function readAsync(stream, cancellationToken) {
|
|
56
|
+
return readAtMostAsync(stream, undefined, cancellationToken);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Reads the next chunk from a stream, asynchronously waiting for more to be read if necessary,
|
|
60
|
+
* without ever consuming more than a given number of bytes from the stream.
|
|
61
|
+
* @param stream The stream to read from.
|
|
62
|
+
* @param maxBytes The maximum number of bytes to consume from the stream, or `undefined` to consume whatever is immediately available.
|
|
63
|
+
* @param cancellationToken A token whose cancellation will result in immediate rejection of the returned promise.
|
|
64
|
+
* @returns The result of reading from the stream. This will be null if the end of the stream is reached before any more can be read.
|
|
65
|
+
*/
|
|
66
|
+
function readAtMostAsync(stream, maxBytes, cancellationToken) {
|
|
67
|
+
if (cancellationToken === null || cancellationToken === void 0 ? void 0 : cancellationToken.isCancelled) {
|
|
68
|
+
return Promise.reject(new cancellationtoken_1.default.CancellationError(cancellationToken.reason));
|
|
69
|
+
}
|
|
70
|
+
// Always try a synchronous read first. Besides being faster, this avoids resuming on a later
|
|
71
|
+
// microtask, by which time the stream may have already emitted its 'end' event.
|
|
72
|
+
const initialChunk = readSyncAtMost(stream, maxBytes);
|
|
73
|
+
if (initialChunk !== null) {
|
|
74
|
+
return Promise.resolve(initialChunk);
|
|
75
|
+
}
|
|
76
|
+
const readable = stream;
|
|
77
|
+
if (readable.errored) {
|
|
78
|
+
// The stream has already failed. Report it immediately rather than waiting for an
|
|
79
|
+
// 'error' event that may have already been emitted.
|
|
80
|
+
return Promise.reject(readable.errored);
|
|
81
|
+
}
|
|
82
|
+
if (readable.readableEnded || readable.destroyed) {
|
|
83
|
+
return Promise.resolve(null);
|
|
84
|
+
}
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
// Note that adding a 'readable' event handler switches the stream to paused mode, which is exactly what we want
|
|
87
|
+
// since we read explicitly. Node.js restores the stream's prior flowing state when the last such handler is removed,
|
|
88
|
+
// so this has no lasting impact on the stream for other consumers.
|
|
89
|
+
const ctReg = cancellationToken === null || cancellationToken === void 0 ? void 0 : cancellationToken.onCancelled(reason => {
|
|
90
|
+
cleanup();
|
|
91
|
+
reject(new cancellationtoken_1.default.CancellationError(reason));
|
|
92
|
+
});
|
|
93
|
+
stream.on('readable', onReadable);
|
|
94
|
+
stream.once('error', onError);
|
|
95
|
+
stream.once('end', onEnd);
|
|
96
|
+
stream.once('close', onClose);
|
|
97
|
+
function onReadable() {
|
|
98
|
+
const chunk = readSyncAtMost(stream, maxBytes);
|
|
99
|
+
if (chunk !== null) {
|
|
100
|
+
cleanup();
|
|
101
|
+
resolve(chunk);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function onError(err) {
|
|
105
|
+
cleanup();
|
|
106
|
+
reject(err);
|
|
107
|
+
}
|
|
108
|
+
function onEnd() {
|
|
109
|
+
cleanup();
|
|
110
|
+
resolve(null);
|
|
111
|
+
}
|
|
112
|
+
function onClose() {
|
|
113
|
+
cleanup();
|
|
114
|
+
resolve(null);
|
|
115
|
+
}
|
|
116
|
+
function cleanup() {
|
|
117
|
+
stream.off('readable', onReadable);
|
|
118
|
+
stream.off('error', onError);
|
|
119
|
+
stream.off('end', onEnd);
|
|
120
|
+
stream.off('close', onClose);
|
|
121
|
+
if (ctReg) {
|
|
122
|
+
ctReg();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Synchronously reads whatever is immediately available from a stream, without consuming more than `maxBytes` bytes.
|
|
129
|
+
* @param stream The stream to read from.
|
|
130
|
+
* @param maxBytes The maximum number of bytes to consume, or `undefined` for no limit.
|
|
131
|
+
* @returns The chunk that was read, or null if nothing was immediately available.
|
|
132
|
+
*/
|
|
133
|
+
function readSyncAtMost(stream, maxBytes) {
|
|
134
|
+
if (maxBytes === undefined) {
|
|
135
|
+
return stream.read();
|
|
136
|
+
}
|
|
137
|
+
if (maxBytes === 0) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
// Only ask for as much as is already buffered so that `read` doesn't return null merely
|
|
141
|
+
// because fewer than `maxBytes` bytes have arrived so far.
|
|
142
|
+
const buffered = stream.readableLength;
|
|
143
|
+
return stream.read(buffered > 0 ? Math.min(buffered, maxBytes) : maxBytes);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Returns a readable stream that will read just a slice of some existing stream.
|
|
147
|
+
* @param stream The stream to read from.
|
|
148
|
+
* @param length The maximum number of bytes to read from the stream.
|
|
149
|
+
* @returns A stream that will read up to the given number of bytes, leaving the rest in the underlying stream.
|
|
150
|
+
*/
|
|
151
|
+
function sliceStream(stream, length) {
|
|
152
|
+
// Reads that are in flight when this stream is destroyed must be cancelled so that
|
|
153
|
+
// they do not go on to consume data from the underlying stream that no one will receive.
|
|
154
|
+
const cts = cancellationtoken_1.default.create();
|
|
155
|
+
return new stream_1.Readable({
|
|
156
|
+
async read(_) {
|
|
157
|
+
try {
|
|
158
|
+
if (length === 0) {
|
|
159
|
+
this.push(null);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const chunk = (await readAtMostAsync(stream, length, cts.token));
|
|
163
|
+
if (chunk === null) {
|
|
164
|
+
// We've reached the end of the source stream.
|
|
165
|
+
this.push(null);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
length -= chunk.length;
|
|
169
|
+
this.push(chunk);
|
|
170
|
+
if (length === 0) {
|
|
171
|
+
// Save another call later by informing immediately that we're at the end of the stream.
|
|
172
|
+
this.push(null);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
catch (err) {
|
|
176
|
+
this.destroy(err);
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
destroy(error, callback) {
|
|
180
|
+
cts.cancel();
|
|
181
|
+
callback(error);
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Returns a readable stream that reads a substream that was written with {@link writeSubstream}.
|
|
187
|
+
* @param stream The stream to read the substream from.
|
|
188
|
+
* @returns A stream that ends when the substream ends, leaving the rest of `stream` available to other readers.
|
|
189
|
+
*/
|
|
44
190
|
function readSubstream(stream) {
|
|
191
|
+
let bytesRemainingInChunk = 0;
|
|
192
|
+
let reachedEnd = false;
|
|
193
|
+
// Reads that are in flight when this stream is destroyed must be cancelled so that
|
|
194
|
+
// they do not go on to consume data from the underlying stream that no one will receive.
|
|
195
|
+
const cts = cancellationtoken_1.default.create();
|
|
45
196
|
return new stream_1.Readable({
|
|
46
197
|
async read(_) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
198
|
+
try {
|
|
199
|
+
if (reachedEnd) {
|
|
200
|
+
this.push(null);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (bytesRemainingInChunk === 0) {
|
|
204
|
+
const lenBuffer = await getBufferFrom(stream, 4, false, cts.token);
|
|
205
|
+
const dv = new DataView(lenBuffer.buffer, lenBuffer.byteOffset, lenBuffer.length);
|
|
206
|
+
bytesRemainingInChunk = dv.getUint32(0, false);
|
|
207
|
+
if (bytesRemainingInChunk === 0) {
|
|
208
|
+
// We've reached the end of the substream.
|
|
209
|
+
reachedEnd = true;
|
|
210
|
+
this.push(null);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// Push whatever is available rather than waiting for the entire chunk to arrive.
|
|
215
|
+
const payload = (await readAtMostAsync(stream, bytesRemainingInChunk, cts.token));
|
|
216
|
+
if (payload === null) {
|
|
217
|
+
throw new Error('Stream terminated before the substream was completed.');
|
|
218
|
+
}
|
|
219
|
+
bytesRemainingInChunk -= payload.length;
|
|
220
|
+
this.push(payload);
|
|
221
|
+
}
|
|
222
|
+
catch (err) {
|
|
223
|
+
this.destroy(err);
|
|
53
224
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
225
|
+
},
|
|
226
|
+
destroy(error, callback) {
|
|
227
|
+
cts.cancel();
|
|
228
|
+
callback(error);
|
|
57
229
|
},
|
|
58
230
|
});
|
|
59
231
|
}
|
|
60
232
|
async function getBufferFrom(readable, size, allowEndOfStream = false, cancellationToken) {
|
|
61
|
-
const streamEnded = new Deferred_1.Deferred();
|
|
62
233
|
if (size === 0) {
|
|
63
|
-
return Buffer.
|
|
234
|
+
return Buffer.alloc(0);
|
|
64
235
|
}
|
|
65
|
-
let
|
|
66
|
-
let
|
|
67
|
-
while (
|
|
236
|
+
let result = null;
|
|
237
|
+
let bytesRead = 0;
|
|
238
|
+
while (bytesRead < size) {
|
|
68
239
|
cancellationToken === null || cancellationToken === void 0 ? void 0 : cancellationToken.throwIfCancelled();
|
|
69
|
-
|
|
70
|
-
if (
|
|
71
|
-
//
|
|
72
|
-
if (
|
|
73
|
-
// stream is closed
|
|
74
|
-
if (!allowEndOfStream) {
|
|
75
|
-
throw new Error('Stream terminated before required bytes were read.');
|
|
76
|
-
}
|
|
77
|
-
// Returns what has been read so far.
|
|
78
|
-
if (readBuffer === null) {
|
|
79
|
-
return null;
|
|
80
|
-
}
|
|
81
|
-
// We need to trim the trailing space.
|
|
82
|
-
return readBuffer.subarray(0, index);
|
|
83
|
-
}
|
|
84
|
-
// we retain this behavior when availableSize === false
|
|
85
|
-
// to make existing unit tests happy (which assumes we will try to read stream when no data is ready.)
|
|
86
|
-
availableSize = size;
|
|
87
|
-
}
|
|
88
|
-
else if (availableSize > size) {
|
|
89
|
-
availableSize = size;
|
|
90
|
-
}
|
|
91
|
-
const newBuffer = readable.read(availableSize);
|
|
92
|
-
if (newBuffer) {
|
|
93
|
-
if (newBuffer.length < availableSize && !allowEndOfStream) {
|
|
240
|
+
const chunk = (await readAtMostAsync(readable, size - bytesRead, cancellationToken));
|
|
241
|
+
if (chunk === null) {
|
|
242
|
+
// The stream ended before we could read everything that was requested.
|
|
243
|
+
if (!allowEndOfStream) {
|
|
94
244
|
throw new Error('Stream terminated before required bytes were read.');
|
|
95
245
|
}
|
|
96
|
-
|
|
97
|
-
if (availableSize === size || newBuffer.length < availableSize) {
|
|
98
|
-
// In the fast pass, we read the entire data once, and do not allocate an extra array.
|
|
99
|
-
return newBuffer;
|
|
100
|
-
}
|
|
101
|
-
// If we read partial data, we need to allocate a buffer to join all data together.
|
|
102
|
-
readBuffer = Buffer.alloc(size);
|
|
103
|
-
}
|
|
104
|
-
// now append new data to the buffer
|
|
105
|
-
newBuffer.copy(readBuffer, index);
|
|
106
|
-
size -= newBuffer.length;
|
|
107
|
-
index += newBuffer.length;
|
|
246
|
+
return bytesRead === 0 ? null : result.subarray(0, bytesRead);
|
|
108
247
|
}
|
|
109
|
-
if (
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
try {
|
|
116
|
-
const endPromise = Promise.race([bytesAvailable.promise, streamEnded.promise]);
|
|
117
|
-
await (cancellationToken ? cancellationToken.racePromise(endPromise) : endPromise);
|
|
118
|
-
}
|
|
119
|
-
finally {
|
|
120
|
-
readable.removeListener('readable', bytesAvailableCallback);
|
|
121
|
-
readable.removeListener('end', streamEndedCallback);
|
|
122
|
-
}
|
|
248
|
+
if (result === null && chunk.length === size) {
|
|
249
|
+
// Fast path: the entire request was satisfied by a single read, so avoid an extra allocation and copy.
|
|
250
|
+
return chunk;
|
|
251
|
+
}
|
|
252
|
+
if (result === null) {
|
|
253
|
+
result = Buffer.alloc(size);
|
|
123
254
|
}
|
|
255
|
+
chunk.copy(result, bytesRead);
|
|
256
|
+
bytesRead += chunk.length;
|
|
124
257
|
}
|
|
125
|
-
return
|
|
258
|
+
return result;
|
|
126
259
|
}
|
|
127
260
|
function throwIfDisposed(value) {
|
|
128
261
|
if (value.isDisposed) {
|
package/js/Utilities.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Utilities.js","sourceRoot":"","sources":["../src/Utilities.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Utilities.js","sourceRoot":"","sources":["../src/Utilities.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,0EAAiD;AACjD,mCAA2C;AAGpC,KAAK,qBAAqB,MAA6B,EAAE,KAAU;IACzE,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC5C,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC,GAA6B,EAAE,EAAE;YACrD,IAAI,GAAG,EAAE,CAAC;gBACT,MAAM,CAAC,GAAG,CAAC,CAAA;YACZ,CAAC;iBAAM,CAAC;gBACP,OAAO,EAAE,CAAA;YACV,CAAC;QACF,CAAC,CAAC,CAAA;IACH,CAAC,CAAC,CAAA;AACH,CAAC;AAED,wBAA+B,MAA6B;IAC3D,OAAO,IAAI,iBAAQ,CAAC;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I,KAAK,CAAC,UAAU,CAAC,CAAA;IAC5B,CAAC;AACF,CAAC;AAED,wBAA+B,aAAqB,EAAE,KAAa,EAAE,oBAA4B,EAAE,MAAM,GAA0B,QAAQ;IAC1I,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,GAAG,aAAa,sBAAsB,CAAC,CAAA;IACxD,CAAC;IAED,IAAI,IAAI,GAAG,oBAAoB,GAAG,CAAC,CAAA;IACnC,IAAI,MAAM,KAAK,QAAQ,EAAE,CAAC;QACzB,IAAI,EAAE,CAAA;IACP,CAAC;IAED,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAA;IACtC,MAAM,QAAQ,GAAG,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAC7D,IAAI,KAAK,GAAG,QAAQ,IAAI,KAAK,GAAG,QAAQ,EAAE,CAAC;QAC1C,MAAM,IAAI,KAAK,CAAC,GAAG,aAAa,yBAAyB,QAAQ,IAAI,QAAQ,GAAG,CAAC,CAAA;IAClF,CAAC;AACF,CAAC;AAED,yBAAmC,KAAQ,EAAE,KAAU;IACtD,IAAI,KAAK,EAAE,CAAC;QACX,MAAM,GAAG,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAChC,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC;YACd,KAAK,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;QACrB,CAAC;IACF,CAAC;AACF,CAAC"}
|
package/js/index.d.ts
CHANGED
|
@@ -4,5 +4,5 @@ export { FullDuplexStream } from './FullDuplexStream';
|
|
|
4
4
|
export { IDisposableObservable } from './IDisposableObservable';
|
|
5
5
|
export { MultiplexingStream } from './MultiplexingStream';
|
|
6
6
|
export { MultiplexingStreamOptions } from './MultiplexingStreamOptions';
|
|
7
|
-
export { writeSubstream, readSubstream } from './Utilities';
|
|
7
|
+
export { writeSubstream, readSubstream, readAsync, sliceStream } from './Utilities';
|
|
8
8
|
export { QualifiedChannelId, ChannelSource } from './QualifiedChannelId';
|
package/js/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ChannelSource = exports.QualifiedChannelId = exports.readSubstream = exports.writeSubstream = exports.MultiplexingStream = exports.FullDuplexStream = exports.Channel = void 0;
|
|
3
|
+
exports.ChannelSource = exports.QualifiedChannelId = exports.sliceStream = exports.readAsync = exports.readSubstream = exports.writeSubstream = exports.MultiplexingStream = exports.FullDuplexStream = exports.Channel = void 0;
|
|
4
4
|
var Channel_1 = require("./Channel");
|
|
5
5
|
Object.defineProperty(exports, "Channel", { enumerable: true, get: function () { return Channel_1.Channel; } });
|
|
6
6
|
var FullDuplexStream_1 = require("./FullDuplexStream");
|
|
@@ -10,6 +10,8 @@ Object.defineProperty(exports, "MultiplexingStream", { enumerable: true, get: fu
|
|
|
10
10
|
var Utilities_1 = require("./Utilities");
|
|
11
11
|
Object.defineProperty(exports, "writeSubstream", { enumerable: true, get: function () { return Utilities_1.writeSubstream; } });
|
|
12
12
|
Object.defineProperty(exports, "readSubstream", { enumerable: true, get: function () { return Utilities_1.readSubstream; } });
|
|
13
|
+
Object.defineProperty(exports, "readAsync", { enumerable: true, get: function () { return Utilities_1.readAsync; } });
|
|
14
|
+
Object.defineProperty(exports, "sliceStream", { enumerable: true, get: function () { return Utilities_1.sliceStream; } });
|
|
13
15
|
var QualifiedChannelId_1 = require("./QualifiedChannelId");
|
|
14
16
|
Object.defineProperty(exports, "QualifiedChannelId", { enumerable: true, get: function () { return QualifiedChannelId_1.QualifiedChannelId; } });
|
|
15
17
|
Object.defineProperty(exports, "ChannelSource", { enumerable: true, get: function () { return QualifiedChannelId_1.ChannelSource; } });
|
package/js/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qCAAmC;AAA1B,kGAAA,OAAO,OAAA;AAEhB,uDAAqD;AAA5C,oHAAA,gBAAgB,OAAA;AAEzB,2DAAyD;AAAhD,wHAAA,kBAAkB,OAAA;AAE3B,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,qCAAmC;AAA1B,kGAAA,OAAO,OAAA;AAEhB,uDAAqD;AAA5C,oHAAA,gBAAgB,OAAA;AAEzB,2DAAyD;AAAhD,wHAAA,kBAAkB,OAAA;AAE3B,yCAAmF;AAA1E,2GAAA,cAAc,OAAA;AAAE,0GAAA,aAAa,OAAA;AAAE,sGAAA,SAAS,OAAA;AAAE,wGAAA,WAAW,OAAA;AAC9D,2DAAwE;AAA/D,wHAAA,kBAAkB,OAAA;AAAE,mHAAA,aAAa,OAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nerdbank-streams",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.14.310-alpha",
|
|
4
4
|
"description": "Multiplexing of streams",
|
|
5
5
|
"readme": "README.md",
|
|
6
6
|
"keywords": [
|
|
@@ -25,38 +25,38 @@
|
|
|
25
25
|
},
|
|
26
26
|
"license": "MIT",
|
|
27
27
|
"private": false,
|
|
28
|
+
"packageManager": "pnpm@11.21.0",
|
|
28
29
|
"scripts": {
|
|
29
30
|
"build": "tsc -p gulpfile.tsconfig.json && gulp",
|
|
30
31
|
"lint": "eslint src --ext ts",
|
|
31
32
|
"test": "jest",
|
|
32
|
-
"watch": "
|
|
33
|
+
"watch": "tsc -p tsconfig.json -w"
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
35
|
-
"@types/
|
|
36
|
-
"@types/
|
|
37
|
-
"@types/msgpack-lite": "0.1.
|
|
38
|
-
"@types/node": "
|
|
39
|
-
"@
|
|
40
|
-
"@typescript-eslint/eslint-plugin": "
|
|
41
|
-
"@typescript-eslint/parser": "
|
|
42
|
-
"eslint": "8.
|
|
43
|
-
"eslint-config-prettier": "
|
|
44
|
-
"eslint-plugin-import": "2.
|
|
36
|
+
"@types/gulp": "4.0.18",
|
|
37
|
+
"@types/jest": "30.0.0",
|
|
38
|
+
"@types/msgpack-lite": "0.1.12",
|
|
39
|
+
"@types/node": "24.13.3",
|
|
40
|
+
"@typescript/native": "npm:typescript@7.0.2",
|
|
41
|
+
"@typescript-eslint/eslint-plugin": "8.67.0",
|
|
42
|
+
"@typescript-eslint/parser": "8.67.0",
|
|
43
|
+
"eslint": "10.8.1",
|
|
44
|
+
"eslint-config-prettier": "10.1.8",
|
|
45
|
+
"eslint-plugin-import": "2.32.0",
|
|
45
46
|
"eslint-plugin-jsx-a11y": "6.10.2",
|
|
46
|
-
"eslint-plugin-prettier": "5.
|
|
47
|
-
"gulp": "5.0.
|
|
47
|
+
"eslint-plugin-prettier": "5.5.6",
|
|
48
|
+
"gulp": "5.0.1",
|
|
48
49
|
"gulp-typescript": "6.0.0-alpha.1",
|
|
49
|
-
"jest": "
|
|
50
|
+
"jest": "30.4.2",
|
|
50
51
|
"jshint": "2.13.6",
|
|
51
|
-
"mkdirp": "
|
|
52
|
-
"nerdbank-gitversioning": "3.
|
|
52
|
+
"mkdirp": "3.0.1",
|
|
53
|
+
"nerdbank-gitversioning": "3.10.91",
|
|
53
54
|
"path": "0.12.7",
|
|
54
|
-
"prettier": "3.
|
|
55
|
-
"
|
|
56
|
-
"ts-jest": "29.2.6",
|
|
55
|
+
"prettier": "3.9.6",
|
|
56
|
+
"ts-jest": "29.4.12",
|
|
57
57
|
"ts-node": "10.9.2",
|
|
58
|
-
"typescript": "
|
|
59
|
-
"vscode-jsonrpc": "
|
|
58
|
+
"typescript": "npm:@typescript/typescript6@6.0.2",
|
|
59
|
+
"vscode-jsonrpc": "9.0.1"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
62
|
"await-semaphore": "^0.1.3",
|
package/src/Channel.ts
CHANGED
|
@@ -81,6 +81,7 @@ export class ChannelClass extends Channel {
|
|
|
81
81
|
|
|
82
82
|
/** A signal which indicates when the <see cref="RemoteWindowRemaining"/> is non-zero. */
|
|
83
83
|
private remoteWindowHasCapacity: Deferred<void>
|
|
84
|
+
private remoteContentReadingCompleted: boolean = false
|
|
84
85
|
|
|
85
86
|
constructor(multiplexingStream: MultiplexingStreamClass, id: QualifiedChannelId, offerParameters: OfferParameters) {
|
|
86
87
|
super(id)
|
|
@@ -120,6 +121,9 @@ export class ChannelClass extends Channel {
|
|
|
120
121
|
// Don't send more than will fit in the remote's receiving window size.
|
|
121
122
|
if (self._multiplexingStream.backpressureSupportEnabled) {
|
|
122
123
|
await self.remoteWindowHasCapacity.promise
|
|
124
|
+
if (self.remoteContentReadingCompleted) {
|
|
125
|
+
break
|
|
126
|
+
}
|
|
123
127
|
if (!self.remoteWindowSize) {
|
|
124
128
|
throw new Error('Remote window size unknown.')
|
|
125
129
|
}
|
|
@@ -158,6 +162,11 @@ export class ChannelClass extends Channel {
|
|
|
158
162
|
// Nothing to do here since data is pushed to us.
|
|
159
163
|
},
|
|
160
164
|
})
|
|
165
|
+
this._duplex.on('close', () => {
|
|
166
|
+
if (!self.isDisposed) {
|
|
167
|
+
caught(self._multiplexingStream.onChannelReadingCompleted(self))
|
|
168
|
+
}
|
|
169
|
+
})
|
|
161
170
|
}
|
|
162
171
|
|
|
163
172
|
public get stream(): NodeJS.ReadWriteStream {
|
|
@@ -254,6 +263,11 @@ export class ChannelClass extends Channel {
|
|
|
254
263
|
}
|
|
255
264
|
}
|
|
256
265
|
|
|
266
|
+
public onContentReadingCompleted() {
|
|
267
|
+
this.remoteContentReadingCompleted = true
|
|
268
|
+
this.remoteWindowHasCapacity.resolve()
|
|
269
|
+
}
|
|
270
|
+
|
|
257
271
|
public dispose(error?: Error | null): void {
|
|
258
272
|
if (!this.isDisposed) {
|
|
259
273
|
super.dispose()
|
package/src/ControlCode.ts
CHANGED
|
@@ -32,4 +32,11 @@ export enum ControlCode {
|
|
|
32
32
|
* Sent when a channel has finished processing data received from the remote party, allowing them to send more data.
|
|
33
33
|
*/
|
|
34
34
|
contentProcessed,
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Sent when a channel reader has completed and can no longer receive content,
|
|
38
|
+
* so the remote party can stop transmitting and release any writer blocked on flow control.
|
|
39
|
+
* Only sent when the protocol version supports backpressure (v2 and later).
|
|
40
|
+
*/
|
|
41
|
+
contentReadingCompleted,
|
|
35
42
|
}
|
package/src/FullDuplexStream.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { Duplex, PassThrough } from 'stream'
|
|
1
|
+
import { Duplex, PassThrough, Readable } from 'stream'
|
|
2
2
|
|
|
3
3
|
export class FullDuplexStream {
|
|
4
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
5
4
|
public static CreatePair(): { first: Duplex; second: Duplex } {
|
|
6
5
|
const pass1 = new PassThrough()
|
|
7
6
|
const pass2 = new PassThrough()
|
|
@@ -11,9 +10,83 @@ export class FullDuplexStream {
|
|
|
11
10
|
}
|
|
12
11
|
}
|
|
13
12
|
|
|
14
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Creates a full duplex stream from a readable stream and a writable stream.
|
|
15
|
+
* @param readable The stream that the duplex stream will read from.
|
|
16
|
+
* @param writable The stream that the duplex stream will write to.
|
|
17
|
+
* @returns A duplex stream. Ending the duplex stream ends `writable`, but destroying the
|
|
18
|
+
* duplex stream does not destroy either of the underlying streams.
|
|
19
|
+
*/
|
|
15
20
|
public static Splice(readable: NodeJS.ReadableStream, writable: NodeJS.WritableStream): Duplex {
|
|
21
|
+
let ended = false
|
|
22
|
+
let readableListenerAttached = false
|
|
23
|
+
|
|
24
|
+
// Data is pulled from the source stream on demand (rather than pushed eagerly) so that
|
|
25
|
+
// back-pressure is preserved and so that data that is already available on the source stream
|
|
26
|
+
// can be read synchronously from the duplex stream.
|
|
27
|
+
const pump = () => {
|
|
28
|
+
if (ended || duplex.destroyed) {
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let chunk: any
|
|
33
|
+
while ((chunk = readable.read()) !== null) {
|
|
34
|
+
if (!duplex.push(chunk)) {
|
|
35
|
+
// Our own consumer is saturated. `_read` will be called again when it wants more.
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if ((readable as Readable).readableEnded) {
|
|
41
|
+
onEnd()
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!readableListenerAttached) {
|
|
46
|
+
readableListenerAttached = true
|
|
47
|
+
readable.once('readable', onReadable)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const onReadable = () => {
|
|
52
|
+
readableListenerAttached = false
|
|
53
|
+
pump()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const detachReadableListener = () => {
|
|
57
|
+
if (readableListenerAttached) {
|
|
58
|
+
readableListenerAttached = false
|
|
59
|
+
readable.removeListener('readable', onReadable)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const onEnd = () => {
|
|
64
|
+
if (!ended) {
|
|
65
|
+
ended = true
|
|
66
|
+
detachReadableListener()
|
|
67
|
+
if (!duplex.destroyed) {
|
|
68
|
+
duplex.push(null)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const onClose = () => {
|
|
74
|
+
if (!ended) {
|
|
75
|
+
// The source stream was destroyed before it ended, so our own readable side can never complete.
|
|
76
|
+
detachReadableListener()
|
|
77
|
+
duplex.destroy(new Error('Premature close of the underlying readable stream.'))
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const onError = (err: Error) => {
|
|
82
|
+
duplex.destroy(err)
|
|
83
|
+
}
|
|
84
|
+
|
|
16
85
|
const duplex = new Duplex({
|
|
86
|
+
read() {
|
|
87
|
+
pump()
|
|
88
|
+
},
|
|
89
|
+
|
|
17
90
|
write(chunk, encoding, callback) {
|
|
18
91
|
writable.write(chunk, encoding, callback)
|
|
19
92
|
},
|
|
@@ -21,11 +94,22 @@ export class FullDuplexStream {
|
|
|
21
94
|
final(callback) {
|
|
22
95
|
writable.end(callback)
|
|
23
96
|
},
|
|
97
|
+
|
|
98
|
+
destroy(error, callback) {
|
|
99
|
+
// Stop pumping, but leave the 'error' handlers attached. If either underlying stream
|
|
100
|
+
// fails later, forwarding the error to this (already destroyed) stream harmlessly
|
|
101
|
+
// absorbs it instead of crashing the process with an unhandled 'error' event.
|
|
102
|
+
detachReadableListener()
|
|
103
|
+
readable.removeListener('end', onEnd)
|
|
104
|
+
readable.removeListener('close', onClose)
|
|
105
|
+
callback(error)
|
|
106
|
+
},
|
|
24
107
|
})
|
|
25
108
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
109
|
+
readable.on('end', onEnd)
|
|
110
|
+
readable.on('close', onClose)
|
|
111
|
+
readable.on('error', onError)
|
|
112
|
+
writable.on('error', onError)
|
|
29
113
|
|
|
30
114
|
return duplex
|
|
31
115
|
}
|