@softistx/image 0.0.0-stage → 0.6.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 +551 -2
- package/dist/chunks/index-b71y3faz.js +20 -0
- package/dist/chunks/index-b71y3faz.js.map +10 -0
- package/dist/chunks/index-pj79f6q4.js +732 -0
- package/dist/chunks/index-pj79f6q4.js.map +23 -0
- package/dist/chunks/index-wfsrqqk2.js +16 -0
- package/dist/chunks/index-wfsrqqk2.js.map +10 -0
- package/dist/crop/crop-png.d.ts +39 -0
- package/dist/crop/crop-png.d.ts.map +1 -0
- package/dist/crop/from-query.d.ts +11 -0
- package/dist/crop/from-query.d.ts.map +1 -0
- package/dist/crop/png-chunks.d.ts +29 -0
- package/dist/crop/png-chunks.d.ts.map +1 -0
- package/dist/crop/png.d.ts +24 -0
- package/dist/crop/png.d.ts.map +1 -0
- package/dist/crop/row-assembler.d.ts +17 -0
- package/dist/crop/row-assembler.d.ts.map +1 -0
- package/dist/crop/unfilter.d.ts +10 -0
- package/dist/crop/unfilter.d.ts.map +1 -0
- package/dist/errors/image-error.d.ts +49 -0
- package/dist/errors/image-error.d.ts.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +42 -0
- package/dist/index.js.map +11 -0
- package/dist/input/read-input.d.ts +10 -0
- package/dist/input/read-input.d.ts.map +1 -0
- package/dist/input/sniff.d.ts +9 -0
- package/dist/input/sniff.d.ts.map +1 -0
- package/dist/pdf/index.d.ts +3 -0
- package/dist/pdf/index.d.ts.map +1 -0
- package/dist/pdf/index.js +166 -0
- package/dist/pdf/index.js.map +10 -0
- package/dist/pdf/render-pdf-page.d.ts +56 -0
- package/dist/pdf/render-pdf-page.d.ts.map +1 -0
- package/dist/process/bun-errors.d.ts +8 -0
- package/dist/process/bun-errors.d.ts.map +1 -0
- package/dist/process/geometry.d.ts +49 -0
- package/dist/process/geometry.d.ts.map +1 -0
- package/dist/process/process.d.ts +23 -0
- package/dist/process/process.d.ts.map +1 -0
- package/dist/render/render-image.d.ts +61 -0
- package/dist/render/render-image.d.ts.map +1 -0
- package/dist/s3/bind-image-store.d.ts +27 -0
- package/dist/s3/bind-image-store.d.ts.map +1 -0
- package/dist/s3/index.d.ts +4 -0
- package/dist/s3/index.d.ts.map +1 -0
- package/dist/s3/index.js +73 -0
- package/dist/s3/index.js.map +11 -0
- package/dist/s3/types.d.ts +61 -0
- package/dist/s3/types.d.ts.map +1 -0
- package/dist/s3/upload-image.d.ts +23 -0
- package/dist/s3/upload-image.d.ts.map +1 -0
- package/dist/set/define-image-set.d.ts +25 -0
- package/dist/set/define-image-set.d.ts.map +1 -0
- package/dist/set/limit.d.ts +7 -0
- package/dist/set/limit.d.ts.map +1 -0
- package/dist/set/options.d.ts +104 -0
- package/dist/set/options.d.ts.map +1 -0
- package/dist/set/runtime.d.ts +7 -0
- package/dist/set/runtime.d.ts.map +1 -0
- package/dist/set/types.d.ts +60 -0
- package/dist/set/types.d.ts.map +1 -0
- package/dist/set/whole.d.ts +3 -0
- package/dist/set/whole.d.ts.map +1 -0
- package/docs/README.md +13 -0
- package/docs/guide/pdf.md +60 -0
- package/docs/guide/processing.md +313 -0
- package/docs/guide/rendering.md +88 -0
- package/docs/guide/storing.md +219 -0
- package/docs/roadmap.md +71 -0
- package/docs/troubleshooting.md +724 -0
- package/package.json +90 -5
|
@@ -0,0 +1,724 @@
|
|
|
1
|
+
# Troubleshooting
|
|
2
|
+
|
|
3
|
+
Two kinds of failure come out of this package.
|
|
4
|
+
|
|
5
|
+
- **`ImageError`** is a refusal of the upload. It has a `code`
|
|
6
|
+
(`TOO_LARGE`, `UNSUPPORTED_TYPE`, `UNDECODABLE`, `TOO_SMALL` or
|
|
7
|
+
`INVALID_CROP`) and a `reason` (`bytes`, `pixels`, `format`,
|
|
8
|
+
`heic-unavailable`, `decode`, `dimensions` or `crop`). It is raised before anything is stored, and its message never
|
|
9
|
+
quotes the uploaded bytes. When Bun's own error caused it, that error is
|
|
10
|
+
`error.cause`.
|
|
11
|
+
- **`TypeError` and `RangeError`** are for what the caller wrote: a bad option,
|
|
12
|
+
an input of the wrong type, a size the set does not have. They
|
|
13
|
+
are programming errors, not upload refusals.
|
|
14
|
+
|
|
15
|
+
Any other error is not this package's: it is rethrown untouched (see
|
|
16
|
+
[Errors that are not an `ImageError`](#errors-that-are-not-an-imageerror)).
|
|
17
|
+
|
|
18
|
+
- **Install and runtime**
|
|
19
|
+
- [`@softistx/image needs Bun 1.4.1 or later, where Bun.Image exists`](#softistximage-needs-bun-141-or-later-where-bunimage-exists)
|
|
20
|
+
- [`this machine cannot decode HEIC (Linux cannot)`](#this-machine-cannot-decode-heic-linux-cannot)
|
|
21
|
+
- [`this machine cannot decode <format>`](#this-machine-cannot-decode-format)
|
|
22
|
+
- [Memory grows under large images](#memory-grows-under-large-images)
|
|
23
|
+
- [`@softistx/image/pdf needs the optional peers pdfjs-dist and @napi-rs/canvas`](#softistximagepdf-needs-the-optional-peers-pdfjs-dist-and-napi-rscanvas)
|
|
24
|
+
- [`the upload is not a PDF`](#the-upload-is-not-a-pdf)
|
|
25
|
+
- [`the page would render to <w>x<h> pixels, over the limit`](#the-page-would-render-to-wxh-pixels-over-the-limit)
|
|
26
|
+
- [`the PDF cannot be rendered: <why>`](#the-pdf-cannot-be-rendered-why)
|
|
27
|
+
- **Upload refusals (`ImageError`)**
|
|
28
|
+
- [`the upload is over <maxBytes> bytes`](#the-upload-is-over-maxbytes-bytes)
|
|
29
|
+
- [`the image has too many pixels`](#the-image-has-too-many-pixels)
|
|
30
|
+
- [`the PNG has <w>x<h> pixels, over the <maxPixels> limit`](#the-png-has-wxh-pixels-over-the-maxpixels-limit)
|
|
31
|
+
- [`the upload is not a JPEG, PNG, WebP or HEIC`](#the-upload-is-not-a-jpeg-png-webp-or-heic)
|
|
32
|
+
- [`the <format> image cannot be decoded`](#the-format-image-cannot-be-decoded)
|
|
33
|
+
- [`the PNG cannot be read: <why>`](#the-png-cannot-be-read-why)
|
|
34
|
+
- [`the image is <w>x<h>, under <minSide>px on its short side`](#the-image-is-wxh-under-minsidepx-on-its-short-side)
|
|
35
|
+
- [`the crop is <size>px, under the <minSide>px minimum`](#the-crop-is-sizepx-under-the-minsidepx-minimum)
|
|
36
|
+
- **Input and options**
|
|
37
|
+
- [`image input must be a Uint8Array, an ArrayBuffer, a Blob or a ReadableStream of Uint8Array`](#image-input-must-be-a-uint8array-an-arraybuffer-a-blob-or-a-readablestream-of-uint8array)
|
|
38
|
+
- [`a stream chunk must be a Uint8Array`](#a-stream-chunk-must-be-a-uint8array)
|
|
39
|
+
- [`<option> must be a whole number of <range>, got <value>`](#option-must-be-a-whole-number-of-range-got-value)
|
|
40
|
+
- [`sizes must be a non-empty array of whole numbers`](#sizes-must-be-a-non-empty-array-of-whole-numbers)
|
|
41
|
+
- [`sizes must be unique, got [<sizes>]`](#sizes-must-be-unique-got-sizes)
|
|
42
|
+
- [`format must be 'webp' or 'png', got <value>`](#format-must-be-webp-or-png-got-value)
|
|
43
|
+
- [`lossless must be a boolean, got <value>`](#lossless-must-be-a-boolean-got-value)
|
|
44
|
+
- [`lossless applies to WebP only: a PNG is always lossless`](#lossless-applies-to-webp-only-a-png-is-always-lossless)
|
|
45
|
+
- [`quality applies to WebP only: a PNG is always lossless`](#quality-applies-to-webp-only-a-png-is-always-lossless)
|
|
46
|
+
- **Crops (`INVALID_CROP`)**
|
|
47
|
+
- [`a crop is { x, y, size }, three whole numbers of 0 or more`](#a-crop-is--x-y-size--three-whole-numbers-of-0-or-more)
|
|
48
|
+
- [`a <size>px crop at (<x>, <y>) does not fit the <w>x<h> image`](#a-sizepx-crop-at-x-y-does-not-fit-the-wxh-image)
|
|
49
|
+
- [`a <w>x<h> region at (<x>, <y>) does not fit a <w>x<h> image`](#a-wxh-region-at-x-y-does-not-fit-a-wxh-image)
|
|
50
|
+
- **`renderImage`**
|
|
51
|
+
- [`a crop is { x, y, width, height }, whole numbers: ...`](#renderimage)
|
|
52
|
+
- [`a <w>x<h> crop at (<x>, <y>) does not fit the <w>x<h> image`](#renderimage)
|
|
53
|
+
- [`the crop is <w>x<h>, under the <w>x<h> asked for`](#renderimage)
|
|
54
|
+
- [`format must be 'jpeg', 'webp' or 'png', got <value>` and the quality `TypeError`](#renderimage)
|
|
55
|
+
- **Storage (`@softistx/image/s3`)**
|
|
56
|
+
- [`<call>: size <n> is not one of the set's sizes (<sizes>)`](#call-size-n-is-not-one-of-the-sets-sizes-sizes)
|
|
57
|
+
- [An S3 error from `replace`](#an-s3-error-from-replace)
|
|
58
|
+
- [`remove` throws `NoSuchKey`, or the bytes stay after `remove`](#remove-throws-nosuchkey-or-the-bytes-stay-after-remove)
|
|
59
|
+
- [The bucket is bound with a public `acl`](#the-bucket-is-bound-with-a-public-acl)
|
|
60
|
+
- **Errors that are not an `ImageError`**
|
|
61
|
+
- [Errors that are not an `ImageError`](#errors-that-are-not-an-imageerror)
|
|
62
|
+
|
|
63
|
+
## Install and runtime
|
|
64
|
+
|
|
65
|
+
### `@softistx/image needs Bun 1.4.1 or later, where Bun.Image exists`
|
|
66
|
+
|
|
67
|
+
`TypeError`, thrown by `defineImageSet` before it reads any option.
|
|
68
|
+
|
|
69
|
+
**When:** the set is defined on a runtime where `Bun` is missing or
|
|
70
|
+
`Bun.Image` is not a function: a Bun older than 1.4.1, or Node.
|
|
71
|
+
**Why:** the package decodes and resizes with Bun's own `Bun.Image`, which Bun
|
|
72
|
+
added in 1.4.1. The check runs when the set is defined, so the failure shows at
|
|
73
|
+
startup rather than on the first upload. It is not an `ImageError`: it is not
|
|
74
|
+
the upload's fault.
|
|
75
|
+
**Fix:**
|
|
76
|
+
|
|
77
|
+
```sh
|
|
78
|
+
bun --version # must be 1.4.1 or newer
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
bun upgrade
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Upgrade the runtime that runs the process, the Docker image and CI included,
|
|
86
|
+
not only your machine. The package runs on Bun only: Node has no `Bun.Image`.
|
|
87
|
+
|
|
88
|
+
### `this machine cannot decode HEIC (Linux cannot)`
|
|
89
|
+
|
|
90
|
+
`ImageError`, `code: 'UNSUPPORTED_TYPE'`, `reason: 'heic-unavailable'`.
|
|
91
|
+
|
|
92
|
+
**When:** `process` is given a HEIC photo, on a Linux host.
|
|
93
|
+
**Why:** Bun decodes HEIC only through the operating system's codec, which
|
|
94
|
+
macOS and Windows have and Linux does not. The upload is a valid HEIC; this
|
|
95
|
+
machine cannot read it, so no limit or option of this package changes that.
|
|
96
|
+
**Fix:** ask the browser for a format the server can read. A file input with
|
|
97
|
+
an `accept` list of JPEG, PNG and WebP makes iOS convert a HEIC photo to JPEG
|
|
98
|
+
before it uploads it:
|
|
99
|
+
|
|
100
|
+
```html
|
|
101
|
+
<input type="file" accept="image/jpeg,image/png,image/webp" />
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
And tell the user, rather than a generic failure, when it still arrives:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import { ImageError } from '@softistx/image';
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
await avatars.process(await request.blob());
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (error instanceof ImageError && error.reason === 'heic-unavailable') {
|
|
113
|
+
return new Response('Please upload a JPEG, PNG or WebP photo', {
|
|
114
|
+
status: 415,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Development on a Mac accepts HEIC, so the refusal can first show up in
|
|
122
|
+
production on Linux. Test the refusal, not only the happy path.
|
|
123
|
+
|
|
124
|
+
### `this machine cannot decode <format>`
|
|
125
|
+
|
|
126
|
+
`ImageError`, `code: 'UNSUPPORTED_TYPE'`, `reason: 'format'`. `<format>` is
|
|
127
|
+
`jpeg`, `png` or `webp`.
|
|
128
|
+
|
|
129
|
+
**When:** `process` on an upload whose first bytes say it is that format.
|
|
130
|
+
**Why:** Bun reported the format as unsupported on this build or machine. It is
|
|
131
|
+
rare for these three; check the Bun build before blaming the upload.
|
|
132
|
+
**Fix:**
|
|
133
|
+
|
|
134
|
+
```sh
|
|
135
|
+
bun --version
|
|
136
|
+
bun -e "console.log(typeof Bun.Image)"
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Run the same bytes on a stock Bun 1.4.1 or newer. `error.cause` holds Bun's
|
|
140
|
+
error, with its `code` (`ERR_IMAGE_FORMAT_UNSUPPORTED`).
|
|
141
|
+
|
|
142
|
+
### Memory grows under large images
|
|
143
|
+
|
|
144
|
+
**When:** large photos are processed one after another or at once, for
|
|
145
|
+
example a burst of 36 MP uploads with a small crop, and the process grows by
|
|
146
|
+
hundreds of megabytes, or past a gigabyte, or is killed for memory.
|
|
147
|
+
**Why:** one call peaks at about Bun's decode of the source (4 bytes per pixel)
|
|
148
|
+
+ the intermediate PNG + the crop square, about `4 x (2 x the largest size)^2`
|
|
149
|
+
bytes. Bun 1.4.3 also holds on to each `png()` output until a later
|
|
150
|
+
collection, so sequential worst-case calls climb before the memory falls back.
|
|
151
|
+
Measured on photo-like JPEGs: a 256 px crop grows the process by about 150 MB
|
|
152
|
+
at 25 MP, 210 MB at 36 MP and 280 MB at 49 MP in one call, and 70 sequential
|
|
153
|
+
calls climbed to 716 MB at 25 MP and past 1.6 GB at 36 MP. Centred crops with
|
|
154
|
+
small sizes stay at about 17 MB; a small crop on a huge image, or large `sizes`
|
|
155
|
+
(`[4096]` on 49 MP peaked at 0.9 to 1.3 GB), are the expensive path.
|
|
156
|
+
`concurrency` limits the calls in flight, not this retention, so it is not a
|
|
157
|
+
hard ceiling.
|
|
158
|
+
**Fix:** lower `maxPixels` to what your users really upload (the default is
|
|
159
|
+
25 million), and keep `concurrency` low. The wait queue is unbounded and each
|
|
160
|
+
waiting call holds its bytes (up to `maxBytes`), so cap uploads before
|
|
161
|
+
`process` (a body limit in your server). The calls beyond `concurrency` wait
|
|
162
|
+
their turn, in order:
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
import { defineImageSet } from '@softistx/image';
|
|
166
|
+
|
|
167
|
+
const avatars = defineImageSet({
|
|
168
|
+
maxPixels: 16_000_000, // a 16 MP photo is enough for a 256 px avatar
|
|
169
|
+
concurrency: 1,
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
A photo over `maxPixels` is then refused as
|
|
174
|
+
[`the image has too many pixels`](#the-image-has-too-many-pixels), before it
|
|
175
|
+
is decoded. `maxBytes` bounds the bytes the upload may be, not the pixels it
|
|
176
|
+
claims: a small file can claim many pixels.
|
|
177
|
+
|
|
178
|
+
### `@softistx/image/pdf needs the optional peers pdfjs-dist and @napi-rs/canvas`
|
|
179
|
+
|
|
180
|
+
**When:** the first `renderPdfFirstPage` call, when one of the two packages is
|
|
181
|
+
not installed. A plain `Error`; `error.cause` is the import failure.
|
|
182
|
+
|
|
183
|
+
**Fix:** `bun add pdfjs-dist @napi-rs/canvas`. `@napi-rs/canvas` ships prebuilt
|
|
184
|
+
binaries for macOS, Linux (glibc and musl) and Windows; nothing else is needed.
|
|
185
|
+
|
|
186
|
+
### `the upload is not a PDF`
|
|
187
|
+
|
|
188
|
+
**When:** `renderPdfFirstPage` got bytes with no `%PDF-` in their first 1024
|
|
189
|
+
bytes (`ImageError` `UNSUPPORTED_TYPE`, reason `format`). A picture goes to
|
|
190
|
+
`renderImage` instead.
|
|
191
|
+
|
|
192
|
+
### `the page would render to <w>x<h> pixels, over the limit`
|
|
193
|
+
|
|
194
|
+
**When:** `renderPdfFirstPage` was asked for an output over `maxPixels` or over
|
|
195
|
+
8192 on a side (`ImageError` `TOO_LARGE`, reason `pixels`). Refused before the
|
|
196
|
+
page is drawn.
|
|
197
|
+
|
|
198
|
+
**Fix:** ask for a smaller `width` (or a `height` cap), or raise `maxPixels`.
|
|
199
|
+
|
|
200
|
+
### `the PDF cannot be rendered: <why>`
|
|
201
|
+
|
|
202
|
+
**When:** the PDF is damaged or password-protected (`it is damaged or
|
|
203
|
+
encrypted`), has no page, or page 1 fails to draw (`ImageError` `UNDECODABLE`,
|
|
204
|
+
reason `decode`). `error.cause` is pdf.js's own error.
|
|
205
|
+
|
|
206
|
+
**Fix:** none for the file; answer with a 4xx and keep the PDF without a preview.
|
|
207
|
+
|
|
208
|
+
## Upload refusals
|
|
209
|
+
|
|
210
|
+
### `the upload is over <maxBytes> bytes`
|
|
211
|
+
|
|
212
|
+
`ImageError`, `code: 'TOO_LARGE'`, `reason: 'bytes'`.
|
|
213
|
+
|
|
214
|
+
**When:** `process` on an upload bigger than `maxBytes` (default 5 MiB). A
|
|
215
|
+
`Uint8Array`, an `ArrayBuffer` or a `Blob` is checked by its size before it is
|
|
216
|
+
read; a stream is cancelled at the chunk that crosses the limit.
|
|
217
|
+
**Why:** the limit is a cap on what the server will read, never on what it
|
|
218
|
+
stores.
|
|
219
|
+
**Fix:** raise `maxBytes` if the limit is too tight, or answer the user. The
|
|
220
|
+
default admits most 12 MP photos, but a 48 MP phone JPEG is about 11 MB and is
|
|
221
|
+
refused here even where `maxPixels` would admit it:
|
|
222
|
+
|
|
223
|
+
```ts
|
|
224
|
+
const avatars = defineImageSet({ maxBytes: 10 * 1024 * 1024 });
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### `the image has too many pixels`
|
|
228
|
+
|
|
229
|
+
`ImageError`, `code: 'TOO_LARGE'`, `reason: 'pixels'`.
|
|
230
|
+
|
|
231
|
+
**When:** `process` on an image whose width times height is over `maxPixels`
|
|
232
|
+
(default 25 million, which covers a 24 MP phone; a 48 MP phone is over it).
|
|
233
|
+
**Why:** a small file can decode to a huge image, and the decoded pixels are
|
|
234
|
+
what take the memory. Bun refuses it before decoding, with
|
|
235
|
+
`ERR_IMAGE_TOO_MANY_PIXELS` as `error.cause.code`.
|
|
236
|
+
**Fix:** answer the user with a smaller photo, or raise `maxPixels` knowing the
|
|
237
|
+
cost in [memory](#memory-grows-under-large-images):
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
const avatars = defineImageSet({ maxPixels: 100_000_000 });
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### `the PNG has <w>x<h> pixels, over the <maxPixels> limit`
|
|
244
|
+
|
|
245
|
+
`ImageError`, `code: 'TOO_LARGE'`, `reason: 'pixels'`.
|
|
246
|
+
|
|
247
|
+
**When:** `process` on an image that passed the first pixel check but whose
|
|
248
|
+
intermediate PNG, read by this package's own reader, is over `maxPixels`.
|
|
249
|
+
**Why:** the pipeline shrinks the image to a PNG, then reads it itself while it
|
|
250
|
+
inflates, so `maxPixels` is checked twice. This second check is the
|
|
251
|
+
safety net behind the first; it should not fire for a normal upload.
|
|
252
|
+
**Fix:** the same as for the first check. If you see it with an image the
|
|
253
|
+
first check lets through, report it with the image's size and format.
|
|
254
|
+
|
|
255
|
+
### `the request is not an image: its content-type must be image/*`
|
|
256
|
+
|
|
257
|
+
`ImageError`, `code: 'UNSUPPORTED_TYPE'`, `reason: 'format'`.
|
|
258
|
+
|
|
259
|
+
**When:** `uploadImage` on a request whose `content-type` header is missing or
|
|
260
|
+
is not `image/*`, such as `text/plain` or `application/octet-stream`.
|
|
261
|
+
**Why:** the helper refuses before reading the body. (The bytes are still judged
|
|
262
|
+
by their first bytes afterwards.)
|
|
263
|
+
**Fix:** have the client send the file's own type, and answer the refusal with a
|
|
264
|
+
4xx (415):
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
await fetch('/images/item', {
|
|
268
|
+
method: 'PUT',
|
|
269
|
+
headers: { 'content-type': file.type }, // image/png, image/jpeg, ...
|
|
270
|
+
body: file,
|
|
271
|
+
});
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### `the upload is not a JPEG, PNG, WebP or HEIC`
|
|
275
|
+
|
|
276
|
+
`ImageError`, `code: 'UNSUPPORTED_TYPE'`, `reason: 'format'`.
|
|
277
|
+
|
|
278
|
+
**When:** `process` on bytes that start like none of the four formats: a GIF,
|
|
279
|
+
an AVIF, an SVG, a PDF, or a file renamed to `.jpg`.
|
|
280
|
+
**Why:** the type is judged from the first bytes, never from the file name or
|
|
281
|
+
the declared content type, so a lie in either changes nothing.
|
|
282
|
+
**Fix:** convert before uploading, or restrict the input and answer the user:
|
|
283
|
+
|
|
284
|
+
```html
|
|
285
|
+
<input type="file" accept="image/jpeg,image/png,image/webp" />
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### `the <format> image cannot be decoded`
|
|
289
|
+
|
|
290
|
+
`ImageError`, `code: 'UNDECODABLE'`, `reason: 'decode'`. `<format>` is `jpeg`,
|
|
291
|
+
`png`, `webp` or `heic`.
|
|
292
|
+
|
|
293
|
+
**When:** `process` on a file that starts like that format but is truncated,
|
|
294
|
+
damaged or of a variant Bun does not read.
|
|
295
|
+
**Why:** the first bytes are right and the content is not. Bun's error, with
|
|
296
|
+
`ERR_IMAGE_DECODE_FAILED` or `ERR_IMAGE_UNKNOWN_FORMAT` as its `code`, is
|
|
297
|
+
`error.cause`.
|
|
298
|
+
**Fix:** ask the user for another file, and log the cause when the file looks
|
|
299
|
+
fine:
|
|
300
|
+
|
|
301
|
+
```ts
|
|
302
|
+
catch (error) {
|
|
303
|
+
if (error instanceof ImageError && error.reason === 'decode') {
|
|
304
|
+
console.warn('undecodable upload', error.cause);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### `the PNG cannot be read: <why>`
|
|
310
|
+
|
|
311
|
+
`ImageError`, `code: 'UNDECODABLE'`, `reason: 'decode'`. `<why>` is one of
|
|
312
|
+
`no PNG signature`, `no IHDR chunk`, `the size is not valid`, `only 8-bit
|
|
313
|
+
samples are read`, `an interlaced PNG is not read`, `this colour type (a
|
|
314
|
+
palette, for one) is not read`, `no IDAT chunk`, and the other damage the
|
|
315
|
+
reader names.
|
|
316
|
+
|
|
317
|
+
**When:** `process`, in the step after the shrink, on the PNG that Bun
|
|
318
|
+
produced.
|
|
319
|
+
**Why:** this package reads that intermediate PNG with its own small decoder,
|
|
320
|
+
which accepts only what Bun's shrink writes. A refusal here means the
|
|
321
|
+
intermediate file was not what was expected, not that the upload was a bad
|
|
322
|
+
PNG: an uploaded interlaced or palette PNG is read by Bun first and re-encoded.
|
|
323
|
+
**Fix:** none on your side. Report it with the Bun version and the upload's
|
|
324
|
+
format and dimensions at <https://github.com/softistx/nxgt-data/issues>.
|
|
325
|
+
|
|
326
|
+
### `the image is <w>x<h>, under <minSide>px on its short side`
|
|
327
|
+
|
|
328
|
+
`ImageError`, `code: 'TOO_SMALL'`, `reason: 'dimensions'`.
|
|
329
|
+
|
|
330
|
+
**When:** `process` with no `crop`, on an image whose short side is under
|
|
331
|
+
`minSide`. `minSide` defaults to the largest of `sizes` (256 with the
|
|
332
|
+
defaults), so that no variant is enlarged.
|
|
333
|
+
**Why:** a variant is never produced larger than its source.
|
|
334
|
+
**Fix:** ask for a bigger photo, or lower `minSide` and accept that the
|
|
335
|
+
largest variant is upscaled from less, or drop the large size:
|
|
336
|
+
|
|
337
|
+
```ts
|
|
338
|
+
const avatars = defineImageSet({ sizes: [128, 64] }); // minSide becomes 128
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
### `the crop is <size>px, under the <minSide>px minimum`
|
|
342
|
+
|
|
343
|
+
`ImageError`, `code: 'TOO_SMALL'`, `reason: 'dimensions'`.
|
|
344
|
+
|
|
345
|
+
**When:** `process` with a `crop` whose `size` is under `minSide`.
|
|
346
|
+
**Why:** `minSide` applies to the crop's own side when one is given. The
|
|
347
|
+
crop's shape and fit are checked first: a crop that does not fit the image is an
|
|
348
|
+
`INVALID_CROP`, whatever its size. Only a crop that fits and is under `minSide`
|
|
349
|
+
is `TOO_SMALL`.
|
|
350
|
+
**Fix:** make the cropper UI enforce the same minimum as the `minSide` option,
|
|
351
|
+
so it never offers a smaller crop. Do not raise `size` in code: a larger crop
|
|
352
|
+
at the same `x` and `y` can reach outside the image.
|
|
353
|
+
|
|
354
|
+
```ts
|
|
355
|
+
const avatars = defineImageSet({ sizes: [256, 64] }); // minSide is 256
|
|
356
|
+
// cropper: minimum crop size 256, the same number as the set's minSide
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
## Input and options
|
|
360
|
+
|
|
361
|
+
### `image input must be a Uint8Array, an ArrayBuffer, a Blob or a ReadableStream of Uint8Array`
|
|
362
|
+
|
|
363
|
+
`TypeError`.
|
|
364
|
+
|
|
365
|
+
**When:** `process` with anything else: a `string`, a `Buffer` from another
|
|
366
|
+
realm, a `File` wrapper, a `Response`, `undefined`.
|
|
367
|
+
**Why:** the input must be one of the four types.
|
|
368
|
+
**Fix:**
|
|
369
|
+
|
|
370
|
+
```ts
|
|
371
|
+
await avatars.process(await response.blob());
|
|
372
|
+
// or: await avatars.process(request.body!) // a ReadableStream<Uint8Array>
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
### `a stream chunk must be a Uint8Array`
|
|
376
|
+
|
|
377
|
+
`TypeError`.
|
|
378
|
+
|
|
379
|
+
**When:** `process` on a `ReadableStream` that yields something other than
|
|
380
|
+
`Uint8Array`, a string for example.
|
|
381
|
+
**Why:** a stream is read chunk by chunk, and a text stream cannot be
|
|
382
|
+
concatenated into image bytes. The stream is cancelled.
|
|
383
|
+
**Fix:** pass a byte stream, not a text one:
|
|
384
|
+
|
|
385
|
+
```ts
|
|
386
|
+
const bytes: ReadableStream<Uint8Array> = (await fetch(url)).body!;
|
|
387
|
+
await avatars.process(bytes);
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### `<option> must be a whole number of <range>, got <value>`
|
|
391
|
+
|
|
392
|
+
`TypeError`, thrown by `defineImageSet`, before any upload is read.
|
|
393
|
+
|
|
394
|
+
**When:** an option is a fraction, a string, `NaN` or out of its range.
|
|
395
|
+
`<option>` is `maxBytes`, `maxPixels`, `minSide`, `quality` (1 to 100),
|
|
396
|
+
`concurrency` (1 to 64) or `sizes[]` (1 to 4096).
|
|
397
|
+
**Why:** each is a count, and a wrong one would otherwise fail in the middle of
|
|
398
|
+
an upload.
|
|
399
|
+
**Fix:**
|
|
400
|
+
|
|
401
|
+
```ts
|
|
402
|
+
defineImageSet({ quality: 80, concurrency: 2, maxBytes: 5 * 1024 * 1024 });
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
Read `maxBytes` from an environment variable with `Number(...)` and check it
|
|
406
|
+
is whole.
|
|
407
|
+
|
|
408
|
+
### `sizes must be a non-empty array of whole numbers`
|
|
409
|
+
|
|
410
|
+
`TypeError`, thrown by `defineImageSet`.
|
|
411
|
+
|
|
412
|
+
**When:** `sizes` is not an array, or is empty.
|
|
413
|
+
**Why:** a set needs at least one variant.
|
|
414
|
+
**Fix:**
|
|
415
|
+
|
|
416
|
+
```ts
|
|
417
|
+
defineImageSet({ sizes: [256, 64] });
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
### `sizes must be unique, got [<sizes>]`
|
|
421
|
+
|
|
422
|
+
`TypeError`, thrown by `defineImageSet`.
|
|
423
|
+
|
|
424
|
+
**When:** `sizes` repeats a value, for example `[64, 64]`.
|
|
425
|
+
**Why:** two variants of one size would write the same key.
|
|
426
|
+
**Fix:**
|
|
427
|
+
|
|
428
|
+
```ts
|
|
429
|
+
defineImageSet({ sizes: [256, 64] });
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
### `format must be 'webp' or 'png', got <value>`
|
|
433
|
+
|
|
434
|
+
`TypeError`, thrown by `defineImageSet`.
|
|
435
|
+
|
|
436
|
+
**When:** `format` is anything else: `'avif'`, `'jpeg'`, or `'PNG'` in capitals.
|
|
437
|
+
**Why:** the variants are WebP or PNG only, and the value is compared exactly.
|
|
438
|
+
**Fix:**
|
|
439
|
+
|
|
440
|
+
```ts
|
|
441
|
+
defineImageSet({ format: 'png' });
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
### `lossless must be a boolean, got <value>`
|
|
445
|
+
|
|
446
|
+
`TypeError`, thrown by `defineImageSet`.
|
|
447
|
+
|
|
448
|
+
**When:** `lossless` is a string or a number, for example read from an
|
|
449
|
+
environment variable as `'true'`.
|
|
450
|
+
**Why:** a truthy check would make `'false'` lossless.
|
|
451
|
+
**Fix:**
|
|
452
|
+
|
|
453
|
+
```ts
|
|
454
|
+
defineImageSet({ lossless: process.env.LOSSLESS === 'true' });
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
### `lossless applies to WebP only: a PNG is always lossless`
|
|
458
|
+
|
|
459
|
+
`TypeError`, thrown by `defineImageSet`.
|
|
460
|
+
|
|
461
|
+
**When:** `lossless` is given (`true` or `false`) beside `format: 'png'`.
|
|
462
|
+
**Why:** a PNG has no lossy mode, so the option would be ignored silently.
|
|
463
|
+
**Fix:** drop `lossless`, or drop `format` for a lossless WebP:
|
|
464
|
+
|
|
465
|
+
```ts
|
|
466
|
+
defineImageSet({ format: 'png' }); // PNG
|
|
467
|
+
defineImageSet({ lossless: true }); // lossless WebP
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
### `quality applies to WebP only: a PNG is always lossless`
|
|
471
|
+
|
|
472
|
+
`TypeError`, thrown by `defineImageSet`.
|
|
473
|
+
|
|
474
|
+
**When:** `quality` is given beside `format: 'png'`.
|
|
475
|
+
**Why:** a PNG has no quality setting, so the option would be ignored silently.
|
|
476
|
+
**Fix:** drop `quality`:
|
|
477
|
+
|
|
478
|
+
```ts
|
|
479
|
+
defineImageSet({ format: 'png', sizes: [512] });
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
## Crops
|
|
483
|
+
|
|
484
|
+
### `a crop is { x, y, size }, three whole numbers of 0 or more`
|
|
485
|
+
|
|
486
|
+
`ImageError`, `code: 'INVALID_CROP'`, `reason: 'crop'`.
|
|
487
|
+
|
|
488
|
+
**When:** `process` with a `crop` whose `x`, `y` or `size` is a fraction, a
|
|
489
|
+
negative number or not a number, or a `crop` that is `null` or not an object.
|
|
490
|
+
**Why:** a crop is pixels in the oriented image, as the user sees it.
|
|
491
|
+
Cropper libraries often report fractions.
|
|
492
|
+
**Fix:**
|
|
493
|
+
|
|
494
|
+
Answer it with a 4xx (400): the crop comes from the request.
|
|
495
|
+
|
|
496
|
+
```ts
|
|
497
|
+
const crop = {
|
|
498
|
+
x: Math.round(area.x),
|
|
499
|
+
y: Math.round(area.y),
|
|
500
|
+
size: Math.round(area.width),
|
|
501
|
+
};
|
|
502
|
+
await avatars.process(blob, { crop });
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
### A blank or non-numeric `?x=&y=&size=` is refused
|
|
506
|
+
|
|
507
|
+
`ImageError`, `code: 'INVALID_CROP'`, `reason: 'crop'`, with the message of
|
|
508
|
+
the crop entry above.
|
|
509
|
+
|
|
510
|
+
**When:** `uploadImage` or `cropFromQuery` on a query such as `?x=&y=2&size=3`
|
|
511
|
+
or `?x=abc&y=2&size=3`, or with only some of the three present.
|
|
512
|
+
**Why:** `cropFromQuery` turns a missing, blank or non-numeric value into `NaN`
|
|
513
|
+
instead of guessing, and the set refuses `NaN` as an invalid crop. Only a query
|
|
514
|
+
with none of the three gives the default centred crop.
|
|
515
|
+
**Fix:** send all three whole numbers, or none. Answer the error with a 4xx
|
|
516
|
+
(400): the crop comes from the request.
|
|
517
|
+
|
|
518
|
+
```ts
|
|
519
|
+
// PUT /images/item?x=0&y=0&size=300
|
|
520
|
+
```
|
|
521
|
+
|
|
522
|
+
### `a <size>px crop at (<x>, <y>) does not fit the <w>x<h> image`
|
|
523
|
+
|
|
524
|
+
`ImageError`, `code: 'INVALID_CROP'`, `reason: 'crop'`.
|
|
525
|
+
|
|
526
|
+
**When:** `process` with a `crop` that reaches outside the image, or has
|
|
527
|
+
`size` 0.
|
|
528
|
+
**Why:** `x + size` is over the width or `y + size` over the height. The crop is
|
|
529
|
+
in the oriented frame, after the EXIF rotation, so a crop computed on the
|
|
530
|
+
unrotated pixels of a phone photo misses.
|
|
531
|
+
**Fix:** clamp to the oriented size that the cropper showed, or send no crop and
|
|
532
|
+
get a centred square:
|
|
533
|
+
|
|
534
|
+
```ts
|
|
535
|
+
await avatars.process(blob); // centred square of the short side
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
### `a <w>x<h> region at (<x>, <y>) does not fit a <w>x<h> image`
|
|
539
|
+
|
|
540
|
+
`RangeError`.
|
|
541
|
+
|
|
542
|
+
**When:** never from a call that goes through `process`.
|
|
543
|
+
**Why:** an internal check on the pixels that the package crops, after the crop
|
|
544
|
+
has already been validated. Reaching it is a bug in this package.
|
|
545
|
+
**Fix:** report it at <https://github.com/softistx/nxgt-data/issues> with the
|
|
546
|
+
image's size and format, and the crop given.
|
|
547
|
+
|
|
548
|
+
## `renderImage`
|
|
549
|
+
|
|
550
|
+
### `a crop is { x, y, width, height }, whole numbers: x and y of 0 or more, width and height of 1 or more`
|
|
551
|
+
|
|
552
|
+
`ImageError`, `code: 'INVALID_CROP'`, `reason: 'crop'`.
|
|
553
|
+
|
|
554
|
+
**When:** `renderImage` with a `crop` that is not an object, lacks a field, or has a
|
|
555
|
+
fraction, a negative `x` or `y`, or a `width` or `height` under 1.
|
|
556
|
+
**Why:** the crop is pixels of the upright picture, and cropper libraries often
|
|
557
|
+
report fractions. It comes from a request, so it is a refusal, not a bug.
|
|
558
|
+
**Fix:** round before sending, and answer 400.
|
|
559
|
+
|
|
560
|
+
```ts
|
|
561
|
+
const crop = {
|
|
562
|
+
x: Math.round(area.x),
|
|
563
|
+
y: Math.round(area.y),
|
|
564
|
+
width: Math.round(area.width),
|
|
565
|
+
height: Math.round(area.height),
|
|
566
|
+
};
|
|
567
|
+
await renderImage(blob, { crop, width: 800, height: 450, format: 'jpeg' });
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
### `a <w>x<h> crop at (<x>, <y>) does not fit the <w>x<h> image`
|
|
571
|
+
|
|
572
|
+
`ImageError`, `code: 'INVALID_CROP'`, `reason: 'crop'`.
|
|
573
|
+
|
|
574
|
+
**When:** `renderImage` with a rectangle that reaches outside the picture.
|
|
575
|
+
**Why:** `x + width` is over the picture's width or `y + height` over its
|
|
576
|
+
height, in the frame after EXIF rotation (JPEG only).
|
|
577
|
+
**Fix:** clamp the rectangle to `source.width` x `source.height`, or send no
|
|
578
|
+
crop. Read the size with a first call, or from the client's own preview.
|
|
579
|
+
|
|
580
|
+
### `the crop is <w>x<h>, under the <w>x<h> asked for`
|
|
581
|
+
|
|
582
|
+
`ImageError`, `code: 'TOO_SMALL'`, `reason: 'dimensions'` (the message says
|
|
583
|
+
`the image is` when there is no crop).
|
|
584
|
+
|
|
585
|
+
**When:** `renderImage` where the crop (or the whole picture) is smaller than
|
|
586
|
+
`width` or `height`.
|
|
587
|
+
**Why:** a picture is never enlarged.
|
|
588
|
+
**Fix:** clamp the target to the crop, keeping the ratio, or answer 422.
|
|
589
|
+
|
|
590
|
+
```ts
|
|
591
|
+
const scale = Math.min(1, crop.width / 800, crop.height / 450);
|
|
592
|
+
await renderImage(blob, {
|
|
593
|
+
crop,
|
|
594
|
+
width: Math.floor(800 * scale),
|
|
595
|
+
height: Math.floor(450 * scale),
|
|
596
|
+
format: 'jpeg',
|
|
597
|
+
});
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
### `format must be 'jpeg', 'webp' or 'png', got <value>`, and the `quality` `TypeError`
|
|
601
|
+
|
|
602
|
+
`TypeError`.
|
|
603
|
+
|
|
604
|
+
**When:** `renderImage` with another `format`, a `quality` beside
|
|
605
|
+
`format: 'png'` (`quality applies to JPEG and WebP only: a PNG is always lossless`),
|
|
606
|
+
or a `width`, `height`, `quality`, `maxBytes` or `maxPixels` that is not a whole
|
|
607
|
+
number in range (`<option> must be a whole number of <range>, got <value>`).
|
|
608
|
+
**Why:** these are the caller's code, checked on every call.
|
|
609
|
+
**Fix:** pass `'jpeg'`, `'webp'` or `'png'`, leave `quality` out for PNG, and keep
|
|
610
|
+
`width` and `height` between 1 and 8192.
|
|
611
|
+
|
|
612
|
+
## Storage
|
|
613
|
+
|
|
614
|
+
### `<call>: size <n> is not one of the set's sizes (<sizes>)`
|
|
615
|
+
|
|
616
|
+
`RangeError`, from `@softistx/image/s3`, for example
|
|
617
|
+
`presignGet: size 128 is not one of the set's sizes (256, 64)`. `<call>` is
|
|
618
|
+
`presignGet`, `bytes` or `keyFor` for a caller; `replace` and `remove` use the
|
|
619
|
+
same text internally.
|
|
620
|
+
|
|
621
|
+
**When:** `presignGet`, `bytes` or `keyFor` of a store, with a `size` the set
|
|
622
|
+
does not produce. It is the caller's bug, so it is a `RangeError`, not an
|
|
623
|
+
`ImageError`; a route that takes the size from a URL checks it against
|
|
624
|
+
`set.sizes` first and answers 404 (see [storing](guide/storing.md#serving)).
|
|
625
|
+
**Why:** the store's sizes are the set's sizes, and a key for another size would
|
|
626
|
+
point at nothing.
|
|
627
|
+
**Fix:** read the sizes off the set rather than writing numbers twice:
|
|
628
|
+
|
|
629
|
+
```ts
|
|
630
|
+
import { defineImageSet } from '@softistx/image';
|
|
631
|
+
import { bindImageStore } from '@softistx/image/s3';
|
|
632
|
+
import { bindBucket, defineBucket } from '@softistx/s3';
|
|
633
|
+
|
|
634
|
+
const avatarBucket = defineBucket({
|
|
635
|
+
bucket: 'avatars',
|
|
636
|
+
key: (p: { imageId: string; size: number }) =>
|
|
637
|
+
`avatars/${p.imageId}/${p.size}.webp`,
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
const set = defineImageSet({ sizes: [256, 64] });
|
|
641
|
+
const store = bindImageStore(
|
|
642
|
+
set,
|
|
643
|
+
bindBucket(avatarBucket, {
|
|
644
|
+
endpoint: 'https://s3.example.com', // your own, from the environment
|
|
645
|
+
accessKeyId: 'access-key',
|
|
646
|
+
secretAccessKey: 'secret-key',
|
|
647
|
+
}),
|
|
648
|
+
);
|
|
649
|
+
const imageId = crypto.randomUUID();
|
|
650
|
+
store.presignGet({ imageId }, set.sizes[0]);
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
Refusals of the upload (`TOO_LARGE`, `UNSUPPORTED_TYPE`, ...) happen in
|
|
654
|
+
`replace` before anything is written, so a refused upload stored nothing.
|
|
655
|
+
|
|
656
|
+
### An S3 error from `replace`
|
|
657
|
+
|
|
658
|
+
**When:** a put to the bucket fails inside `replace`: a bad credential, a
|
|
659
|
+
network error, a missing bucket, a refused write (a bucket that does not accept
|
|
660
|
+
the set's `contentType` — `image/webp`, or `image/png` for a `format: 'png'`
|
|
661
|
+
set — or whose `maxSize` is under the largest variant) — or, since
|
|
662
|
+
`@softistx/s3` 0.6.0, a `TIMEOUT`: the bucket's `timeoutMs` bounds each variant's
|
|
663
|
+
put, so a default sized for reads can cut an upload (`replace` takes no
|
|
664
|
+
per-call timeout).
|
|
665
|
+
**Why:** the store does not wrap the service's errors. Every put is awaited,
|
|
666
|
+
then a best-effort delete removes the keys whose put succeeded (never the
|
|
667
|
+
refused one), and the first error is rethrown **unchanged**, so `@softistx/s3`'s
|
|
668
|
+
own handling and messages apply. A put that failed after sending (a network
|
|
669
|
+
error) may still land on the server after the cleanup.
|
|
670
|
+
**Fix:** for a `TIMEOUT`, raise the bucket's `timeoutMs` default. Otherwise
|
|
671
|
+
read the cause in [`@softistx/s3`'s troubleshooting](https://github.com/softistx/nxgt-data/blob/develop/packages/s3/docs/troubleshooting.md)
|
|
672
|
+
and use a fresh id per upload, so that the cleanup never deletes an image that
|
|
673
|
+
is in use:
|
|
674
|
+
|
|
675
|
+
```ts
|
|
676
|
+
const imageId = crypto.randomUUID();
|
|
677
|
+
const { version } = await store.replace({ imageId }, await request.blob());
|
|
678
|
+
await store.remove({ imageId: former }); // only after the new one is stored
|
|
679
|
+
```
|
|
680
|
+
|
|
681
|
+
With fixed params, a failed `replace` leaves the object under the refused key
|
|
682
|
+
as it was, but the keys that were written by this call are removed, so the
|
|
683
|
+
image can end up with a mix of old and missing sizes: use a fresh id.
|
|
684
|
+
|
|
685
|
+
### `remove` throws `NoSuchKey`, or the bytes stay after `remove`
|
|
686
|
+
|
|
687
|
+
**When:** `remove` on a service that answers 404 to a delete of a missing object
|
|
688
|
+
(for example GCS), or on a versioned bucket.
|
|
689
|
+
**Why:** a 404 answer is thrown as `NoSuchKey`, not swallowed; and a versioned
|
|
690
|
+
bucket keeps the bytes behind delete markers.
|
|
691
|
+
**Fix:** save the new id first, then remove the former image, never inside a
|
|
692
|
+
transaction that may roll back; catch `NoSuchKey` where the service answers it,
|
|
693
|
+
and expire noncurrent versions with a lifecycle rule.
|
|
694
|
+
|
|
695
|
+
### The bucket is bound with a public `acl`
|
|
696
|
+
|
|
697
|
+
**When:** `bindBucket` was given `acl: 'public-read'` as a default.
|
|
698
|
+
**Why:** Bun sends it on every put, and the store promises objects that are
|
|
699
|
+
never public, but it does not pass an `acl` of its own, so the default wins;
|
|
700
|
+
on AWS a bucket with ACLs disabled refuses it.
|
|
701
|
+
**Fix:** bind the bucket without an `acl`. A bucket policy can still make
|
|
702
|
+
objects public if you want that.
|
|
703
|
+
|
|
704
|
+
## Errors that are not an `ImageError`
|
|
705
|
+
|
|
706
|
+
**When:** `process` throws something that is neither an `ImageError` nor one of
|
|
707
|
+
the `TypeError` or `RangeError` above: an out-of-memory or an unexpected Bun failure.
|
|
708
|
+
**Why:** only the Bun errors whose `code` means a refusal of the upload
|
|
709
|
+
(`ERR_IMAGE_TOO_MANY_PIXELS`, `ERR_IMAGE_FORMAT_UNSUPPORTED`,
|
|
710
|
+
`ERR_IMAGE_DECODE_FAILED`, `ERR_IMAGE_UNKNOWN_FORMAT`) are turned into an
|
|
711
|
+
`ImageError`, with Bun's error as `error.cause`. Any other error is rethrown
|
|
712
|
+
untouched, so a bug or an out-of-memory is never taken for a bad upload.
|
|
713
|
+
**Fix:** answer a 4xx only for an `ImageError`, and let the rest be a 5xx:
|
|
714
|
+
|
|
715
|
+
```ts
|
|
716
|
+
try {
|
|
717
|
+
await avatars.process(blob);
|
|
718
|
+
} catch (error) {
|
|
719
|
+
if (error instanceof ImageError) {
|
|
720
|
+
return new Response(error.message, { status: 422 });
|
|
721
|
+
}
|
|
722
|
+
throw error; // a bug, or the runtime: log it, answer 500
|
|
723
|
+
}
|
|
724
|
+
```
|