@adviser/cement 0.1.11 → 0.1.12
Sign up to get free protection for your applications and to get access to all the features.
- package/base_sys_abstraction-BCdXJHNa.d.ts +21 -0
- package/base_sys_abstraction-BYZvxqsW.d.cts +21 -0
- package/chunk-J2IM7FHM.js +65 -0
- package/chunk-J2IM7FHM.js.map +1 -0
- package/chunk-MWAFFTM4.js +82 -0
- package/chunk-MWAFFTM4.js.map +1 -0
- package/chunk-PZTL4UNG.js +233 -0
- package/chunk-PZTL4UNG.js.map +1 -0
- package/index.cjs +987 -0
- package/index.cjs.map +1 -0
- package/index.d.cts +196 -0
- package/index.d.ts +196 -11
- package/index.js +611 -26
- package/index.js.map +1 -1
- package/node/index.cjs +436 -0
- package/node/index.cjs.map +1 -0
- package/node/index.d.cts +47 -0
- package/node/index.d.ts +47 -4
- package/node/index.js +215 -17
- package/node/index.js.map +1 -1
- package/package.json +34 -8
- package/sys_abstraction-CjljYIkv.d.cts +65 -0
- package/sys_abstraction-CjljYIkv.d.ts +65 -0
- package/utils/index.cjs +270 -0
- package/utils/index.cjs.map +1 -0
- package/utils/index.d.cts +33 -0
- package/utils/index.d.ts +33 -6
- package/utils/index.js +232 -20
- package/utils/index.js.map +1 -1
- package/web/index.cjs +286 -0
- package/web/index.cjs.map +1 -0
- package/web/index.d.cts +6 -0
- package/web/index.d.ts +6 -2
- package/web/index.js +7 -16
- package/web/index.js.map +1 -1
- package/base_sys_abstraction.d.ts +0 -79
- package/base_sys_abstraction.d.ts.map +0 -1
- package/base_sys_abstraction.js +0 -186
- package/base_sys_abstraction.js.map +0 -1
- package/base_sys_abstraction.test.d.ts +0 -2
- package/base_sys_abstraction.test.d.ts.map +0 -1
- package/base_sys_abstraction.test.js +0 -77
- package/base_sys_abstraction.test.js.map +0 -1
- package/file_service.d.ts +0 -18
- package/file_service.d.ts.map +0 -1
- package/file_service.js +0 -3
- package/file_service.js.map +0 -1
- package/future.d.ts +0 -8
- package/future.d.ts.map +0 -1
- package/future.js +0 -42
- package/future.js.map +0 -1
- package/future.test.d.ts +0 -2
- package/future.test.d.ts.map +0 -1
- package/future.test.js +0 -30
- package/future.test.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/logger.d.ts +0 -36
- package/logger.d.ts.map +0 -1
- package/logger.js +0 -11
- package/logger.js.map +0 -1
- package/logger.test.d.ts +0 -2
- package/logger.test.d.ts.map +0 -1
- package/logger.test.js +0 -372
- package/logger.test.js.map +0 -1
- package/logger_impl.d.ts +0 -64
- package/logger_impl.d.ts.map +0 -1
- package/logger_impl.js +0 -331
- package/logger_impl.js.map +0 -1
- package/node/index.d.ts.map +0 -1
- package/node/mock_file_service.d.ts +0 -11
- package/node/mock_file_service.d.ts.map +0 -1
- package/node/mock_file_service.js +0 -38
- package/node/mock_file_service.js.map +0 -1
- package/node/mock_file_service.test.d.ts +0 -2
- package/node/mock_file_service.test.d.ts.map +0 -1
- package/node/mock_file_service.test.js +0 -19
- package/node/mock_file_service.test.js.map +0 -1
- package/node/node_file_service.d.ts +0 -16
- package/node/node_file_service.d.ts.map +0 -1
- package/node/node_file_service.js +0 -84
- package/node/node_file_service.js.map +0 -1
- package/node/node_sys_abstraction.d.ts +0 -21
- package/node/node_sys_abstraction.d.ts.map +0 -1
- package/node/node_sys_abstraction.js +0 -101
- package/node/node_sys_abstraction.js.map +0 -1
- package/node/node_sys_abstraction.test.d.ts +0 -2
- package/node/node_sys_abstraction.test.d.ts.map +0 -1
- package/node/node_sys_abstraction.test.js +0 -57
- package/node/node_sys_abstraction.test.js.map +0 -1
- package/option.d.ts +0 -25
- package/option.d.ts.map +0 -1
- package/option.js +0 -53
- package/option.js.map +0 -1
- package/result.d.ts +0 -31
- package/result.d.ts.map +0 -1
- package/result.js +0 -69
- package/result.js.map +0 -1
- package/result.test.d.ts +0 -2
- package/result.test.d.ts.map +0 -1
- package/result.test.js +0 -39
- package/result.test.js.map +0 -1
- package/sys_abstraction.d.ts +0 -35
- package/sys_abstraction.d.ts.map +0 -1
- package/sys_abstraction.js +0 -35
- package/sys_abstraction.js.map +0 -1
- package/test/log_collector.d.ts +0 -22
- package/test/log_collector.d.ts.map +0 -1
- package/test/log_collector.js +0 -66
- package/test/log_collector.js.map +0 -1
- package/test/mock_logger.d.ts +0 -13
- package/test/mock_logger.d.ts.map +0 -1
- package/test/mock_logger.js +0 -30
- package/test/mock_logger.js.map +0 -1
- package/test/mock_logger.test.d.ts +0 -2
- package/test/mock_logger.test.d.ts.map +0 -1
- package/test/mock_logger.test.js +0 -49
- package/test/mock_logger.test.js.map +0 -1
- package/test/test-exit-handler.d.ts +0 -1
- package/test/test-exit-handler.d.ts.map +0 -1
- package/test/test-exit-handler.js +0 -76
- package/test/test-exit-handler.js.map +0 -1
- package/time.d.ts +0 -13
- package/time.d.ts.map +0 -1
- package/time.js +0 -18
- package/time.js.map +0 -1
- package/utils/index.d.ts.map +0 -1
- package/utils/rebuffer.d.ts +0 -3
- package/utils/rebuffer.d.ts.map +0 -1
- package/utils/rebuffer.js +0 -65
- package/utils/rebuffer.js.map +0 -1
- package/utils/rebuffer.test.d.ts +0 -2
- package/utils/rebuffer.test.d.ts.map +0 -1
- package/utils/rebuffer.test.js +0 -79
- package/utils/rebuffer.test.js.map +0 -1
- package/utils/stream2string.d.ts +0 -3
- package/utils/stream2string.d.ts.map +0 -1
- package/utils/stream2string.js +0 -53
- package/utils/stream2string.js.map +0 -1
- package/utils/stream2string.test.d.ts +0 -2
- package/utils/stream2string.test.d.ts.map +0 -1
- package/utils/stream2string.test.js +0 -31
- package/utils/stream2string.test.js.map +0 -1
- package/utils/stream_map.d.ts +0 -9
- package/utils/stream_map.d.ts.map +0 -1
- package/utils/stream_map.js +0 -67
- package/utils/stream_map.js.map +0 -1
- package/utils/stream_map.test.d.ts +0 -2
- package/utils/stream_map.test.d.ts.map +0 -1
- package/utils/stream_map.test.js +0 -89
- package/utils/stream_map.test.js.map +0 -1
- package/utils/stream_test_helper.d.ts +0 -15
- package/utils/stream_test_helper.d.ts.map +0 -1
- package/utils/stream_test_helper.js +0 -42
- package/utils/stream_test_helper.js.map +0 -1
- package/utils/string2stream.d.ts +0 -3
- package/utils/string2stream.d.ts.map +0 -1
- package/utils/string2stream.js +0 -18
- package/utils/string2stream.js.map +0 -1
- package/utils/string2stream.test.d.ts +0 -2
- package/utils/string2stream.test.d.ts.map +0 -1
- package/utils/string2stream.test.js +0 -9
- package/utils/string2stream.test.js.map +0 -1
- package/web/index.d.ts.map +0 -1
- package/web/web_sys_abstraction.d.ts +0 -4
- package/web/web_sys_abstraction.d.ts.map +0 -1
- package/web/web_sys_abstraction.js +0 -65
- package/web/web_sys_abstraction.js.map +0 -1
package/utils/index.cjs
ADDED
@@ -0,0 +1,270 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
|
+
var __export = (target, all) => {
|
7
|
+
for (var name in all)
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
9
|
+
};
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
12
|
+
for (let key of __getOwnPropNames(from))
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
15
|
+
}
|
16
|
+
return to;
|
17
|
+
};
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
|
+
|
20
|
+
// src/utils/index.ts
|
21
|
+
var utils_exports = {};
|
22
|
+
__export(utils_exports, {
|
23
|
+
array2stream: () => array2stream,
|
24
|
+
devnull: () => devnull,
|
25
|
+
rebuffer: () => rebuffer,
|
26
|
+
rebufferArray: () => rebufferArray,
|
27
|
+
receiveFromStream: () => receiveFromStream,
|
28
|
+
sendToStream: () => sendToStream,
|
29
|
+
stream2array: () => stream2array,
|
30
|
+
stream2string: () => stream2string,
|
31
|
+
stream2uint8array: () => stream2uint8array,
|
32
|
+
streamMap: () => streamMap,
|
33
|
+
string2stream: () => string2stream,
|
34
|
+
uint8array2stream: () => uint8array2stream
|
35
|
+
});
|
36
|
+
module.exports = __toCommonJS(utils_exports);
|
37
|
+
|
38
|
+
// src/utils/stream_map.ts
|
39
|
+
function streamMap(s, sm) {
|
40
|
+
const state = { reader: s.getReader(), streamMap: sm, idx: 0 };
|
41
|
+
return new ReadableStream({
|
42
|
+
async pull(controller) {
|
43
|
+
const { done, value } = await state.reader.read();
|
44
|
+
if (done) {
|
45
|
+
state.streamMap.Close && state.streamMap.Close();
|
46
|
+
controller.close();
|
47
|
+
return;
|
48
|
+
}
|
49
|
+
const promiseOrU = state.streamMap.Map(value, state.idx++);
|
50
|
+
let mapped;
|
51
|
+
if (promiseOrU instanceof Promise || typeof promiseOrU.then === "function") {
|
52
|
+
mapped = await promiseOrU;
|
53
|
+
} else {
|
54
|
+
mapped = promiseOrU;
|
55
|
+
}
|
56
|
+
controller.enqueue(mapped);
|
57
|
+
}
|
58
|
+
});
|
59
|
+
}
|
60
|
+
async function devnull(a) {
|
61
|
+
const reader = a.getReader();
|
62
|
+
let cnt = 0;
|
63
|
+
while (true) {
|
64
|
+
const { done } = await reader.read();
|
65
|
+
if (done) {
|
66
|
+
break;
|
67
|
+
}
|
68
|
+
cnt++;
|
69
|
+
}
|
70
|
+
return cnt;
|
71
|
+
}
|
72
|
+
function array2stream(a) {
|
73
|
+
let i = 0;
|
74
|
+
return new ReadableStream({
|
75
|
+
pull(controller) {
|
76
|
+
if (i >= a.length) {
|
77
|
+
controller.close();
|
78
|
+
return;
|
79
|
+
}
|
80
|
+
controller.enqueue(a[i]);
|
81
|
+
i++;
|
82
|
+
}
|
83
|
+
});
|
84
|
+
}
|
85
|
+
async function stream2array(a) {
|
86
|
+
const ret = [];
|
87
|
+
const reader = a.getReader();
|
88
|
+
while (true) {
|
89
|
+
const { done, value } = await reader.read();
|
90
|
+
if (done) {
|
91
|
+
break;
|
92
|
+
}
|
93
|
+
ret.push(value);
|
94
|
+
}
|
95
|
+
return ret;
|
96
|
+
}
|
97
|
+
|
98
|
+
// src/utils/rebuffer.ts
|
99
|
+
async function rebufferArray(a, chunkSize) {
|
100
|
+
return stream2array(rebuffer(array2stream(a), chunkSize));
|
101
|
+
}
|
102
|
+
function reChunk(cs, chunkSize) {
|
103
|
+
const len = cs.reduce((acc, v) => acc + v.length, 0);
|
104
|
+
const last = cs[cs.length - 1];
|
105
|
+
const lastOfs = len - last.length;
|
106
|
+
const rest = last.subarray(chunkSize - lastOfs);
|
107
|
+
cs[cs.length - 1] = last.subarray(0, chunkSize - lastOfs);
|
108
|
+
const chunk = new Uint8Array(chunkSize);
|
109
|
+
let ofs = 0;
|
110
|
+
for (const c of cs) {
|
111
|
+
chunk.set(c, ofs);
|
112
|
+
ofs += c.length;
|
113
|
+
}
|
114
|
+
return { rest, chunk };
|
115
|
+
}
|
116
|
+
function pump(ps, controller, next) {
|
117
|
+
ps.reader.read().then(({ done, value }) => {
|
118
|
+
if (done) {
|
119
|
+
if (ps.tmpLen > 0) {
|
120
|
+
controller.enqueue(reChunk(ps.tmp, ps.tmpLen).chunk);
|
121
|
+
}
|
122
|
+
controller.close();
|
123
|
+
next();
|
124
|
+
return;
|
125
|
+
}
|
126
|
+
if (ps.tmpLen + value.length > ps.chunkSize) {
|
127
|
+
ps.tmp.push(value);
|
128
|
+
const res = reChunk(ps.tmp, ps.chunkSize);
|
129
|
+
controller.enqueue(res.chunk);
|
130
|
+
ps.tmp = [res.rest];
|
131
|
+
ps.tmpLen = res.rest.length;
|
132
|
+
next();
|
133
|
+
return;
|
134
|
+
} else if (value.length) {
|
135
|
+
ps.tmp.push(value);
|
136
|
+
ps.tmpLen += value.length;
|
137
|
+
}
|
138
|
+
pump(ps, controller, next);
|
139
|
+
});
|
140
|
+
}
|
141
|
+
function rebuffer(a, chunkSize) {
|
142
|
+
const state = {
|
143
|
+
reader: a.getReader(),
|
144
|
+
tmp: [],
|
145
|
+
tmpLen: 0,
|
146
|
+
chunkSize
|
147
|
+
};
|
148
|
+
return new ReadableStream({
|
149
|
+
async pull(controller) {
|
150
|
+
return new Promise((resolve) => {
|
151
|
+
pump(state, controller, resolve);
|
152
|
+
});
|
153
|
+
}
|
154
|
+
});
|
155
|
+
}
|
156
|
+
|
157
|
+
// src/utils/stream2string.ts
|
158
|
+
async function stream2string(stream, maxSize) {
|
159
|
+
if (!stream) {
|
160
|
+
return Promise.resolve("");
|
161
|
+
}
|
162
|
+
const reader = stream.getReader();
|
163
|
+
let res = "";
|
164
|
+
const decoder = new TextDecoder();
|
165
|
+
let rSize = 0;
|
166
|
+
while (typeof maxSize === "undefined" || rSize < maxSize) {
|
167
|
+
try {
|
168
|
+
const read = await reader.read();
|
169
|
+
if (read.done) {
|
170
|
+
break;
|
171
|
+
}
|
172
|
+
if (maxSize && rSize + read.value.length > maxSize) {
|
173
|
+
read.value = read.value.slice(0, maxSize - rSize);
|
174
|
+
}
|
175
|
+
const block = decoder.decode(read.value, { stream: true });
|
176
|
+
rSize += read.value.length;
|
177
|
+
res += block;
|
178
|
+
} catch (err) {
|
179
|
+
return Promise.reject(err);
|
180
|
+
}
|
181
|
+
}
|
182
|
+
return Promise.resolve(res);
|
183
|
+
}
|
184
|
+
async function stream2uint8array(stream) {
|
185
|
+
if (!stream) {
|
186
|
+
return Promise.resolve(new Uint8Array());
|
187
|
+
}
|
188
|
+
const reader = stream.getReader();
|
189
|
+
let res = new Uint8Array();
|
190
|
+
while (1) {
|
191
|
+
try {
|
192
|
+
const { done, value } = await reader.read();
|
193
|
+
if (done) {
|
194
|
+
break;
|
195
|
+
}
|
196
|
+
res = new Uint8Array([...res, ...value]);
|
197
|
+
} catch (err) {
|
198
|
+
return Promise.reject(err);
|
199
|
+
}
|
200
|
+
}
|
201
|
+
return Promise.resolve(res);
|
202
|
+
}
|
203
|
+
|
204
|
+
// src/utils/stream_test_helper.ts
|
205
|
+
async function receiveFromStream(reb, state) {
|
206
|
+
return new Promise((resolve) => {
|
207
|
+
let reBufferCalls = 0;
|
208
|
+
const reader = reb.getReader();
|
209
|
+
function pump2() {
|
210
|
+
reader.read().then(({ done, value }) => {
|
211
|
+
state.CollectorFn({ done, value, fillCalls: state.fillCalls, reBufferCalls });
|
212
|
+
reBufferCalls++;
|
213
|
+
if (done) {
|
214
|
+
resolve();
|
215
|
+
return;
|
216
|
+
}
|
217
|
+
pump2();
|
218
|
+
});
|
219
|
+
}
|
220
|
+
pump2();
|
221
|
+
});
|
222
|
+
}
|
223
|
+
async function sendToStream(reb, state) {
|
224
|
+
return new Promise((resolve) => {
|
225
|
+
const writer = reb.getWriter();
|
226
|
+
function pump2(i) {
|
227
|
+
if (i >= state.sendChunks) {
|
228
|
+
writer.close();
|
229
|
+
resolve();
|
230
|
+
return;
|
231
|
+
}
|
232
|
+
writer.ready.then(() => {
|
233
|
+
state.fillCalls++;
|
234
|
+
writer.write(new Uint8Array(Array(state.sendChunkSize).fill(i)));
|
235
|
+
pump2(i + 1);
|
236
|
+
});
|
237
|
+
}
|
238
|
+
pump2(0);
|
239
|
+
});
|
240
|
+
}
|
241
|
+
|
242
|
+
// src/utils/string2stream.ts
|
243
|
+
function string2stream(str) {
|
244
|
+
const encoder = new TextEncoder();
|
245
|
+
return uint8array2stream(encoder.encode(str));
|
246
|
+
}
|
247
|
+
function uint8array2stream(str) {
|
248
|
+
return new ReadableStream({
|
249
|
+
start(controller) {
|
250
|
+
controller.enqueue(str);
|
251
|
+
controller.close();
|
252
|
+
}
|
253
|
+
});
|
254
|
+
}
|
255
|
+
// Annotate the CommonJS export names for ESM import in node:
|
256
|
+
0 && (module.exports = {
|
257
|
+
array2stream,
|
258
|
+
devnull,
|
259
|
+
rebuffer,
|
260
|
+
rebufferArray,
|
261
|
+
receiveFromStream,
|
262
|
+
sendToStream,
|
263
|
+
stream2array,
|
264
|
+
stream2string,
|
265
|
+
stream2uint8array,
|
266
|
+
streamMap,
|
267
|
+
string2stream,
|
268
|
+
uint8array2stream
|
269
|
+
});
|
270
|
+
//# sourceMappingURL=index.cjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../../src/utils/index.ts","../../../src/utils/stream_map.ts","../../../src/utils/rebuffer.ts","../../../src/utils/stream2string.ts","../../../src/utils/stream_test_helper.ts","../../../src/utils/string2stream.ts"],"sourcesContent":["export * from \"./rebuffer\";\nexport * from \"./stream_map\";\nexport * from \"./stream2string\";\nexport * from \"./stream_test_helper\";\nexport * from \"./string2stream\";\n","export interface StreamMap<T, U> {\n Map(s: T, idx: number): U | Promise<U>;\n readonly Close?: () => void;\n}\nexport function streamMap<T, U>(s: ReadableStream<T>, sm: StreamMap<T, U>): ReadableStream<U> {\n const state = { reader: s.getReader(), streamMap: sm, idx: 0 };\n return new ReadableStream<U>({\n async pull(controller) {\n const { done, value } = await state.reader.read();\n if (done) {\n state.streamMap.Close && state.streamMap.Close();\n controller.close();\n return;\n }\n const promiseOrU = state.streamMap.Map(value, state.idx++);\n let mapped: U;\n if (promiseOrU instanceof Promise || typeof (promiseOrU as { then: () => void }).then === \"function\") {\n mapped = await promiseOrU;\n } else {\n mapped = promiseOrU;\n }\n controller.enqueue(mapped);\n },\n });\n}\n\nexport async function devnull<T>(a: ReadableStream<T>): Promise<number> {\n const reader = a.getReader();\n let cnt = 0;\n while (true) {\n const { done } = await reader.read();\n if (done) {\n break;\n }\n cnt++;\n }\n return cnt;\n}\n\nexport function array2stream<T>(a: T[]): ReadableStream<T> {\n let i = 0;\n return new ReadableStream<T>({\n pull(controller) {\n if (i >= a.length) {\n controller.close();\n return;\n }\n controller.enqueue(a[i]);\n i++;\n },\n });\n}\n\nexport async function stream2array<T>(a: ReadableStream<T>): Promise<T[]> {\n const ret: T[] = [];\n const reader = a.getReader();\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n ret.push(value);\n }\n return ret;\n}\n","import { array2stream, stream2array } from \"./stream_map\";\n\ninterface ReChunkResult {\n readonly rest: Uint8Array;\n readonly chunk: Uint8Array;\n}\n\nexport async function rebufferArray(a: Uint8Array[], chunkSize: number): Promise<Uint8Array[]> {\n return stream2array(rebuffer(array2stream(a), chunkSize));\n}\n\nfunction reChunk(cs: Uint8Array[], chunkSize: number): ReChunkResult {\n const len = cs.reduce((acc, v) => acc + v.length, 0);\n const last = cs[cs.length - 1];\n const lastOfs = len - last.length;\n // console.log(\"reChunk\", len, lastOfs, last.length, chunkSize, chunkSize - lastOfs)\n const rest = last.subarray(chunkSize - lastOfs);\n cs[cs.length - 1] = last.subarray(0, chunkSize - lastOfs);\n const chunk = new Uint8Array(chunkSize);\n let ofs = 0;\n for (const c of cs) {\n chunk.set(c, ofs);\n ofs += c.length;\n }\n return { rest, chunk };\n}\n\ninterface pumpState {\n readonly reader: ReadableStreamDefaultReader<Uint8Array>;\n tmp: Uint8Array[];\n tmpLen: number;\n readonly chunkSize: number;\n}\n\nfunction pump(ps: pumpState, controller: ReadableStreamDefaultController<Uint8Array>, next: () => void): void {\n ps.reader.read().then(({ done, value }) => {\n if (done) {\n if (ps.tmpLen > 0) {\n controller.enqueue(reChunk(ps.tmp, ps.tmpLen).chunk);\n }\n controller.close();\n next();\n return;\n }\n if (ps.tmpLen + value.length > ps.chunkSize) {\n ps.tmp.push(value);\n const res = reChunk(ps.tmp, ps.chunkSize);\n controller.enqueue(res.chunk);\n ps.tmp = [res.rest];\n ps.tmpLen = res.rest.length;\n next();\n return;\n } else if (value.length) {\n ps.tmp.push(value);\n ps.tmpLen += value.length;\n }\n pump(ps, controller, next);\n });\n}\n\nexport function rebuffer(a: ReadableStream<Uint8Array>, chunkSize: number): ReadableStream<Uint8Array> {\n const state: pumpState = {\n reader: a.getReader(),\n tmp: [],\n tmpLen: 0,\n chunkSize,\n };\n return new ReadableStream<Uint8Array>({\n async pull(controller) {\n return new Promise<void>((resolve) => {\n pump(state, controller, resolve);\n });\n },\n });\n}\n","export async function stream2string(stream?: ReadableStream<Uint8Array> | null, maxSize?: number): Promise<string> {\n if (!stream) {\n return Promise.resolve(\"\");\n }\n const reader = stream.getReader();\n let res = \"\";\n const decoder = new TextDecoder();\n let rSize = 0;\n while (typeof maxSize === \"undefined\" || rSize < maxSize) {\n try {\n const read = await reader.read();\n if (read.done) {\n break;\n }\n if (maxSize && rSize + read.value.length > maxSize) {\n read.value = read.value.slice(0, maxSize - rSize);\n }\n const block = decoder.decode(read.value, { stream: true });\n rSize += read.value.length;\n res += block;\n } catch (err) {\n return Promise.reject(err);\n }\n }\n return Promise.resolve(res);\n}\n\nexport async function stream2uint8array(stream?: ReadableStream<Uint8Array> | null): Promise<Uint8Array> {\n if (!stream) {\n return Promise.resolve(new Uint8Array());\n }\n const reader = stream.getReader();\n let res = new Uint8Array();\n // eslint-disable-next-line no-constant-condition\n while (1) {\n try {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n res = new Uint8Array([...res, ...value]);\n } catch (err) {\n return Promise.reject(err);\n }\n }\n return Promise.resolve(res);\n}\n","export interface streamingTestState {\n readonly sendChunks: number;\n readonly sendChunkSize: number;\n fillCalls: number;\n CollectorFn: jest.Mock<void, [{ done: boolean; value: Uint8Array | undefined; fillCalls: number; reBufferCalls: number }]>;\n}\n\nexport async function receiveFromStream(reb: ReadableStream<Uint8Array>, state: streamingTestState) {\n return new Promise<void>((resolve) => {\n let reBufferCalls = 0;\n const reader = reb.getReader();\n function pump() {\n reader.read().then(({ done, value }) => {\n state.CollectorFn({ done, value, fillCalls: state.fillCalls, reBufferCalls });\n reBufferCalls++;\n if (done) {\n resolve();\n return;\n }\n pump();\n });\n }\n pump();\n });\n}\n\nexport async function sendToStream(reb: WritableStream<Uint8Array>, state: streamingTestState) {\n return new Promise<void>((resolve) => {\n const writer = reb.getWriter();\n function pump(i: number) {\n if (i >= state.sendChunks) {\n writer.close();\n resolve();\n return;\n }\n writer.ready.then(() => {\n state.fillCalls++;\n writer.write(new Uint8Array(Array(state.sendChunkSize).fill(i)));\n pump(i + 1);\n });\n }\n pump(0);\n });\n}\n\n// it(\"does nothing\", () => {});\n","export function string2stream(str: string): ReadableStream<Uint8Array> {\n const encoder = new TextEncoder();\n return uint8array2stream(encoder.encode(str));\n}\n\nexport function uint8array2stream(str: Uint8Array): ReadableStream<Uint8Array> {\n return new ReadableStream<Uint8Array>({\n start(controller) {\n controller.enqueue(str);\n controller.close();\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACIO,SAAS,UAAgB,GAAsB,IAAwC;AAC5F,QAAM,QAAQ,EAAE,QAAQ,EAAE,UAAU,GAAG,WAAW,IAAI,KAAK,EAAE;AAC7D,SAAO,IAAI,eAAkB;AAAA,IAC3B,MAAM,KAAK,YAAY;AACrB,YAAM,EAAE,MAAM,MAAM,IAAI,MAAM,MAAM,OAAO,KAAK;AAChD,UAAI,MAAM;AACR,cAAM,UAAU,SAAS,MAAM,UAAU,MAAM;AAC/C,mBAAW,MAAM;AACjB;AAAA,MACF;AACA,YAAM,aAAa,MAAM,UAAU,IAAI,OAAO,MAAM,KAAK;AACzD,UAAI;AACJ,UAAI,sBAAsB,WAAW,OAAQ,WAAoC,SAAS,YAAY;AACpG,iBAAS,MAAM;AAAA,MACjB,OAAO;AACL,iBAAS;AAAA,MACX;AACA,iBAAW,QAAQ,MAAM;AAAA,IAC3B;AAAA,EACF,CAAC;AACH;AAEA,eAAsB,QAAW,GAAuC;AACtE,QAAM,SAAS,EAAE,UAAU;AAC3B,MAAI,MAAM;AACV,SAAO,MAAM;AACX,UAAM,EAAE,KAAK,IAAI,MAAM,OAAO,KAAK;AACnC,QAAI,MAAM;AACR;AAAA,IACF;AACA;AAAA,EACF;AACA,SAAO;AACT;AAEO,SAAS,aAAgB,GAA2B;AACzD,MAAI,IAA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;AACb,gBAAQ;AACR;AAAA,MACF;AACA,aAAO,MAAM,KAAK,MAAM;AACtB,cAAM;AACN,eAAO,MAAM,IAAI,WAAW,MAAM,MAAM,aAAa,EAAE,KAAK,CAAC,CAAC,CAAC;AAC/D,QAAAA,MAAK,IAAI,CAAC;AAAA,MACZ,CAAC;AAAA,IACH;AACA,IAAAA,MAAK,CAAC;AAAA,EACR,CAAC;AACH;;;AC3CO,SAAS,cAAc,KAAyC;AACrE,QAAM,UAAU,IAAI,YAAY;AAChC,SAAO,kBAAkB,QAAQ,OAAO,GAAG,CAAC;AAC9C;AAEO,SAAS,kBAAkB,KAA6C;AAC7E,SAAO,IAAI,eAA2B;AAAA,IACpC,MAAM,YAAY;AAChB,iBAAW,QAAQ,GAAG;AACtB,iBAAW,MAAM;AAAA,IACnB;AAAA,EACF,CAAC;AACH;","names":["pump"]}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
declare function rebufferArray(a: Uint8Array[], chunkSize: number): Promise<Uint8Array[]>;
|
2
|
+
declare function rebuffer(a: ReadableStream<Uint8Array>, chunkSize: number): ReadableStream<Uint8Array>;
|
3
|
+
|
4
|
+
interface StreamMap<T, U> {
|
5
|
+
Map(s: T, idx: number): U | Promise<U>;
|
6
|
+
readonly Close?: () => void;
|
7
|
+
}
|
8
|
+
declare function streamMap<T, U>(s: ReadableStream<T>, sm: StreamMap<T, U>): ReadableStream<U>;
|
9
|
+
declare function devnull<T>(a: ReadableStream<T>): Promise<number>;
|
10
|
+
declare function array2stream<T>(a: T[]): ReadableStream<T>;
|
11
|
+
declare function stream2array<T>(a: ReadableStream<T>): Promise<T[]>;
|
12
|
+
|
13
|
+
declare function stream2string(stream?: ReadableStream<Uint8Array> | null, maxSize?: number): Promise<string>;
|
14
|
+
declare function stream2uint8array(stream?: ReadableStream<Uint8Array> | null): Promise<Uint8Array>;
|
15
|
+
|
16
|
+
interface streamingTestState {
|
17
|
+
readonly sendChunks: number;
|
18
|
+
readonly sendChunkSize: number;
|
19
|
+
fillCalls: number;
|
20
|
+
CollectorFn: jest.Mock<void, [{
|
21
|
+
done: boolean;
|
22
|
+
value: Uint8Array | undefined;
|
23
|
+
fillCalls: number;
|
24
|
+
reBufferCalls: number;
|
25
|
+
}]>;
|
26
|
+
}
|
27
|
+
declare function receiveFromStream(reb: ReadableStream<Uint8Array>, state: streamingTestState): Promise<void>;
|
28
|
+
declare function sendToStream(reb: WritableStream<Uint8Array>, state: streamingTestState): Promise<void>;
|
29
|
+
|
30
|
+
declare function string2stream(str: string): ReadableStream<Uint8Array>;
|
31
|
+
declare function uint8array2stream(str: Uint8Array): ReadableStream<Uint8Array>;
|
32
|
+
|
33
|
+
export { type StreamMap, array2stream, devnull, rebuffer, rebufferArray, receiveFromStream, sendToStream, stream2array, stream2string, stream2uint8array, streamMap, type streamingTestState, string2stream, uint8array2stream };
|
package/utils/index.d.ts
CHANGED
@@ -1,6 +1,33 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
declare function rebufferArray(a: Uint8Array[], chunkSize: number): Promise<Uint8Array[]>;
|
2
|
+
declare function rebuffer(a: ReadableStream<Uint8Array>, chunkSize: number): ReadableStream<Uint8Array>;
|
3
|
+
|
4
|
+
interface StreamMap<T, U> {
|
5
|
+
Map(s: T, idx: number): U | Promise<U>;
|
6
|
+
readonly Close?: () => void;
|
7
|
+
}
|
8
|
+
declare function streamMap<T, U>(s: ReadableStream<T>, sm: StreamMap<T, U>): ReadableStream<U>;
|
9
|
+
declare function devnull<T>(a: ReadableStream<T>): Promise<number>;
|
10
|
+
declare function array2stream<T>(a: T[]): ReadableStream<T>;
|
11
|
+
declare function stream2array<T>(a: ReadableStream<T>): Promise<T[]>;
|
12
|
+
|
13
|
+
declare function stream2string(stream?: ReadableStream<Uint8Array> | null, maxSize?: number): Promise<string>;
|
14
|
+
declare function stream2uint8array(stream?: ReadableStream<Uint8Array> | null): Promise<Uint8Array>;
|
15
|
+
|
16
|
+
interface streamingTestState {
|
17
|
+
readonly sendChunks: number;
|
18
|
+
readonly sendChunkSize: number;
|
19
|
+
fillCalls: number;
|
20
|
+
CollectorFn: jest.Mock<void, [{
|
21
|
+
done: boolean;
|
22
|
+
value: Uint8Array | undefined;
|
23
|
+
fillCalls: number;
|
24
|
+
reBufferCalls: number;
|
25
|
+
}]>;
|
26
|
+
}
|
27
|
+
declare function receiveFromStream(reb: ReadableStream<Uint8Array>, state: streamingTestState): Promise<void>;
|
28
|
+
declare function sendToStream(reb: WritableStream<Uint8Array>, state: streamingTestState): Promise<void>;
|
29
|
+
|
30
|
+
declare function string2stream(str: string): ReadableStream<Uint8Array>;
|
31
|
+
declare function uint8array2stream(str: Uint8Array): ReadableStream<Uint8Array>;
|
32
|
+
|
33
|
+
export { type StreamMap, array2stream, devnull, rebuffer, rebufferArray, receiveFromStream, sendToStream, stream2array, stream2string, stream2uint8array, streamMap, type streamingTestState, string2stream, uint8array2stream };
|
package/utils/index.js
CHANGED
@@ -1,22 +1,234 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
1
|
+
import "../chunk-J2IM7FHM.js";
|
2
|
+
|
3
|
+
// src/utils/stream_map.ts
|
4
|
+
function streamMap(s, sm) {
|
5
|
+
const state = { reader: s.getReader(), streamMap: sm, idx: 0 };
|
6
|
+
return new ReadableStream({
|
7
|
+
async pull(controller) {
|
8
|
+
const { done, value } = await state.reader.read();
|
9
|
+
if (done) {
|
10
|
+
state.streamMap.Close && state.streamMap.Close();
|
11
|
+
controller.close();
|
12
|
+
return;
|
13
|
+
}
|
14
|
+
const promiseOrU = state.streamMap.Map(value, state.idx++);
|
15
|
+
let mapped;
|
16
|
+
if (promiseOrU instanceof Promise || typeof promiseOrU.then === "function") {
|
17
|
+
mapped = await promiseOrU;
|
18
|
+
} else {
|
19
|
+
mapped = promiseOrU;
|
20
|
+
}
|
21
|
+
controller.enqueue(mapped);
|
22
|
+
}
|
23
|
+
});
|
24
|
+
}
|
25
|
+
async function devnull(a) {
|
26
|
+
const reader = a.getReader();
|
27
|
+
let cnt = 0;
|
28
|
+
while (true) {
|
29
|
+
const { done } = await reader.read();
|
30
|
+
if (done) {
|
31
|
+
break;
|
32
|
+
}
|
33
|
+
cnt++;
|
34
|
+
}
|
35
|
+
return cnt;
|
36
|
+
}
|
37
|
+
function array2stream(a) {
|
38
|
+
let i = 0;
|
39
|
+
return new ReadableStream({
|
40
|
+
pull(controller) {
|
41
|
+
if (i >= a.length) {
|
42
|
+
controller.close();
|
43
|
+
return;
|
44
|
+
}
|
45
|
+
controller.enqueue(a[i]);
|
46
|
+
i++;
|
47
|
+
}
|
48
|
+
});
|
49
|
+
}
|
50
|
+
async function stream2array(a) {
|
51
|
+
const ret = [];
|
52
|
+
const reader = a.getReader();
|
53
|
+
while (true) {
|
54
|
+
const { done, value } = await reader.read();
|
55
|
+
if (done) {
|
56
|
+
break;
|
57
|
+
}
|
58
|
+
ret.push(value);
|
59
|
+
}
|
60
|
+
return ret;
|
61
|
+
}
|
62
|
+
|
63
|
+
// src/utils/rebuffer.ts
|
64
|
+
async function rebufferArray(a, chunkSize) {
|
65
|
+
return stream2array(rebuffer(array2stream(a), chunkSize));
|
66
|
+
}
|
67
|
+
function reChunk(cs, chunkSize) {
|
68
|
+
const len = cs.reduce((acc, v) => acc + v.length, 0);
|
69
|
+
const last = cs[cs.length - 1];
|
70
|
+
const lastOfs = len - last.length;
|
71
|
+
const rest = last.subarray(chunkSize - lastOfs);
|
72
|
+
cs[cs.length - 1] = last.subarray(0, chunkSize - lastOfs);
|
73
|
+
const chunk = new Uint8Array(chunkSize);
|
74
|
+
let ofs = 0;
|
75
|
+
for (const c of cs) {
|
76
|
+
chunk.set(c, ofs);
|
77
|
+
ofs += c.length;
|
78
|
+
}
|
79
|
+
return { rest, chunk };
|
80
|
+
}
|
81
|
+
function pump(ps, controller, next) {
|
82
|
+
ps.reader.read().then(({ done, value }) => {
|
83
|
+
if (done) {
|
84
|
+
if (ps.tmpLen > 0) {
|
85
|
+
controller.enqueue(reChunk(ps.tmp, ps.tmpLen).chunk);
|
86
|
+
}
|
87
|
+
controller.close();
|
88
|
+
next();
|
89
|
+
return;
|
90
|
+
}
|
91
|
+
if (ps.tmpLen + value.length > ps.chunkSize) {
|
92
|
+
ps.tmp.push(value);
|
93
|
+
const res = reChunk(ps.tmp, ps.chunkSize);
|
94
|
+
controller.enqueue(res.chunk);
|
95
|
+
ps.tmp = [res.rest];
|
96
|
+
ps.tmpLen = res.rest.length;
|
97
|
+
next();
|
98
|
+
return;
|
99
|
+
} else if (value.length) {
|
100
|
+
ps.tmp.push(value);
|
101
|
+
ps.tmpLen += value.length;
|
102
|
+
}
|
103
|
+
pump(ps, controller, next);
|
104
|
+
});
|
105
|
+
}
|
106
|
+
function rebuffer(a, chunkSize) {
|
107
|
+
const state = {
|
108
|
+
reader: a.getReader(),
|
109
|
+
tmp: [],
|
110
|
+
tmpLen: 0,
|
111
|
+
chunkSize
|
112
|
+
};
|
113
|
+
return new ReadableStream({
|
114
|
+
async pull(controller) {
|
115
|
+
return new Promise((resolve) => {
|
116
|
+
pump(state, controller, resolve);
|
117
|
+
});
|
118
|
+
}
|
119
|
+
});
|
120
|
+
}
|
121
|
+
|
122
|
+
// src/utils/stream2string.ts
|
123
|
+
async function stream2string(stream, maxSize) {
|
124
|
+
if (!stream) {
|
125
|
+
return Promise.resolve("");
|
126
|
+
}
|
127
|
+
const reader = stream.getReader();
|
128
|
+
let res = "";
|
129
|
+
const decoder = new TextDecoder();
|
130
|
+
let rSize = 0;
|
131
|
+
while (typeof maxSize === "undefined" || rSize < maxSize) {
|
132
|
+
try {
|
133
|
+
const read = await reader.read();
|
134
|
+
if (read.done) {
|
135
|
+
break;
|
136
|
+
}
|
137
|
+
if (maxSize && rSize + read.value.length > maxSize) {
|
138
|
+
read.value = read.value.slice(0, maxSize - rSize);
|
139
|
+
}
|
140
|
+
const block = decoder.decode(read.value, { stream: true });
|
141
|
+
rSize += read.value.length;
|
142
|
+
res += block;
|
143
|
+
} catch (err) {
|
144
|
+
return Promise.reject(err);
|
145
|
+
}
|
146
|
+
}
|
147
|
+
return Promise.resolve(res);
|
148
|
+
}
|
149
|
+
async function stream2uint8array(stream) {
|
150
|
+
if (!stream) {
|
151
|
+
return Promise.resolve(new Uint8Array());
|
152
|
+
}
|
153
|
+
const reader = stream.getReader();
|
154
|
+
let res = new Uint8Array();
|
155
|
+
while (1) {
|
156
|
+
try {
|
157
|
+
const { done, value } = await reader.read();
|
158
|
+
if (done) {
|
159
|
+
break;
|
160
|
+
}
|
161
|
+
res = new Uint8Array([...res, ...value]);
|
162
|
+
} catch (err) {
|
163
|
+
return Promise.reject(err);
|
164
|
+
}
|
165
|
+
}
|
166
|
+
return Promise.resolve(res);
|
167
|
+
}
|
168
|
+
|
169
|
+
// src/utils/stream_test_helper.ts
|
170
|
+
async function receiveFromStream(reb, state) {
|
171
|
+
return new Promise((resolve) => {
|
172
|
+
let reBufferCalls = 0;
|
173
|
+
const reader = reb.getReader();
|
174
|
+
function pump2() {
|
175
|
+
reader.read().then(({ done, value }) => {
|
176
|
+
state.CollectorFn({ done, value, fillCalls: state.fillCalls, reBufferCalls });
|
177
|
+
reBufferCalls++;
|
178
|
+
if (done) {
|
179
|
+
resolve();
|
180
|
+
return;
|
181
|
+
}
|
182
|
+
pump2();
|
183
|
+
});
|
184
|
+
}
|
185
|
+
pump2();
|
186
|
+
});
|
187
|
+
}
|
188
|
+
async function sendToStream(reb, state) {
|
189
|
+
return new Promise((resolve) => {
|
190
|
+
const writer = reb.getWriter();
|
191
|
+
function pump2(i) {
|
192
|
+
if (i >= state.sendChunks) {
|
193
|
+
writer.close();
|
194
|
+
resolve();
|
195
|
+
return;
|
196
|
+
}
|
197
|
+
writer.ready.then(() => {
|
198
|
+
state.fillCalls++;
|
199
|
+
writer.write(new Uint8Array(Array(state.sendChunkSize).fill(i)));
|
200
|
+
pump2(i + 1);
|
201
|
+
});
|
202
|
+
}
|
203
|
+
pump2(0);
|
204
|
+
});
|
205
|
+
}
|
206
|
+
|
207
|
+
// src/utils/string2stream.ts
|
208
|
+
function string2stream(str) {
|
209
|
+
const encoder = new TextEncoder();
|
210
|
+
return uint8array2stream(encoder.encode(str));
|
211
|
+
}
|
212
|
+
function uint8array2stream(str) {
|
213
|
+
return new ReadableStream({
|
214
|
+
start(controller) {
|
215
|
+
controller.enqueue(str);
|
216
|
+
controller.close();
|
217
|
+
}
|
218
|
+
});
|
219
|
+
}
|
220
|
+
export {
|
221
|
+
array2stream,
|
222
|
+
devnull,
|
223
|
+
rebuffer,
|
224
|
+
rebufferArray,
|
225
|
+
receiveFromStream,
|
226
|
+
sendToStream,
|
227
|
+
stream2array,
|
228
|
+
stream2string,
|
229
|
+
stream2uint8array,
|
230
|
+
streamMap,
|
231
|
+
string2stream,
|
232
|
+
uint8array2stream
|
15
233
|
};
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
__exportStar(require("./rebuffer"), exports);
|
18
|
-
__exportStar(require("./stream_map"), exports);
|
19
|
-
__exportStar(require("./stream2string"), exports);
|
20
|
-
__exportStar(require("./stream_test_helper"), exports);
|
21
|
-
__exportStar(require("./string2stream"), exports);
|
22
234
|
//# sourceMappingURL=index.js.map
|
package/utils/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,6CAA2B;AAC3B,+CAA6B;AAC7B,kDAAgC;AAChC,uDAAqC;AACrC,kDAAgC"}
|
1
|
+
{"version":3,"sources":["../../../src/utils/stream_map.ts","../../../src/utils/rebuffer.ts","../../../src/utils/stream2string.ts","../../../src/utils/stream_test_helper.ts","../../../src/utils/string2stream.ts"],"sourcesContent":["export interface StreamMap<T, U> {\n Map(s: T, idx: number): U | Promise<U>;\n readonly Close?: () => void;\n}\nexport function streamMap<T, U>(s: ReadableStream<T>, sm: StreamMap<T, U>): ReadableStream<U> {\n const state = { reader: s.getReader(), streamMap: sm, idx: 0 };\n return new ReadableStream<U>({\n async pull(controller) {\n const { done, value } = await state.reader.read();\n if (done) {\n state.streamMap.Close && state.streamMap.Close();\n controller.close();\n return;\n }\n const promiseOrU = state.streamMap.Map(value, state.idx++);\n let mapped: U;\n if (promiseOrU instanceof Promise || typeof (promiseOrU as { then: () => void }).then === \"function\") {\n mapped = await promiseOrU;\n } else {\n mapped = promiseOrU;\n }\n controller.enqueue(mapped);\n },\n });\n}\n\nexport async function devnull<T>(a: ReadableStream<T>): Promise<number> {\n const reader = a.getReader();\n let cnt = 0;\n while (true) {\n const { done } = await reader.read();\n if (done) {\n break;\n }\n cnt++;\n }\n return cnt;\n}\n\nexport function array2stream<T>(a: T[]): ReadableStream<T> {\n let i = 0;\n return new ReadableStream<T>({\n pull(controller) {\n if (i >= a.length) {\n controller.close();\n return;\n }\n controller.enqueue(a[i]);\n i++;\n },\n });\n}\n\nexport async function stream2array<T>(a: ReadableStream<T>): Promise<T[]> {\n const ret: T[] = [];\n const reader = a.getReader();\n while (true) {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n ret.push(value);\n }\n return ret;\n}\n","import { array2stream, stream2array } from \"./stream_map\";\n\ninterface ReChunkResult {\n readonly rest: Uint8Array;\n readonly chunk: Uint8Array;\n}\n\nexport async function rebufferArray(a: Uint8Array[], chunkSize: number): Promise<Uint8Array[]> {\n return stream2array(rebuffer(array2stream(a), chunkSize));\n}\n\nfunction reChunk(cs: Uint8Array[], chunkSize: number): ReChunkResult {\n const len = cs.reduce((acc, v) => acc + v.length, 0);\n const last = cs[cs.length - 1];\n const lastOfs = len - last.length;\n // console.log(\"reChunk\", len, lastOfs, last.length, chunkSize, chunkSize - lastOfs)\n const rest = last.subarray(chunkSize - lastOfs);\n cs[cs.length - 1] = last.subarray(0, chunkSize - lastOfs);\n const chunk = new Uint8Array(chunkSize);\n let ofs = 0;\n for (const c of cs) {\n chunk.set(c, ofs);\n ofs += c.length;\n }\n return { rest, chunk };\n}\n\ninterface pumpState {\n readonly reader: ReadableStreamDefaultReader<Uint8Array>;\n tmp: Uint8Array[];\n tmpLen: number;\n readonly chunkSize: number;\n}\n\nfunction pump(ps: pumpState, controller: ReadableStreamDefaultController<Uint8Array>, next: () => void): void {\n ps.reader.read().then(({ done, value }) => {\n if (done) {\n if (ps.tmpLen > 0) {\n controller.enqueue(reChunk(ps.tmp, ps.tmpLen).chunk);\n }\n controller.close();\n next();\n return;\n }\n if (ps.tmpLen + value.length > ps.chunkSize) {\n ps.tmp.push(value);\n const res = reChunk(ps.tmp, ps.chunkSize);\n controller.enqueue(res.chunk);\n ps.tmp = [res.rest];\n ps.tmpLen = res.rest.length;\n next();\n return;\n } else if (value.length) {\n ps.tmp.push(value);\n ps.tmpLen += value.length;\n }\n pump(ps, controller, next);\n });\n}\n\nexport function rebuffer(a: ReadableStream<Uint8Array>, chunkSize: number): ReadableStream<Uint8Array> {\n const state: pumpState = {\n reader: a.getReader(),\n tmp: [],\n tmpLen: 0,\n chunkSize,\n };\n return new ReadableStream<Uint8Array>({\n async pull(controller) {\n return new Promise<void>((resolve) => {\n pump(state, controller, resolve);\n });\n },\n });\n}\n","export async function stream2string(stream?: ReadableStream<Uint8Array> | null, maxSize?: number): Promise<string> {\n if (!stream) {\n return Promise.resolve(\"\");\n }\n const reader = stream.getReader();\n let res = \"\";\n const decoder = new TextDecoder();\n let rSize = 0;\n while (typeof maxSize === \"undefined\" || rSize < maxSize) {\n try {\n const read = await reader.read();\n if (read.done) {\n break;\n }\n if (maxSize && rSize + read.value.length > maxSize) {\n read.value = read.value.slice(0, maxSize - rSize);\n }\n const block = decoder.decode(read.value, { stream: true });\n rSize += read.value.length;\n res += block;\n } catch (err) {\n return Promise.reject(err);\n }\n }\n return Promise.resolve(res);\n}\n\nexport async function stream2uint8array(stream?: ReadableStream<Uint8Array> | null): Promise<Uint8Array> {\n if (!stream) {\n return Promise.resolve(new Uint8Array());\n }\n const reader = stream.getReader();\n let res = new Uint8Array();\n // eslint-disable-next-line no-constant-condition\n while (1) {\n try {\n const { done, value } = await reader.read();\n if (done) {\n break;\n }\n res = new Uint8Array([...res, ...value]);\n } catch (err) {\n return Promise.reject(err);\n }\n }\n return Promise.resolve(res);\n}\n","export interface streamingTestState {\n readonly sendChunks: number;\n readonly sendChunkSize: number;\n fillCalls: number;\n CollectorFn: jest.Mock<void, [{ done: boolean; value: Uint8Array | undefined; fillCalls: number; reBufferCalls: number }]>;\n}\n\nexport async function receiveFromStream(reb: ReadableStream<Uint8Array>, state: streamingTestState) {\n return new Promise<void>((resolve) => {\n let reBufferCalls = 0;\n const reader = reb.getReader();\n function pump() {\n reader.read().then(({ done, value }) => {\n state.CollectorFn({ done, value, fillCalls: state.fillCalls, reBufferCalls });\n reBufferCalls++;\n if (done) {\n resolve();\n return;\n }\n pump();\n });\n }\n pump();\n });\n}\n\nexport async function sendToStream(reb: WritableStream<Uint8Array>, state: streamingTestState) {\n return new Promise<void>((resolve) => {\n const writer = reb.getWriter();\n function pump(i: number) {\n if (i >= state.sendChunks) {\n writer.close();\n resolve();\n return;\n }\n writer.ready.then(() => {\n state.fillCalls++;\n writer.write(new Uint8Array(Array(state.sendChunkSize).fill(i)));\n pump(i + 1);\n });\n }\n pump(0);\n });\n}\n\n// it(\"does nothing\", () => {});\n","export function string2stream(str: string): ReadableStream<Uint8Array> {\n const encoder = new TextEncoder();\n return uint8array2stream(encoder.encode(str));\n}\n\nexport function uint8array2stream(str: Uint8Array): ReadableStream<Uint8Array> {\n return new ReadableStream<Uint8Array>({\n start(controller) {\n controller.enqueue(str);\n controller.close();\n },\n });\n}\n"],"mappings":";;;AAIO,SAAS,UAAgB,GAAsB,IAAwC;AAC5F,QAAM,QAAQ,EAAE,QAAQ,EAAE,UAAU,GAAG,WAAW,IAAI,KAAK,EAAE;AAC7D,SAA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