Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions types/probe-image-size/lib/parse_sync/bmp.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference types="node" />

declare function parseBmp(data: Buffer):
| {
width: number;
height: number;
type: "bmp";
mime: "image/bmp";
wUnits: "px";
hUnits: "px";
}
| undefined;

export = parseBmp;
15 changes: 15 additions & 0 deletions types/probe-image-size/lib/parse_sync/ico.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// <reference types="node" />

declare function parseIco(data: Buffer):
| {
width: number;
height: number;
variants: Array<{ width: number; height: number }>;
type: "ico";
mime: "image/x-icon";
wUnits: "px";
hUnits: "px";
}
| undefined;

export = parseIco;
14 changes: 14 additions & 0 deletions types/probe-image-size/lib/parse_sync/png.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference types="node" />

declare function parsePng(data: Buffer):
| {
width: number;
height: number;
type: "png";
mime: "image/png";
wUnits: "px";
hUnits: "px";
}
| undefined;

export = parsePng;
67 changes: 67 additions & 0 deletions types/probe-image-size/probe-image-size-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,70 @@ const data = fs.readFileSync("image.jpg");
new probe.Error("Error", "ECONTENT"); // $ExpectType ProbeError
new probe.Error("Error", "ECONTENT", 404); // $ExpectType ProbeError
})();

// Tests for deep imports.

// -- /lib/parse_sync/bmp.js

import parseBmp from "probe-image-size/lib/parse_sync/bmp.js";

/** The smallest BMP that will pass `parseBmp` checks. */
const testBmpMinimal = "BM" // Signature
+ "\x1A\x00\x00\x00" // File size: 26 bytes (header size)
+ "\x00\x00\x00\x00" // Reserved
+ "\x1A\x00\x00\x00" // Pixel data offset: 26 bytes (header size)
+ "\x0C\x00\x00\x00" // Header size: 12 bytes (BITMAPCOREHEADER)
+ "\x01\x00" // Width: 1
+ "\x01\x00" // Height: 1
+ "\x01\x00" // Planes: 1
+ "\x01\x00" // Bits per pixel: 1
+ "\x00\x00\x00\x00"; // Pixel data: 0 (black pixel)

// @ts-expect-error - not a Buffer.
parseBmp(testBmpMinimal);

parseBmp(Buffer.from(testBmpMinimal)); // $ExpectType { width: number; height: number; type: "bmp"; mime: "image/bmp"; wUnits: "px"; hUnits: "px"; } | undefined

// -- /lib/parse_sync/ico.js

import parseIco from "probe-image-size/lib/parse_sync/ico.js";

/** The smallest input that will pass `parseIco` checks (not a complete ICO). */
const testIcoMinimal = "\x00\x00" // Reserved. Must always be 0.
+ "\x01\x00" // Specifies image type: 1 for icon (.ICO)
+ "\x01\x00" // Specifies number of images in the file: 1
+ "\x01" // Width, 1 pixel. 0 means 256 pixels.
+ "\x01"; // Height, 1 pixel. 0 means 256 pixels.

// @ts-expect-error - not a Buffer.
parseIco(testIcoMinimal);

parseIco(Buffer.from(testIcoMinimal)); // $ExpectType { width: number; height: number; variants: { width: number; height: number; }[]; type: "ico"; mime: "image/x-icon"; wUnits: "px"; hUnits: "px"; } | undefined

// -- /lib/parse_sync/png.js

import parsePng from "probe-image-size/lib/parse_sync/png.js";

/** The smallest PNG that will pass `parsePng` checks. */
const testPngMinimal = "\x89PNG\r\n\x1a\n"
+ "\x00\x00\x00\x0D" // IHDR chunk length: 13 bytes
+ "IHDR"
+ "\x00\x00\x00\x01" // Width: 1
+ "\x00\x00\x00\x01"; // Height: 1

// @ts-expect-error - not a Buffer.
parsePng(testPngMinimal);

parsePng(Buffer.from(testPngMinimal)); // $ExpectType { width: number; height: number; type: "png"; mime: "image/png"; wUnits: "px"; hUnits: "px"; } | undefined

// -- /stream.js

import probeStream from "probe-image-size/stream.js";

(async () => {
await probeStream(fs.createReadStream("image.jpg")); // $ExpectType ProbeResult
await probeStream(fs.createReadStream("image.jpg"), true); // $ExpectType ProbeResult

// @ts-expect-error - not a Readable.
await probeStream("Foo");
})();
8 changes: 8 additions & 0 deletions types/probe-image-size/stream.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference types="node" />

import { Readable } from "node:stream";
import { ProbeResult } from ".";

declare function probeStream(source: Readable, keepOpen?: boolean): Promise<ProbeResult>;

export = probeStream;
6 changes: 5 additions & 1 deletion types/probe-image-size/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
},
"files": [
"index.d.ts",
"probe-image-size-tests.ts"
"probe-image-size-tests.ts",
"lib/parse_sync/bmp.d.ts",
"lib/parse_sync/ico.d.ts",
"lib/parse_sync/png.d.ts",
"stream.d.ts"
]
}