Initial Commit
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Magic-byte signature registry.
|
||||
*
|
||||
* Signatures are evaluated in descending priority order; the first match wins.
|
||||
* Priority 105 = disambiguation required (container formats with multiple brands).
|
||||
* Priority 100 = default, unambiguous magic bytes.
|
||||
* Priority 90 = slight ambiguity (e.g. WebM / MKV share EBML).
|
||||
* Priority 80 = deep probe (signature requires many bytes, e.g. TAR at offset 257).
|
||||
*
|
||||
* NOTE: registry.ts must NOT import signatures/index.ts directly — that would
|
||||
* create a circular dependency (signatures/index.ts imports registerSignature
|
||||
* from registry.ts). The side-effect import lives in from-magic-bytes.ts instead.
|
||||
*
|
||||
* @module detection/from-magic-bytes/registry
|
||||
*/
|
||||
|
||||
/** A compiled magic-byte signature entry. */
|
||||
export interface Signature {
|
||||
readonly mime: string;
|
||||
readonly priority: number;
|
||||
readonly minBytes: number;
|
||||
readonly match: (bytes: Uint8Array) => boolean;
|
||||
}
|
||||
|
||||
const SIGS: Signature[] = [];
|
||||
let sortedCache: readonly Signature[] | null = null;
|
||||
|
||||
/** Register a compiled signature. Clears the sorted cache. */
|
||||
export function registerSignature(sig: Signature): void {
|
||||
SIGS.push(sig);
|
||||
sortedCache = null;
|
||||
}
|
||||
|
||||
/** Returns all registered signatures sorted by descending priority. */
|
||||
export function getSignatures(): readonly Signature[] {
|
||||
if (sortedCache === null) {
|
||||
sortedCache = [...SIGS].sort((a, b) => b.priority - a.priority);
|
||||
}
|
||||
return sortedCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset all registered signatures. Intended for tests only.
|
||||
* @internal
|
||||
*/
|
||||
export function resetSignatures(): void {
|
||||
SIGS.length = 0;
|
||||
sortedCache = null;
|
||||
}
|
||||
Reference in New Issue
Block a user