Dev Helpers

Node.js / File signatures

Recognize a PNG header

Dev Helpers · February 11, 2026

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

true

Implementation 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

Related articles

Safely parse malformed input

Parsing user-provided JSON needs a failure path. Successful parsing alone does not establish that the result has the expected shape.

Checksum bytes with CRC32

CRC32 is useful for detecting accidental changes to a byte sequence. Record the input encoding so different producers compute the same checksum.

Attach details to a mark

Performance marks can carry details about the operation they describe. Small stable identifiers are easier to aggregate than large payloads.