@prestyj/ai 4.12.1 → 4.13.3
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/dist/index.cjs +34 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +34 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -336,7 +336,7 @@ var EventStream = class {
|
|
|
336
336
|
}
|
|
337
337
|
}
|
|
338
338
|
};
|
|
339
|
-
var StreamResult = class {
|
|
339
|
+
var StreamResult = class _StreamResult {
|
|
340
340
|
response;
|
|
341
341
|
buffer = [];
|
|
342
342
|
done = false;
|
|
@@ -344,6 +344,18 @@ var StreamResult = class {
|
|
|
344
344
|
resolveResponse;
|
|
345
345
|
rejectResponse;
|
|
346
346
|
resolveWait = null;
|
|
347
|
+
/**
|
|
348
|
+
* High-water mark: when the buffer exceeds this many unconsumed events,
|
|
349
|
+
* the pump pauses until the consumer drains below the low-water mark.
|
|
350
|
+
* Prevents unbounded memory growth when a consumer is slow.
|
|
351
|
+
* Only active when someone IS iterating — if nobody iterates (the `then()`
|
|
352
|
+
* path), backpressure is skipped so the pump can complete and resolve.
|
|
353
|
+
*/
|
|
354
|
+
static HIGH_WATER = 5e3;
|
|
355
|
+
static LOW_WATER = 1e3;
|
|
356
|
+
iterating = false;
|
|
357
|
+
paused = false;
|
|
358
|
+
resolveDrain = null;
|
|
347
359
|
constructor(generator, signal) {
|
|
348
360
|
this.response = new Promise((resolve, reject) => {
|
|
349
361
|
this.resolveResponse = resolve;
|
|
@@ -358,6 +370,13 @@ var StreamResult = class {
|
|
|
358
370
|
this.buffer.push(next.value);
|
|
359
371
|
this.resolveWait?.();
|
|
360
372
|
this.resolveWait = null;
|
|
373
|
+
if (this.iterating && this.buffer.length > _StreamResult.HIGH_WATER) {
|
|
374
|
+
this.paused = true;
|
|
375
|
+
await new Promise((r) => {
|
|
376
|
+
this.resolveDrain = r;
|
|
377
|
+
});
|
|
378
|
+
this.paused = false;
|
|
379
|
+
}
|
|
361
380
|
next = await this._nextWithAbort(generator, signal);
|
|
362
381
|
}
|
|
363
382
|
this.done = true;
|
|
@@ -396,11 +415,20 @@ var StreamResult = class {
|
|
|
396
415
|
}
|
|
397
416
|
}
|
|
398
417
|
async *[Symbol.asyncIterator]() {
|
|
418
|
+
this.iterating = true;
|
|
399
419
|
let index = 0;
|
|
400
420
|
while (true) {
|
|
401
421
|
while (index < this.buffer.length) {
|
|
402
422
|
yield this.buffer[index++];
|
|
403
423
|
}
|
|
424
|
+
if (this.paused && index > _StreamResult.LOW_WATER) {
|
|
425
|
+
this.resolveDrain?.();
|
|
426
|
+
this.resolveDrain = null;
|
|
427
|
+
}
|
|
428
|
+
if (index > 0 && !this.paused) {
|
|
429
|
+
this.buffer.splice(0, index);
|
|
430
|
+
index = 0;
|
|
431
|
+
}
|
|
404
432
|
if (this.error) throw this.error;
|
|
405
433
|
if (this.done) return;
|
|
406
434
|
await new Promise((r) => {
|
|
@@ -413,6 +441,11 @@ var StreamResult = class {
|
|
|
413
441
|
}
|
|
414
442
|
}
|
|
415
443
|
then(onfulfilled, onrejected) {
|
|
444
|
+
if (this.paused) {
|
|
445
|
+
this.paused = false;
|
|
446
|
+
this.resolveDrain?.();
|
|
447
|
+
this.resolveDrain = null;
|
|
448
|
+
}
|
|
416
449
|
return this.response.then(onfulfilled, onrejected);
|
|
417
450
|
}
|
|
418
451
|
};
|