Skip to content

Commit e4a809e

Browse files
committed
Improve docs for fileTypeFromBlob
Fixes #521
1 parent d0f31fe commit e4a809e

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

browser.d.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {FileTypeResult} from './core.js';
22

33
/**
4-
Determine file type from a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
4+
Detect the file type of a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
55
6+
@example
67
```
78
import {fileTypeFromStream} from 'file-type';
89
@@ -18,8 +19,11 @@ console.log(fileType);
1819
export declare function fileTypeFromStream(stream: ReadableStream): Promise<FileTypeResult | undefined>;
1920

2021
/**
21-
Determine file type from a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
22+
Detect the file type of a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
2223
24+
__Note:__ This method is only available in the browser.
25+
26+
@example
2327
```
2428
import {fileTypeFromBlob} from 'file-type';
2529

core.d.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,12 @@ This method is used internally, but can also be used for a special "tokenizer" r
323323
324324
A tokenizer propagates the internal read functions, allowing alternative transport mechanisms, to access files, to be implemented and used.
325325
326+
@param tokenizer - File source implementing the tokenizer interface.
327+
@returns The detected file type and MIME type, or `undefined` when there is no match.
328+
326329
An example is [`@tokenizer/http`](https://github.com/Borewit/tokenizer-http), which requests data using [HTTP-range-requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests). A difference with a conventional stream and the [*tokenizer*](https://github.com/Borewit/strtok3#tokenizer), is that it is able to *ignore* (seek, fast-forward) in the stream. For example, you may only need and read the first 6 bytes, and the last 128 bytes, which may be an advantage in case reading the entire file would take longer.
327330
331+
@example
328332
```
329333
import {makeTokenizer} from '@tokenizer/http';
330334
import {fileTypeFromTokenizer} from 'file-type';
@@ -337,9 +341,6 @@ const fileType = await fileTypeFromTokenizer(httpTokenizer);
337341
console.log(fileType);
338342
//=> {ext: 'mp3', mime: 'audio/mpeg'}
339343
```
340-
341-
@param tokenizer - File source implementing the tokenizer interface.
342-
@returns The detected file type and MIME type, or `undefined` when there is no match.
343344
*/
344345
export function fileTypeFromTokenizer(tokenizer: ITokenizer): Promise<FileTypeResult | undefined>;
345346

readme.md

+27-12
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,6 @@ console.log(fileType);
129129
//=> {ext: 'jpg', mime: 'image/jpeg'}
130130
```
131131

132-
```js
133-
import {fileTypeFromBlob} from 'file-type';
134-
135-
const blob = new Blob(['<?xml version="1.0" encoding="ISO-8859-1" ?>'], {
136-
type: 'plain/text',
137-
endings: 'native'
138-
});
139-
140-
console.log(await fileTypeFromBlob(blob));
141-
//=> {ext: 'txt', mime: 'plain/text'}
142-
```
143-
144132
## API
145133

146134
### fileTypeFromBuffer(buffer)
@@ -202,6 +190,33 @@ Type: [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream
202190

203191
A readable stream representing file data.
204192

193+
### fileTypeFromBlob(blob)
194+
195+
Detect the file type of a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
196+
197+
**Note:** This method is only available in the browser.
198+
199+
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.
200+
201+
Returns a `Promise` for an object with the detected file type and MIME type:
202+
203+
- `ext` - One of the [supported file types](#supported-file-types)
204+
- `mime` - The [MIME type](https://en.wikipedia.org/wiki/Internet_media_type)
205+
206+
Or `undefined` when there is no match.
207+
208+
```js
209+
import {fileTypeFromBlob} from 'file-type';
210+
211+
const blob = new Blob(['<?xml version="1.0" encoding="ISO-8859-1" ?>'], {
212+
type: 'plain/text',
213+
endings: 'native'
214+
});
215+
216+
console.log(await fileTypeFromBlob(blob));
217+
//=> {ext: 'txt', mime: 'plain/text'}
218+
```
219+
205220
### fileTypeFromTokenizer(tokenizer)
206221

207222
Detect the file type from an `ITokenizer` source.

0 commit comments

Comments
 (0)