@phron/image 0.0.0 → 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.
- package/LICENSE +21 -0
- package/README.md +225 -3
- package/dist/index.d.ts +79 -0
- package/dist/index.js +51 -0
- package/dist/native.d.ts +2 -0
- package/dist/native.js +8 -0
- package/dist/next.d.ts +35 -0
- package/dist/next.js +48 -0
- package/dist/options.d.ts +22 -0
- package/dist/options.js +25 -0
- package/native.cjs +852 -0
- package/native.d.cts +81 -0
- package/package.json +92 -5
- package/phron-image.wasm32-wasip1.wasm +0 -0
- package/phron-image.wasm32-wasip1.wasm.d.mts +6 -0
- package/phron-image.wasm32-wasip1.workerd.d.mts +2 -0
- package/phron-image.wasm32-wasip1.workerd.mjs +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Phron
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,228 @@
|
|
|
1
1
|
# @phron/image
|
|
2
2
|
|
|
3
|
-
Build and sign Phron Image URLs.
|
|
3
|
+
Build and sign [Phron Image](https://phron.net) URLs from TypeScript and JavaScript.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
- One Rust implementation of the URL format, bound through napi-rs. On Linux x64 (glibc),
|
|
6
|
+
Node.js (20+) loads a prebuilt native binary. Everywhere else (macOS, arm64, Alpine, Windows)
|
|
7
|
+
it loads the same Rust code compiled to WebAssembly, with the same API and results. No
|
|
8
|
+
install-time compile.
|
|
9
|
+
- Edge runtimes use the WebAssembly build (see below). After each release, CI installs the
|
|
10
|
+
package from npm and runs the shared test vectors in workerd (Cloudflare Workers), in Deno, and
|
|
11
|
+
in Vercel's Edge Runtime emulator. It has not yet run on a Deno Deploy or Vercel deployment.
|
|
12
|
+
`@phron/image/next` builds public URLs in plain TypeScript and runs in the browser.
|
|
13
|
+
- Signed URLs, unsigned public URLs, API URLs, `srcset`, and a Next.js loader for public sources.
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @phron/image
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Security: keys and salts stay on the server
|
|
20
|
+
|
|
21
|
+
A signing key and salt can mint a valid URL for any image in the source. Keep them in server-side
|
|
22
|
+
code and server-side environment variables. **Never ship them to a browser**: not in a client
|
|
23
|
+
component, not in a `NEXT_PUBLIC_*` variable, not in a bundle. The same goes for API keys.
|
|
24
|
+
|
|
25
|
+
## Signed URLs
|
|
26
|
+
|
|
27
|
+
For a source whose access is "API key and signed URLs". Key and salt are the hex strings shown
|
|
28
|
+
once in the [console](https://console.phron.net).
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { options, roundedExpiry, signUrl } from "@phron/image";
|
|
32
|
+
|
|
33
|
+
const url = signUrl({
|
|
34
|
+
label: "my-source", // https://my-source.phronusercontent.com
|
|
35
|
+
key: process.env.PHRON_IMAGE_KEY!,
|
|
36
|
+
salt: process.env.PHRON_IMAGE_SALT!,
|
|
37
|
+
options: [options.resize("fit", 800, 800), options.quality(80)],
|
|
38
|
+
path: "photos/my cat.jpg", // not yet encoded; the SDK percent-encodes it before signing
|
|
39
|
+
format: "webp", // optional, written as @webp
|
|
40
|
+
expiresAt: roundedExpiry(24 * 3600), // optional
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The signature is the base64url (no padding) HMAC-SHA256, keyed with the hex-decoded key, over the
|
|
45
|
+
hex-decoded salt followed by the path exactly as sent. The SDK encodes the path first and signs
|
|
46
|
+
the encoded string.
|
|
47
|
+
|
|
48
|
+
Use `baseUrl` instead of `label` for a custom domain: `baseUrl: "https://img.example.com"`.
|
|
49
|
+
|
|
50
|
+
### Expiry and caching
|
|
51
|
+
|
|
52
|
+
`expiresAt` adds an `exp:<unix seconds>` option, which is part of the signed path. A browser keeps
|
|
53
|
+
a result for up to a day (or until `exp`, if that comes sooner). If every render computes `exp`
|
|
54
|
+
as "now plus an hour", the URL changes every second and the browser can never reuse a cached
|
|
55
|
+
result. `roundedExpiry(ttlSeconds, bucketSeconds = 3600)` rounds the expiry **up** to a bucket
|
|
56
|
+
boundary, so the URL is identical for every render in the same bucket.
|
|
57
|
+
|
|
58
|
+
## Public URLs
|
|
59
|
+
|
|
60
|
+
For a source whose access is "public". No secret, and only a preset and one optional width from
|
|
61
|
+
the source's list of public widths are accepted:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { publicUrl } from "@phron/image";
|
|
65
|
+
|
|
66
|
+
publicUrl({
|
|
67
|
+
label: "my-source",
|
|
68
|
+
preset: "card",
|
|
69
|
+
width: 640,
|
|
70
|
+
path: "photos/cat.jpg",
|
|
71
|
+
format: "webp",
|
|
72
|
+
});
|
|
73
|
+
// https://my-source.phronusercontent.com/_/pr:card/w:640/plain/photos/cat.jpg@webp
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## API URLs
|
|
77
|
+
|
|
78
|
+
The API takes the same options, unsigned, with your API key as a bearer token. The SDK returns
|
|
79
|
+
the URL; you add the header and make the request (server-side).
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { apiUrl, infoUrl, options } from "@phron/image";
|
|
83
|
+
|
|
84
|
+
const url = apiUrl({ label: "my-source", options: [options.width(800)], path: "photos/cat.jpg" });
|
|
85
|
+
const res = await fetch(url, { headers: { Authorization: `Bearer ${process.env.PHRON_API_KEY}` } });
|
|
86
|
+
|
|
87
|
+
const info = infoUrl({ label: "my-source", options: ["size:1"], path: "photos/cat.jpg" });
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Options
|
|
91
|
+
|
|
92
|
+
`options.resize`, `width`, `height`, `quality`, `format`, `gravity`, `preset` and `expires`
|
|
93
|
+
cover the common cases. Anything else goes in as a raw string: `options.raw("blur:3")` or just
|
|
94
|
+
`"blur:3"` in the array.
|
|
95
|
+
|
|
96
|
+
## srcset
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { options, signUrl, srcset } from "@phron/image";
|
|
100
|
+
|
|
101
|
+
const widths = [320, 640, 960, 1280];
|
|
102
|
+
const value = srcset(
|
|
103
|
+
(w) => signUrl({ label: "my-source", key, salt, options: [options.width(w)], path, expiresAt }),
|
|
104
|
+
widths,
|
|
105
|
+
);
|
|
106
|
+
// "https://… 320w, https://… 640w, …"
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Next.js
|
|
110
|
+
|
|
111
|
+
### Public sources: `publicLoader`
|
|
112
|
+
|
|
113
|
+
A custom loader runs in the browser, so it can only build URLs that need no secret. For a public
|
|
114
|
+
source:
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
// next.config.js
|
|
118
|
+
module.exports = {
|
|
119
|
+
images: {
|
|
120
|
+
loader: "custom",
|
|
121
|
+
loaderFile: "./phron-loader.ts",
|
|
122
|
+
// Use the source's list of public widths.
|
|
123
|
+
deviceSizes: [320, 640, 960, 1280],
|
|
124
|
+
imageSizes: [64, 128],
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
// phron-loader.ts
|
|
131
|
+
"use client";
|
|
132
|
+
import { publicLoader } from "@phron/image/next";
|
|
133
|
+
|
|
134
|
+
export default publicLoader({ label: "my-source", preset: "card", format: "webp" });
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
`quality` is ignored: a preset fixes the quality.
|
|
138
|
+
|
|
139
|
+
### Signed sources: sign on the server
|
|
140
|
+
|
|
141
|
+
A client-side loader can never hold the signing key, so it cannot produce signed URLs. For a
|
|
142
|
+
signed source, build the URLs in a Server Component and pass the result down:
|
|
143
|
+
|
|
144
|
+
```tsx
|
|
145
|
+
// app/page.tsx (Server Component)
|
|
146
|
+
import { options, roundedExpiry, signUrl, srcset } from "@phron/image";
|
|
147
|
+
|
|
148
|
+
export default function Page() {
|
|
149
|
+
const expiresAt = roundedExpiry(24 * 3600);
|
|
150
|
+
const sign = (width: number) =>
|
|
151
|
+
signUrl({
|
|
152
|
+
label: "my-source",
|
|
153
|
+
key: process.env.PHRON_IMAGE_KEY!,
|
|
154
|
+
salt: process.env.PHRON_IMAGE_SALT!,
|
|
155
|
+
options: [options.width(width)],
|
|
156
|
+
path: "photos/cat.jpg",
|
|
157
|
+
format: "webp",
|
|
158
|
+
expiresAt,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
// biome-ignore lint/performance/noImgElement: pre-signed srcset
|
|
163
|
+
<img
|
|
164
|
+
src={sign(640)}
|
|
165
|
+
srcSet={srcset(sign, [320, 640, 960, 1280])}
|
|
166
|
+
sizes="(min-width: 960px) 960px, 100vw"
|
|
167
|
+
alt=""
|
|
168
|
+
/>
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Cloudflare Workers
|
|
174
|
+
|
|
175
|
+
Workers cannot load a native binary, so they use the WebAssembly build. Import the module and
|
|
176
|
+
instantiate it once; the functions are the same Rust core (`signUrl`, `publicUrl`, `apiUrl`,
|
|
177
|
+
`infoUrl`, and `optionWidth`, `optionQuality` and so on for the option helpers):
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
import wasm from "@phron/image/wasm";
|
|
181
|
+
import { instantiate } from "@phron/image/workerd";
|
|
182
|
+
|
|
183
|
+
export default {
|
|
184
|
+
async fetch(_req: Request, env: { PHRON_KEY: string; PHRON_SALT: string }) {
|
|
185
|
+
const image = await instantiate(wasm); // resolves to the same instance after the first call
|
|
186
|
+
const url = image.signUrl({
|
|
187
|
+
key: env.PHRON_KEY,
|
|
188
|
+
salt: env.PHRON_SALT,
|
|
189
|
+
baseUrl: "https://acme.phronusercontent.com",
|
|
190
|
+
options: ["w:640"],
|
|
191
|
+
path: "photos/cat.jpg",
|
|
192
|
+
});
|
|
193
|
+
return Response.json({ url });
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
## Deno and Vercel Edge
|
|
199
|
+
|
|
200
|
+
The same two imports work, with the WebAssembly module loaded the way each runtime expects:
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
// Deno: a source-phase import gives the compiled module. Use `./wasm.wasm`, because Deno picks
|
|
204
|
+
// the module type from the file extension.
|
|
205
|
+
import source wasm from "npm:@phron/image/wasm.wasm";
|
|
206
|
+
import { instantiate } from "npm:@phron/image/workerd";
|
|
207
|
+
|
|
208
|
+
const image = await instantiate(wasm);
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
// Vercel Edge: Vercel compiles a `?module` import of a .wasm file to a WebAssembly.Module.
|
|
213
|
+
import wasm from "@phron/image/wasm.wasm?module";
|
|
214
|
+
import { instantiate } from "@phron/image/workerd";
|
|
215
|
+
|
|
216
|
+
const image = await instantiate(wasm);
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Other languages
|
|
220
|
+
|
|
221
|
+
This repository holds every Phron Image SDK. The URL format is implemented once, in the Rust
|
|
222
|
+
crate [`crates/phron-image`](crates/phron-image); each language package binds it, and
|
|
223
|
+
[`conformance/vectors.json`](conformance/vectors.json) pins the output every language must
|
|
224
|
+
reproduce. See [docs/adr/0001-rust-core-multi-language.md](docs/adr/0001-rust-core-multi-language.md).
|
|
225
|
+
|
|
226
|
+
## License
|
|
227
|
+
|
|
228
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -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
|
+
}
|
package/dist/native.d.ts
ADDED
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
|
+
};
|
package/dist/options.js
ADDED
|
@@ -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
|
+
};
|