Skip to content

Commit

Permalink
Be able to abort asynchronous operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Sep 4, 2024
1 parent 988bf4b commit 8e9b4ce
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ export function fileTypeStream(webStream: AnyWebReadableStream<Uint8Array>, opti
export declare class FileTypeParser {
detectors: Iterable<Detector>;

constructor(options?: {customDetectors?: Iterable<Detector>});
constructor(options?: {customDetectors?: Iterable<Detector>; abortSignal: AbortSignal});

/**
Works the same way as {@link fileTypeFromBuffer}, additionally taking into account custom detectors (if any were provided to the constructor).
Expand Down
8 changes: 5 additions & 3 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export async function fileTypeStream(webStream, options) {
export class FileTypeParser {
constructor(options) {
this.detectors = options?.customDetectors;

this.tokenizerOptions = {
abortSignal: options?.abortSignal,
};
this.fromTokenizer = this.fromTokenizer.bind(this);
this.fromBuffer = this.fromBuffer.bind(this);
this.parse = this.parse.bind(this);
Expand Down Expand Up @@ -92,15 +94,15 @@ export class FileTypeParser {
return;
}

return this.fromTokenizer(strtok3.fromBuffer(buffer));
return this.fromTokenizer(strtok3.fromBuffer(buffer, this.tokenizerOptions));
}

async fromBlob(blob) {
return this.fromStream(blob.stream());
}

async fromStream(stream) {
const tokenizer = await strtok3.fromWebStream(stream);
const tokenizer = await strtok3.fromWebStream(stream, this.tokenizerOptions);
try {
return await this.fromTokenizer(tokenizer);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {FileTypeParser, reasonableDetectionSizeInBytes} from './core.js';

export class NodeFileTypeParser extends FileTypeParser {
async fromStream(stream) {
const tokenizer = await (stream instanceof WebReadableStream ? strtok3.fromWebStream(stream) : strtok3.fromStream(stream));
const tokenizer = await (stream instanceof WebReadableStream ? strtok3.fromWebStream(stream, this.tokenizerOptions) : strtok3.fromStream(stream, this.tokenizerOptions));
try {
return super.fromTokenizer(tokenizer);
} finally {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@
],
"dependencies": {
"get-stream": "^9.0.1",
"strtok3": "^8.1.0",
"strtok3": "^9.0.0",
"token-types": "^6.0.0",
"uint8array-extras": "^1.3.0"
},
Expand Down Expand Up @@ -252,5 +252,6 @@
},
"ava": {
"serial": true
}
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,22 @@ const fileType = await parser.fromBuffer(buffer);
console.log(fileType);
```
## Abort signal
Some asynchronous operations can be aborted by passing the [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) interface to the `FileTypeParser` constructor.
```js
import {FileTypeParser} from 'file-type'; // or `NodeFileTypeParser` in Node.js

const abortController = new AbortController()

const parser = new FileTypeParser({abortSignal: abortController.signal});

const promise = parser.fromStream(blob.stream());

abortController.abort(); // Abort file-type reading from the Blob stream.
```
## Supported file types
- [`3g2`](https://en.wikipedia.org/wiki/3GP_and_3G2#3G2) - Multimedia container format defined by the 3GPP2 for 3G CDMA2000 multimedia services
Expand Down

0 comments on commit 8e9b4ce

Please sign in to comment.