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 5, 2024
1 parent 37233b1 commit 1ba1549
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:
fail-fast: false
matrix:
node-version:
- 22
- 20
- 18
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand Down
10 changes: 4 additions & 6 deletions browser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {ReadableWebToNodeStream} from 'readable-web-to-node-stream';
import {fileTypeFromStream as coreFileTypeFromStream} from './core.js';
import {fileTypeFromWebStream} from './core.js';

export async function fileTypeFromStream(stream) {
const readableWebToNodeStream = new ReadableWebToNodeStream(stream);
const fileType = await coreFileTypeFromStream(readableWebToNodeStream);
await readableWebToNodeStream.close();
export async function fileTypeFromStream(webStream) {
const fileType = await fileTypeFromWebStream(webStream);
await webStream.close();
return fileType;
}

Expand Down
22 changes: 17 additions & 5 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {extensions, mimeTypes} from './supported.js';

const minimumBytes = 4100; // A fair amount of file-types are detectable within this range.

export async function fileTypeFromWebStream(webStream) {
return new FileTypeParser().fromWebStream(webStream);
}

export async function fileTypeFromStream(stream) {
return new FileTypeParser().fromStream(stream);
}
Expand Down Expand Up @@ -88,8 +92,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 +104,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 Expand Up @@ -576,7 +588,7 @@ export class FileTypeParser {
) {
// They all can have MIME `video/mp4` except `application/mp4` special-case which is hard to detect.
// For some cases, we're specific, everything else falls to `video/mp4` with `mp4` extension.
const brandMajor = this.buffer.toString('binary', 8, 12).replace('\0', ' ').trim();
const brandMajor = this.buffer.toString('latin1', 8, 12).replace('\0', ' ').trim();
switch (brandMajor) {
case 'avif':
case 'avis':
Expand Down Expand Up @@ -1059,7 +1071,7 @@ export class FileTypeParser {
}

if (this.checkString('AC')) {
const version = this.buffer.toString('binary', 2, 6);
const version = this.buffer.toString('latin1', 2, 6);
if (version.match('^d*') && version >= 1000 && version <= 1050) {
return {
ext: 'dwg',
Expand Down Expand Up @@ -1126,7 +1138,7 @@ export class FileTypeParser {
async function readChunkHeader() {
return {
length: await tokenizer.readToken(Token.INT32_BE),
type: await tokenizer.readToken(new Token.StringType(4, 'binary')),
type: await tokenizer.readToken(new Token.StringType(4, 'latin1')),
};
}

Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,8 @@
"fbx"
],
"dependencies": {
"readable-web-to-node-stream": "^3.0.2",
"strtok3": "^7.0.0",
"token-types": "^5.0.1"
"strtok3": "^7.1.0",
"token-types": "^6.0.0"
},
"devDependencies": {
"@tokenizer/token": "^0.3.0",
Expand Down

0 comments on commit 1ba1549

Please sign in to comment.