-
-
Notifications
You must be signed in to change notification settings - Fork 352
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1fce3c4
Add support for reading from a WebStreams
Borewit 7271b4b
Move Node.js dependencies from `core.js` to `index.js`
Borewit 98f2abd
Update core.d.ts
sindresorhus 5e5017d
Update core.js
sindresorhus 8e083cc
Update index.js
sindresorhus 3caa304
Update index.d.ts
sindresorhus 259a97d
Update index.d.ts
sindresorhus fc746a2
Update index.d.ts
sindresorhus d5d55ef
Move Node detection Stream functionality from primary entry point (co…
Borewit fea8f4c
Accept either the Node.js ReadableStream or the `lib.dom.d.ts` Readab…
Borewit 4e20669
Update index.d.ts
sindresorhus 47dca33
Update core.d.ts
sindresorhus c19a4e8
Update index.d.ts
sindresorhus 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 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 |
---|---|---|
|
@@ -10,6 +10,7 @@ jobs: | |
fail-fast: false | ||
matrix: | ||
node-version: | ||
- 22 | ||
- 20 | ||
- 18 | ||
steps: | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 |
---|---|---|
@@ -1,13 +1,69 @@ | ||
import type {FileTypeResult} from './core.js'; | ||
/** | ||
Typings for Node.js specific entry point. | ||
*/ | ||
|
||
import type {Readable as NodeReadableStream} from 'node:stream'; | ||
import type {FileTypeResult, StreamOptions, AnyWebReadableStream} from './core.js'; | ||
import {FileTypeParser} from './core.js'; | ||
|
||
export type ReadableStreamWithFileType = NodeReadableStream & { | ||
readonly fileType?: FileTypeResult; | ||
}; | ||
|
||
export declare class NodeFileTypeParser extends FileTypeParser { | ||
/** | ||
@param stream - Node.js `stream.Readable` or Web API `ReadableStream`. | ||
*/ | ||
fromStream(stream: AnyWebReadableStream<Uint8Array> | NodeReadableStream): Promise<FileTypeResult | undefined>; | ||
|
||
/** | ||
Works the same way as {@link fileTypeStream}, additionally taking into account custom detectors (if any were provided to the constructor). | ||
*/ | ||
toDetectionStream(readableStream: NodeReadableStream, options?: StreamOptions): Promise<ReadableStreamWithFileType>; | ||
} | ||
|
||
/** | ||
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 - The file path to parse. | ||
@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: AnyWebReadableStream<Uint8Array> | NodeReadableStream): Promise<FileTypeResult | undefined>; | ||
|
||
/** | ||
Returns a `Promise` which resolves to the original readable stream argument, but with an added `fileType` property, which is an object like the one returned from `fileTypeFromFile()`. | ||
|
||
This method can be handy to put in between a stream, but it comes with a price. | ||
Internally `stream()` builds up a buffer of `sampleSize` bytes, used as a sample, to determine the file type. | ||
The sample size impacts the file detection resolution. | ||
A smaller sample size will result in lower probability of the best file type detection. | ||
|
||
**Note:** This method is only available when using Node.js. | ||
**Note:** Requires Node.js 14 or later. | ||
|
||
@param readableStream - A [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) containing a file to examine. | ||
@param options - Maybe used to override the default sample-size. | ||
@returns A `Promise` which resolves to the original readable stream argument, but with an added `fileType` property, which is an object like the one returned from `fileTypeFromFile()`. | ||
|
||
@example | ||
``` | ||
import got from 'got'; | ||
import {fileTypeStream} from 'file-type'; | ||
|
||
const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg'; | ||
|
||
const stream1 = got.stream(url); | ||
const stream2 = await fileTypeStream(stream1, {sampleSize: 1024}); | ||
|
||
if (stream2.fileType?.mime === 'image/jpeg') { | ||
// stream2 can be used to stream the JPEG image (from the very beginning of the stream) | ||
} | ||
``` | ||
*/ | ||
export function fileTypeStream(readableStream: NodeReadableStream, options?: StreamOptions): Promise<ReadableStreamWithFileType>; | ||
|
||
export * from './core.js'; |
This file contains 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
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.
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.