-
Notifications
You must be signed in to change notification settings - Fork 15
TS: add transform and transformAsync type definitions #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4dacc1d
TS: add `transform` and `transformAsync` type definitions
larabr 97daaa9
TS: `transform`, `transformAsync`: Switch to function overload instea…
larabr d98aba1
CI: add tests for legacy type definitions
larabr 22cc3b1
TS: `transform`, `transformAsync`: Simplify type definitions
twiss 62cb334
TS: `transform`, `transformAsync`: fix handling of possibly undefined…
larabr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "extends": "../tsconfig.json", | ||
| "compilerOptions": { | ||
| "noEmit": true, // code tested by tsx | ||
| }, | ||
| "include": ["./typescript.v4.7.test.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* global process */ | ||
| /** | ||
| * Tests cases are taken from `typescript.test.ts` but do not include the WebStreamPonyfill | ||
| * since the v3.3.3 ponyfill module resolution (required with the legacy TS version) does not work properly. | ||
| */ | ||
| import assert from 'assert'; | ||
| import { Readable as NodeNativeReadableStream } from 'stream'; | ||
| import { ReadableStream as NodeWebReadableStream } from 'node:stream/web'; | ||
| import { type NodeWebStream,type Stream, toStream, transform, transformAsync, type Data, readToEnd } from '@openpgp/web-stream-tools'; | ||
| // @ts-expect-error missing defs | ||
| import { ArrayStream, isArrayStream } from '@openpgp/web-stream-tools'; | ||
|
|
||
| const newEmptyTypedNodeWebStream = <T extends Data>() => ( | ||
| new NodeWebReadableStream({ start(ctrl) { ctrl.close(); } }) as NodeWebStream<T> | ||
| ); | ||
|
|
||
| (async () => { | ||
| const nodeWebStream: NodeWebStream<string> = NodeNativeReadableStream.toWeb(new NodeNativeReadableStream()); | ||
| assert(nodeWebStream instanceof NodeWebReadableStream); | ||
| // @ts-expect-error detect node stream type mismatch | ||
| const nodeNativeStream: NodeWebStream<string> = new NodeNativeReadableStream(); | ||
| assert(nodeNativeStream instanceof NodeNativeReadableStream); | ||
|
|
||
| await readToEnd(new Uint8Array([1])) as Uint8Array; | ||
| await readToEnd(new Uint8Array([1]), _ => _) as Uint8Array[]; | ||
| // @ts-expect-error expect string type | ||
| await readToEnd(newEmptyTypedNodeWebStream<string>()) as Uint8Array; | ||
| await readToEnd(newEmptyTypedNodeWebStream<string>(), () => new Uint8Array()) as Uint8Array; | ||
|
|
||
| const anotherNodeWebStream: NodeWebStream<string> = toStream(nodeWebStream); | ||
| assert(anotherNodeWebStream instanceof NodeWebReadableStream); | ||
| // The following type assertion may fail or not depending on the TS and @types/node versions; | ||
| // it doesn't fail with TS v4.7 | ||
| // // @ts-expect-error expect node stream in output | ||
| // const expectedWebStreamButActualNodeStream: WebStream<string> = toStream(nodeWebStream); | ||
| // assert(expectedWebStreamButActualNodeStream instanceof NodeWebReadableStream); | ||
| const newStringStream: Stream<string> = toStream('chunk'); | ||
| assert(newStringStream instanceof NodeWebReadableStream); | ||
| // @ts-expect-error detect type parameter mismatch | ||
| const anotherStringStream: Stream<Uint8Array> = toStream('chunk'); | ||
| assert(anotherStringStream instanceof NodeWebReadableStream); | ||
|
|
||
| assert(isArrayStream(new ArrayStream())) ; // ensure Array is actually extended in e.g. es5 | ||
|
|
||
| const transformDefaultOutput: undefined = transform('string'); | ||
| assert(transformDefaultOutput === undefined); | ||
| const transformStreamUndefinedOutput: Stream<never> = transform(newEmptyTypedNodeWebStream(), () => {}); | ||
| assert(transformStreamUndefinedOutput instanceof NodeWebReadableStream); | ||
| const transformOutputStreamString: Stream<string> = transform(newEmptyTypedNodeWebStream<string>(), () => ''); | ||
| assert(transformOutputStreamString instanceof NodeWebReadableStream); | ||
| const transformProcessOutputString: string = transform('string', () => ''); | ||
| assert(typeof transformProcessOutputString === 'string'); | ||
| const transformConcatOutputString: string = transform('string', () => '', () => ''); | ||
| assert(typeof transformConcatOutputString === 'string'); | ||
| const transformFinishOutputString: string = transform('string', undefined, () => ''); | ||
| assert(typeof transformFinishOutputString === 'string'); | ||
| const transformProcessOutputStreamBytes: Stream<Uint8Array> = transform( | ||
| newEmptyTypedNodeWebStream<string>(), | ||
| () => new Uint8Array() | ||
| ); | ||
| assert(transformProcessOutputStreamBytes instanceof NodeWebReadableStream); | ||
| const transformMismatchingProcessFinishOutput: Stream<string | Uint8Array> = | ||
| transform(newEmptyTypedNodeWebStream<string>(), () => new Uint8Array(), () => ''); | ||
| assert(transformMismatchingProcessFinishOutput instanceof NodeWebReadableStream); | ||
| // @ts-expect-error on async callback | ||
| transform(newEmptyTypedNodeWebStream<string>(), async () => 'string'); | ||
| const transformAsyncOutputStreamStringToBytes: Stream<Uint8Array> = await transformAsync( | ||
| newEmptyTypedNodeWebStream<string>(), | ||
| async () => new Uint8Array(), | ||
| async () => new Uint8Array() | ||
| ); | ||
| assert(transformAsyncOutputStreamStringToBytes instanceof NodeWebReadableStream); | ||
| // @ts-expect-error on sync callback | ||
| transformAsync(newEmptyTypedNodeWebStream<string>(), async () => new Uint8Array(), () => new Uint8Array()); | ||
|
|
||
| console.log('TypeScript definitions are correct'); | ||
| })().catch(e => { | ||
| console.error('TypeScript definitions tests failed by throwing the following error'); | ||
| console.error(e); | ||
| process.exit(1); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.