Skip to content

Commit

Permalink
Add support for reading from a WebStreams
Browse files Browse the repository at this point in the history
- Stream Blob via a WebStreams, instead of buffering the full content
- Update strtok3 to v7.0.0
  • Loading branch information
Borewit committed Jul 4, 2024
1 parent 37233b1 commit 03e64c8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ export class FileTypeParser {
}

async fromBlob(blob) {
const buffer = await blob.arrayBuffer();
return this.fromBuffer(new Uint8Array(buffer));
return this.fromWebStream(blob.stream());
}

async fromStream(stream) {
Expand All @@ -101,6 +100,15 @@ export class FileTypeParser {
}
}

async fromWebStream(webStream) {
const tokenizer = await strtok3.fromWebStream(webStream);
try {
return await this.fromTokenizer(tokenizer);
} finally {
await tokenizer.close();
}
}

async toDetectionStream(readableStream, options = {}) {
const {default: stream} = await import('node:stream');
const {sampleSize = minimumBytes} = options;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@
"dependencies": {
"readable-web-to-node-stream": "^3.0.2",
"strtok3": "^7.0.0",
"token-types": "^5.0.1"
"token-types": "^6.0.0"
},
"devDependencies": {
"@tokenizer/token": "^0.3.0",
Expand Down

0 comments on commit 03e64c8

Please sign in to comment.