1
0
Fork 0
mirror of https://github.com/codedread/bitjs synced 2025-10-04 10:09:16 +02:00

Tweak the file signature sniffer so byte tree initialization happens lazily.

This commit is contained in:
Jeff Schiller 2022-01-14 12:17:03 -08:00
parent 96ba25aef7
commit 7746b8c03f
3 changed files with 57 additions and 32 deletions

View file

@ -11,6 +11,12 @@
import { findMimeType } from '../file/sniffer.js'; import { findMimeType } from '../file/sniffer.js';
/**
* @typedef UnarchivedFile
* @property {string} filename
* @property {Uint8Array} fileData
*/
/** /**
* The UnarchiveEvent types. * The UnarchiveEvent types.
*/ */

View file

@ -38,6 +38,10 @@ const fileSignatures = {
// * an OGG container can be resolved to OGG Audio, OGG Video // * an OGG container can be resolved to OGG Audio, OGG Video
// * an HEIF container can be resolved to AVIF, HEIC // * an HEIF container can be resolved to AVIF, HEIC
/**
* Represents a single byte in the tree. If this node terminates a known MIME type (see magic
* numbers above), then the mimeType field will be set.
*/
class Node { class Node {
/** @param {number} value */ /** @param {number} value */
constructor(value) { constructor(value) {
@ -47,10 +51,18 @@ class Node {
} }
} }
// Top-level node in the tree. /** Top-level node in the byte tree. */
const root = new Node(); let root = null;
/** The maximum depth of the byte tree. */
let maxDepth = 0; let maxDepth = 0;
/**
* This function initializes the byte tree. It is lazily called upon findMimeType(), but if you care
* about when the tree initializes (like in startup, etc), you can call it yourself here.
*/
export function initialize() {
root = new Node();
// Construct the tree, erroring if overlapping mime types are possible. // Construct the tree, erroring if overlapping mime types are possible.
for (const mimeType in fileSignatures) { for (const mimeType in fileSignatures) {
for (const signature of fileSignatures[mimeType]) { for (const signature of fileSignatures[mimeType]) {
@ -81,15 +93,22 @@ for (const mimeType in fileSignatures) {
curNode.mimeType = mimeType; curNode.mimeType = mimeType;
} // for each signature } // for each signature
} }
}
/** /**
* Finds the likely MIME type represented by the ArrayBuffer.
* @param {ArrayBuffer} ab * @param {ArrayBuffer} ab
* @return {string} The MIME type of the buffer, or undefined. * @return {string} The MIME type of the buffer, or undefined.
*/ */
export function findMimeType(ab) { export function findMimeType(ab) {
if (!root) {
initializeTree();
}
const depth = ab.byteLength < maxDepth ? ab.byteLength : maxDepth; const depth = ab.byteLength < maxDepth ? ab.byteLength : maxDepth;
const arr = new Uint8Array(ab).subarray(0, depth); const arr = new Uint8Array(ab).subarray(0, depth);
let curNode = root; let curNode = root;
// Step through bytes, updating curNode as it walks down the byte tree.
for (const byte of arr) { for (const byte of arr) {
// If this node has a placeholder child, just step into it. // If this node has a placeholder child, just step into it.
if (curNode.children['??']) { if (curNode.children['??']) {