ipx 3.1.0 → 3.1.2
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/dist/cli.cjs +7 -8
- package/dist/cli.mjs +4 -5
- package/dist/index.cjs +10 -11
- package/dist/index.d.cts +8 -1
- package/dist/index.d.mts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.mjs +2 -3
- package/dist/shared/ipx.CgX2Ix7n.cjs +1685 -0
- package/dist/shared/ipx.CzhtJU9h.mjs +1671 -0
- package/package.json +30 -27
- package/dist/shared/ipx.C8X6338M.mjs +0 -764
- package/dist/shared/ipx.CHfn1B1U.cjs +0 -778
|
@@ -0,0 +1,1671 @@
|
|
|
1
|
+
import { defu } from 'defu';
|
|
2
|
+
import { withLeadingSlash, hasProtocol, joinURL, decode } from 'ufo';
|
|
3
|
+
import { createError, toNodeListener, createApp, defineEventHandler, setResponseStatus, getRequestHeader, appendResponseHeader, send, getResponseHeader, setResponseHeader, toPlainHandler, toWebHandler } from 'h3';
|
|
4
|
+
import destr from 'destr';
|
|
5
|
+
import { negotiate } from '@fastify/accept-negotiator';
|
|
6
|
+
import getEtag from 'etag';
|
|
7
|
+
import { resolve, join, parse } from 'pathe';
|
|
8
|
+
import { ofetch } from 'ofetch';
|
|
9
|
+
|
|
10
|
+
const Handlers = {
|
|
11
|
+
__proto__: null,
|
|
12
|
+
get b () { return b; },
|
|
13
|
+
get background () { return background; },
|
|
14
|
+
get blur () { return blur; },
|
|
15
|
+
get crop () { return crop; },
|
|
16
|
+
get enlarge () { return enlarge; },
|
|
17
|
+
get extend () { return extend; },
|
|
18
|
+
get extract () { return extract; },
|
|
19
|
+
get fit () { return fit; },
|
|
20
|
+
get flatten () { return flatten; },
|
|
21
|
+
get flip () { return flip; },
|
|
22
|
+
get flop () { return flop; },
|
|
23
|
+
get gamma () { return gamma; },
|
|
24
|
+
get grayscale () { return grayscale; },
|
|
25
|
+
get h () { return h; },
|
|
26
|
+
get height () { return height; },
|
|
27
|
+
get kernel () { return kernel; },
|
|
28
|
+
get median () { return median; },
|
|
29
|
+
get modulate () { return modulate; },
|
|
30
|
+
get negate () { return negate; },
|
|
31
|
+
get normalize () { return normalize; },
|
|
32
|
+
get pos () { return pos; },
|
|
33
|
+
get position () { return position; },
|
|
34
|
+
get q () { return q; },
|
|
35
|
+
get quality () { return quality; },
|
|
36
|
+
get resize () { return resize; },
|
|
37
|
+
get rotate () { return rotate; },
|
|
38
|
+
get s () { return s; },
|
|
39
|
+
get sharpen () { return sharpen; },
|
|
40
|
+
get threshold () { return threshold; },
|
|
41
|
+
get tint () { return tint; },
|
|
42
|
+
get trim () { return trim; },
|
|
43
|
+
get w () { return w; },
|
|
44
|
+
get width () { return width; }
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const decoder = new TextDecoder();
|
|
48
|
+
const toUTF8String = (input, start = 0, end = input.length) => decoder.decode(input.slice(start, end));
|
|
49
|
+
const toHexString = (input, start = 0, end = input.length) => input.slice(start, end).reduce((memo, i) => memo + ("0" + i.toString(16)).slice(-2), "");
|
|
50
|
+
const readInt16LE = (input, offset = 0) => {
|
|
51
|
+
const val = input[offset] + input[offset + 1] * 256;
|
|
52
|
+
return val >= 2 ** 15 ? val - 2 ** 16 : val;
|
|
53
|
+
};
|
|
54
|
+
const readUInt16BE = (input, offset = 0) => input[offset] * 256 + input[offset + 1];
|
|
55
|
+
const readUInt16LE = (input, offset = 0) => input[offset] + input[offset + 1] * 256;
|
|
56
|
+
const readUInt24LE = (input, offset = 0) => input[offset] + input[offset + 1] * 256 + input[offset + 2] * 2 ** 16;
|
|
57
|
+
const readInt32LE = (input, offset = 0) => {
|
|
58
|
+
const val = readUInt32LE(input, offset);
|
|
59
|
+
return val >= 2 ** 31 ? val - 2 ** 32 : val;
|
|
60
|
+
};
|
|
61
|
+
const readUInt32BE = (input, offset = 0) => input[offset] * 2 ** 24 + input[offset + 1] * 2 ** 16 + input[offset + 2] * 256 + input[offset + 3];
|
|
62
|
+
const readUInt32LE = (input, offset = 0) => input[offset] + input[offset + 1] * 256 + input[offset + 2] * 2 ** 16 + input[offset + 3] * 2 ** 24;
|
|
63
|
+
const methods = {
|
|
64
|
+
readUInt16BE,
|
|
65
|
+
readUInt16LE,
|
|
66
|
+
readUInt32BE,
|
|
67
|
+
readUInt32LE
|
|
68
|
+
};
|
|
69
|
+
function readUInt(input, bits, offset, isBigEndian) {
|
|
70
|
+
offset = offset || 0;
|
|
71
|
+
const endian = isBigEndian ? "BE" : "LE";
|
|
72
|
+
const methodName = "readUInt" + bits + endian;
|
|
73
|
+
return methods[methodName](input, offset);
|
|
74
|
+
}
|
|
75
|
+
const encoder = new TextEncoder();
|
|
76
|
+
const toTagCode = (tag) => readUInt32BE(encoder.encode(tag));
|
|
77
|
+
const BOX_HEADER_SIZE = 8;
|
|
78
|
+
function findBox(input, boxName, startOffset = 0, endOffset = input.length) {
|
|
79
|
+
const boxType = toTagCode(boxName);
|
|
80
|
+
let offset = startOffset;
|
|
81
|
+
while (offset + BOX_HEADER_SIZE <= endOffset) {
|
|
82
|
+
const size = readUInt32BE(input, offset);
|
|
83
|
+
if (size < BOX_HEADER_SIZE || offset + size > endOffset) return;
|
|
84
|
+
if (readUInt32BE(input, offset + 4) === boxType) return {
|
|
85
|
+
offset,
|
|
86
|
+
size
|
|
87
|
+
};
|
|
88
|
+
offset += size;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const CORE_HEADER_SIZE = 12;
|
|
92
|
+
const BMP = {
|
|
93
|
+
validate: (input) => toUTF8String(input, 0, 2) === "BM",
|
|
94
|
+
calculate: (input) => {
|
|
95
|
+
if (readUInt32LE(input, 14) === CORE_HEADER_SIZE) return {
|
|
96
|
+
height: readUInt16LE(input, 20),
|
|
97
|
+
width: readUInt16LE(input, 18)
|
|
98
|
+
};
|
|
99
|
+
return {
|
|
100
|
+
height: Math.abs(readInt32LE(input, 22)),
|
|
101
|
+
width: readInt32LE(input, 18)
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const TYPE_ICON = 1;
|
|
106
|
+
const SIZE_HEADER$1 = 6;
|
|
107
|
+
const SIZE_IMAGE_ENTRY = 16;
|
|
108
|
+
function getSizeFromOffset(input, offset) {
|
|
109
|
+
const value = input[offset];
|
|
110
|
+
return value === 0 ? 256 : value;
|
|
111
|
+
}
|
|
112
|
+
function getImageSize$1(input, imageIndex) {
|
|
113
|
+
const offset = SIZE_HEADER$1 + imageIndex * SIZE_IMAGE_ENTRY;
|
|
114
|
+
return {
|
|
115
|
+
height: getSizeFromOffset(input, offset + 1),
|
|
116
|
+
width: getSizeFromOffset(input, offset)
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const ICO = {
|
|
120
|
+
validate(input) {
|
|
121
|
+
const reserved = readUInt16LE(input, 0);
|
|
122
|
+
const imageCount = readUInt16LE(input, 4);
|
|
123
|
+
if (reserved !== 0 || imageCount === 0) return false;
|
|
124
|
+
return readUInt16LE(input, 2) === TYPE_ICON;
|
|
125
|
+
},
|
|
126
|
+
calculate(input) {
|
|
127
|
+
const nbImages = readUInt16LE(input, 4);
|
|
128
|
+
if (SIZE_HEADER$1 + nbImages * SIZE_IMAGE_ENTRY > input.length) throw new TypeError("Invalid ICO");
|
|
129
|
+
const imageSize = getImageSize$1(input, 0);
|
|
130
|
+
if (nbImages === 1) return imageSize;
|
|
131
|
+
const imgs = [imageSize];
|
|
132
|
+
for (let imageIndex = 1; imageIndex < nbImages; imageIndex += 1) imgs.push(getImageSize$1(input, imageIndex));
|
|
133
|
+
return {
|
|
134
|
+
height: imageSize.height,
|
|
135
|
+
images: imgs,
|
|
136
|
+
width: imageSize.width
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const TYPE_CURSOR = 2;
|
|
141
|
+
const CUR = {
|
|
142
|
+
validate(input) {
|
|
143
|
+
const reserved = readUInt16LE(input, 0);
|
|
144
|
+
const imageCount = readUInt16LE(input, 4);
|
|
145
|
+
if (reserved !== 0 || imageCount === 0) return false;
|
|
146
|
+
return readUInt16LE(input, 2) === TYPE_CURSOR;
|
|
147
|
+
},
|
|
148
|
+
calculate: (input) => ICO.calculate(input)
|
|
149
|
+
};
|
|
150
|
+
const DDS = {
|
|
151
|
+
validate: (input) => readUInt32LE(input, 0) === 542327876,
|
|
152
|
+
calculate: (input) => ({
|
|
153
|
+
height: readUInt32LE(input, 12),
|
|
154
|
+
width: readUInt32LE(input, 16)
|
|
155
|
+
})
|
|
156
|
+
};
|
|
157
|
+
const gifRegexp = /^GIF8[79]a/;
|
|
158
|
+
const GIF = {
|
|
159
|
+
validate: (input) => gifRegexp.test(toUTF8String(input, 0, 6)),
|
|
160
|
+
calculate: (input) => ({
|
|
161
|
+
height: readUInt16LE(input, 8),
|
|
162
|
+
width: readUInt16LE(input, 6)
|
|
163
|
+
})
|
|
164
|
+
};
|
|
165
|
+
const FTYP = toTagCode("ftyp");
|
|
166
|
+
const ISPE = toTagCode("ispe");
|
|
167
|
+
const CLAP = toTagCode("clap");
|
|
168
|
+
function larger(largest, image) {
|
|
169
|
+
return !largest || image.width * image.height > largest.width * largest.height ? image : largest;
|
|
170
|
+
}
|
|
171
|
+
function readLargestImageSize(input) {
|
|
172
|
+
const metaBox = findBox(input, "meta");
|
|
173
|
+
const iprpBox = metaBox && findBox(input, "iprp", metaBox.offset + 12, metaBox.offset + metaBox.size);
|
|
174
|
+
const ipcoBox = iprpBox && findBox(input, "ipco", iprpBox.offset + 8, iprpBox.offset + iprpBox.size);
|
|
175
|
+
if (!ipcoBox) throw new TypeError("Invalid HEIF, no ipco box found");
|
|
176
|
+
let largest;
|
|
177
|
+
let image;
|
|
178
|
+
const end = ipcoBox.offset + ipcoBox.size;
|
|
179
|
+
let offset = ipcoBox.offset + 8;
|
|
180
|
+
while (offset + 8 <= end) {
|
|
181
|
+
const size = readUInt32BE(input, offset);
|
|
182
|
+
if (size < 8 || offset + size > end) throw new TypeError("Invalid HEIF, corrupt ipco box");
|
|
183
|
+
const type = readUInt32BE(input, offset + 4);
|
|
184
|
+
if (type === ISPE) {
|
|
185
|
+
if (size < 20) throw new TypeError("Invalid HEIF, corrupt ispe box");
|
|
186
|
+
if (image) largest = larger(largest, image);
|
|
187
|
+
image = {
|
|
188
|
+
width: readUInt32BE(input, offset + 12),
|
|
189
|
+
height: readUInt32BE(input, offset + 16)
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
if (type === CLAP && size >= 24 && image) {
|
|
193
|
+
const widthD = readUInt32BE(input, offset + 12);
|
|
194
|
+
const heightD = readUInt32BE(input, offset + 20);
|
|
195
|
+
if (widthD > 0 && heightD > 0) {
|
|
196
|
+
image.width = Math.round(readUInt32BE(input, offset + 8) / widthD);
|
|
197
|
+
image.height = Math.round(readUInt32BE(input, offset + 16) / heightD);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
offset += size;
|
|
201
|
+
}
|
|
202
|
+
if (!image) throw new TypeError("Invalid HEIF, no ispe box found");
|
|
203
|
+
return larger(largest, image);
|
|
204
|
+
}
|
|
205
|
+
const AVIF_BRANDS = new Set(["avif", "avis"].map((brand) => toTagCode(brand)));
|
|
206
|
+
function detectHeifType(input) {
|
|
207
|
+
const ftypSize = readUInt32BE(input, 0);
|
|
208
|
+
if (readUInt32BE(input, 4) !== FTYP || ftypSize < 12 || ftypSize > input.length) return;
|
|
209
|
+
if (AVIF_BRANDS.has(readUInt32BE(input, 8))) return "avif";
|
|
210
|
+
const majorBrand = toUTF8String(input, 8, 12);
|
|
211
|
+
if ([
|
|
212
|
+
"mif1",
|
|
213
|
+
"msf1",
|
|
214
|
+
"miaf"
|
|
215
|
+
].includes(majorBrand)) {
|
|
216
|
+
for (let offset = 16; offset + 4 <= ftypSize; offset += 4) if (AVIF_BRANDS.has(readUInt32BE(input, offset))) return "avif";
|
|
217
|
+
}
|
|
218
|
+
return [
|
|
219
|
+
"heic",
|
|
220
|
+
"heix",
|
|
221
|
+
"hevc",
|
|
222
|
+
"hevx",
|
|
223
|
+
"mif1",
|
|
224
|
+
"msf1"
|
|
225
|
+
].includes(majorBrand) ? "heic" : void 0;
|
|
226
|
+
}
|
|
227
|
+
const HEIC = {
|
|
228
|
+
validate: (input) => detectHeifType(input) === "heic",
|
|
229
|
+
calculate: (input) => readLargestImageSize(input)
|
|
230
|
+
};
|
|
231
|
+
const SIZE_HEADER = 8;
|
|
232
|
+
const FILE_LENGTH_OFFSET = 4;
|
|
233
|
+
const ENTRY_LENGTH_OFFSET = 4;
|
|
234
|
+
const ICON_TYPE_SIZE = {
|
|
235
|
+
ICON: 32,
|
|
236
|
+
"ICN#": 32,
|
|
237
|
+
"icm#": 16,
|
|
238
|
+
icm4: 16,
|
|
239
|
+
icm8: 16,
|
|
240
|
+
"ics#": 16,
|
|
241
|
+
ics4: 16,
|
|
242
|
+
ics8: 16,
|
|
243
|
+
is32: 16,
|
|
244
|
+
s8mk: 16,
|
|
245
|
+
icp4: 16,
|
|
246
|
+
ic04: 16,
|
|
247
|
+
icsb: 18,
|
|
248
|
+
sb24: 24,
|
|
249
|
+
icl4: 32,
|
|
250
|
+
icl8: 32,
|
|
251
|
+
il32: 32,
|
|
252
|
+
l8mk: 32,
|
|
253
|
+
icp5: 32,
|
|
254
|
+
ic05: 32,
|
|
255
|
+
ic11: 32,
|
|
256
|
+
icsB: 36,
|
|
257
|
+
ich4: 48,
|
|
258
|
+
ich8: 48,
|
|
259
|
+
ih32: 48,
|
|
260
|
+
h8mk: 48,
|
|
261
|
+
SB24: 48,
|
|
262
|
+
icp6: 64,
|
|
263
|
+
ic12: 64,
|
|
264
|
+
it32: 128,
|
|
265
|
+
t8mk: 128,
|
|
266
|
+
ic07: 128,
|
|
267
|
+
ic08: 256,
|
|
268
|
+
ic13: 256,
|
|
269
|
+
ic09: 512,
|
|
270
|
+
ic14: 512,
|
|
271
|
+
ic10: 1024
|
|
272
|
+
};
|
|
273
|
+
const ICON_TYPES = new Map(Object.keys(ICON_TYPE_SIZE).map((type) => [toTagCode(type), type]));
|
|
274
|
+
function readImageHeader(input, imageOffset) {
|
|
275
|
+
const imageLengthOffset = imageOffset + ENTRY_LENGTH_OFFSET;
|
|
276
|
+
if (imageLengthOffset + 4 > input.length) throw new TypeError("Invalid ICNS");
|
|
277
|
+
const entryLength = readUInt32BE(input, imageLengthOffset);
|
|
278
|
+
if (entryLength < 8) throw new TypeError("Invalid ICNS");
|
|
279
|
+
return [ICON_TYPES.get(readUInt32BE(input, imageOffset)), entryLength];
|
|
280
|
+
}
|
|
281
|
+
function getImageSize(type) {
|
|
282
|
+
const size = ICON_TYPE_SIZE[type];
|
|
283
|
+
return {
|
|
284
|
+
width: size,
|
|
285
|
+
height: size,
|
|
286
|
+
type
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
const ICNS = {
|
|
290
|
+
validate: (input) => toUTF8String(input, 0, 4) === "icns",
|
|
291
|
+
calculate(input) {
|
|
292
|
+
const inputLength = input.length;
|
|
293
|
+
const fileLength = readUInt32BE(input, FILE_LENGTH_OFFSET);
|
|
294
|
+
let imageOffset = SIZE_HEADER;
|
|
295
|
+
const images = [];
|
|
296
|
+
const seenTypes = /* @__PURE__ */ new Set();
|
|
297
|
+
while (imageOffset < fileLength && imageOffset < inputLength) {
|
|
298
|
+
const [type, entryLength] = readImageHeader(input, imageOffset);
|
|
299
|
+
imageOffset += entryLength;
|
|
300
|
+
if (type !== void 0 && !seenTypes.has(type)) {
|
|
301
|
+
seenTypes.add(type);
|
|
302
|
+
images.push(getImageSize(type));
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (images.length === 0) throw new TypeError("Invalid ICNS, no icons found");
|
|
306
|
+
if (images.length === 1) return images[0];
|
|
307
|
+
return {
|
|
308
|
+
height: images[0].height,
|
|
309
|
+
images,
|
|
310
|
+
width: images[0].width
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
const J2C = {
|
|
315
|
+
validate: (input) => readUInt32BE(input, 0) === 4283432785,
|
|
316
|
+
calculate: (input) => ({
|
|
317
|
+
height: readUInt32BE(input, 12) - readUInt32BE(input, 20),
|
|
318
|
+
width: readUInt32BE(input, 8) - readUInt32BE(input, 16)
|
|
319
|
+
})
|
|
320
|
+
};
|
|
321
|
+
const JP2 = {
|
|
322
|
+
validate(input) {
|
|
323
|
+
if (toUTF8String(input, 4, 8) !== "jP ") return false;
|
|
324
|
+
return findBox(input, "ftyp") !== void 0;
|
|
325
|
+
},
|
|
326
|
+
calculate(input) {
|
|
327
|
+
const jp2hBox = findBox(input, "jp2h");
|
|
328
|
+
const ihdrBox = jp2hBox && findBox(input, "ihdr", jp2hBox.offset + 8, jp2hBox.offset + jp2hBox.size);
|
|
329
|
+
if (!ihdrBox || ihdrBox.size < 16) throw new TypeError("Invalid JPEG 2000, no ihdr box found");
|
|
330
|
+
return {
|
|
331
|
+
height: readUInt32BE(input, ihdrBox.offset + 8),
|
|
332
|
+
width: readUInt32BE(input, ihdrBox.offset + 12)
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
const EXIF_MARKER = toTagCode("Exif");
|
|
337
|
+
const APP1_DATA_SIZE_BYTES = 2;
|
|
338
|
+
const EXIF_HEADER_BYTES = 6;
|
|
339
|
+
const BIG_ENDIAN_BYTE_ALIGN = 19789;
|
|
340
|
+
const LITTLE_ENDIAN_BYTE_ALIGN = 18761;
|
|
341
|
+
const TIFF_MAGIC = 42;
|
|
342
|
+
const TIFF_HEADER_BYTES = 8;
|
|
343
|
+
const IDF_ENTRY_BYTES = 12;
|
|
344
|
+
const NUM_DIRECTORY_ENTRIES_BYTES = 2;
|
|
345
|
+
function isEXIF(input) {
|
|
346
|
+
return readUInt32BE(input, 2) === EXIF_MARKER;
|
|
347
|
+
}
|
|
348
|
+
function extractSize(input, index) {
|
|
349
|
+
return {
|
|
350
|
+
height: readUInt16BE(input, index),
|
|
351
|
+
width: readUInt16BE(input, index + 2)
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
function extractOrientation(exifBlock, isBigEndian) {
|
|
355
|
+
if (readUInt(exifBlock, 16, 8, isBigEndian) !== TIFF_MAGIC) return;
|
|
356
|
+
const idfOffset = readUInt(exifBlock, 32, 10, isBigEndian);
|
|
357
|
+
const offset = EXIF_HEADER_BYTES + idfOffset;
|
|
358
|
+
if (!(idfOffset >= TIFF_HEADER_BYTES && offset + NUM_DIRECTORY_ENTRIES_BYTES <= exifBlock.length)) return;
|
|
359
|
+
const idfDirectoryEntries = readUInt(exifBlock, 16, offset, isBigEndian);
|
|
360
|
+
for (let directoryEntryNumber = 0; directoryEntryNumber < idfDirectoryEntries; directoryEntryNumber++) {
|
|
361
|
+
const start = offset + NUM_DIRECTORY_ENTRIES_BYTES + directoryEntryNumber * IDF_ENTRY_BYTES;
|
|
362
|
+
const end = start + IDF_ENTRY_BYTES;
|
|
363
|
+
if (start > exifBlock.length) return;
|
|
364
|
+
const block = exifBlock.slice(start, end);
|
|
365
|
+
if (readUInt(block, 16, 0, isBigEndian) === 274) {
|
|
366
|
+
if (readUInt(block, 16, 2, isBigEndian) !== 3) return;
|
|
367
|
+
if (readUInt(block, 32, 4, isBigEndian) !== 1) return;
|
|
368
|
+
return readUInt(block, 16, 8, isBigEndian);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
function validateExifBlock(input, index) {
|
|
373
|
+
const exifBlock = input.slice(APP1_DATA_SIZE_BYTES, index);
|
|
374
|
+
const byteAlign = readUInt16BE(exifBlock, EXIF_HEADER_BYTES);
|
|
375
|
+
const isBigEndian = byteAlign === BIG_ENDIAN_BYTE_ALIGN;
|
|
376
|
+
if (isBigEndian || byteAlign === LITTLE_ENDIAN_BYTE_ALIGN) return extractOrientation(exifBlock, isBigEndian);
|
|
377
|
+
}
|
|
378
|
+
function validateInput(input, index) {
|
|
379
|
+
if (index > input.length) throw new TypeError("Corrupt JPG, exceeded buffer limits");
|
|
380
|
+
}
|
|
381
|
+
function findMarker(input, index) {
|
|
382
|
+
let marker = input.indexOf(255, index);
|
|
383
|
+
while (marker !== -1) {
|
|
384
|
+
while (input[marker + 1] === 255) marker++;
|
|
385
|
+
if (input[marker + 1] !== 0) return marker;
|
|
386
|
+
marker = input.indexOf(255, marker + 2);
|
|
387
|
+
}
|
|
388
|
+
throw new TypeError("Invalid JPG, marker table corrupted");
|
|
389
|
+
}
|
|
390
|
+
const JPG = {
|
|
391
|
+
validate: (input) => toHexString(input, 0, 2) === "ffd8",
|
|
392
|
+
calculate(input) {
|
|
393
|
+
input = input.subarray(2);
|
|
394
|
+
let orientation;
|
|
395
|
+
let next;
|
|
396
|
+
while (input.length > 0) {
|
|
397
|
+
const marker = findMarker(input, 0);
|
|
398
|
+
next = input[marker + 1];
|
|
399
|
+
if (next >= 192 && next <= 195 || next >= 201 && next <= 203) {
|
|
400
|
+
const size = extractSize(input, marker + 5);
|
|
401
|
+
if (!orientation) return size;
|
|
402
|
+
return {
|
|
403
|
+
height: size.height,
|
|
404
|
+
orientation,
|
|
405
|
+
width: size.width
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
if (next === 218) break;
|
|
409
|
+
input = input.subarray(marker + 2);
|
|
410
|
+
const i = readUInt16BE(input, 0);
|
|
411
|
+
validateInput(input, i);
|
|
412
|
+
if (isEXIF(input)) orientation = validateExifBlock(input, i);
|
|
413
|
+
input = input.subarray(i);
|
|
414
|
+
}
|
|
415
|
+
throw new TypeError("Invalid JPG, no size found");
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
function createBitReader(input) {
|
|
419
|
+
let bitOffset = 16;
|
|
420
|
+
return (length) => {
|
|
421
|
+
let result = 0;
|
|
422
|
+
for (let i = 0; i < length; i++, bitOffset++) {
|
|
423
|
+
const byte = input[bitOffset >> 3];
|
|
424
|
+
if (byte === void 0) throw new TypeError("Invalid JXL, truncated codestream");
|
|
425
|
+
result += (byte >> (bitOffset & 7) & 1) * 2 ** i;
|
|
426
|
+
}
|
|
427
|
+
return result;
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
const RATIOS = [
|
|
431
|
+
[1, 1],
|
|
432
|
+
[12, 10],
|
|
433
|
+
[4, 3],
|
|
434
|
+
[3, 2],
|
|
435
|
+
[16, 9],
|
|
436
|
+
[5, 4],
|
|
437
|
+
[2, 1]
|
|
438
|
+
];
|
|
439
|
+
function calculateCodestream(input) {
|
|
440
|
+
const readBits = createBitReader(input);
|
|
441
|
+
const readSize = (isSmall) => isSmall ? 8 * (1 + readBits(5)) : 1 + readBits([
|
|
442
|
+
9,
|
|
443
|
+
13,
|
|
444
|
+
18,
|
|
445
|
+
30
|
|
446
|
+
][readBits(2)]);
|
|
447
|
+
const isSmall = readBits(1) === 1;
|
|
448
|
+
const height = readSize(isSmall);
|
|
449
|
+
const ratio = readBits(3);
|
|
450
|
+
if (ratio === 0) return {
|
|
451
|
+
height,
|
|
452
|
+
width: readSize(isSmall)
|
|
453
|
+
};
|
|
454
|
+
const [numerator, denominator] = RATIOS[ratio - 1];
|
|
455
|
+
return {
|
|
456
|
+
height,
|
|
457
|
+
width: Math.floor(height * numerator / denominator)
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
const CODESTREAM_PREFIX_BYTES = 32;
|
|
461
|
+
const JXLC = toTagCode("jxlc");
|
|
462
|
+
const JXLP = toTagCode("jxlp");
|
|
463
|
+
function extractCodestream(input) {
|
|
464
|
+
const parts = [];
|
|
465
|
+
let length = 0;
|
|
466
|
+
let offset = 0;
|
|
467
|
+
while (offset + 8 <= input.length && length < CODESTREAM_PREFIX_BYTES) {
|
|
468
|
+
let size = readUInt32BE(input, offset);
|
|
469
|
+
const type = readUInt32BE(input, offset + 4);
|
|
470
|
+
let headerSize = 8;
|
|
471
|
+
if (size === 1) {
|
|
472
|
+
if (offset + 16 > input.length) break;
|
|
473
|
+
size = readUInt32BE(input, offset + 8) * 2 ** 32 + readUInt32BE(input, offset + 12);
|
|
474
|
+
headerSize = 16;
|
|
475
|
+
} else if (size === 0) size = input.length - offset;
|
|
476
|
+
if (type === JXLC || type === JXLP) {
|
|
477
|
+
const start = offset + headerSize + (type === JXLP ? 4 : 0);
|
|
478
|
+
if (size < start - offset) throw new TypeError(`Invalid JXL, corrupt ${type === JXLP ? "jxlp" : "jxlc"} box`);
|
|
479
|
+
const end = Math.min(offset + size, input.length, start + CODESTREAM_PREFIX_BYTES - length);
|
|
480
|
+
if (end > start) {
|
|
481
|
+
parts.push(input.subarray(start, end));
|
|
482
|
+
length += end - start;
|
|
483
|
+
}
|
|
484
|
+
if (type === JXLC) break;
|
|
485
|
+
}
|
|
486
|
+
if (size < headerSize) throw new TypeError("Invalid JXL, corrupt box");
|
|
487
|
+
offset += size;
|
|
488
|
+
}
|
|
489
|
+
const codestream = new Uint8Array(length);
|
|
490
|
+
let position = 0;
|
|
491
|
+
for (const part of parts) {
|
|
492
|
+
codestream.set(part, position);
|
|
493
|
+
position += part.length;
|
|
494
|
+
}
|
|
495
|
+
return codestream;
|
|
496
|
+
}
|
|
497
|
+
const isCodestream = (input) => toHexString(input, 0, 2) === "ff0a";
|
|
498
|
+
const JXL = {
|
|
499
|
+
validate: (input) => {
|
|
500
|
+
if (isCodestream(input)) return true;
|
|
501
|
+
if (toUTF8String(input, 4, 8) !== "JXL ") return false;
|
|
502
|
+
const ftypBox = findBox(input, "ftyp");
|
|
503
|
+
return ftypBox !== void 0 && toUTF8String(input, ftypBox.offset + 8, ftypBox.offset + 12) === "jxl ";
|
|
504
|
+
},
|
|
505
|
+
calculate: (input) => {
|
|
506
|
+
const codestream = isCodestream(input) ? input : extractCodestream(input);
|
|
507
|
+
if (!isCodestream(codestream)) throw new TypeError("Invalid JXL, no codestream found");
|
|
508
|
+
return calculateCodestream(codestream);
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
const LITTLE_ENDIAN = 67305985;
|
|
512
|
+
const BIG_ENDIAN = 16909060;
|
|
513
|
+
const KTX = {
|
|
514
|
+
validate: (input) => {
|
|
515
|
+
const signature = toUTF8String(input, 1, 7);
|
|
516
|
+
return signature === "KTX 11" || signature === "KTX 20";
|
|
517
|
+
},
|
|
518
|
+
calculate: (input) => {
|
|
519
|
+
if (input[5] === 50) return {
|
|
520
|
+
height: readUInt32LE(input, 24),
|
|
521
|
+
width: readUInt32LE(input, 20)
|
|
522
|
+
};
|
|
523
|
+
const endianness = readUInt32LE(input, 12);
|
|
524
|
+
if (endianness !== LITTLE_ENDIAN && endianness !== BIG_ENDIAN) throw new TypeError("Invalid KTX, unknown endianness");
|
|
525
|
+
const isBigEndian = endianness === BIG_ENDIAN;
|
|
526
|
+
return {
|
|
527
|
+
height: readUInt(input, 32, 40, isBigEndian),
|
|
528
|
+
width: readUInt(input, 32, 36, isBigEndian)
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
const pngSignature = "PNG\r\n\n";
|
|
533
|
+
const pngImageHeaderChunkName = "IHDR";
|
|
534
|
+
const pngFriedChunkName = "CgBI";
|
|
535
|
+
const PNG = {
|
|
536
|
+
validate(input) {
|
|
537
|
+
if (pngSignature === toUTF8String(input, 1, 8)) {
|
|
538
|
+
let chunkName = toUTF8String(input, 12, 16);
|
|
539
|
+
if (chunkName === pngFriedChunkName) chunkName = toUTF8String(input, 28, 32);
|
|
540
|
+
if (chunkName !== pngImageHeaderChunkName) throw new TypeError("Invalid PNG");
|
|
541
|
+
return true;
|
|
542
|
+
}
|
|
543
|
+
return false;
|
|
544
|
+
},
|
|
545
|
+
calculate(input) {
|
|
546
|
+
if (toUTF8String(input, 12, 16) === pngFriedChunkName) return {
|
|
547
|
+
height: readUInt32BE(input, 36),
|
|
548
|
+
width: readUInt32BE(input, 32)
|
|
549
|
+
};
|
|
550
|
+
return {
|
|
551
|
+
height: readUInt32BE(input, 20),
|
|
552
|
+
width: readUInt32BE(input, 16)
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
const PNMTypes = {
|
|
557
|
+
P1: "pbm/ascii",
|
|
558
|
+
P2: "pgm/ascii",
|
|
559
|
+
P3: "ppm/ascii",
|
|
560
|
+
P4: "pbm",
|
|
561
|
+
P5: "pgm",
|
|
562
|
+
P6: "ppm",
|
|
563
|
+
P7: "pam",
|
|
564
|
+
PF: "pfm",
|
|
565
|
+
Pf: "pfm"
|
|
566
|
+
};
|
|
567
|
+
const isLineBreak = (byte) => byte === 10 || byte === 13;
|
|
568
|
+
const isWhitespace = (byte) => byte === 32 || byte >= 9 && byte <= 13;
|
|
569
|
+
const toInteger = (value = "") => /^\d+$/.test(value) ? Number(value) : NaN;
|
|
570
|
+
const PAMLine = /^\s*(width|height|endhdr)(?!\S)\s*(\S*)/i;
|
|
571
|
+
const handlers = {
|
|
572
|
+
default: (input) => {
|
|
573
|
+
const [width, height] = readHeader(input, "tokens");
|
|
574
|
+
return {
|
|
575
|
+
height: toInteger(height),
|
|
576
|
+
width: toInteger(width)
|
|
577
|
+
};
|
|
578
|
+
},
|
|
579
|
+
pam: (input) => {
|
|
580
|
+
const size = {};
|
|
581
|
+
for (const line of readHeader(input, "lines")) {
|
|
582
|
+
const [, tag = "", value] = PAMLine.exec(line) || [];
|
|
583
|
+
const key = tag.toLowerCase();
|
|
584
|
+
if (key === "width" || key === "height") size[key] = toInteger(value);
|
|
585
|
+
if (key === "endhdr" || "width" in size && "height" in size) break;
|
|
586
|
+
}
|
|
587
|
+
return {
|
|
588
|
+
height: size.height,
|
|
589
|
+
width: size.width
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
function* readHeader(input, unit) {
|
|
594
|
+
const isSeparator = unit === "lines" ? isLineBreak : isWhitespace;
|
|
595
|
+
const values = unit === "lines" ? /[^\n\r]+/g : /[^\t-\r ]+/g;
|
|
596
|
+
let start = 2;
|
|
597
|
+
while (start < input.length) {
|
|
598
|
+
let end = start;
|
|
599
|
+
let cut = start;
|
|
600
|
+
let inComment = false;
|
|
601
|
+
while (end < input.length && (end - start < 1024 || cut === start)) {
|
|
602
|
+
const byte = input[end++];
|
|
603
|
+
inComment = byte === 35 || inComment && !isLineBreak(byte);
|
|
604
|
+
if (!inComment && isSeparator(byte)) cut = end;
|
|
605
|
+
}
|
|
606
|
+
if (end === input.length) cut = end;
|
|
607
|
+
yield* toUTF8String(input, start, cut).replace(/#[^\n\r]*/g, "\n").match(values) || [];
|
|
608
|
+
start = cut;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
const PNM = {
|
|
612
|
+
validate: (input) => toUTF8String(input, 0, 2) in PNMTypes,
|
|
613
|
+
calculate(input) {
|
|
614
|
+
const signature = toUTF8String(input, 0, 2);
|
|
615
|
+
const type = PNMTypes[signature];
|
|
616
|
+
return (handlers[type] || handlers.default)(input);
|
|
617
|
+
}
|
|
618
|
+
};
|
|
619
|
+
const PSD = {
|
|
620
|
+
validate: (input) => toUTF8String(input, 0, 4) === "8BPS",
|
|
621
|
+
calculate: (input) => ({
|
|
622
|
+
height: readUInt32BE(input, 14),
|
|
623
|
+
width: readUInt32BE(input, 18)
|
|
624
|
+
})
|
|
625
|
+
};
|
|
626
|
+
const svgReg = /<svg\s([^"'>]|"[^"]*"|'[^']*')*>/;
|
|
627
|
+
const commentReg = /<!--[\s\S]*?(?:-->|$)/g;
|
|
628
|
+
const extractorRegExps = {
|
|
629
|
+
height: /\sheight=(["'])([^%]+?)\1/,
|
|
630
|
+
viewbox: /\sviewbox=(["'])([\s\S]+?)\1/i,
|
|
631
|
+
width: /\swidth=(["'])([^%]+?)\1/
|
|
632
|
+
};
|
|
633
|
+
const TAG_START = 60;
|
|
634
|
+
const TAG_END = 62;
|
|
635
|
+
const SVG_TAG = [
|
|
636
|
+
TAG_START,
|
|
637
|
+
115,
|
|
638
|
+
118,
|
|
639
|
+
103
|
|
640
|
+
];
|
|
641
|
+
const DOUBLE_QUOTE = 34;
|
|
642
|
+
const SINGLE_QUOTE = 39;
|
|
643
|
+
const unicodeSpaceReg = /^(?:\u00C2\u00A0|\u00E1\u009A\u0080|\u00E2\u0080[\u0080-\u008A\u00A8\u00A9\u00AF]|\u00E2\u0081\u009F|\u00E3\u0080\u0080|\u00EF\u00BB\u00BF)/;
|
|
644
|
+
function isSpaceAt(input, offset) {
|
|
645
|
+
const byte = input[offset];
|
|
646
|
+
if (byte < 128) return byte === 32 || byte >= 9 && byte <= 13;
|
|
647
|
+
const bytes = String.fromCharCode(byte, input[offset + 1], input[offset + 2]);
|
|
648
|
+
return unicodeSpaceReg.test(bytes);
|
|
649
|
+
}
|
|
650
|
+
const isCommentAt = (input, offset) => input[offset] === TAG_START && offset + 3 < input.length && input[offset + 1] === 33 && input[offset + 2] === 45 && input[offset + 3] === 45;
|
|
651
|
+
function skipComment(input, offset) {
|
|
652
|
+
for (let end = offset + 4; end + 2 < input.length; end++) if (input[end] === 45 && input[end + 1] === 45 && input[end + 2] === TAG_END) return end + 3;
|
|
653
|
+
return input.length;
|
|
654
|
+
}
|
|
655
|
+
function extractRoot(input) {
|
|
656
|
+
const length = input.length;
|
|
657
|
+
let start = 0;
|
|
658
|
+
let offset = 0;
|
|
659
|
+
let matched = 0;
|
|
660
|
+
while (offset < length) {
|
|
661
|
+
const byte = input[offset];
|
|
662
|
+
if (byte === TAG_START && isCommentAt(input, offset)) {
|
|
663
|
+
offset = skipComment(input, offset);
|
|
664
|
+
continue;
|
|
665
|
+
}
|
|
666
|
+
if (matched === SVG_TAG.length && isSpaceAt(input, offset)) break;
|
|
667
|
+
if (byte === TAG_START) {
|
|
668
|
+
start = offset;
|
|
669
|
+
matched = 1;
|
|
670
|
+
} else if (matched < SVG_TAG.length && byte === SVG_TAG[matched]) matched++;
|
|
671
|
+
else matched = 0;
|
|
672
|
+
offset++;
|
|
673
|
+
}
|
|
674
|
+
let quote = 0;
|
|
675
|
+
for (offset++; offset < length; offset++) {
|
|
676
|
+
const byte = input[offset];
|
|
677
|
+
if (byte === TAG_START && isCommentAt(input, offset)) {
|
|
678
|
+
offset = skipComment(input, offset) - 1;
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
if (quote) {
|
|
682
|
+
if (byte === quote) quote = 0;
|
|
683
|
+
} else if (byte === DOUBLE_QUOTE || byte === SINGLE_QUOTE) quote = byte;
|
|
684
|
+
else if (byte === TAG_END) break;
|
|
685
|
+
}
|
|
686
|
+
if (offset >= length) return;
|
|
687
|
+
try {
|
|
688
|
+
return toUTF8String(input, start, offset + 1).replace(commentReg, "");
|
|
689
|
+
} catch {
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
const INCH_CM = 2.54;
|
|
694
|
+
const units = {
|
|
695
|
+
in: 96,
|
|
696
|
+
cm: 96 / INCH_CM,
|
|
697
|
+
em: 16,
|
|
698
|
+
ex: 8,
|
|
699
|
+
m: 96 / INCH_CM * 100,
|
|
700
|
+
mm: 96 / INCH_CM / 10,
|
|
701
|
+
pc: 16,
|
|
702
|
+
pt: 96 / 72,
|
|
703
|
+
px: 1
|
|
704
|
+
};
|
|
705
|
+
const unitsReg = new RegExp(`^([0-9.]+(?:e\\d+)?)(${Object.keys(units).join("|")})?$`);
|
|
706
|
+
function parseLength(len) {
|
|
707
|
+
const m = unitsReg.exec(len);
|
|
708
|
+
if (!m) return;
|
|
709
|
+
return Number(m[1]) * (units[m[2]] || 1);
|
|
710
|
+
}
|
|
711
|
+
function parseViewbox(viewbox) {
|
|
712
|
+
const bounds = viewbox.trim().split(/[\s,]+/, 4);
|
|
713
|
+
return {
|
|
714
|
+
height: parseLength(bounds[3]),
|
|
715
|
+
width: parseLength(bounds[2])
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
function parseAttributes(root) {
|
|
719
|
+
const width = root.match(extractorRegExps.width);
|
|
720
|
+
const height = root.match(extractorRegExps.height);
|
|
721
|
+
const viewbox = root.match(extractorRegExps.viewbox);
|
|
722
|
+
return {
|
|
723
|
+
height: height && Math.round(parseLength(height[2])),
|
|
724
|
+
viewbox: viewbox && parseViewbox(viewbox[2]),
|
|
725
|
+
width: width && Math.round(parseLength(width[2]))
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
function calculateByDimensions(attrs) {
|
|
729
|
+
return {
|
|
730
|
+
height: attrs.height,
|
|
731
|
+
width: attrs.width
|
|
732
|
+
};
|
|
733
|
+
}
|
|
734
|
+
function toPixels(length) {
|
|
735
|
+
return Math.max(1, Math.round(length));
|
|
736
|
+
}
|
|
737
|
+
function calculateByViewbox(attrs, viewbox) {
|
|
738
|
+
const vbWidth = viewbox.width;
|
|
739
|
+
const vbHeight = viewbox.height;
|
|
740
|
+
if (attrs.width) return {
|
|
741
|
+
height: toPixels(attrs.width * vbHeight / vbWidth),
|
|
742
|
+
width: attrs.width
|
|
743
|
+
};
|
|
744
|
+
if (attrs.height) return {
|
|
745
|
+
height: attrs.height,
|
|
746
|
+
width: toPixels(attrs.height * vbWidth / vbHeight)
|
|
747
|
+
};
|
|
748
|
+
return {
|
|
749
|
+
height: toPixels(vbHeight),
|
|
750
|
+
width: toPixels(vbWidth)
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
const SVG = {
|
|
754
|
+
validate: (input) => svgReg.test(toUTF8String(input, 0, 1e3)),
|
|
755
|
+
calculate(input) {
|
|
756
|
+
const root = extractRoot(input);
|
|
757
|
+
if (root) {
|
|
758
|
+
const attrs = parseAttributes(root);
|
|
759
|
+
if (attrs.width && attrs.height) return calculateByDimensions(attrs);
|
|
760
|
+
if (attrs.viewbox?.width && attrs.viewbox.height) return calculateByViewbox(attrs, attrs.viewbox);
|
|
761
|
+
}
|
|
762
|
+
throw new TypeError("Invalid SVG");
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
const TGA = {
|
|
766
|
+
validate(input) {
|
|
767
|
+
return readUInt16LE(input, 0) === 0 && readUInt16LE(input, 4) === 0;
|
|
768
|
+
},
|
|
769
|
+
calculate(input) {
|
|
770
|
+
return {
|
|
771
|
+
height: readUInt16LE(input, 14),
|
|
772
|
+
width: readUInt16LE(input, 12)
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
};
|
|
776
|
+
const TAG_WIDTH = 256;
|
|
777
|
+
const TAG_HEIGHT = 257;
|
|
778
|
+
const TYPE_SHORT = 3;
|
|
779
|
+
const TYPE_LONG = 4;
|
|
780
|
+
const TYPE_LONG8 = 16;
|
|
781
|
+
const MAX_ENTRIES = 4096;
|
|
782
|
+
function readUInt64(input, offset, isBigEndian) {
|
|
783
|
+
const high = readUInt(input, 32, offset + (isBigEndian ? 0 : 4), isBigEndian);
|
|
784
|
+
const low = readUInt(input, 32, offset + (isBigEndian ? 4 : 0), isBigEndian);
|
|
785
|
+
return high * 2 ** 32 + low;
|
|
786
|
+
}
|
|
787
|
+
function readValue(input, type, offset, isBigEndian, isBigTiff) {
|
|
788
|
+
switch (type) {
|
|
789
|
+
case TYPE_SHORT: return readUInt(input, 16, offset, isBigEndian);
|
|
790
|
+
case TYPE_LONG: return readUInt(input, 32, offset, isBigEndian);
|
|
791
|
+
case TYPE_LONG8: {
|
|
792
|
+
if (!isBigTiff) return;
|
|
793
|
+
const value = readUInt64(input, offset, isBigEndian);
|
|
794
|
+
if (!Number.isSafeInteger(value)) throw new TypeError("Invalid Tiff. Value too large");
|
|
795
|
+
return value;
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
const signatures = /* @__PURE__ */ new Set([
|
|
800
|
+
"49492a00",
|
|
801
|
+
"4d4d002a",
|
|
802
|
+
"49492b00",
|
|
803
|
+
"4d4d002b"
|
|
804
|
+
]);
|
|
805
|
+
const TIFF = {
|
|
806
|
+
validate: (input) => signatures.has(toHexString(input, 0, 4)),
|
|
807
|
+
calculate(input) {
|
|
808
|
+
const isBigEndian = toUTF8String(input, 0, 2) === "MM";
|
|
809
|
+
const isBigTiff = readUInt(input, 16, 2, isBigEndian) === 43;
|
|
810
|
+
let entryCount;
|
|
811
|
+
let entriesOffset;
|
|
812
|
+
let entrySize;
|
|
813
|
+
if (isBigTiff) {
|
|
814
|
+
const byteSize = readUInt(input, 16, 4, isBigEndian);
|
|
815
|
+
const reserved = readUInt(input, 16, 6, isBigEndian);
|
|
816
|
+
if (byteSize !== 8 || reserved !== 0) throw new TypeError("Invalid BigTIFF header");
|
|
817
|
+
const ifdOffset = readUInt64(input, 8, isBigEndian);
|
|
818
|
+
if (!(ifdOffset + 8 <= input.length)) throw new TypeError("Invalid Tiff. IFD offset out of bounds");
|
|
819
|
+
entryCount = readUInt64(input, ifdOffset, isBigEndian);
|
|
820
|
+
entriesOffset = ifdOffset + 8;
|
|
821
|
+
entrySize = 20;
|
|
822
|
+
} else {
|
|
823
|
+
const ifdOffset = readUInt(input, 32, 4, isBigEndian);
|
|
824
|
+
if (!(ifdOffset + 2 <= input.length)) throw new TypeError("Invalid Tiff. IFD offset out of bounds");
|
|
825
|
+
entryCount = readUInt(input, 16, ifdOffset, isBigEndian);
|
|
826
|
+
entriesOffset = ifdOffset + 2;
|
|
827
|
+
entrySize = 12;
|
|
828
|
+
}
|
|
829
|
+
const tags = {};
|
|
830
|
+
for (let index = 0; index < Math.min(entryCount, MAX_ENTRIES); index++) {
|
|
831
|
+
const offset = entriesOffset + index * entrySize;
|
|
832
|
+
if (offset + entrySize > input.length) break;
|
|
833
|
+
const code = readUInt(input, 16, offset, isBigEndian);
|
|
834
|
+
const type = readUInt(input, 16, offset + 2, isBigEndian);
|
|
835
|
+
if ((isBigTiff ? readUInt64(input, offset + 4, isBigEndian) : readUInt(input, 32, offset + 4, isBigEndian)) === 1) tags[code] = readValue(input, type, offset + (isBigTiff ? 12 : 8), isBigEndian, isBigTiff);
|
|
836
|
+
if (tags[TAG_WIDTH] && tags[TAG_HEIGHT]) break;
|
|
837
|
+
}
|
|
838
|
+
const width = tags[TAG_WIDTH];
|
|
839
|
+
const height = tags[TAG_HEIGHT];
|
|
840
|
+
if (!width || !height) throw new TypeError("Invalid Tiff. Missing tags");
|
|
841
|
+
return {
|
|
842
|
+
height,
|
|
843
|
+
width
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
function calculateExtended(input) {
|
|
848
|
+
return {
|
|
849
|
+
height: 1 + readUInt24LE(input, 7),
|
|
850
|
+
width: 1 + readUInt24LE(input, 4)
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
function calculateLossless(input) {
|
|
854
|
+
return {
|
|
855
|
+
height: 1 + ((input[4] & 15) << 10 | input[3] << 2 | (input[2] & 192) >> 6),
|
|
856
|
+
width: 1 + ((input[2] & 63) << 8 | input[1])
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
function calculateLossy(input) {
|
|
860
|
+
return {
|
|
861
|
+
height: readInt16LE(input, 8) & 16383,
|
|
862
|
+
width: readInt16LE(input, 6) & 16383
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
const typeHandlers = {
|
|
866
|
+
bmp: BMP,
|
|
867
|
+
cur: CUR,
|
|
868
|
+
dds: DDS,
|
|
869
|
+
gif: GIF,
|
|
870
|
+
heic: HEIC,
|
|
871
|
+
icns: ICNS,
|
|
872
|
+
ico: ICO,
|
|
873
|
+
j2c: J2C,
|
|
874
|
+
jp2: JP2,
|
|
875
|
+
jpg: JPG,
|
|
876
|
+
jxl: JXL,
|
|
877
|
+
ktx: KTX,
|
|
878
|
+
png: PNG,
|
|
879
|
+
pnm: PNM,
|
|
880
|
+
psd: PSD,
|
|
881
|
+
svg: SVG,
|
|
882
|
+
tga: TGA,
|
|
883
|
+
tiff: TIFF,
|
|
884
|
+
webp: {
|
|
885
|
+
validate(input) {
|
|
886
|
+
const riffHeader = toUTF8String(input, 0, 4) === "RIFF";
|
|
887
|
+
const webpHeader = toUTF8String(input, 8, 12) === "WEBP";
|
|
888
|
+
const vp8Header = toUTF8String(input, 12, 15) === "VP8";
|
|
889
|
+
return riffHeader && webpHeader && vp8Header;
|
|
890
|
+
},
|
|
891
|
+
calculate(input) {
|
|
892
|
+
if (input.length < 30) throw new TypeError("Invalid WebP, truncated input");
|
|
893
|
+
const chunkHeader = toUTF8String(input, 12, 16);
|
|
894
|
+
input = input.slice(20, 30);
|
|
895
|
+
if (chunkHeader === "VP8X") {
|
|
896
|
+
const extendedHeader = input[0];
|
|
897
|
+
const validStart = (extendedHeader & 192) === 0;
|
|
898
|
+
const validEnd = (extendedHeader & 1) === 0;
|
|
899
|
+
if (validStart && validEnd) return calculateExtended(input);
|
|
900
|
+
else throw new TypeError("Invalid WebP");
|
|
901
|
+
}
|
|
902
|
+
if (chunkHeader === "VP8 " && input[0] !== 47) return calculateLossy(input);
|
|
903
|
+
const signature = toHexString(input, 3, 6);
|
|
904
|
+
if (chunkHeader === "VP8L" && signature !== "9d012a") return calculateLossless(input);
|
|
905
|
+
throw new TypeError("Invalid WebP");
|
|
906
|
+
}
|
|
907
|
+
},
|
|
908
|
+
avif: {
|
|
909
|
+
validate: (input) => detectHeifType(input) === "avif",
|
|
910
|
+
calculate: (input) => HEIC.calculate(input)
|
|
911
|
+
}
|
|
912
|
+
};
|
|
913
|
+
const keys = Object.keys(typeHandlers);
|
|
914
|
+
const firstBytes = {
|
|
915
|
+
56: "psd",
|
|
916
|
+
66: "bmp",
|
|
917
|
+
68: "dds",
|
|
918
|
+
71: "gif",
|
|
919
|
+
73: "tiff",
|
|
920
|
+
77: "tiff",
|
|
921
|
+
82: "webp",
|
|
922
|
+
105: "icns",
|
|
923
|
+
137: "png",
|
|
924
|
+
255: "jpg"
|
|
925
|
+
};
|
|
926
|
+
function detector(input) {
|
|
927
|
+
const byte = input[0];
|
|
928
|
+
if (byte in firstBytes) {
|
|
929
|
+
const type = firstBytes[byte];
|
|
930
|
+
if (type && typeHandlers[type].validate(input)) return type;
|
|
931
|
+
}
|
|
932
|
+
return keys.find((key) => typeHandlers[key].validate(input));
|
|
933
|
+
}
|
|
934
|
+
function imageMeta(input) {
|
|
935
|
+
if (!(input instanceof Uint8Array)) throw new TypeError("Input should be a Uint8Array");
|
|
936
|
+
const type = detector(input);
|
|
937
|
+
if (type !== void 0 && type in typeHandlers) {
|
|
938
|
+
const size = typeHandlers[type].calculate(input);
|
|
939
|
+
if (size !== void 0) {
|
|
940
|
+
if (size.images && size.images.length > 1) {
|
|
941
|
+
for (const image of size.images) if (image.width * image.height > size.width * size.height) {
|
|
942
|
+
size.width = image.width;
|
|
943
|
+
size.height = image.height;
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
if (!Number.isSafeInteger(size.width) || !Number.isSafeInteger(size.height)) throw new TypeError(`Invalid ${type}, truncated or corrupt input`);
|
|
947
|
+
if (size.width <= 0 || size.height < 0) throw new TypeError(`Invalid ${type}, zero or negative size`);
|
|
948
|
+
size.type = type;
|
|
949
|
+
return size;
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
throw new TypeError(`Unsupported file type: ${type}`);
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
function VArg(argument) {
|
|
956
|
+
return destr(argument);
|
|
957
|
+
}
|
|
958
|
+
function parseArgs(arguments_, mappers) {
|
|
959
|
+
const vargs = arguments_.split("_");
|
|
960
|
+
return mappers.map((v, index) => v(vargs[index]));
|
|
961
|
+
}
|
|
962
|
+
function getHandler(key) {
|
|
963
|
+
return Handlers[key];
|
|
964
|
+
}
|
|
965
|
+
function applyHandler(context, pipe, handler, argumentsString) {
|
|
966
|
+
const arguments_ = handler.args ? parseArgs(argumentsString, handler.args) : [];
|
|
967
|
+
return handler.apply(context, pipe, ...arguments_);
|
|
968
|
+
}
|
|
969
|
+
function clampDimensionsPreservingAspectRatio(sourceDimensions, desiredDimensions) {
|
|
970
|
+
const desiredAspectRatio = desiredDimensions.width / desiredDimensions.height;
|
|
971
|
+
let { width, height } = desiredDimensions;
|
|
972
|
+
if (sourceDimensions.width && width > sourceDimensions.width) {
|
|
973
|
+
width = sourceDimensions.width;
|
|
974
|
+
height = Math.round(sourceDimensions.width / desiredAspectRatio);
|
|
975
|
+
}
|
|
976
|
+
if (sourceDimensions.height && height > sourceDimensions.height) {
|
|
977
|
+
height = sourceDimensions.height;
|
|
978
|
+
width = Math.round(sourceDimensions.height * desiredAspectRatio);
|
|
979
|
+
}
|
|
980
|
+
return { width, height };
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
const quality = {
|
|
984
|
+
args: [VArg],
|
|
985
|
+
order: -1,
|
|
986
|
+
apply: (context, _pipe, quality2) => {
|
|
987
|
+
context.quality = quality2;
|
|
988
|
+
}
|
|
989
|
+
};
|
|
990
|
+
const fit = {
|
|
991
|
+
args: [VArg],
|
|
992
|
+
order: -1,
|
|
993
|
+
apply: (context, _pipe, fit2) => {
|
|
994
|
+
context.fit = fit2;
|
|
995
|
+
}
|
|
996
|
+
};
|
|
997
|
+
const position = {
|
|
998
|
+
args: [VArg],
|
|
999
|
+
order: -1,
|
|
1000
|
+
apply: (context, _pipe, position2) => {
|
|
1001
|
+
context.position = position2;
|
|
1002
|
+
}
|
|
1003
|
+
};
|
|
1004
|
+
const HEX_RE = /^([\da-f]{2})([\da-f]{2})([\da-f]{2})$/i;
|
|
1005
|
+
const SHORTHEX_RE = /^([\da-f])([\da-f])([\da-f])$/i;
|
|
1006
|
+
const background = {
|
|
1007
|
+
args: [VArg],
|
|
1008
|
+
order: -1,
|
|
1009
|
+
apply: (context, _pipe, background2) => {
|
|
1010
|
+
background2 = String(background2);
|
|
1011
|
+
if (!background2.startsWith("#") && (HEX_RE.test(background2) || SHORTHEX_RE.test(background2))) {
|
|
1012
|
+
background2 = "#" + background2;
|
|
1013
|
+
}
|
|
1014
|
+
context.background = background2;
|
|
1015
|
+
}
|
|
1016
|
+
};
|
|
1017
|
+
const enlarge = {
|
|
1018
|
+
args: [],
|
|
1019
|
+
apply: (context) => {
|
|
1020
|
+
context.enlarge = true;
|
|
1021
|
+
}
|
|
1022
|
+
};
|
|
1023
|
+
const kernel = {
|
|
1024
|
+
args: [VArg],
|
|
1025
|
+
apply: (context, _pipe, kernel2) => {
|
|
1026
|
+
context.kernel = kernel2;
|
|
1027
|
+
}
|
|
1028
|
+
};
|
|
1029
|
+
const width = {
|
|
1030
|
+
args: [VArg],
|
|
1031
|
+
apply: (context, pipe, width2) => {
|
|
1032
|
+
return pipe.resize(width2, void 0, {
|
|
1033
|
+
withoutEnlargement: !context.enlarge
|
|
1034
|
+
});
|
|
1035
|
+
}
|
|
1036
|
+
};
|
|
1037
|
+
const height = {
|
|
1038
|
+
args: [VArg],
|
|
1039
|
+
apply: (context, pipe, height2) => {
|
|
1040
|
+
return pipe.resize(void 0, height2, {
|
|
1041
|
+
withoutEnlargement: !context.enlarge
|
|
1042
|
+
});
|
|
1043
|
+
}
|
|
1044
|
+
};
|
|
1045
|
+
const resize = {
|
|
1046
|
+
args: [VArg, VArg, VArg],
|
|
1047
|
+
apply: (context, pipe, size) => {
|
|
1048
|
+
let [width2, height2] = String(size).split("x").map(Number);
|
|
1049
|
+
if (!width2) {
|
|
1050
|
+
return;
|
|
1051
|
+
}
|
|
1052
|
+
if (!height2) {
|
|
1053
|
+
height2 = width2;
|
|
1054
|
+
}
|
|
1055
|
+
if (!context.enlarge) {
|
|
1056
|
+
const clamped = clampDimensionsPreservingAspectRatio(context.meta, {
|
|
1057
|
+
width: width2,
|
|
1058
|
+
height: height2
|
|
1059
|
+
});
|
|
1060
|
+
width2 = clamped.width;
|
|
1061
|
+
height2 = clamped.height;
|
|
1062
|
+
}
|
|
1063
|
+
return pipe.resize(width2, height2, {
|
|
1064
|
+
fit: context.fit,
|
|
1065
|
+
position: context.position,
|
|
1066
|
+
background: context.background,
|
|
1067
|
+
kernel: context.kernel
|
|
1068
|
+
});
|
|
1069
|
+
}
|
|
1070
|
+
};
|
|
1071
|
+
const trim = {
|
|
1072
|
+
args: [VArg],
|
|
1073
|
+
apply: (_context, pipe, threshold2) => {
|
|
1074
|
+
return pipe.trim(threshold2);
|
|
1075
|
+
}
|
|
1076
|
+
};
|
|
1077
|
+
const extend = {
|
|
1078
|
+
args: [VArg, VArg, VArg, VArg],
|
|
1079
|
+
apply: (context, pipe, top, right, bottom, left) => {
|
|
1080
|
+
return pipe.extend({
|
|
1081
|
+
top,
|
|
1082
|
+
left,
|
|
1083
|
+
bottom,
|
|
1084
|
+
right,
|
|
1085
|
+
background: context.background
|
|
1086
|
+
});
|
|
1087
|
+
}
|
|
1088
|
+
};
|
|
1089
|
+
const extract = {
|
|
1090
|
+
args: [VArg, VArg, VArg, VArg],
|
|
1091
|
+
apply: (_context, pipe, left, top, width2, height2) => {
|
|
1092
|
+
return pipe.extract({
|
|
1093
|
+
left,
|
|
1094
|
+
top,
|
|
1095
|
+
width: width2,
|
|
1096
|
+
height: height2
|
|
1097
|
+
});
|
|
1098
|
+
}
|
|
1099
|
+
};
|
|
1100
|
+
const rotate = {
|
|
1101
|
+
args: [VArg],
|
|
1102
|
+
apply: (context, pipe, angel) => {
|
|
1103
|
+
return pipe.rotate(angel, {
|
|
1104
|
+
background: context.background
|
|
1105
|
+
});
|
|
1106
|
+
}
|
|
1107
|
+
};
|
|
1108
|
+
const flip = {
|
|
1109
|
+
args: [],
|
|
1110
|
+
apply: (_context, pipe) => {
|
|
1111
|
+
return pipe.flip();
|
|
1112
|
+
}
|
|
1113
|
+
};
|
|
1114
|
+
const flop = {
|
|
1115
|
+
args: [],
|
|
1116
|
+
apply: (_context, pipe) => {
|
|
1117
|
+
return pipe.flop();
|
|
1118
|
+
}
|
|
1119
|
+
};
|
|
1120
|
+
const sharpen = {
|
|
1121
|
+
args: [VArg, VArg, VArg],
|
|
1122
|
+
apply: (_context, pipe, sigma, flat, jagged) => {
|
|
1123
|
+
return pipe.sharpen(sigma, flat, jagged);
|
|
1124
|
+
}
|
|
1125
|
+
};
|
|
1126
|
+
const median = {
|
|
1127
|
+
args: [VArg, VArg, VArg],
|
|
1128
|
+
apply: (_context, pipe, size) => {
|
|
1129
|
+
return pipe.median(size);
|
|
1130
|
+
}
|
|
1131
|
+
};
|
|
1132
|
+
const blur = {
|
|
1133
|
+
args: [VArg, VArg, VArg],
|
|
1134
|
+
apply: (_context, pipe, sigma) => {
|
|
1135
|
+
return pipe.blur(sigma);
|
|
1136
|
+
}
|
|
1137
|
+
};
|
|
1138
|
+
const flatten = {
|
|
1139
|
+
args: [VArg, VArg, VArg],
|
|
1140
|
+
apply: (context, pipe) => {
|
|
1141
|
+
return pipe.flatten({
|
|
1142
|
+
background: context.background
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
};
|
|
1146
|
+
const gamma = {
|
|
1147
|
+
args: [VArg, VArg, VArg],
|
|
1148
|
+
apply: (_context, pipe, gamma2, gammaOut) => {
|
|
1149
|
+
return pipe.gamma(gamma2, gammaOut);
|
|
1150
|
+
}
|
|
1151
|
+
};
|
|
1152
|
+
const negate = {
|
|
1153
|
+
args: [VArg, VArg, VArg],
|
|
1154
|
+
apply: (_context, pipe) => {
|
|
1155
|
+
return pipe.negate();
|
|
1156
|
+
}
|
|
1157
|
+
};
|
|
1158
|
+
const normalize = {
|
|
1159
|
+
args: [VArg, VArg, VArg],
|
|
1160
|
+
apply: (_context, pipe) => {
|
|
1161
|
+
return pipe.normalize();
|
|
1162
|
+
}
|
|
1163
|
+
};
|
|
1164
|
+
const threshold = {
|
|
1165
|
+
args: [VArg],
|
|
1166
|
+
apply: (_context, pipe, threshold2) => {
|
|
1167
|
+
return pipe.threshold(threshold2);
|
|
1168
|
+
}
|
|
1169
|
+
};
|
|
1170
|
+
const modulate = {
|
|
1171
|
+
args: [VArg],
|
|
1172
|
+
apply: (_context, pipe, brightness, saturation, hue) => {
|
|
1173
|
+
return pipe.modulate({
|
|
1174
|
+
brightness,
|
|
1175
|
+
saturation,
|
|
1176
|
+
hue
|
|
1177
|
+
});
|
|
1178
|
+
}
|
|
1179
|
+
};
|
|
1180
|
+
const tint = {
|
|
1181
|
+
args: [VArg],
|
|
1182
|
+
apply: (_context, pipe, rgb) => {
|
|
1183
|
+
return pipe.tint(rgb);
|
|
1184
|
+
}
|
|
1185
|
+
};
|
|
1186
|
+
const grayscale = {
|
|
1187
|
+
args: [VArg],
|
|
1188
|
+
apply: (_context, pipe) => {
|
|
1189
|
+
return pipe.grayscale();
|
|
1190
|
+
}
|
|
1191
|
+
};
|
|
1192
|
+
const crop = extract;
|
|
1193
|
+
const q = quality;
|
|
1194
|
+
const b = background;
|
|
1195
|
+
const w = width;
|
|
1196
|
+
const h = height;
|
|
1197
|
+
const s = resize;
|
|
1198
|
+
const pos = position;
|
|
1199
|
+
|
|
1200
|
+
function getEnv(name) {
|
|
1201
|
+
return name in process.env ? destr(process.env[name]) : void 0;
|
|
1202
|
+
}
|
|
1203
|
+
function cachedPromise(function_) {
|
|
1204
|
+
let p;
|
|
1205
|
+
return (...arguments_) => {
|
|
1206
|
+
if (p) {
|
|
1207
|
+
return p;
|
|
1208
|
+
}
|
|
1209
|
+
p = Promise.resolve(function_(...arguments_));
|
|
1210
|
+
return p;
|
|
1211
|
+
};
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
const SUPPORTED_FORMATS = /* @__PURE__ */ new Set([
|
|
1215
|
+
"jpeg",
|
|
1216
|
+
"png",
|
|
1217
|
+
"webp",
|
|
1218
|
+
"avif",
|
|
1219
|
+
"tiff",
|
|
1220
|
+
"heif",
|
|
1221
|
+
"gif",
|
|
1222
|
+
"heic"
|
|
1223
|
+
]);
|
|
1224
|
+
function createIPX(userOptions) {
|
|
1225
|
+
const options = defu(userOptions, {
|
|
1226
|
+
alias: getEnv("IPX_ALIAS") || {},
|
|
1227
|
+
maxAge: getEnv("IPX_MAX_AGE") ?? 60,
|
|
1228
|
+
sharpOptions: {
|
|
1229
|
+
jpegProgressive: true
|
|
1230
|
+
}
|
|
1231
|
+
});
|
|
1232
|
+
options.alias = Object.fromEntries(
|
|
1233
|
+
Object.entries(options.alias || {}).map((e) => [
|
|
1234
|
+
withLeadingSlash(e[0]),
|
|
1235
|
+
e[1]
|
|
1236
|
+
])
|
|
1237
|
+
);
|
|
1238
|
+
const getSharp = cachedPromise(async () => {
|
|
1239
|
+
return await import('sharp').then(
|
|
1240
|
+
(r) => r.default || r
|
|
1241
|
+
);
|
|
1242
|
+
});
|
|
1243
|
+
const getSVGO = cachedPromise(async () => {
|
|
1244
|
+
const { optimize } = await import('svgo');
|
|
1245
|
+
return { optimize };
|
|
1246
|
+
});
|
|
1247
|
+
return function ipx(id, modifiers = {}, opts = {}) {
|
|
1248
|
+
if (!id) {
|
|
1249
|
+
throw createError({
|
|
1250
|
+
statusCode: 400,
|
|
1251
|
+
statusText: `IPX_MISSING_ID`,
|
|
1252
|
+
message: `Resource id is missing`
|
|
1253
|
+
});
|
|
1254
|
+
}
|
|
1255
|
+
id = hasProtocol(id) ? id : withLeadingSlash(id);
|
|
1256
|
+
for (const base in options.alias) {
|
|
1257
|
+
if (id.startsWith(base)) {
|
|
1258
|
+
id = joinURL(options.alias[base], id.slice(base.length));
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
const storage = hasProtocol(id) ? options.httpStorage || options.storage : options.storage || options.httpStorage;
|
|
1262
|
+
if (!storage) {
|
|
1263
|
+
throw createError({
|
|
1264
|
+
statusCode: 500,
|
|
1265
|
+
statusText: `IPX_NO_STORAGE`,
|
|
1266
|
+
message: "No storage configured!"
|
|
1267
|
+
});
|
|
1268
|
+
}
|
|
1269
|
+
const getSourceMeta = cachedPromise(async () => {
|
|
1270
|
+
const sourceMeta = await storage.getMeta(id, opts);
|
|
1271
|
+
if (!sourceMeta) {
|
|
1272
|
+
throw createError({
|
|
1273
|
+
statusCode: 404,
|
|
1274
|
+
statusText: `IPX_RESOURCE_NOT_FOUND`,
|
|
1275
|
+
message: `Resource not found: ${id}`
|
|
1276
|
+
});
|
|
1277
|
+
}
|
|
1278
|
+
const _maxAge = sourceMeta.maxAge ?? options.maxAge;
|
|
1279
|
+
return {
|
|
1280
|
+
maxAge: typeof _maxAge === "string" ? Number.parseInt(_maxAge) : _maxAge,
|
|
1281
|
+
mtime: sourceMeta.mtime ? new Date(sourceMeta.mtime) : void 0
|
|
1282
|
+
};
|
|
1283
|
+
});
|
|
1284
|
+
const getSourceData = cachedPromise(async () => {
|
|
1285
|
+
const sourceData = await storage.getData(id, opts);
|
|
1286
|
+
if (!sourceData) {
|
|
1287
|
+
throw createError({
|
|
1288
|
+
statusCode: 404,
|
|
1289
|
+
statusText: `IPX_RESOURCE_NOT_FOUND`,
|
|
1290
|
+
message: `Resource not found: ${id}`
|
|
1291
|
+
});
|
|
1292
|
+
}
|
|
1293
|
+
return Buffer.from(sourceData);
|
|
1294
|
+
});
|
|
1295
|
+
const process = cachedPromise(async () => {
|
|
1296
|
+
const sourceData = await getSourceData();
|
|
1297
|
+
let imageMeta$1;
|
|
1298
|
+
try {
|
|
1299
|
+
imageMeta$1 = imageMeta(sourceData);
|
|
1300
|
+
} catch {
|
|
1301
|
+
throw createError({
|
|
1302
|
+
statusCode: 400,
|
|
1303
|
+
statusText: `IPX_INVALID_IMAGE`,
|
|
1304
|
+
message: `Cannot parse image metadata: ${id}`
|
|
1305
|
+
});
|
|
1306
|
+
}
|
|
1307
|
+
let mFormat = modifiers.f || modifiers.format;
|
|
1308
|
+
if (mFormat === "jpg") {
|
|
1309
|
+
mFormat = "jpeg";
|
|
1310
|
+
}
|
|
1311
|
+
const format = mFormat && SUPPORTED_FORMATS.has(mFormat) ? mFormat : SUPPORTED_FORMATS.has(imageMeta$1.type || "") ? imageMeta$1.type : "jpeg";
|
|
1312
|
+
if (imageMeta$1.type === "svg" && !mFormat) {
|
|
1313
|
+
if (options.svgo === false) {
|
|
1314
|
+
return {
|
|
1315
|
+
data: sourceData,
|
|
1316
|
+
format: "svg+xml",
|
|
1317
|
+
meta: imageMeta$1
|
|
1318
|
+
};
|
|
1319
|
+
} else {
|
|
1320
|
+
const { optimize } = await getSVGO();
|
|
1321
|
+
const svg = optimize(sourceData.toString("utf8"), {
|
|
1322
|
+
...options.svgo,
|
|
1323
|
+
plugins: ["removeScripts", ...options.svgo?.plugins || []]
|
|
1324
|
+
}).data;
|
|
1325
|
+
return {
|
|
1326
|
+
data: svg,
|
|
1327
|
+
format: "svg+xml",
|
|
1328
|
+
meta: imageMeta$1
|
|
1329
|
+
};
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
const animated = modifiers.animated !== void 0 || modifiers.a !== void 0 || format === "gif";
|
|
1333
|
+
const Sharp = await getSharp();
|
|
1334
|
+
let sharp = Sharp(sourceData, { animated, ...options.sharpOptions });
|
|
1335
|
+
Object.assign(
|
|
1336
|
+
sharp.options,
|
|
1337
|
+
options.sharpOptions
|
|
1338
|
+
);
|
|
1339
|
+
const handlers = Object.entries(modifiers).map(([name, arguments_]) => ({
|
|
1340
|
+
handler: getHandler(name),
|
|
1341
|
+
name,
|
|
1342
|
+
args: arguments_
|
|
1343
|
+
})).filter((h) => h.handler).sort((a, b) => {
|
|
1344
|
+
const aKey = (a.handler.order || a.name || "").toString();
|
|
1345
|
+
const bKey = (b.handler.order || b.name || "").toString();
|
|
1346
|
+
return aKey.localeCompare(bKey);
|
|
1347
|
+
});
|
|
1348
|
+
const handlerContext = { meta: imageMeta$1 };
|
|
1349
|
+
for (const h of handlers) {
|
|
1350
|
+
sharp = applyHandler(handlerContext, sharp, h.handler, h.args) || sharp;
|
|
1351
|
+
}
|
|
1352
|
+
if (SUPPORTED_FORMATS.has(format || "")) {
|
|
1353
|
+
sharp = sharp.toFormat(format, {
|
|
1354
|
+
quality: handlerContext.quality
|
|
1355
|
+
});
|
|
1356
|
+
}
|
|
1357
|
+
const processedImage = await sharp.toBuffer();
|
|
1358
|
+
return {
|
|
1359
|
+
data: processedImage,
|
|
1360
|
+
format,
|
|
1361
|
+
meta: imageMeta$1
|
|
1362
|
+
};
|
|
1363
|
+
});
|
|
1364
|
+
return {
|
|
1365
|
+
getSourceMeta,
|
|
1366
|
+
process
|
|
1367
|
+
};
|
|
1368
|
+
};
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
const MODIFIER_SEP = /[&,]/g;
|
|
1372
|
+
const MODIFIER_VAL_SEP = /[:=_]/;
|
|
1373
|
+
function createIPXH3Handler(ipx) {
|
|
1374
|
+
const _handler = async (event) => {
|
|
1375
|
+
const [modifiersString = "", ...idSegments] = event.path.slice(
|
|
1376
|
+
1
|
|
1377
|
+
/* leading slash */
|
|
1378
|
+
).split("/");
|
|
1379
|
+
const id = safeString(decode(idSegments.join("/")));
|
|
1380
|
+
if (!modifiersString) {
|
|
1381
|
+
throw createError({
|
|
1382
|
+
statusCode: 400,
|
|
1383
|
+
statusText: `IPX_MISSING_MODIFIERS`,
|
|
1384
|
+
message: `Modifiers are missing: ${id}`
|
|
1385
|
+
});
|
|
1386
|
+
}
|
|
1387
|
+
if (!id || id === "/") {
|
|
1388
|
+
throw createError({
|
|
1389
|
+
statusCode: 400,
|
|
1390
|
+
statusText: `IPX_MISSING_ID`,
|
|
1391
|
+
message: `Resource id is missing: ${event.path}`
|
|
1392
|
+
});
|
|
1393
|
+
}
|
|
1394
|
+
const modifiers = /* @__PURE__ */ Object.create(null);
|
|
1395
|
+
if (modifiersString !== "_") {
|
|
1396
|
+
for (const p of modifiersString.split(MODIFIER_SEP)) {
|
|
1397
|
+
const [key, ...values] = p.split(MODIFIER_VAL_SEP);
|
|
1398
|
+
modifiers[safeString(key)] = values.map((v) => safeString(decode(v))).join("_");
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
const mFormat = modifiers.f || modifiers.format;
|
|
1402
|
+
if (mFormat === "auto") {
|
|
1403
|
+
const acceptHeader = getRequestHeader(event, "accept") || "";
|
|
1404
|
+
const animated = modifiers.animated ?? modifiers.a;
|
|
1405
|
+
const autoFormat = autoDetectFormat(
|
|
1406
|
+
acceptHeader,
|
|
1407
|
+
// #234 "animated" param adds {animated: ''} to the modifiers
|
|
1408
|
+
// TODO: fix modifiers to normalized to boolean
|
|
1409
|
+
!!animated || animated === ""
|
|
1410
|
+
);
|
|
1411
|
+
delete modifiers.f;
|
|
1412
|
+
delete modifiers.format;
|
|
1413
|
+
if (autoFormat) {
|
|
1414
|
+
modifiers.format = autoFormat;
|
|
1415
|
+
appendResponseHeader(event, "vary", "Accept");
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
const img = ipx(id, modifiers);
|
|
1419
|
+
const sourceMeta = await img.getSourceMeta();
|
|
1420
|
+
sendResponseHeaderIfNotSet(
|
|
1421
|
+
event,
|
|
1422
|
+
"content-security-policy",
|
|
1423
|
+
"default-src 'none'"
|
|
1424
|
+
);
|
|
1425
|
+
if (sourceMeta.mtime) {
|
|
1426
|
+
sendResponseHeaderIfNotSet(
|
|
1427
|
+
event,
|
|
1428
|
+
"last-modified",
|
|
1429
|
+
sourceMeta.mtime.toUTCString()
|
|
1430
|
+
);
|
|
1431
|
+
const _ifModifiedSince = getRequestHeader(event, "if-modified-since");
|
|
1432
|
+
if (_ifModifiedSince && new Date(_ifModifiedSince) >= sourceMeta.mtime) {
|
|
1433
|
+
setResponseStatus(event, 304);
|
|
1434
|
+
return send(event);
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
const { data, format } = await img.process();
|
|
1438
|
+
if (typeof sourceMeta.maxAge === "number") {
|
|
1439
|
+
sendResponseHeaderIfNotSet(
|
|
1440
|
+
event,
|
|
1441
|
+
"cache-control",
|
|
1442
|
+
`max-age=${+sourceMeta.maxAge}, public, s-maxage=${+sourceMeta.maxAge}`
|
|
1443
|
+
);
|
|
1444
|
+
}
|
|
1445
|
+
const etag = getEtag(data);
|
|
1446
|
+
sendResponseHeaderIfNotSet(event, "etag", etag);
|
|
1447
|
+
if (etag && getRequestHeader(event, "if-none-match") === etag) {
|
|
1448
|
+
setResponseStatus(event, 304);
|
|
1449
|
+
return send(event);
|
|
1450
|
+
}
|
|
1451
|
+
if (format) {
|
|
1452
|
+
sendResponseHeaderIfNotSet(event, "content-type", `image/${format}`);
|
|
1453
|
+
}
|
|
1454
|
+
return data;
|
|
1455
|
+
};
|
|
1456
|
+
return defineEventHandler(async (event) => {
|
|
1457
|
+
try {
|
|
1458
|
+
return await _handler(event);
|
|
1459
|
+
} catch (_error) {
|
|
1460
|
+
const error = createError(_error);
|
|
1461
|
+
setResponseStatus(event, error.statusCode, error.statusMessage);
|
|
1462
|
+
return {
|
|
1463
|
+
error: {
|
|
1464
|
+
message: `[${error.statusCode}] [${error.statusMessage || "IPX_ERROR"}] ${error.message}`
|
|
1465
|
+
}
|
|
1466
|
+
};
|
|
1467
|
+
}
|
|
1468
|
+
});
|
|
1469
|
+
}
|
|
1470
|
+
function createIPXH3App(ipx) {
|
|
1471
|
+
const app = createApp({ debug: true });
|
|
1472
|
+
app.use(createIPXH3Handler(ipx));
|
|
1473
|
+
return app;
|
|
1474
|
+
}
|
|
1475
|
+
function createIPXWebServer(ipx) {
|
|
1476
|
+
return toWebHandler(createIPXH3App(ipx));
|
|
1477
|
+
}
|
|
1478
|
+
function createIPXNodeServer(ipx) {
|
|
1479
|
+
return toNodeListener(createIPXH3App(ipx));
|
|
1480
|
+
}
|
|
1481
|
+
function createIPXPlainServer(ipx) {
|
|
1482
|
+
return toPlainHandler(createIPXH3App(ipx));
|
|
1483
|
+
}
|
|
1484
|
+
function sendResponseHeaderIfNotSet(event, name, value) {
|
|
1485
|
+
if (!getResponseHeader(event, name)) {
|
|
1486
|
+
setResponseHeader(event, name, value);
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
function autoDetectFormat(acceptHeader, animated) {
|
|
1490
|
+
if (animated) {
|
|
1491
|
+
const acceptMime2 = negotiate(acceptHeader, ["image/webp", "image/gif"]);
|
|
1492
|
+
return acceptMime2?.split("/")[1] || "gif";
|
|
1493
|
+
}
|
|
1494
|
+
const acceptMime = negotiate(acceptHeader, [
|
|
1495
|
+
"image/avif",
|
|
1496
|
+
"image/webp",
|
|
1497
|
+
"image/jpeg",
|
|
1498
|
+
"image/png",
|
|
1499
|
+
"image/tiff",
|
|
1500
|
+
"image/heif",
|
|
1501
|
+
"image/gif"
|
|
1502
|
+
]);
|
|
1503
|
+
return acceptMime?.split("/")[1] || "jpeg";
|
|
1504
|
+
}
|
|
1505
|
+
function safeString(input) {
|
|
1506
|
+
return JSON.stringify(input).replace(/^"|"$/g, "").replace(/\\+/g, "\\").replace(/\\"/g, '"');
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
function ipxFSStorage(_options = {}) {
|
|
1510
|
+
const dirs = resolveDirs(_options.dir);
|
|
1511
|
+
const maxAge = _options.maxAge || getEnv("IPX_FS_MAX_AGE");
|
|
1512
|
+
const _getFS = cachedPromise(
|
|
1513
|
+
() => import('node:fs/promises').catch(() => {
|
|
1514
|
+
throw createError({
|
|
1515
|
+
statusCode: 500,
|
|
1516
|
+
statusText: `IPX_FILESYSTEM_ERROR`,
|
|
1517
|
+
message: `Failed to resolve filesystem module`
|
|
1518
|
+
});
|
|
1519
|
+
})
|
|
1520
|
+
);
|
|
1521
|
+
const resolveFile = async (id) => {
|
|
1522
|
+
const fs = await _getFS();
|
|
1523
|
+
for (const dir of dirs) {
|
|
1524
|
+
const filePath = join(dir, id);
|
|
1525
|
+
if (!isValidPath(filePath) || !filePath.startsWith(dir + "/")) {
|
|
1526
|
+
throw createError({
|
|
1527
|
+
statusCode: 403,
|
|
1528
|
+
statusText: `IPX_FORBIDDEN_PATH`,
|
|
1529
|
+
message: `Forbidden path: ${id}`
|
|
1530
|
+
});
|
|
1531
|
+
}
|
|
1532
|
+
try {
|
|
1533
|
+
const stats = await fs.stat(filePath);
|
|
1534
|
+
if (!stats.isFile()) {
|
|
1535
|
+
continue;
|
|
1536
|
+
}
|
|
1537
|
+
return {
|
|
1538
|
+
stats,
|
|
1539
|
+
read: () => fs.readFile(filePath)
|
|
1540
|
+
};
|
|
1541
|
+
} catch (error) {
|
|
1542
|
+
if (error.code === "ENOENT") {
|
|
1543
|
+
continue;
|
|
1544
|
+
}
|
|
1545
|
+
throw createError({
|
|
1546
|
+
statusCode: 403,
|
|
1547
|
+
statusText: `IPX_FORBIDDEN_FILE`,
|
|
1548
|
+
message: `Cannot access file: ${id}`
|
|
1549
|
+
});
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
throw createError({
|
|
1553
|
+
statusCode: 404,
|
|
1554
|
+
statusText: `IPX_FILE_NOT_FOUND`,
|
|
1555
|
+
message: `File not found: ${id}`
|
|
1556
|
+
});
|
|
1557
|
+
};
|
|
1558
|
+
return {
|
|
1559
|
+
name: "ipx:node-fs",
|
|
1560
|
+
async getMeta(id) {
|
|
1561
|
+
const { stats } = await resolveFile(id);
|
|
1562
|
+
return {
|
|
1563
|
+
mtime: stats.mtime,
|
|
1564
|
+
maxAge
|
|
1565
|
+
};
|
|
1566
|
+
},
|
|
1567
|
+
async getData(id) {
|
|
1568
|
+
const { read } = await resolveFile(id);
|
|
1569
|
+
return read();
|
|
1570
|
+
}
|
|
1571
|
+
};
|
|
1572
|
+
}
|
|
1573
|
+
const isWindows = process.platform === "win32";
|
|
1574
|
+
function isValidPath(fp) {
|
|
1575
|
+
if (isWindows) {
|
|
1576
|
+
fp = fp.slice(parse(fp).root.length);
|
|
1577
|
+
}
|
|
1578
|
+
if (/["*:<>?|]/.test(fp)) {
|
|
1579
|
+
return false;
|
|
1580
|
+
}
|
|
1581
|
+
return true;
|
|
1582
|
+
}
|
|
1583
|
+
function resolveDirs(dirs) {
|
|
1584
|
+
if (!dirs || !Array.isArray(dirs)) {
|
|
1585
|
+
const dir = resolve(dirs || getEnv("IPX_FS_DIR") || ".");
|
|
1586
|
+
return [dir];
|
|
1587
|
+
}
|
|
1588
|
+
return dirs.map((dirs2) => resolve(dirs2));
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
const HTTP_RE = /^https?:\/\//;
|
|
1592
|
+
function ipxHttpStorage(_options = {}) {
|
|
1593
|
+
const allowAllDomains = _options.allowAllDomains ?? getEnv("IPX_HTTP_ALLOW_ALL_DOMAINS") ?? false;
|
|
1594
|
+
let _domains = _options.domains || getEnv("IPX_HTTP_DOMAINS") || [];
|
|
1595
|
+
const defaultMaxAge = _options.maxAge || getEnv("IPX_HTTP_MAX_AGE") || 300;
|
|
1596
|
+
const fetchOptions = _options.fetchOptions || getEnv("IPX_HTTP_FETCH_OPTIONS") || {};
|
|
1597
|
+
if (typeof _domains === "string") {
|
|
1598
|
+
_domains = _domains.split(",").map((s) => s.trim());
|
|
1599
|
+
}
|
|
1600
|
+
const domains = new Set(
|
|
1601
|
+
_domains.map((d) => {
|
|
1602
|
+
if (!HTTP_RE.test(d)) {
|
|
1603
|
+
d = "http://" + d;
|
|
1604
|
+
}
|
|
1605
|
+
return new URL(d).hostname;
|
|
1606
|
+
}).filter(Boolean)
|
|
1607
|
+
);
|
|
1608
|
+
function validateId(id) {
|
|
1609
|
+
const url = new URL(decodeURIComponent(id));
|
|
1610
|
+
if (!url.hostname) {
|
|
1611
|
+
throw createError({
|
|
1612
|
+
statusCode: 403,
|
|
1613
|
+
statusText: `IPX_MISSING_HOSTNAME`,
|
|
1614
|
+
message: `Hostname is missing: ${id}`
|
|
1615
|
+
});
|
|
1616
|
+
}
|
|
1617
|
+
if (!allowAllDomains && !domains.has(url.hostname)) {
|
|
1618
|
+
throw createError({
|
|
1619
|
+
statusCode: 403,
|
|
1620
|
+
statusText: `IPX_FORBIDDEN_HOST`,
|
|
1621
|
+
message: `Forbidden host: ${url.hostname}`
|
|
1622
|
+
});
|
|
1623
|
+
}
|
|
1624
|
+
return url.toString();
|
|
1625
|
+
}
|
|
1626
|
+
function parseResponse(response) {
|
|
1627
|
+
let maxAge = defaultMaxAge;
|
|
1628
|
+
if (_options.ignoreCacheControl !== true) {
|
|
1629
|
+
const _cacheControl = response.headers.get("cache-control");
|
|
1630
|
+
if (_cacheControl) {
|
|
1631
|
+
const m = _cacheControl.match(/max-age=(\d+)/);
|
|
1632
|
+
if (m && m[1]) {
|
|
1633
|
+
maxAge = Number.parseInt(m[1]);
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
let mtime;
|
|
1638
|
+
const _lastModified = response.headers.get("last-modified");
|
|
1639
|
+
if (_lastModified) {
|
|
1640
|
+
mtime = new Date(_lastModified);
|
|
1641
|
+
}
|
|
1642
|
+
return { maxAge, mtime };
|
|
1643
|
+
}
|
|
1644
|
+
return {
|
|
1645
|
+
name: "ipx:http",
|
|
1646
|
+
async getMeta(id) {
|
|
1647
|
+
const url = validateId(id);
|
|
1648
|
+
try {
|
|
1649
|
+
const response = await ofetch.raw(url, {
|
|
1650
|
+
...fetchOptions,
|
|
1651
|
+
method: "HEAD"
|
|
1652
|
+
});
|
|
1653
|
+
const { maxAge, mtime } = parseResponse(response);
|
|
1654
|
+
return { mtime, maxAge };
|
|
1655
|
+
} catch {
|
|
1656
|
+
return {};
|
|
1657
|
+
}
|
|
1658
|
+
},
|
|
1659
|
+
async getData(id) {
|
|
1660
|
+
const url = validateId(id);
|
|
1661
|
+
const response = await ofetch(url, {
|
|
1662
|
+
...fetchOptions,
|
|
1663
|
+
method: "GET",
|
|
1664
|
+
responseType: "arrayBuffer"
|
|
1665
|
+
});
|
|
1666
|
+
return response;
|
|
1667
|
+
}
|
|
1668
|
+
};
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
export { ipxFSStorage as a, createIPXNodeServer as b, createIPX as c, createIPXH3App as d, createIPXH3Handler as e, createIPXPlainServer as f, createIPXWebServer as g, ipxHttpStorage as i };
|