avatarsniff 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,86 @@
1
+ import { R as RgbaImage, D as DetectOptions, a as DefaultAvatarDetection, b as DecodeOptions, I as ImageFormat } from './types-BlZG8aqK.cjs';
2
+ export { c as DETECTOR_NAMES, d as DetectorName, e as DetectorToggles } from './types-BlZG8aqK.cjs';
3
+
4
+ /**
5
+ * Analyse raw image pixels and decide whether they look like a generic/default
6
+ * provider avatar. Pure: the same input always yields the same result.
7
+ * Framework- and runtime-agnostic - no DOM, no Node, no native dependency.
8
+ *
9
+ * Runs an independent structural detector per family (initials, solidColor,
10
+ * personIcon, identicon); the first to claim the image wins, and `matched`
11
+ * reports which. Disable families via `options.detect` (each defaults on). The
12
+ * heuristics key on *structure*, never on specific palette colours, so they
13
+ * keep working as providers add new ones.
14
+ */
15
+ declare function analyzeImage(image: RgbaImage, options?: DetectOptions): DefaultAvatarDetection;
16
+
17
+ /**
18
+ * Built-in, zero-dependency image decoding for PNG, JPEG, GIF, WEBP and SVG.
19
+ *
20
+ * Two layers, no npm or native dependencies:
21
+ *
22
+ * 1. **Native layer** - when the runtime provides `createImageBitmap` + a
23
+ * canvas (`OffscreenCanvas` or a DOM `<canvas>`), every format is decoded
24
+ * by the platform: browsers, web workers, Deno, Bun. SVG is rasterised via
25
+ * an `<img>` element when a DOM is present.
26
+ * 2. **Pure-JS layer** - for plain Node (no canvas), PNG and GIF are decoded in
27
+ * pure JS (PNG inflate uses the built-in `DecompressionStream`) and JPEG via
28
+ * the bundled `jpeg-js` (a devDependency inlined into `dist`, so consumers
29
+ * still install zero dependencies). WEBP and SVG aren't decoded here; they
30
+ * return `null`, which callers treat as "not a default, keep it" - the right
31
+ * outcome for a real photo/graphic.
32
+ *
33
+ * Inputs larger than `maxBytes` (default 10MB) are rejected before decoding.
34
+ */
35
+ declare const DEFAULT_MAX_BYTES: number;
36
+ declare function sniffFormat(bytes: Uint8Array): ImageFormat;
37
+ /**
38
+ * Decode encoded image bytes (PNG, JPEG, GIF, WEBP, SVG) to RGBA pixels.
39
+ * Returns `null` when the format can't be decoded in the current runtime or the
40
+ * input exceeds `maxBytes`. Zero dependencies.
41
+ */
42
+ declare function decodeImage(bytes: Uint8Array | ArrayBuffer, options?: DecodeOptions): Promise<RgbaImage | null>;
43
+
44
+ type Decoder = (bytes: Uint8Array) => Promise<RgbaImage | null>;
45
+ /**
46
+ * Register a decoder for a format. The opt-in `avatarsniff/webp` and
47
+ * `avatarsniff/svg` entry points call this on import, so importing them makes
48
+ * `decodeImage`/`sniff` handle that format in plain Node too. You
49
+ * can also register your own decoder for any format.
50
+ */
51
+ declare function registerDecoder(format: ImageFormat, decoder: Decoder): void;
52
+
53
+ /** Minimal structural shape of a canvas `ImageData` (RGBA pixels). */
54
+ interface ImageDataLike {
55
+ data: Uint8ClampedArray | Uint8Array | number[];
56
+ width: number;
57
+ height: number;
58
+ }
59
+ /**
60
+ * Anything `sniff` accepts:
61
+ * - encoded bytes (`Uint8Array` / `ArrayBuffer`) — decoded, then analysed;
62
+ * - raw pixels (a canvas `ImageData` or any `RgbaImage`) — analysed directly;
63
+ * - a URL string — fetched, then decoded and analysed;
64
+ * - `null` / `undefined` — resolves to `null` (nothing to sniff).
65
+ */
66
+ type SniffInput = Uint8Array | ArrayBuffer | ImageDataLike | RgbaImage | string | null | undefined;
67
+ /**
68
+ * Detect whether an avatar is a generic provider default. The one entry point —
69
+ * it sniffs out what you passed and does the right thing:
70
+ *
71
+ * ```ts
72
+ * await sniff(bytes); // Uint8Array / ArrayBuffer
73
+ * await sniff(canvasImageData); // browser <canvas> getImageData()
74
+ * await sniff(user.photoUrl); // URL string (fetched for you)
75
+ * await sniff(bytes, { detect: { identicon: false } });
76
+ * ```
77
+ *
78
+ * Always async. Returns `null` only when there was nothing to sniff — a
79
+ * nullish input, or a URL that was missing or failed to fetch — so the caller
80
+ * can keep the existing image. Undecodable bytes resolve to a not-a-default
81
+ * verdict (a real photo we can't read is safely kept, not wrongly replaced).
82
+ */
83
+ declare function sniff(input: string | null | undefined, options?: DetectOptions): Promise<DefaultAvatarDetection | null>;
84
+ declare function sniff(input: Uint8Array | ArrayBuffer | ImageDataLike | RgbaImage, options?: DetectOptions): Promise<DefaultAvatarDetection>;
85
+
86
+ export { DEFAULT_MAX_BYTES, DecodeOptions, type Decoder, DefaultAvatarDetection, DetectOptions, type ImageDataLike, ImageFormat, RgbaImage, type SniffInput, analyzeImage, decodeImage, registerDecoder, sniff, sniffFormat };
@@ -0,0 +1,86 @@
1
+ import { R as RgbaImage, D as DetectOptions, a as DefaultAvatarDetection, b as DecodeOptions, I as ImageFormat } from './types-BlZG8aqK.js';
2
+ export { c as DETECTOR_NAMES, d as DetectorName, e as DetectorToggles } from './types-BlZG8aqK.js';
3
+
4
+ /**
5
+ * Analyse raw image pixels and decide whether they look like a generic/default
6
+ * provider avatar. Pure: the same input always yields the same result.
7
+ * Framework- and runtime-agnostic - no DOM, no Node, no native dependency.
8
+ *
9
+ * Runs an independent structural detector per family (initials, solidColor,
10
+ * personIcon, identicon); the first to claim the image wins, and `matched`
11
+ * reports which. Disable families via `options.detect` (each defaults on). The
12
+ * heuristics key on *structure*, never on specific palette colours, so they
13
+ * keep working as providers add new ones.
14
+ */
15
+ declare function analyzeImage(image: RgbaImage, options?: DetectOptions): DefaultAvatarDetection;
16
+
17
+ /**
18
+ * Built-in, zero-dependency image decoding for PNG, JPEG, GIF, WEBP and SVG.
19
+ *
20
+ * Two layers, no npm or native dependencies:
21
+ *
22
+ * 1. **Native layer** - when the runtime provides `createImageBitmap` + a
23
+ * canvas (`OffscreenCanvas` or a DOM `<canvas>`), every format is decoded
24
+ * by the platform: browsers, web workers, Deno, Bun. SVG is rasterised via
25
+ * an `<img>` element when a DOM is present.
26
+ * 2. **Pure-JS layer** - for plain Node (no canvas), PNG and GIF are decoded in
27
+ * pure JS (PNG inflate uses the built-in `DecompressionStream`) and JPEG via
28
+ * the bundled `jpeg-js` (a devDependency inlined into `dist`, so consumers
29
+ * still install zero dependencies). WEBP and SVG aren't decoded here; they
30
+ * return `null`, which callers treat as "not a default, keep it" - the right
31
+ * outcome for a real photo/graphic.
32
+ *
33
+ * Inputs larger than `maxBytes` (default 10MB) are rejected before decoding.
34
+ */
35
+ declare const DEFAULT_MAX_BYTES: number;
36
+ declare function sniffFormat(bytes: Uint8Array): ImageFormat;
37
+ /**
38
+ * Decode encoded image bytes (PNG, JPEG, GIF, WEBP, SVG) to RGBA pixels.
39
+ * Returns `null` when the format can't be decoded in the current runtime or the
40
+ * input exceeds `maxBytes`. Zero dependencies.
41
+ */
42
+ declare function decodeImage(bytes: Uint8Array | ArrayBuffer, options?: DecodeOptions): Promise<RgbaImage | null>;
43
+
44
+ type Decoder = (bytes: Uint8Array) => Promise<RgbaImage | null>;
45
+ /**
46
+ * Register a decoder for a format. The opt-in `avatarsniff/webp` and
47
+ * `avatarsniff/svg` entry points call this on import, so importing them makes
48
+ * `decodeImage`/`sniff` handle that format in plain Node too. You
49
+ * can also register your own decoder for any format.
50
+ */
51
+ declare function registerDecoder(format: ImageFormat, decoder: Decoder): void;
52
+
53
+ /** Minimal structural shape of a canvas `ImageData` (RGBA pixels). */
54
+ interface ImageDataLike {
55
+ data: Uint8ClampedArray | Uint8Array | number[];
56
+ width: number;
57
+ height: number;
58
+ }
59
+ /**
60
+ * Anything `sniff` accepts:
61
+ * - encoded bytes (`Uint8Array` / `ArrayBuffer`) — decoded, then analysed;
62
+ * - raw pixels (a canvas `ImageData` or any `RgbaImage`) — analysed directly;
63
+ * - a URL string — fetched, then decoded and analysed;
64
+ * - `null` / `undefined` — resolves to `null` (nothing to sniff).
65
+ */
66
+ type SniffInput = Uint8Array | ArrayBuffer | ImageDataLike | RgbaImage | string | null | undefined;
67
+ /**
68
+ * Detect whether an avatar is a generic provider default. The one entry point —
69
+ * it sniffs out what you passed and does the right thing:
70
+ *
71
+ * ```ts
72
+ * await sniff(bytes); // Uint8Array / ArrayBuffer
73
+ * await sniff(canvasImageData); // browser <canvas> getImageData()
74
+ * await sniff(user.photoUrl); // URL string (fetched for you)
75
+ * await sniff(bytes, { detect: { identicon: false } });
76
+ * ```
77
+ *
78
+ * Always async. Returns `null` only when there was nothing to sniff — a
79
+ * nullish input, or a URL that was missing or failed to fetch — so the caller
80
+ * can keep the existing image. Undecodable bytes resolve to a not-a-default
81
+ * verdict (a real photo we can't read is safely kept, not wrongly replaced).
82
+ */
83
+ declare function sniff(input: string | null | undefined, options?: DetectOptions): Promise<DefaultAvatarDetection | null>;
84
+ declare function sniff(input: Uint8Array | ArrayBuffer | ImageDataLike | RgbaImage, options?: DetectOptions): Promise<DefaultAvatarDetection>;
85
+
86
+ export { DEFAULT_MAX_BYTES, DecodeOptions, type Decoder, DefaultAvatarDetection, DetectOptions, type ImageDataLike, ImageFormat, RgbaImage, type SniffInput, analyzeImage, decodeImage, registerDecoder, sniff, sniffFormat };