@reactivex/ix-es5-esm
Version:
The Interactive Extensions for JavaScript
120 lines (118 loc) • 4.7 kB
JavaScript
import { __awaiter, __extends, __generator, __values } from "tslib";
import { AsyncIterableX } from './asynciterablex.js';
import { safeRace } from '../util/safeRace.js';
var NON_FLOWING = 0;
var READABLE = 1;
var ENDED = 2;
var ERRORED = 3;
/** @ignore */
/** @ignore */
var ReadableStreamAsyncIterable = /** @class */ (function (_super) {
__extends(ReadableStreamAsyncIterable, _super);
function ReadableStreamAsyncIterable(stream, size) {
var _this = _super.call(this) || this;
_this._stream = stream;
_this._defaultSize = size;
_this._state = NON_FLOWING;
_this._error = null;
_this._rejectFns = new Set();
var onError = function (err) {
var e_1, _a;
_this._state = ERRORED;
_this._error = err;
try {
for (var _b = __values(_this._rejectFns), _c = _b.next(); !_c.done; _c = _b.next()) {
var rejectFn = _c.value;
rejectFn(err);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
};
var onEnd = function () {
_this._state = ENDED;
};
_this._stream['once']('error', onError);
_this._stream['once']('end', onEnd);
return _this;
}
ReadableStreamAsyncIterable.prototype[Symbol.asyncIterator] = function () {
return this;
};
ReadableStreamAsyncIterable.prototype.next = function (size) {
if (size === void 0) { size = this._defaultSize; }
return __awaiter(this, void 0, void 0, function () {
var value;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(this._state === NON_FLOWING)) return [3 /*break*/, 3];
return [4 /*yield*/, safeRace([this._waitReadable(), this._waitEnd()])];
case 1:
_a.sent();
return [4 /*yield*/, this.next(size)];
case 2: return [2 /*return*/, _a.sent()];
case 3:
if (this._state === ENDED) {
return [2 /*return*/, { done: true, value: undefined }];
}
if (this._state === ERRORED) {
throw this._error;
}
value = this._stream['read'](size);
if (!(value !== null)) return [3 /*break*/, 4];
return [2 /*return*/, { done: false, value: value }];
case 4:
this._state = NON_FLOWING;
return [4 /*yield*/, this.next(size)];
case 5: return [2 /*return*/, _a.sent()];
}
});
});
};
ReadableStreamAsyncIterable.prototype._waitReadable = function () {
var _this = this;
return new Promise(function (resolve, reject) {
var onReadable = function () {
_this._state = READABLE;
_this._rejectFns.delete(reject);
resolve();
};
_this._rejectFns.add(reject);
_this._stream['once']('readable', onReadable);
});
};
ReadableStreamAsyncIterable.prototype._waitEnd = function () {
var _this = this;
if (!this._endPromise) {
this._endPromise = new Promise(function (resolve, reject) {
var onEnd = function () {
_this._state = ENDED;
_this._rejectFns.delete(reject);
resolve();
};
_this._rejectFns.add(reject);
_this._stream['once']('end', onEnd);
});
}
return this._endPromise;
};
return ReadableStreamAsyncIterable;
}(AsyncIterableX));
export { ReadableStreamAsyncIterable };
/**
* Creates a new async-iterable from a Node.js stream.
*
* @param {NodeJS.ReadableStream} stream The Node.js stream to convert to an async-iterable.
* @param {number} [size] The size of the buffers for the stream.
* @returns {(AsyncIterableX<string | Buffer>)} An async-iterable containing data from the stream either in string or Buffer format.
*/
export function fromNodeStream(stream, size) {
return new ReadableStreamAsyncIterable(stream, size);
}
//# sourceMappingURL=fromnodestream.js.map