@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.
- package/LICENSE +21 -0
- package/README.md +373 -2
- package/dist/body.d.ts +24 -0
- package/dist/body.d.ts.map +1 -0
- package/dist/bucket.d.ts +21 -0
- package/dist/bucket.d.ts.map +1 -0
- package/dist/checks.d.ts +11 -0
- package/dist/checks.d.ts.map +1 -0
- package/dist/chunks/index-8s779ve6.js +106 -0
- package/dist/chunks/index-8s779ve6.js.map +10 -0
- package/dist/headers.d.ts +19 -0
- package/dist/headers.d.ts.map +1 -0
- package/dist/image/index.d.ts +2 -0
- package/dist/image/index.d.ts.map +1 -0
- package/dist/image/index.js +199 -0
- package/dist/image/index.js.map +11 -0
- package/dist/image/peer.d.ts +14 -0
- package/dist/image/peer.d.ts.map +1 -0
- package/dist/image/rendition.d.ts +109 -0
- package/dist/image/rendition.d.ts.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +825 -0
- package/dist/index.js.map +22 -0
- package/dist/key.d.ts +40 -0
- package/dist/key.d.ts.map +1 -0
- package/dist/kind-callbacks.d.ts +28 -0
- package/dist/kind-callbacks.d.ts.map +1 -0
- package/dist/kind-check.d.ts +20 -0
- package/dist/kind-check.d.ts.map +1 -0
- package/dist/kind.d.ts +173 -0
- package/dist/kind.d.ts.map +1 -0
- package/dist/range.d.ts +17 -0
- package/dist/range.d.ts.map +1 -0
- package/dist/read.d.ts +11 -0
- package/dist/read.d.ts.map +1 -0
- package/dist/refusal.d.ts +90 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/rendition.d.ts +94 -0
- package/dist/rendition.d.ts.map +1 -0
- package/dist/routes.d.ts +62 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/sniff.d.ts +16 -0
- package/dist/sniff.d.ts.map +1 -0
- package/dist/write.d.ts +42 -0
- package/dist/write.d.ts.map +1 -0
- package/docs/README.md +11 -0
- package/docs/guide.md +902 -0
- package/docs/roadmap.md +73 -0
- package/docs/troubleshooting.md +563 -0
- package/package.json +65 -5
package/dist/index.js
ADDED
|
@@ -0,0 +1,825 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MEDIA_REFUSAL_STATUS2,
|
|
3
|
+
refusalOfVerdict,
|
|
4
|
+
MediaRefusal2,
|
|
5
|
+
isMediaRefusal2,
|
|
6
|
+
refusalReply
|
|
7
|
+
} from "./chunks/index-8s779ve6.js";
|
|
8
|
+
|
|
9
|
+
// src/key.ts
|
|
10
|
+
var KEY = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
11
|
+
function isMediaKey(value) {
|
|
12
|
+
return typeof value === "string" && KEY.test(value);
|
|
13
|
+
}
|
|
14
|
+
function newMediaKey() {
|
|
15
|
+
return crypto.randomUUID();
|
|
16
|
+
}
|
|
17
|
+
var KIND_NAME = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
18
|
+
function isMediaKindName(value) {
|
|
19
|
+
return typeof value === "string" && KIND_NAME.test(value);
|
|
20
|
+
}
|
|
21
|
+
function isMediaSize(value) {
|
|
22
|
+
return typeof value === "string" && /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(value);
|
|
23
|
+
}
|
|
24
|
+
function mediaObjectKey(kind, key, size) {
|
|
25
|
+
return size === undefined ? `${kind}/${key}` : `${kind}/${key}/${size}`;
|
|
26
|
+
}
|
|
27
|
+
var PREFIX_SEGMENT = /^[A-Za-z0-9][A-Za-z0-9._=-]*$/;
|
|
28
|
+
function checkedPrefix(value) {
|
|
29
|
+
if (value === undefined || value === "")
|
|
30
|
+
return;
|
|
31
|
+
const valid = typeof value === "string" && value.split("/").every((segment) => PREFIX_SEGMENT.test(segment) && segment !== "." && segment !== "..");
|
|
32
|
+
if (!valid) {
|
|
33
|
+
throw new TypeError(`@softistx/alxia-media: keyPrefix gave ${JSON.stringify(value)}, which is not folders of letters, digits and . _ = - joined by "/"`);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
function mediaPlace(kind, prefix, extension) {
|
|
38
|
+
const folders = checkedPrefix(prefix);
|
|
39
|
+
const suffix = extension === undefined ? "" : `.${extension}`;
|
|
40
|
+
return (key, size) => `${folders === undefined ? "" : `${folders}/`}${mediaObjectKey(kind, key, size === undefined ? undefined : `${size}${suffix}`)}`;
|
|
41
|
+
}
|
|
42
|
+
// src/sniff.ts
|
|
43
|
+
var SNIFF_BYTES = 16;
|
|
44
|
+
var at = (head, offset, bytes) => bytes.every((byte, index) => head[offset + index] === byte);
|
|
45
|
+
var ascii = (text) => [...text].map((c) => c.charCodeAt(0));
|
|
46
|
+
var MEDIA_SIGNATURES = {
|
|
47
|
+
"image/png": (h) => at(h, 0, [137, 80, 78, 71, 13, 10, 26, 10]),
|
|
48
|
+
"image/jpeg": (h) => at(h, 0, [255, 216, 255]),
|
|
49
|
+
"image/gif": (h) => at(h, 0, ascii("GIF87a")) || at(h, 0, ascii("GIF89a")),
|
|
50
|
+
"image/webp": (h) => at(h, 0, ascii("RIFF")) && at(h, 8, ascii("WEBP")),
|
|
51
|
+
"image/avif": (h) => at(h, 4, ascii("ftyp")) && (at(h, 8, ascii("avif")) || at(h, 8, ascii("avis"))),
|
|
52
|
+
"application/pdf": (h) => at(h, 0, ascii("%PDF-")),
|
|
53
|
+
"audio/mpeg": (h) => at(h, 0, ascii("ID3")) || h[0] === 255 && ((h[1] ?? 0) & 224) === 224,
|
|
54
|
+
"audio/aac": (h) => h[0] === 255 && ((h[1] ?? 0) & 246) === 240,
|
|
55
|
+
"audio/ogg": (h) => at(h, 0, ascii("OggS")),
|
|
56
|
+
"audio/wav": (h) => at(h, 0, ascii("RIFF")) && at(h, 8, ascii("WAVE")),
|
|
57
|
+
"audio/mp4": (h) => at(h, 4, ascii("ftyp")),
|
|
58
|
+
"video/mp4": (h) => at(h, 4, ascii("ftyp")),
|
|
59
|
+
"video/quicktime": (h) => at(h, 4, ascii("ftyp")) && at(h, 8, ascii("qt ")),
|
|
60
|
+
"video/webm": (h) => at(h, 0, [26, 69, 223, 163])
|
|
61
|
+
};
|
|
62
|
+
function canSniff(type) {
|
|
63
|
+
return Object.hasOwn(MEDIA_SIGNATURES, type);
|
|
64
|
+
}
|
|
65
|
+
function sniffs(type, head) {
|
|
66
|
+
const signature = Object.hasOwn(MEDIA_SIGNATURES, type) ? MEDIA_SIGNATURES[type] : undefined;
|
|
67
|
+
return signature?.(head) === true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/kind-check.ts
|
|
71
|
+
function essenceOf(type) {
|
|
72
|
+
return (type.split(";")[0] ?? "").trim().toLowerCase();
|
|
73
|
+
}
|
|
74
|
+
var TYPE = /^[a-z0-9][a-z0-9!#$&^_.+-]*\/[a-z0-9][a-z0-9!#$&^_.+-]*$/;
|
|
75
|
+
var isExactType = (type) => TYPE.test(essenceOf(type)) && essenceOf(type) === type;
|
|
76
|
+
var ANY_TYPE = "*";
|
|
77
|
+
var acceptsAny = (accept) => accept.length === 1 && accept[0] === ANY_TYPE;
|
|
78
|
+
var isWellFormedType = (type) => TYPE.test(type);
|
|
79
|
+
function checkRenditions(where, sizes, renditions) {
|
|
80
|
+
if (renditions === undefined)
|
|
81
|
+
return;
|
|
82
|
+
if (sizes.length === 0) {
|
|
83
|
+
throw new TypeError(`${where}: renditions needs sizes, the names of what it makes`);
|
|
84
|
+
}
|
|
85
|
+
if (renditions.types.length === 0) {
|
|
86
|
+
throw new TypeError(`${where}: renditions lists no content type`);
|
|
87
|
+
}
|
|
88
|
+
const { keyExtension } = renditions;
|
|
89
|
+
if (keyExtension !== undefined && !(typeof keyExtension === "string" && /^[a-z0-9]+$/.test(keyExtension))) {
|
|
90
|
+
throw new TypeError(`${where}: renditions keyExtension must be lower-case letters and digits with no dot, got ${JSON.stringify(keyExtension)}`);
|
|
91
|
+
}
|
|
92
|
+
for (const type of renditions.types) {
|
|
93
|
+
if (!isExactType(type)) {
|
|
94
|
+
throw new TypeError(`${where}: renditions type ${JSON.stringify(type)} is not an exact content type`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function checkAccept(where, accept, sniff) {
|
|
99
|
+
if (accept === ANY_TYPE)
|
|
100
|
+
return [ANY_TYPE];
|
|
101
|
+
if (!Array.isArray(accept) || accept.length === 0) {
|
|
102
|
+
throw new TypeError(`${where}: accept lists no content type`);
|
|
103
|
+
}
|
|
104
|
+
return accept.map((type) => {
|
|
105
|
+
const essence = essenceOf(type);
|
|
106
|
+
if (!isExactType(type)) {
|
|
107
|
+
throw new TypeError(`${where}: ${JSON.stringify(type)} is not an exact content type (no wildcard, no parameters)`);
|
|
108
|
+
}
|
|
109
|
+
if (sniff === true && !canSniff(essence)) {
|
|
110
|
+
throw new TypeError(`${where}: no signature is known for ${essence}; pass sniff: 'known' to check only the types that have one, or sniff: false to take it unverified`);
|
|
111
|
+
}
|
|
112
|
+
return essence;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/kind.ts
|
|
117
|
+
function storage(where, input, accept) {
|
|
118
|
+
const listed = (option, value) => {
|
|
119
|
+
if (!Array.isArray(value)) {
|
|
120
|
+
throw new TypeError(`${where}: ${option} must be a list of types`);
|
|
121
|
+
}
|
|
122
|
+
const types = value.map((type) => essenceOf(String(type)));
|
|
123
|
+
for (const type of types) {
|
|
124
|
+
if (!accept.includes(type)) {
|
|
125
|
+
throw new TypeError(`${where}: ${option} lists ${type}, which accept does not take`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return [...new Set(types)];
|
|
129
|
+
};
|
|
130
|
+
const {
|
|
131
|
+
keepOriginal: kept,
|
|
132
|
+
renditionTypes: made,
|
|
133
|
+
attachment: forced
|
|
134
|
+
} = input;
|
|
135
|
+
let keepOriginal = true;
|
|
136
|
+
if (Array.isArray(kept)) {
|
|
137
|
+
keepOriginal = listed("keepOriginal", kept);
|
|
138
|
+
if (keepOriginal.length === 0) {
|
|
139
|
+
throw new TypeError(`${where}: keepOriginal lists no type; use false to store no original`);
|
|
140
|
+
}
|
|
141
|
+
} else if (kept === false) {
|
|
142
|
+
keepOriginal = false;
|
|
143
|
+
} else if (kept !== undefined && kept !== true) {
|
|
144
|
+
throw new TypeError(`${where}: keepOriginal must be a boolean or a list of types`);
|
|
145
|
+
}
|
|
146
|
+
const every = acceptsAny(accept) ? [] : accept;
|
|
147
|
+
const renditionTypes = made === undefined ? every.filter((type) => !Array.isArray(keepOriginal) || !keepOriginal.includes(type)) : listed("renditionTypes", made);
|
|
148
|
+
if (keepOriginal !== true && input.renditions === undefined) {
|
|
149
|
+
throw new TypeError(`${where}: keepOriginal: false needs renditions, or nothing would be stored`);
|
|
150
|
+
}
|
|
151
|
+
if (keepOriginal !== true) {
|
|
152
|
+
const kept = Array.isArray(keepOriginal) ? keepOriginal : [];
|
|
153
|
+
for (const type of every) {
|
|
154
|
+
if (!kept.includes(type) && !renditionTypes.includes(type)) {
|
|
155
|
+
throw new TypeError(`${where}: an upload of ${type} would store nothing: keep it in keepOriginal or list it in renditionTypes`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
let attachment = false;
|
|
160
|
+
if (Array.isArray(forced))
|
|
161
|
+
attachment = listed("attachment", forced);
|
|
162
|
+
else if (forced === true)
|
|
163
|
+
attachment = true;
|
|
164
|
+
else if (forced !== undefined && forced !== false) {
|
|
165
|
+
throw new TypeError(`${where}: attachment must be a boolean or a list of types`);
|
|
166
|
+
}
|
|
167
|
+
return { keepOriginal, renditionTypes, attachment };
|
|
168
|
+
}
|
|
169
|
+
function keepsOriginal(kind, type) {
|
|
170
|
+
return kind.keepOriginal || kind.keepOriginalTypes.includes(type);
|
|
171
|
+
}
|
|
172
|
+
function defineMediaKind(input) {
|
|
173
|
+
if (!isMediaKindName(input.name)) {
|
|
174
|
+
throw new TypeError(`defineMediaKind(): the name ${JSON.stringify(input.name)} must be lower-case words and digits joined by hyphens, starting with a letter`);
|
|
175
|
+
}
|
|
176
|
+
const where = `defineMediaKind(${JSON.stringify(input.name)})`;
|
|
177
|
+
if (!Number.isSafeInteger(input.maxBytes) || input.maxBytes < 1) {
|
|
178
|
+
throw new TypeError(`${where}: maxBytes must be a whole number of bytes, 1 or more`);
|
|
179
|
+
}
|
|
180
|
+
const sniff = input.sniff ?? true;
|
|
181
|
+
const accept = checkAccept(where, input.accept, sniff);
|
|
182
|
+
const sizes = input.sizes ?? [];
|
|
183
|
+
for (const size of sizes) {
|
|
184
|
+
if (!isMediaSize(size)) {
|
|
185
|
+
throw new TypeError(`${where}: the size ${JSON.stringify(size)} is not a name`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (acceptsAny(accept) && input.renditions) {
|
|
189
|
+
throw new TypeError(`${where}: accept: '*' takes no renditions, they need the types they are made from; sizes alone are fine, the app writes them`);
|
|
190
|
+
}
|
|
191
|
+
checkRenditions(where, sizes, input.renditions);
|
|
192
|
+
const { keepOriginal, renditionTypes, attachment } = storage(where, input, accept);
|
|
193
|
+
if (input.cacheControl !== undefined && (typeof input.cacheControl !== "string" || input.cacheControl.trim() === "" || /[\r\n]/.test(input.cacheControl))) {
|
|
194
|
+
throw new TypeError(`${where}: cacheControl must be a non-empty Cache-Control value on one line`);
|
|
195
|
+
}
|
|
196
|
+
const isPublic = input.public === true;
|
|
197
|
+
if (!isPublic && typeof input.authorizeRead !== "function") {
|
|
198
|
+
throw new TypeError(`${where}: give authorizeRead, or public: true`);
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
name: input.name,
|
|
202
|
+
accept: [...new Set(accept)],
|
|
203
|
+
maxBytes: input.maxBytes,
|
|
204
|
+
public: isPublic,
|
|
205
|
+
sniff,
|
|
206
|
+
sizes: [...sizes],
|
|
207
|
+
renditions: input.renditions,
|
|
208
|
+
keepOriginal: keepOriginal === true,
|
|
209
|
+
keepOriginalTypes: Array.isArray(keepOriginal) ? keepOriginal : [],
|
|
210
|
+
renditionTypes,
|
|
211
|
+
attachment,
|
|
212
|
+
cacheControl: input.cacheControl,
|
|
213
|
+
authorizeWrite: input.authorizeWrite,
|
|
214
|
+
authorizeRead: input.authorizeRead ?? (() => true),
|
|
215
|
+
authorizeDelete: input.authorizeDelete,
|
|
216
|
+
throttle: input.throttle,
|
|
217
|
+
quota: input.quota
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
// src/routes.ts
|
|
221
|
+
import {
|
|
222
|
+
definePlugin,
|
|
223
|
+
markFactory
|
|
224
|
+
} from "@softistx/alxia";
|
|
225
|
+
|
|
226
|
+
// src/bucket.ts
|
|
227
|
+
import { isBucketUnavailable, S3Error } from "@softistx/s3";
|
|
228
|
+
function refusalOfFailure(error, kind) {
|
|
229
|
+
if (isMediaRefusal2(error))
|
|
230
|
+
return error;
|
|
231
|
+
if (error instanceof S3Error) {
|
|
232
|
+
switch (error.code) {
|
|
233
|
+
case "RANGE_NOT_SATISFIABLE":
|
|
234
|
+
return new MediaRefusal2("range_not_satisfiable", error.objectSize === undefined ? {} : { size: error.objectSize }, { cause: error });
|
|
235
|
+
case "TOO_LARGE":
|
|
236
|
+
return new MediaRefusal2("too_large", { limit: kind.maxBytes }, { cause: error });
|
|
237
|
+
case "SIZE_MISMATCH":
|
|
238
|
+
return new MediaRefusal2("size_mismatch", {}, { cause: error });
|
|
239
|
+
case "SOURCE_FAILED":
|
|
240
|
+
return new MediaRefusal2("interrupted", {}, { cause: error });
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (isBucketUnavailable(error)) {
|
|
244
|
+
return new MediaRefusal2("unavailable", {}, { cause: error });
|
|
245
|
+
}
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// src/read.ts
|
|
250
|
+
import { S3Error as S3Error2 } from "@softistx/s3";
|
|
251
|
+
|
|
252
|
+
// src/headers.ts
|
|
253
|
+
var YEAR = 31536000;
|
|
254
|
+
var UNTYPED = new Set(["application/octet-stream", "binary/octet-stream"]);
|
|
255
|
+
var INLINE = new Set(["application/pdf"]);
|
|
256
|
+
var isInlineSafe = (type) => canSniff(type) || type === "text/plain";
|
|
257
|
+
function servedType(kind, stored, sized = false) {
|
|
258
|
+
const essence = (stored ?? "").split(";")[0]?.trim().toLowerCase() ?? "";
|
|
259
|
+
const made = sized ? kind.renditions?.types ?? [] : [];
|
|
260
|
+
const appWritten = sized && kind.renditions === undefined;
|
|
261
|
+
if (acceptsAny(kind.accept) || appWritten && essence !== "" && isInlineSafe(essence)) {
|
|
262
|
+
return essence !== "" && isInlineSafe(essence) ? { type: essence, known: true } : { type: "application/octet-stream", known: false };
|
|
263
|
+
}
|
|
264
|
+
if (essence !== "" && (kind.accept.includes(essence) || made.includes(essence))) {
|
|
265
|
+
return { type: essence, known: true };
|
|
266
|
+
}
|
|
267
|
+
if ((essence === "" || UNTYPED.has(essence)) && kind.accept.length === 1 && kind.accept[0] !== undefined) {
|
|
268
|
+
return { type: kind.accept[0], known: true };
|
|
269
|
+
}
|
|
270
|
+
return { type: "application/octet-stream", known: false };
|
|
271
|
+
}
|
|
272
|
+
function mediaHeaders(kind, opened, sized = false) {
|
|
273
|
+
const headers = new Headers;
|
|
274
|
+
const { type, known } = servedType(kind, opened.contentType, sized);
|
|
275
|
+
headers.set("content-type", type);
|
|
276
|
+
headers.set("cache-control", kind.cacheControl ?? (kind.public ? `public, max-age=${YEAR}, immutable` : `private, max-age=${YEAR}, immutable`));
|
|
277
|
+
headers.set("x-content-type-options", "nosniff");
|
|
278
|
+
headers.set("cross-origin-resource-policy", kind.public ? "cross-origin" : "same-origin");
|
|
279
|
+
headers.set("content-security-policy", "sandbox; default-src 'none'");
|
|
280
|
+
headers.set("accept-ranges", "bytes");
|
|
281
|
+
const forced = kind.attachment === true || Array.isArray(kind.attachment) && kind.attachment.includes(type);
|
|
282
|
+
if (!known || forced)
|
|
283
|
+
headers.set("content-disposition", "attachment");
|
|
284
|
+
else if (INLINE.has(type))
|
|
285
|
+
headers.set("content-disposition", "inline");
|
|
286
|
+
if (opened.eTag)
|
|
287
|
+
headers.set("etag", opened.eTag);
|
|
288
|
+
if (opened.lastModified) {
|
|
289
|
+
headers.set("last-modified", opened.lastModified.toUTCString());
|
|
290
|
+
}
|
|
291
|
+
return headers;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// src/range.ts
|
|
295
|
+
function parseRange(header) {
|
|
296
|
+
if (header === null)
|
|
297
|
+
return;
|
|
298
|
+
const match = /^bytes=(\d*)-(\d*)$/.exec(header.trim());
|
|
299
|
+
if (!match)
|
|
300
|
+
return;
|
|
301
|
+
const [, first = "", last = ""] = match;
|
|
302
|
+
if (first === "") {
|
|
303
|
+
if (last === "")
|
|
304
|
+
return;
|
|
305
|
+
const suffix = Number(last);
|
|
306
|
+
return Number.isSafeInteger(suffix) ? { suffix } : undefined;
|
|
307
|
+
}
|
|
308
|
+
const start = Number(first);
|
|
309
|
+
if (!Number.isSafeInteger(start))
|
|
310
|
+
return;
|
|
311
|
+
if (last === "")
|
|
312
|
+
return { start };
|
|
313
|
+
const end = Number(last);
|
|
314
|
+
if (!Number.isSafeInteger(end) || end < start)
|
|
315
|
+
return;
|
|
316
|
+
return { start, end };
|
|
317
|
+
}
|
|
318
|
+
function matchesEtag(header, etag) {
|
|
319
|
+
if (header === null)
|
|
320
|
+
return false;
|
|
321
|
+
const strip = (tag) => tag.trim().replace(/^W\//, "");
|
|
322
|
+
const mine = strip(etag);
|
|
323
|
+
return header.split(",").some((tag) => strip(tag) === "*" || strip(tag) === mine);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// src/read.ts
|
|
327
|
+
var notFound = () => new MediaRefusal2("not_found");
|
|
328
|
+
var s3Range = (range) => {
|
|
329
|
+
if (range === undefined)
|
|
330
|
+
return;
|
|
331
|
+
if ("suffix" in range)
|
|
332
|
+
return { suffix: range.suffix };
|
|
333
|
+
return range.end === undefined ? { start: range.start } : { start: range.start, end: range.end + 1 };
|
|
334
|
+
};
|
|
335
|
+
async function notModified({ ctx, kind, sized, ifNoneMatch }, found) {
|
|
336
|
+
if (!matchesEtag(ifNoneMatch, found.eTag ?? ""))
|
|
337
|
+
return;
|
|
338
|
+
await found.stream.cancel();
|
|
339
|
+
const out = mediaHeaders(kind, found, sized);
|
|
340
|
+
for (const name of ["content-type", "accept-ranges", "content-disposition"]) {
|
|
341
|
+
out.delete(name);
|
|
342
|
+
}
|
|
343
|
+
return ctx.reply(304, undefined, { headers: out });
|
|
344
|
+
}
|
|
345
|
+
var mismatched = (ifRange, object) => ifRange !== null && (object.eTag === undefined || ifRange.trim() !== object.eTag);
|
|
346
|
+
async function whole(serving, unsatisfiable) {
|
|
347
|
+
const all = await serving.open(undefined);
|
|
348
|
+
if (all === undefined)
|
|
349
|
+
throw notFound();
|
|
350
|
+
const same = await notModified(serving, all);
|
|
351
|
+
if (same !== undefined)
|
|
352
|
+
return { reply: same };
|
|
353
|
+
if (mismatched(serving.ifRange, all))
|
|
354
|
+
return { object: all };
|
|
355
|
+
await all.stream.cancel();
|
|
356
|
+
return unsatisfiable(all.size);
|
|
357
|
+
}
|
|
358
|
+
async function opening(serving, range) {
|
|
359
|
+
if (range !== undefined && "suffix" in range && range.suffix === 0) {
|
|
360
|
+
return whole(serving, (size) => {
|
|
361
|
+
throw new MediaRefusal2("range_not_satisfiable", size === undefined ? {} : { size });
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
let object;
|
|
365
|
+
try {
|
|
366
|
+
object = await serving.open(range);
|
|
367
|
+
} catch (error) {
|
|
368
|
+
if ((serving.ifNoneMatch !== null || serving.ifRange !== null) && error instanceof S3Error2 && error.code === "RANGE_NOT_SATISFIABLE") {
|
|
369
|
+
return whole(serving, () => {
|
|
370
|
+
throw error;
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
throw error;
|
|
374
|
+
}
|
|
375
|
+
if (object === undefined)
|
|
376
|
+
throw notFound();
|
|
377
|
+
const same = await notModified(serving, object);
|
|
378
|
+
return same === undefined ? { object } : { reply: same };
|
|
379
|
+
}
|
|
380
|
+
function streamed({ ctx, kind, sized }, opened) {
|
|
381
|
+
const out = mediaHeaders(kind, opened, sized);
|
|
382
|
+
if (opened.contentRange !== undefined) {
|
|
383
|
+
out.set("content-range", opened.contentRange);
|
|
384
|
+
if (opened.contentLength !== undefined) {
|
|
385
|
+
out.set("content-length", String(opened.contentLength));
|
|
386
|
+
}
|
|
387
|
+
return ctx.reply(206, opened.stream, { headers: out });
|
|
388
|
+
}
|
|
389
|
+
const length = opened.size ?? opened.contentLength;
|
|
390
|
+
if (length !== undefined)
|
|
391
|
+
out.set("content-length", String(length));
|
|
392
|
+
return ctx.reply(200, opened.stream, { headers: out });
|
|
393
|
+
}
|
|
394
|
+
async function serve(ctx, kind, bucket, raw, timeoutMs, place) {
|
|
395
|
+
const { key, size } = raw;
|
|
396
|
+
if (!isMediaKey(key))
|
|
397
|
+
throw notFound();
|
|
398
|
+
if (size !== undefined && !kind.sizes.includes(size))
|
|
399
|
+
throw notFound();
|
|
400
|
+
if (!kind.public) {
|
|
401
|
+
const denied = refusalOfVerdict(await kind.authorizeRead(ctx, key), notFound());
|
|
402
|
+
if (denied !== undefined)
|
|
403
|
+
throw denied;
|
|
404
|
+
}
|
|
405
|
+
const params = { key: place(key, size) };
|
|
406
|
+
const headers = ctx.request.headers;
|
|
407
|
+
const range = parseRange(headers.get("range"));
|
|
408
|
+
const serving = {
|
|
409
|
+
ctx,
|
|
410
|
+
kind,
|
|
411
|
+
sized: size !== undefined,
|
|
412
|
+
ifNoneMatch: headers.get("if-none-match"),
|
|
413
|
+
ifRange: headers.get("if-range"),
|
|
414
|
+
open: (wanted) => bucket.open(params, {
|
|
415
|
+
timeoutMs,
|
|
416
|
+
...wanted !== undefined && { range: s3Range(wanted) }
|
|
417
|
+
})
|
|
418
|
+
};
|
|
419
|
+
const first = await opening(serving, range);
|
|
420
|
+
if ("reply" in first)
|
|
421
|
+
return first.reply;
|
|
422
|
+
let opened = first.object;
|
|
423
|
+
if (range !== undefined && opened.contentRange !== undefined && mismatched(serving.ifRange, opened)) {
|
|
424
|
+
await opened.stream.cancel();
|
|
425
|
+
const whole = await serving.open(undefined);
|
|
426
|
+
if (whole === undefined)
|
|
427
|
+
throw notFound();
|
|
428
|
+
opened = whole;
|
|
429
|
+
}
|
|
430
|
+
return streamed(serving, opened);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// src/write.ts
|
|
434
|
+
import { S3Error as S3Error3 } from "@softistx/s3";
|
|
435
|
+
|
|
436
|
+
// src/body.ts
|
|
437
|
+
function joined(chunks, length) {
|
|
438
|
+
const out = new Uint8Array(length);
|
|
439
|
+
let at = 0;
|
|
440
|
+
for (const chunk of chunks) {
|
|
441
|
+
out.set(chunk, at);
|
|
442
|
+
at += chunk.length;
|
|
443
|
+
}
|
|
444
|
+
return out;
|
|
445
|
+
}
|
|
446
|
+
async function readWithin(reader, ms) {
|
|
447
|
+
let timer;
|
|
448
|
+
const stalled = new Promise((_, reject) => {
|
|
449
|
+
timer = setTimeout(() => reject(new MediaRefusal2("stalled")), ms);
|
|
450
|
+
});
|
|
451
|
+
try {
|
|
452
|
+
return await Promise.race([reader.read(), stalled]);
|
|
453
|
+
} finally {
|
|
454
|
+
clearTimeout(timer);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
async function readHead(reader, bytes, idleMs) {
|
|
458
|
+
const chunks = [];
|
|
459
|
+
let length = 0;
|
|
460
|
+
let done = false;
|
|
461
|
+
while (length < bytes && !done) {
|
|
462
|
+
const step = await readWithin(reader, idleMs);
|
|
463
|
+
if (step.done)
|
|
464
|
+
done = true;
|
|
465
|
+
else if (step.value.length > 0) {
|
|
466
|
+
chunks.push(step.value);
|
|
467
|
+
length += step.value.length;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return { chunks, length, done };
|
|
471
|
+
}
|
|
472
|
+
function resumed(chunks, reader) {
|
|
473
|
+
let next = 0;
|
|
474
|
+
let reading = false;
|
|
475
|
+
let stalled = false;
|
|
476
|
+
const body = new ReadableStream({
|
|
477
|
+
async pull(controller) {
|
|
478
|
+
const held = chunks[next];
|
|
479
|
+
if (held !== undefined) {
|
|
480
|
+
next += 1;
|
|
481
|
+
controller.enqueue(held);
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
if (reader === undefined) {
|
|
485
|
+
controller.close();
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
reading = true;
|
|
489
|
+
try {
|
|
490
|
+
const step = await reader.read();
|
|
491
|
+
if (step.done)
|
|
492
|
+
controller.close();
|
|
493
|
+
else
|
|
494
|
+
controller.enqueue(step.value);
|
|
495
|
+
} finally {
|
|
496
|
+
reading = false;
|
|
497
|
+
}
|
|
498
|
+
},
|
|
499
|
+
cancel: (reason) => {
|
|
500
|
+
stalled = reading;
|
|
501
|
+
return reader?.cancel(reason);
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
return { body, stalled: () => stalled };
|
|
505
|
+
}
|
|
506
|
+
async function collected(held, heldLength, reader, limit, idleMs) {
|
|
507
|
+
const chunks = [...held];
|
|
508
|
+
let length = heldLength;
|
|
509
|
+
for (;; ) {
|
|
510
|
+
const step = await readWithin(reader, idleMs);
|
|
511
|
+
if (step.done)
|
|
512
|
+
break;
|
|
513
|
+
length += step.value.length;
|
|
514
|
+
if (length > limit) {
|
|
515
|
+
await reader.cancel().catch(() => {
|
|
516
|
+
return;
|
|
517
|
+
});
|
|
518
|
+
throw new MediaRefusal2("too_large", { limit });
|
|
519
|
+
}
|
|
520
|
+
chunks.push(step.value);
|
|
521
|
+
}
|
|
522
|
+
return { chunks, length };
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// src/checks.ts
|
|
526
|
+
async function admitted(ctx, kind) {
|
|
527
|
+
const denied = refusalOfVerdict(await kind.authorizeWrite(ctx), new MediaRefusal2("forbidden"));
|
|
528
|
+
if (denied !== undefined)
|
|
529
|
+
throw denied;
|
|
530
|
+
if (kind.throttle !== undefined) {
|
|
531
|
+
const decision = await kind.throttle(ctx);
|
|
532
|
+
if (decision === false)
|
|
533
|
+
throw new MediaRefusal2("throttled");
|
|
534
|
+
if (typeof decision === "number") {
|
|
535
|
+
throw new MediaRefusal2("throttled", { retryAfter: decision });
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
const { request } = ctx;
|
|
539
|
+
const declaredType = essenceOf(request.headers.get("content-type") ?? "");
|
|
540
|
+
const any = acceptsAny(kind.accept);
|
|
541
|
+
const type = any && declaredType === "" ? "application/octet-stream" : declaredType;
|
|
542
|
+
if (any ? !isWellFormedType(type) : !kind.accept.includes(type)) {
|
|
543
|
+
throw new MediaRefusal2("unsupported_type", { accept: kind.accept });
|
|
544
|
+
}
|
|
545
|
+
const header = request.headers.get("content-length");
|
|
546
|
+
let declared;
|
|
547
|
+
if (header !== null) {
|
|
548
|
+
if (!/^\d+$/.test(header.trim()))
|
|
549
|
+
throw new MediaRefusal2("size_mismatch");
|
|
550
|
+
declared = Number(header.trim());
|
|
551
|
+
if (declared === 0)
|
|
552
|
+
throw new MediaRefusal2("empty_body");
|
|
553
|
+
if (declared > kind.maxBytes) {
|
|
554
|
+
throw new MediaRefusal2("too_large", { limit: kind.maxBytes });
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
if (kind.quota !== undefined) {
|
|
558
|
+
const over = refusalOfVerdict(await kind.quota(ctx, { declared, type }), new MediaRefusal2("forbidden"));
|
|
559
|
+
if (over !== undefined)
|
|
560
|
+
throw over;
|
|
561
|
+
}
|
|
562
|
+
return { type, declared };
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// src/rendition.ts
|
|
566
|
+
function answered(kind, error) {
|
|
567
|
+
if (error instanceof MediaRefusal2 && error.code === "unsupported_type" && error.details.accept === undefined) {
|
|
568
|
+
return new MediaRefusal2("unsupported_type", { ...error.details, accept: kind.accept }, { cause: error.cause ?? error });
|
|
569
|
+
}
|
|
570
|
+
return error;
|
|
571
|
+
}
|
|
572
|
+
async function readBack(bucket, key, timeoutMs) {
|
|
573
|
+
const opened = await bucket.open({ key }, { timeoutMs });
|
|
574
|
+
if (opened === undefined) {
|
|
575
|
+
throw new Error(`@softistx/alxia-media: ${key} was gone right after its upload`);
|
|
576
|
+
}
|
|
577
|
+
return new Uint8Array(await new Response(opened.stream).arrayBuffer());
|
|
578
|
+
}
|
|
579
|
+
function checked(kind, made) {
|
|
580
|
+
if (!kind.sizes.includes(made.size)) {
|
|
581
|
+
throw new TypeError(`${kind.name}: renditions made the size ${JSON.stringify(made.size)}, which sizes does not list`);
|
|
582
|
+
}
|
|
583
|
+
if (!kind.renditions.types.includes(made.contentType)) {
|
|
584
|
+
throw new TypeError(`${kind.name}: renditions made a ${made.contentType}, which renditions.types does not list`);
|
|
585
|
+
}
|
|
586
|
+
return made;
|
|
587
|
+
}
|
|
588
|
+
async function storeRenditions(bucket, kind, upload, options) {
|
|
589
|
+
const { timeoutMs, place, request, held } = options;
|
|
590
|
+
const written = held === undefined ? [place(upload.key)] : [];
|
|
591
|
+
const sizes = [];
|
|
592
|
+
const renditions = [];
|
|
593
|
+
try {
|
|
594
|
+
const bytes = held ?? await readBack(bucket, place(upload.key), timeoutMs);
|
|
595
|
+
const made = await kind.renditions.make({
|
|
596
|
+
key: upload.key,
|
|
597
|
+
contentType: upload.contentType,
|
|
598
|
+
bytes,
|
|
599
|
+
request
|
|
600
|
+
});
|
|
601
|
+
for (const rendition of made.map((one) => checked(kind, one))) {
|
|
602
|
+
const key = place(upload.key, rendition.size);
|
|
603
|
+
written.push(key);
|
|
604
|
+
await bucket.put({ key }, rendition.bytes, {
|
|
605
|
+
type: rendition.contentType,
|
|
606
|
+
timeoutMs
|
|
607
|
+
});
|
|
608
|
+
sizes.push(rendition.size);
|
|
609
|
+
renditions.push({
|
|
610
|
+
size: rendition.size,
|
|
611
|
+
contentType: rendition.contentType,
|
|
612
|
+
...rendition.width !== undefined && { width: rendition.width },
|
|
613
|
+
...rendition.height !== undefined && { height: rendition.height }
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
} catch (error) {
|
|
617
|
+
await removeAll(bucket, written, timeoutMs);
|
|
618
|
+
throw answered(kind, error);
|
|
619
|
+
}
|
|
620
|
+
return { sizes, renditions, objects: written };
|
|
621
|
+
}
|
|
622
|
+
async function removeAll(bucket, keys, timeoutMs) {
|
|
623
|
+
await Promise.all(keys.map((key) => bucket.delete({ key }, { timeoutMs }).catch(() => {
|
|
624
|
+
return;
|
|
625
|
+
})));
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
// src/write.ts
|
|
629
|
+
async function store(bucket, params, source, options) {
|
|
630
|
+
try {
|
|
631
|
+
await bucket.put(params, source.body, options);
|
|
632
|
+
} catch (error) {
|
|
633
|
+
if (source.stalled() && error instanceof S3Error3 && error.code === "TIMEOUT") {
|
|
634
|
+
throw new MediaRefusal2("stalled", {}, { cause: error });
|
|
635
|
+
}
|
|
636
|
+
throw error;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
async function upload(ctx, kind, writing, answer) {
|
|
640
|
+
const { bucket, timeoutMs, place } = writing;
|
|
641
|
+
const stored = await storeBody(ctx, kind, writing);
|
|
642
|
+
const { bytes: held, ...result } = stored;
|
|
643
|
+
const { renditions } = kind;
|
|
644
|
+
let sizes = [];
|
|
645
|
+
let made = [];
|
|
646
|
+
const keeps = keepsOriginal(kind, result.contentType);
|
|
647
|
+
let objects = keeps ? [place(result.key)] : [];
|
|
648
|
+
if (renditions !== undefined && kind.renditionTypes.includes(result.contentType)) {
|
|
649
|
+
const done = await storeRenditions(bucket, { ...kind, renditions }, result, {
|
|
650
|
+
timeoutMs,
|
|
651
|
+
place,
|
|
652
|
+
request: ctx.request,
|
|
653
|
+
...held !== undefined && { held }
|
|
654
|
+
});
|
|
655
|
+
sizes = done.sizes;
|
|
656
|
+
made = done.renditions;
|
|
657
|
+
objects = done.objects;
|
|
658
|
+
}
|
|
659
|
+
if (answer === undefined)
|
|
660
|
+
return result;
|
|
661
|
+
try {
|
|
662
|
+
return await answer({
|
|
663
|
+
...result,
|
|
664
|
+
kind: kind.name,
|
|
665
|
+
sizes,
|
|
666
|
+
renditions: made
|
|
667
|
+
});
|
|
668
|
+
} catch (error) {
|
|
669
|
+
await removeAll(bucket, objects, timeoutMs);
|
|
670
|
+
throw error;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
async function held(head, reader, kind, timeoutMs) {
|
|
674
|
+
const all = head.done ? { chunks: head.chunks, length: head.length } : await collected(head.chunks, head.length, reader, kind.maxBytes, timeoutMs);
|
|
675
|
+
if (all.length > kind.maxBytes) {
|
|
676
|
+
throw new MediaRefusal2("too_large", { limit: kind.maxBytes });
|
|
677
|
+
}
|
|
678
|
+
return all;
|
|
679
|
+
}
|
|
680
|
+
async function storeBody(ctx, kind, { bucket, timeoutMs, place }) {
|
|
681
|
+
const { type, declared } = await admitted(ctx, kind);
|
|
682
|
+
if (ctx.request.body === null)
|
|
683
|
+
throw new MediaRefusal2("empty_body");
|
|
684
|
+
const reader = ctx.request.body.getReader();
|
|
685
|
+
try {
|
|
686
|
+
const head = await readHead(reader, SNIFF_BYTES, timeoutMs);
|
|
687
|
+
if (head.length === 0)
|
|
688
|
+
throw new MediaRefusal2("empty_body");
|
|
689
|
+
const checked = kind.sniff === "known" || acceptsAny(kind.accept) ? kind.sniff !== false && canSniff(type) : kind.sniff;
|
|
690
|
+
if (checked && !sniffs(type, joined(head.chunks, head.length))) {
|
|
691
|
+
throw new MediaRefusal2("unsupported_type", { accept: kind.accept });
|
|
692
|
+
}
|
|
693
|
+
const key = newMediaKey();
|
|
694
|
+
const params = { key: place(key) };
|
|
695
|
+
const result = (size) => ({ key, contentType: type, size });
|
|
696
|
+
if (!keepsOriginal(kind, type)) {
|
|
697
|
+
const all = await held(head, reader, kind, timeoutMs);
|
|
698
|
+
if (declared !== undefined && all.length !== declared) {
|
|
699
|
+
throw new MediaRefusal2("size_mismatch");
|
|
700
|
+
}
|
|
701
|
+
return {
|
|
702
|
+
...result(all.length),
|
|
703
|
+
bytes: joined(all.chunks, all.length)
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
const options = { type, timeoutMs };
|
|
707
|
+
if (declared !== undefined) {
|
|
708
|
+
const source = resumed(head.chunks, reader);
|
|
709
|
+
await store(bucket, params, source, { ...options, size: declared });
|
|
710
|
+
return result(declared);
|
|
711
|
+
}
|
|
712
|
+
const all = await held(head, reader, kind, timeoutMs);
|
|
713
|
+
await store(bucket, params, resumed(all.chunks), {
|
|
714
|
+
...options,
|
|
715
|
+
size: all.length
|
|
716
|
+
});
|
|
717
|
+
return result(all.length);
|
|
718
|
+
} catch (error) {
|
|
719
|
+
await reader.cancel().catch(() => {
|
|
720
|
+
return;
|
|
721
|
+
});
|
|
722
|
+
throw error;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
async function remove(ctx, kind, key, { bucket, timeoutMs, place }) {
|
|
726
|
+
if (!isMediaKey(key))
|
|
727
|
+
throw new MediaRefusal2("not_found");
|
|
728
|
+
if (kind.authorizeDelete === undefined)
|
|
729
|
+
throw new MediaRefusal2("not_found");
|
|
730
|
+
const denied = refusalOfVerdict(await kind.authorizeDelete(ctx, key), new MediaRefusal2("forbidden"));
|
|
731
|
+
if (denied !== undefined)
|
|
732
|
+
throw denied;
|
|
733
|
+
for (const size of [undefined, ...kind.sizes]) {
|
|
734
|
+
await bucket.delete({ key: place(key, size) }, { timeoutMs });
|
|
735
|
+
}
|
|
736
|
+
return ctx.reply(204);
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
// src/routes.ts
|
|
740
|
+
function mediaRoutes(kinds, options) {
|
|
741
|
+
const prefix = (options.prefix ?? "/media").replace(/\/+$/, "");
|
|
742
|
+
if (!prefix.startsWith("/")) {
|
|
743
|
+
throw new TypeError(`mediaRoutes(): prefix ${prefix} must start with "/"`);
|
|
744
|
+
}
|
|
745
|
+
const names = new Set;
|
|
746
|
+
for (const kind of kinds) {
|
|
747
|
+
if (names.has(kind.name)) {
|
|
748
|
+
throw new TypeError(`mediaRoutes(): the kind ${kind.name} is given twice`);
|
|
749
|
+
}
|
|
750
|
+
names.add(kind.name);
|
|
751
|
+
}
|
|
752
|
+
const context = {
|
|
753
|
+
prefix,
|
|
754
|
+
bucket: options.bucket,
|
|
755
|
+
readTimeoutMs: options.readTimeoutMs ?? 1e4,
|
|
756
|
+
writeTimeoutMs: options.writeTimeoutMs ?? 60000,
|
|
757
|
+
refusal: options.refusal,
|
|
758
|
+
keyPrefix: options.keyPrefix,
|
|
759
|
+
uploaded: options.uploaded
|
|
760
|
+
};
|
|
761
|
+
return definePlugin()((app) => {
|
|
762
|
+
const routes = app;
|
|
763
|
+
for (const kind of kinds) {
|
|
764
|
+
declareKind(routes, kind, context);
|
|
765
|
+
}
|
|
766
|
+
return app;
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
markFactory(mediaRoutes, "plugin");
|
|
770
|
+
async function placeOf(context, kind, ctx) {
|
|
771
|
+
const prefix = await context.keyPrefix?.(ctx, kind.name);
|
|
772
|
+
return mediaPlace(kind.name, checkedPrefix(prefix), kind.renditions?.keyExtension);
|
|
773
|
+
}
|
|
774
|
+
function guarded(context, kind, run) {
|
|
775
|
+
return async (ctx) => {
|
|
776
|
+
try {
|
|
777
|
+
return await run(ctx);
|
|
778
|
+
} catch (error) {
|
|
779
|
+
const refusal = refusalOfFailure(error, kind);
|
|
780
|
+
if (refusal === undefined)
|
|
781
|
+
throw error;
|
|
782
|
+
return context.refusal?.(refusal, ctx) ?? refusalReply(ctx, refusal);
|
|
783
|
+
}
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
function declareKind(routes, kind, context) {
|
|
787
|
+
const { prefix, bucket, readTimeoutMs, writeTimeoutMs } = context;
|
|
788
|
+
const base = `${prefix}/${kind.name}`;
|
|
789
|
+
const read = (size) => guarded(context, kind, async (ctx) => serve(ctx, kind, bucket, {
|
|
790
|
+
key: ctx.pathParams["key"],
|
|
791
|
+
size: size ? ctx.pathParams["size"] : undefined
|
|
792
|
+
}, readTimeoutMs, await placeOf(context, kind, ctx)));
|
|
793
|
+
routes.put(base, guarded(context, kind, async (ctx) => {
|
|
794
|
+
const place = await placeOf(context, kind, ctx);
|
|
795
|
+
const { uploaded } = context;
|
|
796
|
+
const body = await upload(ctx, kind, { bucket, timeoutMs: writeTimeoutMs, place }, uploaded && ((result) => uploaded(result, ctx)));
|
|
797
|
+
return ctx.reply(200, body);
|
|
798
|
+
}));
|
|
799
|
+
routes.get(`${base}/:key`, read(false));
|
|
800
|
+
if (kind.sizes.length > 0)
|
|
801
|
+
routes.get(`${base}/:key/:size`, read(true));
|
|
802
|
+
if (kind.authorizeDelete !== undefined) {
|
|
803
|
+
routes.delete(`${base}/:key`, guarded(context, kind, async (ctx) => remove(ctx, kind, ctx.pathParams["key"], {
|
|
804
|
+
bucket,
|
|
805
|
+
timeoutMs: writeTimeoutMs,
|
|
806
|
+
place: await placeOf(context, kind, ctx)
|
|
807
|
+
})));
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
export {
|
|
811
|
+
MEDIA_REFUSAL_STATUS2 as MEDIA_REFUSAL_STATUS,
|
|
812
|
+
MEDIA_SIGNATURES,
|
|
813
|
+
MediaRefusal2 as MediaRefusal,
|
|
814
|
+
defineMediaKind,
|
|
815
|
+
isMediaKey,
|
|
816
|
+
isMediaKindName,
|
|
817
|
+
isMediaRefusal2 as isMediaRefusal,
|
|
818
|
+
mediaObjectKey,
|
|
819
|
+
mediaPlace,
|
|
820
|
+
mediaRoutes,
|
|
821
|
+
newMediaKey
|
|
822
|
+
};
|
|
823
|
+
|
|
824
|
+
//# debugId=CC809879571B9A0064756E2164756E21
|
|
825
|
+
//# sourceMappingURL=index.js.map
|