-
-
Notifications
You must be signed in to change notification settings - Fork 377
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
Add support for reading from a WebStreams #635
Changes from 2 commits
1fce3c4
7271b4b
98f2abd
5e5017d
8e083cc
3caa304
259a97d
fc746a2
d5d55ef
fea8f4c
4e20669
47dca33
c19a4e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ jobs: | |
fail-fast: false | ||
matrix: | ||
node-version: | ||
- 22 | ||
- 20 | ||
- 18 | ||
steps: | ||
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import type {Readable as ReadableStream} from 'node:stream'; | ||
/** | ||
* Typings for primary entry point, Node.js specific typings can be found in index.d.ts | ||
*/ | ||
|
||
import type {ReadableStream as WebReadableStream} from 'node:stream/web'; | ||
import type {Readable as NodeReadableStream} from 'node:stream'; | ||
import type {ITokenizer} from 'strtok3'; | ||
|
||
export type FileExtension = | ||
|
@@ -318,7 +323,7 @@ export type FileTypeResult = { | |
readonly mime: MimeType; | ||
}; | ||
|
||
export type ReadableStreamWithFileType = ReadableStream & { | ||
export type ReadableStreamWithFileType = WebReadableStream & { | ||
readonly fileType?: FileTypeResult; | ||
}; | ||
|
||
|
@@ -339,10 +344,10 @@ Detect the file type of a Node.js [readable stream](https://nodejs.org/api/strea | |
|
||
The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer. | ||
|
||
@param stream - A readable stream representing file data. | ||
@param stream - A Node.js Readable stream or Web API Readable Stream representing file data. The Web Readable stream **must be a byte stream**. | ||
@returns The detected file type, or `undefined` when there is no match. | ||
*/ | ||
export function fileTypeFromStream(stream: ReadableStream): Promise<FileTypeResult | undefined>; | ||
export function fileTypeFromStream(stream: WebReadableStream): Promise<FileTypeResult | undefined>; | ||
|
||
/** | ||
Detect the file type from an [`ITokenizer`](https://github.com/Borewit/strtok3#tokenizer) source. | ||
|
@@ -420,7 +425,7 @@ if (stream2.fileType?.mime === 'image/jpeg') { | |
} | ||
``` | ||
*/ | ||
export function fileTypeStream(readableStream: ReadableStream, options?: StreamOptions): Promise<ReadableStreamWithFileType>; | ||
export function fileTypeStream(readableStream: WebReadableStream<Uint8Array>, options?: StreamOptions): Promise<ReadableStreamWithFileType>; | ||
|
||
/** | ||
Detect the file type of a [`Blob`](https://nodejs.org/api/buffer.html#class-blob) or [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File). | ||
|
@@ -511,7 +516,7 @@ export declare class FileTypeParser { | |
/** | ||
Works the same way as {@link fileTypeFromStream}, additionally taking into account custom detectors (if any were provided to the constructor). | ||
*/ | ||
fromStream(stream: ReadableStream): Promise<FileTypeResult | undefined>; | ||
fromStream(stream: NodeReadableStream): Promise<FileTypeResult | undefined>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, should probably be done as well. (see #636) |
||
|
||
/** | ||
Works the same way as {@link fileTypeFromTokenizer}, additionally taking into account custom detectors (if any were provided to the constructor). | ||
|
@@ -526,5 +531,5 @@ export declare class FileTypeParser { | |
/** | ||
Works the same way as {@link fileTypeStream}, additionally taking into account custom detectors (if any were provided to the constructor). | ||
*/ | ||
toDetectionStream(readableStream: ReadableStream, options?: StreamOptions): Promise<FileTypeResult | undefined>; | ||
toDetectionStream(readableStream: NodeReadableStream, options?: StreamOptions): Promise<FileTypeResult | undefined>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,30 @@ | ||
import type {FileTypeResult} from './core.js'; | ||
|
||
/** | ||
Detect the file type of a file path. | ||
* Typings for Node.js specific entry point | ||
*/ | ||
|
||
import type {Readable as NodeReadableStream} from 'node:stream'; | ||
import type {ReadableStream as WebReadableStream} from 'node:stream/web'; | ||
import type {FileTypeResult} from './core.js'; | ||
import {FileTypeParser} from './core.js'; | ||
|
||
The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer. | ||
export declare class NodeFileTypeParser extends FileTypeParser { | ||
/** | ||
* | ||
* @param stream Node.js Stream readable or Web API StreamReadable | ||
*/ | ||
fromStream(stream: WebReadableStream<Uint8Array> | NodeReadableStream): Promise<FileTypeResult | undefined>; | ||
} | ||
|
||
@param path - The file path to parse. | ||
@returns The detected file type and MIME type or `undefined` when there is no match. | ||
*/ | ||
/** | ||
* Detect the file type of a file path. | ||
* | ||
* The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer. | ||
* | ||
* @param path | ||
* @returns The detected file type and MIME type or `undefined` when there is no match. | ||
*/ | ||
export function fileTypeFromFile(path: string): Promise<FileTypeResult | undefined>; | ||
|
||
export function fileTypeFromStream(stream: WebReadableStream<Uint8Array> | NodeReadableStream): Promise<FileTypeResult | undefined>; | ||
|
||
export * from './core.js'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type should be available globally, so I don't think we need to import it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The global type in incompatible with Node.js type. If I change it here, the problem will appear elsewhere,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what that means, but this is
core.d.ts
, so it shouldn't import types only available for Node.js.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Node.js Readable types are incompatible with the global
lib.dom.d.ts
types, one things the challenges mentioned in #588 (comment), related to this issue DefinitelyTyped/DefinitelyTyped#60377, which unfortunately got closed with PR aiming to resolve that.I used the Node.js, as Node.js has been the primary supported platform. Using the
lib.dom.d.ts
I need hack in types mapping cast somewhere.Maybe good accept both, an do an ugly type cast, just for the convenience for our users.
Off-topic: The other mind f*ck, was the BYOB Readable Stream, The "Bring-Your-Own-Buffer" reader:
Well the first things the zero-copy method does is hijacking the buffer you bring, and essentially turns into junk (there is formal property for this state, forgot the name), meaning it can no longer be used. Which essentially forces you to create a new Buffer (as it becomes totally useless after providing it), and then copying the data to the Buffer you wanted to have it written in the first place. So the only feature BYOB has, versus the ReadableStreamDefaultReader is that you can control the chunk length to be written. How confusing, what a disappointment.