A file signature is more informative than an extension when identifying a candidate format. A matching prefix alone does not prove the whole file is valid.
Worked example
import { Buffer } from "node:buffer";
const signature = Buffer.from("89504e470d0a1a0a", "hex");
console.log(
signature.subarray(1, 4).toString("ascii") === "PNG" && signature[0] === 0x89,
);Result
trueImplementation notes
Several image formats share common-looking bytes later in the file. Check the correct offset and the complete signature required by the format.
Try it yourself
Test a short buffer and a valid prefix followed by unrelated content.
Open the Node.js example →Editorial date February 11, 2026 · Published December 6, 2026