@tstdl/base 0.90.7 → 0.90.8
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/package.json
CHANGED
package/utils/stream/index.d.ts
CHANGED
package/utils/stream/index.js
CHANGED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { isNotNull, isNull } from '../type-guards.js';
|
|
2
|
+
export function toBytesStream(stream, options) {
|
|
3
|
+
try { // try to use byob mode from source
|
|
4
|
+
let byobReader;
|
|
5
|
+
return new ReadableStream({
|
|
6
|
+
type: 'bytes',
|
|
7
|
+
autoAllocateChunkSize: 10240,
|
|
8
|
+
start() {
|
|
9
|
+
byobReader = stream.getReader({ mode: 'byob' });
|
|
10
|
+
},
|
|
11
|
+
async pull(controller) {
|
|
12
|
+
const readResult = await byobReader.read(controller.byobRequest.view);
|
|
13
|
+
if (readResult.done) {
|
|
14
|
+
controller.close();
|
|
15
|
+
controller.byobRequest.respond(0);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
controller.byobRequest.respond(readResult.value.byteLength);
|
|
19
|
+
},
|
|
20
|
+
async cancel(reason) {
|
|
21
|
+
if (options?.ignoreCancel == true) {
|
|
22
|
+
byobReader.releaseLock();
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
await byobReader.cancel(reason);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
catch { /* ignore */ }
|
|
31
|
+
let reader;
|
|
32
|
+
let buffer = null;
|
|
33
|
+
return new ReadableStream({
|
|
34
|
+
type: 'bytes',
|
|
35
|
+
start() {
|
|
36
|
+
reader = stream.getReader();
|
|
37
|
+
},
|
|
38
|
+
async pull(controller) {
|
|
39
|
+
const isByobRequest = isNotNull(controller.byobRequest);
|
|
40
|
+
const bufferIsEmpty = isNull(buffer) || (buffer.byteLength == 0);
|
|
41
|
+
if (!isByobRequest && !bufferIsEmpty) {
|
|
42
|
+
// we stil have data left in buffer from previous pull which was byob
|
|
43
|
+
controller.enqueue(buffer);
|
|
44
|
+
buffer = null;
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (bufferIsEmpty) {
|
|
48
|
+
const readResult = await reader.read();
|
|
49
|
+
if (readResult.done) {
|
|
50
|
+
controller.close();
|
|
51
|
+
controller.byobRequest?.respond(0);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
buffer = readResult.value; // eslint-disable-line require-atomic-updates
|
|
55
|
+
}
|
|
56
|
+
if (!isByobRequest) {
|
|
57
|
+
controller.enqueue(buffer);
|
|
58
|
+
buffer = null;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const targetView = controller.byobRequest.view;
|
|
62
|
+
const targetArray = new Uint8Array(targetView.buffer, targetView.byteOffset, targetView.byteLength);
|
|
63
|
+
const setLength = Math.min(buffer.byteLength, targetArray.byteLength);
|
|
64
|
+
const sourceArray = new Uint8Array(buffer.buffer, buffer.byteOffset, setLength);
|
|
65
|
+
targetArray.set(sourceArray);
|
|
66
|
+
buffer = new Uint8Array(buffer.buffer, buffer.byteOffset + setLength);
|
|
67
|
+
controller.byobRequest.respond(setLength);
|
|
68
|
+
},
|
|
69
|
+
async cancel(reason) {
|
|
70
|
+
if (options?.ignoreCancel == true) {
|
|
71
|
+
reader.releaseLock();
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
await reader.cancel(reason);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|