1313const fileSignatures = {
1414 // Document formats.
1515 'application/pdf' : [ [ 0x25 , 0x50 , 0x44 , 0x46 , 0x2d ] ] ,
16+
1617 // Archive formats:
1718 'application/x-tar' : [
1819 [ 0x75 , 0x73 , 0x74 , 0x61 , 0x72 , 0x00 , 0x30 , 0x30 ] ,
1920 [ 0x75 , 0x73 , 0x74 , 0x61 , 0x72 , 0x20 , 0x20 , 0x00 ] ,
2021 ] ,
21- // Compressed archive formats.
2222 'application/x-7z-compressed' : [ [ 0x37 , 0x7A , 0xBC , 0xAF , 0x27 , 0x1C ] ] ,
2323 'application/x-bzip2' : [ [ 0x42 , 0x5A , 0x68 ] ] ,
2424 'application/x-rar-compressed' : [ [ 0x52 , 0x61 , 0x72 , 0x21 , 0x1A , 0x07 ] ] ,
2525 'application/zip' : [ [ 0x50 , 0x4B , 0x03 , 0x04 ] , [ 0x50 , 0x4B , 0x05 , 0x06 ] , [ 0x50 , 0x4B , 0x07 , 0x08 ] ] ,
26+
2627 // Image formats.
2728 'image/bmp' : [ [ 0x42 , 0x4D ] ] ,
2829 'image/gif' : [ [ 0x47 , 0x49 , 0x46 , 0x38 ] ] ,
2930 'image/jpeg' : [ [ 0xFF , 0xD8 , 0xFF ] ] ,
3031 'image/png' : [ [ 0x89 , 0x50 , 0x4E , 0x47 , 0x0D , 0x0A , 0x1A , 0x0A ] ] ,
3132 'image/webp' : [ [ 0x52 , 0x49 , 0x46 , 0x46 , '??' , '??' , '??' , '??' , 0x57 , 0x45 , 0x42 , 0x50 ] ] ,
33+ 'image/x-icon' : [ [ 0x00 , 0x00 , 0x01 , 0x00 ] ] ,
34+
3235 // Audio/Video formats.
3336 'application/ogg' : [ [ 0x4F , 0x67 , 0x67 , 0x53 ] ] ,
3437 'audio/flac' : [ [ 0x66 , 0x4C , 0x61 , 0x43 ] ] ,
@@ -77,8 +80,12 @@ export function initialize() {
7780 for ( const byte of signature ) {
7881 if ( curNode . children [ byte ] === undefined ) {
7982 if ( byte === '??' && ! curNode . children [ '??' ] && Object . keys ( curNode . children ) . length > 0 ) {
83+ // Reset the byte tree, it is bogus.
84+ root = null ;
8085 throw 'Cannot add a placeholder child to a node that has non-placeholder children' ;
8186 } else if ( byte !== '??' && curNode . children [ '??' ] ) {
87+ // Reset the byte tree, it is bogus.
88+ root = null ;
8289 throw 'Cannot add a non-placeholder child to a node that has a placeholder child' ;
8390 }
8491 curNode . children [ byte ] = new Node ( byte ) ;
@@ -114,6 +121,7 @@ export function findMimeType(ab) {
114121 const depth = ab . byteLength < maxDepth ? ab . byteLength : maxDepth ;
115122 const arr = new Uint8Array ( ab ) . subarray ( 0 , depth ) ;
116123 let curNode = root ;
124+ let mimeType ;
117125 // Step through bytes, updating curNode as it walks down the byte tree.
118126 for ( const byte of arr ) {
119127 // If this node has a placeholder child, just step into it.
@@ -123,6 +131,10 @@ export function findMimeType(ab) {
123131 }
124132 if ( curNode . children [ byte ] === undefined ) return undefined ;
125133 curNode = curNode . children [ byte ] ;
126- if ( curNode . mimeType ) return curNode . mimeType ;
134+ if ( curNode . mimeType ) {
135+ mimeType = curNode . mimeType ;
136+ break ;
137+ }
127138 }
139+ return mimeType ;
128140}
0 commit comments