@zapwebp/core 1.1.1 → 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.
Files changed (45) hide show
  1. package/README.md +79 -51
  2. package/agent.json +180 -0
  3. package/package.json +8 -2
  4. package/src/cache.js +145 -0
  5. package/src/engine/decode.js +60 -0
  6. package/src/engine/encode.js +106 -0
  7. package/src/engine/pipeline.js +116 -0
  8. package/src/engine/transform.js +37 -0
  9. package/src/errors/index.js +34 -0
  10. package/src/formats/avif.js +1 -1
  11. package/src/formats/gif.js +8 -2
  12. package/src/formats/index.js +5 -0
  13. package/src/formats/jpeg.js +14 -3
  14. package/src/formats/png.js +1 -1
  15. package/src/formats/svg.js +76 -14
  16. package/src/formats/tiff.js +8 -2
  17. package/src/formats/webp.js +1 -1
  18. package/src/index.d.ts +286 -9
  19. package/src/index.js +951 -80
  20. package/src/journal/index.js +73 -0
  21. package/src/metadata/c2pa.js +3 -10
  22. package/src/metadata/chunk-stripper.js +235 -0
  23. package/src/metadata/exif.js +3 -11
  24. package/src/metadata/icc.js +3 -10
  25. package/src/metadata/png-text.js +3 -10
  26. package/src/metadata/registry.js +25 -2
  27. package/src/metadata/xmp.js +3 -16
  28. package/src/parallel.js +134 -0
  29. package/src/policy.js +95 -6
  30. package/src/registry/compression.js +84 -0
  31. package/src/registry/formats.js +30 -0
  32. package/src/registry/metadata.js +28 -0
  33. package/src/registry/transforms.js +76 -0
  34. package/src/report.js +404 -0
  35. package/src/safety/path.js +52 -0
  36. package/src/safety/runtime.js +66 -0
  37. package/src/safety/ssrf.js +9 -0
  38. package/src/safety/svg.js +66 -0
  39. package/src/signals.js +683 -0
  40. package/src/smart.js +351 -0
  41. package/src/team-policy.js +319 -0
  42. package/src/testing.d.ts +18 -0
  43. package/src/testing.js +114 -0
  44. package/src/tools.js +240 -0
  45. 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
+
@@ -14,16 +14,9 @@ const c2paHandler = {
14
14
  return fields;
15
15
  },
16
16
 
17
- async strip(bytes) {
18
- const data = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
19
- try {
20
- const sharp = require('sharp');
21
- const meta = await sharp(data).metadata();
22
- if (!meta.format) return data;
23
- return await sharp(data).toBuffer();
24
- } catch {
25
- return data;
26
- }
17
+ async strip(bytes, options = {}) {
18
+ const { stripContainerMetadata } = require('./chunk-stripper');
19
+ return stripContainerMetadata(bytes, options);
27
20
  },
28
21
  };
29
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
+ };
@@ -28,17 +28,9 @@ const exifHandler = {
28
28
  return fields;
29
29
  },
30
30
 
31
- async strip(bytes) {
32
- const data = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
33
- try {
34
- const sharp = require('sharp');
35
- // Re-encode via sharp without metadata to strip EXIF
36
- const meta = await sharp(data).metadata();
37
- if (!meta.format) return data;
38
- return await sharp(data).toBuffer();
39
- } catch {
40
- return data;
41
- }
31
+ async strip(bytes, options = {}) {
32
+ const { stripContainerMetadata } = require('./chunk-stripper');
33
+ return stripContainerMetadata(bytes, options);
42
34
  },
43
35
  };
44
36
 
@@ -14,16 +14,9 @@ const iccHandler = {
14
14
  return fields;
15
15
  },
16
16
 
17
- async strip(bytes) {
18
- const data = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
19
- try {
20
- const sharp = require('sharp');
21
- const meta = await sharp(data).metadata();
22
- if (!meta.format) return data;
23
- return await sharp(data).toBuffer();
24
- } catch {
25
- return data;
26
- }
17
+ async strip(bytes, options = {}) {
18
+ const { stripContainerMetadata } = require('./chunk-stripper');
19
+ return stripContainerMetadata(bytes, options);
27
20
  },
28
21
  };
29
22
 
@@ -16,16 +16,9 @@ const pngTextHandler = {
16
16
  return fields;
17
17
  },
18
18
 
19
- async strip(bytes) {
20
- const data = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
21
- try {
22
- const sharp = require('sharp');
23
- const meta = await sharp(data).metadata();
24
- if (!meta.format) return data;
25
- return await sharp(data).toBuffer();
26
- } catch {
27
- return data;
28
- }
19
+ async strip(bytes, options = {}) {
20
+ const { stripContainerMetadata } = require('./chunk-stripper');
21
+ return stripContainerMetadata(bytes, options);
29
22
  },
30
23
  };
31
24
 
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const { ZapwebpError } = require('@zapwebp/shared');
4
+ const { stripContainerMetadata } = require('./chunk-stripper');
4
5
 
5
6
  /**
6
7
  * @typedef {Object} MetadataField
@@ -115,7 +116,19 @@ class MetadataRegistry {
115
116
 
116
117
  // 2. Strip pass
117
118
  let current = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
118
- for (const handler of activeHandlers) {
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) {
119
132
  try {
120
133
  const stripped = await handler.strip(current, options);
121
134
  current = Buffer.isBuffer(stripped) ? stripped : Buffer.from(stripped);
@@ -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
- const remaining = await handler.inspect(current).catch(() => []);
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',
@@ -21,22 +21,9 @@ const xmpHandler = {
21
21
  return fields;
22
22
  },
23
23
 
24
- async strip(bytes) {
25
- const data = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
26
- const text = data.toString('utf8');
27
- if (text.includes('<svg')) {
28
- // Strip <metadata> and rdf:RDF from SVG
29
- const cleaned = text.replace(/<metadata[\s\S]*?<\/metadata>/gi, '').replace(/<rdf:RDF[\s\S]*?<\/rdf:RDF>/gi, '');
30
- return Buffer.from(cleaned, 'utf8');
31
- }
32
- try {
33
- const sharp = require('sharp');
34
- const meta = await sharp(data).metadata();
35
- if (!meta.format) return data;
36
- return await sharp(data).toBuffer();
37
- } catch {
38
- return data;
39
- }
24
+ async strip(bytes, options = {}) {
25
+ const { stripContainerMetadata } = require('./chunk-stripper');
26
+ return stripContainerMetadata(bytes, options);
40
27
  },
41
28
  };
42
29
 
@@ -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
+ };