@zapwebp/core 1.1.0 → 1.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/README.md +79 -51
- package/agent.json +180 -0
- package/package.json +9 -3
- package/src/cache.js +145 -0
- package/src/engine/decode.js +60 -0
- package/src/engine/encode.js +106 -0
- package/src/engine/pipeline.js +116 -0
- package/src/engine/transform.js +37 -0
- package/src/errors/index.js +34 -0
- package/src/formats/avif.js +3 -3
- package/src/formats/gif.js +8 -3
- package/src/formats/index.js +5 -0
- package/src/formats/jpeg.js +15 -4
- package/src/formats/png.js +3 -3
- package/src/formats/svg.js +75 -14
- package/src/formats/tiff.js +8 -3
- package/src/formats/webp.js +3 -3
- package/src/index.d.ts +286 -9
- package/src/index.js +998 -91
- package/src/journal/index.js +73 -0
- package/src/metadata/c2pa.js +3 -11
- package/src/metadata/chunk-stripper.js +235 -0
- package/src/metadata/exif.js +3 -12
- package/src/metadata/icc.js +3 -11
- package/src/metadata/png-text.js +3 -11
- package/src/metadata/registry.js +26 -3
- package/src/metadata/xmp.js +3 -17
- package/src/parallel.js +134 -0
- package/src/policy.js +95 -6
- package/src/registry/compression.js +84 -0
- package/src/registry/formats.js +30 -0
- package/src/registry/metadata.js +28 -0
- package/src/registry/transforms.js +76 -0
- package/src/report.js +404 -0
- package/src/safety/path.js +52 -0
- package/src/safety/runtime.js +66 -0
- package/src/safety/ssrf.js +9 -0
- package/src/safety/svg.js +66 -0
- package/src/signals.js +683 -0
- package/src/smart.js +351 -0
- package/src/team-policy.js +319 -0
- package/src/testing.d.ts +18 -0
- package/src/testing.js +114 -0
- package/src/tools.js +240 -0
- package/src/types.ts +68 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('node:crypto');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} JournalEntry
|
|
7
|
+
* @property {string} id - Unique transaction identifier
|
|
8
|
+
* @property {string} timestamp - ISO timestamp
|
|
9
|
+
* @property {string} inputHash - SHA-256 hash of input bytes
|
|
10
|
+
* @property {string} outputHash - SHA-256 hash of output bytes
|
|
11
|
+
* @property {string} fromFormat - Input format
|
|
12
|
+
* @property {string} toFormat - Output format
|
|
13
|
+
* @property {number} durationMs - Conversion duration in milliseconds
|
|
14
|
+
* @property {object} options - Options applied
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
class TransactionJournal {
|
|
18
|
+
constructor(limit = 100) {
|
|
19
|
+
this._entries = [];
|
|
20
|
+
this._limit = limit;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Records a conversion transaction.
|
|
25
|
+
* @param {object} params
|
|
26
|
+
* @returns {JournalEntry}
|
|
27
|
+
*/
|
|
28
|
+
record({ inputBytes, outputBytes, fromFormat, toFormat, durationMs, options = {} }) {
|
|
29
|
+
const inputHash = crypto.createHash('sha256').update(inputBytes).digest('hex');
|
|
30
|
+
const outputHash = crypto.createHash('sha256').update(outputBytes).digest('hex');
|
|
31
|
+
const entry = {
|
|
32
|
+
id: crypto.randomUUID ? crypto.randomUUID() : `tx_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
|
|
33
|
+
timestamp: new Date().toISOString(),
|
|
34
|
+
inputHash,
|
|
35
|
+
outputHash,
|
|
36
|
+
fromFormat,
|
|
37
|
+
toFormat,
|
|
38
|
+
durationMs,
|
|
39
|
+
options: { ...options },
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
this._entries.unshift(entry);
|
|
43
|
+
if (this._entries.length > this._limit) {
|
|
44
|
+
this._entries.pop();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return entry;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Retrieves recent transaction entries.
|
|
52
|
+
* @param {number} [count]
|
|
53
|
+
* @returns {JournalEntry[]}
|
|
54
|
+
*/
|
|
55
|
+
getEntries(count) {
|
|
56
|
+
return count ? this._entries.slice(0, count) : [...this._entries];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Clears the in-memory journal.
|
|
61
|
+
*/
|
|
62
|
+
clear() {
|
|
63
|
+
this._entries = [];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const defaultJournal = new TransactionJournal();
|
|
68
|
+
|
|
69
|
+
module.exports = {
|
|
70
|
+
TransactionJournal,
|
|
71
|
+
defaultJournal,
|
|
72
|
+
};
|
|
73
|
+
|
package/src/metadata/c2pa.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const sharp = require('sharp');
|
|
4
|
-
|
|
5
3
|
/** @type {import('./registry').MetadataHandler} */
|
|
6
4
|
const c2paHandler = {
|
|
7
5
|
name: 'c2pa',
|
|
@@ -16,15 +14,9 @@ const c2paHandler = {
|
|
|
16
14
|
return fields;
|
|
17
15
|
},
|
|
18
16
|
|
|
19
|
-
async strip(bytes) {
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
const meta = await sharp(data).metadata();
|
|
23
|
-
if (!meta.format) return data;
|
|
24
|
-
return await sharp(data).toBuffer();
|
|
25
|
-
} catch {
|
|
26
|
-
return data;
|
|
27
|
-
}
|
|
17
|
+
async strip(bytes, options = {}) {
|
|
18
|
+
const { stripContainerMetadata } = require('./chunk-stripper');
|
|
19
|
+
return stripContainerMetadata(bytes, options);
|
|
28
20
|
},
|
|
29
21
|
};
|
|
30
22
|
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Non-destructive metadata chunk stripping for JPEG, WebP, PNG, and SVG containers.
|
|
5
|
+
* Manipulates binary container structures directly without pixel decompression or re-encoding.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Strips metadata segments (APP1, APP2, APP13, COM) from JPEG containers.
|
|
10
|
+
* Preserves SOI, JFIF (APP0), Adobe (APP14), DQT, DHT, SOF, and entropy scan data (SOS).
|
|
11
|
+
*
|
|
12
|
+
* @param {Buffer} buf
|
|
13
|
+
* @param {object} [options]
|
|
14
|
+
* @param {boolean} [options.keepIcc=false]
|
|
15
|
+
* @returns {Buffer}
|
|
16
|
+
*/
|
|
17
|
+
function stripJpegMetadata(buf, { keepIcc = false } = {}) {
|
|
18
|
+
if (!Buffer.isBuffer(buf) || buf.length < 4 || buf[0] !== 0xff || buf[1] !== 0xd8) {
|
|
19
|
+
return buf;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const chunks = [buf.subarray(0, 2)]; // Keep SOI
|
|
23
|
+
let offset = 2;
|
|
24
|
+
|
|
25
|
+
while (offset < buf.length) {
|
|
26
|
+
if (buf[offset] !== 0xff) break;
|
|
27
|
+
while (buf[offset] === 0xff) offset++; // Skip fill bytes
|
|
28
|
+
if (offset >= buf.length) break;
|
|
29
|
+
|
|
30
|
+
const marker = buf[offset++];
|
|
31
|
+
if (marker === 0xd8) continue; // SOI
|
|
32
|
+
|
|
33
|
+
if (marker === 0xd9) { // EOI
|
|
34
|
+
chunks.push(Buffer.from([0xff, 0xd9]));
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (marker === 0xda) { // SOS (Start of Scan) - remainder is compressed entropy stream
|
|
39
|
+
chunks.push(buf.subarray(offset - 2));
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (offset + 2 > buf.length) break;
|
|
44
|
+
const length = buf.readUInt16BE(offset);
|
|
45
|
+
const segEnd = offset + length;
|
|
46
|
+
if (segEnd > buf.length) break;
|
|
47
|
+
|
|
48
|
+
let drop = false;
|
|
49
|
+
// APP1 (EXIF / XMP)
|
|
50
|
+
if (marker === 0xe1) {
|
|
51
|
+
drop = true;
|
|
52
|
+
}
|
|
53
|
+
// APP2 (ICC Profile)
|
|
54
|
+
else if (marker === 0xe2 && !keepIcc) {
|
|
55
|
+
drop = true;
|
|
56
|
+
}
|
|
57
|
+
// APP13 (Photoshop IRB / IPTC)
|
|
58
|
+
else if (marker === 0xed) {
|
|
59
|
+
drop = true;
|
|
60
|
+
}
|
|
61
|
+
// COM (Comment)
|
|
62
|
+
else if (marker === 0xfe) {
|
|
63
|
+
drop = true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!drop) {
|
|
67
|
+
chunks.push(buf.subarray(offset - 2, segEnd));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
offset = segEnd;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return Buffer.concat(chunks);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Strips metadata chunks (EXIF, XMP, ICCP) from WebP RIFF containers.
|
|
78
|
+
* Preserves VP8/VP8L pixel payload and updates VP8X header flags.
|
|
79
|
+
*
|
|
80
|
+
* @param {Buffer} buf
|
|
81
|
+
* @param {object} [options]
|
|
82
|
+
* @param {boolean} [options.keepIcc=false]
|
|
83
|
+
* @returns {Buffer}
|
|
84
|
+
*/
|
|
85
|
+
function stripWebpMetadata(buf, { keepIcc = false } = {}) {
|
|
86
|
+
if (
|
|
87
|
+
!Buffer.isBuffer(buf) ||
|
|
88
|
+
buf.length < 12 ||
|
|
89
|
+
buf.toString('ascii', 0, 4) !== 'RIFF' ||
|
|
90
|
+
buf.toString('ascii', 8, 12) !== 'WEBP'
|
|
91
|
+
) {
|
|
92
|
+
return buf;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const keptChunks = [];
|
|
96
|
+
let offset = 12;
|
|
97
|
+
|
|
98
|
+
while (offset + 8 <= buf.length) {
|
|
99
|
+
const fourcc = buf.toString('ascii', offset, offset + 4);
|
|
100
|
+
const chunkSize = buf.readUInt32LE(offset + 4);
|
|
101
|
+
const paddedSize = chunkSize + (chunkSize & 1);
|
|
102
|
+
const chunkEnd = offset + 8 + paddedSize;
|
|
103
|
+
if (chunkEnd > buf.length) break;
|
|
104
|
+
|
|
105
|
+
if (fourcc === 'EXIF' || fourcc === 'XMP ' || (fourcc === 'ICCP' && !keepIcc)) {
|
|
106
|
+
// Drop metadata chunk
|
|
107
|
+
} else if (fourcc === 'VP8X') {
|
|
108
|
+
const vp8xChunk = Buffer.from(buf.subarray(offset, chunkEnd));
|
|
109
|
+
// Byte 8 within chunk is flags (offset 0-3: VP8X, 4-7: size, 8: flags)
|
|
110
|
+
if (vp8xChunk.length > 8) {
|
|
111
|
+
let flags = vp8xChunk[8];
|
|
112
|
+
flags &= ~(1 << 3); // Clear XMP (bit 3)
|
|
113
|
+
flags &= ~(1 << 2); // Clear EXIF (bit 2)
|
|
114
|
+
if (!keepIcc) flags &= ~(1 << 0); // Clear ICCP (bit 0)
|
|
115
|
+
vp8xChunk[8] = flags;
|
|
116
|
+
}
|
|
117
|
+
keptChunks.push(vp8xChunk);
|
|
118
|
+
} else {
|
|
119
|
+
keptChunks.push(buf.subarray(offset, chunkEnd));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
offset = chunkEnd;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const payload = Buffer.concat(keptChunks);
|
|
126
|
+
const header = Buffer.alloc(12);
|
|
127
|
+
header.write('RIFF', 0, 4, 'ascii');
|
|
128
|
+
header.writeUInt32LE(payload.length + 4, 4);
|
|
129
|
+
header.write('WEBP', 8, 4, 'ascii');
|
|
130
|
+
return Buffer.concat([header, payload]);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Strips metadata chunks (eXIf, tEXt, iTXt, zTXt, iCCP) from PNG containers.
|
|
135
|
+
* Preserves IHDR, PLTE, IDAT, tRNS, and IEND.
|
|
136
|
+
*
|
|
137
|
+
* @param {Buffer} buf
|
|
138
|
+
* @param {object} [options]
|
|
139
|
+
* @param {boolean} [options.keepIcc=false]
|
|
140
|
+
* @returns {Buffer}
|
|
141
|
+
*/
|
|
142
|
+
function stripPngMetadata(buf, { keepIcc = false } = {}) {
|
|
143
|
+
const pngSig = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
144
|
+
if (!Buffer.isBuffer(buf) || buf.length < 8 || !buf.subarray(0, 8).equals(pngSig)) {
|
|
145
|
+
return buf;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const kept = [buf.subarray(0, 8)];
|
|
149
|
+
let offset = 8;
|
|
150
|
+
|
|
151
|
+
while (offset + 12 <= buf.length) {
|
|
152
|
+
const length = buf.readUInt32BE(offset);
|
|
153
|
+
const type = buf.toString('ascii', offset + 4, offset + 8);
|
|
154
|
+
const chunkEnd = offset + 12 + length;
|
|
155
|
+
if (chunkEnd > buf.length) break;
|
|
156
|
+
|
|
157
|
+
if (
|
|
158
|
+
type === 'eXIf' ||
|
|
159
|
+
type === 'tEXt' ||
|
|
160
|
+
type === 'iTXt' ||
|
|
161
|
+
type === 'zTXt' ||
|
|
162
|
+
type === 'tIME' ||
|
|
163
|
+
type === 'pHYs' ||
|
|
164
|
+
(type === 'iCCP' && !keepIcc)
|
|
165
|
+
) {
|
|
166
|
+
// Drop metadata chunk
|
|
167
|
+
} else {
|
|
168
|
+
kept.push(buf.subarray(offset, chunkEnd));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
offset = chunkEnd;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return Buffer.concat(kept);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Strips XML metadata blocks from SVG text buffers.
|
|
179
|
+
*
|
|
180
|
+
* @param {Buffer} buf
|
|
181
|
+
* @returns {Buffer}
|
|
182
|
+
*/
|
|
183
|
+
function stripSvgMetadata(buf) {
|
|
184
|
+
if (!Buffer.isBuffer(buf)) return buf;
|
|
185
|
+
const str = buf.toString('utf8');
|
|
186
|
+
if (!str.includes('<svg')) return buf;
|
|
187
|
+
|
|
188
|
+
const cleaned = str
|
|
189
|
+
.replace(/<metadata[\s\S]*?<\/metadata>/gi, '')
|
|
190
|
+
.replace(/<rdf:RDF[\s\S]*?<\/rdf:RDF>/gi, '');
|
|
191
|
+
|
|
192
|
+
return Buffer.from(cleaned, 'utf8');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Strips container metadata non-destructively for any supported image format.
|
|
197
|
+
*
|
|
198
|
+
* @param {Buffer|Uint8Array} bytes
|
|
199
|
+
* @param {object} [options]
|
|
200
|
+
* @returns {Buffer}
|
|
201
|
+
*/
|
|
202
|
+
function stripContainerMetadata(bytes, options = {}) {
|
|
203
|
+
const buf = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
|
|
204
|
+
const keepIcc = Array.isArray(options.keep) && options.keep.map((k) => String(k).toLowerCase()).includes('icc');
|
|
205
|
+
|
|
206
|
+
// JPEG
|
|
207
|
+
if (buf.length >= 4 && buf[0] === 0xff && buf[1] === 0xd8) {
|
|
208
|
+
return stripJpegMetadata(buf, { keepIcc });
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// WebP
|
|
212
|
+
if (buf.length >= 12 && buf.toString('ascii', 0, 4) === 'RIFF' && buf.toString('ascii', 8, 12) === 'WEBP') {
|
|
213
|
+
return stripWebpMetadata(buf, { keepIcc });
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// PNG
|
|
217
|
+
if (buf.length >= 8 && buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47) {
|
|
218
|
+
return stripPngMetadata(buf, { keepIcc });
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// SVG
|
|
222
|
+
if (buf.subarray(0, 1024).toString('utf8').includes('<svg')) {
|
|
223
|
+
return stripSvgMetadata(buf);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return buf;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
module.exports = {
|
|
230
|
+
stripJpegMetadata,
|
|
231
|
+
stripWebpMetadata,
|
|
232
|
+
stripPngMetadata,
|
|
233
|
+
stripSvgMetadata,
|
|
234
|
+
stripContainerMetadata,
|
|
235
|
+
};
|
package/src/metadata/exif.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const sharp = require('sharp');
|
|
4
|
-
|
|
5
3
|
/** @type {import('./registry').MetadataHandler} */
|
|
6
4
|
const exifHandler = {
|
|
7
5
|
name: 'exif',
|
|
@@ -30,16 +28,9 @@ const exifHandler = {
|
|
|
30
28
|
return fields;
|
|
31
29
|
},
|
|
32
30
|
|
|
33
|
-
async strip(bytes) {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
// Re-encode via sharp without metadata to strip EXIF
|
|
37
|
-
const meta = await sharp(data).metadata();
|
|
38
|
-
if (!meta.format) return data;
|
|
39
|
-
return await sharp(data).toBuffer();
|
|
40
|
-
} catch {
|
|
41
|
-
return data;
|
|
42
|
-
}
|
|
31
|
+
async strip(bytes, options = {}) {
|
|
32
|
+
const { stripContainerMetadata } = require('./chunk-stripper');
|
|
33
|
+
return stripContainerMetadata(bytes, options);
|
|
43
34
|
},
|
|
44
35
|
};
|
|
45
36
|
|
package/src/metadata/icc.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const sharp = require('sharp');
|
|
4
|
-
|
|
5
3
|
/** @type {import('./registry').MetadataHandler} */
|
|
6
4
|
const iccHandler = {
|
|
7
5
|
name: 'icc',
|
|
@@ -16,15 +14,9 @@ const iccHandler = {
|
|
|
16
14
|
return fields;
|
|
17
15
|
},
|
|
18
16
|
|
|
19
|
-
async strip(bytes) {
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
const meta = await sharp(data).metadata();
|
|
23
|
-
if (!meta.format) return data;
|
|
24
|
-
return await sharp(data).toBuffer();
|
|
25
|
-
} catch {
|
|
26
|
-
return data;
|
|
27
|
-
}
|
|
17
|
+
async strip(bytes, options = {}) {
|
|
18
|
+
const { stripContainerMetadata } = require('./chunk-stripper');
|
|
19
|
+
return stripContainerMetadata(bytes, options);
|
|
28
20
|
},
|
|
29
21
|
};
|
|
30
22
|
|
package/src/metadata/png-text.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const sharp = require('sharp');
|
|
4
|
-
|
|
5
3
|
/** @type {import('./registry').MetadataHandler} */
|
|
6
4
|
const pngTextHandler = {
|
|
7
5
|
name: 'png-text',
|
|
@@ -18,15 +16,9 @@ const pngTextHandler = {
|
|
|
18
16
|
return fields;
|
|
19
17
|
},
|
|
20
18
|
|
|
21
|
-
async strip(bytes) {
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
const meta = await sharp(data).metadata();
|
|
25
|
-
if (!meta.format) return data;
|
|
26
|
-
return await sharp(data).toBuffer();
|
|
27
|
-
} catch {
|
|
28
|
-
return data;
|
|
29
|
-
}
|
|
19
|
+
async strip(bytes, options = {}) {
|
|
20
|
+
const { stripContainerMetadata } = require('./chunk-stripper');
|
|
21
|
+
return stripContainerMetadata(bytes, options);
|
|
30
22
|
},
|
|
31
23
|
};
|
|
32
24
|
|
package/src/metadata/registry.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const sharp = require('sharp');
|
|
4
3
|
const { ZapwebpError } = require('@zapwebp/shared');
|
|
4
|
+
const { stripContainerMetadata } = require('./chunk-stripper');
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @typedef {Object} MetadataField
|
|
@@ -116,7 +116,19 @@ class MetadataRegistry {
|
|
|
116
116
|
|
|
117
117
|
// 2. Strip pass
|
|
118
118
|
let current = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
|
|
119
|
-
|
|
119
|
+
|
|
120
|
+
// Single-pass optimization for standard raster metadata (EXIF, XMP, ICC, C2PA, PNG-text)
|
|
121
|
+
const standardNames = ['exif', 'xmp', 'c2pa', 'icc', 'png-text'];
|
|
122
|
+
const activeStandard = activeHandlers.filter((h) => standardNames.includes(h.name.toLowerCase()));
|
|
123
|
+
const activeCustom = activeHandlers.filter((h) => !standardNames.includes(h.name.toLowerCase()));
|
|
124
|
+
|
|
125
|
+
if (activeStandard.length > 0) {
|
|
126
|
+
// Non-destructive chunk stripping directly on the binary container
|
|
127
|
+
// Preserves original compressed pixel streams (zero generation loss, no sharp toBuffer)
|
|
128
|
+
current = stripContainerMetadata(current, options);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
for (const handler of activeCustom) {
|
|
120
132
|
try {
|
|
121
133
|
const stripped = await handler.strip(current, options);
|
|
122
134
|
current = Buffer.isBuffer(stripped) ? stripped : Buffer.from(stripped);
|
|
@@ -132,6 +144,7 @@ class MetadataRegistry {
|
|
|
132
144
|
try {
|
|
133
145
|
if (current.length >= 16) {
|
|
134
146
|
// Check raster parseability
|
|
147
|
+
const sharp = require('sharp');
|
|
135
148
|
await sharp(current, { failOn: 'none' }).metadata();
|
|
136
149
|
}
|
|
137
150
|
} catch {
|
|
@@ -147,7 +160,17 @@ class MetadataRegistry {
|
|
|
147
160
|
|
|
148
161
|
// 4. Zero-Trust Verification Pass: Re-inspect to assert metadata is gone
|
|
149
162
|
for (const handler of activeHandlers) {
|
|
150
|
-
|
|
163
|
+
let remaining;
|
|
164
|
+
try {
|
|
165
|
+
remaining = await handler.inspect(current);
|
|
166
|
+
} catch (err) {
|
|
167
|
+
throw new ZapwebpError({
|
|
168
|
+
code: 'E_CLEAN_FAILED',
|
|
169
|
+
message: `Zero-Trust verification failed: Handler "${handler.name}" crashed during post-clean inspection: ${err.message}`,
|
|
170
|
+
fix: 'Report this bug to the metadata handler author.',
|
|
171
|
+
cause: err,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
151
174
|
if (Array.isArray(remaining) && remaining.length > 0) {
|
|
152
175
|
throw new ZapwebpError({
|
|
153
176
|
code: 'E_CLEAN_FAILED',
|
package/src/metadata/xmp.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const sharp = require('sharp');
|
|
4
|
-
|
|
5
3
|
/** @type {import('./registry').MetadataHandler} */
|
|
6
4
|
const xmpHandler = {
|
|
7
5
|
name: 'xmp',
|
|
@@ -23,21 +21,9 @@ const xmpHandler = {
|
|
|
23
21
|
return fields;
|
|
24
22
|
},
|
|
25
23
|
|
|
26
|
-
async strip(bytes) {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
if (text.includes('<svg')) {
|
|
30
|
-
// Strip <metadata> and rdf:RDF from SVG
|
|
31
|
-
const cleaned = text.replace(/<metadata[\s\S]*?<\/metadata>/gi, '').replace(/<rdf:RDF[\s\S]*?<\/rdf:RDF>/gi, '');
|
|
32
|
-
return Buffer.from(cleaned, 'utf8');
|
|
33
|
-
}
|
|
34
|
-
try {
|
|
35
|
-
const meta = await sharp(data).metadata();
|
|
36
|
-
if (!meta.format) return data;
|
|
37
|
-
return await sharp(data).toBuffer();
|
|
38
|
-
} catch {
|
|
39
|
-
return data;
|
|
40
|
-
}
|
|
24
|
+
async strip(bytes, options = {}) {
|
|
25
|
+
const { stripContainerMetadata } = require('./chunk-stripper');
|
|
26
|
+
return stripContainerMetadata(bytes, options);
|
|
41
27
|
},
|
|
42
28
|
};
|
|
43
29
|
|
package/src/parallel.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const os = require('node:os');
|
|
4
|
+
const { ZapwebpError } = require('@zapwebp/shared');
|
|
5
|
+
|
|
6
|
+
const DEFAULT_CONCURRENCY = Math.max(1, Math.min(os.cpus()?.length || 4, 8));
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Concurrently processes an array of items with a fixed concurrency worker pool.
|
|
10
|
+
* Prevents memory thrashing while maximizing multi-core throughput.
|
|
11
|
+
* Stops all workers immediately on first error or abort signal to prevent resource leaks.
|
|
12
|
+
*
|
|
13
|
+
* @template T, R
|
|
14
|
+
* @param {T[]} items - Array of input items to process
|
|
15
|
+
* @param {number} [concurrency] - Max simultaneous active promises
|
|
16
|
+
* @param {(item: T, index: number) => Promise<R>} workerFn - Async function processing each item
|
|
17
|
+
* @param {object} [options]
|
|
18
|
+
* @param {AbortSignal} [options.signal] - Optional AbortSignal for external cancellation
|
|
19
|
+
* @returns {Promise<R[]>}
|
|
20
|
+
*/
|
|
21
|
+
async function parallelMap(items, concurrency = DEFAULT_CONCURRENCY, workerFn, options = {}) {
|
|
22
|
+
let actualConcurrency = concurrency;
|
|
23
|
+
let actualWorkerFn = workerFn;
|
|
24
|
+
let actualOptions = options;
|
|
25
|
+
|
|
26
|
+
if (typeof concurrency === 'function') {
|
|
27
|
+
actualWorkerFn = concurrency;
|
|
28
|
+
actualConcurrency = DEFAULT_CONCURRENCY;
|
|
29
|
+
actualOptions = typeof workerFn === 'object' && workerFn !== null ? workerFn : {};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!Array.isArray(items) || items.length === 0) return [];
|
|
33
|
+
if (typeof actualWorkerFn !== 'function') {
|
|
34
|
+
throw new TypeError('parallelMap workerFn must be a function.');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const signal = actualOptions?.signal;
|
|
38
|
+
if (signal?.aborted) {
|
|
39
|
+
throw new ZapwebpError({
|
|
40
|
+
code: 'E_ABORTED',
|
|
41
|
+
message: 'Operation aborted.',
|
|
42
|
+
fix: 'Retry the operation when ready.',
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const sanitizedConcurrency = Number.isFinite(actualConcurrency) && actualConcurrency > 0
|
|
47
|
+
? Math.floor(actualConcurrency)
|
|
48
|
+
: DEFAULT_CONCURRENCY;
|
|
49
|
+
const limit = Math.max(1, Math.min(sanitizedConcurrency, items.length));
|
|
50
|
+
const results = new Array(items.length);
|
|
51
|
+
let currentIndex = 0;
|
|
52
|
+
let aborted = false;
|
|
53
|
+
let firstError = null;
|
|
54
|
+
|
|
55
|
+
const onAbort = () => {
|
|
56
|
+
aborted = true;
|
|
57
|
+
};
|
|
58
|
+
if (signal) {
|
|
59
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function worker() {
|
|
63
|
+
while (!aborted && !signal?.aborted && currentIndex < items.length) {
|
|
64
|
+
const index = currentIndex++;
|
|
65
|
+
try {
|
|
66
|
+
results[index] = await actualWorkerFn(items[index], index);
|
|
67
|
+
} catch (err) {
|
|
68
|
+
if (!firstError) firstError = err;
|
|
69
|
+
aborted = true;
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const workers = Array.from({ length: limit }, () => worker().catch(() => {}));
|
|
77
|
+
await Promise.all(workers);
|
|
78
|
+
} finally {
|
|
79
|
+
if (signal) {
|
|
80
|
+
signal.removeEventListener('abort', onAbort);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (firstError) throw firstError;
|
|
85
|
+
if (signal?.aborted) {
|
|
86
|
+
throw new ZapwebpError({
|
|
87
|
+
code: 'E_ABORTED',
|
|
88
|
+
message: 'Operation aborted.',
|
|
89
|
+
fix: 'Retry the operation when ready.',
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return results;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* High-performance batch conversion manager.
|
|
97
|
+
*/
|
|
98
|
+
class ConversionPool {
|
|
99
|
+
/**
|
|
100
|
+
* @param {object} [options]
|
|
101
|
+
* @param {number} [options.concurrency]
|
|
102
|
+
*/
|
|
103
|
+
constructor(options = {}) {
|
|
104
|
+
this.concurrency = options.concurrency || DEFAULT_CONCURRENCY;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Processes a batch of inputs concurrently through a conversion function.
|
|
109
|
+
*
|
|
110
|
+
* @param {Array<any>} inputs
|
|
111
|
+
* @param {object} options
|
|
112
|
+
* @param {Function} convertFn
|
|
113
|
+
* @returns {Promise<Array<any>>}
|
|
114
|
+
*/
|
|
115
|
+
async processBatch(inputs, options, convertFn) {
|
|
116
|
+
return parallelMap(inputs, this.concurrency, async (input, idx) => {
|
|
117
|
+
try {
|
|
118
|
+
const res = await convertFn(input, options);
|
|
119
|
+
return { success: true, index: idx, output: res };
|
|
120
|
+
} catch (err) {
|
|
121
|
+
return { success: false, index: idx, error: err };
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const defaultPool = new ConversionPool();
|
|
128
|
+
|
|
129
|
+
module.exports = {
|
|
130
|
+
parallelMap,
|
|
131
|
+
ConversionPool,
|
|
132
|
+
defaultPool,
|
|
133
|
+
DEFAULT_CONCURRENCY,
|
|
134
|
+
};
|