8889841cindex.js000066600000056132150514465730006236 0ustar00'use strict'; const { multiByteIndexOf, stringToBytes, readUInt64LE, tarHeaderChecksumMatches, uint8ArrayUtf8ByteString } = require('./util'); const supported = require('./supported'); const xpiZipFilename = stringToBytes('META-INF/mozilla.rsa'); const oxmlContentTypes = stringToBytes('[Content_Types].xml'); const oxmlRels = stringToBytes('_rels/.rels'); const fileType = input => { if (!(input instanceof Uint8Array || input instanceof ArrayBuffer || Buffer.isBuffer(input))) { throw new TypeError(`Expected the \`input\` argument to be of type \`Uint8Array\` or \`Buffer\` or \`ArrayBuffer\`, got \`${typeof input}\``); } const buffer = input instanceof Uint8Array ? input : new Uint8Array(input); if (!(buffer && buffer.length > 1)) { return; } const check = (header, options) => { options = { offset: 0, ...options }; for (let i = 0; i < header.length; i++) { // If a bitmask is set if (options.mask) { // If header doesn't equal `buf` with bits masked off if (header[i] !== (options.mask[i] & buffer[i + options.offset])) { return false; } } else if (header[i] !== buffer[i + options.offset]) { return false; } } return true; }; const checkString = (header, options) => check(stringToBytes(header), options); if (check([0xFF, 0xD8, 0xFF])) { return { ext: 'jpg', mime: 'image/jpeg' }; } if (check([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) { // APNG format (https://wiki.mozilla.org/APNG_Specification) // 1. Find the first IDAT (image data) chunk (49 44 41 54) // 2. Check if there is an "acTL" chunk before the IDAT one (61 63 54 4C) // Offset calculated as follows: // - 8 bytes: PNG signature // - 4 (length) + 4 (chunk type) + 13 (chunk data) + 4 (CRC): IHDR chunk const startIndex = 33; const firstImageDataChunkIndex = buffer.findIndex((el, i) => i >= startIndex && buffer[i] === 0x49 && buffer[i + 1] === 0x44 && buffer[i + 2] === 0x41 && buffer[i + 3] === 0x54); const sliced = buffer.subarray(startIndex, firstImageDataChunkIndex); if (sliced.findIndex((el, i) => sliced[i] === 0x61 && sliced[i + 1] === 0x63 && sliced[i + 2] === 0x54 && sliced[i + 3] === 0x4C) >= 0) { return { ext: 'apng', mime: 'image/apng' }; } return { ext: 'png', mime: 'image/png' }; } if (check([0x47, 0x49, 0x46])) { return { ext: 'gif', mime: 'image/gif' }; } if (check([0x57, 0x45, 0x42, 0x50], {offset: 8})) { return { ext: 'webp', mime: 'image/webp' }; } if (check([0x46, 0x4C, 0x49, 0x46])) { return { ext: 'flif', mime: 'image/flif' }; } // `cr2`, `orf`, and `arw` need to be before `tif` check if ( (check([0x49, 0x49, 0x2A, 0x0]) || check([0x4D, 0x4D, 0x0, 0x2A])) && check([0x43, 0x52], {offset: 8}) ) { return { ext: 'cr2', mime: 'image/x-canon-cr2' }; } if (check([0x49, 0x49, 0x52, 0x4F, 0x08, 0x00, 0x00, 0x00, 0x18])) { return { ext: 'orf', mime: 'image/x-olympus-orf' }; } if ( check([0x49, 0x49, 0x2A, 0x00]) && (check([0x10, 0xFB, 0x86, 0x01], {offset: 4}) || check([0x08, 0x00, 0x00, 0x00], {offset: 4})) && // This pattern differentiates ARW from other TIFF-ish file types: check([0x00, 0xFE, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x01], {offset: 9}) ) { return { ext: 'arw', mime: 'image/x-sony-arw' }; } if ( check([0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00]) && (check([0x2D, 0x00, 0xFE, 0x00], {offset: 8}) || check([0x27, 0x00, 0xFE, 0x00], {offset: 8})) ) { return { ext: 'dng', mime: 'image/x-adobe-dng' }; } if ( check([0x49, 0x49, 0x2A, 0x00]) && check([0x1C, 0x00, 0xFE, 0x00], {offset: 8}) ) { return { ext: 'nef', mime: 'image/x-nikon-nef' }; } if (check([0x49, 0x49, 0x55, 0x00, 0x18, 0x00, 0x00, 0x00, 0x88, 0xE7, 0x74, 0xD8])) { return { ext: 'rw2', mime: 'image/x-panasonic-rw2' }; } // `raf` is here just to keep all the raw image detectors together. if (checkString('FUJIFILMCCD-RAW')) { return { ext: 'raf', mime: 'image/x-fujifilm-raf' }; } if ( check([0x49, 0x49, 0x2A, 0x0]) || check([0x4D, 0x4D, 0x0, 0x2A]) ) { return { ext: 'tif', mime: 'image/tiff' }; } if (check([0x42, 0x4D])) { return { ext: 'bmp', mime: 'image/bmp' }; } if (check([0x49, 0x49, 0xBC])) { return { ext: 'jxr', mime: 'image/vnd.ms-photo' }; } if (check([0x38, 0x42, 0x50, 0x53])) { return { ext: 'psd', mime: 'image/vnd.adobe.photoshop' }; } // Zip-based file formats // Need to be before the `zip` check const zipHeader = [0x50, 0x4B, 0x3, 0x4]; if (check(zipHeader)) { if ( check([0x6D, 0x69, 0x6D, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2F, 0x65, 0x70, 0x75, 0x62, 0x2B, 0x7A, 0x69, 0x70], {offset: 30}) ) { return { ext: 'epub', mime: 'application/epub+zip' }; } // Assumes signed `.xpi` from addons.mozilla.org if (check(xpiZipFilename, {offset: 30})) { return { ext: 'xpi', mime: 'application/x-xpinstall' }; } if (checkString('mimetypeapplication/vnd.oasis.opendocument.text', {offset: 30})) { return { ext: 'odt', mime: 'application/vnd.oasis.opendocument.text' }; } if (checkString('mimetypeapplication/vnd.oasis.opendocument.spreadsheet', {offset: 30})) { return { ext: 'ods', mime: 'application/vnd.oasis.opendocument.spreadsheet' }; } if (checkString('mimetypeapplication/vnd.oasis.opendocument.presentation', {offset: 30})) { return { ext: 'odp', mime: 'application/vnd.oasis.opendocument.presentation' }; } // The docx, xlsx and pptx file types extend the Office Open XML file format: // https://en.wikipedia.org/wiki/Office_Open_XML_file_formats // We look for: // - one entry named '[Content_Types].xml' or '_rels/.rels', // - one entry indicating specific type of file. // MS Office, OpenOffice and LibreOffice may put the parts in different order, so the check should not rely on it. let zipHeaderIndex = 0; // The first zip header was already found at index 0 let oxmlFound = false; let type; do { const offset = zipHeaderIndex + 30; if (!oxmlFound) { oxmlFound = (check(oxmlContentTypes, {offset}) || check(oxmlRels, {offset})); } if (!type) { if (checkString('word/', {offset})) { type = { ext: 'docx', mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }; } else if (checkString('ppt/', {offset})) { type = { ext: 'pptx', mime: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' }; } else if (checkString('xl/', {offset})) { type = { ext: 'xlsx', mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }; } } if (oxmlFound && type) { return type; } zipHeaderIndex = multiByteIndexOf(buffer, zipHeader, offset); } while (zipHeaderIndex >= 0); // No more zip parts available in the buffer, but maybe we are almost certain about the type? if (type) { return type; } } if ( check([0x50, 0x4B]) && (buffer[2] === 0x3 || buffer[2] === 0x5 || buffer[2] === 0x7) && (buffer[3] === 0x4 || buffer[3] === 0x6 || buffer[3] === 0x8) ) { return { ext: 'zip', mime: 'application/zip' }; } if ( check([0x30, 0x30, 0x30, 0x30, 0x30, 0x30], {offset: 148, mask: [0xF8, 0xF8, 0xF8, 0xF8, 0xF8, 0xF8]}) && // Valid tar checksum tarHeaderChecksumMatches(buffer) ) { return { ext: 'tar', mime: 'application/x-tar' }; } if ( check([0x52, 0x61, 0x72, 0x21, 0x1A, 0x7]) && (buffer[6] === 0x0 || buffer[6] === 0x1) ) { return { ext: 'rar', mime: 'application/x-rar-compressed' }; } if (check([0x1F, 0x8B, 0x8])) { return { ext: 'gz', mime: 'application/gzip' }; } if (check([0x42, 0x5A, 0x68])) { return { ext: 'bz2', mime: 'application/x-bzip2' }; } if (check([0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C])) { return { ext: '7z', mime: 'application/x-7z-compressed' }; } if (check([0x78, 0x01])) { return { ext: 'dmg', mime: 'application/x-apple-diskimage' }; } // `mov` format variants if ( check([0x66, 0x72, 0x65, 0x65], {offset: 4}) || // `free` check([0x6D, 0x64, 0x61, 0x74], {offset: 4}) || // `mdat` MJPEG check([0x6D, 0x6F, 0x6F, 0x76], {offset: 4}) || // `moov` check([0x77, 0x69, 0x64, 0x65], {offset: 4}) // `wide` ) { return { ext: 'mov', mime: 'video/quicktime' }; } // File Type Box (https://en.wikipedia.org/wiki/ISO_base_media_file_format) // It's not required to be first, but it's recommended to be. Almost all ISO base media files start with `ftyp` box. // `ftyp` box must contain a brand major identifier, which must consist of ISO 8859-1 printable characters. // Here we check for 8859-1 printable characters (for simplicity, it's a mask which also catches one non-printable character). if ( checkString('ftyp', {offset: 4}) && (buffer[8] & 0x60) !== 0x00 // Brand major, first character ASCII? ) { // 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 = uint8ArrayUtf8ByteString(buffer, 8, 12).replace('\0', ' ').trim(); switch (brandMajor) { case 'mif1': return {ext: 'heic', mime: 'image/heif'}; case 'msf1': return {ext: 'heic', mime: 'image/heif-sequence'}; case 'heic': case 'heix': return {ext: 'heic', mime: 'image/heic'}; case 'hevc': case 'hevx': return {ext: 'heic', mime: 'image/heic-sequence'}; case 'qt': return {ext: 'mov', mime: 'video/quicktime'}; case 'M4V': case 'M4VH': case 'M4VP': return {ext: 'm4v', mime: 'video/x-m4v'}; case 'M4P': return {ext: 'm4p', mime: 'video/mp4'}; case 'M4B': return {ext: 'm4b', mime: 'audio/mp4'}; case 'M4A': return {ext: 'm4a', mime: 'audio/x-m4a'}; case 'F4V': return {ext: 'f4v', mime: 'video/mp4'}; case 'F4P': return {ext: 'f4p', mime: 'video/mp4'}; case 'F4A': return {ext: 'f4a', mime: 'audio/mp4'}; case 'F4B': return {ext: 'f4b', mime: 'audio/mp4'}; default: if (brandMajor.startsWith('3g')) { if (brandMajor.startsWith('3g2')) { return {ext: '3g2', mime: 'video/3gpp2'}; } return {ext: '3gp', mime: 'video/3gpp'}; } return {ext: 'mp4', mime: 'video/mp4'}; } } if (check([0x4D, 0x54, 0x68, 0x64])) { return { ext: 'mid', mime: 'audio/midi' }; } // https://github.com/threatstack/libmagic/blob/master/magic/Magdir/matroska if (check([0x1A, 0x45, 0xDF, 0xA3])) { const sliced = buffer.subarray(4, 4 + 4096); const idPos = sliced.findIndex((el, i, arr) => arr[i] === 0x42 && arr[i + 1] === 0x82); if (idPos !== -1) { const docTypePos = idPos + 3; const findDocType = type => [...type].every((c, i) => sliced[docTypePos + i] === c.charCodeAt(0)); if (findDocType('matroska')) { return { ext: 'mkv', mime: 'video/x-matroska' }; } if (findDocType('webm')) { return { ext: 'webm', mime: 'video/webm' }; } } } // RIFF file format which might be AVI, WAV, QCP, etc if (check([0x52, 0x49, 0x46, 0x46])) { if (check([0x41, 0x56, 0x49], {offset: 8})) { return { ext: 'avi', mime: 'video/vnd.avi' }; } if (check([0x57, 0x41, 0x56, 0x45], {offset: 8})) { return { ext: 'wav', mime: 'audio/vnd.wave' }; } // QLCM, QCP file if (check([0x51, 0x4C, 0x43, 0x4D], {offset: 8})) { return { ext: 'qcp', mime: 'audio/qcelp' }; } } // ASF_Header_Object first 80 bytes if (check([0x30, 0x26, 0xB2, 0x75, 0x8E, 0x66, 0xCF, 0x11, 0xA6, 0xD9])) { // Search for header should be in first 1KB of file. let offset = 30; do { const objectSize = readUInt64LE(buffer, offset + 16); if (check([0x91, 0x07, 0xDC, 0xB7, 0xB7, 0xA9, 0xCF, 0x11, 0x8E, 0xE6, 0x00, 0xC0, 0x0C, 0x20, 0x53, 0x65], {offset})) { // Sync on Stream-Properties-Object (B7DC0791-A9B7-11CF-8EE6-00C00C205365) if (check([0x40, 0x9E, 0x69, 0xF8, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B], {offset: offset + 24})) { // Found audio: return { ext: 'wma', mime: 'audio/x-ms-wma' }; } if (check([0xC0, 0xEF, 0x19, 0xBC, 0x4D, 0x5B, 0xCF, 0x11, 0xA8, 0xFD, 0x00, 0x80, 0x5F, 0x5C, 0x44, 0x2B], {offset: offset + 24})) { // Found video: return { ext: 'wmv', mime: 'video/x-ms-asf' }; } break; } offset += objectSize; } while (offset + 24 <= buffer.length); // Default to ASF generic extension return { ext: 'asf', mime: 'application/vnd.ms-asf' }; } if ( check([0x0, 0x0, 0x1, 0xBA]) || check([0x0, 0x0, 0x1, 0xB3]) ) { return { ext: 'mpg', mime: 'video/mpeg' }; } // Check for MPEG header at different starting offsets for (let start = 0; start < 2 && start < (buffer.length - 16); start++) { if ( check([0x49, 0x44, 0x33], {offset: start}) || // ID3 header check([0xFF, 0xE2], {offset: start, mask: [0xFF, 0xE6]}) // MPEG 1 or 2 Layer 3 header ) { return { ext: 'mp3', mime: 'audio/mpeg' }; } if ( check([0xFF, 0xE4], {offset: start, mask: [0xFF, 0xE6]}) // MPEG 1 or 2 Layer 2 header ) { return { ext: 'mp2', mime: 'audio/mpeg' }; } if ( check([0xFF, 0xF8], {offset: start, mask: [0xFF, 0xFC]}) // MPEG 2 layer 0 using ADTS ) { return { ext: 'mp2', mime: 'audio/mpeg' }; } if ( check([0xFF, 0xF0], {offset: start, mask: [0xFF, 0xFC]}) // MPEG 4 layer 0 using ADTS ) { return { ext: 'mp4', mime: 'audio/mpeg' }; } } // Needs to be before `ogg` check if (check([0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64], {offset: 28})) { return { ext: 'opus', mime: 'audio/opus' }; } // If 'OggS' in first bytes, then OGG container if (check([0x4F, 0x67, 0x67, 0x53])) { // This is a OGG container // If ' theora' in header. if (check([0x80, 0x74, 0x68, 0x65, 0x6F, 0x72, 0x61], {offset: 28})) { return { ext: 'ogv', mime: 'video/ogg' }; } // If '\x01video' in header. if (check([0x01, 0x76, 0x69, 0x64, 0x65, 0x6F, 0x00], {offset: 28})) { return { ext: 'ogm', mime: 'video/ogg' }; } // If ' FLAC' in header https://xiph.org/flac/faq.html if (check([0x7F, 0x46, 0x4C, 0x41, 0x43], {offset: 28})) { return { ext: 'oga', mime: 'audio/ogg' }; } // 'Speex ' in header https://en.wikipedia.org/wiki/Speex if (check([0x53, 0x70, 0x65, 0x65, 0x78, 0x20, 0x20], {offset: 28})) { return { ext: 'spx', mime: 'audio/ogg' }; } // If '\x01vorbis' in header if (check([0x01, 0x76, 0x6F, 0x72, 0x62, 0x69, 0x73], {offset: 28})) { return { ext: 'ogg', mime: 'audio/ogg' }; } // Default OGG container https://www.iana.org/assignments/media-types/application/ogg return { ext: 'ogx', mime: 'application/ogg' }; } if (check([0x66, 0x4C, 0x61, 0x43])) { return { ext: 'flac', mime: 'audio/x-flac' }; } if (check([0x4D, 0x41, 0x43, 0x20])) { // 'MAC ' return { ext: 'ape', mime: 'audio/ape' }; } if (check([0x77, 0x76, 0x70, 0x6B])) { // 'wvpk' return { ext: 'wv', mime: 'audio/wavpack' }; } if (check([0x23, 0x21, 0x41, 0x4D, 0x52, 0x0A])) { return { ext: 'amr', mime: 'audio/amr' }; } if (check([0x25, 0x50, 0x44, 0x46])) { return { ext: 'pdf', mime: 'application/pdf' }; } if (check([0x4D, 0x5A])) { return { ext: 'exe', mime: 'application/x-msdownload' }; } if ( (buffer[0] === 0x43 || buffer[0] === 0x46) && check([0x57, 0x53], {offset: 1}) ) { return { ext: 'swf', mime: 'application/x-shockwave-flash' }; } if (check([0x7B, 0x5C, 0x72, 0x74, 0x66])) { return { ext: 'rtf', mime: 'application/rtf' }; } if (check([0x00, 0x61, 0x73, 0x6D])) { return { ext: 'wasm', mime: 'application/wasm' }; } if ( check([0x77, 0x4F, 0x46, 0x46]) && ( check([0x00, 0x01, 0x00, 0x00], {offset: 4}) || check([0x4F, 0x54, 0x54, 0x4F], {offset: 4}) ) ) { return { ext: 'woff', mime: 'font/woff' }; } if ( check([0x77, 0x4F, 0x46, 0x32]) && ( check([0x00, 0x01, 0x00, 0x00], {offset: 4}) || check([0x4F, 0x54, 0x54, 0x4F], {offset: 4}) ) ) { return { ext: 'woff2', mime: 'font/woff2' }; } if ( check([0x4C, 0x50], {offset: 34}) && ( check([0x00, 0x00, 0x01], {offset: 8}) || check([0x01, 0x00, 0x02], {offset: 8}) || check([0x02, 0x00, 0x02], {offset: 8}) ) ) { return { ext: 'eot', mime: 'application/vnd.ms-fontobject' }; } if (check([0x00, 0x01, 0x00, 0x00, 0x00])) { return { ext: 'ttf', mime: 'font/ttf' }; } if (check([0x4F, 0x54, 0x54, 0x4F, 0x00])) { return { ext: 'otf', mime: 'font/otf' }; } if (check([0x00, 0x00, 0x01, 0x00])) { return { ext: 'ico', mime: 'image/x-icon' }; } if (check([0x00, 0x00, 0x02, 0x00])) { return { ext: 'cur', mime: 'image/x-icon' }; } if (check([0x46, 0x4C, 0x56, 0x01])) { return { ext: 'flv', mime: 'video/x-flv' }; } if (check([0x25, 0x21])) { return { ext: 'ps', mime: 'application/postscript' }; } if (check([0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])) { return { ext: 'xz', mime: 'application/x-xz' }; } if (check([0x53, 0x51, 0x4C, 0x69])) { return { ext: 'sqlite', mime: 'application/x-sqlite3' }; } if (check([0x4E, 0x45, 0x53, 0x1A])) { return { ext: 'nes', mime: 'application/x-nintendo-nes-rom' }; } if (check([0x43, 0x72, 0x32, 0x34])) { return { ext: 'crx', mime: 'application/x-google-chrome-extension' }; } if ( check([0x4D, 0x53, 0x43, 0x46]) || check([0x49, 0x53, 0x63, 0x28]) ) { return { ext: 'cab', mime: 'application/vnd.ms-cab-compressed' }; } // Needs to be before `ar` check if (check([0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E, 0x0A, 0x64, 0x65, 0x62, 0x69, 0x61, 0x6E, 0x2D, 0x62, 0x69, 0x6E, 0x61, 0x72, 0x79])) { return { ext: 'deb', mime: 'application/x-deb' }; } if (check([0x21, 0x3C, 0x61, 0x72, 0x63, 0x68, 0x3E])) { return { ext: 'ar', mime: 'application/x-unix-archive' }; } if (check([0xED, 0xAB, 0xEE, 0xDB])) { return { ext: 'rpm', mime: 'application/x-rpm' }; } if ( check([0x1F, 0xA0]) || check([0x1F, 0x9D]) ) { return { ext: 'Z', mime: 'application/x-compress' }; } if (check([0x4C, 0x5A, 0x49, 0x50])) { return { ext: 'lz', mime: 'application/x-lzip' }; } if (check([0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E])) { return { ext: 'msi', mime: 'application/x-msi' }; } if (check([0x06, 0x0E, 0x2B, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0D, 0x01, 0x02, 0x01, 0x01, 0x02])) { return { ext: 'mxf', mime: 'application/mxf' }; } if (check([0x47], {offset: 4}) && (check([0x47], {offset: 192}) || check([0x47], {offset: 196}))) { return { ext: 'mts', mime: 'video/mp2t' }; } if (check([0x42, 0x4C, 0x45, 0x4E, 0x44, 0x45, 0x52])) { return { ext: 'blend', mime: 'application/x-blender' }; } if (check([0x42, 0x50, 0x47, 0xFB])) { return { ext: 'bpg', mime: 'image/bpg' }; } if (check([0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A])) { // JPEG-2000 family if (check([0x6A, 0x70, 0x32, 0x20], {offset: 20})) { return { ext: 'jp2', mime: 'image/jp2' }; } if (check([0x6A, 0x70, 0x78, 0x20], {offset: 20})) { return { ext: 'jpx', mime: 'image/jpx' }; } if (check([0x6A, 0x70, 0x6D, 0x20], {offset: 20})) { return { ext: 'jpm', mime: 'image/jpm' }; } if (check([0x6D, 0x6A, 0x70, 0x32], {offset: 20})) { return { ext: 'mj2', mime: 'image/mj2' }; } } if (check([0x46, 0x4F, 0x52, 0x4D])) { return { ext: 'aif', mime: 'audio/aiff' }; } if (checkString(' new Promise((resolve, reject) => { // Using `eval` to work around issues when bundling with Webpack const stream = eval('require')('stream'); // eslint-disable-line no-eval readableStream.on('error', reject); readableStream.once('readable', () => { const pass = new stream.PassThrough(); const chunk = readableStream.read(module.exports.minimumBytes) || readableStream.read(); try { pass.fileType = fileType(chunk); } catch (error) { reject(error); } readableStream.unshift(chunk); if (stream.pipeline) { resolve(stream.pipeline(readableStream, pass, () => {})); } else { resolve(readableStream.pipe(pass)); } }); }); Object.defineProperty(fileType, 'extensions', { get() { return new Set(supported.extensions); } }); Object.defineProperty(fileType, 'mimeTypes', { get() { return new Set(supported.mimeTypes); } }); supported.js000066600000007467150514465730007163 0ustar00'use strict'; module.exports = { extensions: [ 'jpg', 'png', 'apng', 'gif', 'webp', 'flif', 'cr2', 'orf', 'arw', 'dng', 'nef', 'rw2', 'raf', 'tif', 'bmp', 'jxr', 'psd', 'zip', 'tar', 'rar', 'gz', 'bz2', '7z', 'dmg', 'mp4', 'mid', 'mkv', 'webm', 'mov', 'avi', 'mpg', 'mp2', 'mp3', 'm4a', 'oga', 'ogg', 'ogv', 'opus', 'flac', 'wav', 'spx', 'amr', 'pdf', 'epub', 'exe', 'swf', 'rtf', 'wasm', 'woff', 'woff2', 'eot', 'ttf', 'otf', 'ico', 'flv', 'ps', 'xz', 'sqlite', 'nes', 'crx', 'xpi', 'cab', 'deb', 'ar', 'rpm', 'Z', 'lz', 'msi', 'mxf', 'mts', 'blend', 'bpg', 'docx', 'pptx', 'xlsx', '3gp', '3g2', 'jp2', 'jpm', 'jpx', 'mj2', 'aif', 'qcp', 'odt', 'ods', 'odp', 'xml', 'mobi', 'heic', 'cur', 'ktx', 'ape', 'wv', 'wmv', 'wma', 'dcm', 'ics', 'glb', 'pcap', 'dsf', 'lnk', 'alias', 'voc', 'ac3', 'm4v', 'm4p', 'm4b', 'f4v', 'f4p', 'f4b', 'f4a', 'mie', 'asf', 'ogm', 'ogx', 'mpc', 'arrow', 'shp' ], mimeTypes: [ 'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/flif', 'image/x-canon-cr2', 'image/tiff', 'image/bmp', 'image/vnd.ms-photo', 'image/vnd.adobe.photoshop', 'application/epub+zip', 'application/x-xpinstall', 'application/vnd.oasis.opendocument.text', 'application/vnd.oasis.opendocument.spreadsheet', 'application/vnd.oasis.opendocument.presentation', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/x-tar', 'application/x-rar-compressed', 'application/gzip', 'application/x-bzip2', 'application/x-7z-compressed', 'application/x-apple-diskimage', 'application/x-apache-arrow', 'video/mp4', 'audio/midi', 'video/x-matroska', 'video/webm', 'video/quicktime', 'video/vnd.avi', 'audio/vnd.wave', 'audio/qcelp', 'audio/x-ms-wma', 'video/x-ms-asf', 'application/vnd.ms-asf', 'video/mpeg', 'video/3gpp', 'audio/mpeg', 'audio/mp4', // RFC 4337 'audio/opus', 'video/ogg', 'audio/ogg', 'application/ogg', 'audio/x-flac', 'audio/ape', 'audio/wavpack', 'audio/amr', 'application/pdf', 'application/x-msdownload', 'application/x-shockwave-flash', 'application/rtf', 'application/wasm', 'font/woff', 'font/woff2', 'application/vnd.ms-fontobject', 'font/ttf', 'font/otf', 'image/x-icon', 'video/x-flv', 'application/postscript', 'application/x-xz', 'application/x-sqlite3', 'application/x-nintendo-nes-rom', 'application/x-google-chrome-extension', 'application/vnd.ms-cab-compressed', 'application/x-deb', 'application/x-unix-archive', 'application/x-rpm', 'application/x-compress', 'application/x-lzip', 'application/x-msi', 'application/x-mie', 'application/mxf', 'video/mp2t', 'application/x-blender', 'image/bpg', 'image/jp2', 'image/jpx', 'image/jpm', 'image/mj2', 'audio/aiff', 'application/xml', 'application/x-mobipocket-ebook', 'image/heif', 'image/heif-sequence', 'image/heic', 'image/heic-sequence', 'image/ktx', 'application/dicom', 'audio/x-musepack', 'text/calendar', 'model/gltf-binary', 'application/vnd.tcpdump.pcap', 'audio/x-dsf', // Non-standard 'application/x.ms.shortcut', // Invented by us 'application/x.apple.alias', // Invented by us 'audio/x-voc', 'audio/vnd.dolby.dd-raw', 'audio/x-m4a', 'image/apng', 'image/x-olympus-orf', 'image/x-sony-arw', 'image/x-adobe-dng', 'image/x-nikon-nef', 'image/x-panasonic-rw2', 'image/x-fujifilm-raf', 'video/x-m4v', 'video/3gpp2', 'application/x-esri-shape' ] }; readme.md000066600000030207150514465730006343 0ustar00# file-type [![Build Status](https://travis-ci.org/sindresorhus/file-type.svg?branch=master)](https://travis-ci.org/sindresorhus/file-type) > Detect the file type of a Buffer/Uint8Array/ArrayBuffer The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer. ## Install ``` $ npm install file-type ``` ## Usage ##### Node.js ```js const readChunk = require('read-chunk'); const fileType = require('file-type'); const buffer = readChunk.sync('unicorn.png', 0, fileType.minimumBytes); fileType(buffer); //=> {ext: 'png', mime: 'image/png'} ``` Or from a remote location: ```js const https = require('https'); const fileType = require('file-type'); const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg'; https.get(url, response => { response.on('readable', () => { const chunk = response.read(fileType.minimumBytes); response.destroy(); console.log(fileType(chunk)); //=> {ext: 'jpg', mime: 'image/jpeg'} }); }); ``` Or from a stream: ```js const stream = require('stream'); const fs = require('fs'); const crypto = require('crypto'); const fileType = require('file-type'); (async () => { const read = fs.createReadStream('encrypted.enc'); const decipher = crypto.createDecipheriv(alg, key, iv); const fileTypeStream = await fileType.stream(stream.pipeline(read, decipher)); console.log(fileTypeStream.fileType); //=> {ext: 'mov', mime: 'video/quicktime'} const write = fs.createWriteStream(`decrypted.${fileTypeStream.fileType.ext}`); fileTypeStream.pipe(write); })(); ``` ##### Browser ```js const xhr = new XMLHttpRequest(); xhr.open('GET', 'unicorn.png'); xhr.responseType = 'arraybuffer'; xhr.onload = () => { fileType(new Uint8Array(this.response)); //=> {ext: 'png', mime: 'image/png'} }; xhr.send(); ``` ## API ### fileType(input) Returns an `object` with: - `ext` - One of the [supported file types](#supported-file-types) - `mime` - The [MIME type](https://en.wikipedia.org/wiki/Internet_media_type) Or `undefined` when there is no match. #### input Type: `Buffer | Uint8Array | ArrayBuffer` It only needs the first `.minimumBytes` bytes. The exception is detection of `docx`, `pptx`, and `xlsx` which potentially requires reading the whole file. ### fileType.minimumBytes Type: `number` The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hardcode it. ### fileType.stream(readableStream) Detect the file type of a readable stream. Returns a `Promise` which resolves to the original readable stream argument, but with an added `fileType` property, which is an object like the one returned from `fileType()`. *Note:* This method is only for Node.js. #### readableStream Type: [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable) ### fileType.extensions Returns a set of supported file extensions. ### fileType.mimeTypes Returns a set of supported MIME types. ## Supported file types - [`jpg`](https://en.wikipedia.org/wiki/JPEG) - [`png`](https://en.wikipedia.org/wiki/Portable_Network_Graphics) - [`apng`](https://en.wikipedia.org/wiki/APNG) - Animated Portable Network Graphics - [`gif`](https://en.wikipedia.org/wiki/GIF) - [`webp`](https://en.wikipedia.org/wiki/WebP) - [`flif`](https://en.wikipedia.org/wiki/Free_Lossless_Image_Format) - [`cr2`](https://fileinfo.com/extension/cr2) - Canon Raw image file (v2) - [`orf`](https://en.wikipedia.org/wiki/ORF_format) - Olympus Raw image file - [`arw`](https://en.wikipedia.org/wiki/Raw_image_format#ARW) - Sony Alpha Raw image file - [`dng`](https://en.wikipedia.org/wiki/Digital_Negative) - Adobe Digital Negative image file - [`nef`](https://www.nikonusa.com/en/learn-and-explore/a/products-and-innovation/nikon-electronic-format-nef.html) - Nikon Electronic Format image file - [`rw2`](https://en.wikipedia.org/wiki/Raw_image_format) - Panasonic RAW image file - [`raf`](https://en.wikipedia.org/wiki/Raw_image_format) - Fujifilm RAW image file - [`tif`](https://en.wikipedia.org/wiki/Tagged_Image_File_Format) - [`bmp`](https://en.wikipedia.org/wiki/BMP_file_format) - [`jxr`](https://en.wikipedia.org/wiki/JPEG_XR) - [`psd`](https://en.wikipedia.org/wiki/Adobe_Photoshop#File_format) - [`zip`](https://en.wikipedia.org/wiki/Zip_(file_format)) - [`tar`](https://en.wikipedia.org/wiki/Tar_(computing)#File_format) - [`rar`](https://en.wikipedia.org/wiki/RAR_(file_format)) - [`gz`](https://en.wikipedia.org/wiki/Gzip) - [`bz2`](https://en.wikipedia.org/wiki/Bzip2) - [`7z`](https://en.wikipedia.org/wiki/7z) - [`dmg`](https://en.wikipedia.org/wiki/Apple_Disk_Image) - [`mp4`](https://en.wikipedia.org/wiki/MPEG-4_Part_14#Filename_extensions) - [`mid`](https://en.wikipedia.org/wiki/MIDI) - [`mkv`](https://en.wikipedia.org/wiki/Matroska) - [`webm`](https://en.wikipedia.org/wiki/WebM) - [`mov`](https://en.wikipedia.org/wiki/QuickTime_File_Format) - [`avi`](https://en.wikipedia.org/wiki/Audio_Video_Interleave) - [`mpg`](https://en.wikipedia.org/wiki/MPEG-1) - [`mp2`](https://en.wikipedia.org/wiki/MPEG-1_Audio_Layer_II) - [`mp3`](https://en.wikipedia.org/wiki/MP3) - [`ogg`](https://en.wikipedia.org/wiki/Ogg) - [`ogv`](https://en.wikipedia.org/wiki/Ogg) - [`ogm`](https://en.wikipedia.org/wiki/Ogg) - [`oga`](https://en.wikipedia.org/wiki/Ogg) - [`spx`](https://en.wikipedia.org/wiki/Ogg) - [`ogx`](https://en.wikipedia.org/wiki/Ogg) - [`opus`](https://en.wikipedia.org/wiki/Opus_(audio_format)) - [`flac`](https://en.wikipedia.org/wiki/FLAC) - [`wav`](https://en.wikipedia.org/wiki/WAV) - [`qcp`](https://en.wikipedia.org/wiki/QCP) - [`amr`](https://en.wikipedia.org/wiki/Adaptive_Multi-Rate_audio_codec) - [`pdf`](https://en.wikipedia.org/wiki/Portable_Document_Format) - [`epub`](https://en.wikipedia.org/wiki/EPUB) - [`mobi`](https://en.wikipedia.org/wiki/Mobipocket) - Mobipocket - [`exe`](https://en.wikipedia.org/wiki/.exe) - [`swf`](https://en.wikipedia.org/wiki/SWF) - [`rtf`](https://en.wikipedia.org/wiki/Rich_Text_Format) - [`woff`](https://en.wikipedia.org/wiki/Web_Open_Font_Format) - [`woff2`](https://en.wikipedia.org/wiki/Web_Open_Font_Format) - [`eot`](https://en.wikipedia.org/wiki/Embedded_OpenType) - [`ttf`](https://en.wikipedia.org/wiki/TrueType) - [`otf`](https://en.wikipedia.org/wiki/OpenType) - [`ico`](https://en.wikipedia.org/wiki/ICO_(file_format)) - [`flv`](https://en.wikipedia.org/wiki/Flash_Video) - [`ps`](https://en.wikipedia.org/wiki/Postscript) - [`xz`](https://en.wikipedia.org/wiki/Xz) - [`sqlite`](https://www.sqlite.org/fileformat2.html) - [`nes`](https://fileinfo.com/extension/nes) - [`crx`](https://developer.chrome.com/extensions/crx) - [`xpi`](https://en.wikipedia.org/wiki/XPInstall) - [`cab`](https://en.wikipedia.org/wiki/Cabinet_(file_format)) - [`deb`](https://en.wikipedia.org/wiki/Deb_(file_format)) - [`ar`](https://en.wikipedia.org/wiki/Ar_(Unix)) - [`rpm`](https://fileinfo.com/extension/rpm) - [`Z`](https://fileinfo.com/extension/z) - [`lz`](https://en.wikipedia.org/wiki/Lzip) - [`msi`](https://en.wikipedia.org/wiki/Windows_Installer) - [`mxf`](https://en.wikipedia.org/wiki/Material_Exchange_Format) - [`mts`](https://en.wikipedia.org/wiki/.m2ts) - [`wasm`](https://en.wikipedia.org/wiki/WebAssembly) - [`blend`](https://wiki.blender.org/index.php/Dev:Source/Architecture/File_Format) - [`bpg`](https://bellard.org/bpg/) - [`docx`](https://en.wikipedia.org/wiki/Office_Open_XML) - [`pptx`](https://en.wikipedia.org/wiki/Office_Open_XML) - [`xlsx`](https://en.wikipedia.org/wiki/Office_Open_XML) - [`jp2`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000 - [`jpm`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000 - [`jpx`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000 - [`mj2`](https://en.wikipedia.org/wiki/Motion_JPEG_2000) - Motion JPEG 2000 - [`aif`](https://en.wikipedia.org/wiki/Audio_Interchange_File_Format) - [`odt`](https://en.wikipedia.org/wiki/OpenDocument) - OpenDocument for word processing - [`ods`](https://en.wikipedia.org/wiki/OpenDocument) - OpenDocument for spreadsheets - [`odp`](https://en.wikipedia.org/wiki/OpenDocument) - OpenDocument for presentations - [`xml`](https://en.wikipedia.org/wiki/XML) - [`heic`](https://nokiatech.github.io/heif/technical.html) - [`cur`](https://en.wikipedia.org/wiki/ICO_(file_format)) - [`ktx`](https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/) - [`ape`](https://en.wikipedia.org/wiki/Monkey%27s_Audio) - Monkey's Audio - [`wv`](https://en.wikipedia.org/wiki/WavPack) - WavPack - [`asf`](https://en.wikipedia.org/wiki/Advanced_Systems_Format) - Advanced Systems Format - [`wma`](https://en.wikipedia.org/wiki/Windows_Media_Audio) - Windows Media Audio - [`wmv`](https://en.wikipedia.org/wiki/Windows_Media_Video) - Windows Media Video - [`dcm`](https://en.wikipedia.org/wiki/DICOM#Data_format) - DICOM Image File - [`mpc`](https://en.wikipedia.org/wiki/Musepack) - Musepack (SV7 & SV8) - [`ics`](https://en.wikipedia.org/wiki/ICalendar#Data_format) - iCalendar - [`glb`](https://github.com/KhronosGroup/glTF) - GL Transmission Format - [`pcap`](https://wiki.wireshark.org/Development/LibpcapFileFormat) - Libpcap File Format - [`dsf`](https://dsd-guide.com/sites/default/files/white-papers/DSFFileFormatSpec_E.pdf) - Sony DSD Stream File (DSF) - [`lnk`](https://en.wikipedia.org/wiki/Shortcut_%28computing%29#Microsoft_Windows) - Microsoft Windows file shortcut - [`alias`](https://en.wikipedia.org/wiki/Alias_%28Mac_OS%29) - macOS Alias file - [`voc`](https://wiki.multimedia.cx/index.php/Creative_Voice) - Creative Voice File - [`ac3`](https://www.atsc.org/standard/a522012-digital-audio-compression-ac-3-e-ac-3-standard-12172012/) - ATSC A/52 Audio File - [`3gp`](https://en.wikipedia.org/wiki/3GP_and_3G2#3GP) - Multimedia container format defined by the Third Generation Partnership Project (3GPP) for 3G UMTS multimedia services - [`3g2`](https://en.wikipedia.org/wiki/3GP_and_3G2#3G2) - Multimedia container format defined by the 3GPP2 for 3G CDMA2000 multimedia services - [`m4v`](https://en.wikipedia.org/wiki/M4V) - MPEG-4 Visual bitstreams - [`m4p`](https://en.wikipedia.org/wiki/MPEG-4_Part_14#Filename_extensions) - MPEG-4 files with audio streams encrypted by FairPlay Digital Rights Management as were sold through the iTunes Store - [`m4a`](https://en.wikipedia.org/wiki/M4A) - Audio-only MPEG-4 files - [`m4b`](https://en.wikipedia.org/wiki/M4B) - Audiobook and podcast MPEG-4 files, which also contain metadata including chapter markers, images, and hyperlinks - [`f4v`](https://en.wikipedia.org/wiki/Flash_Video) - ISO base media file format used by Adobe Flash Player - [`f4p`](https://en.wikipedia.org/wiki/Flash_Video) - ISO base media file format protected by Adobe Access DRM used by Adobe Flash Player - [`f4a`](https://en.wikipedia.org/wiki/Flash_Video) - Audio-only ISO base media file format used by Adobe Flash Player - [`f4b`](https://en.wikipedia.org/wiki/Flash_Video) - Audiobook and podcast ISO base media file format used by Adobe Flash Player - [`mie`](https://en.wikipedia.org/wiki/Sidecar_file) - Dedicated meta information format which supports storage of binary as well as textual meta information - [`shp`](https://en.wikipedia.org/wiki/Shapefile) - Geospatial vector data format - [`arrow`](https://arrow.apache.org) - Columnar format for tables of data *SVG isn't included as it requires the whole file to be read, but you can get it [here](https://github.com/sindresorhus/is-svg).* *Pull requests are welcome for additional commonly used file types, except for `doc`, `xls`, `ppt`.* ## file-type for enterprise Available as part of the Tidelift Subscription. The maintainers of file-type and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-file-type?utm_source=npm-file-type&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) ## Related - [file-type-cli](https://github.com/sindresorhus/file-type-cli) - CLI for this module ## Maintainers - [Sindre Sorhus](https://github.com/sindresorhus) - [Mikael Finstad](https://github.com/mifi) - [Ben Brook](https://github.com/bencmbrook) util.js000066600000004344150514465730006102 0ustar00'use strict'; exports.stringToBytes = string => [...string].map(character => character.charCodeAt(0)); const uint8ArrayUtf8ByteString = (array, start, end) => { return String.fromCharCode(...array.slice(start, end)); }; exports.readUInt64LE = (buffer, offset = 0) => { let n = buffer[offset]; let mul = 1; let i = 0; while (++i < 8) { mul *= 0x100; n += buffer[offset + i] * mul; } return n; }; exports.tarHeaderChecksumMatches = buffer => { // Does not check if checksum field characters are valid if (buffer.length < 512) { // `tar` header size, cannot compute checksum without it return false; } const MASK_8TH_BIT = 0x80; let sum = 256; // Intitalize sum, with 256 as sum of 8 spaces in checksum field let signedBitSum = 0; // Initialize signed bit sum for (let i = 0; i < 148; i++) { const byte = buffer[i]; sum += byte; signedBitSum += byte & MASK_8TH_BIT; // Add signed bit to signed bit sum } // Skip checksum field for (let i = 156; i < 512; i++) { const byte = buffer[i]; sum += byte; signedBitSum += byte & MASK_8TH_BIT; // Add signed bit to signed bit sum } const readSum = parseInt(uint8ArrayUtf8ByteString(buffer, 148, 154), 8); // Read sum in header // Some implementations compute checksum incorrectly using signed bytes return ( // Checksum in header equals the sum we calculated readSum === sum || // Checksum in header equals sum we calculated plus signed-to-unsigned delta readSum === (sum - (signedBitSum << 1)) ); }; exports.multiByteIndexOf = (buffer, bytesToSearch, startAt = 0) => { // `Buffer#indexOf()` can search for multiple bytes if (Buffer && Buffer.isBuffer(buffer)) { return buffer.indexOf(Buffer.from(bytesToSearch), startAt); } const nextBytesMatch = (buffer, bytes, startIndex) => { for (let i = 1; i < bytes.length; i++) { if (bytes[i] !== buffer[startIndex + i]) { return false; } } return true; }; // `Uint8Array#indexOf()` can search for only a single byte let index = buffer.indexOf(bytesToSearch[0], startAt); while (index >= 0) { if (nextBytesMatch(buffer, bytesToSearch, index)) { return index; } index = buffer.indexOf(bytesToSearch[0], index + 1); } return -1; }; exports.uint8ArrayUtf8ByteString = uint8ArrayUtf8ByteString; license000066600000002125150514465730006127 0ustar00MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. index.d.ts000066600000015752150514465730006475 0ustar00/// import {Readable as ReadableStream} from 'stream'; declare namespace fileType { type FileType = | 'jpg' | 'png' | 'apng' | 'gif' | 'webp' | 'flif' | 'cr2' | 'orf' | 'arw' | 'dng' | 'nef' | 'rw2' | 'raf' | 'tif' | 'bmp' | 'jxr' | 'psd' | 'zip' | 'tar' | 'rar' | 'gz' | 'bz2' | '7z' | 'dmg' | 'mp4' | 'mid' | 'mkv' | 'webm' | 'mov' | 'avi' | 'wmv' | 'mpg' | 'mp2' | 'mp3' | 'm4a' | 'ogg' | 'opus' | 'flac' | 'wav' | 'qcp' | 'amr' | 'pdf' | 'epub' | 'mobi' | 'exe' | 'swf' | 'rtf' | 'woff' | 'woff2' | 'eot' | 'ttf' | 'otf' | 'ico' | 'flv' | 'ps' | 'xz' | 'sqlite' | 'nes' | 'crx' | 'xpi' | 'cab' | 'deb' | 'ar' | 'rpm' | 'Z' | 'lz' | 'msi' | 'mxf' | 'mts' | 'wasm' | 'blend' | 'bpg' | 'docx' | 'pptx' | 'xlsx' | '3gp' | '3g2' | 'jp2' | 'jpm' | 'jpx' | 'mj2' | 'aif' | 'odt' | 'ods' | 'odp' | 'xml' | 'heic' | 'cur' | 'ktx' | 'ape' | 'wv' | 'asf' | 'wma' | 'dcm' | 'mpc' | 'ics' | 'glb' | 'pcap' | 'dsf' | 'lnk' | 'alias' | 'voc' | 'ac3' | 'm4b' | 'm4p' | 'm4v' | 'f4a' | 'f4b' | 'f4p' | 'f4v' | 'mie' | 'ogv' | 'ogm' | 'oga' | 'spx' | 'ogx' | 'arrow' | 'shp'; type MimeType = | 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp' | 'image/flif' | 'image/x-canon-cr2' | 'image/tiff' | 'image/bmp' | 'image/vnd.ms-photo' | 'image/vnd.adobe.photoshop' | 'application/epub+zip' | 'application/x-xpinstall' | 'application/vnd.oasis.opendocument.text' | 'application/vnd.oasis.opendocument.spreadsheet' | 'application/vnd.oasis.opendocument.presentation' | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' | 'application/vnd.openxmlformats-officedocument.presentationml.presentation' | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' | 'application/zip' | 'application/x-tar' | 'application/x-rar-compressed' | 'application/gzip' | 'application/x-bzip2' | 'application/x-7z-compressed' | 'application/x-apple-diskimage' | 'video/mp4' | 'audio/midi' | 'video/x-matroska' | 'video/webm' | 'video/quicktime' | 'video/vnd.avi' | 'audio/vnd.wave' | 'audio/qcelp' | 'audio/x-ms-wma' | 'video/x-ms-asf' | 'application/vnd.ms-asf' | 'video/mpeg' | 'video/3gpp' | 'audio/mpeg' | 'audio/mp4' // RFC 4337 | 'audio/opus' | 'video/ogg' | 'audio/ogg' | 'application/ogg' | 'audio/x-flac' | 'audio/ape' | 'audio/wavpack' | 'audio/amr' | 'application/pdf' | 'application/x-msdownload' | 'application/x-shockwave-flash' | 'application/rtf' | 'application/wasm' | 'font/woff' | 'font/woff2' | 'application/vnd.ms-fontobject' | 'font/ttf' | 'font/otf' | 'image/x-icon' | 'video/x-flv' | 'application/postscript' | 'application/x-xz' | 'application/x-sqlite3' | 'application/x-nintendo-nes-rom' | 'application/x-google-chrome-extension' | 'application/vnd.ms-cab-compressed' | 'application/x-deb' | 'application/x-unix-archive' | 'application/x-rpm' | 'application/x-compress' | 'application/x-lzip' | 'application/x-msi' | 'application/x-mie' | 'application/x-apache-arrow' | 'application/mxf' | 'video/mp2t' | 'application/x-blender' | 'image/bpg' | 'image/jp2' | 'image/jpx' | 'image/jpm' | 'image/mj2' | 'audio/aiff' | 'application/xml' | 'application/x-mobipocket-ebook' | 'image/heif' | 'image/heif-sequence' | 'image/heic' | 'image/heic-sequence' | 'image/ktx' | 'application/dicom' | 'audio/x-musepack' | 'text/calendar' | 'model/gltf-binary' | 'application/vnd.tcpdump.pcap' | 'audio/x-dsf' // Non-standard | 'application/x.ms.shortcut' // Invented by us | 'application/x.apple.alias' // Invented by us | 'audio/x-voc' | 'audio/vnd.dolby.dd-raw' | 'audio/x-m4a' | 'image/apng' | 'image/x-olympus-orf' | 'image/x-sony-arw' | 'image/x-adobe-dng' | 'image/x-nikon-nef' | 'image/x-panasonic-rw2' | 'image/x-fujifilm-raf' | 'video/x-m4v' | 'video/3gpp2' | 'application/x-esri-shape'; interface FileTypeResult { /** One of the supported [file types](https://github.com/sindresorhus/file-type#supported-file-types). */ ext: FileType; /** The detected [MIME type](https://en.wikipedia.org/wiki/Internet_media_type). */ mime: MimeType; } type ReadableStreamWithFileType = ReadableStream & { readonly fileType?: FileTypeResult; }; } declare const fileType: { /** Detect the file type of a `Buffer`/`Uint8Array`/`ArrayBuffer`. The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer. @param buffer - It only needs the first `.minimumBytes` bytes. The exception is detection of `docx`, `pptx`, and `xlsx` which potentially requires reading the whole file. @returns The detected file type and MIME type or `undefined` when there was no match. @example ``` import readChunk = require('read-chunk'); import fileType = require('file-type'); const buffer = readChunk.sync('unicorn.png', 0, fileType.minimumBytes); fileType(buffer); //=> {ext: 'png', mime: 'image/png'} // Or from a remote location: import * as http from 'http'; const url = 'https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif'; http.get(url, response => { response.on('readable', () => { const chunk = response.read(fileType.minimumBytes); response.destroy(); console.log(fileType(chunk)); //=> {ext: 'gif', mime: 'image/gif'} }); }); ``` */ (buffer: Buffer | Uint8Array | ArrayBuffer): fileType.FileTypeResult | undefined; /** The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hard-code it. */ readonly minimumBytes: number; /** Supported file extensions. */ readonly extensions: readonly fileType.FileType[]; /** Supported MIME types. */ readonly mimeTypes: readonly fileType.MimeType[]; /** Detect the file type of a readable stream. @param readableStream - A readable stream containing a file to examine, see: [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable). @returns A `Promise` which resolves to the original readable stream argument, but with an added `fileType` property, which is an object like the one returned from `fileType()`. @example ``` import * as fs from 'fs'; import * as crypto from 'crypto'; import fileType = require('file-type'); (async () => { const read = fs.createReadStream('encrypted.enc'); const decipher = crypto.createDecipheriv(alg, key, iv); const stream = await fileType.stream(read.pipe(decipher)); console.log(stream.fileType); //=> {ext: 'mov', mime: 'video/quicktime'} const write = fs.createWriteStream(`decrypted.${stream.fileType.ext}`); stream.pipe(write); })(); ``` */ readonly stream: ( readableStream: ReadableStream ) => Promise; }; export = fileType; package.json000066600000003630150514465730007052 0ustar00{ "name": "file-type", "version": "12.4.2", "description": "Detect the file type of a Buffer/Uint8Array/ArrayBuffer", "license": "MIT", "repository": "sindresorhus/file-type", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=8" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts", "supported.js", "util.js" ], "keywords": [ "mime", "file", "type", "archive", "image", "img", "pic", "picture", "flash", "photo", "video", "detect", "check", "is", "exif", "exe", "binary", "buffer", "uint8array", "jpg", "png", "apng", "gif", "webp", "flif", "cr2", "orf", "arw", "dng", "nef", "rw2", "raf", "tif", "bmp", "jxr", "psd", "zip", "tar", "rar", "gz", "bz2", "7z", "dmg", "mp4", "mid", "mkv", "webm", "mov", "avi", "mpg", "mp2", "mp3", "m4a", "ogg", "opus", "flac", "wav", "amr", "pdf", "epub", "mobi", "swf", "rtf", "woff", "woff2", "eot", "ttf", "otf", "ico", "flv", "ps", "xz", "sqlite", "xpi", "cab", "deb", "ar", "rpm", "Z", "lz", "msi", "mxf", "mts", "wasm", "webassembly", "blend", "bpg", "docx", "pptx", "xlsx", "3gp", "jp2", "jpm", "jpx", "mj2", "aif", "odt", "ods", "odp", "xml", "heic", "wma", "ics", "glb", "pcap", "dsf", "lnk", "alias", "voc", "ac3", "3g2", "m4b", "m4p", "m4v", "f4a", "f4b", "f4p", "f4v", "mie", "qcp", "wmv", "asf", "ogv", "ogm", "oga", "spx", "ogx", "ape", "wv", "cur", "nes", "crx", "ktx", "dcm", "mpc", "arrow", "shp" ], "devDependencies": { "@types/node": "^12.7.2", "ava": "^2.3.0", "noop-stream": "^0.1.0", "pify": "^4.0.1", "read-chunk": "^3.2.0", "tsd": "^0.7.1", "xo": "^0.24.0" } }