@phron/image 0.0.0 → 0.1.0

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,79 @@
1
+ export type { Gravity, ResizeType } from "./options.js";
2
+ export { options } from "./options.js";
3
+ /** Where a browser URL points: pass `baseUrl` (a custom domain) or `label`, never both. */
4
+ export interface Target {
5
+ baseUrl?: string;
6
+ /** Source label: `https://<label>.phronusercontent.com`. */
7
+ label?: string;
8
+ }
9
+ export interface SignUrlParams extends Target {
10
+ /** Hex signing key, as shown once in the console. Server-side only. */
11
+ key: string;
12
+ /** Hex salt, as shown once in the console. Server-side only. */
13
+ salt: string;
14
+ /** Processing options, for example `[options.width(800), options.quality(80)]`. */
15
+ options?: readonly string[];
16
+ /** Source path, not yet encoded. */
17
+ path: string;
18
+ /** Output format, written as `@<format>`. */
19
+ format?: string;
20
+ /** Unix seconds. Adds an `exp:<n>` option, which is part of the signed path. */
21
+ expiresAt?: number;
22
+ }
23
+ /**
24
+ * Build a signed browser URL: `<base>/<signature>/<options>/plain/<path>[@<format>]`.
25
+ * The signing is done by the Rust core. Server-side only: `key` and `salt` must never reach a
26
+ * browser. Throws if an input is invalid.
27
+ */
28
+ export declare function signUrl(params: SignUrlParams): string;
29
+ export interface PublicUrlParams extends Target {
30
+ /** Preset name. */
31
+ preset: string;
32
+ /** One of the source's public widths. */
33
+ width?: number;
34
+ path: string;
35
+ format?: string;
36
+ }
37
+ /**
38
+ * Build an unsigned browser URL for a source whose access is "public":
39
+ * `<base>/_/pr:<preset>[/w:<width>]/plain/<path>[@<format>]`. No secret involved.
40
+ */
41
+ export declare function publicUrl(params: PublicUrlParams): string;
42
+ export interface ApiUrlParams {
43
+ label: string;
44
+ /** Override the API host (default `https://api.phron.net`). */
45
+ apiBaseUrl?: string;
46
+ options?: readonly string[];
47
+ path: string;
48
+ format?: string;
49
+ }
50
+ /**
51
+ * Build an API URL: `https://api.phron.net/v1/image/<label>/_/<options>/plain/<path>`.
52
+ * Send it with an `Authorization: Bearer <API key>` header. Not signed.
53
+ */
54
+ export declare function apiUrl(params: ApiUrlParams): string;
55
+ export interface InfoUrlParams {
56
+ label: string;
57
+ apiBaseUrl?: string;
58
+ /** Info options, for example `["size:1", "format:1"]`. */
59
+ options?: readonly string[];
60
+ path: string;
61
+ }
62
+ /** Build an info URL: `https://api.phron.net/v1/image/<label>/info/<info options>/plain/<path>`. */
63
+ export declare function infoUrl(params: InfoUrlParams): string;
64
+ /**
65
+ * Percent-encode a source path: each segment is encoded, slashes are kept, a leading slash is
66
+ * dropped. `@` is encoded too, because an unescaped `@` starts the output format.
67
+ */
68
+ export declare function encodePath(path: string): string;
69
+ /**
70
+ * Round an expiry up to a bucket boundary: `now + ttl`, rounded UP to a multiple of
71
+ * `bucketSeconds`. The result is a unix time in seconds.
72
+ *
73
+ * A URL that carries the same `exp` on every render stays byte-identical, so the browser's
74
+ * cache (which may keep a result for up to a day) keeps working. An `exp` of "now plus one
75
+ * hour" changes every second and defeats it.
76
+ */
77
+ export declare function roundedExpiry(ttlSeconds: number, bucketSeconds?: number, nowMs?: number): number;
78
+ /** Build a `srcset` value: `<url> <width>w, ...`, one URL per width. */
79
+ export declare function srcset(builder: (width: number) => string, widths: readonly number[]): string;
package/dist/index.js ADDED
@@ -0,0 +1,51 @@
1
+ import { native } from "./native.js";
2
+ export { options } from "./options.js";
3
+ const list = (options) => options ? [...options] : undefined;
4
+ /**
5
+ * Build a signed browser URL: `<base>/<signature>/<options>/plain/<path>[@<format>]`.
6
+ * The signing is done by the Rust core. Server-side only: `key` and `salt` must never reach a
7
+ * browser. Throws if an input is invalid.
8
+ */
9
+ export function signUrl(params) {
10
+ return native.signUrl({ ...params, options: list(params.options) });
11
+ }
12
+ /**
13
+ * Build an unsigned browser URL for a source whose access is "public":
14
+ * `<base>/_/pr:<preset>[/w:<width>]/plain/<path>[@<format>]`. No secret involved.
15
+ */
16
+ export function publicUrl(params) {
17
+ return native.publicUrl(params);
18
+ }
19
+ /**
20
+ * Build an API URL: `https://api.phron.net/v1/image/<label>/_/<options>/plain/<path>`.
21
+ * Send it with an `Authorization: Bearer <API key>` header. Not signed.
22
+ */
23
+ export function apiUrl(params) {
24
+ return native.apiUrl({ ...params, options: list(params.options) });
25
+ }
26
+ /** Build an info URL: `https://api.phron.net/v1/image/<label>/info/<info options>/plain/<path>`. */
27
+ export function infoUrl(params) {
28
+ return native.infoUrl({ ...params, options: list(params.options) });
29
+ }
30
+ /**
31
+ * Percent-encode a source path: each segment is encoded, slashes are kept, a leading slash is
32
+ * dropped. `@` is encoded too, because an unescaped `@` starts the output format.
33
+ */
34
+ export function encodePath(path) {
35
+ return native.encodePath(path);
36
+ }
37
+ /**
38
+ * Round an expiry up to a bucket boundary: `now + ttl`, rounded UP to a multiple of
39
+ * `bucketSeconds`. The result is a unix time in seconds.
40
+ *
41
+ * A URL that carries the same `exp` on every render stays byte-identical, so the browser's
42
+ * cache (which may keep a result for up to a day) keeps working. An `exp` of "now plus one
43
+ * hour" changes every second and defeats it.
44
+ */
45
+ export function roundedExpiry(ttlSeconds, bucketSeconds = 3600, nowMs = Date.now()) {
46
+ return native.roundedExpiry(nowMs / 1000, ttlSeconds, bucketSeconds);
47
+ }
48
+ /** Build a `srcset` value: `<url> <width>w, ...`, one URL per width. */
49
+ export function srcset(builder, widths) {
50
+ return widths.map((w) => `${builder(w)} ${w}w`).join(", ");
51
+ }
@@ -0,0 +1,2 @@
1
+ import type * as Native from "../native.d.cts";
2
+ export declare const native: typeof Native;
package/dist/native.js ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * The napi-rs addon, loaded through the generated `native.cjs` (it picks the platform package,
3
+ * `@phron/image-<platform>`, or a local `.node` file in development). The addon is generated at
4
+ * build time and is not in version control.
5
+ */
6
+ import { createRequire } from "node:module";
7
+ const require = createRequire(import.meta.url);
8
+ export const native = require("../native.cjs");
package/dist/next.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * `next/image` loader for public sources. It runs in the browser, where the native addon cannot
3
+ * load, so it builds the unsigned public URL itself instead of calling the Rust core. This is
4
+ * the one URL builder written outside the core: the `public` cases of `conformance/vectors.json`
5
+ * pin it to the core's output (tests/conformance.test.ts). Retire it when a WebAssembly build of
6
+ * the core can serve browsers (docs/adr/0001-rust-core-multi-language.md).
7
+ */
8
+ export interface NextImageLoaderProps {
9
+ src: string;
10
+ width: number;
11
+ quality?: number;
12
+ }
13
+ export interface PublicLoaderParams {
14
+ /** A custom domain, for example `https://img.example.com`. Pass this or `label`. */
15
+ baseUrl?: string;
16
+ /** Source label: `https://<label>.phronusercontent.com`. */
17
+ label?: string;
18
+ /** Preset name. */
19
+ preset: string;
20
+ format?: string;
21
+ }
22
+ /**
23
+ * A `next/image` custom loader for a source whose access is "public". It builds unsigned
24
+ * URLs, so it holds no secret and is safe in client code.
25
+ *
26
+ * Next.js asks for the widths in `images.deviceSizes` and `images.imageSizes`; set those to
27
+ * the source's list of public widths.
28
+ *
29
+ * ```ts
30
+ * "use client";
31
+ * import { publicLoader } from "@phron/image/next";
32
+ * export default publicLoader({ label: "my-source", preset: "card" });
33
+ * ```
34
+ */
35
+ export declare function publicLoader(params: PublicLoaderParams): ({ src, width }: NextImageLoaderProps) => string;
package/dist/next.js ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * `next/image` loader for public sources. It runs in the browser, where the native addon cannot
3
+ * load, so it builds the unsigned public URL itself instead of calling the Rust core. This is
4
+ * the one URL builder written outside the core: the `public` cases of `conformance/vectors.json`
5
+ * pin it to the core's output (tests/conformance.test.ts). Retire it when a WebAssembly build of
6
+ * the core can serve browsers (docs/adr/0001-rust-core-multi-language.md).
7
+ */
8
+ const LABEL = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/i;
9
+ const encodePath = (path) => path
10
+ .replace(/^\/+/, "")
11
+ .split("/")
12
+ .map((segment) => encodeURIComponent(segment))
13
+ .join("/");
14
+ function baseOf({ baseUrl, label }) {
15
+ if (baseUrl !== undefined && label !== undefined)
16
+ throw new TypeError("pass either baseUrl or label, not both");
17
+ if (baseUrl !== undefined)
18
+ return baseUrl.replace(/\/+$/, "");
19
+ if (label === undefined)
20
+ throw new TypeError("baseUrl or label is required");
21
+ if (!LABEL.test(label))
22
+ throw new TypeError(`invalid source label: ${JSON.stringify(label)}`);
23
+ return `https://${label}.phronusercontent.com`;
24
+ }
25
+ /**
26
+ * A `next/image` custom loader for a source whose access is "public". It builds unsigned
27
+ * URLs, so it holds no secret and is safe in client code.
28
+ *
29
+ * Next.js asks for the widths in `images.deviceSizes` and `images.imageSizes`; set those to
30
+ * the source's list of public widths.
31
+ *
32
+ * ```ts
33
+ * "use client";
34
+ * import { publicLoader } from "@phron/image/next";
35
+ * export default publicLoader({ label: "my-source", preset: "card" });
36
+ * ```
37
+ */
38
+ export function publicLoader(params) {
39
+ const base = baseOf(params);
40
+ const suffix = params.format ? `@${encodeURIComponent(params.format)}` : "";
41
+ // `quality` is ignored on purpose: a preset fixes the quality, and an unsigned URL accepts
42
+ // only a preset and a width.
43
+ return ({ src, width }) => {
44
+ if (!Number.isInteger(width) || width <= 0)
45
+ throw new TypeError("width must be a positive integer");
46
+ return `${base}/_/pr:${encodeURIComponent(params.preset)}/w:${width}/plain/${encodePath(src)}${suffix}`;
47
+ };
48
+ }
@@ -0,0 +1,22 @@
1
+ export type ResizeType = "fit" | "fill" | "fill-down" | "force" | "auto";
2
+ export type Gravity = "no" | "so" | "ea" | "we" | "noea" | "nowe" | "soea" | "sowe" | "ce" | "sm";
3
+ export declare const options: {
4
+ /** `rs:<type>:<width>:<height>` */
5
+ readonly resize: (type: ResizeType, width: number, height: number) => string;
6
+ /** `w:<width>` */
7
+ readonly width: (width: number) => string;
8
+ /** `h:<height>` */
9
+ readonly height: (height: number) => string;
10
+ /** `q:<quality>` (1 to 100). */
11
+ readonly quality: (quality: number) => string;
12
+ /** `f:<format>`. A URL can also carry the format as an `@<format>` suffix. */
13
+ readonly format: (format: string) => string;
14
+ /** `g:<gravity>[:<x offset>:<y offset>]` */
15
+ readonly gravity: (gravity: Gravity, xOffset?: number, yOffset?: number) => string;
16
+ /** `pr:<preset>` */
17
+ readonly preset: (name: string) => string;
18
+ /** `exp:<unix seconds>` */
19
+ readonly expires: (unixSeconds: number) => string;
20
+ /** Escape hatch: an option written exactly as it should appear in the URL. */
21
+ readonly raw: (option: string) => string;
22
+ };
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Typed helpers for the common processing options. Each returns the option as a string, so
3
+ * they mix freely with raw strings in an `options` array. The rules live in the Rust core.
4
+ */
5
+ import { native } from "./native.js";
6
+ export const options = {
7
+ /** `rs:<type>:<width>:<height>` */
8
+ resize: (type, width, height) => native.optionResize(type, width, height),
9
+ /** `w:<width>` */
10
+ width: (width) => native.optionWidth(width),
11
+ /** `h:<height>` */
12
+ height: (height) => native.optionHeight(height),
13
+ /** `q:<quality>` (1 to 100). */
14
+ quality: (quality) => native.optionQuality(quality),
15
+ /** `f:<format>`. A URL can also carry the format as an `@<format>` suffix. */
16
+ format: (format) => native.optionFormat(format),
17
+ /** `g:<gravity>[:<x offset>:<y offset>]` */
18
+ gravity: (gravity, xOffset, yOffset) => native.optionGravity(gravity, xOffset, yOffset),
19
+ /** `pr:<preset>` */
20
+ preset: (name) => native.optionPreset(name),
21
+ /** `exp:<unix seconds>` */
22
+ expires: (unixSeconds) => native.optionExpires(unixSeconds),
23
+ /** Escape hatch: an option written exactly as it should appear in the URL. */
24
+ raw: (option) => native.optionRaw(option),
25
+ };