@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
package/src/testing.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const sharp = require('sharp');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a valid SVG image buffer for unit and integration testing.
|
|
7
|
+
*
|
|
8
|
+
* @param {object} [options]
|
|
9
|
+
* @param {number} [options.width=100]
|
|
10
|
+
* @param {number} [options.height=100]
|
|
11
|
+
* @param {boolean} [options.transparent=true]
|
|
12
|
+
* @param {string} [options.fill='#8dc63f']
|
|
13
|
+
* @param {boolean} [options.withMetadata=false]
|
|
14
|
+
* @returns {Buffer}
|
|
15
|
+
*/
|
|
16
|
+
function createMockSvg(options = {}) {
|
|
17
|
+
const width = options.width || 100;
|
|
18
|
+
const height = options.height || 100;
|
|
19
|
+
const fill = options.fill || '#8dc63f';
|
|
20
|
+
const transparent = options.transparent !== false;
|
|
21
|
+
|
|
22
|
+
const bgRect = transparent
|
|
23
|
+
? ''
|
|
24
|
+
: `<rect width="${width}" height="${height}" fill="#ffffff"/>`;
|
|
25
|
+
|
|
26
|
+
const metadataBlock = options.withMetadata
|
|
27
|
+
? `<metadata>
|
|
28
|
+
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/">
|
|
29
|
+
<rdf:Description>
|
|
30
|
+
<dc:creator>Test Suite</dc:creator>
|
|
31
|
+
<dc:title>Mock SVG Asset</dc:title>
|
|
32
|
+
</rdf:Description>
|
|
33
|
+
</rdf:RDF>
|
|
34
|
+
</metadata>`
|
|
35
|
+
: '';
|
|
36
|
+
|
|
37
|
+
const svgXml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
38
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}">
|
|
39
|
+
${metadataBlock}
|
|
40
|
+
${bgRect}
|
|
41
|
+
<circle cx="${Math.round(width / 2)}" cy="${Math.round(height / 2)}" r="${Math.round(Math.min(width, height) / 3)}" fill="${fill}"/>
|
|
42
|
+
</svg>`;
|
|
43
|
+
|
|
44
|
+
return Buffer.from(svgXml, 'utf8');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Creates a valid raster or vector image buffer for unit testing.
|
|
49
|
+
*
|
|
50
|
+
* @param {object} [options]
|
|
51
|
+
* @param {'svg'|'png'|'webp'|'jpeg'|'avif'} [options.format='png']
|
|
52
|
+
* @param {number} [options.width=64]
|
|
53
|
+
* @param {number} [options.height=64]
|
|
54
|
+
* @param {boolean} [options.transparent=true]
|
|
55
|
+
* @param {boolean} [options.withMetadata=false]
|
|
56
|
+
* @returns {Promise<Buffer>}
|
|
57
|
+
*/
|
|
58
|
+
async function createMockImage(options = {}) {
|
|
59
|
+
const format = (options.format || 'png').toLowerCase();
|
|
60
|
+
const width = options.width || 64;
|
|
61
|
+
const height = options.height || 64;
|
|
62
|
+
const transparent = options.transparent !== false && format !== 'jpeg';
|
|
63
|
+
|
|
64
|
+
if (format === 'svg') {
|
|
65
|
+
return createMockSvg({
|
|
66
|
+
width,
|
|
67
|
+
height,
|
|
68
|
+
transparent,
|
|
69
|
+
withMetadata: options.withMetadata,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const channels = transparent ? 4 : 3;
|
|
74
|
+
const background = transparent
|
|
75
|
+
? { r: 141, g: 198, b: 63, alpha: 0.8 }
|
|
76
|
+
: { r: 141, g: 198, b: 63 };
|
|
77
|
+
|
|
78
|
+
let pipeline = sharp({
|
|
79
|
+
create: {
|
|
80
|
+
width,
|
|
81
|
+
height,
|
|
82
|
+
channels,
|
|
83
|
+
background,
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
if (format === 'png') {
|
|
88
|
+
pipeline = pipeline.png();
|
|
89
|
+
} else if (format === 'webp') {
|
|
90
|
+
pipeline = pipeline.webp();
|
|
91
|
+
} else if (format === 'jpeg' || format === 'jpg') {
|
|
92
|
+
pipeline = pipeline.jpeg();
|
|
93
|
+
} else if (format === 'avif') {
|
|
94
|
+
pipeline = pipeline.avif();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (options.withMetadata) {
|
|
98
|
+
pipeline = pipeline.withMetadata({
|
|
99
|
+
exif: {
|
|
100
|
+
IFD0: {
|
|
101
|
+
Artist: 'Zapwebp Test Suite',
|
|
102
|
+
Software: 'Zapwebp Mock Generator',
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return pipeline.toBuffer();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
module.exports = {
|
|
112
|
+
createMockSvg,
|
|
113
|
+
createMockImage,
|
|
114
|
+
};
|
package/src/tools.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
const fs = require('node:fs');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Returns typed MCP-compliant tool definitions for runtime discovery.
|
|
8
|
+
* Can be consumed by MCP servers, AI agents, SDK consumers, and IDE extensions.
|
|
9
|
+
*
|
|
10
|
+
* @returns {Array<{ name: string, description: string, inputSchema: object }>}
|
|
11
|
+
*/
|
|
12
|
+
function listTools() {
|
|
13
|
+
return [
|
|
14
|
+
{
|
|
15
|
+
name: 'convert_image',
|
|
16
|
+
description: 'Convert an image while preserving transparency and stripping privacy metadata by default.',
|
|
17
|
+
inputSchema: {
|
|
18
|
+
type: 'object',
|
|
19
|
+
properties: {
|
|
20
|
+
input: {
|
|
21
|
+
type: 'string',
|
|
22
|
+
description: 'Image file path, remote HTTP URL, base64 data, or SVG markup',
|
|
23
|
+
},
|
|
24
|
+
to: {
|
|
25
|
+
type: 'string',
|
|
26
|
+
enum: ['webp', 'avif', 'png', 'jpeg'],
|
|
27
|
+
default: 'webp',
|
|
28
|
+
description: 'Target format',
|
|
29
|
+
},
|
|
30
|
+
quality: {
|
|
31
|
+
type: 'number',
|
|
32
|
+
minimum: 1,
|
|
33
|
+
maximum: 100,
|
|
34
|
+
default: 85,
|
|
35
|
+
description: 'Lossy compression quality (1-100)',
|
|
36
|
+
},
|
|
37
|
+
width: {
|
|
38
|
+
type: 'integer',
|
|
39
|
+
minimum: 1,
|
|
40
|
+
description: 'Target pixel width (aspect ratio preserved if height omitted)',
|
|
41
|
+
},
|
|
42
|
+
height: {
|
|
43
|
+
type: 'integer',
|
|
44
|
+
minimum: 1,
|
|
45
|
+
description: 'Target pixel height',
|
|
46
|
+
},
|
|
47
|
+
clean: {
|
|
48
|
+
type: 'boolean',
|
|
49
|
+
default: true,
|
|
50
|
+
description: 'Strip EXIF, XMP, IPTC, and C2PA metadata',
|
|
51
|
+
},
|
|
52
|
+
lossless: {
|
|
53
|
+
type: 'boolean',
|
|
54
|
+
default: false,
|
|
55
|
+
description: 'Lossless compression mode (for WebP and AVIF)',
|
|
56
|
+
},
|
|
57
|
+
background: {
|
|
58
|
+
type: 'string',
|
|
59
|
+
description: 'Hex background color for opaque targets (e.g. #ffffff)',
|
|
60
|
+
},
|
|
61
|
+
maxOutputBytes: {
|
|
62
|
+
type: 'integer',
|
|
63
|
+
description: 'Strict byte size limit on output',
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
required: ['input'],
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: 'batch_convert',
|
|
71
|
+
description: 'Convert a collection of images concurrently with shared options.',
|
|
72
|
+
inputSchema: {
|
|
73
|
+
type: 'object',
|
|
74
|
+
properties: {
|
|
75
|
+
inputs: {
|
|
76
|
+
type: 'array',
|
|
77
|
+
items: { type: 'string' },
|
|
78
|
+
description: 'Array of file paths, URLs, or image byte buffers',
|
|
79
|
+
},
|
|
80
|
+
to: {
|
|
81
|
+
type: 'string',
|
|
82
|
+
enum: ['webp', 'avif', 'png', 'jpeg'],
|
|
83
|
+
default: 'webp',
|
|
84
|
+
},
|
|
85
|
+
quality: {
|
|
86
|
+
type: 'number',
|
|
87
|
+
default: 85,
|
|
88
|
+
},
|
|
89
|
+
clean: {
|
|
90
|
+
type: 'boolean',
|
|
91
|
+
default: true,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
required: ['inputs'],
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
name: 'inspect_image',
|
|
99
|
+
description: 'Inspect image format, dimensions, transparency, chunks, and metadata without re-encoding.',
|
|
100
|
+
inputSchema: {
|
|
101
|
+
type: 'object',
|
|
102
|
+
properties: {
|
|
103
|
+
input: {
|
|
104
|
+
type: 'string',
|
|
105
|
+
description: 'Image file path, URL, or base64 data',
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
required: ['input'],
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: 'suggest_conversion',
|
|
113
|
+
description: 'Recommend the optimal target format, quality, and dimensions based on image alpha and intended use case.',
|
|
114
|
+
inputSchema: {
|
|
115
|
+
type: 'object',
|
|
116
|
+
properties: {
|
|
117
|
+
input: {
|
|
118
|
+
type: 'string',
|
|
119
|
+
description: 'Image file path or byte data',
|
|
120
|
+
},
|
|
121
|
+
useCase: {
|
|
122
|
+
type: 'string',
|
|
123
|
+
enum: ['icon', 'hero', 'thumbnail', 'photo', 'general'],
|
|
124
|
+
default: 'general',
|
|
125
|
+
description: 'Intended asset placement context',
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
required: ['input'],
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
name: 'verify_clean',
|
|
133
|
+
description: 'Verify that an image contains zero residual metadata (EXIF, XMP, GPS, C2PA) and emit verification status.',
|
|
134
|
+
inputSchema: {
|
|
135
|
+
type: 'object',
|
|
136
|
+
properties: {
|
|
137
|
+
input: {
|
|
138
|
+
type: 'string',
|
|
139
|
+
description: 'Image file path or byte data to audit',
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
required: ['input'],
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: 'verify_alpha',
|
|
147
|
+
description: 'Inspect alpha channel pixels to verify whether an image contains true transparency.',
|
|
148
|
+
inputSchema: {
|
|
149
|
+
type: 'object',
|
|
150
|
+
properties: {
|
|
151
|
+
input: {
|
|
152
|
+
type: 'string',
|
|
153
|
+
description: 'Image file path or byte data',
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
required: ['input'],
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
name: 'audit_conversion',
|
|
161
|
+
description: 'Audit a shell command (ImageMagick, ffmpeg, cwebp) for transparency loss bugs or security risks.',
|
|
162
|
+
inputSchema: {
|
|
163
|
+
type: 'object',
|
|
164
|
+
properties: {
|
|
165
|
+
command: {
|
|
166
|
+
type: 'string',
|
|
167
|
+
description: 'Shell command string (e.g., "convert icon.svg icon.png")',
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
required: ['command'],
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
name: 'explain_black_background',
|
|
175
|
+
description: 'Explain why an image conversion resulted in an unwanted black background and provide the exact fix.',
|
|
176
|
+
inputSchema: {
|
|
177
|
+
type: 'object',
|
|
178
|
+
properties: {
|
|
179
|
+
sourceFormat: {
|
|
180
|
+
type: 'string',
|
|
181
|
+
description: 'Original input format (e.g. svg, png)',
|
|
182
|
+
},
|
|
183
|
+
targetFormat: {
|
|
184
|
+
type: 'string',
|
|
185
|
+
description: 'Destination format (e.g. jpeg)',
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
required: ['sourceFormat', 'targetFormat'],
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
name: 'convert_url',
|
|
193
|
+
description: 'Safely fetch an image from a remote URL (with SSRF and private subnet protection) and convert it.',
|
|
194
|
+
inputSchema: {
|
|
195
|
+
type: 'object',
|
|
196
|
+
properties: {
|
|
197
|
+
url: {
|
|
198
|
+
type: 'string',
|
|
199
|
+
description: 'Remote HTTP/HTTPS image URL',
|
|
200
|
+
},
|
|
201
|
+
to: {
|
|
202
|
+
type: 'string',
|
|
203
|
+
enum: ['webp', 'avif', 'png', 'jpeg'],
|
|
204
|
+
default: 'webp',
|
|
205
|
+
},
|
|
206
|
+
quality: {
|
|
207
|
+
type: 'number',
|
|
208
|
+
default: 85,
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
required: ['url'],
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Loads the static agent.json runtime manifest.
|
|
219
|
+
* @returns {object}
|
|
220
|
+
*/
|
|
221
|
+
function getAgentManifest() {
|
|
222
|
+
const manifestPath = path.resolve(__dirname, '..', 'agent.json');
|
|
223
|
+
try {
|
|
224
|
+
const raw = fs.readFileSync(manifestPath, 'utf8');
|
|
225
|
+
return JSON.parse(raw);
|
|
226
|
+
} catch {
|
|
227
|
+
return {
|
|
228
|
+
name: '@zapwebp/core',
|
|
229
|
+
version: '1.1.1',
|
|
230
|
+
description: 'Zapwebp Core Engine',
|
|
231
|
+
capabilities: ['convert', 'inspect', 'clean', 'verify_alpha', 'audit', 'testing'],
|
|
232
|
+
tools: listTools(),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
module.exports = {
|
|
238
|
+
listTools,
|
|
239
|
+
getAgentManifest,
|
|
240
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export interface RawImage {
|
|
2
|
+
data: Uint8Array;
|
|
3
|
+
width: number;
|
|
4
|
+
height: number;
|
|
5
|
+
channels: 4;
|
|
6
|
+
hasAlpha: boolean;
|
|
7
|
+
format?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface ConvertOptions {
|
|
11
|
+
to?: string;
|
|
12
|
+
format?: string;
|
|
13
|
+
width?: number;
|
|
14
|
+
height?: number;
|
|
15
|
+
quality?: number;
|
|
16
|
+
lossless?: boolean;
|
|
17
|
+
clean?: boolean;
|
|
18
|
+
stripMetadata?: boolean;
|
|
19
|
+
background?: string;
|
|
20
|
+
defaultBackground?: string;
|
|
21
|
+
allowUnsafe?: boolean;
|
|
22
|
+
maxBytes?: number;
|
|
23
|
+
maxInputBytes?: number;
|
|
24
|
+
maxOutputBytes?: number;
|
|
25
|
+
transforms?: TransformStep[];
|
|
26
|
+
policy?: any;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface InspectMetadataSummary {
|
|
30
|
+
hasExif?: boolean;
|
|
31
|
+
hasXmp?: boolean;
|
|
32
|
+
hasC2pa?: boolean;
|
|
33
|
+
summary?: string[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface InspectResult {
|
|
37
|
+
format: string;
|
|
38
|
+
width: number;
|
|
39
|
+
height: number;
|
|
40
|
+
hasAlpha: boolean;
|
|
41
|
+
bytes: number;
|
|
42
|
+
metadata?: InspectMetadataSummary;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface FormatHandler {
|
|
46
|
+
name: string;
|
|
47
|
+
mimeTypes: readonly string[];
|
|
48
|
+
extensions: readonly string[];
|
|
49
|
+
supportsAlpha: boolean;
|
|
50
|
+
isInput?: boolean;
|
|
51
|
+
isOutput?: boolean;
|
|
52
|
+
detect(bytes: Uint8Array): boolean;
|
|
53
|
+
decode(bytes: Uint8Array, options?: any): Promise<RawImage>;
|
|
54
|
+
encode(image: RawImage, options?: any): Promise<Uint8Array>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface MetadataHandler {
|
|
58
|
+
name: string;
|
|
59
|
+
inspect(bytes: Uint8Array): Promise<any[]>;
|
|
60
|
+
strip(bytes: Uint8Array, options?: any): Promise<Uint8Array>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface TransformStep {
|
|
64
|
+
name: string;
|
|
65
|
+
apply(image: RawImage, options?: any): Promise<RawImage>;
|
|
66
|
+
getMetadata?(): any;
|
|
67
|
+
}
|
|
68
|
+
|