arcadible 1.0.29 → 1.0.31

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.
@@ -0,0 +1,408 @@
1
+ // PNG, read and written with Node's own zlib: any PNG a developer's tools
2
+ // write, read into 8-bit RGBA, scaled, and written back as 8-bit RGBA. It's
3
+ // what `arc pack` and `arc serve` make a build's favicon.ico from, the
4
+ // project's store icon scaled down (./favicon.ts).
5
+ // [import]
6
+ import zlib from "node:zlib";
7
+ // [declare]
8
+ const SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
9
+ // [declare]
10
+ // the color types: how many samples a pixel has, and the depths each allows.
11
+ const GRAY = 0;
12
+ const RGB = 2;
13
+ const PALETTE = 3;
14
+ const GRAY_ALPHA = 4;
15
+ const RGBA = 6;
16
+ const SAMPLES = {
17
+ [GRAY]: 1,
18
+ [GRAY_ALPHA]: 2,
19
+ [PALETTE]: 1,
20
+ [RGB]: 3,
21
+ [RGBA]: 4,
22
+ };
23
+ const DEPTHS = {
24
+ [GRAY]: [1, 2, 4, 8, 16],
25
+ [GRAY_ALPHA]: [8, 16],
26
+ [PALETTE]: [1, 2, 4, 8],
27
+ [RGB]: [8, 16],
28
+ [RGBA]: [8, 16],
29
+ };
30
+ // [declare]
31
+ // the largest image read, in pixels a side: a store icon is at most 2048,
32
+ // and arc serve reads one that wasn't validated.
33
+ const SIDE_MAX = 8192;
34
+ // [declare]
35
+ // Adam7's seven passes: where each starts, and its step, across and down.
36
+ const PASSES = [
37
+ [0, 0, 8, 8],
38
+ [4, 0, 8, 8],
39
+ [0, 4, 4, 8],
40
+ [2, 0, 4, 4],
41
+ [0, 2, 2, 4],
42
+ [1, 0, 2, 2],
43
+ [0, 1, 1, 2],
44
+ ];
45
+ // [declare]
46
+ // the CRC-32 of every byte, for each chunk's check: Node's zlib.crc32 is
47
+ // newer than some of the Node 22s the CLI runs on.
48
+ const CRC = Uint32Array.from({ length: 256 }, (_, n) => {
49
+ let c = n;
50
+ for (let k = 0; k < 8; k += 1) {
51
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
52
+ }
53
+ return c >>> 0;
54
+ });
55
+ // [function]
56
+ // the error for a file that isn't a PNG this module reads.
57
+ function invalid(why) {
58
+ return new Error(`Not a PNG that can be read: ${why}.`);
59
+ }
60
+ // [function]
61
+ function crc32(bytes) {
62
+ let c = 0xffffffff;
63
+ for (const byte of bytes) {
64
+ c = (CRC[(c ^ byte) & 0xff] ?? 0) ^ (c >>> 8);
65
+ }
66
+ return (c ^ 0xffffffff) >>> 0;
67
+ }
68
+ // [function]
69
+ function readHeader(data) {
70
+ if (data.length !== 13) {
71
+ throw invalid("IHDR");
72
+ }
73
+ const header = {
74
+ depth: data.readUInt8(8),
75
+ height: data.readUInt32BE(4),
76
+ interlaced: data.readUInt8(12) === 1,
77
+ type: data.readUInt8(9),
78
+ width: data.readUInt32BE(0),
79
+ };
80
+ if (!DEPTHS[header.type]?.includes(header.depth)) {
81
+ throw invalid("color type or bit depth");
82
+ }
83
+ if (data.readUInt8(10) !== 0 || data.readUInt8(11) !== 0) {
84
+ throw invalid("compression or filter method");
85
+ }
86
+ if (data.readUInt8(12) > 1) {
87
+ throw invalid("interlace method");
88
+ }
89
+ const { height, width } = header;
90
+ if (!width || !height || width > SIDE_MAX || height > SIDE_MAX) {
91
+ throw invalid(`dimensions, ${width} × ${height}`);
92
+ }
93
+ return header;
94
+ }
95
+ // [function]
96
+ // the chunks a PNG's pixels need; the rest, such as its text, are skipped.
97
+ function readChunks(file) {
98
+ if (file.length < 8 || !file.subarray(0, 8).equals(SIGNATURE)) {
99
+ throw invalid("signature");
100
+ }
101
+ let header = null;
102
+ let palette = null;
103
+ let transparency = null;
104
+ const data = [];
105
+ let at = 8;
106
+ for (;;) {
107
+ if (at + 12 > file.length) {
108
+ throw invalid("ends early");
109
+ }
110
+ const length = file.readUInt32BE(at);
111
+ const type = file.toString("latin1", at + 4, at + 8);
112
+ const body = file.subarray(at + 8, at + 8 + length);
113
+ if (body.length !== length || at + 12 + length > file.length) {
114
+ throw invalid("ends early");
115
+ }
116
+ at += 12 + length;
117
+ if (type === "IHDR") {
118
+ header = readHeader(body);
119
+ }
120
+ else if (type === "PLTE") {
121
+ palette = body;
122
+ }
123
+ else if (type === "tRNS") {
124
+ transparency = body;
125
+ }
126
+ else if (type === "IDAT") {
127
+ data.push(body);
128
+ }
129
+ else if (type === "IEND") {
130
+ break;
131
+ }
132
+ }
133
+ if (!header) {
134
+ throw invalid("no IHDR");
135
+ }
136
+ if (header.type === PALETTE && !palette) {
137
+ throw invalid("no PLTE");
138
+ }
139
+ return { data: Buffer.concat(data), header, palette, transparency };
140
+ }
141
+ // [function]
142
+ // the Paeth predictor: whichever of left, up, or upper left is nearest
143
+ // their sum.
144
+ function paeth(a, b, c) {
145
+ const p = a + b - c;
146
+ const pa = Math.abs(p - a);
147
+ const pb = Math.abs(p - b);
148
+ const pc = Math.abs(p - c);
149
+ return pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
150
+ }
151
+ // [function]
152
+ // undoes each row's filter in place: `rows` is one pass's rows, each a
153
+ // filter byte and `stride` bytes, `bpp` bytes a pixel, at least one.
154
+ function unfilter(rows, stride, bpp) {
155
+ const out = Buffer.alloc(stride * (rows.length / (stride + 1)));
156
+ for (let y = 0; y * (stride + 1) < rows.length; y += 1) {
157
+ const filter = rows[y * (stride + 1)];
158
+ const from = y * (stride + 1) + 1;
159
+ const to = y * stride;
160
+ for (let x = 0; x < stride; x += 1) {
161
+ const raw = rows[from + x] ?? 0;
162
+ const left = x >= bpp ? (out[to + x - bpp] ?? 0) : 0;
163
+ const up = y ? (out[to - stride + x] ?? 0) : 0;
164
+ const upLeft = y && x >= bpp ? (out[to - stride + x - bpp] ?? 0) : 0;
165
+ const predicted = filter === 0
166
+ ? 0
167
+ : filter === 1
168
+ ? left
169
+ : filter === 2
170
+ ? up
171
+ : filter === 3
172
+ ? (left + up) >> 1
173
+ : filter === 4
174
+ ? paeth(left, up, upLeft)
175
+ : -1;
176
+ if (predicted < 0) {
177
+ throw invalid("filter type");
178
+ }
179
+ out[to + x] = (raw + predicted) & 0xff;
180
+ }
181
+ }
182
+ return out;
183
+ }
184
+ // [function]
185
+ // the samples of one row, unfiltered, as numbers at the image's depth.
186
+ function samplesOf(row, count, depth) {
187
+ const out = [];
188
+ for (let i = 0; i < count; i += 1) {
189
+ if (depth === 16) {
190
+ out.push(row.readUInt16BE(i * 2));
191
+ }
192
+ else if (depth === 8) {
193
+ out.push(row[i] ?? 0);
194
+ }
195
+ else {
196
+ const bit = i * depth;
197
+ const byte = row[bit >> 3] ?? 0;
198
+ out.push((byte >> (8 - depth - (bit & 7))) & ((1 << depth) - 1));
199
+ }
200
+ }
201
+ return out;
202
+ }
203
+ // [function]
204
+ // one pixel's samples as 8-bit RGBA, with the image's transparency.
205
+ function rgbaOf(samples, chunks) {
206
+ const { depth, type } = chunks.header;
207
+ const trns = chunks.transparency;
208
+ const max = (1 << depth) - 1;
209
+ const to8 = (v) => depth === 16 ? v >> 8 : Math.round((v * 255) / max);
210
+ const s = (i) => samples[i] ?? 0;
211
+ if (type === PALETTE) {
212
+ const index = s(0);
213
+ const palette = chunks.palette ?? Buffer.alloc(0);
214
+ if (index * 3 + 2 >= palette.length) {
215
+ throw invalid("palette index");
216
+ }
217
+ return [
218
+ palette[index * 3] ?? 0,
219
+ palette[index * 3 + 1] ?? 0,
220
+ palette[index * 3 + 2] ?? 0,
221
+ trns && index < trns.length ? (trns[index] ?? 255) : 255,
222
+ ];
223
+ }
224
+ if (type === GRAY) {
225
+ const g = to8(s(0));
226
+ const clear = trns?.length === 2 && trns.readUInt16BE(0) === s(0);
227
+ return [g, g, g, clear ? 0 : 255];
228
+ }
229
+ if (type === GRAY_ALPHA) {
230
+ const g = to8(s(0));
231
+ return [g, g, g, to8(s(1))];
232
+ }
233
+ if (type === RGB) {
234
+ const clear = trns?.length === 6 &&
235
+ trns.readUInt16BE(0) === s(0) &&
236
+ trns.readUInt16BE(2) === s(1) &&
237
+ trns.readUInt16BE(4) === s(2);
238
+ return [to8(s(0)), to8(s(1)), to8(s(2)), clear ? 0 : 255];
239
+ }
240
+ return [to8(s(0)), to8(s(1)), to8(s(2)), to8(s(3))];
241
+ }
242
+ // [function]
243
+ // a PNG file read as 8-bit RGBA: any color type and bit depth, interlaced or
244
+ // not, with its transparency. Throws for a file it can't read.
245
+ export function decode(file) {
246
+ const chunks = readChunks(file);
247
+ const { depth, height, interlaced, type, width } = chunks.header;
248
+ const channels = SAMPLES[type] ?? 1;
249
+ const bpp = Math.max(1, (channels * depth) >> 3);
250
+ const passes = interlaced ? PASSES : [[0, 0, 1, 1]];
251
+ // what the rows take, filter bytes and all, so a stream that would
252
+ // inflate past them is refused rather than read
253
+ const sizes = passes.map(([x0, y0, dx, dy]) => {
254
+ const across = Math.ceil((width - x0) / dx);
255
+ const down = Math.ceil((height - y0) / dy);
256
+ const stride = Math.ceil((across * channels * depth) / 8);
257
+ return { across, down, stride };
258
+ });
259
+ const expected = sizes.reduce((sum, { across, down, stride }) => sum + (across && down ? (stride + 1) * down : 0), 0);
260
+ let raw;
261
+ try {
262
+ raw = zlib.inflateSync(chunks.data, { maxOutputLength: expected });
263
+ }
264
+ catch {
265
+ throw invalid("image data");
266
+ }
267
+ if (raw.length < expected) {
268
+ throw invalid("image data ends early");
269
+ }
270
+ const data = new Uint8Array(width * height * 4);
271
+ let at = 0;
272
+ passes.forEach(([x0, y0, dx, dy], p) => {
273
+ const { across, down, stride } = sizes[p] ?? {
274
+ across: 0,
275
+ down: 0,
276
+ stride: 0,
277
+ };
278
+ if (!across || !down) {
279
+ return;
280
+ }
281
+ const rows = unfilter(raw.subarray(at, at + (stride + 1) * down), stride, bpp);
282
+ at += (stride + 1) * down;
283
+ for (let y = 0; y < down; y += 1) {
284
+ const row = rows.subarray(y * stride, (y + 1) * stride);
285
+ const samples = samplesOf(row, across * channels, depth);
286
+ for (let x = 0; x < across; x += 1) {
287
+ const pixel = rgbaOf(samples.slice(x * channels, (x + 1) * channels), chunks);
288
+ data.set(pixel, ((y0 + y * dy) * width + x0 + x * dx) * 4);
289
+ }
290
+ }
291
+ });
292
+ return { data, height, width };
293
+ }
294
+ // [function]
295
+ // each output index's sources along an axis of `from` to `to` pixels: the
296
+ // source pixels it covers, each by the share it covers, so scaling down
297
+ // averages them all.
298
+ function taps(from, to) {
299
+ const scale = from / to;
300
+ return Array.from({ length: to }, (_, i) => {
301
+ const start = i * scale;
302
+ const end = Math.min(from, start + Math.max(scale, 1));
303
+ const out = [];
304
+ for (let j = Math.floor(start); j < end; j += 1) {
305
+ const weight = Math.min(j + 1, end) - Math.max(j, start);
306
+ if (weight > 0) {
307
+ out.push({ index: j, weight: weight / (end - start) });
308
+ }
309
+ }
310
+ return out;
311
+ });
312
+ }
313
+ // [function]
314
+ // `pixels` scaled to `width` × `height`, each output pixel the average of
315
+ // what it covers, weighted by its alpha, so a clear pixel's color doesn't
316
+ // bleed into the edge.
317
+ export function scale(pixels, width, height) {
318
+ const across = taps(pixels.width, width);
319
+ const down = taps(pixels.height, height);
320
+ // across first, premultiplied: a row a source row, `width` wide
321
+ const wide = new Float64Array(width * pixels.height * 4);
322
+ for (let y = 0; y < pixels.height; y += 1) {
323
+ across.forEach((sources, x) => {
324
+ for (const { index, weight } of sources) {
325
+ const from = (y * pixels.width + index) * 4;
326
+ const alpha = ((pixels.data[from + 3] ?? 0) / 255) * weight;
327
+ const to = (y * width + x) * 4;
328
+ for (let c = 0; c < 3; c += 1) {
329
+ wide[to + c] =
330
+ (wide[to + c] ?? 0) + (pixels.data[from + c] ?? 0) * alpha;
331
+ }
332
+ wide[to + 3] = (wide[to + 3] ?? 0) + alpha;
333
+ }
334
+ });
335
+ }
336
+ const data = new Uint8Array(width * height * 4);
337
+ down.forEach((sources, y) => {
338
+ for (let x = 0; x < width; x += 1) {
339
+ const sum = [0, 0, 0, 0];
340
+ for (const { index, weight } of sources) {
341
+ const from = (index * width + x) * 4;
342
+ for (let c = 0; c < 4; c += 1) {
343
+ sum[c] = (sum[c] ?? 0) + (wide[from + c] ?? 0) * weight;
344
+ }
345
+ }
346
+ const alpha = sum[3] ?? 0;
347
+ const to = (y * width + x) * 4;
348
+ for (let c = 0; c < 3; c += 1) {
349
+ data[to + c] = alpha ? Math.round((sum[c] ?? 0) / alpha) : 0;
350
+ }
351
+ data[to + 3] = Math.round(alpha * 255);
352
+ }
353
+ });
354
+ return { data, height, width };
355
+ }
356
+ // [function]
357
+ // one chunk: its length, type, body, and check.
358
+ function chunk(type, body) {
359
+ const head = Buffer.alloc(8);
360
+ head.writeUInt32BE(body.length, 0);
361
+ head.write(type, 4, "latin1");
362
+ const check = Buffer.alloc(4);
363
+ check.writeUInt32BE(crc32(Buffer.concat([head.subarray(4), body])), 0);
364
+ return Buffer.concat([head, body, check]);
365
+ }
366
+ // [function]
367
+ // a row filtered each of the five ways, keeping the one whose bytes are
368
+ // smallest as signed numbers, which tends to compress best.
369
+ function filterRow(row, prior) {
370
+ let best = null;
371
+ let bestSum = Infinity;
372
+ for (let filter = 0; filter < 5; filter += 1) {
373
+ const out = Buffer.alloc(row.length + 1);
374
+ out[0] = filter;
375
+ let sum = 0;
376
+ for (let x = 0; x < row.length; x += 1) {
377
+ const left = x >= 4 ? (row[x - 4] ?? 0) : 0;
378
+ const up = prior?.[x] ?? 0;
379
+ const upLeft = x >= 4 ? (prior?.[x - 4] ?? 0) : 0;
380
+ const predicted = [0, left, up, (left + up) >> 1, paeth(left, up, upLeft)][filter] ?? 0;
381
+ const value = ((row[x] ?? 0) - predicted) & 0xff;
382
+ out[x + 1] = value;
383
+ sum += value < 128 ? value : 256 - value;
384
+ }
385
+ if (sum < bestSum) {
386
+ best = out;
387
+ bestSum = sum;
388
+ }
389
+ }
390
+ return best ?? Buffer.alloc(row.length + 1);
391
+ }
392
+ // [function]
393
+ // `pixels` as a PNG file, 8-bit RGBA.
394
+ export function encode({ data, height, width }) {
395
+ const header = Buffer.alloc(13);
396
+ header.writeUInt32BE(width, 0);
397
+ header.writeUInt32BE(height, 4);
398
+ header.writeUInt8(8, 8);
399
+ header.writeUInt8(RGBA, 9);
400
+ const stride = width * 4;
401
+ const rows = Array.from({ length: height }, (_, y) => filterRow(data.subarray(y * stride, (y + 1) * stride), y ? data.subarray((y - 1) * stride, y * stride) : null));
402
+ return Buffer.concat([
403
+ SIGNATURE,
404
+ chunk("IHDR", header),
405
+ chunk("IDAT", zlib.deflateSync(Buffer.concat(rows), { level: 9 })),
406
+ chunk("IEND", Buffer.alloc(0)),
407
+ ]);
408
+ }
package/dist/util/zip.js CHANGED
@@ -27,6 +27,11 @@ export class Zip {
27
27
  this.zip.addFile(join(this.prefix, name), fs.readFileSync(file));
28
28
  }
29
29
  // [method]
30
+ // `content`, at `name` in the zip.
31
+ addBuffer(name, content) {
32
+ this.zip.addFile(join(this.prefix, name), content);
33
+ }
34
+ // [method]
30
35
  addJson(name, value) {
31
36
  const entry = join(this.prefix, name);
32
37
  const content = Buffer.from(JSON.stringify(deepSort(value), null, 2), "utf8");