ipx 4.0.0-alpha.2 → 4.0.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.
- package/README.md +505 -77
- package/dist/THIRD-PARTY-LICENSES.md.gz +0 -0
- package/dist/_chunks/libs/@fastify/accept-negotiator.mjs +23 -24
- package/dist/_chunks/libs/crossws.d.mts +1 -0
- package/dist/_chunks/libs/css-select.mjs +1078 -938
- package/dist/_chunks/libs/css-tree.mjs +175 -297
- package/dist/_chunks/libs/csso.mjs +65 -110
- package/dist/_chunks/libs/etag.mjs +1 -1
- package/dist/_chunks/libs/h3.d.mts +3 -0
- package/dist/_chunks/libs/h3.mjs +138 -95
- package/dist/_chunks/libs/image-meta.d.mts +2 -6
- package/dist/_chunks/libs/image-meta.mjs +465 -306
- package/dist/_chunks/libs/sax.mjs +82 -80
- package/dist/_chunks/libs/svgo.mjs +970 -1013
- package/dist/_chunks/libs/ufo.mjs +17 -24
- package/dist/_chunks/node-fs.mjs +1100 -200
- package/dist/_chunks/rolldown-runtime.mjs +2 -11
- package/dist/cli.d.mts +1 -1
- package/dist/cli.mjs +4 -3
- package/dist/index.d.mts +242 -21
- package/dist/index.mjs +2 -2
- package/package.json +32 -28
- package/dist/THIRD-PARTY-LICENSES.md +0 -539
package/README.md
CHANGED
|
@@ -9,37 +9,383 @@
|
|
|
9
9
|
|
|
10
10
|
High performance, secure and easy-to-use image optimizer powered by [sharp](https://github.com/lovell/sharp) and [svgo](https://github.com/svg/svgo).
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
Point IPX at a directory or a list of allowed domains, and every image is available in any size, format and quality straight from its URL:
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
```
|
|
15
|
+
/w_512,f_webp/photos/buffalo.png
|
|
16
|
+
```
|
|
15
17
|
|
|
16
18
|
> [!NOTE]
|
|
17
|
-
> This is the active development branch for IPX v4. Check out [v3](https://github.com/unjs/ipx/tree/v3) for v3 docs.
|
|
18
|
-
|
|
19
|
-
- The server creation APIs have changed. See the Programmatic API section for examples.
|
|
20
|
-
- The JSON error format has changed from `{ error: string }` to `{ status, statusText, message }`.
|
|
19
|
+
> This is the active development branch for IPX v4. Check out [v3](https://github.com/unjs/ipx/tree/v3) for v3 docs, and release notes ([alpha](https://github.com/unjs/ipx/releases/tag/v4.0.0-alpha.1), [beta](https://github.com/unjs/ipx/releases/tag/v4.0.0-beta.1)) if you are upgrading.
|
|
21
20
|
|
|
22
|
-
##
|
|
21
|
+
## Quick Start
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
Using `npx`:
|
|
23
|
+
Start a server for the images in the current directory with the `ipx` command:
|
|
27
24
|
|
|
28
25
|
```bash
|
|
29
26
|
npx ipx serve --dir ./
|
|
30
27
|
```
|
|
31
28
|
|
|
32
|
-
Using `bun
|
|
29
|
+
Using `bun`:
|
|
33
30
|
|
|
34
31
|
```bash
|
|
35
32
|
bunx ipx serve --dir ./
|
|
36
33
|
```
|
|
37
34
|
|
|
38
|
-
|
|
35
|
+
Then open the printed URL and add modifiers to any image path, for example `http://localhost:3000/w_200/buffalo.png`.
|
|
36
|
+
|
|
37
|
+
To embed IPX in your own app instead, see the [Programmatic API](#programmatic-api).
|
|
38
|
+
|
|
39
|
+
## Image URLs
|
|
40
|
+
|
|
41
|
+
A request URL is a list of modifiers, followed by the id of the source image:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
/<modifiers>/<id>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Multiple modifiers are separated by `,` and their arguments by `_`. Use `_` alone when no modifier is needed.
|
|
48
|
+
|
|
49
|
+
| URL | Result |
|
|
50
|
+
| -------------------------------------------------- | ------------------------------------------------------------------------------------------- |
|
|
51
|
+
| `/_/static/buffalo.png` | The original image. |
|
|
52
|
+
| `/w_200/static/buffalo.png` | Width set to `200`, original format (`png`) kept. |
|
|
53
|
+
| `/f_webp/static/buffalo.png` | Format changed to `webp`, everything else kept as in the source. |
|
|
54
|
+
| `/f_auto/static/buffalo.png` | Best format for the client (avif/webp/jpeg), negotiated from the browser's `accept` header. |
|
|
55
|
+
| `/s_200x200,fit_contain,f_webp/static/buffalo.png` | Resized to fit inside `200x200px` on a background canvas and converted to `webp`. |
|
|
56
|
+
|
|
57
|
+
The URL style is configurable, see [Custom URL Style](#custom-url-style).
|
|
58
|
+
|
|
59
|
+
## Modifiers
|
|
60
|
+
|
|
61
|
+
Modifier arguments are separated with `_` and validated before they reach sharp. Invalid input is rejected with a `400 IPX_INVALID_MODIFIER_ARG` (or `400 IPX_MISSING_MODIFIER_ARG` for a required argument) instead of failing the request. Some arguments can only be validated by libvips once it runs, which happens after the whole pipeline is set up; those surface as a `400 IPX_INVALID_MODIFIER`. Trailing arguments may be omitted to keep the sharp default, except where noted.
|
|
62
|
+
|
|
63
|
+
Colours (`background`, `tint`) accept any colour sharp understands: hex (`f00`, `ff0000`, `ff000080`) with an optional leading `#` — which cannot be used inside a URL path, so it may be dropped — or a CSS colour name (`red`). Boolean arguments accept `true` / `false` as well as the shorter `1` / `0`.
|
|
64
|
+
|
|
65
|
+
<!-- automd:ipx-operations -->
|
|
66
|
+
|
|
67
|
+
<table>
|
|
68
|
+
<tr>
|
|
69
|
+
<td valign="top" align="left" width="50%">
|
|
70
|
+
<div align="center"><img src="./assets/operations/width.jpg?v=db01a531" width="160" alt="/w_160/scene.jpg"></div>
|
|
71
|
+
<div><b><code>width</code> / <code>w</code></b></div>
|
|
72
|
+
<code>/w_160/scene.jpg</code>
|
|
73
|
+
<br>
|
|
74
|
+
Resize to a width in pixels, a positive integer. The height follows the aspect ratio. (<a href="https://sharp.pixelplumbing.com/api-resize#resize">docs</a>)
|
|
75
|
+
</td>
|
|
76
|
+
<td valign="top" align="left" width="50%">
|
|
77
|
+
<div align="center"><img src="./assets/operations/height.jpg?v=ee67aac0" width="180" alt="/h_120/scene.jpg"></div>
|
|
78
|
+
<div><b><code>height</code> / <code>h</code></b></div>
|
|
79
|
+
<code>/h_120/scene.jpg</code>
|
|
80
|
+
<br>
|
|
81
|
+
Resize to a height in pixels, a positive integer. The width follows the aspect ratio. (<a href="https://sharp.pixelplumbing.com/api-resize#resize">docs</a>)
|
|
82
|
+
</td>
|
|
83
|
+
</tr>
|
|
84
|
+
<tr>
|
|
85
|
+
<td valign="top" align="left" width="50%">
|
|
86
|
+
<div align="center"><img src="./assets/operations/resize.jpg?v=99dda472" width="200" alt="/s_200x200/scene.jpg"></div>
|
|
87
|
+
<div><b><code>resize</code> / <code>s</code></b></div>
|
|
88
|
+
<code>/s_200x200/scene.jpg</code>
|
|
89
|
+
<br>
|
|
90
|
+
Resize to <code>{width}x{height}</code>, both positive integers. A single value (<code>s_200</code>) is a square. (<a href="https://sharp.pixelplumbing.com/api-resize#resize">docs</a>)
|
|
91
|
+
</td>
|
|
92
|
+
<td valign="top" align="left" width="50%">
|
|
93
|
+
<div align="center"><img src="./assets/operations/kernel.jpg?v=96e57f6e" width="80" alt="/s_80x80,kernel_nearest/photo.jpg"></div>
|
|
94
|
+
<div><b><code>kernel</code></b></div>
|
|
95
|
+
<code>/s_80x80,kernel_nearest/photo.jpg</code>
|
|
96
|
+
<br>
|
|
97
|
+
Sets <code>kernel</code> option for <code>resize</code>. One of <code>nearest</code>, <code>linear</code>, <code>cubic</code>, <code>mitchell</code>, <code>lanczos2</code>, <code>lanczos3</code> (default), <code>mks2013</code> or <code>mks2021</code>. (<a href="https://sharp.pixelplumbing.com/api-resize#resize">docs</a>)
|
|
98
|
+
</td>
|
|
99
|
+
</tr>
|
|
100
|
+
<tr>
|
|
101
|
+
<td valign="top" align="left" width="50%">
|
|
102
|
+
<div align="center"><img src="./assets/operations/fit.jpg?v=08b8660c" width="300" alt="/s_300x150,fit_contain,b_00ff00/scene.jpg"></div>
|
|
103
|
+
<div><b><code>fit</code></b></div>
|
|
104
|
+
<code>/s_300x150,fit_contain,b_00ff00/scene.jpg</code>
|
|
105
|
+
<br>
|
|
106
|
+
Sets <code>fit</code> option for <code>resize</code>. One of <code>contain</code>, <code>cover</code> (default), <code>fill</code>, <code>inside</code> or <code>outside</code>. (<a href="https://sharp.pixelplumbing.com/api-resize#resize">docs</a>)
|
|
107
|
+
</td>
|
|
108
|
+
<td valign="top" align="left" width="50%">
|
|
109
|
+
<div align="center"><img src="./assets/operations/position.jpg?v=187fe5e7" width="107" alt="/s_150x300,pos_top/scene.jpg"></div>
|
|
110
|
+
<div><b><code>position</code> / <code>pos</code></b></div>
|
|
111
|
+
<code>/s_150x300,pos_top/scene.jpg</code>
|
|
112
|
+
<br>
|
|
113
|
+
Sets <code>position</code> option for <code>resize</code>. A position (<code>top</code>, <code>right top</code>, ..., <code>left top</code>), gravity (<code>north</code>, <code>northeast</code>, ..., <code>center</code>) or strategy (<code>entropy</code>, <code>attention</code>), also accepted as their numeric constants. Since <code>_</code> separates arguments, multi-word values use <code>-</code> (<code>pos_right-top</code>). (<a href="https://sharp.pixelplumbing.com/api-resize#resize">docs</a>)
|
|
114
|
+
</td>
|
|
115
|
+
</tr>
|
|
116
|
+
<tr>
|
|
117
|
+
<td valign="top" align="left" width="50%">
|
|
118
|
+
<div align="center"><img src="./assets/operations/extend.jpg?v=0dfb2098" width="320" alt="/extend_20_40_20_40_mirror/scene.jpg"></div>
|
|
119
|
+
<div><b><code>extend</code></b></div>
|
|
120
|
+
<code>/extend_20_40_20_40_mirror/scene.jpg</code>
|
|
121
|
+
<br>
|
|
122
|
+
Pad or extrude the edges, as <code>extend_{top}_{right}_{bottom}_{left}</code> with an optional <code>{extendWith}</code>. Edges are integers between <code>0</code> and <code>10000</code>. <code>extendWith</code> is <code>background</code> (default), <code>copy</code>, <code>repeat</code> or <code>mirror</code>; <code>background</code> takes its colour from the <code>background</code> / <code>b</code> modifier. (<a href="https://sharp.pixelplumbing.com/api-resize#extend">docs</a>)
|
|
123
|
+
</td>
|
|
124
|
+
<td valign="top" align="left" width="50%">
|
|
125
|
+
<div align="center"><img src="./assets/operations/background.jpg?v=6b197635" width="320" alt="/rotate_45,b_00ff00/scene.jpg"></div>
|
|
126
|
+
<div><b><code>background</code> / <code>b</code></b></div>
|
|
127
|
+
<code>/rotate_45,b_00ff00/scene.jpg</code>
|
|
128
|
+
<br>
|
|
129
|
+
Background colour, a hex (<code>f00</code>, <code>ff0000</code>) or named (<code>red</code>) colour. Used by <code>extend</code>, <code>rotate</code>, <code>flatten</code>, <code>opacity</code> and <code>resize</code> (with <code>fit_contain</code>).
|
|
130
|
+
</td>
|
|
131
|
+
</tr>
|
|
132
|
+
<tr>
|
|
133
|
+
<td valign="top" align="left" width="50%">
|
|
134
|
+
<div align="center"><img src="./assets/operations/extract.jpg?v=ac75bdaa" width="160" alt="/extract_150_10_160_120/scene.jpg"></div>
|
|
135
|
+
<div><b><code>extract</code> / <code>crop</code></b></div>
|
|
136
|
+
<code>/extract_150_10_160_120/scene.jpg</code>
|
|
137
|
+
<br>
|
|
138
|
+
Crop a region, as <code>extract_{left}_{top}_{width}_{height}</code>. All four arguments are required; <code>width</code> and <code>height</code> must be positive. (<a href="https://sharp.pixelplumbing.com/api-resize#extract">docs</a>)
|
|
139
|
+
</td>
|
|
140
|
+
<td valign="top" align="left" width="50%">
|
|
141
|
+
<div align="center"><img src="./assets/operations/format.webp?v=90841432" width="320" alt="/f_webp/photo.jpg"></div>
|
|
142
|
+
<div><b><code>format</code> / <code>f</code></b></div>
|
|
143
|
+
<code>/f_webp/photo.jpg</code>
|
|
144
|
+
<br>
|
|
145
|
+
Output format. One of <code>jpg</code>, <code>jpeg</code>, <code>png</code>, <code>webp</code>, <code>avif</code>, <code>gif</code>, <code>heif</code>, <code>tiff</code> or <code>auto</code> (experimental, only with middleware). (<a href="https://sharp.pixelplumbing.com/api-output#toformat">docs</a>)
|
|
146
|
+
</td>
|
|
147
|
+
</tr>
|
|
148
|
+
<tr>
|
|
149
|
+
<td valign="top" align="left" width="50%">
|
|
150
|
+
<div align="center"><img src="./assets/operations/quality.jpg?v=865c529f" width="320" alt="/q_10/photo.jpg"></div>
|
|
151
|
+
<div><b><code>quality</code> / <code>q</code></b></div>
|
|
152
|
+
<code>/q_10/photo.jpg</code>
|
|
153
|
+
<br>
|
|
154
|
+
Encoding quality, an integer between <code>1</code> and <code>100</code>. A lower value means a smaller file.
|
|
155
|
+
</td>
|
|
156
|
+
<td valign="top" align="left" width="50%">
|
|
157
|
+
<div align="center"><img src="./assets/operations/rotate.jpg?v=4bc4f0b2" width="320" alt="/rotate_45/scene.jpg"></div>
|
|
158
|
+
<div><b><code>rotate</code></b></div>
|
|
159
|
+
<code>/rotate_45/scene.jpg</code>
|
|
160
|
+
<br>
|
|
161
|
+
Angle in degrees, between <code>-3600</code> and <code>3600</code>. Angles that are not a multiple of <code>90</code> fill the corners with the <code>background</code> / <code>b</code> colour. Without an angle (<code>rotate</code>) the image is auto-oriented from its EXIF tag. (<a href="https://sharp.pixelplumbing.com/api-operation#rotate">docs</a>)
|
|
162
|
+
</td>
|
|
163
|
+
</tr>
|
|
164
|
+
<tr>
|
|
165
|
+
<td valign="top" align="left" width="50%">
|
|
166
|
+
<div align="center"><img src="./assets/operations/enlarge.jpg?v=9c6fb296" width="320" alt="/enlarge,s_400x400/photo.jpg"></div>
|
|
167
|
+
<div><b><code>enlarge</code></b></div>
|
|
168
|
+
<code>/enlarge,s_400x400/photo.jpg</code>
|
|
169
|
+
<br>
|
|
170
|
+
Allow the image to be upscaled (<code>enlarge_false</code> turns it back off). Without it, the source is never upscaled: a requested box that is too large is shrunk, preserving its aspect ratio, so the output is never larger than the source, except along the padded axis of <code>fit_contain</code>.
|
|
171
|
+
</td>
|
|
172
|
+
<td valign="top" align="left" width="50%">
|
|
173
|
+
<div align="center"><img src="./assets/operations/flip.jpg?v=40a31bca" width="320" alt="/flip/scene.jpg"></div>
|
|
174
|
+
<div><b><code>flip</code></b></div>
|
|
175
|
+
<code>/flip/scene.jpg</code>
|
|
176
|
+
<br>
|
|
177
|
+
Mirror the image vertically, about the horizontal axis. Combine with <code>flop</code> to turn it upside down. (<a href="https://sharp.pixelplumbing.com/api-operation#flip">docs</a>)
|
|
178
|
+
</td>
|
|
179
|
+
</tr>
|
|
180
|
+
<tr>
|
|
181
|
+
<td valign="top" align="left" width="50%">
|
|
182
|
+
<div align="center"><img src="./assets/operations/flop.jpg?v=747fa554" width="320" alt="/flop/scene.jpg"></div>
|
|
183
|
+
<div><b><code>flop</code></b></div>
|
|
184
|
+
<code>/flop/scene.jpg</code>
|
|
185
|
+
<br>
|
|
186
|
+
Mirror the image horizontally, about the vertical axis. Combine with <code>flip</code> to turn it upside down. (<a href="https://sharp.pixelplumbing.com/api-operation#flop">docs</a>)
|
|
187
|
+
</td>
|
|
188
|
+
<td valign="top" align="left" width="50%">
|
|
189
|
+
<div align="center"><img src="./assets/operations/sharpen.jpg?v=eafda8ec" width="320" alt="/sharpen_5/photo.jpg"></div>
|
|
190
|
+
<div><b><code>sharpen</code></b></div>
|
|
191
|
+
<code>/sharpen_5/photo.jpg</code>
|
|
192
|
+
<br>
|
|
193
|
+
Sharpen the image, as <code>{sigma}_{flat}_{jagged}_{x1}_{y2}_{y3}</code>. <code>sigma</code> is a number between <code>0.000001</code> and <code>10</code>, the rest between <code>0</code> and <code>1000000</code>. Without arguments (<code>sharpen</code>) a mild sharpen is applied. (<a href="https://sharp.pixelplumbing.com/api-operation#sharpen">docs</a>)
|
|
194
|
+
</td>
|
|
195
|
+
</tr>
|
|
196
|
+
<tr>
|
|
197
|
+
<td valign="top" align="left" width="50%">
|
|
198
|
+
<div align="center"><img src="./assets/operations/median.jpg?v=2317e13b" width="320" alt="/median_10/photo.jpg"></div>
|
|
199
|
+
<div><b><code>median</code></b></div>
|
|
200
|
+
<code>/median_10/photo.jpg</code>
|
|
201
|
+
<br>
|
|
202
|
+
Apply a median filter, removing noise while keeping edges. Square mask size, an integer between <code>1</code> and <code>1000</code> (defaults to <code>3</code>). (<a href="https://sharp.pixelplumbing.com/api-operation#median">docs</a>)
|
|
203
|
+
</td>
|
|
204
|
+
<td valign="top" align="left" width="50%">
|
|
205
|
+
<div align="center"><img src="./assets/operations/blur.jpg?v=565d775a" width="320" alt="/blur_5/edges.jpg"></div>
|
|
206
|
+
<div><b><code>blur</code></b></div>
|
|
207
|
+
<code>/blur_5/edges.jpg</code>
|
|
208
|
+
<br>
|
|
209
|
+
Gaussian blur, as <code>{sigma}_{precision}_{minAmplitude}</code>. <code>sigma</code> is a number between <code>0.3</code> and <code>1000</code>, <code>precision</code> one of <code>integer</code> (default), <code>float</code> or <code>approximate</code>, <code>minAmplitude</code> between <code>0.001</code> and <code>1</code>. Without arguments (<code>blur</code>) a mild blur is applied. (<a href="https://sharp.pixelplumbing.com/api-operation#blur">docs</a>)
|
|
210
|
+
</td>
|
|
211
|
+
</tr>
|
|
212
|
+
<tr>
|
|
213
|
+
<td valign="top" align="left" width="50%">
|
|
214
|
+
<div align="center"><img src="./assets/operations/dilate.png?v=17bf0c5d" width="320" alt="/dilate_4/graphic.png"></div>
|
|
215
|
+
<div><b><code>dilate</code></b></div>
|
|
216
|
+
<code>/dilate_4/graphic.png</code>
|
|
217
|
+
<br>
|
|
218
|
+
Expand foreground objects. Width in pixels, an integer between <code>1</code> and <code>100</code> (defaults to <code>1</code>), capped below the sharp maximum since the cost grows with the width. (<a href="https://sharp.pixelplumbing.com/api-operation#dilate">docs</a>)
|
|
219
|
+
</td>
|
|
220
|
+
<td valign="top" align="left" width="50%">
|
|
221
|
+
<div align="center"><img src="./assets/operations/erode.png?v=b5c7e49a" width="320" alt="/erode_4/graphic.png"></div>
|
|
222
|
+
<div><b><code>erode</code></b></div>
|
|
223
|
+
<code>/erode_4/graphic.png</code>
|
|
224
|
+
<br>
|
|
225
|
+
Shrink foreground objects. Width in pixels, an integer between <code>1</code> and <code>100</code> (defaults to <code>1</code>), capped below the sharp maximum since the cost grows with the width. (<a href="https://sharp.pixelplumbing.com/api-operation#erode">docs</a>)
|
|
226
|
+
</td>
|
|
227
|
+
</tr>
|
|
228
|
+
<tr>
|
|
229
|
+
<td valign="top" align="left" width="50%">
|
|
230
|
+
<div align="center"><img src="./assets/operations/clahe.jpg?v=0aa709f5" width="320" alt="/clahe_20_20_5/edges.jpg"></div>
|
|
231
|
+
<div><b><code>clahe</code></b></div>
|
|
232
|
+
<code>/clahe_20_20_5/edges.jpg</code>
|
|
233
|
+
<br>
|
|
234
|
+
Contrast limiting adaptive histogram equalization, as <code>{width}_{height}_{maxSlope}</code>. <code>width</code> is required and <code>height</code> defaults to it (a square window); both are integers between <code>1</code> and <code>100</code>, clamped to the source dimensions. <code>maxSlope</code> is an integer between <code>0</code> and <code>100</code> (defaults to <code>3</code>). (<a href="https://sharp.pixelplumbing.com/api-operation#clahe">docs</a>)
|
|
235
|
+
</td>
|
|
236
|
+
<td valign="top" align="left" width="50%">
|
|
237
|
+
<div align="center"><img src="./assets/operations/flatten.jpg?v=02455cd1" width="320" alt="/flatten,b_00ff00,f_jpeg/alpha.png"></div>
|
|
238
|
+
<div><b><code>flatten</code></b></div>
|
|
239
|
+
<code>/flatten,b_00ff00,f_jpeg/alpha.png</code>
|
|
240
|
+
<br>
|
|
241
|
+
Remove the alpha channel, if any, and replace transparency with the <code>background</code> / <code>b</code> colour. (<a href="https://sharp.pixelplumbing.com/api-operation#flatten">docs</a>)
|
|
242
|
+
</td>
|
|
243
|
+
</tr>
|
|
244
|
+
<tr>
|
|
245
|
+
<td valign="top" align="left" width="50%">
|
|
246
|
+
<div align="center"><img src="./assets/operations/unflatten.webp?v=db6b8455" width="320" alt="/unflatten,f_webp/graphic.png"></div>
|
|
247
|
+
<div><b><code>unflatten</code></b></div>
|
|
248
|
+
<code>/unflatten,f_webp/graphic.png</code>
|
|
249
|
+
<br>
|
|
250
|
+
Make every fully white pixel transparent, so the output format needs an alpha channel (<code>png</code>, <code>webp</code>, <code>avif</code>). (<a href="https://sharp.pixelplumbing.com/api-operation#unflatten">docs</a>)
|
|
251
|
+
</td>
|
|
252
|
+
<td valign="top" align="left" width="50%">
|
|
253
|
+
<div align="center"><img src="./assets/operations/gamma.jpg?v=ec2a179b" width="320" alt="/gamma_3/colour.jpg"></div>
|
|
254
|
+
<div><b><code>gamma</code></b></div>
|
|
255
|
+
<code>/gamma_3/colour.jpg</code>
|
|
256
|
+
<br>
|
|
257
|
+
Gamma correction, as <code>{gamma}_{gammaOut}</code>, each a number between <code>1.0</code> and <code>3.0</code> (defaults to <code>2.2</code>). (<a href="https://sharp.pixelplumbing.com/api-operation#gamma">docs</a>)
|
|
258
|
+
</td>
|
|
259
|
+
</tr>
|
|
260
|
+
<tr>
|
|
261
|
+
<td valign="top" align="left" width="50%">
|
|
262
|
+
<div align="center"><img src="./assets/operations/negate.jpg?v=6dcd6c51" width="320" alt="/negate/colour.jpg"></div>
|
|
263
|
+
<div><b><code>negate</code></b></div>
|
|
264
|
+
<code>/negate/colour.jpg</code>
|
|
265
|
+
<br>
|
|
266
|
+
Produce the negative of the image. Optional <code>{alpha}</code> (<code>true</code> by default) controls whether the alpha channel is negated too, e.g. <code>negate_false</code>. (<a href="https://sharp.pixelplumbing.com/api-operation#negate">docs</a>)
|
|
267
|
+
</td>
|
|
268
|
+
<td valign="top" align="left" width="50%">
|
|
269
|
+
<div align="center"><img src="./assets/operations/normalize.jpg?v=7ab13cca" width="320" alt="/normalize_10_90/edges.jpg"></div>
|
|
270
|
+
<div><b><code>normalize</code></b></div>
|
|
271
|
+
<code>/normalize_10_90/edges.jpg</code>
|
|
272
|
+
<br>
|
|
273
|
+
Stretch the luminance to the full dynamic range, as <code>{lower}_{upper}</code> percentiles. <code>lower</code> is a number between <code>0</code> and <code>99</code> (defaults to <code>1</code>), <code>upper</code> between <code>1</code> and <code>100</code> (defaults to <code>99</code>) and greater than <code>lower</code>. (<a href="https://sharp.pixelplumbing.com/api-operation#normalize">docs</a>)
|
|
274
|
+
</td>
|
|
275
|
+
</tr>
|
|
276
|
+
<tr>
|
|
277
|
+
<td valign="top" align="left" width="50%">
|
|
278
|
+
<div align="center"><img src="./assets/operations/threshold.jpg?v=e870d914" width="320" alt="/threshold_128/colour.jpg"></div>
|
|
279
|
+
<div><b><code>threshold</code></b></div>
|
|
280
|
+
<code>/threshold_128/colour.jpg</code>
|
|
281
|
+
<br>
|
|
282
|
+
Map every pixel to black or white, as <code>{threshold}_{grayscale}</code>. <code>threshold</code> is an integer between <code>0</code> and <code>255</code> (defaults to <code>128</code>). Optional <code>grayscale</code> (<code>true</code> by default) converts to single channel first, e.g. <code>threshold_128_false</code>. (<a href="https://sharp.pixelplumbing.com/api-operation#threshold">docs</a>)
|
|
283
|
+
</td>
|
|
284
|
+
<td valign="top" align="left" width="50%">
|
|
285
|
+
<div align="center"><img src="./assets/operations/linear.jpg?v=04090af0" width="320" alt="/linear_1.5_-30/colour.jpg"></div>
|
|
286
|
+
<div><b><code>linear</code></b></div>
|
|
287
|
+
<code>/linear_1.5_-30/colour.jpg</code>
|
|
288
|
+
<br>
|
|
289
|
+
Levels adjustment applying <code>a * input + b</code>, as <code>{a}_{b}</code>. <code>a</code> is the multiplier (defaults to <code>1</code>) and <code>b</code> the offset (defaults to <code>0</code>), both numbers. (<a href="https://sharp.pixelplumbing.com/api-operation#linear">docs</a>)
|
|
290
|
+
</td>
|
|
291
|
+
</tr>
|
|
292
|
+
<tr>
|
|
293
|
+
<td valign="top" align="left" width="50%">
|
|
294
|
+
<div align="center"><img src="./assets/operations/tint.jpg?v=7a727e4a" width="320" alt="/tint_00ff00/colour.jpg"></div>
|
|
295
|
+
<div><b><code>tint</code></b></div>
|
|
296
|
+
<code>/tint_00ff00/colour.jpg</code>
|
|
297
|
+
<br>
|
|
298
|
+
Tint the image, keeping its luminance. A hex (<code>f00</code>, <code>ff0000</code>) or named (<code>red</code>) colour. (<a href="https://sharp.pixelplumbing.com/api-colour#tint">docs</a>)
|
|
299
|
+
</td>
|
|
300
|
+
<td valign="top" align="left" width="50%">
|
|
301
|
+
<div align="center"><img src="./assets/operations/grayscale.jpg?v=8e4a38ec" width="320" alt="/grayscale/colour.jpg"></div>
|
|
302
|
+
<div><b><code>grayscale</code></b></div>
|
|
303
|
+
<code>/grayscale/colour.jpg</code>
|
|
304
|
+
<br>
|
|
305
|
+
Convert the image to 8-bit greyscale, mapping every pixel to its luminance. (<a href="https://sharp.pixelplumbing.com/api-colour#grayscale">docs</a>)
|
|
306
|
+
</td>
|
|
307
|
+
</tr>
|
|
308
|
+
<tr>
|
|
309
|
+
<td valign="top" align="left" width="50%">
|
|
310
|
+
<div align="center"><img src="./assets/operations/modulate.jpg?v=f1cce46c" width="320" alt="/modulate_1.5_2_90_10/colour.jpg"></div>
|
|
311
|
+
<div><b><code>modulate</code></b></div>
|
|
312
|
+
<code>/modulate_1.5_2_90_10/colour.jpg</code>
|
|
313
|
+
<br>
|
|
314
|
+
Transform the image, as <code>{brightness}_{saturation}_{hue}_{lightness}</code>. <code>brightness</code> and <code>saturation</code> are numbers <code>>= 0</code>, <code>hue</code> an integer in degrees. Each is also available on its own (below). (<a href="https://sharp.pixelplumbing.com/api-operation#modulate">docs</a>)
|
|
315
|
+
</td>
|
|
316
|
+
<td valign="top" align="left" width="50%">
|
|
317
|
+
<div align="center"><img src="./assets/operations/brightness.jpg?v=c79f82bb" width="320" alt="/brightness_1.5/colour.jpg"></div>
|
|
318
|
+
<div><b><code>brightness</code></b></div>
|
|
319
|
+
<code>/brightness_1.5/colour.jpg</code>
|
|
320
|
+
<br>
|
|
321
|
+
Brightness multiplier, a number <code>>= 0</code> (<code>1</code> leaves the image unchanged). Required. (<a href="https://sharp.pixelplumbing.com/api-operation#modulate">docs</a>)
|
|
322
|
+
</td>
|
|
323
|
+
</tr>
|
|
324
|
+
<tr>
|
|
325
|
+
<td valign="top" align="left" width="50%">
|
|
326
|
+
<div align="center"><img src="./assets/operations/saturation.jpg?v=9a395c34" width="320" alt="/saturation_0.3/colour.jpg"></div>
|
|
327
|
+
<div><b><code>saturation</code></b></div>
|
|
328
|
+
<code>/saturation_0.3/colour.jpg</code>
|
|
329
|
+
<br>
|
|
330
|
+
Saturation multiplier, a number <code>>= 0</code> (<code>0</code> is greyscale). Required. (<a href="https://sharp.pixelplumbing.com/api-operation#modulate">docs</a>)
|
|
331
|
+
</td>
|
|
332
|
+
<td valign="top" align="left" width="50%">
|
|
333
|
+
<div align="center"><img src="./assets/operations/hue.jpg?v=8dff0588" width="320" alt="/hue_90/colour.jpg"></div>
|
|
334
|
+
<div><b><code>hue</code></b></div>
|
|
335
|
+
<code>/hue_90/colour.jpg</code>
|
|
336
|
+
<br>
|
|
337
|
+
Hue rotation, an integer in degrees that wraps around at <code>360</code>. Required. (<a href="https://sharp.pixelplumbing.com/api-operation#modulate">docs</a>)
|
|
338
|
+
</td>
|
|
339
|
+
</tr>
|
|
340
|
+
<tr>
|
|
341
|
+
<td valign="top" align="left" width="50%">
|
|
342
|
+
<div align="center"><img src="./assets/operations/lightness.jpg?v=fe72d3a8" width="320" alt="/lightness_30/colour.jpg"></div>
|
|
343
|
+
<div><b><code>lightness</code></b></div>
|
|
344
|
+
<code>/lightness_30/colour.jpg</code>
|
|
345
|
+
<br>
|
|
346
|
+
Lightness addend, a number added to the lightness of every pixel. Required. (<a href="https://sharp.pixelplumbing.com/api-operation#modulate">docs</a>)
|
|
347
|
+
</td>
|
|
348
|
+
<td valign="top" align="left" width="50%">
|
|
349
|
+
<div align="center"><img src="./assets/operations/opacity.webp?v=75d9647c" width="320" alt="/opacity_0.5,f_webp/alpha.png"></div>
|
|
350
|
+
<div><b><code>opacity</code></b></div>
|
|
351
|
+
<code>/opacity_0.5,f_webp/alpha.png</code>
|
|
352
|
+
<br>
|
|
353
|
+
Opacity, a number between <code>0</code> and <code>1</code>. Required. The image is made transparent, so the output format needs an alpha channel (<code>png</code>, <code>webp</code>, <code>avif</code>), or set the <code>background</code> / <code>b</code> colour to blend into it instead, e.g. <code>opacity_0.5,b_fff,f_jpeg</code>.
|
|
354
|
+
</td>
|
|
355
|
+
</tr>
|
|
356
|
+
<tr>
|
|
357
|
+
<td valign="top" align="left" width="50%">
|
|
358
|
+
<div align="center"><img src="./assets/operations/animated.gif?v=e51060bb" width="160" alt="/a,w_160/animated.gif"></div>
|
|
359
|
+
<div><b><code>animated</code> / <code>a</code></b></div>
|
|
360
|
+
<code>/a,w_160/animated.gif</code>
|
|
361
|
+
<br>
|
|
362
|
+
Process every frame of an animated image instead of the first one. Experimental.
|
|
363
|
+
</td>
|
|
364
|
+
<td valign="top" align="left" width="50%">
|
|
365
|
+
<div align="center"><em>No sample: the sample source has no uniform border to trim.</em></div>
|
|
366
|
+
<div><b><code>trim</code></b></div>
|
|
367
|
+
<code>/trim_30/photo.jpg</code>
|
|
368
|
+
<br>
|
|
369
|
+
Trim edge pixels that are within the threshold of the top-left pixel colour. Threshold is a number <code>>= 0</code> (defaults to <code>10</code>). (<a href="https://sharp.pixelplumbing.com/api-resize#trim">docs</a>)
|
|
370
|
+
</td>
|
|
371
|
+
</tr>
|
|
372
|
+
<tr>
|
|
373
|
+
<td valign="top" align="left" width="50%">
|
|
374
|
+
<div align="center"><em>No sample: the sample source carries no EXIF <code>Orientation</code> tag.</em></div>
|
|
375
|
+
<div><b><code>autoorient</code></b></div>
|
|
376
|
+
<code>/autoorient/photo.jpg</code>
|
|
377
|
+
<br>
|
|
378
|
+
Rotate and flip the image according to its EXIF <code>Orientation</code> tag, then remove the tag. Also applied by <code>rotate</code> without an angle. (<a href="https://sharp.pixelplumbing.com/api-operation#autoorient">docs</a>)
|
|
379
|
+
</td>
|
|
380
|
+
<td valign="top" align="left" width="50%"></td>
|
|
381
|
+
</tr>
|
|
382
|
+
</table>
|
|
383
|
+
|
|
384
|
+
<!-- /automd -->
|
|
39
385
|
|
|
40
386
|
## Programmatic API
|
|
41
387
|
|
|
42
|
-
|
|
388
|
+
Create an IPX instance with `createIPX()`, then either serve it directly or mount it as a handler in your own app.
|
|
43
389
|
|
|
44
390
|
**Example:** Using built-in server
|
|
45
391
|
|
|
@@ -124,101 +470,183 @@ app.listen(3000, () => {
|
|
|
124
470
|
|
|
125
471
|
<!-- /automd -->
|
|
126
472
|
|
|
127
|
-
##
|
|
473
|
+
## Config
|
|
128
474
|
|
|
129
|
-
|
|
475
|
+
Every option can also be set universally with an `IPX_*` environment variable, which is how the CLI is configured. Explicit options win over the environment.
|
|
130
476
|
|
|
131
|
-
|
|
477
|
+
### General
|
|
132
478
|
|
|
133
|
-
|
|
479
|
+
| Option | Environment variable | Default | Description |
|
|
480
|
+
| -------------------- | -------------------------- | ------- | --------------------------------------------------------- |
|
|
481
|
+
| `alias` | `IPX_ALIAS` | `{}` | Map URL prefixes to other prefixes or remote origins. |
|
|
482
|
+
| `maxOutputDimension` | `IPX_MAX_OUTPUT_DIMENSION` | `8192` | Maximum width and height (in pixels) of the output image. |
|
|
483
|
+
| `allowedModifiers` | `IPX_ALLOWED_MODIFIERS` | (all) | Modifiers that requests are allowed to use. |
|
|
134
484
|
|
|
135
|
-
`/
|
|
485
|
+
Requested `width`, `height` and `resize` dimensions are clamped to `maxOutputDimension`, preserving the requested aspect ratio, and `extend` edges are clamped so the extended canvas stays within it. This bounds how much memory a single request can allocate: sharp only limits the _input_ size, so without it `/enlarge,s_20000x20000/image.jpg` (or `/extend_10000_10000_10000_10000/image.jpg`) allocates gigabytes from a small source image. Set to `false` to disable, which is only safe when modifiers come from a trusted source.
|
|
136
486
|
|
|
137
|
-
|
|
487
|
+
`allowedModifiers` restricts a public endpoint to the modifiers a site actually uses, for example `allowedModifiers: ["width", "height", "format", "quality"]` or `IPX_ALLOWED_MODIFIERS=width,height,format,quality`. Aliases follow their modifier (allowing `width` also allows `w`). Any other modifier, including an unknown one, is rejected with a `400` before the source is fetched, so expensive operations (`blur`, `median`, ...) cannot be triggered. Only modifier names are checked, not their values: allowing `format` allows every supported output format, including the slow-to-encode `avif`. The same image also stays reachable under many URLs (`?cb=1`, `w_10` vs `w_1e1`, ...), so this does not prevent busting a CDN cache.
|
|
138
488
|
|
|
139
|
-
|
|
489
|
+
### Server (`createIPXFetchHandler`, `createIPXNodeHandler`, `serveIPX`)
|
|
140
490
|
|
|
141
|
-
|
|
491
|
+
| Option | Environment variable | Default | Description |
|
|
492
|
+
| ------------- | -------------------- | ---------------------------------------------------- | ------------------------------------------------------- |
|
|
493
|
+
| `autoFormats` | `IPX_AUTO_FORMATS` | `avif`, `webp`, `jpeg`, `png`, `tiff`, `heif`, `gif` | Formats `f_auto` can pick from, in order of preference. |
|
|
142
494
|
|
|
143
|
-
|
|
495
|
+
`f_auto` serves the format that the client's `accept` header lists with the highest q-value, and equal q-values go to the earlier entry in `autoFormats`. Wildcards such as `image/*` and `*/*` do not match a format, so browsers only negotiate the formats they list explicitly (in practice `avif`, `webp` and `png`). When nothing matches, `jpeg` is served, even if it is not in the list. Animated images only consider `webp` and `gif`, and fall back to `gif`.
|
|
144
496
|
|
|
145
|
-
|
|
497
|
+
AVIF compresses best but is much slower to encode, so leave it out when encoding time matters more than size:
|
|
146
498
|
|
|
147
|
-
|
|
499
|
+
```ts
|
|
500
|
+
createIPXFetchHandler(ipx, { autoFormats: ["webp", "jpeg"] });
|
|
501
|
+
```
|
|
148
502
|
|
|
149
|
-
|
|
503
|
+
With the CLI: `IPX_AUTO_FORMATS=webp,jpeg ipx serve`. Unknown formats throw when the handler is created.
|
|
150
504
|
|
|
151
|
-
|
|
505
|
+
### Filesystem source (`ipxFSStorage`)
|
|
152
506
|
|
|
153
|
-
|
|
154
|
-
- Default: `{}`
|
|
507
|
+
Enabled by default with the CLI only.
|
|
155
508
|
|
|
156
|
-
|
|
509
|
+
| Option | Environment variable | Default | Description |
|
|
510
|
+
| ------------------------- | ----------------------------------- | ----------------------- | ------------------------------------------------------ |
|
|
511
|
+
| `dir` | `IPX_FS_DIR` | `.` (current directory) | Directory (or directories) files are served from. |
|
|
512
|
+
| `maxAge` | `IPX_FS_MAX_AGE` | `60` (via `maxAge`) | `cache-control` max-age, in seconds, for served files. |
|
|
513
|
+
| `allowSymlinksOutsideDir` | `IPX_FS_ALLOW_SYMLINKS_OUTSIDE_DIR` | `false` | Allows symlinks inside `dir` to resolve outside of it. |
|
|
157
514
|
|
|
158
|
-
(
|
|
515
|
+
### HTTP(s) source (`ipxHttpStorage`)
|
|
159
516
|
|
|
160
|
-
|
|
517
|
+
Enabled by default with the CLI only.
|
|
161
518
|
|
|
162
|
-
|
|
519
|
+
| Option | Environment variable | Default | Description |
|
|
520
|
+
| ----------------- | ---------------------------- | ------- | --------------------------------------------------------------------------------------------- |
|
|
521
|
+
| `domains` | `IPX_HTTP_DOMAINS` | `[]` | Allowlist of hostnames images can be fetched from. |
|
|
522
|
+
| `maxAge` | `IPX_HTTP_MAX_AGE` | `300` | `cache-control` max-age, in seconds, when upstream sends none. |
|
|
523
|
+
| `fetchOptions` | `IPX_HTTP_FETCH_OPTIONS` | `{}` | Passed to `fetch()`. |
|
|
524
|
+
| `allowAllDomains` | `IPX_HTTP_ALLOW_ALL_DOMAINS` | `false` | Disables the allowlist. Unsafe on a public server. |
|
|
525
|
+
| `blockPrivateIPs` | `IPX_HTTP_BLOCK_PRIVATE_IPS` | `false` | Rejects hosts that are, or resolve to, a non-public IP address (best effort, see note below). |
|
|
163
526
|
|
|
164
|
-
|
|
527
|
+
Only `http:` and `https:` URLs are allowed (anything else is rejected with `403 IPX_FORBIDDEN_PROTOCOL`), and redirects are followed **only within the allowlist**, up to 3 hops: each redirect target is re-validated and a redirect to a host that is not listed is rejected with `403 IPX_FORBIDDEN_HOST` (`502 IPX_TOO_MANY_REDIRECTS` beyond 3 hops). Previously redirects were followed blindly, which let an allowlisted host with an open redirect bounce IPX to internal addresses such as the cloud metadata service (SSRF). If an allowlisted host redirects to a CDN, add the CDN hostname to the allowlist as well. Redirect re-validation is skipped when `allowAllDomains` is enabled without `blockPrivateIPs` (nothing to validate) or when `redirect` is explicitly set in `fetchOptions`.
|
|
165
528
|
|
|
166
|
-
|
|
529
|
+
#### Blocking private IP addresses
|
|
167
530
|
|
|
168
|
-
|
|
531
|
+
The allowlist matches hostnames, not addresses, so an allowlisted host whose DNS record points at `127.0.0.1`, `169.254.169.254` or an RFC1918 address is fetched like any other. That is intended — allowlisted domains are trusted — but `blockPrivateIPs: true` adds a second line of defense for setups where the allowlist is broad (or `allowAllDomains` is on):
|
|
169
532
|
|
|
170
|
-
|
|
533
|
+
```ts
|
|
534
|
+
ipxHttpStorage({ domains: ["cdn.example.com"], blockPrivateIPs: true });
|
|
535
|
+
```
|
|
171
536
|
|
|
172
|
-
|
|
537
|
+
The host of the requested URL **and of every redirect hop** must be a public address. IP literals are checked directly, hostnames are resolved with the OS resolver (`dns.lookup`, so `/etc/hosts` counts) and _all_ returned addresses must be public. Loopback, `0.0.0.0/8`, RFC1918, CGNAT (`100.64.0.0/10`), link-local (`169.254.0.0/16`, `fe80::/10`), unique-local (`fc00::/7`), multicast, reserved/test ranges and the IPv4-mapped/compatible IPv6 forms of all of them (`::ffff:127.0.0.1` and friends) are rejected with `403 IPX_FORBIDDEN_IP`. A host that cannot be resolved is a `502 IPX_DNS_LOOKUP_FAILED`, and a runtime without `node:net`/`node:dns` a `500 IPX_IP_CHECK_UNAVAILABLE` — the check fails closed rather than quietly turning itself off.
|
|
173
538
|
|
|
174
|
-
-
|
|
539
|
+
It is **off by default**: many deployments legitimately fetch from in-cluster origins (internal object storage, a sidecar, `localhost` in development), and enabling it by default would break them.
|
|
175
540
|
|
|
176
|
-
|
|
541
|
+
> [!WARNING]
|
|
542
|
+
> `blockPrivateIPs` is best effort and does **not** protect against DNS rebinding. The hostname is resolved once to validate it and again when `fetch` opens the socket, so a DNS record the attacker controls (with a very short TTL) can answer with a public address during validation and a private one during the fetch. This matters when the attacker can choose the hostname: with `allowAllDomains`, or an allowlisted domain whose DNS you do not control. If IPX must not reach internal services, enforce it at the network level: run it behind an egress proxy, or a firewall / network policy that blocks private ranges and the cloud metadata service (on AWS, also require IMDSv2).
|
|
177
543
|
|
|
178
|
-
|
|
544
|
+
It is also not a substitute for the allowlist — a public address that happens to be reachable is still fetched.
|
|
179
545
|
|
|
180
|
-
|
|
546
|
+
## SVG Images
|
|
181
547
|
|
|
182
|
-
-
|
|
548
|
+
SVG images are not processed by sharp. They are sanitized, optimized with [svgo](https://github.com/svg/svgo) and served as `image/svg+xml`. Input that is not well-formed XML (an unescaped `&` is a common cause) is rejected with a `400 IPX_INVALID_SVG`.
|
|
183
549
|
|
|
184
|
-
|
|
550
|
+
```ts
|
|
551
|
+
createIPX({
|
|
552
|
+
storage,
|
|
553
|
+
svg: {
|
|
554
|
+
// SVGO config, or `false` to disable optimization
|
|
555
|
+
optimize: { multipass: true },
|
|
556
|
+
// Serve SVG images unsanitized. Only for fully trusted sources!
|
|
557
|
+
unsafeSkipSanitize: false,
|
|
558
|
+
},
|
|
559
|
+
});
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
### Optimization
|
|
563
|
+
|
|
564
|
+
SVGO's `preset-default` is applied unless you configure `plugins` yourself. Output is always a re-serialized document, never byte-identical to the source: ids are renamed, elements that are neither visible nor referenced within the same file are dropped, shapes are converted to paths and `<style>` rules are inlined.
|
|
565
|
+
|
|
566
|
+
That is fine for images used as `<img src>` or as CSS backgrounds, but it can break consumers that reach into the document:
|
|
567
|
+
|
|
568
|
+
- Sprite sheets: a `<symbol id="icon">` with no `<use>` in the same file is removed, so `<use href="/sprite.svg#icon">` renders nothing.
|
|
569
|
+
- References by id from outside the file, since ids are renamed (`icon-home` becomes `a`).
|
|
570
|
+
- Selectors in a host page that inlines the SVG, since `<rect>` becomes `<path>` and class-based rules are inlined.
|
|
571
|
+
|
|
572
|
+
Use `svg: { optimize: false }` to sanitize without optimizing, or keep optimization with the structural plugins disabled (about 1% larger output for typical icons):
|
|
573
|
+
|
|
574
|
+
```ts
|
|
575
|
+
createIPX({
|
|
576
|
+
storage,
|
|
577
|
+
svg: {
|
|
578
|
+
optimize: {
|
|
579
|
+
plugins: [
|
|
580
|
+
{
|
|
581
|
+
name: "preset-default",
|
|
582
|
+
params: {
|
|
583
|
+
overrides: { cleanupIds: false, removeHiddenElems: false },
|
|
584
|
+
},
|
|
585
|
+
},
|
|
586
|
+
],
|
|
587
|
+
},
|
|
588
|
+
},
|
|
589
|
+
});
|
|
590
|
+
```
|
|
185
591
|
|
|
186
|
-
|
|
592
|
+
### Sanitization
|
|
187
593
|
|
|
188
|
-
|
|
594
|
+
SVG documents can carry active content, so IPX **always** sanitizes them before serving. Sanitization is independent of optimization: `svg: { optimize: false }` only disables SVGO's optimization plugins.
|
|
595
|
+
|
|
596
|
+
Removed from every SVG:
|
|
597
|
+
|
|
598
|
+
- `<script>` elements (including namespaced ones such as `<svg:script>`)
|
|
599
|
+
- Event handler attributes (any `on*` attribute)
|
|
600
|
+
- Embedded foreign documents: `<foreignObject>`, `<iframe>`, `<embed>`, `<object>`, `<base>`, `<link>` and `<meta>`
|
|
601
|
+
- Event handler elements: `<handler>` and `<listener>`
|
|
602
|
+
- SMIL animations (`<animate>`, `<animateMotion>`, `<animateTransform>` and `<set>`) that assign an `on*` attribute or an unsafe URI, which could otherwise re-introduce a handler after load
|
|
603
|
+
- URIs (`href`, `xlink:href` and `src`) with a scheme other than `http:`, `https:`, `mailto:`, `tel:`, `ftp:` or a non-SVG `data:image/*` — in particular `javascript:`, including obfuscated variants using entities or control characters
|
|
604
|
+
- The `<!DOCTYPE>` declaration and all processing instructions (`<?…?>`), which are serialized unescaped and can smuggle markup past an HTML parser
|
|
605
|
+
|
|
606
|
+
**External references are kept.** Attributes such as `<image href="https://…">`, `<use href="…">`, external fonts and `@import` inside `<style>` are preserved, since stripping them would break legitimate images. They are not a script execution vector, but they do allow the SVG to make requests to third-party origins (and thereby leak the viewer's IP address) when rendered as a document. If this matters for your threat model, host such images from a separate origin or block the requests with a Content-Security-Policy.
|
|
607
|
+
|
|
608
|
+
The bundled server sends `content-security-policy: default-src 'none'` with successful responses by default, which blocks both script execution and external references in browsers that honor it. Custom servers built on the programmatic API should send the same header, since sanitization cannot cover every future browser behavior on its own.
|
|
609
|
+
|
|
610
|
+
Sanitization can be disabled with `svg: { unsafeSkipSanitize: true }`. Only do this when every source is fully trusted: IPX will then serve SVG images with XSS payloads unchanged.
|
|
611
|
+
|
|
612
|
+
## Custom URL Style
|
|
613
|
+
|
|
614
|
+
The `parseURL` option accepts a function that extracts the resource id and modifiers from the request URL, allowing any URL style you like. It receives the raw (still percent-encoded) request URL, so it is free to decode it however the URL style requires.
|
|
615
|
+
|
|
616
|
+
**Example:** modifiers in the filename (`/<id>@@<modifiers>.<format>`), which can be preferable when prerendering images for static hosting.
|
|
617
|
+
|
|
618
|
+
```ts
|
|
619
|
+
import { createIPXFetchHandler, parseIPXURL } from "ipx";
|
|
620
|
+
|
|
621
|
+
const handler = createIPXFetchHandler(ipx, {
|
|
622
|
+
parseURL(url) {
|
|
623
|
+
const path = decodeURIComponent(new URL(url).pathname.slice(1));
|
|
624
|
+
|
|
625
|
+
const match = path.match(/^(.+)@@(.+)\.([^.]+)$/);
|
|
626
|
+
if (!match) {
|
|
627
|
+
// Not our style, fall back to the default `/<modifiers>/<id>`
|
|
628
|
+
return parseIPXURL(url);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
const [, id = "", modifiersString = "", format = ""] = match;
|
|
632
|
+
const modifiers = Object.fromEntries(
|
|
633
|
+
modifiersString.split(",").map((m) => {
|
|
634
|
+
const [key = "", ...values] = m.split("_");
|
|
635
|
+
return [key, values.join("_")];
|
|
636
|
+
}),
|
|
637
|
+
);
|
|
638
|
+
|
|
639
|
+
return { id, modifiers: { ...modifiers, format } };
|
|
640
|
+
},
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
// http://localhost:3000/static/buffalo.png@@s_200x200.webp
|
|
644
|
+
// http://localhost:3000/static/buffalo.png@@grayscale,w_200.webp
|
|
645
|
+
```
|
|
646
|
+
|
|
647
|
+
The parser may be async, and can throw an `HTTPError` (re-exported from `ipx`) to reject a request with a specific status code.
|
|
189
648
|
|
|
190
|
-
|
|
191
|
-
| -------------- | :-------------------------------------------------------------- | :--------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
192
|
-
| width / w | [Docs](https://sharp.pixelplumbing.com/api-resize#resize) | `/width_200/buffalo.png` or `/w_200/buffalo.png` |
|
|
193
|
-
| height / h | [Docs](https://sharp.pixelplumbing.com/api-resize#resize) | `/height_200/buffalo.png` or `/h_200/buffalo.png` |
|
|
194
|
-
| resize / s | [Docs](https://sharp.pixelplumbing.com/api-resize#resize) | `/s_200x200/buffalo.png` |
|
|
195
|
-
| kernel | [Docs](https://sharp.pixelplumbing.com/api-resize#resize) | `/s_200x200,kernel_nearest/buffalo.png` | Supported kernel: `nearest`, `cubic`, `mitchell`, `lanczos2` and `lanczos3` (default). |
|
|
196
|
-
| fit | [Docs](https://sharp.pixelplumbing.com/api-resize#resize) | `/s_200x200,fit_outside/buffalo.png` | Sets `fit` option for `resize`. |
|
|
197
|
-
| position / pos | [Docs](https://sharp.pixelplumbing.com/api-resize#resize) | `/s_200x200,pos_top/buffalo.png` | Sets `position` option for `resize`. |
|
|
198
|
-
| trim | [Docs](https://sharp.pixelplumbing.com/api-resize#trim) | `/trim_100/buffalo.png` |
|
|
199
|
-
| extend | [Docs](https://sharp.pixelplumbing.com/api-resize#extend) | `/extend_{top}_{right}_{bottom}_{left}/buffalo.png` | Extend / pad / extrude one or more edges of the image with either the provided background colour or pixels derived from the image. |
|
|
200
|
-
| background / b | \_ | `/r_45,b_00ff00/buffalo.png` |
|
|
201
|
-
| extract | [Docs](https://sharp.pixelplumbing.com/api-resize#extract) | `/extract_{left}_{top}_{width}_{height}/buffalo.png` | Extract/crop a region of the image. |
|
|
202
|
-
| crop | [Docs](https://sharp.pixelplumbing.com/api-resize#extract) | `/crop_{left}_{top}_{width}_{height}/buffalo.png` | Alias for extract. Extract/crop a region of the image. |
|
|
203
|
-
| format / f | [Docs](https://sharp.pixelplumbing.com/api-output#toformat) | `/format_webp/buffalo.png` or `/f_webp/buffalo.png` | Supported format: `jpg`, `jpeg`, `png`, `webp`, `avif`, `gif`, `heif`, `tiff` and `auto` (experimental only with middleware) |
|
|
204
|
-
| quality / q | \_ | `/quality_50/buffalo.png` or `/q_50/buffalo.png` | Accepted values: 0 to 100 |
|
|
205
|
-
| rotate | [Docs](https://sharp.pixelplumbing.com/api-operation#rotate) | `/rotate_45/buffalo.png` |
|
|
206
|
-
| enlarge | \_ | `/enlarge,s_2000x2000/buffalo.png` | Allow the image to be upscaled. By default the returned image will never be larger than the source in any dimension, while preserving the requested aspect ratio. |
|
|
207
|
-
| flip | [Docs](https://sharp.pixelplumbing.com/api-operation#flip) | `/flip/buffalo.png` |
|
|
208
|
-
| flop | [Docs](https://sharp.pixelplumbing.com/api-operation#flop) | `/flop/buffalo.png` |
|
|
209
|
-
| sharpen | [Docs](https://sharp.pixelplumbing.com/api-operation#sharpen) | `/sharpen_30/buffalo.png` |
|
|
210
|
-
| median | [Docs](https://sharp.pixelplumbing.com/api-operation#median) | `/median_10/buffalo.png` |
|
|
211
|
-
| blur | [Docs](https://sharp.pixelplumbing.com/api-operation#blur) | `/blur_5/buffalo.png` |
|
|
212
|
-
| gamma | [Docs](https://sharp.pixelplumbing.com/api-operation#gamma) | `/gamma_3/buffalo.png` |
|
|
213
|
-
| negate | [Docs](https://sharp.pixelplumbing.com/api-operation#negate) | `/negate/buffalo.png` |
|
|
214
|
-
| normalize | [Docs](https://sharp.pixelplumbing.com/api-operation#normalize) | `/normalize/buffalo.png` |
|
|
215
|
-
| threshold | [Docs](https://sharp.pixelplumbing.com/api-operation#threshold) | `/threshold_10/buffalo.png` |
|
|
216
|
-
| tint | [Docs](https://sharp.pixelplumbing.com/api-colour#tint) | `/tint_1098123/buffalo.png` |
|
|
217
|
-
| grayscale | [Docs](https://sharp.pixelplumbing.com/api-colour#grayscale) | `/grayscale/buffalo.png` |
|
|
218
|
-
| flatten | [Docs](https://sharp.pixelplumbing.com/api-operation#flatten) | `/flatten/buffalo.png` | Remove alpha channel, if any, and replace with background colour. |
|
|
219
|
-
| modulate | [Docs](https://sharp.pixelplumbing.com/api-operation#modulate) | `/modulate_brightness_saturation_hue/buffalo.png` | Transforms the image using brightness, saturation and hue rotation. |
|
|
220
|
-
| crop | [Docs](https://sharp.pixelplumbing.com/api-resize#extract) | `/crop_{left}_{top}_{width}_{height}/buffalo.png` | Alias for extract. Extract/crop a region of the image. |
|
|
221
|
-
| animated / a | - | `/animated/buffalo.gif` or `/a/buffalo.gif` | Experimental |
|
|
649
|
+
Returned values are escaped by IPX, so custom parsers don't need to do it themselves. Note this is not an access check — exactly as with the default URL style, what the resulting id is allowed to resolve to is enforced by the storage layer (`ipxFSStorage`'s directory boundary, `ipxHttpStorage`'s domain allowlist).
|
|
222
650
|
|
|
223
651
|
## License
|
|
224
652
|
|