diff --git a/package.json b/package.json index fb3370b7..8735eb6c 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "CHANGELOG.md" ], "dependencies": { - "@types/node": "^13.7.4", + "@types/node": ">=13.7.4", "tslib": "^2.3.0" }, "devDependencies": { @@ -99,7 +99,7 @@ "ts-jest": "28.0.7", "ts-node": "8.6.2", "typedoc": "0.25.4", - "typescript": "4.6.2", + "typescript": "5.3.3", "validate-commit-msg": "2.14.0", "web-stream-tools": "0.0.1", "web-streams-polyfill": "2.1.0", diff --git a/src/asynciterable/asasynciterable.ts b/src/asynciterable/asasynciterable.ts index dae3307f..02bc05a3 100644 --- a/src/asynciterable/asasynciterable.ts +++ b/src/asynciterable/asasynciterable.ts @@ -2,41 +2,54 @@ import { AsyncIterableX } from './asynciterablex'; import { OperatorAsyncFunction, UnaryFunction } from '../interfaces'; import { Transform, TransformCallback, TransformOptions } from 'stream'; -export interface AsyncIterableTransform extends AsyncIterableX, Transform { +export interface AsyncIterableTransform + extends AsyncIterableX, + NodeJS.ReadableStream, + NodeJS.WritableStream { + new (options?: TransformOptions): AsyncIterableTransform; pipe(...operations: UnaryFunction, R>[]): R; pipe(...operations: OperatorAsyncFunction[]): AsyncIterableX; pipe(writable: R, options?: { end?: boolean }): R; - [Symbol.asyncIterator](): AsyncIterableIterator; + [Symbol.asyncIterator](): AsyncIterableIterator; } const asyncIterableMixin = Symbol('asyncIterableMixin'); /** @ignore */ -export class AsyncIterableTransform extends Transform { - private static [asyncIterableMixin] = false; - constructor(options?: TransformOptions) { - super(options); - // If this is the first time AsyncIterableTransform is being constructed, - // mixin the methods from the AsyncIterableX's prototype. - if (!AsyncIterableTransform[asyncIterableMixin]) { - AsyncIterableTransform[asyncIterableMixin] = true; - Object.defineProperties( - AsyncIterableTransform.prototype, - Object.getOwnPropertyDescriptors(AsyncIterableX.prototype) - ); - } - } - /** @nocollapse */ - _flush(callback: TransformCallback): void { - callback(); - } - /** @nocollapse */ - _transform(chunk: any, _encoding: string, callback: TransformCallback): void { - callback(null, chunk); +export function AsyncIterableTransform( + this: AsyncIterableTransform, + options?: TransformOptions +) { + Transform.call(this as any, options); + // If this is the first time AsyncIterableTransform is being constructed, + // mixin the methods from the AsyncIterableX's prototype. + if (!AsyncIterableTransform[asyncIterableMixin]) { + AsyncIterableTransform[asyncIterableMixin] = true; + Object.defineProperties( + AsyncIterableTransform.prototype, + Object.getOwnPropertyDescriptors(AsyncIterableX.prototype) + ); } } +AsyncIterableTransform.prototype = Object.create(Transform.prototype); + +AsyncIterableTransform[asyncIterableMixin] = false; + +/** @nocollapse */ +AsyncIterableTransform.prototype._flush = function (callback: TransformCallback): void { + callback(); +}; +/** @nocollapse */ +AsyncIterableTransform.prototype._transform = function ( + chunk: any, + _encoding: string, + callback: TransformCallback +): void { + callback(null, chunk); +}; + /** @ignore */ export function asAsyncIterable(options: TransformOptions = {}) { - return new AsyncIterableTransform(options); + return Reflect.construct(AsyncIterableTransform, [options]) as AsyncIterableTransform; } diff --git a/src/asynciterable/fromdomstream.ts b/src/asynciterable/fromdomstream.ts index cd0ba5f2..35f68500 100644 --- a/src/asynciterable/fromdomstream.ts +++ b/src/asynciterable/fromdomstream.ts @@ -65,7 +65,7 @@ async function* _consumeReader( /** @ignore */ async function* defaultReaderToAsyncIterator(reader: ReadableStreamDefaultReader) { - let r: ReadableStreamDefaultReadResult; + let r: ReadableStreamReadResult; while (!(r = await reader.read()).done) { yield r.value; } @@ -73,7 +73,7 @@ async function* defaultReaderToAsyncIterator(reader: ReadableStreamDefa /** @ignore */ async function* byobReaderToAsyncIterator(reader: ReadableStreamReader) { - let r: ReadableStreamDefaultReadResult; + let r: ReadableStreamReadResult; let value: number | ArrayBufferLike = yield null!; while (!(r = await readNext(reader, value, 0)).done) { value = yield r.value; @@ -85,7 +85,7 @@ async function readNext( reader: ReadableStreamReader, bufferOrLen: ArrayBufferLike | number, offset: number -): Promise> { +): Promise> { let size: number; let buffer: ArrayBufferLike; @@ -108,7 +108,7 @@ async function readInto( buffer: ArrayBufferLike, offset: number, size: number -): Promise> { +): Promise> { let innerOffset = offset; if (innerOffset >= size) { return { done: false, value: new Uint8Array(buffer, 0, size) }; diff --git a/src/asynciterable/operators/withabort.ts b/src/asynciterable/operators/withabort.ts index 7eec37b6..00fb7fdd 100644 --- a/src/asynciterable/operators/withabort.ts +++ b/src/asynciterable/operators/withabort.ts @@ -16,6 +16,7 @@ export class WithAbortAsyncIterable extends AsyncIterableX { } [Symbol.asyncIterator](): AsyncIterator { + // @ts-ignore return this._source[Symbol.asyncIterator](this._signal); } } diff --git a/src/asynciterable/todomstream.ts b/src/asynciterable/todomstream.ts index fc0adfee..9edf2029 100644 --- a/src/asynciterable/todomstream.ts +++ b/src/asynciterable/todomstream.ts @@ -51,12 +51,12 @@ class UnderlyingAsyncIterableDefaultSource extends AbstractUnderl super(source); } // eslint-disable-next-line consistent-return - async pull(controller: ReadableStreamDefaultController) { + async pull(controller: ReadableStreamController) { const source = this._source; if (source) { const r = await source.next(controller.desiredSize); if (!r.done) { - return controller.enqueue(r.value); + return (controller as ReadableStreamDefaultController).enqueue(r.value); } } controller.close(); @@ -87,7 +87,9 @@ class UnderlyingAsyncIterableByteSource) { if (!(controller as any).byobRequest) { - return await this.fallbackDefaultSource.pull(controller); + return await this.fallbackDefaultSource.pull( + controller as ReadableStreamDefaultController + ); } if (this._source) { const { view } = (controller as any).byobRequest;