@zapwebp/core 1.1.0 → 1.1.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zapwebp/core",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Format-aware image conversion engine with transparency-safe outputs.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
@@ -21,7 +21,7 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@resvg/resvg-js": "^2.6.2",
24
- "sharp": "^0.33.5",
24
+ "sharp": "^0.35.5",
25
25
  "@zapwebp/shared": "1.1.2"
26
26
  },
27
27
  "publishConfig": {
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const sharp = require('sharp');
4
-
5
3
  /** @type {import('./registry').FormatHandler} */
6
4
  const avifHandler = {
7
5
  name: 'avif',
@@ -22,6 +20,7 @@ const avifHandler = {
22
20
  },
23
21
 
24
22
  async decode(bytes) {
23
+ const sharp = require('sharp');
25
24
  const { data: raw, info } = await sharp(bytes, { limitInputPixels: 4096 * 4096, pages: 1, animated: false })
26
25
  .ensureAlpha()
27
26
  .raw()
@@ -35,6 +34,7 @@ const avifHandler = {
35
34
  },
36
35
 
37
36
  async encode(image, options = {}) {
37
+ const sharp = require('sharp');
38
38
  const raw = Buffer.isBuffer(image.data)
39
39
  ? image.data
40
40
  : Buffer.from(image.data.buffer, image.data.byteOffset, image.data.byteLength);
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const sharp = require('sharp');
4
-
5
3
  /** @type {import('./registry').FormatHandler} */
6
4
  const gifHandler = {
7
5
  name: 'gif',
@@ -19,6 +17,7 @@ const gifHandler = {
19
17
  },
20
18
 
21
19
  async decode(bytes) {
20
+ const sharp = require('sharp');
22
21
  const { data: raw, info } = await sharp(bytes, { limitInputPixels: 4096 * 4096, pages: 1, animated: false })
23
22
  .ensureAlpha()
24
23
  .raw()
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const sharp = require('sharp');
4
-
5
3
  /** @type {import('./registry').FormatHandler} */
6
4
  const jpegHandler = {
7
5
  name: 'jpeg',
@@ -17,6 +15,7 @@ const jpegHandler = {
17
15
  },
18
16
 
19
17
  async decode(bytes) {
18
+ const sharp = require('sharp');
20
19
  const { data: raw, info } = await sharp(bytes, { limitInputPixels: 4096 * 4096, pages: 1, animated: false })
21
20
  .ensureAlpha()
22
21
  .raw()
@@ -34,6 +33,7 @@ const jpegHandler = {
34
33
  throw new Error('JPEG output does not support lossless encoding.');
35
34
  }
36
35
 
36
+ const sharp = require('sharp');
37
37
  const raw = Buffer.isBuffer(image.data)
38
38
  ? image.data
39
39
  : Buffer.from(image.data.buffer, image.data.byteOffset, image.data.byteLength);
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const sharp = require('sharp');
4
-
5
3
  const PNG_HEADER = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
6
4
 
7
5
  /** @type {import('./registry').FormatHandler} */
@@ -20,6 +18,7 @@ const pngHandler = {
20
18
  },
21
19
 
22
20
  async decode(bytes) {
21
+ const sharp = require('sharp');
23
22
  const { data: raw, info } = await sharp(bytes, { limitInputPixels: 4096 * 4096, pages: 1, animated: false })
24
23
  .ensureAlpha()
25
24
  .raw()
@@ -33,6 +32,7 @@ const pngHandler = {
33
32
  },
34
33
 
35
34
  async encode(image, options = {}) {
35
+ const sharp = require('sharp');
36
36
  const raw = Buffer.isBuffer(image.data)
37
37
  ? image.data
38
38
  : Buffer.from(image.data.buffer, image.data.byteOffset, image.data.byteLength);
@@ -1,8 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const { Resvg } = require('@resvg/resvg-js');
4
- const sharp = require('sharp');
5
-
6
3
  /**
7
4
  * Validates an SVG input buffer or string.
8
5
  * @param {Buffer | Uint8Array | string} input
@@ -55,6 +52,8 @@ const svgHandler = {
55
52
  },
56
53
 
57
54
  async decode(bytes) {
55
+ const { Resvg } = require('@resvg/resvg-js');
56
+ const sharp = require('sharp');
58
57
  const svg = validateSvgString(bytes);
59
58
  const dims = getSvgDimensions(svg);
60
59
  const width = dims.width || 2048;
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const sharp = require('sharp');
4
-
5
3
  /** @type {import('./registry').FormatHandler} */
6
4
  const tiffHandler = {
7
5
  name: 'tiff',
@@ -23,6 +21,7 @@ const tiffHandler = {
23
21
  },
24
22
 
25
23
  async decode(bytes) {
24
+ const sharp = require('sharp');
26
25
  const { data: raw, info } = await sharp(bytes, { limitInputPixels: 4096 * 4096, pages: 1, animated: false })
27
26
  .ensureAlpha()
28
27
  .raw()
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const sharp = require('sharp');
4
-
5
3
  /** @type {import('./registry').FormatHandler} */
6
4
  const webpHandler = {
7
5
  name: 'webp',
@@ -18,6 +16,7 @@ const webpHandler = {
18
16
  },
19
17
 
20
18
  async decode(bytes) {
19
+ const sharp = require('sharp');
21
20
  const { data: raw, info } = await sharp(bytes, { limitInputPixels: 4096 * 4096, pages: 1, animated: false })
22
21
  .ensureAlpha()
23
22
  .raw()
@@ -31,6 +30,7 @@ const webpHandler = {
31
30
  },
32
31
 
33
32
  async encode(image, options = {}) {
33
+ const sharp = require('sharp');
34
34
  const raw = Buffer.isBuffer(image.data)
35
35
  ? image.data
36
36
  : Buffer.from(image.data.buffer, image.data.byteOffset, image.data.byteLength);
package/src/index.js CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  const path = require('node:path');
4
4
  const crypto = require('node:crypto');
5
- const { Resvg } = require('@resvg/resvg-js');
6
- const sharp = require('sharp');
7
5
  const formats = require('./formats');
8
6
  const metadata = require('./metadata');
9
7
  const {
@@ -20,6 +18,37 @@ const {
20
18
  evaluateQuality,
21
19
  } = require('./policy');
22
20
 
21
+ let _sharpInstance = null;
22
+ function getSharp() {
23
+ if (_sharpInstance) return _sharpInstance;
24
+ try {
25
+ _sharpInstance = require('sharp');
26
+ return _sharpInstance;
27
+ } catch (err) {
28
+ throw new ZapwebpError({
29
+ code: 'E_ENGINE_UNAVAILABLE',
30
+ message: 'Native raster engine (sharp) is not available in this environment: ' + (err && err.message ? err.message : String(err)),
31
+ fix: 'Install sharp via "npm install sharp" or ensure native prebuilts are compiled for this architecture.',
32
+ });
33
+ }
34
+ }
35
+
36
+ let _resvgInstance = null;
37
+ function getResvg() {
38
+ if (_resvgInstance) return _resvgInstance;
39
+ try {
40
+ const mod = require('@resvg/resvg-js');
41
+ _resvgInstance = mod.Resvg || mod;
42
+ return _resvgInstance;
43
+ } catch (err) {
44
+ throw new ZapwebpError({
45
+ code: 'E_ENGINE_UNAVAILABLE',
46
+ message: 'Native vector engine (@resvg/resvg-js) is not available in this environment: ' + (err && err.message ? err.message : String(err)),
47
+ fix: 'Install @resvg/resvg-js or ensure native prebuilts are compiled for this architecture.',
48
+ });
49
+ }
50
+ }
51
+
23
52
  const MAX_WIDTH = MAX_DIMENSION;
24
53
  const FIT_MODES = Object.freeze(['contain', 'cover', 'fill', 'inside', 'outside']);
25
54
 
@@ -60,6 +89,8 @@ function asBuffer(input) {
60
89
  function detectFormat(input) {
61
90
  const data = asBuffer(input);
62
91
  if (!data.length) throw new Error('The input file is empty.');
92
+ const registered = formats.detectFormatHandler(data);
93
+ if (registered) return registered.name;
63
94
  if (data.length >= 8 && data.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) return 'png';
64
95
  if (data.length >= 3 && data[0] === 0xff && data[1] === 0xd8 && data[2] === 0xff) return 'jpeg';
65
96
  if (data.length >= 6 && (data.subarray(0, 6).toString('ascii') === 'GIF87a' || data.subarray(0, 6).toString('ascii') === 'GIF89a')) return 'gif';
@@ -277,7 +308,7 @@ function generateCleanCertificate(output) {
277
308
  */
278
309
  async function verifyAlpha(input) {
279
310
  const data = asBuffer(input);
280
- const { data: raw, info } = await sharp(data, { limitInputPixels: MAX_PIXELS }).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
311
+ const { data: raw, info } = await getSharp()(data, { limitInputPixels: MAX_PIXELS }).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
281
312
  const w = info.width;
282
313
  const h = info.height;
283
314
 
@@ -448,7 +479,7 @@ async function inspectImage(input) {
448
479
  if (width && height && (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0)) throw new Error('The SVG has invalid intrinsic dimensions.');
449
480
  return { format, width: width || undefined, height: height || undefined, hasAlpha: true, bytes: data.length, metadata };
450
481
  }
451
- const meta = await sharp(data, { limitInputPixels: MAX_PIXELS, pages: 1, animated: false, failOn: 'error' }).metadata();
482
+ const meta = await getSharp()(data, { limitInputPixels: MAX_PIXELS, pages: 1, animated: false, failOn: 'error' }).metadata();
452
483
  checkDimensions(meta.width, meta.height);
453
484
  return { format, width: meta.width, height: meta.height, hasAlpha: Boolean(meta.hasAlpha), bytes: data.length, metadata };
454
485
  }
@@ -528,11 +559,12 @@ async function convertImage(input, options = {}) {
528
559
  checkDimensions(rasterWidth, rasterHeight);
529
560
  const resvgOptions = { background: opts.svgBackground ?? 'rgba(0, 0, 0, 0)' };
530
561
  resvgOptions.fitTo = { mode: renderMode, value: renderValue };
531
- source = new Resvg(svg, resvgOptions).render().asPng();
532
- const svgMeta = await sharp(source, { limitInputPixels: MAX_PIXELS }).metadata();
562
+ const ResvgClass = getResvg();
563
+ source = new ResvgClass(svg, resvgOptions).render().asPng();
564
+ const svgMeta = await getSharp()(source, { limitInputPixels: MAX_PIXELS }).metadata();
533
565
  sourceHasAlpha = Boolean(svgMeta.hasAlpha);
534
566
  } else {
535
- const meta = await sharp(source, { limitInputPixels: MAX_PIXELS, pages: 1, animated: false, failOn: 'error' }).metadata();
567
+ const meta = await getSharp()(source, { limitInputPixels: MAX_PIXELS, pages: 1, animated: false, failOn: 'error' }).metadata();
536
568
  checkDimensions(meta.width, meta.height);
537
569
  sourceHasAlpha = Boolean(meta.hasAlpha);
538
570
  }
@@ -607,10 +639,11 @@ async function convertImage(input, options = {}) {
607
639
  if (opts.to === 'jpeg' && targetLossless) throw new Error('JPEG output does not support lossless encoding.');
608
640
 
609
641
  const createPipeline = () => {
642
+ const sharpInstance = getSharp();
610
643
  if (rawBufferInfo) {
611
- return sharp(rawBufferInfo.buffer, { raw: rawBufferInfo.rawOptions });
644
+ return sharpInstance(rawBufferInfo.buffer, { raw: rawBufferInfo.rawOptions });
612
645
  }
613
- return sharp(source, { limitInputPixels: MAX_PIXELS, pages: 1, animated: false, failOn: 'error' });
646
+ return sharpInstance(source, { limitInputPixels: MAX_PIXELS, pages: 1, animated: false, failOn: 'error' });
614
647
  };
615
648
 
616
649
  const encodeWithQuality = async (q, isLossless) => {
@@ -764,9 +797,10 @@ async function decodeToRgba(input, format) {
764
797
  const width = dims.width || 2048;
765
798
  const height = dims.height || 2048;
766
799
  checkDimensions(width, height);
767
- source = new Resvg(svg, { fitTo: { mode: 'original' }, background: 'rgba(0, 0, 0, 0)' }).render().asPng();
800
+ const ResvgClass = getResvg();
801
+ source = new ResvgClass(svg, { fitTo: { mode: 'original' }, background: 'rgba(0, 0, 0, 0)' }).render().asPng();
768
802
  }
769
- const pipeline = sharp(source, { limitInputPixels: MAX_PIXELS, pages: 1, animated: false, failOn: 'error' }).ensureAlpha();
803
+ const pipeline = getSharp()(source, { limitInputPixels: MAX_PIXELS, pages: 1, animated: false, failOn: 'error' }).ensureAlpha();
770
804
  const { data: raw, info } = await pipeline.raw().toBuffer({ resolveWithObject: true });
771
805
  checkDimensions(info.width, info.height);
772
806
  return {
@@ -812,7 +846,7 @@ async function encodeFromRgba(image, options = {}) {
812
846
  throw new Error('JPEG output does not support lossless encoding.');
813
847
  }
814
848
 
815
- let pipeline = sharp(raw, {
849
+ let pipeline = getSharp()(raw, {
816
850
  raw: {
817
851
  width: image.width,
818
852
  height: image.height,
@@ -956,6 +990,8 @@ module.exports = {
956
990
  registerMetadataHandler: metadata.registerMetadataHandler,
957
991
  getMetadataHandlers: metadata.getMetadataHandlers,
958
992
  cleanBytes: metadata.cleanBytes,
993
+ getSharp,
994
+ getResvg,
959
995
  MAX_WIDTH,
960
996
  INPUT_FORMATS,
961
997
  OUTPUT_FORMATS,
@@ -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',
@@ -19,6 +17,7 @@ const c2paHandler = {
19
17
  async strip(bytes) {
20
18
  const data = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
21
19
  try {
20
+ const sharp = require('sharp');
22
21
  const meta = await sharp(data).metadata();
23
22
  if (!meta.format) return data;
24
23
  return await sharp(data).toBuffer();
@@ -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',
@@ -33,6 +31,7 @@ const exifHandler = {
33
31
  async strip(bytes) {
34
32
  const data = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
35
33
  try {
34
+ const sharp = require('sharp');
36
35
  // Re-encode via sharp without metadata to strip EXIF
37
36
  const meta = await sharp(data).metadata();
38
37
  if (!meta.format) return data;
@@ -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',
@@ -19,6 +17,7 @@ const iccHandler = {
19
17
  async strip(bytes) {
20
18
  const data = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
21
19
  try {
20
+ const sharp = require('sharp');
22
21
  const meta = await sharp(data).metadata();
23
22
  if (!meta.format) return data;
24
23
  return await sharp(data).toBuffer();
@@ -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',
@@ -21,6 +19,7 @@ const pngTextHandler = {
21
19
  async strip(bytes) {
22
20
  const data = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes);
23
21
  try {
22
+ const sharp = require('sharp');
24
23
  const meta = await sharp(data).metadata();
25
24
  if (!meta.format) return data;
26
25
  return await sharp(data).toBuffer();
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
 
3
- const sharp = require('sharp');
4
3
  const { ZapwebpError } = require('@zapwebp/shared');
5
4
 
6
5
  /**
@@ -132,6 +131,7 @@ class MetadataRegistry {
132
131
  try {
133
132
  if (current.length >= 16) {
134
133
  // Check raster parseability
134
+ const sharp = require('sharp');
135
135
  await sharp(current, { failOn: 'none' }).metadata();
136
136
  }
137
137
  } catch {
@@ -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',
@@ -32,6 +30,7 @@ const xmpHandler = {
32
30
  return Buffer.from(cleaned, 'utf8');
33
31
  }
34
32
  try {
33
+ const sharp = require('sharp');
35
34
  const meta = await sharp(data).metadata();
36
35
  if (!meta.format) return data;
37
36
  return await sharp(data).toBuffer();