ix
Version:
The Interactive Extensions for JavaScript
69 lines (67 loc) • 2.43 kB
JavaScript
import { __awaiter } from "tslib";
import { AsyncIterableX } from './asynciterablex.mjs';
import { Readable } from 'stream';
const done = (_) => __awaiter(void 0, void 0, void 0, function* () { return null; });
/** @ignore */
/** @ignore */
export class AsyncIterableReadable extends Readable {
constructor(source, options) {
super(options);
this._pulling = false;
this._objectMode = true;
this._iterator = source[Symbol.asyncIterator]();
this._objectMode = !options || !!options.objectMode;
}
_read(size) {
const it = this._iterator;
if (it && !this._pulling && (this._pulling = true)) {
Promise.resolve(this._pull(it, size)).then((p) => (this._pulling = p));
}
}
_destroy(err, cb) {
const it = this._iterator;
this._iterator = undefined;
const fn = (it && (err ? it.throw : it.return)) || done;
fn.call(it, err).then(() => cb && cb(null));
}
// eslint-disable-next-line complexity
_pull(it, size) {
return __awaiter(this, void 0, void 0, function* () {
let innerSize = size;
const objectMode = this._objectMode;
let r;
while (this.readable && !(r = yield it.next(innerSize)).done) {
if (innerSize != null) {
if (objectMode) {
innerSize -= 1;
}
else {
innerSize -= Buffer.byteLength(r.value || '');
}
}
if (!this.push(r.value) || innerSize <= 0) {
break;
}
}
if ((r && r.done) || !this.readable) {
this.push(null);
if (it.return) {
yield it.return();
}
}
return !this.readable;
});
}
}
export function toNodeStream(source, options) {
return !options || options.objectMode === true
? new AsyncIterableReadable(source, options)
: new AsyncIterableReadable(source, options);
}
export function toNodeStreamProto(options) {
return !options || options.objectMode === true
? new AsyncIterableReadable(this, options)
: new AsyncIterableReadable(this, options);
}
AsyncIterableX.prototype.toNodeStream = toNodeStreamProto;
//# sourceMappingURL=tonodestream.mjs.map