roxify 1.2.7 → 1.2.9
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/CHANGELOG.md +74 -0
- package/LICENSE +21 -21
- package/README.md +370 -117
- package/dist/cli.d.ts +2 -2
- package/dist/cli.js +607 -606
- package/dist/index.d.ts +13 -13
- package/dist/index.js +13 -13
- package/dist/minpng.d.ts +8 -8
- package/dist/minpng.js +238 -238
- package/dist/pack.d.ts +31 -31
- package/dist/pack.js +205 -188
- package/dist/roxify-cli +0 -0
- package/dist/utils/constants.d.ts +38 -38
- package/dist/utils/constants.js +22 -22
- package/dist/utils/crc.d.ts +4 -4
- package/dist/utils/crc.js +29 -29
- package/dist/utils/decoder.d.ts +4 -4
- package/dist/utils/decoder.js +665 -665
- package/dist/utils/encoder.d.ts +4 -4
- package/dist/utils/encoder.js +391 -422
- package/dist/utils/errors.d.ts +9 -9
- package/dist/utils/errors.js +18 -18
- package/dist/utils/helpers.d.ts +11 -11
- package/dist/utils/helpers.js +112 -76
- package/dist/utils/inspection.d.ts +19 -19
- package/dist/utils/inspection.js +518 -518
- package/dist/utils/optimization.d.ts +3 -3
- package/dist/utils/optimization.js +640 -640
- package/dist/utils/reconstitution.d.ts +3 -3
- package/dist/utils/reconstitution.js +266 -266
- package/dist/utils/rust-cli-wrapper.d.ts +1 -0
- package/dist/utils/rust-cli-wrapper.js +36 -0
- package/dist/utils/types.d.ts +46 -44
- package/dist/utils/types.js +1 -1
- package/dist/utils/zstd.d.ts +17 -17
- package/dist/utils/zstd.js +153 -131
- package/libroxify_native.node +0 -0
- package/package.json +48 -65
package/dist/utils/encoder.js
CHANGED
|
@@ -1,422 +1,391 @@
|
|
|
1
|
-
import cliProgress from 'cli-progress';
|
|
2
|
-
import { createCipheriv, pbkdf2Sync, randomBytes } from 'crypto';
|
|
3
|
-
import sharp from 'sharp';
|
|
4
|
-
import * as zlib from 'zlib';
|
|
5
|
-
import { unpackBuffer } from '../pack.js';
|
|
6
|
-
import { COMPRESSION_MARKERS, ENC_AES, ENC_NONE, ENC_XOR, MAGIC, MARKER_END, MARKER_START, PIXEL_MAGIC, PNG_HEADER, } from './constants.js';
|
|
7
|
-
import { crc32 } from './crc.js';
|
|
8
|
-
import { colorsToBytes } from './helpers.js';
|
|
9
|
-
import { optimizePngBuffer } from './optimization.js';
|
|
10
|
-
import { parallelZstdCompress } from './zstd.js';
|
|
11
|
-
export async function encodeBinaryToPng(input, opts = {}) {
|
|
12
|
-
let progressBar = null;
|
|
13
|
-
if (opts.showProgress) {
|
|
14
|
-
progressBar = new cliProgress.SingleBar({
|
|
15
|
-
format: ' {bar} {percentage}% | {step} | {elapsed}s',
|
|
16
|
-
}, cliProgress.Presets.shades_classic);
|
|
17
|
-
progressBar.start(100, 0, { step: 'Starting', elapsed: '0' });
|
|
18
|
-
const startTime = Date.now();
|
|
19
|
-
if (!opts.onProgress) {
|
|
20
|
-
opts.onProgress = (info) => {
|
|
21
|
-
let pct = 0;
|
|
22
|
-
if (info.phase === 'compress_progress' && info.loaded && info.total) {
|
|
23
|
-
pct = (info.loaded / info.total) * 50;
|
|
24
|
-
}
|
|
25
|
-
else if (info.phase === 'compress_done') {
|
|
26
|
-
pct = 50;
|
|
27
|
-
}
|
|
28
|
-
else if (info.phase === 'encrypt_done') {
|
|
29
|
-
pct = 80;
|
|
30
|
-
}
|
|
31
|
-
else if (info.phase === 'png_gen') {
|
|
32
|
-
pct = 90;
|
|
33
|
-
}
|
|
34
|
-
else if (info.phase === 'done') {
|
|
35
|
-
pct = 100;
|
|
36
|
-
}
|
|
37
|
-
progressBar.update(Math.floor(pct), {
|
|
38
|
-
step: info.phase.replace('_', ' '),
|
|
39
|
-
elapsed: String(Math.floor((Date.now() - startTime) / 1000)),
|
|
40
|
-
});
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
let payloadInput;
|
|
45
|
-
let totalLen = 0;
|
|
46
|
-
if (Array.isArray(input)) {
|
|
47
|
-
payloadInput = [MAGIC, ...input];
|
|
48
|
-
totalLen = MAGIC.length + input.reduce((a, b) => a + b.length, 0);
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
payloadInput = [MAGIC, input];
|
|
52
|
-
totalLen = MAGIC.length + input.length;
|
|
53
|
-
}
|
|
54
|
-
if (opts.onProgress)
|
|
55
|
-
opts.onProgress({ phase: 'compress_start', total: totalLen });
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
const
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
const
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
metaParts.push(nameBuf);
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const
|
|
172
|
-
lenBuf.
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
const
|
|
184
|
-
payloadLenBuf.
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
Buffer.from([
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
const
|
|
219
|
-
lenBuf.
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const
|
|
224
|
-
const
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
const
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const
|
|
237
|
-
const
|
|
238
|
-
const
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
let
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
const
|
|
255
|
-
const
|
|
256
|
-
const
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
let
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
const
|
|
333
|
-
const
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
}
|
|
393
|
-
else {
|
|
394
|
-
bufScr = await sharp(raw, {
|
|
395
|
-
raw: { width, height, channels: 3 },
|
|
396
|
-
})
|
|
397
|
-
.png({
|
|
398
|
-
compressionLevel: 3,
|
|
399
|
-
palette: false,
|
|
400
|
-
effort: 1,
|
|
401
|
-
adaptiveFiltering: false,
|
|
402
|
-
})
|
|
403
|
-
.toBuffer();
|
|
404
|
-
}
|
|
405
|
-
raw = Buffer.alloc(0);
|
|
406
|
-
if (opts.onProgress)
|
|
407
|
-
opts.onProgress({ phase: 'png_compress', loaded: 100, total: 100 });
|
|
408
|
-
if (opts.onProgress)
|
|
409
|
-
opts.onProgress({ phase: 'optimizing', loaded: 0, total: 100 });
|
|
410
|
-
try {
|
|
411
|
-
const optimized = await optimizePngBuffer(bufScr, true);
|
|
412
|
-
if (opts.onProgress)
|
|
413
|
-
opts.onProgress({ phase: 'optimizing', loaded: 100, total: 100 });
|
|
414
|
-
progressBar?.stop();
|
|
415
|
-
return optimized;
|
|
416
|
-
}
|
|
417
|
-
catch (e) {
|
|
418
|
-
progressBar?.stop();
|
|
419
|
-
return bufScr;
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
}
|
|
1
|
+
import cliProgress from 'cli-progress';
|
|
2
|
+
import { createCipheriv, pbkdf2Sync, randomBytes } from 'crypto';
|
|
3
|
+
import sharp from 'sharp';
|
|
4
|
+
import * as zlib from 'zlib';
|
|
5
|
+
import { unpackBuffer } from '../pack.js';
|
|
6
|
+
import { COMPRESSION_MARKERS, ENC_AES, ENC_NONE, ENC_XOR, MAGIC, MARKER_END, MARKER_START, PIXEL_MAGIC, PNG_HEADER, } from './constants.js';
|
|
7
|
+
import { crc32 } from './crc.js';
|
|
8
|
+
import { colorsToBytes } from './helpers.js';
|
|
9
|
+
import { optimizePngBuffer } from './optimization.js';
|
|
10
|
+
import { parallelZstdCompress } from './zstd.js';
|
|
11
|
+
export async function encodeBinaryToPng(input, opts = {}) {
|
|
12
|
+
let progressBar = null;
|
|
13
|
+
if (opts.showProgress) {
|
|
14
|
+
progressBar = new cliProgress.SingleBar({
|
|
15
|
+
format: ' {bar} {percentage}% | {step} | {elapsed}s',
|
|
16
|
+
}, cliProgress.Presets.shades_classic);
|
|
17
|
+
progressBar.start(100, 0, { step: 'Starting', elapsed: '0' });
|
|
18
|
+
const startTime = Date.now();
|
|
19
|
+
if (!opts.onProgress) {
|
|
20
|
+
opts.onProgress = (info) => {
|
|
21
|
+
let pct = 0;
|
|
22
|
+
if (info.phase === 'compress_progress' && info.loaded && info.total) {
|
|
23
|
+
pct = (info.loaded / info.total) * 50;
|
|
24
|
+
}
|
|
25
|
+
else if (info.phase === 'compress_done') {
|
|
26
|
+
pct = 50;
|
|
27
|
+
}
|
|
28
|
+
else if (info.phase === 'encrypt_done') {
|
|
29
|
+
pct = 80;
|
|
30
|
+
}
|
|
31
|
+
else if (info.phase === 'png_gen') {
|
|
32
|
+
pct = 90;
|
|
33
|
+
}
|
|
34
|
+
else if (info.phase === 'done') {
|
|
35
|
+
pct = 100;
|
|
36
|
+
}
|
|
37
|
+
progressBar.update(Math.floor(pct), {
|
|
38
|
+
step: info.phase.replace('_', ' '),
|
|
39
|
+
elapsed: String(Math.floor((Date.now() - startTime) / 1000)),
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
let payloadInput;
|
|
45
|
+
let totalLen = 0;
|
|
46
|
+
if (Array.isArray(input)) {
|
|
47
|
+
payloadInput = [MAGIC, ...input];
|
|
48
|
+
totalLen = MAGIC.length + input.reduce((a, b) => a + b.length, 0);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
payloadInput = [MAGIC, input];
|
|
52
|
+
totalLen = MAGIC.length + input.length;
|
|
53
|
+
}
|
|
54
|
+
if (opts.onProgress)
|
|
55
|
+
opts.onProgress({ phase: 'compress_start', total: totalLen });
|
|
56
|
+
const compressionLevel = opts.compressionLevel ?? 3;
|
|
57
|
+
let payload = await parallelZstdCompress(payloadInput, compressionLevel, (loaded, total) => {
|
|
58
|
+
if (opts.onProgress) {
|
|
59
|
+
opts.onProgress({
|
|
60
|
+
phase: 'compress_progress',
|
|
61
|
+
loaded,
|
|
62
|
+
total,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
if (opts.onProgress)
|
|
67
|
+
opts.onProgress({ phase: 'compress_done', loaded: payload.length });
|
|
68
|
+
if (Array.isArray(input)) {
|
|
69
|
+
input.length = 0;
|
|
70
|
+
}
|
|
71
|
+
if (opts.passphrase && !opts.encrypt) {
|
|
72
|
+
opts.encrypt = 'aes';
|
|
73
|
+
}
|
|
74
|
+
if (opts.encrypt === 'auto' && !opts._skipAuto) {
|
|
75
|
+
const candidates = ['none', 'xor', 'aes'];
|
|
76
|
+
const candidateBufs = [];
|
|
77
|
+
for (const c of candidates) {
|
|
78
|
+
const testBuf = await encodeBinaryToPng(input, {
|
|
79
|
+
...opts,
|
|
80
|
+
encrypt: c,
|
|
81
|
+
_skipAuto: true,
|
|
82
|
+
});
|
|
83
|
+
candidateBufs.push({ enc: c, buf: testBuf });
|
|
84
|
+
}
|
|
85
|
+
candidateBufs.sort((a, b) => a.buf.length - b.buf.length);
|
|
86
|
+
return candidateBufs[0].buf;
|
|
87
|
+
}
|
|
88
|
+
if (opts.passphrase && opts.encrypt && opts.encrypt !== 'auto') {
|
|
89
|
+
const encChoice = opts.encrypt;
|
|
90
|
+
if (opts.onProgress)
|
|
91
|
+
opts.onProgress({ phase: 'encrypt_start' });
|
|
92
|
+
if (encChoice === 'aes') {
|
|
93
|
+
const salt = randomBytes(16);
|
|
94
|
+
const iv = randomBytes(12);
|
|
95
|
+
const PBKDF2_ITERS = 1000000;
|
|
96
|
+
const key = pbkdf2Sync(opts.passphrase, salt, PBKDF2_ITERS, 32, 'sha256');
|
|
97
|
+
const cipher = createCipheriv('aes-256-gcm', key, iv);
|
|
98
|
+
const encParts = [];
|
|
99
|
+
for (const chunk of payload) {
|
|
100
|
+
encParts.push(cipher.update(chunk));
|
|
101
|
+
}
|
|
102
|
+
encParts.push(cipher.final());
|
|
103
|
+
const tag = cipher.getAuthTag();
|
|
104
|
+
payload = [Buffer.from([ENC_AES]), salt, iv, tag, ...encParts];
|
|
105
|
+
if (opts.onProgress)
|
|
106
|
+
opts.onProgress({ phase: 'encrypt_done' });
|
|
107
|
+
}
|
|
108
|
+
else if (encChoice === 'xor') {
|
|
109
|
+
const xoredParts = [];
|
|
110
|
+
let offset = 0;
|
|
111
|
+
const keyBuf = Buffer.from(opts.passphrase, 'utf8');
|
|
112
|
+
for (const chunk of payload) {
|
|
113
|
+
const out = Buffer.alloc(chunk.length);
|
|
114
|
+
for (let i = 0; i < chunk.length; i++) {
|
|
115
|
+
out[i] = chunk[i] ^ keyBuf[(offset + i) % keyBuf.length];
|
|
116
|
+
}
|
|
117
|
+
offset += chunk.length;
|
|
118
|
+
xoredParts.push(out);
|
|
119
|
+
}
|
|
120
|
+
payload = [Buffer.from([ENC_XOR]), ...xoredParts];
|
|
121
|
+
if (opts.onProgress)
|
|
122
|
+
opts.onProgress({ phase: 'encrypt_done' });
|
|
123
|
+
}
|
|
124
|
+
else if (encChoice === 'none') {
|
|
125
|
+
payload = [Buffer.from([ENC_NONE]), ...payload];
|
|
126
|
+
if (opts.onProgress)
|
|
127
|
+
opts.onProgress({ phase: 'encrypt_done' });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
payload = [Buffer.from([ENC_NONE]), ...payload];
|
|
132
|
+
}
|
|
133
|
+
const payloadTotalLen = payload.reduce((a, b) => a + b.length, 0);
|
|
134
|
+
if (opts.onProgress)
|
|
135
|
+
opts.onProgress({ phase: 'meta_prep_done', loaded: payloadTotalLen });
|
|
136
|
+
const metaParts = [];
|
|
137
|
+
const includeName = opts.includeName === undefined ? true : !!opts.includeName;
|
|
138
|
+
if (includeName && opts.name) {
|
|
139
|
+
const nameBuf = Buffer.from(opts.name, 'utf8');
|
|
140
|
+
metaParts.push(Buffer.from([nameBuf.length]));
|
|
141
|
+
metaParts.push(nameBuf);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
metaParts.push(Buffer.from([0]));
|
|
145
|
+
}
|
|
146
|
+
let meta = [...metaParts, ...payload];
|
|
147
|
+
if (opts.includeFileList && opts.fileList) {
|
|
148
|
+
let sizeMap = null;
|
|
149
|
+
if (!Array.isArray(input)) {
|
|
150
|
+
try {
|
|
151
|
+
const unpack = unpackBuffer(input);
|
|
152
|
+
if (unpack) {
|
|
153
|
+
sizeMap = {};
|
|
154
|
+
for (const ef of unpack.files)
|
|
155
|
+
sizeMap[ef.path] = ef.buf.length;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
catch (e) { }
|
|
159
|
+
}
|
|
160
|
+
const normalized = opts.fileList.map((f) => {
|
|
161
|
+
if (typeof f === 'string')
|
|
162
|
+
return { name: f, size: sizeMap && sizeMap[f] ? sizeMap[f] : 0 };
|
|
163
|
+
if (f && typeof f === 'object') {
|
|
164
|
+
if (f.name)
|
|
165
|
+
return { name: f.name, size: f.size ?? 0 };
|
|
166
|
+
if (f.path)
|
|
167
|
+
return { name: f.path, size: f.size ?? 0 };
|
|
168
|
+
}
|
|
169
|
+
return { name: String(f), size: 0 };
|
|
170
|
+
});
|
|
171
|
+
const jsonBuf = Buffer.from(JSON.stringify(normalized), 'utf8');
|
|
172
|
+
const lenBuf = Buffer.alloc(4);
|
|
173
|
+
lenBuf.writeUInt32BE(jsonBuf.length, 0);
|
|
174
|
+
meta = [...meta, Buffer.from('rXFL', 'utf8'), lenBuf, jsonBuf];
|
|
175
|
+
}
|
|
176
|
+
if (opts.output === 'rox') {
|
|
177
|
+
return Buffer.concat([MAGIC, ...meta]);
|
|
178
|
+
}
|
|
179
|
+
{
|
|
180
|
+
const nameBuf = opts.name
|
|
181
|
+
? Buffer.from(opts.name, 'utf8')
|
|
182
|
+
: Buffer.alloc(0);
|
|
183
|
+
const nameLen = nameBuf.length;
|
|
184
|
+
const payloadLenBuf = Buffer.alloc(4);
|
|
185
|
+
payloadLenBuf.writeUInt32BE(payloadTotalLen, 0);
|
|
186
|
+
const version = 1;
|
|
187
|
+
let metaPixel = [
|
|
188
|
+
Buffer.from([version]),
|
|
189
|
+
Buffer.from([nameLen]),
|
|
190
|
+
nameBuf,
|
|
191
|
+
payloadLenBuf,
|
|
192
|
+
...payload,
|
|
193
|
+
];
|
|
194
|
+
if (opts.includeFileList && opts.fileList) {
|
|
195
|
+
let sizeMap2 = null;
|
|
196
|
+
if (!Array.isArray(input)) {
|
|
197
|
+
try {
|
|
198
|
+
const unpack = unpackBuffer(input);
|
|
199
|
+
if (unpack) {
|
|
200
|
+
sizeMap2 = {};
|
|
201
|
+
for (const ef of unpack.files)
|
|
202
|
+
sizeMap2[ef.path] = ef.buf.length;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
catch (e) { }
|
|
206
|
+
}
|
|
207
|
+
const normalized = opts.fileList.map((f) => {
|
|
208
|
+
if (typeof f === 'string')
|
|
209
|
+
return { name: f, size: sizeMap2 && sizeMap2[f] ? sizeMap2[f] : 0 };
|
|
210
|
+
if (f && typeof f === 'object') {
|
|
211
|
+
if (f.name)
|
|
212
|
+
return { name: f.name, size: f.size ?? 0 };
|
|
213
|
+
if (f.path)
|
|
214
|
+
return { name: f.path, size: f.size ?? 0 };
|
|
215
|
+
}
|
|
216
|
+
return { name: String(f), size: 0 };
|
|
217
|
+
});
|
|
218
|
+
const jsonBuf = Buffer.from(JSON.stringify(normalized), 'utf8');
|
|
219
|
+
const lenBuf = Buffer.alloc(4);
|
|
220
|
+
lenBuf.writeUInt32BE(jsonBuf.length, 0);
|
|
221
|
+
metaPixel = [...metaPixel, Buffer.from('rXFL', 'utf8'), lenBuf, jsonBuf];
|
|
222
|
+
}
|
|
223
|
+
const dataWithoutMarkers = [PIXEL_MAGIC, ...metaPixel];
|
|
224
|
+
const dataWithoutMarkersLen = dataWithoutMarkers.reduce((a, b) => a + b.length, 0);
|
|
225
|
+
const padding = (3 - (dataWithoutMarkersLen % 3)) % 3;
|
|
226
|
+
const paddedData = padding > 0
|
|
227
|
+
? [...dataWithoutMarkers, Buffer.alloc(padding)]
|
|
228
|
+
: dataWithoutMarkers;
|
|
229
|
+
const markerStartBytes = colorsToBytes(MARKER_START);
|
|
230
|
+
const compressionMarkerBytes = colorsToBytes(COMPRESSION_MARKERS.zstd);
|
|
231
|
+
const dataWithMarkers = [
|
|
232
|
+
markerStartBytes,
|
|
233
|
+
compressionMarkerBytes,
|
|
234
|
+
...paddedData,
|
|
235
|
+
];
|
|
236
|
+
const bytesPerPixel = 3;
|
|
237
|
+
const dataWithMarkersLen = dataWithMarkers.reduce((a, b) => a + b.length, 0);
|
|
238
|
+
const dataPixels = Math.ceil(dataWithMarkersLen / 3);
|
|
239
|
+
const totalPixels = dataPixels + MARKER_END.length;
|
|
240
|
+
const maxWidth = 16384;
|
|
241
|
+
let side = Math.ceil(Math.sqrt(totalPixels));
|
|
242
|
+
if (side < MARKER_END.length)
|
|
243
|
+
side = MARKER_END.length;
|
|
244
|
+
let logicalWidth;
|
|
245
|
+
let logicalHeight;
|
|
246
|
+
if (side <= maxWidth) {
|
|
247
|
+
logicalWidth = side;
|
|
248
|
+
logicalHeight = side;
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
logicalWidth = Math.min(maxWidth, totalPixels);
|
|
252
|
+
logicalHeight = Math.ceil(totalPixels / logicalWidth);
|
|
253
|
+
}
|
|
254
|
+
const scale = 1;
|
|
255
|
+
const width = logicalWidth * scale;
|
|
256
|
+
const height = logicalHeight * scale;
|
|
257
|
+
const LARGE_IMAGE_PIXELS = 10000000;
|
|
258
|
+
const useManualPng = width * height > LARGE_IMAGE_PIXELS || !!process.env.ROX_FAST_PNG;
|
|
259
|
+
let raw;
|
|
260
|
+
let stride = 0;
|
|
261
|
+
if (useManualPng) {
|
|
262
|
+
stride = width * 3 + 1;
|
|
263
|
+
raw = Buffer.alloc(height * stride);
|
|
264
|
+
const flatData = Buffer.concat(dataWithMarkers);
|
|
265
|
+
const markerEndBytes = Buffer.alloc(MARKER_END.length * 3);
|
|
266
|
+
for (let i = 0; i < MARKER_END.length; i++) {
|
|
267
|
+
markerEndBytes[i * 3] = MARKER_END[i].r;
|
|
268
|
+
markerEndBytes[i * 3 + 1] = MARKER_END[i].g;
|
|
269
|
+
markerEndBytes[i * 3 + 2] = MARKER_END[i].b;
|
|
270
|
+
}
|
|
271
|
+
const totalDataBytes = logicalWidth * logicalHeight * 3;
|
|
272
|
+
const fullData = Buffer.alloc(totalDataBytes);
|
|
273
|
+
const markerStartPos = (logicalHeight - 1) * logicalWidth * 3 +
|
|
274
|
+
(logicalWidth - MARKER_END.length) * 3;
|
|
275
|
+
flatData.copy(fullData, 0, 0, Math.min(flatData.length, markerStartPos));
|
|
276
|
+
markerEndBytes.copy(fullData, markerStartPos);
|
|
277
|
+
for (let row = 0; row < height; row++) {
|
|
278
|
+
raw[row * stride] = 0;
|
|
279
|
+
fullData.copy(raw, row * stride + 1, row * width * 3, (row + 1) * width * 3);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
raw = Buffer.alloc(width * height * bytesPerPixel);
|
|
284
|
+
const flatData = Buffer.concat(dataWithMarkers);
|
|
285
|
+
flatData.copy(raw, 0, 0, Math.min(flatData.length, raw.length));
|
|
286
|
+
}
|
|
287
|
+
payload.length = 0;
|
|
288
|
+
dataWithMarkers.length = 0;
|
|
289
|
+
metaPixel.length = 0;
|
|
290
|
+
meta.length = 0;
|
|
291
|
+
paddedData.length = 0;
|
|
292
|
+
dataWithoutMarkers.length = 0;
|
|
293
|
+
if (opts.onProgress)
|
|
294
|
+
opts.onProgress({ phase: 'png_gen', loaded: 0, total: height });
|
|
295
|
+
let bufScr;
|
|
296
|
+
if (useManualPng) {
|
|
297
|
+
const bytesPerRow = width * 3;
|
|
298
|
+
const scanlinesData = Buffer.alloc(height * (1 + bytesPerRow));
|
|
299
|
+
const progressStep = Math.max(1, Math.floor(height / 20));
|
|
300
|
+
for (let row = 0; row < height; row++) {
|
|
301
|
+
scanlinesData[row * (1 + bytesPerRow)] = 0;
|
|
302
|
+
const srcStart = row * stride + 1;
|
|
303
|
+
const dstStart = row * (1 + bytesPerRow) + 1;
|
|
304
|
+
raw.copy(scanlinesData, dstStart, srcStart, srcStart + bytesPerRow);
|
|
305
|
+
if (opts.onProgress && row % progressStep === 0) {
|
|
306
|
+
opts.onProgress({ phase: 'png_gen', loaded: row, total: height });
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (opts.onProgress)
|
|
310
|
+
opts.onProgress({ phase: 'png_compress', loaded: 0, total: 100 });
|
|
311
|
+
const idatData = zlib.deflateSync(scanlinesData, {
|
|
312
|
+
level: 0,
|
|
313
|
+
memLevel: 8,
|
|
314
|
+
strategy: zlib.constants.Z_FILTERED,
|
|
315
|
+
});
|
|
316
|
+
raw = Buffer.alloc(0);
|
|
317
|
+
const ihdrData = Buffer.alloc(13);
|
|
318
|
+
ihdrData.writeUInt32BE(width, 0);
|
|
319
|
+
ihdrData.writeUInt32BE(height, 4);
|
|
320
|
+
ihdrData[8] = 8;
|
|
321
|
+
ihdrData[9] = 2;
|
|
322
|
+
ihdrData[10] = 0;
|
|
323
|
+
ihdrData[11] = 0;
|
|
324
|
+
ihdrData[12] = 0;
|
|
325
|
+
const ihdrType = Buffer.from('IHDR', 'utf8');
|
|
326
|
+
const ihdrCrc = crc32(ihdrData, crc32(ihdrType));
|
|
327
|
+
const ihdrCrcBuf = Buffer.alloc(4);
|
|
328
|
+
ihdrCrcBuf.writeUInt32BE(ihdrCrc, 0);
|
|
329
|
+
const ihdrLen = Buffer.alloc(4);
|
|
330
|
+
ihdrLen.writeUInt32BE(ihdrData.length, 0);
|
|
331
|
+
const idatType = Buffer.from('IDAT', 'utf8');
|
|
332
|
+
const idatCrc = crc32(idatData, crc32(idatType));
|
|
333
|
+
const idatCrcBuf = Buffer.alloc(4);
|
|
334
|
+
idatCrcBuf.writeUInt32BE(idatCrc, 0);
|
|
335
|
+
const idatLen = Buffer.alloc(4);
|
|
336
|
+
idatLen.writeUInt32BE(idatData.length, 0);
|
|
337
|
+
const iendType = Buffer.from('IEND', 'utf8');
|
|
338
|
+
const iendCrc = crc32(Buffer.alloc(0), crc32(iendType));
|
|
339
|
+
const iendCrcBuf = Buffer.alloc(4);
|
|
340
|
+
iendCrcBuf.writeUInt32BE(iendCrc, 0);
|
|
341
|
+
const iendLen = Buffer.alloc(4);
|
|
342
|
+
iendLen.writeUInt32BE(0, 0);
|
|
343
|
+
bufScr = Buffer.concat([
|
|
344
|
+
PNG_HEADER,
|
|
345
|
+
ihdrLen,
|
|
346
|
+
ihdrType,
|
|
347
|
+
ihdrData,
|
|
348
|
+
ihdrCrcBuf,
|
|
349
|
+
idatLen,
|
|
350
|
+
idatType,
|
|
351
|
+
idatData,
|
|
352
|
+
idatCrcBuf,
|
|
353
|
+
iendLen,
|
|
354
|
+
iendType,
|
|
355
|
+
iendCrcBuf,
|
|
356
|
+
]);
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
bufScr = await sharp(raw, {
|
|
360
|
+
raw: { width, height, channels: 3 },
|
|
361
|
+
})
|
|
362
|
+
.png({
|
|
363
|
+
compressionLevel: 3,
|
|
364
|
+
palette: false,
|
|
365
|
+
effort: 1,
|
|
366
|
+
adaptiveFiltering: false,
|
|
367
|
+
})
|
|
368
|
+
.toBuffer();
|
|
369
|
+
}
|
|
370
|
+
raw = Buffer.alloc(0);
|
|
371
|
+
if (opts.onProgress)
|
|
372
|
+
opts.onProgress({ phase: 'png_compress', loaded: 100, total: 100 });
|
|
373
|
+
if (opts.skipOptimization) {
|
|
374
|
+
progressBar?.stop();
|
|
375
|
+
return bufScr;
|
|
376
|
+
}
|
|
377
|
+
if (opts.onProgress)
|
|
378
|
+
opts.onProgress({ phase: 'optimizing', loaded: 0, total: 100 });
|
|
379
|
+
try {
|
|
380
|
+
const optimized = await optimizePngBuffer(bufScr, true);
|
|
381
|
+
if (opts.onProgress)
|
|
382
|
+
opts.onProgress({ phase: 'optimizing', loaded: 100, total: 100 });
|
|
383
|
+
progressBar?.stop();
|
|
384
|
+
return optimized;
|
|
385
|
+
}
|
|
386
|
+
catch (e) {
|
|
387
|
+
progressBar?.stop();
|
|
388
|
+
return bufScr;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|