@@ -10,6 +10,10 @@ import {extensions, mimeTypes} from './supported.js';
10
10
11
11
const minimumBytes = 4100 ; // A fair amount of file-types are detectable within this range.
12
12
13
+ export async function fileTypeFromWebStream ( webStream ) {
14
+ return new FileTypeParser ( ) . fromWebStream ( webStream ) ;
15
+ }
16
+
13
17
export async function fileTypeFromStream ( stream ) {
14
18
return new FileTypeParser ( ) . fromStream ( stream ) ;
15
19
}
@@ -88,8 +92,7 @@ export class FileTypeParser {
88
92
}
89
93
90
94
async fromBlob ( blob ) {
91
- const buffer = await blob . arrayBuffer ( ) ;
92
- return this . fromBuffer ( new Uint8Array ( buffer ) ) ;
95
+ return this . fromWebStream ( blob . stream ( ) ) ;
93
96
}
94
97
95
98
async fromStream ( stream ) {
@@ -101,6 +104,15 @@ export class FileTypeParser {
101
104
}
102
105
}
103
106
107
+ async fromWebStream ( webStream ) {
108
+ const tokenizer = await strtok3 . fromWebStream ( webStream ) ;
109
+ try {
110
+ return await this . fromTokenizer ( tokenizer ) ;
111
+ } finally {
112
+ await tokenizer . close ( ) ;
113
+ }
114
+ }
115
+
104
116
async toDetectionStream ( readableStream , options = { } ) {
105
117
const { default : stream } = await import ( 'node:stream' ) ;
106
118
const { sampleSize = minimumBytes } = options ;
@@ -576,7 +588,7 @@ export class FileTypeParser {
576
588
) {
577
589
// They all can have MIME `video/mp4` except `application/mp4` special-case which is hard to detect.
578
590
// For some cases, we're specific, everything else falls to `video/mp4` with `mp4` extension.
579
- const brandMajor = this . buffer . toString ( 'binary ' , 8 , 12 ) . replace ( '\0' , ' ' ) . trim ( ) ;
591
+ const brandMajor = this . buffer . toString ( 'latin1 ' , 8 , 12 ) . replace ( '\0' , ' ' ) . trim ( ) ;
580
592
switch ( brandMajor ) {
581
593
case 'avif' :
582
594
case 'avis' :
@@ -1059,7 +1071,7 @@ export class FileTypeParser {
1059
1071
}
1060
1072
1061
1073
if ( this . checkString ( 'AC' ) ) {
1062
- const version = this . buffer . toString ( 'binary ' , 2 , 6 ) ;
1074
+ const version = this . buffer . toString ( 'latin1 ' , 2 , 6 ) ;
1063
1075
if ( version . match ( '^d*' ) && version >= 1000 && version <= 1050 ) {
1064
1076
return {
1065
1077
ext : 'dwg' ,
@@ -1126,7 +1138,7 @@ export class FileTypeParser {
1126
1138
async function readChunkHeader ( ) {
1127
1139
return {
1128
1140
length : await tokenizer . readToken ( Token . INT32_BE ) ,
1129
- type : await tokenizer . readToken ( new Token . StringType ( 4 , 'binary ' ) ) ,
1141
+ type : await tokenizer . readToken ( new Token . StringType ( 4 , 'latin1 ' ) ) ,
1130
1142
} ;
1131
1143
}
1132
1144
0 commit comments