ipx 4.0.0-alpha.2 → 4.0.0-beta.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/dist/cli.d.mts CHANGED
@@ -1 +1 @@
1
- export { };
1
+ export {}
package/dist/cli.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  import { createIPX, ipxFSStorage, ipxHttpStorage, serveIPX } from "./_chunks/node-fs.mjs";
3
3
  import { parseArgs } from "node:util";
4
4
  var name = "ipx";
5
- var version = "4.0.0-alpha.1";
5
+ var version = "4.0.0-beta.0";
6
6
  const { positionals, values } = parseArgs({
7
7
  allowPositionals: true,
8
8
  options: {
package/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ImageMeta } from "./_chunks/libs/image-meta.mjs";
2
+ import { HTTPError } from "./_chunks/libs/h3.mjs";
2
3
  import { NodeHttpHandler, Server, ServerOptions } from "srvx";
3
4
  import { Color, KernelEnum, Sharp, SharpOptions } from "sharp";
4
5
  import { Config } from "svgo";
@@ -34,6 +35,30 @@ interface HandlerContext {
34
35
  * @optional
35
36
  */
36
37
  kernel?: keyof KernelEnum;
38
+ /**
39
+ * The maximum width and height of the output image, or `false` when
40
+ * unlimited. Set from the `maxOutputDimension` IPX option.
41
+ * @optional
42
+ */
43
+ maxOutputDimension?: number | false;
44
+ /**
45
+ * Dimensions the image is expected to have once the resize modifiers
46
+ * (`resize`, `width`, `height`) have been applied, recorded by those
47
+ * handlers after their own clamping.
48
+ *
49
+ * libvips resizes before it extends, whatever order the modifiers appear in
50
+ * the URL, so `extend` budgets its edges against these rather than against
51
+ * {@link meta}: budgeting against the source lets the two clamps stack and
52
+ * the canvas end up at twice `maxOutputDimension` on each axis.
53
+ *
54
+ * Undefined when no resize modifier was applied (or when nothing could be
55
+ * projected from it), in which case `extend` falls back to {@link meta}.
56
+ * @optional
57
+ */
58
+ outputDimensions?: {
59
+ width?: number;
60
+ height?: number;
61
+ };
37
62
  /**
38
63
  * Metadata about the image being processed.
39
64
  */
@@ -106,6 +131,7 @@ type IPXSourceMeta = {
106
131
  */
107
132
  maxAge?: number;
108
133
  };
134
+ type FlagModifier = true | "true" | "";
109
135
  type FormatModifier = "jpeg" | "jpg" | "png" | "webp" | "avif" | "gif" | "heif" | "tiff" | "auto" | (string & {});
110
136
  interface IPXModifiers {
111
137
  format: FormatModifier;
@@ -119,28 +145,39 @@ interface IPXModifiers {
119
145
  b: string;
120
146
  position: string;
121
147
  pos: string;
122
- enlarge: true | "true";
148
+ enlarge: FlagModifier;
123
149
  kernel: "nearest" | "cubic" | "mitchell" | "lanczos2" | "lanczos3" | (string & {});
124
150
  trim: number | string;
125
151
  extend: string;
126
152
  extract: string;
127
153
  crop: string;
128
154
  rotate: number | string;
129
- flip: true | "true";
130
- flop: true | "true";
155
+ autoorient: FlagModifier;
156
+ flip: FlagModifier;
157
+ flop: FlagModifier;
131
158
  sharpen: number | string;
132
159
  median: number | string;
133
160
  blur: number | string;
134
- flatten: true | "true";
161
+ dilate: number | string;
162
+ erode: number | string;
163
+ clahe: number | string;
164
+ flatten: FlagModifier;
165
+ unflatten: FlagModifier;
135
166
  gamma: string;
136
- negate: true | "true";
137
- normalize: true | "true";
167
+ negate: FlagModifier | string;
168
+ normalize: FlagModifier | string;
138
169
  threshold: number | string;
170
+ linear: string;
139
171
  modulate: string;
172
+ brightness: number | string;
173
+ saturation: number | string;
174
+ hue: number | string;
175
+ lightness: number | string;
176
+ opacity: number | string;
140
177
  tint: number | string;
141
- grayscale: true | "true";
142
- animated: true | "true";
143
- a: true | "true";
178
+ grayscale: FlagModifier;
179
+ animated: FlagModifier;
180
+ a: FlagModifier;
144
181
  width: string | number;
145
182
  w: string | number;
146
183
  height: string | number;
@@ -177,6 +214,26 @@ type IPXOptions = {
177
214
  * @optional
178
215
  */
179
216
  maxAge?: number;
217
+ /**
218
+ * Maximum width and height (in pixels) of the output image.
219
+ *
220
+ * Modifiers are user input and sharp's `limitInputPixels` only bounds the
221
+ * *input*, so without this limit a request such as
222
+ * `/enlarge,s_20000x20000/img.jpg` or `/extend_10000_10000_10000_10000/img.jpg`
223
+ * makes sharp allocate a multi-gigabyte output buffer from a tiny source
224
+ * image, which is an easy way to exhaust the server memory.
225
+ *
226
+ * Requested `width`, `height`, `resize` dimensions are clamped to this value
227
+ * (preserving the requested aspect ratio) and `extend` edges are clamped so
228
+ * that the extended canvas stays within it.
229
+ *
230
+ * Set to `false` to disable the limit (only safe when modifiers come from a
231
+ * trusted source).
232
+ *
233
+ * @default 8192
234
+ * @optional
235
+ */
236
+ maxOutputDimension?: number | false;
180
237
  /**
181
238
  * A mapping of URL aliases to their corresponding URLs, used to simplify resource identifiers.
182
239
  * @optional
@@ -197,10 +254,28 @@ type IPXOptions = {
197
254
  */
198
255
  httpStorage?: IPXStorage;
199
256
  /**
200
- * Configuration for the SVGO library used when processing SVG images.
257
+ * Options for SVG images, which are processed by SVGO instead of Sharp.
201
258
  * @optional
202
259
  */
203
- svgo?: false | Config;
260
+ svg?: {
261
+ /**
262
+ * Configuration for the SVGO library used to optimize SVG images.
263
+ *
264
+ * SVGO's `preset-default` is applied unless custom `plugins` are configured.
265
+ * Set to `false` to disable optimization. Sanitization is applied either way.
266
+ * @optional
267
+ */
268
+ optimize?: false | Config;
269
+ /**
270
+ * Disable SVG sanitization.
271
+ *
272
+ * Sanitized SVG images cannot execute scripts. Only disable this if all sources
273
+ * are fully trusted, as it allows serving SVG images with XSS payloads.
274
+ * @default false
275
+ * @optional
276
+ */
277
+ unsafeSkipSanitize?: boolean;
278
+ };
204
279
  };
205
280
  /**
206
281
  * Creates an IPX image processing instance with the specified options.
@@ -210,12 +285,62 @@ type IPXOptions = {
210
285
  */
211
286
  declare function createIPX(userOptions: IPXOptions): IPX;
212
287
  type FetchHandler = (request: Request | string | URL) => Response | Promise<Response>;
213
- declare function createIPXFetchHandler(ipx: IPX): FetchHandler;
214
- declare function createIPXNodeHandler(ipx: IPX): NodeHttpHandler;
215
- declare function serveIPX(ipx: IPX, opts?: Omit<ServerOptions, "fetch">): Server;
288
+ interface IPXHandlerOptions {
289
+ /**
290
+ * Custom URL parser to extract the resource id and modifiers from the request URL.
291
+ * Can be async.
292
+ *
293
+ * Receives the raw (absolute, still percent-encoded) request URL, so parsers can
294
+ * decode it themselves without going through h3's normalization.
295
+ *
296
+ * Defaults to {@link parseIPXURL} which handles URLs in the form `/<modifiers>/<id>`.
297
+ *
298
+ * Returned values are escaped (control characters) by the handler, so custom parsers
299
+ * do not need to escape them. This is not an access check: exactly as with the default
300
+ * parser, what the resulting `id` is allowed to resolve to is enforced by the storage
301
+ * layer. Throw an `HTTPError` to reject a request with a specific status code.
302
+ *
303
+ * @optional
304
+ */
305
+ parseURL?: IPXURLParser;
306
+ }
307
+ declare function createIPXFetchHandler(ipx: IPX, opts?: IPXHandlerOptions): FetchHandler;
308
+ declare function createIPXNodeHandler(ipx: IPX, opts?: IPXHandlerOptions): NodeHttpHandler;
309
+ declare function serveIPX(ipx: IPX, opts?: Omit<ServerOptions, "fetch"> & IPXHandlerOptions): Server;
310
+ interface IPXParsedURL {
311
+ /**
312
+ * The identifier of the source image (path or URL, depending on the storage).
313
+ */
314
+ id: string;
315
+ /**
316
+ * Modifiers to apply, keyed by modifier name. See {@link IPXModifiers}.
317
+ *
318
+ * Values are coerced to strings. Use `""` (or `undefined`) for valueless
319
+ * modifiers such as `grayscale`.
320
+ */
321
+ modifiers: Record<string, string | number | boolean | undefined>;
322
+ }
323
+ type IPXURLParser = (url: string) => IPXParsedURL | Promise<IPXParsedURL>;
324
+ /**
325
+ * Default IPX URL parser, handling URLs in the form `/<modifiers>/<id>`.
326
+ *
327
+ * Use `_` as the modifiers segment to apply none (`/_/image.png`).
328
+ *
329
+ * @param {string} url - The raw (absolute) request URL.
330
+ * @returns {IPXParsedURL} The parsed resource id and modifiers. See {@link IPXParsedURL}.
331
+ * @throws {HTTPError} If the modifiers segment is missing.
332
+ */
333
+ declare function parseIPXURL(url: string): IPXParsedURL;
216
334
  type HTTPStorageOptions = {
217
335
  /**
218
336
  * Custom options for fetch operations, such as headers or method overrides.
337
+ *
338
+ * Redirects are followed manually (one hop at a time) so that each redirect target
339
+ * can be re-validated against the {@link HTTPStorageOptions.domains} allowlist
340
+ * (and {@link HTTPStorageOptions.blockPrivateIPs} when enabled).
341
+ * Explicitly setting `redirect` here opts out of this protection: the value is passed
342
+ * to `fetch` as-is and redirect targets are **not** validated (a redirect to an
343
+ * internal address is then possible: SSRF).
219
344
  * @optional
220
345
  */
221
346
  fetchOptions?: RequestInit;
@@ -226,6 +351,11 @@ type HTTPStorageOptions = {
226
351
  maxAge?: number;
227
352
  /**
228
353
  * Whitelist of domains from which resource fetching is allowed. Can be a single string or an array of strings.
354
+ *
355
+ * Only `http:` and `https:` URLs are allowed. Redirects are followed only within this
356
+ * allowlist (up to 3 hops): a redirect to a host that is not listed is rejected with
357
+ * `403 IPX_FORBIDDEN_HOST`, so an allowlisted host cannot bounce IPX to an internal
358
+ * address. Hosts that redirect to a CDN must have the CDN hostname listed here too.
229
359
  * @optional
230
360
  */
231
361
  domains?: string | string[];
@@ -239,6 +369,22 @@ type HTTPStorageOptions = {
239
369
  * @optional
240
370
  */
241
371
  ignoreCacheControl?: boolean;
372
+ /**
373
+ * If set to true, reject URLs whose host is (or resolves to) a non-public IP address
374
+ * with `403 IPX_FORBIDDEN_IP`. Checked for the requested URL and for every redirect hop.
375
+ *
376
+ * This is **defense in depth**, not the primary access control: the
377
+ * {@link HTTPStorageOptions.domains} allowlist is. It only helps when an allowlisted
378
+ * (or, with `allowAllDomains`, arbitrary) hostname points at loopback, link-local
379
+ * (`169.254.169.254`, the cloud metadata service), RFC1918, CGNAT, unique-local or
380
+ * otherwise non-public space.
381
+ *
382
+ * Disabled by default because many legitimate deployments fetch from in-cluster origins
383
+ * (internal object storage, sidecars, `localhost` during development), which this blocks.
384
+ * @optional
385
+ * @default false
386
+ */
387
+ blockPrivateIPs?: boolean;
242
388
  };
243
389
  /**
244
390
  * Creates an HTTP storage handler for IPX that fetches image data from external URLs.
@@ -256,10 +402,32 @@ type NodeFSSOptions = {
256
402
  */
257
403
  dir?: string | string[];
258
404
  /**
259
- * The directory or list of directories from which to serve files. If not specified, the current directory is used by default.
405
+ * `max-age`, in seconds, reported for files served from this storage. Used for the
406
+ * `cache-control` header. Falls back to the IPX-wide `maxAge` (default 60) when unset.
407
+ *
408
+ * `0` is meaningful and is passed through as-is — it disables caching rather than
409
+ * falling back to the default.
260
410
  * @optional
261
411
  */
262
412
  maxAge?: number;
413
+ /**
414
+ * If set to true, a symlink inside {@link NodeFSSOptions.dir} may resolve to a file outside
415
+ * of it. By default such files are rejected with `403 IPX_FORBIDDEN_SYMLINK`, since the
416
+ * lexical `../` check alone says nothing about where a path physically ends up.
417
+ *
418
+ * Symlinks that stay inside `dir` are always followed, whatever this is set to.
419
+ *
420
+ * This is **defense in depth**, not a fix for a remotely reachable hole: planting a symlink
421
+ * in the served directory already requires a symlink-create or arbitrary-write primitive
422
+ * there, and the served filesystem is assumed to be trusted. Comparable static file servers
423
+ * (`express.static`, nginx with `disable_symlinks off`) follow symlinks by default too.
424
+ *
425
+ * Enable it for the deployments where symlinking assets into the served directory is
426
+ * deliberate — monorepo `public/` layouts, CI artifact linking, Docker layer tricks.
427
+ * @optional
428
+ * @default false
429
+ */
430
+ allowSymlinksOutsideDir?: boolean;
263
431
  };
264
432
  /**
265
433
  * Creates a file system storage handler for IPX that allows images to be served from local directories specified in the options.
@@ -287,4 +455,4 @@ type UnstorageIPXStorageOptions = {
287
455
  * @throws {H3Error} If there is a problem retrieving or converting the storage data, detailed error information is thrown. See {@link H3Error}.
288
456
  */
289
457
  declare function unstorageToIPXStorage(storage: Storage | Driver, _options?: UnstorageIPXStorageOptions | string): IPXStorage;
290
- export { type HTTPStorageOptions, type Handler, type HandlerContext, type IPX, type IPXModifiers, type IPXOptions, type IPXStorage, type IPXStorageMeta, type IPXStorageOptions, type NodeFSSOptions, type UnstorageIPXStorageOptions, createIPX, createIPXFetchHandler, createIPXNodeHandler, ipxFSStorage, ipxHttpStorage, serveIPX, unstorageToIPXStorage };
458
+ export { HTTPError, type HTTPStorageOptions, type Handler, type HandlerContext, type IPX, type IPXHandlerOptions, type IPXModifiers, type IPXOptions, type IPXParsedURL, type IPXStorage, type IPXStorageMeta, type IPXStorageOptions, type IPXURLParser, type NodeFSSOptions, type UnstorageIPXStorageOptions, createIPX, createIPXFetchHandler, createIPXNodeHandler, ipxFSStorage, ipxHttpStorage, parseIPXURL, serveIPX, unstorageToIPXStorage };
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { HTTPError } from "./_chunks/libs/h3.mjs";
2
- import { createIPX, createIPXFetchHandler, createIPXNodeHandler, ipxFSStorage, ipxHttpStorage, serveIPX } from "./_chunks/node-fs.mjs";
2
+ import { createIPX, createIPXFetchHandler, createIPXNodeHandler, ipxFSStorage, ipxHttpStorage, parseIPXURL, serveIPX } from "./_chunks/node-fs.mjs";
3
3
  function unstorageToIPXStorage(storage, _options = {}) {
4
4
  const options = typeof _options === "string" ? { prefix: _options } : _options;
5
5
  const resolveKey = (id) => options.prefix ? `${options.prefix}:${id}` : id;
@@ -29,4 +29,4 @@ function unstorageToIPXStorage(storage, _options = {}) {
29
29
  }
30
30
  };
31
31
  }
32
- export { createIPX, createIPXFetchHandler, createIPXNodeHandler, ipxFSStorage, ipxHttpStorage, serveIPX, unstorageToIPXStorage };
32
+ export { HTTPError, createIPX, createIPXFetchHandler, createIPXNodeHandler, ipxFSStorage, ipxHttpStorage, parseIPXURL, serveIPX, unstorageToIPXStorage };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ipx",
3
- "version": "4.0.0-alpha.2",
3
+ "version": "4.0.0-beta.1",
4
4
  "description": "High performance, secure and easy-to-use image optimizer.",
5
5
  "repository": "unjs/ipx",
6
6
  "license": "MIT",
@@ -19,40 +19,40 @@
19
19
  ],
20
20
  "scripts": {
21
21
  "build": "obuild",
22
+ "gen:operations": "node ./scripts/gen-operations.ts",
22
23
  "ipx": "node ./src/cli.ts",
23
- "lint": "eslint . && prettier -c src test",
24
- "lint:fix": "automd && eslint . --fix && prettier -w src test",
24
+ "lint": "eslint . && prettier -c src test scripts automd.config.ts",
25
+ "lint:fix": "automd && eslint . --fix && prettier -w src test scripts automd.config.ts",
25
26
  "prepack": "pnpm build",
26
- "release": "pnpm test && pnpm build && changelogen --release --prerelease --publish --publishTag alpha --push",
27
+ "release": "pnpm test && pnpm build && changelogen --release --prerelease --push && npm publish --tag latest",
27
28
  "start": "node bin/ipx.js",
28
29
  "test": "pnpm lint && pnpm test:types && vitest run --coverage",
29
30
  "test:types": "tsc --noEmit"
30
31
  },
31
32
  "dependencies": {
32
- "pathe": "^2.0.3",
33
- "sharp": "^0.35.2",
34
- "srvx": "^0.11.17"
33
+ "sharp": "^0.35.3",
34
+ "srvx": "^0.12.4"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@fastify/accept-negotiator": "^2.0.1",
38
38
  "@types/etag": "^1.8.4",
39
39
  "@types/is-valid-path": "^0.1.2",
40
- "@vitest/coverage-v8": "^4.1.9",
40
+ "@vitest/coverage-v8": "^4.1.10",
41
41
  "automd": "^0.4.3",
42
42
  "changelogen": "^0.6.2",
43
- "eslint": "^10.5.0",
43
+ "eslint": "^10.8.0",
44
44
  "eslint-config-unjs": "^0.6.2",
45
45
  "etag": "^1.8.1",
46
- "h3": "2.0.1-rc.22",
46
+ "h3": "2.0.1-rc.26",
47
47
  "image-meta": "^0.2.2",
48
- "obuild": "^0.4.37",
49
- "oxc-minify": "^0.137.0",
50
- "prettier": "^3.8.4",
51
- "svgo": "^4.0.1",
52
- "typescript": "^6.0.3",
48
+ "obuild": "^0.4.38",
49
+ "oxc-minify": ">=0.141.0",
50
+ "prettier": "^3.9.6",
51
+ "svgo": "^4.0.2",
52
+ "typescript": "^6",
53
53
  "ufo": "^1.6.4",
54
54
  "unstorage": "^1.17.5",
55
- "vitest": "^4.1.9"
55
+ "vitest": "^4.1.10"
56
56
  },
57
57
  "peerDependencies": {
58
58
  "unstorage": "*"
@@ -62,5 +62,8 @@
62
62
  "optional": true
63
63
  }
64
64
  },
65
- "packageManager": "pnpm@11.9.0"
65
+ "engines": {
66
+ "node": "^20.16.0 || >=22.3.0"
67
+ },
68
+ "packageManager": "pnpm@11.17.0"
66
69
  }