From 2fcd4401beae188066a32575060e726028141706 Mon Sep 17 00:00:00 2001 From: Paul Taylor <178183+trxcllnt@users.noreply.github.com> Date: Thu, 28 Dec 2023 23:20:57 -0800 Subject: [PATCH] chore(typescript): update to typescript@5.3.3 (#358) * chore(typescript): update to typescript@5.3.3 (fixes #345) * chore(dependencies): update @types/node (fixes #335) * fix(withabort): add ts-ignore to withabort operator impl (fixes #345) --- package.json | 4 +- src/asynciterable/asasynciterable.ts | 61 ++++++++++++++---------- src/asynciterable/fromdomstream.ts | 8 ++-- src/asynciterable/operators/withabort.ts | 1 + src/asynciterable/todomstream.ts | 8 ++-- 5 files changed, 49 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index ab6734f7..0eb8dba5 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.16.10", - "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 2897b564..59313f38 100644 --- a/src/asynciterable/asasynciterable.ts +++ b/src/asynciterable/asasynciterable.ts @@ -2,39 +2,52 @@ 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'); -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); +}; + 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 6fe26ef8..835afd65 100644 --- a/src/asynciterable/fromdomstream.ts +++ b/src/asynciterable/fromdomstream.ts @@ -63,7 +63,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; } @@ -71,7 +71,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; @@ -83,7 +83,7 @@ async function readNext( reader: ReadableStreamReader, bufferOrLen: ArrayBufferLike | number, offset: number -): Promise> { +): Promise> { let size: number; let buffer: ArrayBufferLike; @@ -106,7 +106,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 cb052851..83a06a0e 100644 --- a/src/asynciterable/todomstream.ts +++ b/src/asynciterable/todomstream.ts @@ -49,12 +49,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(); @@ -85,7 +85,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;