@softistx/alxia-media 0.0.0-stage → 0.12.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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +373 -2
  3. package/dist/body.d.ts +24 -0
  4. package/dist/body.d.ts.map +1 -0
  5. package/dist/bucket.d.ts +21 -0
  6. package/dist/bucket.d.ts.map +1 -0
  7. package/dist/checks.d.ts +11 -0
  8. package/dist/checks.d.ts.map +1 -0
  9. package/dist/chunks/index-8s779ve6.js +106 -0
  10. package/dist/chunks/index-8s779ve6.js.map +10 -0
  11. package/dist/headers.d.ts +19 -0
  12. package/dist/headers.d.ts.map +1 -0
  13. package/dist/image/index.d.ts +2 -0
  14. package/dist/image/index.d.ts.map +1 -0
  15. package/dist/image/index.js +199 -0
  16. package/dist/image/index.js.map +11 -0
  17. package/dist/image/peer.d.ts +14 -0
  18. package/dist/image/peer.d.ts.map +1 -0
  19. package/dist/image/rendition.d.ts +109 -0
  20. package/dist/image/rendition.d.ts.map +1 -0
  21. package/dist/index.d.ts +9 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +825 -0
  24. package/dist/index.js.map +22 -0
  25. package/dist/key.d.ts +40 -0
  26. package/dist/key.d.ts.map +1 -0
  27. package/dist/kind-callbacks.d.ts +28 -0
  28. package/dist/kind-callbacks.d.ts.map +1 -0
  29. package/dist/kind-check.d.ts +20 -0
  30. package/dist/kind-check.d.ts.map +1 -0
  31. package/dist/kind.d.ts +173 -0
  32. package/dist/kind.d.ts.map +1 -0
  33. package/dist/range.d.ts +17 -0
  34. package/dist/range.d.ts.map +1 -0
  35. package/dist/read.d.ts +11 -0
  36. package/dist/read.d.ts.map +1 -0
  37. package/dist/refusal.d.ts +90 -0
  38. package/dist/refusal.d.ts.map +1 -0
  39. package/dist/rendition.d.ts +94 -0
  40. package/dist/rendition.d.ts.map +1 -0
  41. package/dist/routes.d.ts +62 -0
  42. package/dist/routes.d.ts.map +1 -0
  43. package/dist/sniff.d.ts +16 -0
  44. package/dist/sniff.d.ts.map +1 -0
  45. package/dist/write.d.ts +42 -0
  46. package/dist/write.d.ts.map +1 -0
  47. package/docs/README.md +11 -0
  48. package/docs/guide.md +902 -0
  49. package/docs/roadmap.md +73 -0
  50. package/docs/troubleshooting.md +563 -0
  51. package/package.json +65 -5
