mirror of
https://github.com/codedread/bitjs
synced 2025-10-03 17:49:16 +02:00
Tweak the file signature sniffer so byte tree initialization happens lazily.
This commit is contained in:
parent
96ba25aef7
commit
7746b8c03f
3 changed files with 57 additions and 32 deletions
|
@ -11,6 +11,12 @@
|
|||
|
||||
import { findMimeType } from '../file/sniffer.js';
|
||||
|
||||
/**
|
||||
* @typedef UnarchivedFile
|
||||
* @property {string} filename
|
||||
* @property {Uint8Array} fileData
|
||||
*/
|
||||
|
||||
/**
|
||||
* The UnarchiveEvent types.
|
||||
*/
|
||||
|
|
|
@ -33,10 +33,10 @@ export {
|
|||
*/
|
||||
|
||||
/**
|
||||
* @typedef UnarchivedFile
|
||||
* @property {string} filename
|
||||
* @property {Uint8Array} fileData
|
||||
*/
|
||||
* @typedef UnarchivedFile
|
||||
* @property {string} filename
|
||||
* @property {Uint8Array} fileData
|
||||
*/
|
||||
|
||||
/**
|
||||
* The goal is to make this testable - send getUnarchiver() an array buffer of
|
||||
|
|
|
@ -38,6 +38,10 @@ const fileSignatures = {
|
|||
// * an OGG container can be resolved to OGG Audio, OGG Video
|
||||
// * 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 {
|
||||
/** @param {number} value */
|
||||
constructor(value) {
|
||||
|
@ -47,12 +51,20 @@ class Node {
|
|||
}
|
||||
}
|
||||
|
||||
// Top-level node in the tree.
|
||||
const root = new Node();
|
||||
/** Top-level node in the byte tree. */
|
||||
let root = null;
|
||||
/** The maximum depth of the byte tree. */
|
||||
let maxDepth = 0;
|
||||
|
||||
// Construct the tree, erroring if overlapping mime types are possible.
|
||||
for (const mimeType in fileSignatures) {
|
||||
/**
|
||||
* 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.
|
||||
for (const mimeType in fileSignatures) {
|
||||
for (const signature of fileSignatures[mimeType]) {
|
||||
let curNode = root;
|
||||
let depth = 0;
|
||||
|
@ -80,16 +92,23 @@ for (const mimeType in fileSignatures) {
|
|||
}
|
||||
curNode.mimeType = mimeType;
|
||||
} // for each signature
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the likely MIME type represented by the ArrayBuffer.
|
||||
* @param {ArrayBuffer} ab
|
||||
* @return {string} The MIME type of the buffer, or undefined.
|
||||
*/
|
||||
export function findMimeType(ab) {
|
||||
if (!root) {
|
||||
initializeTree();
|
||||
}
|
||||
|
||||
const depth = ab.byteLength < maxDepth ? ab.byteLength : maxDepth;
|
||||
const arr = new Uint8Array(ab).subarray(0, depth);
|
||||
let curNode = root;
|
||||
// Step through bytes, updating curNode as it walks down the byte tree.
|
||||
for (const byte of arr) {
|
||||
// If this node has a placeholder child, just step into it.
|
||||
if (curNode.children['??']) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue