@anabranch/fs 0.1.0
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/LICENSE +21 -0
- package/README.md +45 -0
- package/esm/_dnt.polyfills.d.ts +7 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -0
- package/esm/_dnt.polyfills.js +1 -0
- package/esm/anabranch/index.d.ts +44 -0
- package/esm/anabranch/index.d.ts.map +1 -0
- package/esm/anabranch/index.js +41 -0
- package/esm/anabranch/streams/channel.d.ts +15 -0
- package/esm/anabranch/streams/channel.d.ts.map +1 -0
- package/esm/anabranch/streams/channel.js +122 -0
- package/esm/anabranch/streams/source.d.ts +68 -0
- package/esm/anabranch/streams/source.d.ts.map +1 -0
- package/esm/anabranch/streams/source.js +72 -0
- package/esm/anabranch/streams/stream.d.ts +431 -0
- package/esm/anabranch/streams/stream.d.ts.map +1 -0
- package/esm/anabranch/streams/stream.js +625 -0
- package/esm/anabranch/streams/task.d.ts +117 -0
- package/esm/anabranch/streams/task.d.ts.map +1 -0
- package/esm/anabranch/streams/task.js +416 -0
- package/esm/anabranch/streams/util.d.ts +33 -0
- package/esm/anabranch/streams/util.d.ts.map +1 -0
- package/esm/anabranch/streams/util.js +18 -0
- package/esm/fs/dir.d.ts +17 -0
- package/esm/fs/dir.d.ts.map +1 -0
- package/esm/fs/dir.js +90 -0
- package/esm/fs/errors.d.ts +64 -0
- package/esm/fs/errors.d.ts.map +1 -0
- package/esm/fs/errors.js +125 -0
- package/esm/fs/glob_match.d.ts +15 -0
- package/esm/fs/glob_match.d.ts.map +1 -0
- package/esm/fs/glob_match.js +73 -0
- package/esm/fs/index.d.ts +38 -0
- package/esm/fs/index.d.ts.map +1 -0
- package/esm/fs/index.js +31 -0
- package/esm/fs/read.d.ts +22 -0
- package/esm/fs/read.d.ts.map +1 -0
- package/esm/fs/read.js +75 -0
- package/esm/fs/types.d.ts +67 -0
- package/esm/fs/types.d.ts.map +1 -0
- package/esm/fs/types.js +1 -0
- package/esm/fs/watch.d.ts +17 -0
- package/esm/fs/watch.d.ts.map +1 -0
- package/esm/fs/watch.js +37 -0
- package/esm/fs/write.d.ts +16 -0
- package/esm/fs/write.d.ts.map +1 -0
- package/esm/fs/write.js +42 -0
- package/esm/package.json +3 -0
- package/package.json +24 -0
|
@@ -0,0 +1,625 @@
|
|
|
1
|
+
import { AggregateError } from "./util.js";
|
|
2
|
+
const isPromise = (value) => value != null && typeof value.then === "function";
|
|
3
|
+
const isAsyncIterable = (value) => {
|
|
4
|
+
if (value === null || value === undefined) {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
return Symbol.asyncIterator in Object(value);
|
|
8
|
+
};
|
|
9
|
+
const isIterable = (value) => {
|
|
10
|
+
if (value === null || value === undefined) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return Symbol.iterator in Object(value);
|
|
14
|
+
};
|
|
15
|
+
const toAsyncIterable = (iterable) => {
|
|
16
|
+
if (isAsyncIterable(iterable)) {
|
|
17
|
+
return iterable;
|
|
18
|
+
}
|
|
19
|
+
if (isIterable(iterable)) {
|
|
20
|
+
return (async function* () {
|
|
21
|
+
yield* iterable;
|
|
22
|
+
})();
|
|
23
|
+
}
|
|
24
|
+
throw new TypeError("flatMap function must return an iterable");
|
|
25
|
+
};
|
|
26
|
+
export class _StreamImpl {
|
|
27
|
+
constructor(source, concurrency = Infinity, bufferSize = Infinity) {
|
|
28
|
+
Object.defineProperty(this, "source", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
writable: true,
|
|
32
|
+
value: source
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(this, "concurrency", {
|
|
35
|
+
enumerable: true,
|
|
36
|
+
configurable: true,
|
|
37
|
+
writable: true,
|
|
38
|
+
value: concurrency
|
|
39
|
+
});
|
|
40
|
+
Object.defineProperty(this, "bufferSize", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: true,
|
|
44
|
+
value: bufferSize
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
async toArray() {
|
|
48
|
+
const results = [];
|
|
49
|
+
for await (const result of this.source()) {
|
|
50
|
+
results.push(result);
|
|
51
|
+
}
|
|
52
|
+
return results;
|
|
53
|
+
}
|
|
54
|
+
async collect() {
|
|
55
|
+
const successes = [];
|
|
56
|
+
const errors = [];
|
|
57
|
+
for await (const result of this.source()) {
|
|
58
|
+
if (result.type === "success") {
|
|
59
|
+
successes.push(result.value);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
errors.push(result.error);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (errors.length) {
|
|
66
|
+
throw new AggregateError(errors);
|
|
67
|
+
}
|
|
68
|
+
return successes;
|
|
69
|
+
}
|
|
70
|
+
successes() {
|
|
71
|
+
const source = this.source;
|
|
72
|
+
return {
|
|
73
|
+
async *[Symbol.asyncIterator]() {
|
|
74
|
+
for await (const result of source()) {
|
|
75
|
+
if (result.type === "success") {
|
|
76
|
+
yield result.value;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
errors() {
|
|
83
|
+
const source = this.source;
|
|
84
|
+
return {
|
|
85
|
+
async *[Symbol.asyncIterator]() {
|
|
86
|
+
for await (const result of source()) {
|
|
87
|
+
if (result.type === "error") {
|
|
88
|
+
yield result.error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
[Symbol.asyncIterator]() {
|
|
95
|
+
return this.source()[Symbol.asyncIterator]();
|
|
96
|
+
}
|
|
97
|
+
transform(handler) {
|
|
98
|
+
const source = this.source;
|
|
99
|
+
const concurrency = this.concurrency;
|
|
100
|
+
const bufferSize = this.bufferSize;
|
|
101
|
+
return new _StreamImpl(async function* () {
|
|
102
|
+
for await (const result of source()) {
|
|
103
|
+
const outputs = await handler(result);
|
|
104
|
+
for (const output of outputs) {
|
|
105
|
+
yield output;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}, concurrency, bufferSize);
|
|
109
|
+
}
|
|
110
|
+
concurrentMap(fn, concurrency, bufferSize) {
|
|
111
|
+
return this.concurrentTransform(async (result) => {
|
|
112
|
+
if (result.type !== "success") {
|
|
113
|
+
return [result];
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
const value = await fn(result.value);
|
|
117
|
+
return [{ type: "success", value }];
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
return [{ type: "error", error: error }];
|
|
121
|
+
}
|
|
122
|
+
}, concurrency, bufferSize);
|
|
123
|
+
}
|
|
124
|
+
concurrentTransform(handler, concurrency, bufferSize) {
|
|
125
|
+
const source = this.source;
|
|
126
|
+
const maxConcurrency = Number.isFinite(concurrency)
|
|
127
|
+
? Math.max(1, concurrency)
|
|
128
|
+
: Infinity;
|
|
129
|
+
const maxBufferSize = Number.isFinite(bufferSize)
|
|
130
|
+
? Math.max(1, bufferSize)
|
|
131
|
+
: Infinity;
|
|
132
|
+
return (async function* () {
|
|
133
|
+
const queue = [];
|
|
134
|
+
let head = 0;
|
|
135
|
+
let inFlight = 0;
|
|
136
|
+
let done = false;
|
|
137
|
+
const waiters = [];
|
|
138
|
+
const wake = () => {
|
|
139
|
+
while (waiters.length > 0) {
|
|
140
|
+
const resolve = waiters.shift();
|
|
141
|
+
if (resolve)
|
|
142
|
+
resolve();
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const sleep = () => new Promise((resolve) => {
|
|
146
|
+
waiters.push(resolve);
|
|
147
|
+
});
|
|
148
|
+
const push = (result) => {
|
|
149
|
+
queue.push(result);
|
|
150
|
+
wake();
|
|
151
|
+
};
|
|
152
|
+
const size = () => queue.length - head;
|
|
153
|
+
(async () => {
|
|
154
|
+
try {
|
|
155
|
+
for await (const result of source()) {
|
|
156
|
+
while (inFlight >= maxConcurrency || size() >= maxBufferSize) {
|
|
157
|
+
await sleep();
|
|
158
|
+
}
|
|
159
|
+
inFlight += 1;
|
|
160
|
+
Promise.resolve()
|
|
161
|
+
.then(async () => {
|
|
162
|
+
const outputs = await handler(result);
|
|
163
|
+
for (const output of outputs) {
|
|
164
|
+
push(output);
|
|
165
|
+
}
|
|
166
|
+
})
|
|
167
|
+
.finally(() => {
|
|
168
|
+
inFlight -= 1;
|
|
169
|
+
wake();
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
push({ type: "error", error: error });
|
|
175
|
+
}
|
|
176
|
+
done = true;
|
|
177
|
+
wake();
|
|
178
|
+
})();
|
|
179
|
+
while (true) {
|
|
180
|
+
while (size() === 0 && (!done || inFlight > 0)) {
|
|
181
|
+
await sleep();
|
|
182
|
+
}
|
|
183
|
+
if (size() === 0) {
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
const next = queue[head];
|
|
187
|
+
queue[head] = undefined;
|
|
188
|
+
head += 1;
|
|
189
|
+
if (head > 256 && head * 2 >= queue.length) {
|
|
190
|
+
queue.splice(0, head);
|
|
191
|
+
head = 0;
|
|
192
|
+
}
|
|
193
|
+
if (next) {
|
|
194
|
+
yield next;
|
|
195
|
+
wake();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
})();
|
|
199
|
+
}
|
|
200
|
+
sequentialMap(fn) {
|
|
201
|
+
const source = this.source;
|
|
202
|
+
return (async function* () {
|
|
203
|
+
for await (const result of source()) {
|
|
204
|
+
if (result.type === "success") {
|
|
205
|
+
try {
|
|
206
|
+
const mappedValue = fn(result.value);
|
|
207
|
+
if (isPromise(mappedValue)) {
|
|
208
|
+
yield {
|
|
209
|
+
type: "success",
|
|
210
|
+
value: await mappedValue,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
yield {
|
|
215
|
+
type: "success",
|
|
216
|
+
value: mappedValue,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
yield { type: "error", error: error };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
yield result;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
})();
|
|
229
|
+
}
|
|
230
|
+
concurrentTryMap(fn, errFn, concurrency, bufferSize) {
|
|
231
|
+
return this.concurrentTransform(async (result) => {
|
|
232
|
+
if (result.type !== "success") {
|
|
233
|
+
return [result];
|
|
234
|
+
}
|
|
235
|
+
try {
|
|
236
|
+
const value = await fn(result.value);
|
|
237
|
+
return [{ type: "success", value }];
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
const mappedError = await errFn(error, result.value);
|
|
241
|
+
return [{ type: "error", error: mappedError }];
|
|
242
|
+
}
|
|
243
|
+
}, concurrency, bufferSize);
|
|
244
|
+
}
|
|
245
|
+
concurrentFlatMap(fn, concurrency, bufferSize) {
|
|
246
|
+
return this.concurrentTransform(async (result) => {
|
|
247
|
+
if (result.type !== "success") {
|
|
248
|
+
return [result];
|
|
249
|
+
}
|
|
250
|
+
try {
|
|
251
|
+
const mapped = await fn(result.value);
|
|
252
|
+
const outputs = [];
|
|
253
|
+
for await (const value of toAsyncIterable(mapped)) {
|
|
254
|
+
outputs.push({ type: "success", value });
|
|
255
|
+
}
|
|
256
|
+
return outputs;
|
|
257
|
+
}
|
|
258
|
+
catch (error) {
|
|
259
|
+
return [{ type: "error", error: error }];
|
|
260
|
+
}
|
|
261
|
+
}, concurrency, bufferSize);
|
|
262
|
+
}
|
|
263
|
+
map(fn) {
|
|
264
|
+
const concurrency = this.concurrency;
|
|
265
|
+
const bufferSize = this.bufferSize;
|
|
266
|
+
if (Number.isFinite(concurrency) && concurrency > 1) {
|
|
267
|
+
return new _StreamImpl(() => this.concurrentMap(fn, concurrency, bufferSize), concurrency, bufferSize);
|
|
268
|
+
}
|
|
269
|
+
return new _StreamImpl(() => this.sequentialMap(fn), concurrency, bufferSize);
|
|
270
|
+
}
|
|
271
|
+
tryMap(fn, errFn) {
|
|
272
|
+
const concurrency = this.concurrency;
|
|
273
|
+
const bufferSize = this.bufferSize;
|
|
274
|
+
if (Number.isFinite(concurrency) && concurrency > 1) {
|
|
275
|
+
return new _StreamImpl(() => this.concurrentTryMap(fn, errFn, concurrency, bufferSize), concurrency, bufferSize);
|
|
276
|
+
}
|
|
277
|
+
return this.transform(async (result) => {
|
|
278
|
+
if (result.type === "success") {
|
|
279
|
+
try {
|
|
280
|
+
const value = await fn(result.value);
|
|
281
|
+
return [{ type: "success", value }];
|
|
282
|
+
}
|
|
283
|
+
catch (error) {
|
|
284
|
+
const mappedError = await errFn(error, result.value);
|
|
285
|
+
return [{ type: "error", error: mappedError }];
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return [result];
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
flatMap(fn) {
|
|
292
|
+
const concurrency = this.concurrency;
|
|
293
|
+
const bufferSize = this.bufferSize;
|
|
294
|
+
if (Number.isFinite(concurrency) && concurrency > 1) {
|
|
295
|
+
return new _StreamImpl(() => this.concurrentFlatMap(fn, concurrency, bufferSize), concurrency, bufferSize);
|
|
296
|
+
}
|
|
297
|
+
return this.transform(async (result) => {
|
|
298
|
+
if (result.type !== "success") {
|
|
299
|
+
return [result];
|
|
300
|
+
}
|
|
301
|
+
try {
|
|
302
|
+
const mapped = await fn(result.value);
|
|
303
|
+
const outputs = [];
|
|
304
|
+
for await (const value of toAsyncIterable(mapped)) {
|
|
305
|
+
outputs.push({ type: "success", value });
|
|
306
|
+
}
|
|
307
|
+
return outputs;
|
|
308
|
+
}
|
|
309
|
+
catch (error) {
|
|
310
|
+
return [{ type: "error", error: error }];
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
filter(fn) {
|
|
315
|
+
const source = this.source;
|
|
316
|
+
const concurrency = this.concurrency;
|
|
317
|
+
const bufferSize = this.bufferSize;
|
|
318
|
+
return new _StreamImpl(async function* () {
|
|
319
|
+
for await (const result of source()) {
|
|
320
|
+
if (result.type === "success") {
|
|
321
|
+
try {
|
|
322
|
+
const shouldInclude = fn(result.value);
|
|
323
|
+
if (isPromise(shouldInclude)) {
|
|
324
|
+
if (await shouldInclude) {
|
|
325
|
+
yield result;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
else if (shouldInclude) {
|
|
329
|
+
yield result;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
yield { type: "error", error: error };
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
yield result;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}, concurrency, bufferSize);
|
|
341
|
+
}
|
|
342
|
+
tap(fn) {
|
|
343
|
+
const source = this.source;
|
|
344
|
+
const concurrency = this.concurrency;
|
|
345
|
+
const bufferSize = this.bufferSize;
|
|
346
|
+
return new _StreamImpl(async function* () {
|
|
347
|
+
for await (const result of source()) {
|
|
348
|
+
if (result.type === "success") {
|
|
349
|
+
try {
|
|
350
|
+
const ret = fn(result.value);
|
|
351
|
+
if (isPromise(ret))
|
|
352
|
+
await ret;
|
|
353
|
+
yield result;
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
yield { type: "error", error: error };
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
yield result;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}, concurrency, bufferSize);
|
|
364
|
+
}
|
|
365
|
+
tapErr(fn) {
|
|
366
|
+
const source = this.source;
|
|
367
|
+
const concurrency = this.concurrency;
|
|
368
|
+
const bufferSize = this.bufferSize;
|
|
369
|
+
return new _StreamImpl(async function* () {
|
|
370
|
+
for await (const result of source()) {
|
|
371
|
+
if (result.type === "error") {
|
|
372
|
+
try {
|
|
373
|
+
const ret = fn(result.error);
|
|
374
|
+
if (isPromise(ret))
|
|
375
|
+
await ret;
|
|
376
|
+
yield result;
|
|
377
|
+
}
|
|
378
|
+
catch (error) {
|
|
379
|
+
yield { type: "error", error: error };
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
yield result;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}, concurrency, bufferSize);
|
|
387
|
+
}
|
|
388
|
+
take(n) {
|
|
389
|
+
const source = this.source;
|
|
390
|
+
const concurrency = this.concurrency;
|
|
391
|
+
const bufferSize = this.bufferSize;
|
|
392
|
+
return new _StreamImpl(async function* () {
|
|
393
|
+
const gen = source();
|
|
394
|
+
try {
|
|
395
|
+
let count = 0;
|
|
396
|
+
for await (const result of gen) {
|
|
397
|
+
if (result.type === "success") {
|
|
398
|
+
if (count >= n)
|
|
399
|
+
break;
|
|
400
|
+
count += 1;
|
|
401
|
+
}
|
|
402
|
+
yield result;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
finally {
|
|
406
|
+
await gen.return(undefined);
|
|
407
|
+
}
|
|
408
|
+
}, concurrency, bufferSize);
|
|
409
|
+
}
|
|
410
|
+
takeWhile(fn) {
|
|
411
|
+
const source = this.source;
|
|
412
|
+
const concurrency = this.concurrency;
|
|
413
|
+
const bufferSize = this.bufferSize;
|
|
414
|
+
return new _StreamImpl(async function* () {
|
|
415
|
+
const gen = source();
|
|
416
|
+
try {
|
|
417
|
+
for await (const result of gen) {
|
|
418
|
+
if (result.type === "success") {
|
|
419
|
+
try {
|
|
420
|
+
const shouldContinue = fn(result.value);
|
|
421
|
+
if (isPromise(shouldContinue)
|
|
422
|
+
? !(await shouldContinue)
|
|
423
|
+
: !shouldContinue) {
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
yield result;
|
|
427
|
+
}
|
|
428
|
+
catch (error) {
|
|
429
|
+
yield { type: "error", error: error };
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
else {
|
|
434
|
+
yield result;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
finally {
|
|
439
|
+
await gen.return(undefined);
|
|
440
|
+
}
|
|
441
|
+
}, concurrency, bufferSize);
|
|
442
|
+
}
|
|
443
|
+
async partition() {
|
|
444
|
+
const successes = [];
|
|
445
|
+
const errors = [];
|
|
446
|
+
for await (const result of this.source()) {
|
|
447
|
+
if (result.type === "success") {
|
|
448
|
+
successes.push(result.value);
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
errors.push(result.error);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return { successes, errors };
|
|
455
|
+
}
|
|
456
|
+
async fold(fn, initialValue) {
|
|
457
|
+
let accumulator = initialValue;
|
|
458
|
+
const errors = [];
|
|
459
|
+
for await (const result of this.source()) {
|
|
460
|
+
if (result.type === "success") {
|
|
461
|
+
accumulator = await fn(accumulator, result.value);
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
errors.push(result.error);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
if (errors.length) {
|
|
468
|
+
throw new AggregateError(errors);
|
|
469
|
+
}
|
|
470
|
+
return accumulator;
|
|
471
|
+
}
|
|
472
|
+
scan(fn, initialValue) {
|
|
473
|
+
const source = this.source;
|
|
474
|
+
const concurrency = this.concurrency;
|
|
475
|
+
const bufferSize = this.bufferSize;
|
|
476
|
+
return new _StreamImpl(async function* () {
|
|
477
|
+
let accumulator = initialValue;
|
|
478
|
+
for await (const result of source()) {
|
|
479
|
+
if (result.type === "success") {
|
|
480
|
+
try {
|
|
481
|
+
accumulator = await fn(accumulator, result.value);
|
|
482
|
+
yield { type: "success", value: accumulator };
|
|
483
|
+
}
|
|
484
|
+
catch (error) {
|
|
485
|
+
yield { type: "error", error: error };
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
else {
|
|
489
|
+
yield result;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}, concurrency, bufferSize);
|
|
493
|
+
}
|
|
494
|
+
chunks(size) {
|
|
495
|
+
if (size <= 0) {
|
|
496
|
+
throw new Error("chunks size must be positive");
|
|
497
|
+
}
|
|
498
|
+
const source = this.source;
|
|
499
|
+
const concurrency = this.concurrency;
|
|
500
|
+
const bufferSize = this.bufferSize;
|
|
501
|
+
return new _StreamImpl(async function* () {
|
|
502
|
+
let chunk = [];
|
|
503
|
+
for await (const result of source()) {
|
|
504
|
+
if (result.type === "success") {
|
|
505
|
+
chunk.push(result.value);
|
|
506
|
+
if (chunk.length === size) {
|
|
507
|
+
yield { type: "success", value: chunk };
|
|
508
|
+
chunk = [];
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
if (chunk.length > 0) {
|
|
513
|
+
yield { type: "success", value: chunk };
|
|
514
|
+
chunk = [];
|
|
515
|
+
}
|
|
516
|
+
yield result;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
if (chunk.length > 0) {
|
|
520
|
+
yield { type: "success", value: chunk };
|
|
521
|
+
}
|
|
522
|
+
}, concurrency, bufferSize);
|
|
523
|
+
}
|
|
524
|
+
mapErr(fn) {
|
|
525
|
+
return this.transform(async (result) => {
|
|
526
|
+
if (result.type === "error") {
|
|
527
|
+
try {
|
|
528
|
+
const mappedError = await fn(result.error);
|
|
529
|
+
return [
|
|
530
|
+
{ type: "error", error: mappedError },
|
|
531
|
+
];
|
|
532
|
+
}
|
|
533
|
+
catch (error) {
|
|
534
|
+
return [
|
|
535
|
+
{ type: "error", error: error },
|
|
536
|
+
];
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return [result];
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
filterErr(fn) {
|
|
543
|
+
return this.transform(async (result) => {
|
|
544
|
+
if (result.type === "error") {
|
|
545
|
+
try {
|
|
546
|
+
const shouldInclude = await fn(result.error);
|
|
547
|
+
if (shouldInclude) {
|
|
548
|
+
return [result];
|
|
549
|
+
}
|
|
550
|
+
return [];
|
|
551
|
+
}
|
|
552
|
+
catch (error) {
|
|
553
|
+
return [{ type: "error", error: error }];
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
return [result];
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
async foldErr(fn, initialValue) {
|
|
560
|
+
let accumulator = initialValue;
|
|
561
|
+
for await (const result of this.source()) {
|
|
562
|
+
if (result.type === "error") {
|
|
563
|
+
accumulator = await fn(accumulator, result.error);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return accumulator;
|
|
567
|
+
}
|
|
568
|
+
recoverWhen(guard, fn) {
|
|
569
|
+
return this.transform(async (result) => {
|
|
570
|
+
if (result.type === "error" && guard(result.error)) {
|
|
571
|
+
try {
|
|
572
|
+
const recoveredValue = await fn(result.error);
|
|
573
|
+
return [
|
|
574
|
+
{
|
|
575
|
+
type: "success",
|
|
576
|
+
value: recoveredValue,
|
|
577
|
+
},
|
|
578
|
+
];
|
|
579
|
+
}
|
|
580
|
+
catch (error) {
|
|
581
|
+
return [
|
|
582
|
+
{
|
|
583
|
+
type: "error",
|
|
584
|
+
error: error,
|
|
585
|
+
},
|
|
586
|
+
];
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
return [result];
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
recover(fn) {
|
|
593
|
+
return this.transform(async (result) => {
|
|
594
|
+
if (result.type === "error") {
|
|
595
|
+
try {
|
|
596
|
+
const recoveredValue = await fn(result.error);
|
|
597
|
+
return [
|
|
598
|
+
{
|
|
599
|
+
type: "success",
|
|
600
|
+
value: recoveredValue,
|
|
601
|
+
},
|
|
602
|
+
];
|
|
603
|
+
}
|
|
604
|
+
catch (error) {
|
|
605
|
+
return [
|
|
606
|
+
{ type: "error", error: error },
|
|
607
|
+
];
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
return [result];
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
throwOn(guard) {
|
|
614
|
+
const source = this.source;
|
|
615
|
+
return new _StreamImpl(async function* () {
|
|
616
|
+
for await (const result of source()) {
|
|
617
|
+
if (result.type === "error" && guard(result.error)) {
|
|
618
|
+
// throwOn intentionally terminates iteration instead of collecting.
|
|
619
|
+
throw result.error;
|
|
620
|
+
}
|
|
621
|
+
yield result;
|
|
622
|
+
}
|
|
623
|
+
}, this.concurrency, this.bufferSize);
|
|
624
|
+
}
|
|
625
|
+
}
|