@@ -0,0 +1,73 @@
1
+ # Roadmap
2
+
3
+ What `@softistx/alxia-media` gives an app, and what is coming. This page is a
4
+ direction, not a commitment: the version something shipped in is the only
5
+ number on it. Every release, with each change it made, will be in the
6
+ package's `CHANGELOG.md`.
7
+
8
+ ## Now
9
+
10
+ The opt-in hooks for tenant-scoped, cropped pictures (softistx/alxia#633) are
11
+ merged for the next minor, 0.3.0; see Shipped, Unreleased.
12
+
13
+ - **Moving to the `@softistx` scope.** The package is now `@softistx/alxia-media` (it was `@alxia/media`). Same code and API; replace the dependency and the imports ([migration guide](https://github.com/softistx/alxia/blob/develop/packages/core/docs/guide/migrating-from-alxia-scope.md)). The old name is deprecated.
14
+
15
+ ## Next
16
+
17
+ Reading a stored rendition in another format (`?format=png`) for the kinds that
18
+ declare it.
19
+
20
+ ## Later
21
+
22
+ Nothing scheduled yet.
23
+
24
+ ## Not planned
25
+
26
+ - **Presigned URLs and a public media hostname.** The point of the package is
27
+ that the bucket stays private and the server answers.
28
+ - **Transcoding in the routes.** An upload is stored as it was sent, after the
29
+ checks. Resizing belongs to the image subpath, opt-in.
30
+ - **URLs in responses.** The routes return keys; the page builds the path.
31
+
32
+ ## Shipped
33
+
34
+ ### Unreleased
35
+
36
+ - Per-type storage in one kind: `keepOriginal: [types]`, `renditionTypes`, `attachment`
37
+ (forced `Content-Disposition: attachment`), and `withImageRendition`'s
38
+ `enlarge: false` and `fit: 'inside'` (softistx/alxia#792).
39
+ - `withImageRendition({ full: { fit: 'inside', maxSide } })`: the whole picture, aspect
40
+ ratio kept, never enlarged, metadata stripped, besides the squares
41
+ (softistx/alxia#788).
42
+
43
+ ### 0.7.0
44
+
45
+ - `quota(ctx, { declared, type })` on a kind: refuse an upload over a storage
46
+ quota from its declared `Content-Length`, before the body is read
47
+ (softistx/alxia#757).
48
+ - `accept: '*'`: a kind that stores any content type (known signatures sniffed,
49
+ the rest as declared), with only the sniffable types and `text/plain` shown
50
+ in the page and every other file a download (softistx/alxia#756).
51
+
52
+ ### 0.3.0
53
+
54
+ - Opt-in hooks for tenant-scoped, cropped pictures: `crop` on `withImageRendition`,
55
+ `keepOriginal: false`, `keyPrefix`, refusals of your own from the `authorize*`
56
+ callbacks, refusal parameters and the `uploaded` response hook.
57
+ - `@softistx/image` peer range is `^0.5.0` (now `^0.5.0 || ^0.6.0`); PNG renditions are encoded by it directly.
58
+
59
+ ### 0.2.0
60
+
61
+ Published as 0.2.0.
62
+
63
+ - `defineMediaKind`: a kind of file, with its authorization, content types,
64
+ size limit and throttle.
65
+ - `mediaRoutes`: `PUT`, `GET` (Range, ETag, 304) and optional `DELETE` over a
66
+ private bucket, streamed both ways, with typed refusals.
67
+ - `@alxia/media/image`: `withImageRendition` makes the sizes of each uploaded
68
+ picture at upload, served by the same routes; a kind's `renditions` option
69
+ lets an app make renditions of its own.
70
+ - `@alxia/react-router/media`: the same-site proxy, so a browser never talks
71
+ to the API directly; the session's Bearer, the declared length checked
72
+ before the API is called, a header deadline, and an allowlist of the
73
+ response headers.
@@ -0,0 +1,563 @@
1
+ # Troubleshooting
2
+
3
+ Each entry is headed by what you see: a response, a thrown message or a
4
+ TypeScript error.
5
+
6
+ **Install**
7
+
8
+ - [`404 / no version matching, or 401 from npm.softistx.com`](#404--no-version-matching-or-401-from-npmsoftistxcom)
9
+
10
+ **Startup**
11
+
12
+ - [`TypeError: defineMediaKind(): the name … must be lower-case words and digits joined by hyphens, starting with a letter`](#typeerror-definemediakind-the-name--must-be-lower-case-words-and-digits-joined-by-hyphens-starting-with-a-letter)
13
+ - [`TypeError: defineMediaKind(…): maxBytes must be a whole number of bytes, 1 or more`](#typeerror-definemediakind-maxbytes-must-be-a-whole-number-of-bytes-1-or-more)
14
+ - [`TypeError: defineMediaKind(…): accept lists no content type`](#typeerror-definemediakind-accept-lists-no-content-type)
15
+ - [`TypeError: defineMediaKind(…): the size … is not a name`](#typeerror-definemediakind-the-size--is-not-a-name)
16
+ - [`TypeError: defineMediaKind(…): give authorizeRead, or public: true`](#typeerror-definemediakind-give-authorizeread-or-public-true)
17
+ - [`TypeError: defineMediaKind(…): no signature is known for …`](#typeerror-definemediakind-no-signature-is-known-for-)
18
+ - [`TypeError: defineMediaKind(…): … is not an exact content type`](#typeerror-definemediakind--is-not-an-exact-content-type)
19
+ - [`TypeError: mediaRoutes(): the kind … is given twice`](#typeerror-mediaroutes-the-kind--is-given-twice)
20
+ - [`TypeError: mediaRoutes(): prefix … must start with "/"`](#typeerror-mediaroutes-prefix--must-start-with-)
21
+ - [`the plugin reads "viewer", which this app's context does not give`](#the-plugin-reads-viewer-which-this-apps-context-does-not-give)
22
+ - [`TypeError: defineMediaKind(…): accept: '*' takes no renditions, …`](#typeerror-definemediakind-accept--takes-no-renditions-)
23
+ - [`TypeError: defineMediaKind(…): renditions needs sizes, the names of what it makes`](#typeerror-definemediakind-renditions-needs-sizes-the-names-of-what-it-makes)
24
+ - [`TypeError: defineMediaKind(…): renditions lists no content type`](#typeerror-definemediakind-renditions-lists-no-content-type)
25
+ - [`TypeError: defineMediaKind(…): renditions type … is not an exact content type`](#typeerror-definemediakind-renditions-type--is-not-an-exact-content-type)
26
+ - [`TypeError: defineMediaKind(…): cacheControl must be a non-empty Cache-Control value on one line`](#typeerror-definemediakind-cachecontrol-must-be-a-non-empty-cache-control-value-on-one-line)
27
+ - [`TypeError: defineMediaKind(…): keepOriginal: false needs renditions, or nothing would be stored`](#typeerror-definemediakind-keeporiginal-false-needs-renditions-or-nothing-would-be-stored)
28
+ - [`TypeError: @softistx/alxia-media: keyPrefix gave …`](#typeerror-softistxalxia-media-keyprefix-gave-)
29
+ - [One tenant sees another tenant's picture through a CDN](#one-tenant-sees-another-tenants-picture-through-a-cdn)
30
+ - [`TypeError: @softistx/alxia-media: an authorize callback answers true, false or { code, status }`](#typeerror-softistxalxia-media-an-authorize-callback-answers-true-false-or--code-status-)
31
+ - [`TypeError: withImageRendition(): quality is the WebP quality …`](#typeerror-withimagerendition-quality-is-the-webp-quality-)
32
+ - [`TypeError: withImageRendition(): crop must be "query" or a function of the request`](#typeerror-withimagerendition-crop-must-be-query-or-a-function-of-the-request)
33
+ - [`@softistx/image needs Bun 1.4.1 or later, where Bun.Image exists`](#softistximage-needs-bun-141-or-later-where-bunimage-exists)
34
+ - [`Error: @softistx/alxia-media/image needs @softistx/image, an optional peer that is not installed: bun add @softistx/image`](#error-softistxalxia-mediaimage-needs-softistximage-an-optional-peer-that-is-not-installed-bun-add-softistximage)
35
+ - [`error: Could not resolve: "@softistx/image". Maybe you need to "bun install"?`](#error-could-not-resolve-softistximage-maybe-you-need-to-bun-install)
36
+ - [`TypeError: withImageRendition(): format must be "png" or "webp", got …`](#typeerror-withimagerendition-format-must-be-png-or-webp-got-)
37
+ - [`TypeError: withImageRendition(): give sizes …, full …, or both`, and `full must be { fit: "inside", maxSide }`](#typeerror-withimagerendition-give-sizes--full--or-both-and-full-must-be--fit-inside-maxside-)
38
+ - [`TypeError: sizes must be unique, got […]`, or another option message from `withImageRendition`](#typeerror-sizes-must-be-unique-got--or-another-option-message-from-withimagerendition)
39
+
40
+ **Uploads**
41
+
42
+ - [`415 unsupported_type` for a file that is the right type](#415-unsupported_type-for-a-file-that-is-the-right-type)
43
+ - [`413 too_large` for a file under the limit](#413-too_large-for-a-file-under-the-limit)
44
+ - [`400 empty_body`](#400-empty_body)
45
+ - [`400 size_mismatch` or `400 interrupted`](#400-size_mismatch-or-400-interrupted)
46
+ - [`408 stalled`](#408-stalled)
47
+ - [`429 throttled`](#429-throttled)
48
+ - [A `quota` refusal](#a-quota-refusal)
49
+ - [`503 unavailable` after a long upload](#503-unavailable-after-a-long-upload)
50
+ - [`422 unprocessable`](#422-unprocessable)
51
+ - [`400 invalid_crop`](#400-invalid_crop)
52
+ - [`415 unsupported_type` for a GIF or a HEIC sent to an image kind](#415-unsupported_type-for-a-gif-or-a-heic-sent-to-an-image-kind)
53
+ - [`TypeError: <kind>: renditions made the size …, which sizes does not list`](#typeerror-kind-renditions-made-the-size--which-sizes-does-not-list)
54
+ - [`TypeError: <kind>: renditions made a …, which renditions.types does not list`](#typeerror-kind-renditions-made-a--which-renditionstypes-does-not-list)
55
+ - [`Error: @softistx/alxia-media: <kind>/<key> was gone right after its upload`](#error-softistxalxia-media-kindkey-was-gone-right-after-its-upload)
56
+
57
+ **Reads**
58
+
59
+ - [`404` for a file that exists](#404-for-a-file-that-exists)
60
+ - [Audio does not seek, or Safari will not play it](#audio-does-not-seek-or-safari-will-not-play-it)
61
+ - [The browser downloads a file instead of showing it](#the-browser-downloads-a-file-instead-of-showing-it)
62
+ - [A file is fetched again on every page load](#a-file-is-fetched-again-on-every-page-load)
63
+ - [`416` with `Content-Range: bytes */…`](#416-with-content-range-bytes-)
64
+ - [`404` at a size, for a picture uploaded before the size was added](#404-at-a-size-for-a-picture-uploaded-before-the-size-was-added)
65
+ - [`404` at a size the kind declares, for a file that exists](#404-at-a-size-the-kind-declares-for-a-file-that-exists)
66
+ - [`403 forbidden` on a `DELETE`](#403-forbidden-on-a-delete)
67
+ - [`404 not_found` on a `DELETE`](#404-not_found-on-a-delete)
68
+
69
+ ## Install
70
+
71
+ ### `404 / no version matching, or 401 from npm.softistx.com`
72
+
73
+ **When:** `bun add` or `bun install` of `@softistx/s3` or `@softistx/image`: against npmjs with a range older than the versions below (404 / no version matching), or through a leftover `@softistx` scope entry pointing at `npm.softistx.com` without its read token (401).
74
+
75
+ **Why:** `@softistx/s3` before 0.13.1 and `@softistx/image` before 0.6.1 were never published on npmjs, which is the only registry of `@softistx`. The 401 comes from `npm.softistx.com` itself, which apps no longer hold a token for.
76
+
77
+ **Fix:** drop the `@softistx` scope entry from `bunfig.toml` (or `.npmrc`) and bump to `@softistx/s3` `^0.13.1` and, for `@softistx/alxia-media/image`, `@softistx/image` `^0.6.1`.
78
+
79
+ ## Startup
80
+
81
+ ### `TypeError: defineMediaKind(): the name … must be lower-case words and digits joined by hyphens, starting with a letter`
82
+
83
+ The name is a URL segment and the bucket folder, so it is held to
84
+ `[a-z][a-z0-9]*(-[a-z0-9]+)*`: `avatars`, `question-files`, `v2-scans`. Not
85
+ `Avatars`, `question_files`, `2-scans` or `../x`. Rename the kind; for objects
86
+ already in a bucket, name it after their folder.
87
+
88
+ ### `TypeError: defineMediaKind(…): maxBytes must be a whole number of bytes, 1 or more`
89
+
90
+ `maxBytes` is a safe integer, 1 or more: `10 * 1024 * 1024`, not `0`, `1.5`,
91
+ `Infinity` or a string read from the environment. Convert it first:
92
+
93
+ ```ts
94
+ import { defineMediaKind } from '@softistx/alxia-media';
95
+
96
+ export const scans = defineMediaKind({
97
+ name: 'scans',
98
+ accept: ['application/pdf'],
99
+ maxBytes: Number.parseInt(process.env['SCAN_MAX_BYTES'] ?? '10485760', 10),
100
+ authorizeWrite: () => true,
101
+ authorizeRead: () => true,
102
+ });
103
+ ```
104
+
105
+ ### `TypeError: defineMediaKind(…): accept lists no content type`
106
+
107
+ `accept` is empty, so no upload could ever pass. List the exact types the kind
108
+ takes (`['image/png', 'image/jpeg']`). A kind that is only read (its files are
109
+ written elsewhere) still lists the types it serves. A kind that must store
110
+ any file (a file manager) declares `accept: '*'`.
111
+
112
+ ### `TypeError: defineMediaKind(…): the size … is not a name`
113
+
114
+ A `sizes` entry becomes a URL segment and part of the bucket key
115
+ (`<kind>/<key>/<size>`), so it is lower-case words and digits joined by
116
+ hyphens: `thumb`, `256`, `w-640`; not `Thumb`, `w_640` or `../x`.
117
+
118
+ ### `TypeError: defineMediaKind(…): give authorizeRead, or public: true`
119
+
120
+ A private kind has no `authorizeRead`. The types refuse it too, unless the
121
+ input was cast. Give the check, or say that anyone may read the files:
122
+
123
+ ```ts
124
+ import { defineMediaKind } from '@softistx/alxia-media';
125
+
126
+ export const logos = defineMediaKind({
127
+ name: 'logos',
128
+ accept: ['image/png'],
129
+ maxBytes: 256 * 1024,
130
+ authorizeWrite: () => true,
131
+ public: true, // served `public`, readable cross-origin, with no check
132
+ });
133
+ ```
134
+
135
+ ### `TypeError: defineMediaKind(…): no signature is known for …`
136
+
137
+ The kind accepts a type whose first bytes this package cannot check, so it
138
+ could not refuse a file that lies about its type. Either accept only the
139
+ types it knows (PNG, JPEG, GIF, WebP, AVIF, PDF, MP3, AAC (`audio/aac`, raw ADTS), Ogg,
140
+ WAV, MP4 audio (`audio/mp4`), MP4 video, QuickTime (`video/quicktime`, an
141
+ iPhone `.mov`), WebM), or, when the kind mixes them with types it
142
+ does not know (docx, xlsx, CSV), set `sniff: 'known'`: the known types keep
143
+ their first-bytes check and the others are taken as declared. `sniff: false`
144
+ checks none:
145
+
146
+ ```ts
147
+ import { defineMediaKind } from '@softistx/alxia-media';
148
+
149
+ export const exports_ = defineMediaKind({
150
+ name: 'exports',
151
+ accept: ['text/csv'],
152
+ sniff: false, // the type is taken as declared
153
+ maxBytes: 1024 * 1024,
154
+ authorizeWrite: () => true,
155
+ authorizeRead: () => true,
156
+ });
157
+ ```
158
+
159
+ A kind that mixes known and unknown types keeps the check for the known ones:
160
+
161
+ ```ts
162
+ export const documents = defineMediaKind({
163
+ name: 'documents',
164
+ accept: ['application/pdf', 'text/csv'],
165
+ sniff: 'known', // the PDF is checked, the CSV is taken as declared
166
+ maxBytes: 1024 * 1024,
167
+ authorizeWrite: () => true,
168
+ authorizeRead: () => true,
169
+ });
170
+ ```
171
+
172
+ A file served with `nosniff` and `sandbox` CSP is still not interpreted by the
173
+ browser as anything but its stored type.
174
+
175
+ ### `TypeError: defineMediaKind(…): … is not an exact content type`
176
+
177
+ `accept` lists exact types: `image/png`, not `image/*`, not
178
+ `image/png; charset=…`, not `IMAGE/PNG`. List each type, or give
179
+ `accept: '*'` (the string alone, not `['*']` among others) to take any type.
180
+
181
+ ### A file stored under `accept: '*'` downloads instead of showing
182
+
183
+ By design: only the types the package can sniff (raster images, audio, video, PDF) and
184
+ `text/plain` are shown in the page, by exact name; any other type, HTML, SVG
185
+ and every `+xml` type included, is served as
186
+ `application/octet-stream` with `Content-Disposition: attachment`.
187
+
188
+ ### `TypeError: defineMediaKind(…): accept: '*' takes no renditions, …`
189
+
190
+ `renditions` make a size from the types they list, which a kind taking any type
191
+ cannot name. Keep `sizes` (names only) on the `accept: '*'` kind and write them
192
+ from your own worker at `mediaPlace(kind, prefix)(key, size)`; see the guide's
193
+ "Sizes your own worker writes". The old message, `accept: '*' takes no sizes or
194
+ renditions`, no longer appears: `sizes` alone are accepted.
195
+
196
+ ### `404` at a size the kind declares, for a file that exists
197
+
198
+ With no `renditions`, nothing makes a size: the app writes it. It answers `404`
199
+ until your worker has stored the object at `mediaPlace(kind, prefix)(key,
200
+ size)` (same `prefix` as `keyPrefix`, no extension). Check the key in the
201
+ bucket.
202
+
203
+ ### `TypeError: mediaRoutes(): the kind … is given twice`
204
+
205
+ Two kinds share a `name`, which is their URL segment and bucket folder. Rename
206
+ one.
207
+
208
+ ### `TypeError: mediaRoutes(): prefix … must start with "/"`
209
+
210
+ The `prefix` option is a path on your origin: `/media`, `/api/files`, not
211
+ `media` or `https://cdn…`. A trailing `/` is dropped.
212
+
213
+ ### `the plugin reads "viewer", which this app's context does not give`
214
+
215
+ The kinds' callbacks read `viewer` (`defineMediaKind<{ viewer: … }>`), and
216
+ nothing before `.plugin(mediaRoutes(…))` adds it. Mount the routes after the
217
+ middleware or `derive` that gives it. See
218
+ [core's troubleshooting](https://github.com/softistx/alxia/blob/develop/packages/core/docs/troubleshooting.md).
219
+
220
+ ### `TypeError: defineMediaKind(…): renditions needs sizes, the names of what it makes`
221
+
222
+ A kind was given `renditions` and no `sizes`. Each rendition is stored and
223
+ served under a size, so list them: `sizes: ['text']`. `withImageRendition`
224
+ gives both. A `sizes` written after the spread overrides the image's, which
225
+ leaves its renditions unmatched, so write none beside it.
226
+
227
+ ### `TypeError: defineMediaKind(…): renditions lists no content type`
228
+
229
+ `renditions.types` is empty. List the exact types `make` returns:
230
+ `types: ['image/png']`.
231
+
232
+ ### `TypeError: defineMediaKind(…): renditions type … is not an exact content type`
233
+
234
+ A `renditions.types` entry has a wildcard, parameters or upper case:
235
+ `image/*`, `text/plain; charset=utf-8`, `Image/PNG`. Give `image/png`,
236
+ `text/plain`.
237
+
238
+ ### `TypeError: defineMediaKind(…): keepOriginal: false needs renditions, or nothing would be stored`
239
+
240
+ The kind says `keepOriginal: false` and has no `renditions`: the upload would
241
+ be stored nowhere.
242
+
243
+ **Fix:** add `...withImageRendition({ sizes: [...] })` (or your own
244
+ `renditions` and `sizes`), or drop `keepOriginal`. A `keepOriginal` list of
245
+ types needs `renditions` too.
246
+
247
+ ### `TypeError: defineMediaKind(…): an upload of <type> would store nothing`
248
+
249
+ A kind with `keepOriginal: false` or a list of types accepts `<type>`, but it is
250
+ neither in the `keepOriginal` list nor in `renditionTypes`. **Fix:** add it to
251
+ one of them. Other declaration errors of the same options (`keepOriginal lists
252
+ <type>, which accept does not take`, `attachment must be a boolean or a list of
253
+ types`) name the option and the type to correct. `withImageRendition` throws a
254
+ `TypeError` for a `fit` other than `cover` or `inside`, a non-boolean `enlarge`,
255
+ and a `crop` with `fit: 'inside'`.
256
+
257
+ ### `TypeError: defineMediaKind(…): cacheControl must be a non-empty Cache-Control value on one line`
258
+
259
+ The kind's `cacheControl` is empty, blank, or has a line break in it.
260
+
261
+ **Fix:** give one header value, for example `cacheControl: 'private, no-cache'`.
262
+
263
+ ### `TypeError: defineMediaKind(…): renditions keyExtension must be lower-case letters and digits with no dot`
264
+
265
+ A custom `renditions` sets `keyExtension: '.webp'` (or `'WEBP'`).
266
+
267
+ **Fix:** write the extension alone: `keyExtension: 'webp'`.
268
+
269
+ ### Reads of an existing `.../256.webp` object answer `404`
270
+
271
+ Rendition keys carry no extension by default (`<key>/256`).
272
+
273
+ **Fix:** declare `withImageRendition({ ..., keyExtension: true })` (see the
274
+ guide, "Keeping the file extension on rendition keys").
275
+
276
+ ### `TypeError: @softistx/alxia-media: keyPrefix gave …`
277
+
278
+ The `keyPrefix` hook returned something other than folders of letters,
279
+ digits and `. _ = -` joined by `/` (`''` and `undefined` mean none). It is
280
+ thrown at the request, a `500`, and nothing is stored: a prefix is never
281
+ cleaned up, since changing it could place an object in another tenant's folder.
282
+
283
+ **Fix:** return `tenants/${id}` with an id you control, not user input as is;
284
+ to refuse a request with no tenant, throw `new MediaRefusal('forbidden')`.
285
+
286
+ ### One tenant sees another tenant's picture through a CDN
287
+
288
+ A public kind is served `public, max-age=31536000, immutable`. A `keyPrefix`
289
+ that reads a header or the host is invisible to a shared cache, which keeps the
290
+ first tenant's answer under the URL. **Fix:** make the kind private, add
291
+ `Vary` on the header, or put the tenant in the URL.
292
+
293
+ ### `TypeError: @softistx/alxia-media: an authorize callback answers true, false or { code, status }`
294
+
295
+ An `authorize*` callback returned an object whose `code` is not snake_case
296
+ (`in_use`, not `In Use`), or whose `status` is not a whole number from 400 to
297
+ 599 (a `200` is not a refusal), or that left `status` out for a code of its own (only a code of the package, such as `unauthenticated`, has a status by default). It is a `500`.
298
+
299
+ **Fix:** return `{ code: 'in_use', status: 409 }`, or `false` for the default
300
+ refusal.
301
+
302
+ ### `TypeError: withImageRendition(): quality is the WebP quality …`
303
+
304
+ `quality` was given with `format: 'png'` (the default format). A PNG is not lossy.
305
+
306
+ **Fix:** drop `quality`, or use `format: 'webp'`.
307
+
308
+ ### `TypeError: withImageRendition(): crop must be "query" or a function of the request`
309
+
310
+ `crop` was given something else (`true`, an object).
311
+
312
+ **Fix:** `crop: 'query'`, or `crop: (request) => ({ x, y, size })`.
313
+
314
+ ### `@softistx/image needs Bun 1.4.1 or later, where Bun.Image exists`
315
+
316
+ Thrown as a `TypeError` by `@softistx/image` when `withImageRendition` is
317
+ called on an older Bun, or on Node: the pictures are decoded and encoded by
318
+ `Bun.Image`, which Bun 1.4.1 added. Upgrade Bun (`bun upgrade`) and run the
319
+ server with it; in a Dockerfile, use an `oven/bun` image of 1.4.1 or later.
320
+
321
+ ### `Error: @softistx/alxia-media/image needs @softistx/image, an optional peer that is not installed: bun add @softistx/image`
322
+
323
+ `@softistx/alxia-media/image` imports `@softistx/image` when it is itself imported, so
324
+ the app fails at startup, not at the first upload. Install the peer. It is public on
325
+ npmjs:
326
+
327
+ ```sh
328
+ bun add @softistx/image
329
+ ```
330
+
331
+ An app that imports only `@softistx/alxia-media` never needs it.
332
+
333
+ ### `error: Could not resolve: "@softistx/image". Maybe you need to "bun install"?`
334
+
335
+ Bun's own message, from `bun build` bundling a server that imports
336
+ `@softistx/alxia-media/image` without the peer installed: the subpath imports it by a
337
+ literal specifier, so the bundler looks for it and puts it in the bundle.
338
+ Install it as in the entry above and build again.
339
+
340
+ ### `TypeError: withImageRendition(): format must be "png" or "webp", got …`
341
+
342
+ `format` is the type each size is **stored** as: `'png'` (the default) or
343
+ `'webp'`, nothing else. It does not limit what may be uploaded, which is the
344
+ kind's `accept`.
345
+
346
+ ### `TypeError: withImageRendition(): give sizes …, full …, or both`, and `full must be { fit: "inside", maxSide }`
347
+
348
+ `withImageRendition` needs at least one rendition: `sizes` (the squares), `full`
349
+ (the whole picture) or both. `full` is exactly `{ fit: 'inside', maxSide }`,
350
+ with `maxSide` a whole number of 1 to 8192; `'inside'` is the only fit.
351
+
352
+ ```ts
353
+ withImageRendition({ full: { fit: 'inside', maxSide: 1600 } }); // /<key>/full
354
+ ```
355
+
356
+ ### `TypeError: sizes must be unique, got […]`, or another option message from `withImageRendition`
357
+
358
+ `withImageRendition` hands `sizes`, `minSide`, `maxPixels`, `quality` and
359
+ `concurrency` to `@softistx/image`'s `defineImageSet`, which checks them where
360
+ the kind is declared: `sizes must be a non-empty array of whole numbers` (when `full` is not set),
361
+ `sizes[] must be a whole number of 1 to 4096, got …`, `quality must be a
362
+ whole number of 1 to 100, got …`. `sizes` are pixels (`[256, 64]`), not names.
363
+ Each message is explained in `@softistx/image`'s troubleshooting page.
364
+
365
+ ## Uploads
366
+
367
+ ### `415 unsupported_type` for a file that is the right type
368
+
369
+ Three causes. The request's `Content-Type` is not in `accept` (the body of the
370
+ refusal lists `accept`). The header is missing: send the file's type, which
371
+ `fetch` does for a `File` body but not for a `Blob` built without one. Or the
372
+ first bytes are not that type's: a file renamed `.png` that is a JPEG, or a PNG
373
+ sent as `application/pdf`. The check reads the bytes, not the name.
374
+
375
+ ### `413 too_large` for a file under the limit
376
+
377
+ `limit` in the body is the kind's `maxBytes`, counted on the bytes of the
378
+ **body**. Send the raw file as the body: a `multipart/form-data` body is larger
379
+ than the file (boundaries) and its type is not the file's. The routes take a
380
+ raw body.
381
+
382
+ A body with no `Content-Length` (chunked) is read up to `maxBytes` and refused
383
+ as soon as it outgrows it.
384
+
385
+ ### `400 empty_body`
386
+
387
+ The body has no bytes. A `fetch` with `body: undefined`, or a zero-length file.
388
+
389
+ ### `400 size_mismatch` or `400 interrupted`
390
+
391
+ `size_mismatch`: the body had a different number of bytes than its
392
+ `Content-Length`, or the header is not a number. `interrupted`: the client went
393
+ away, or the stream failed, part-way. Nothing is stored in either case; retry
394
+ the upload. If a proxy rewrites the body (compression, chunking) without
395
+ fixing the header, stream it through unchanged.
396
+
397
+ ### `408 stalled`
398
+
399
+ The client sent nothing for `writeTimeoutMs` (60 s by default) in the middle of
400
+ its body: a dropped connection the server did not see close, a proxy that
401
+ buffers and stops, a tab put to sleep. The reader is cancelled and nothing is
402
+ stored; retry the upload. The deadline is idle time and starts over at each
403
+ chunk, so a slow upload that keeps moving is never cut. Raise `writeTimeoutMs`
404
+ for clients that legitimately pause longer.
405
+
406
+ ### `429 throttled`
407
+
408
+ The kind's `throttle` refused. `Retry-After` carries the seconds when it
409
+ returned a number. The permission was already checked, so only callers who may
410
+ upload spend the budget.
411
+
412
+ ### A `quota` refusal
413
+
414
+ The kind's `quota` refused: `403 forbidden` for `false`, or the code and status
415
+ of the `MediaDenial` it answered (`402 quota_exceeded`, say). It runs after the
416
+ `415` and `413` checks, so a wrong type or a file over `maxBytes` never reaches
417
+ it, and before the body is read. Its `declared` is `undefined` for a chunked
418
+ upload with no `Content-Length`: decide there whether to count `maxBytes` or to
419
+ refuse. A `quota` that throws is a `500`, and stores nothing.
420
+
421
+ ### `503 unavailable` after a long upload
422
+
423
+ The bucket did not answer within the deadline. The upload's deadline is idle
424
+ time, not total time: it starts over after each chunk, so it trips on a
425
+ bucket that stopped taking bytes or answering. A client that stopped sending
426
+ is `408 stalled` instead. Check the bucket's health, and raise
427
+ `writeTimeoutMs` if it is legitimately slow.
428
+
429
+ ### `422 unprocessable`
430
+
431
+ The kind's `renditions` could not be made, and nothing was stored. With
432
+ `withImageRendition`, `reason` in the body says why:
433
+
434
+ - `decode`: the first bytes are a PNG, JPEG or WebP and the rest is damaged
435
+ or cut short. Send the whole file;
436
+ - `dimensions` (with `min` in the body, the smallest side in pixels): the short side is under `minSide`, the largest size by
437
+ default, since nothing is enlarged. Ask for a larger picture, or pass a
438
+ smaller `minSide`;
439
+ - `pixels`: the picture claims more than `maxPixels` (25 000 000 by default).
440
+
441
+ A `make` of your own throws `new MediaRefusal('unprocessable', { reason })`
442
+ with its own reasons.
443
+
444
+ ### `400 invalid_crop`
445
+
446
+ The kind's `withImageRendition` has `crop` and the crop the request asked for
447
+ is refused, and nothing was stored. With `crop: 'query'` the upload's
448
+ `?x=&y=&size=` must be all three present, whole numbers of 0 or more, with
449
+ `x + size` and `y + size` inside the picture **as a person sees it** (after
450
+ EXIF rotation, so a portrait photo from a phone has its width and height
451
+ swapped against the stored pixels). A crop function that returns `NaN`
452
+ or a fraction is refused the same way.
453
+
454
+ **Fix:** send three integers that fit; read the picture's displayed size
455
+ before cropping; send none of the three for the centred default.
456
+
457
+ ### `415 unsupported_type` for a GIF or a HEIC sent to an image kind
458
+
459
+ The upload passed `accept` and the sniff, then `@softistx/image` could not
460
+ decode its format: a GIF, an AVIF, or a HEIC on Linux. It decodes PNG, JPEG
461
+ and WebP everywhere. Keep the kind's `accept` to those three.
462
+
463
+ ### `TypeError: <kind>: renditions made the size …, which sizes does not list`
464
+
465
+ The upload answers `500`: a `make` of your own returned a size the kind does
466
+ not list, and the file was removed. Return only the kind's `sizes`, or add the
467
+ size to them.
468
+
469
+ ### `TypeError: <kind>: renditions made a …, which renditions.types does not list`
470
+
471
+ The same, for the type: `make` returned a `contentType` that is not in
472
+ `renditions.types`. Add it, so a read serves it as that type.
473
+
474
+ ### `Error: @softistx/alxia-media: <kind>/<key> was gone right after its upload`
475
+
476
+ The upload's `500`: the file was stored, then the bucket did not have it when
477
+ it was read back for its renditions. Something else deletes in that folder
478
+ (a lifecycle rule, a sweep of unreferenced keys run during the upload): spare
479
+ keys younger than a few minutes.
480
+
481
+ ## Reads
482
+
483
+ ### `404` for a file that exists
484
+
485
+ A refused reader and a missing file answer the same `404` on purpose. Check, in
486
+ this order: the key is the UUID the upload answered (the whole key, lower case,
487
+ no extension); `authorizeRead` returns `true` for this caller (log inside it);
488
+ the object is at `<kind>/<key>` in the bucket the routes were given; the
489
+ `prefix` the page uses is the one the routes were mounted at.
490
+
491
+ ### Audio does not seek, or Safari will not play it
492
+
493
+ The response must be `206` with `Accept-Ranges: bytes`. The routes do that. A
494
+ proxy or page server in front of them usually drops `Range` or buffers the body;
495
+ forward `Range` and the `206` status unchanged. See the guide's
496
+ [proxy section](guide.md#behind-a-proxy-or-another-server).
497
+
498
+ ### The browser downloads a file instead of showing it
499
+
500
+ A stored type the kind does not accept is served as
501
+ `application/octet-stream` with `Content-Disposition: attachment`. The object
502
+ was stored with another `Content-Type` (or none, in a kind with several types).
503
+ Re-upload it through `PUT`, or put it with the right type.
504
+
505
+ A PDF that Chromium shows as a blank page or downloads, where Firefox and
506
+ Safari show it, is the `Content-Security-Policy: sandbox` every file is sent
507
+ with: Chromium's PDF viewer may not render a sandboxed document. Keep the
508
+ header; see the guide's [PDF section](guide.md#pdfs-and-the-sandbox-policy).
509
+
510
+ ### A file is fetched again on every page load
511
+
512
+ Something overrides `Cache-Control`. The routes send `private, max-age=31536000,
513
+ immutable` (`public, …` for a public kind). A middleware that sets
514
+ `Cache-Control: no-store` on every response replaces it: exempt the media
515
+ prefix. A kind that must not be cached for a year sets `cacheControl` (for
516
+ example `'private, no-cache'`, see the guide's caching section). A browser still revalidates on a hard reload; with the `ETag` that is a
517
+ `304` with no body.
518
+
519
+ ### `416` with `Content-Range: bytes */…`
520
+
521
+ The client asked for bytes past the end (`bytes=5000-` on a 1000-byte file). The
522
+ header carries the size. Browsers recover by asking again from the start.
523
+
524
+ ### `404` at a size, for a picture uploaded before the size was added
525
+
526
+ Renditions are made at upload. A size added to `withImageRendition` later
527
+ exists only for pictures uploaded after; an older key answers `404` at it,
528
+ with the other sizes still served. Make the missing renditions once, in a
529
+ script over the existing keys: open the original at `mediaObjectKey(kind,
530
+ key)` (with a `keyPrefix`, `mediaPlace(kind, prefix)(key)`), pass its bytes to the kind's `renditions.make({ key, contentType,
531
+ bytes })`, and `bucket.put` each rendition it returns at
532
+ `mediaObjectKey(kind, key, rendition.size)`, or, with a prefix or a `keyExtension`,
533
+ `mediaPlace(kind, prefix, kind.renditions?.keyExtension)(key, rendition.size)`, with its `contentType`. Or ask
534
+ for the picture again.
535
+
536
+ ### `409 in_use` (or another code of your own) on a `DELETE`
537
+
538
+ `authorizeDelete` answered `{ code, status }` instead of a boolean. That is
539
+ the app's own refusal, answered with that status and `Cache-Control:
540
+ no-store`, and nothing is removed. `params` are in the body beside `error`.
541
+ The same holds for `authorizeWrite` and `authorizeRead`.
542
+
543
+ **Fix:** resolve what the callback says (here, remove the records that still
544
+ carry the key, then delete again).
545
+
546
+ ### `403 forbidden` on a `DELETE`
547
+
548
+ `authorizeDelete(ctx, key)` returned `false` for this caller and this key. A
549
+ kind without `authorizeDelete` has no `DELETE` route at all: that is a `404`
550
+ (see below).
551
+
552
+ **Fix:** log inside `authorizeDelete(ctx, key)`; check the context it reads is
553
+ built before `mediaRoutes` is mounted.
554
+
555
+ ### `404 not_found` on a `DELETE`
556
+
557
+ The route is not there: the kind declares no `authorizeDelete`, so `DELETE
558
+ <prefix>/<kind>/<key>` is not mounted for it, or the key is not a lower-case
559
+ UUID, or the `prefix` differs from the one the routes were mounted at. A
560
+ `DELETE` of a well-formed key whose object is already gone is not an error: it
561
+ answers `204`.
562
+
563
+ **Fix:** add `authorizeDelete` to the kind; send the key the upload answered.