file-type buffer detection: working versions (tested 2026-10-16)
Detect PNG, PDF, ZIP and GIF from small in-memory magic byte buffers with file-type, asserting ext and mime, and undefined for plain text.
passed5 passed · 0 failed2026-10-16 19:54 UTC- Status
- passed
- Last passed
- 2026-10-16 19:54 UTC
- Tests
- 5 / 5
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
file-type@22.2.0@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38958328921
The code
Exactly the files that passed. Your agent can pull them with get_recipe("file-type-buffer-detect") (install), or see them on GitHub.
package.json
{
"name": "file-type-buffer-detect",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"file-type": "22.2.0"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/detect.ts
import { fileTypeFromBuffer } from "file-type";
export type Detected = {
ext: string;
mime: string;
};
export async function detect(bytes: Uint8Array): Promise<Detected | undefined> {
const result = await fileTypeFromBuffer(bytes);
if (!result) {
return undefined;
}
return { ext: result.ext, mime: result.mime };
}
test/detect.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { detect } from "../src/detect.ts";
const bytes = (values: number[]): Uint8Array => Uint8Array.from(values);
const PNG = bytes([
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
]);
const PDF = bytes([
0x25, 0x50, 0x44, 0x46, 0x2d, 0x31, 0x2e, 0x37,
0x0a, 0x25, 0xe2, 0xe3, 0xcf, 0xd3, 0x0a, 0x0a,
]);
const ZIP = bytes([
0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]);
const GIF = bytes([
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x64, 0x00,
0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]);
test("detects png from magic bytes", async () => {
assert.deepEqual(await detect(PNG), { ext: "png", mime: "image/png" });
});
test("detects pdf from magic bytes", async () => {
assert.deepEqual(await detect(PDF), { ext: "pdf", mime: "application/pdf" });
});
test("detects zip from magic bytes", async () => {
assert.deepEqual(await detect(ZIP), { ext: "zip", mime: "application/zip" });
});
test("detects gif from magic bytes", async () => {
assert.deepEqual(await detect(GIF), { ext: "gif", mime: "image/gif" });
});
test("returns undefined for plain text", async () => {
const text = new TextEncoder().encode("just some plain text, not a binary format");
assert.equal(await detect(text), undefined);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.