arcadible 1.0.29 → 1.0.30

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.
@@ -10,6 +10,7 @@ import { MEDIA, PACKAGED } from "../../config/file.js";
10
10
  import { deployDirMissing, packageIndexMissing, packagePathReserved, packageTooLarge, runnerNotBuilt, unableToFind, } from "../../phrase/index.js";
11
11
  import { ZIP_SIZE } from "../../config/limit.js";
12
12
  import { check, measure } from "./limits.js";
13
+ import { favicon } from "../../util/favicon.js";
13
14
  import { handle, tool } from "../../config/product.js";
14
15
  import { toMb } from "../../util/size.js";
15
16
  import { unsound } from "./unsound.js";
@@ -21,6 +22,8 @@ const resolve = path.resolve;
21
22
  const TOOL = tool;
22
23
  // [declare]
23
24
  const MAIN = "index.html";
25
+ // what browsers ask a build for, which packing adds when it has none
26
+ const FAVICON = "favicon.ico";
24
27
  const MANIFEST = PACKAGED;
25
28
  const OUTPUT = `.${TOOL}/package`;
26
29
  // the folder every build's origin keeps for Arcadible's own files, such as
@@ -87,7 +90,7 @@ function verifyTotal(zip) {
87
90
  // [function]
88
91
  // `deployDir` when the input is deploy_dir, rather than a folder --input
89
92
  // named: a missing deploy_dir means the build hasn't written it.
90
- function pack(name, game, { deployDir, inputPath, mediaPath, onWarning, outputPath, runner, client, }) {
93
+ function pack(name, game, { deployDir, icon, inputPath, mediaPath, onWarning, outputPath, runner, client, }) {
91
94
  const zip = new Zip(name);
92
95
  return pathExists(inputPath)
93
96
  .then((existsInput) => {
@@ -102,7 +105,14 @@ function pack(name, game, { deployDir, inputPath, mediaPath, onWarning, outputPa
102
105
  return zip;
103
106
  })
104
107
  .then(verifyPartial)
105
- .then(() => pathExists(mediaPath))
108
+ .then(async () => {
109
+ // never over the developer's own: only a build without one gets it,
110
+ // so a direct and a git release have the same
111
+ if (!zip.has(FAVICON)) {
112
+ zip.addBuffer(FAVICON, await favicon(icon));
113
+ }
114
+ return pathExists(mediaPath);
115
+ })
106
116
  .then((existsMedia) => {
107
117
  if (existsMedia) {
108
118
  zip.addDir(mediaPath, MEDIA);
@@ -158,6 +168,7 @@ export function packTool({ input, onWarning, output, ...conf }) {
158
168
  const inputPath = resolve(root, input ?? workflow.deploy_dir);
159
169
  return pack(parse(file).name, game, {
160
170
  deployDir: inputPath === deployDir,
171
+ icon: game.icon ? resolve(root, game.icon) : null,
161
172
  inputPath,
162
173
  mediaPath: resolve(root, MEDIA),
163
174
  onWarning,
@@ -12,7 +12,8 @@ import { ARGUMENT_INVALID, DEPLOY_DIR_MISSING, PORT_IN_USE, } from "../../config
12
12
  import { clientNames } from "../../schema/client.js";
13
13
  import { buildCommand } from "../../util/command.js";
14
14
  import { ArcError } from "../../util/error.js";
15
- import { dirFiles } from "../../serve/files.js";
15
+ import { dirFiles, withDefault } from "../../serve/files.js";
16
+ import { favicon } from "../../util/favicon.js";
16
17
  import { given } from "../../parser/given.js";
17
18
  import * as phrase from "../../phrase/index.js";
18
19
  import { media } from "../../parser/media.js";
@@ -254,7 +255,14 @@ export async function serveTool(conf) {
254
255
  if (!(await pathExists(deployDir))) {
255
256
  throw new ArcError(DEPLOY_DIR_MISSING, phrase.deployDirMissing(relative(root, deployDir) || "."));
256
257
  }
257
- const served = await listen({ ...options, files: dirFiles(deployDir) });
258
+ // a build without a favicon.ico is answered with the one arc pack would
259
+ // add, made from the store icon as it is when asked
260
+ const files = withDefault(dirFiles(deployDir), "favicon.ico", async () => {
261
+ const { game: art } = await media(root);
262
+ const icon = art?.["icon"];
263
+ return favicon(typeof icon === "string" ? join(root, icon) : null);
264
+ });
265
+ const served = await listen({ ...options, files });
258
266
  const stop = watchDir(deployDir, () => {
259
267
  served.reload();
260
268
  handler.onReload();
@@ -0,0 +1,10 @@
1
+ // the package's own files, in `assets` beside `dist`, since the build copies
2
+ // only what it compiles. Nothing else is imported here, so arc serve's
3
+ // server, which needs nothing else from the CLI, can read it.
4
+ // [import]
5
+ import { join } from "node:path";
6
+ // [declare]
7
+ // the play page's own favicon.ico (app/src/web/arcadible-play/content): the
8
+ // local play page's for a game without a store icon, and a build's when it
9
+ // has neither its own nor a store icon (../util/favicon.ts).
10
+ export const FAVICON = join(import.meta.dirname, "..", "..", "assets", "favicon.ico");
@@ -125,11 +125,16 @@ export function client() {
125
125
  });
126
126
  }
127
127
  if (clientNames(table).join(" ").length > MAX_NAMES_LENGTH) {
128
+ // the servers are named only where the manifest lists them, since
129
+ // the docs leave `server_origins` out until it ships (message.ts).
130
+ const servers = table.server_origins?.length
131
+ ? ", with client.server_origins,"
132
+ : "";
128
133
  ctx.addIssue({
129
134
  code: "custom",
130
135
  path: ["outbound_hosts"],
131
136
  ...message("outbound_hosts", [
132
- `Must be, with client.server_origins, at most ${MAX_NAMES_LENGTH.toLocaleString("en-US")} characters together.`,
137
+ `Must be${servers} at most ${MAX_NAMES_LENGTH.toLocaleString("en-US")} characters together.`,
133
138
  ], ISSUE.TOO_LONG),
134
139
  });
135
140
  }
@@ -6,6 +6,7 @@
6
6
  import { createReadStream } from "node:fs";
7
7
  import { readdir, stat } from "node:fs/promises";
8
8
  import { join } from "node:path";
9
+ import { Readable } from "node:stream";
9
10
  // [function]
10
11
  // whether every segment of `segments` is named exactly so in `root`.
11
12
  async function exactCase(root, segments) {
@@ -37,3 +38,26 @@ export function dirFiles(root) {
37
38
  },
38
39
  };
39
40
  }
41
+ // [function]
42
+ // `files`, with `name` at the root answered by `make()` when `files` has
43
+ // none, as a build without a favicon.ico has Arcadible's packing add one.
44
+ // It's made each time it's asked for, so a change to what it's made from
45
+ // shows on the next request.
46
+ export function withDefault(files, name, make) {
47
+ return {
48
+ async find(segments) {
49
+ const found = await files.find(segments);
50
+ if (found || segments.length !== 1 || segments[0] !== name) {
51
+ return found;
52
+ }
53
+ const body = await make();
54
+ return {
55
+ mtime: new Date(),
56
+ open: (range) => Readable.from([
57
+ range ? body.subarray(range.start, range.end + 1) : body,
58
+ ]),
59
+ size: body.length,
60
+ };
61
+ },
62
+ };
63
+ }
@@ -8,9 +8,9 @@
8
8
  // game's store icon, as the play page's does, or Arcadible's.
9
9
  // [import]
10
10
  import { readFile } from "node:fs/promises";
11
- import { join } from "node:path";
12
11
  // [import]
13
12
  import { endWith, pathOf } from "./game.js";
13
+ import { FAVICON } from "../config/asset.js";
14
14
  import { isTable } from "../util/objs.js";
15
15
  import { originOf } from "./ticket.js";
16
16
  import { SCOPES, } from "./values.js";
@@ -95,11 +95,6 @@ const BODY_MAX = 4096;
95
95
  // the loading screen's art and icon, PNGs, or 404 without.
96
96
  const ART = "/_arcadible/art.png";
97
97
  const ICON_PNG = "/_arcadible/icon.png";
98
- // the play page's own favicon.ico (app/src/web/arcadible-play/content), for a
99
- // game without a store icon, and for /favicon.ico, which browsers ask for.
100
- // It's in the package's `assets`, beside `dist`, since the build copies only
101
- // what it compiles.
102
- export const FAVICON = join(import.meta.dirname, "..", "..", "assets", "favicon.ico");
103
98
  // [declare]
104
99
  // the headers of the page's responses, as the play page's: it's
105
100
  // cross-origin isolated, and nothing frames it.
@@ -0,0 +1,63 @@
1
+ // a build's favicon.ico, for a build without its own: the project's store
2
+ // icon scaled to 16, 32, and 48 pixels, each a PNG inside the ICO, or the
3
+ // play page's own without one. `arc pack` puts it in the package, and `arc
4
+ // serve` answers /favicon.ico with it, so every build has one.
5
+ // [import]
6
+ import { readFile } from "node:fs/promises";
7
+ // [import]
8
+ import { FAVICON } from "../config/asset.js";
9
+ import { decode, encode, scale } from "./png.js";
10
+ // [declare]
11
+ // what browsers pick from for a tab, a bookmark, and a shortcut. An ICO's
12
+ // images are at most 256 a side, and the icon is at least 512.
13
+ export const SIZES = [16, 32, 48];
14
+ // [declare]
15
+ const DIR_SIZE = 6;
16
+ const ENTRY_SIZE = 16;
17
+ // [function]
18
+ // an ICO holding `images`, each a PNG of `size` pixels a side, in order.
19
+ export function ico(images) {
20
+ const dir = Buffer.alloc(DIR_SIZE);
21
+ dir.writeUInt16LE(0, 0);
22
+ dir.writeUInt16LE(1, 2);
23
+ dir.writeUInt16LE(images.length, 4);
24
+ let offset = DIR_SIZE + ENTRY_SIZE * images.length;
25
+ const entries = images.map(({ png, size }) => {
26
+ const entry = Buffer.alloc(ENTRY_SIZE);
27
+ // 0 says 256
28
+ entry.writeUInt8(size % 256, 0);
29
+ entry.writeUInt8(size % 256, 1);
30
+ entry.writeUInt8(0, 2);
31
+ entry.writeUInt8(0, 3);
32
+ entry.writeUInt16LE(1, 4);
33
+ entry.writeUInt16LE(32, 6);
34
+ entry.writeUInt32LE(png.length, 8);
35
+ entry.writeUInt32LE(offset, 12);
36
+ offset += png.length;
37
+ return entry;
38
+ });
39
+ return Buffer.concat([dir, ...entries, ...images.map(({ png }) => png)]);
40
+ }
41
+ // [function]
42
+ // the favicon.ico made from `icon`, a PNG's bytes, scaled to each of SIZES.
43
+ // Throws for one that can't be read as a PNG.
44
+ export function iconIco(icon) {
45
+ const pixels = decode(icon);
46
+ return ico(SIZES.map((size) => ({ png: encode(scale(pixels, size, size)), size })));
47
+ }
48
+ // [function]
49
+ // the favicon.ico for a build without its own: made from `icon`, the store
50
+ // icon's path, when there is one and it can be read as a PNG, else the play
51
+ // page's own. `arc validate` reads only the icon's header, so one whose
52
+ // pixels can't be read gets Arcadible's rather than failing the pack.
53
+ export async function favicon(icon) {
54
+ if (icon) {
55
+ try {
56
+ return iconIco(await readFile(icon));
57
+ }
58
+ catch {
59
+ // the play page's own, below
60
+ }
61
+ }
62
+ return readFile(FAVICON);
63
+ }
@@ -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");
@@ -43,7 +43,7 @@
43
43
  },
44
44
  "license": "Apache-2.0",
45
45
  "name": "arcadible",
46
- "version": "1.0.29"
46
+ "version": "1.0.30"
47
47
  },
48
48
  "node_modules/@asamuzakjp/css-color": {
49
49
  "dependencies": {
@@ -3252,5 +3252,5 @@
3252
3252
  }
3253
3253
  },
3254
3254
  "requires": true,
3255
- "version": "1.0.29"
3255
+ "version": "1.0.30"
3256
3256
  }
package/package.json CHANGED
@@ -67,5 +67,5 @@
67
67
  "test": "tsc && npm run lint && node --test \"test/**/*.test.js\" \"test/**/*.test.mjs\""
68
68
  },
69
69
  "type": "module",
70
- "version": "1.0.29"
70
+ "version": "1.0.30"
71
71
  }