ipx 0.7.1 → 0.9.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/README.md +23 -21
- package/bin/ipx.mjs +2 -0
- package/dist/{cli.js → chunks/middleware.cjs} +59 -51
- package/dist/chunks/middleware.mjs +527 -0
- package/dist/cli.cjs +33 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.mjs +27 -0
- package/dist/index.cjs +19 -518
- package/dist/index.mjs +13 -503
- package/package.json +15 -15
package/dist/index.cjs
CHANGED
|
@@ -2,521 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const isValidPath__default = /*#__PURE__*/_interopDefaultLegacy(isValidPath);
|
|
25
|
-
const destr__default = /*#__PURE__*/_interopDefaultLegacy(destr);
|
|
26
|
-
const http__default = /*#__PURE__*/_interopDefaultLegacy(http);
|
|
27
|
-
const https__default = /*#__PURE__*/_interopDefaultLegacy(https);
|
|
28
|
-
const fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
|
|
29
|
-
const getEtag__default = /*#__PURE__*/_interopDefaultLegacy(getEtag);
|
|
30
|
-
const xss__default = /*#__PURE__*/_interopDefaultLegacy(xss);
|
|
31
|
-
|
|
32
|
-
const Handlers = /*#__PURE__*/Object.freeze({
|
|
33
|
-
__proto__: null,
|
|
34
|
-
get quality () { return quality; },
|
|
35
|
-
get fit () { return fit; },
|
|
36
|
-
get background () { return background; },
|
|
37
|
-
get width () { return width; },
|
|
38
|
-
get height () { return height; },
|
|
39
|
-
get resize () { return resize; },
|
|
40
|
-
get trim () { return trim; },
|
|
41
|
-
get extend () { return extend; },
|
|
42
|
-
get extract () { return extract; },
|
|
43
|
-
get rotate () { return rotate; },
|
|
44
|
-
get flip () { return flip; },
|
|
45
|
-
get flop () { return flop; },
|
|
46
|
-
get sharpen () { return sharpen; },
|
|
47
|
-
get median () { return median; },
|
|
48
|
-
get blur () { return blur; },
|
|
49
|
-
get flatten () { return flatten; },
|
|
50
|
-
get gamma () { return gamma; },
|
|
51
|
-
get negate () { return negate; },
|
|
52
|
-
get normalize () { return normalize; },
|
|
53
|
-
get threshold () { return threshold; },
|
|
54
|
-
get modulate () { return modulate; },
|
|
55
|
-
get tint () { return tint; },
|
|
56
|
-
get grayscale () { return grayscale; },
|
|
57
|
-
get crop () { return crop; },
|
|
58
|
-
get q () { return q; },
|
|
59
|
-
get b () { return b; },
|
|
60
|
-
get w () { return w; },
|
|
61
|
-
get h () { return h; },
|
|
62
|
-
get s () { return s; }
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
function getEnv(name, defaultValue) {
|
|
66
|
-
var _a;
|
|
67
|
-
return (_a = destr__default['default'](process.env[name])) != null ? _a : defaultValue;
|
|
68
|
-
}
|
|
69
|
-
function cachedPromise(fn) {
|
|
70
|
-
let p;
|
|
71
|
-
return (...args) => {
|
|
72
|
-
if (p) {
|
|
73
|
-
return p;
|
|
74
|
-
}
|
|
75
|
-
p = Promise.resolve(fn(...args));
|
|
76
|
-
return p;
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
class IPXError extends Error {
|
|
80
|
-
}
|
|
81
|
-
function createError(message, statusCode) {
|
|
82
|
-
const err = new IPXError(message);
|
|
83
|
-
err.statusMessage = "IPX: " + message;
|
|
84
|
-
err.statusCode = statusCode;
|
|
85
|
-
return err;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const createFilesystemSource = (options) => {
|
|
89
|
-
const rootDir = path.resolve(options.dir);
|
|
90
|
-
return async (id) => {
|
|
91
|
-
const fsPath = path.resolve(path.join(rootDir, id));
|
|
92
|
-
if (!isValidPath__default['default'](id) || id.includes("..") || !fsPath.startsWith(rootDir)) {
|
|
93
|
-
throw createError("Forbidden path:" + id, 403);
|
|
94
|
-
}
|
|
95
|
-
let stats;
|
|
96
|
-
try {
|
|
97
|
-
stats = await fsExtra.stat(fsPath);
|
|
98
|
-
} catch (err) {
|
|
99
|
-
if (err.code === "ENOENT") {
|
|
100
|
-
throw createError("File not found: " + fsPath, 404);
|
|
101
|
-
} else {
|
|
102
|
-
throw createError("File access error for " + fsPath + ":" + err.code, 403);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
if (!stats.isFile()) {
|
|
106
|
-
throw createError("Path should be a file: " + fsPath, 400);
|
|
107
|
-
}
|
|
108
|
-
return {
|
|
109
|
-
mtime: stats.mtime,
|
|
110
|
-
maxAge: options.maxAge || 300,
|
|
111
|
-
getData: cachedPromise(() => fsExtra.readFile(fsPath))
|
|
112
|
-
};
|
|
113
|
-
};
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const createHTTPSource = (options) => {
|
|
117
|
-
const httpsAgent = new https__default['default'].Agent({ keepAlive: true });
|
|
118
|
-
const httpAgent = new http__default['default'].Agent({ keepAlive: true });
|
|
119
|
-
let domains = options.domains || [];
|
|
120
|
-
if (typeof domains === "string") {
|
|
121
|
-
domains = domains.split(",").map((s) => s.trim());
|
|
122
|
-
}
|
|
123
|
-
const hosts = domains.map((domain) => ufo.parseURL(domain, "https://").host);
|
|
124
|
-
return async (id, reqOptions) => {
|
|
125
|
-
const parsedUrl = ufo.parseURL(id, "https://");
|
|
126
|
-
if (!parsedUrl.host) {
|
|
127
|
-
throw createError("Hostname is missing: " + id, 403);
|
|
128
|
-
}
|
|
129
|
-
if (!(reqOptions == null ? void 0 : reqOptions.bypassDomain) && !hosts.find((host) => parsedUrl.host === host)) {
|
|
130
|
-
throw createError("Forbidden host: " + parsedUrl.host, 403);
|
|
131
|
-
}
|
|
132
|
-
const response = await fetch__default['default'](id, {
|
|
133
|
-
agent: id.startsWith("https") ? httpsAgent : httpAgent
|
|
134
|
-
});
|
|
135
|
-
if (!response.ok) {
|
|
136
|
-
throw createError(response.statusText || "fetch error", response.status || 500);
|
|
137
|
-
}
|
|
138
|
-
let maxAge = options.maxAge || 300;
|
|
139
|
-
const _cacheControl = response.headers.get("cache-control");
|
|
140
|
-
if (_cacheControl) {
|
|
141
|
-
const m = _cacheControl.match(/max-age=(\d+)/);
|
|
142
|
-
if (m && m[1]) {
|
|
143
|
-
maxAge = parseInt(m[1]);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
let mtime;
|
|
147
|
-
const _lastModified = response.headers.get("last-modified");
|
|
148
|
-
if (_lastModified) {
|
|
149
|
-
mtime = new Date(_lastModified);
|
|
150
|
-
}
|
|
151
|
-
return {
|
|
152
|
-
mtime,
|
|
153
|
-
maxAge,
|
|
154
|
-
getData: cachedPromise(() => response.buffer())
|
|
155
|
-
};
|
|
156
|
-
};
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
function VArg(arg) {
|
|
160
|
-
return destr__default['default'](arg);
|
|
161
|
-
}
|
|
162
|
-
function parseArgs(args, mappers) {
|
|
163
|
-
const vargs = args.split("_");
|
|
164
|
-
return mappers.map((v, i) => v(vargs[i]));
|
|
165
|
-
}
|
|
166
|
-
function getHandler(key) {
|
|
167
|
-
return Handlers[key];
|
|
168
|
-
}
|
|
169
|
-
function applyHandler(ctx, pipe, handler, argsStr) {
|
|
170
|
-
const args = handler.args ? parseArgs(argsStr, handler.args) : [];
|
|
171
|
-
return handler.apply(ctx, pipe, ...args);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const quality = {
|
|
175
|
-
args: [VArg],
|
|
176
|
-
order: -1,
|
|
177
|
-
apply: (context, _pipe, quality2) => {
|
|
178
|
-
context.quality = quality2;
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
const fit = {
|
|
182
|
-
args: [VArg],
|
|
183
|
-
order: -1,
|
|
184
|
-
apply: (context, _pipe, fit2) => {
|
|
185
|
-
context.fit = fit2;
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
const HEX_RE = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i;
|
|
189
|
-
const SHORTHEX_RE = /^([a-f\d])([a-f\d])([a-f\d])$/i;
|
|
190
|
-
const background = {
|
|
191
|
-
args: [VArg],
|
|
192
|
-
order: -1,
|
|
193
|
-
apply: (context, _pipe, background2) => {
|
|
194
|
-
background2 = String(background2);
|
|
195
|
-
if (!background2.startsWith("#") && (HEX_RE.test(background2) || SHORTHEX_RE.test(background2))) {
|
|
196
|
-
background2 = "#" + background2;
|
|
197
|
-
}
|
|
198
|
-
context.background = background2;
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
const width = {
|
|
202
|
-
args: [VArg],
|
|
203
|
-
apply: (_context, pipe, width2) => {
|
|
204
|
-
return pipe.resize(width2, null);
|
|
205
|
-
}
|
|
206
|
-
};
|
|
207
|
-
const height = {
|
|
208
|
-
args: [VArg],
|
|
209
|
-
apply: (_context, pipe, height2) => {
|
|
210
|
-
return pipe.resize(null, height2);
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
const resize = {
|
|
214
|
-
args: [VArg, VArg, VArg],
|
|
215
|
-
apply: (context, pipe, size) => {
|
|
216
|
-
const [width2, height2] = String(size).split("x").map((v) => Number(v));
|
|
217
|
-
return pipe.resize(width2, height2, {
|
|
218
|
-
fit: context.fit,
|
|
219
|
-
background: context.background
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
const trim = {
|
|
224
|
-
args: [VArg],
|
|
225
|
-
apply: (_context, pipe, threshold2) => {
|
|
226
|
-
return pipe.trim(threshold2);
|
|
227
|
-
}
|
|
228
|
-
};
|
|
229
|
-
const extend = {
|
|
230
|
-
args: [VArg, VArg, VArg, VArg],
|
|
231
|
-
apply: (context, pipe, top, right, bottom, left) => {
|
|
232
|
-
return pipe.extend({
|
|
233
|
-
top,
|
|
234
|
-
left,
|
|
235
|
-
bottom,
|
|
236
|
-
right,
|
|
237
|
-
background: context.background
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
const extract = {
|
|
242
|
-
args: [VArg, VArg, VArg, VArg],
|
|
243
|
-
apply: (context, pipe, top, right, bottom, left) => {
|
|
244
|
-
return pipe.extend({
|
|
245
|
-
top,
|
|
246
|
-
left,
|
|
247
|
-
bottom,
|
|
248
|
-
right,
|
|
249
|
-
background: context.background
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
const rotate = {
|
|
254
|
-
args: [VArg],
|
|
255
|
-
apply: (context, pipe, angel) => {
|
|
256
|
-
return pipe.rotate(angel, {
|
|
257
|
-
background: context.background
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
};
|
|
261
|
-
const flip = {
|
|
262
|
-
args: [],
|
|
263
|
-
apply: (_context, pipe) => {
|
|
264
|
-
return pipe.flip();
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
const flop = {
|
|
268
|
-
args: [],
|
|
269
|
-
apply: (_context, pipe) => {
|
|
270
|
-
return pipe.flop();
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
const sharpen = {
|
|
274
|
-
args: [VArg, VArg, VArg],
|
|
275
|
-
apply: (_context, pipe, sigma, flat, jagged) => {
|
|
276
|
-
return pipe.sharpen(sigma, flat, jagged);
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
const median = {
|
|
280
|
-
args: [VArg, VArg, VArg],
|
|
281
|
-
apply: (_context, pipe, size) => {
|
|
282
|
-
return pipe.median(size);
|
|
283
|
-
}
|
|
284
|
-
};
|
|
285
|
-
const blur = {
|
|
286
|
-
args: [VArg, VArg, VArg],
|
|
287
|
-
apply: (_context, pipe) => {
|
|
288
|
-
return pipe.blur();
|
|
289
|
-
}
|
|
290
|
-
};
|
|
291
|
-
const flatten = {
|
|
292
|
-
args: [VArg, VArg, VArg],
|
|
293
|
-
apply: (context, pipe) => {
|
|
294
|
-
return pipe.flatten({
|
|
295
|
-
background: context.background
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
};
|
|
299
|
-
const gamma = {
|
|
300
|
-
args: [VArg, VArg, VArg],
|
|
301
|
-
apply: (_context, pipe, gamma2, gammaOut) => {
|
|
302
|
-
return pipe.gamma(gamma2, gammaOut);
|
|
303
|
-
}
|
|
304
|
-
};
|
|
305
|
-
const negate = {
|
|
306
|
-
args: [VArg, VArg, VArg],
|
|
307
|
-
apply: (_context, pipe) => {
|
|
308
|
-
return pipe.negate();
|
|
309
|
-
}
|
|
310
|
-
};
|
|
311
|
-
const normalize = {
|
|
312
|
-
args: [VArg, VArg, VArg],
|
|
313
|
-
apply: (_context, pipe) => {
|
|
314
|
-
return pipe.normalize();
|
|
315
|
-
}
|
|
316
|
-
};
|
|
317
|
-
const threshold = {
|
|
318
|
-
args: [VArg],
|
|
319
|
-
apply: (_context, pipe, threshold2) => {
|
|
320
|
-
return pipe.threshold(threshold2);
|
|
321
|
-
}
|
|
322
|
-
};
|
|
323
|
-
const modulate = {
|
|
324
|
-
args: [VArg],
|
|
325
|
-
apply: (_context, pipe, brightness, saturation, hue) => {
|
|
326
|
-
return pipe.modulate({
|
|
327
|
-
brightness,
|
|
328
|
-
saturation,
|
|
329
|
-
hue
|
|
330
|
-
});
|
|
331
|
-
}
|
|
332
|
-
};
|
|
333
|
-
const tint = {
|
|
334
|
-
args: [VArg],
|
|
335
|
-
apply: (_context, pipe, rgb) => {
|
|
336
|
-
return pipe.tint(rgb);
|
|
337
|
-
}
|
|
338
|
-
};
|
|
339
|
-
const grayscale = {
|
|
340
|
-
args: [VArg],
|
|
341
|
-
apply: (_context, pipe) => {
|
|
342
|
-
return pipe.grayscale();
|
|
343
|
-
}
|
|
344
|
-
};
|
|
345
|
-
const crop = extract;
|
|
346
|
-
const q = quality;
|
|
347
|
-
const b = background;
|
|
348
|
-
const w = width;
|
|
349
|
-
const h = height;
|
|
350
|
-
const s = resize;
|
|
351
|
-
|
|
352
|
-
const SUPPORTED_FORMATS = ["jpeg", "png", "webp", "avif", "tiff"];
|
|
353
|
-
function createIPX(userOptions) {
|
|
354
|
-
const defaults = {
|
|
355
|
-
dir: getEnv("IPX_DIR", "."),
|
|
356
|
-
domains: getEnv("IPX_DOMAINS", []),
|
|
357
|
-
alias: getEnv("IPX_ALIAS", {}),
|
|
358
|
-
sharp: {}
|
|
359
|
-
};
|
|
360
|
-
const options = defu__default['default'](userOptions, defaults);
|
|
361
|
-
options.alias = Object.fromEntries(Object.entries(options.alias).map((e) => [ufo.withLeadingSlash(e[0]), e[1]]));
|
|
362
|
-
const ctx = {
|
|
363
|
-
sources: {}
|
|
364
|
-
};
|
|
365
|
-
if (options.dir) {
|
|
366
|
-
ctx.sources.filesystem = createFilesystemSource({
|
|
367
|
-
dir: options.dir
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
if (options.domains) {
|
|
371
|
-
ctx.sources.http = createHTTPSource({
|
|
372
|
-
domains: options.domains
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
|
-
return function ipx(id, modifiers = {}, reqOptions = {}) {
|
|
376
|
-
if (!id) {
|
|
377
|
-
throw createError("resource id is missing", 400);
|
|
378
|
-
}
|
|
379
|
-
id = ufo.hasProtocol(id) ? id : ufo.withLeadingSlash(id);
|
|
380
|
-
for (const base in options.alias) {
|
|
381
|
-
if (id.startsWith(base)) {
|
|
382
|
-
id = ufo.joinURL(options.alias[base], id.substr(base.length));
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
const getSrc = cachedPromise(() => {
|
|
386
|
-
const source = ufo.hasProtocol(id) ? "http" : "filesystem";
|
|
387
|
-
if (!ctx.sources[source]) {
|
|
388
|
-
throw createError("Unknown source: " + source, 400);
|
|
389
|
-
}
|
|
390
|
-
return ctx.sources[source](id, reqOptions);
|
|
391
|
-
});
|
|
392
|
-
const getData = cachedPromise(async () => {
|
|
393
|
-
const src = await getSrc();
|
|
394
|
-
const data = await src.getData();
|
|
395
|
-
const meta = imageMeta__default['default'](data);
|
|
396
|
-
const mFormat = modifiers.f || modifiers.format;
|
|
397
|
-
let format = mFormat || meta.type;
|
|
398
|
-
if (format === "jpg") {
|
|
399
|
-
format = "jpeg";
|
|
400
|
-
}
|
|
401
|
-
if (meta.type === "svg" && !mFormat) {
|
|
402
|
-
return {
|
|
403
|
-
data,
|
|
404
|
-
format: "svg+xml",
|
|
405
|
-
meta
|
|
406
|
-
};
|
|
407
|
-
}
|
|
408
|
-
const animated = modifiers.animated !== void 0 || modifiers.a !== void 0;
|
|
409
|
-
if (animated) {
|
|
410
|
-
format = "webp";
|
|
411
|
-
}
|
|
412
|
-
let sharp = Sharp__default['default'](data, { animated });
|
|
413
|
-
Object.assign(sharp.options, options.sharp);
|
|
414
|
-
const handlers = Object.entries(modifiers).map(([name, args]) => ({ handler: getHandler(name), name, args })).filter((h) => h.handler).sort((a, b) => {
|
|
415
|
-
const aKey = (a.handler.order || a.name || "").toString();
|
|
416
|
-
const bKey = (b.handler.order || b.name || "").toString();
|
|
417
|
-
return aKey.localeCompare(bKey);
|
|
418
|
-
});
|
|
419
|
-
const handlerCtx = {};
|
|
420
|
-
for (const h of handlers) {
|
|
421
|
-
sharp = applyHandler(handlerCtx, sharp, h.handler, h.args) || sharp;
|
|
422
|
-
}
|
|
423
|
-
if (SUPPORTED_FORMATS.includes(format)) {
|
|
424
|
-
sharp = sharp.toFormat(format, {
|
|
425
|
-
quality: handlerCtx.quality,
|
|
426
|
-
progressive: format === "jpeg"
|
|
427
|
-
});
|
|
428
|
-
}
|
|
429
|
-
const newData = await sharp.toBuffer();
|
|
430
|
-
return {
|
|
431
|
-
data: newData,
|
|
432
|
-
format,
|
|
433
|
-
meta
|
|
434
|
-
};
|
|
435
|
-
});
|
|
436
|
-
return {
|
|
437
|
-
src: getSrc,
|
|
438
|
-
data: getData
|
|
439
|
-
};
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
async function _handleRequest(req, ipx) {
|
|
444
|
-
const res = {
|
|
445
|
-
statusCode: 200,
|
|
446
|
-
statusMessage: "",
|
|
447
|
-
headers: {},
|
|
448
|
-
body: ""
|
|
449
|
-
};
|
|
450
|
-
const [modifiersStr = "", ...idSegments] = req.url.substr(1).split("/");
|
|
451
|
-
const id = ufo.decode(idSegments.join("/"));
|
|
452
|
-
if (!modifiersStr) {
|
|
453
|
-
throw createError("Modifiers is missing in path: " + req.url, 400);
|
|
454
|
-
}
|
|
455
|
-
if (!id || id === "/") {
|
|
456
|
-
throw createError("Resource id is missing: " + req.url, 400);
|
|
457
|
-
}
|
|
458
|
-
const modifiers = Object.create(null);
|
|
459
|
-
if (modifiersStr !== "_") {
|
|
460
|
-
for (const p of modifiersStr.split(",")) {
|
|
461
|
-
const [key, value = ""] = p.split("_");
|
|
462
|
-
modifiers[key] = ufo.decode(value);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
const img = ipx(id, modifiers, req.options);
|
|
466
|
-
const src = await img.src();
|
|
467
|
-
if (src.mtime) {
|
|
468
|
-
if (req.headers["if-modified-since"]) {
|
|
469
|
-
if (new Date(req.headers["if-modified-since"]) >= src.mtime) {
|
|
470
|
-
res.statusCode = 304;
|
|
471
|
-
return res;
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
res.headers["Last-Modified"] = +src.mtime + "";
|
|
475
|
-
}
|
|
476
|
-
if (src.maxAge !== void 0) {
|
|
477
|
-
res.headers["Cache-Control"] = `max-age=${+src.maxAge}, public, s-maxage=${+src.maxAge}`;
|
|
478
|
-
}
|
|
479
|
-
const { data, format } = await img.data();
|
|
480
|
-
const etag = getEtag__default['default'](data);
|
|
481
|
-
res.headers.ETag = etag;
|
|
482
|
-
if (etag && req.headers["if-none-match"] === etag) {
|
|
483
|
-
res.statusCode = 304;
|
|
484
|
-
return res;
|
|
485
|
-
}
|
|
486
|
-
if (format) {
|
|
487
|
-
res.headers["Content-Type"] = `image/${format}`;
|
|
488
|
-
}
|
|
489
|
-
res.body = data;
|
|
490
|
-
return res;
|
|
491
|
-
}
|
|
492
|
-
function handleRequest(req, ipx) {
|
|
493
|
-
return _handleRequest(req, ipx).catch((err) => {
|
|
494
|
-
const statusCode = parseInt(err.statusCode) || 500;
|
|
495
|
-
const statusMessage = err.statusMessage ? xss__default['default'](err.statusMessage) : `IPX Error (${statusCode})`;
|
|
496
|
-
if (process.env.NODE_ENV !== "production" && statusCode === 500) {
|
|
497
|
-
console.error(err);
|
|
498
|
-
}
|
|
499
|
-
return {
|
|
500
|
-
statusCode,
|
|
501
|
-
statusMessage,
|
|
502
|
-
body: statusMessage,
|
|
503
|
-
headers: {}
|
|
504
|
-
};
|
|
505
|
-
});
|
|
506
|
-
}
|
|
507
|
-
function createIPXMiddleware(ipx) {
|
|
508
|
-
return function IPXMiddleware(req, res) {
|
|
509
|
-
handleRequest({ url: req.url, headers: req.headers }, ipx).then((_res) => {
|
|
510
|
-
res.statusCode = _res.statusCode;
|
|
511
|
-
res.statusMessage = _res.statusMessage;
|
|
512
|
-
for (const name in _res.headers) {
|
|
513
|
-
res.setHeader(name, _res.headers[name]);
|
|
514
|
-
}
|
|
515
|
-
res.end(_res.body);
|
|
516
|
-
});
|
|
517
|
-
};
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
exports.createIPX = createIPX;
|
|
521
|
-
exports.createIPXMiddleware = createIPXMiddleware;
|
|
522
|
-
exports.handleRequest = handleRequest;
|
|
5
|
+
const middleware = require('./chunks/middleware.cjs');
|
|
6
|
+
require('defu');
|
|
7
|
+
require('image-meta');
|
|
8
|
+
require('ufo');
|
|
9
|
+
require('fs');
|
|
10
|
+
require('pathe');
|
|
11
|
+
require('is-valid-path');
|
|
12
|
+
require('http');
|
|
13
|
+
require('https');
|
|
14
|
+
require('ohmyfetch');
|
|
15
|
+
require('destr');
|
|
16
|
+
require('etag');
|
|
17
|
+
require('xss');
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
exports.createIPX = middleware.createIPX;
|
|
22
|
+
exports.createIPXMiddleware = middleware.createIPXMiddleware;
|
|
23
|
+
exports.handleRequest = middleware.handleRequest;
|