@realfavicongenerator/image-adapter-node 0.5.0 → 0.6.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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ const adapter_1 = require("./adapter");
39
+ const fs = __importStar(require("fs"));
40
+ const path = __importStar(require("path"));
41
+ const sharp_1 = __importDefault(require("sharp"));
42
+ describe('NodeImageAdapter', () => {
43
+ describe('getImageSize', () => {
44
+ it('should return correct dimensions for a PNG image', () => __awaiter(void 0, void 0, void 0, function* () {
45
+ const adapter = yield (0, adapter_1.getNodeImageAdapter)();
46
+ // Read the test image from fixtures
47
+ const imagePath = path.join(__dirname, '../fixtures/test-image.png');
48
+ const imageBuffer = fs.readFileSync(imagePath);
49
+ // Convert to data URL
50
+ const base64 = imageBuffer.toString('base64');
51
+ const dataUrl = `data:image/png;base64,${base64}`;
52
+ // Get image size
53
+ const size = yield adapter.getImageSize(dataUrl);
54
+ // Assert dimensions match the test image (100x50)
55
+ expect(size.width).toBe(100);
56
+ expect(size.height).toBe(50);
57
+ }));
58
+ });
59
+ describe('convertSvgToPng', () => {
60
+ it('should convert an SVG to a PNG buffer', () => __awaiter(void 0, void 0, void 0, function* () {
61
+ const adapter = yield (0, adapter_1.getNodeImageAdapter)();
62
+ // Create an SVG with a blue rectangle
63
+ const svg = adapter.createSvg();
64
+ svg.size(200, 150);
65
+ svg.rect(200, 150).fill('#0000ff');
66
+ // Convert to PNG
67
+ const pngBuffer = yield adapter.convertSvgToPng(svg);
68
+ // Verify it's a valid PNG buffer
69
+ expect(Buffer.isBuffer(pngBuffer)).toBe(true);
70
+ // Verify the PNG has correct dimensions using sharp
71
+ const metadata = yield (0, sharp_1.default)(pngBuffer).metadata();
72
+ expect(metadata.format).toBe('png');
73
+ expect(metadata.width).toBe(200);
74
+ expect(metadata.height).toBe(150);
75
+ }));
76
+ });
77
+ describe('getImageData', () => {
78
+ it('should resize an image and return raw pixel data', () => __awaiter(void 0, void 0, void 0, function* () {
79
+ const adapter = yield (0, adapter_1.getNodeImageAdapter)();
80
+ // Read the test image from fixtures
81
+ const imagePath = path.join(__dirname, '../fixtures/test-image.png');
82
+ const imageBuffer = fs.readFileSync(imagePath);
83
+ // Convert to data URL
84
+ const base64 = imageBuffer.toString('base64');
85
+ const dataUrl = `data:image/png;base64,${base64}`;
86
+ // Get raw image data resized to 80x80
87
+ const rawData = yield adapter.getImageData(dataUrl, 80);
88
+ // Read the reference image and convert to raw data
89
+ const referencePath = path.join(__dirname, '../fixtures/test-image-small-square.png');
90
+ const referenceBuffer = fs.readFileSync(referencePath);
91
+ const referenceRawData = yield (0, sharp_1.default)(referenceBuffer)
92
+ .raw()
93
+ .toBuffer();
94
+ // Compare the raw data
95
+ expect(Buffer.isBuffer(rawData)).toBe(true);
96
+ expect(rawData.length).toBe(referenceRawData.length);
97
+ expect(rawData.toString('hex')).toBe(referenceRawData.toString('hex'));
98
+ }));
99
+ });
100
+ });
package/fixtures/.keep ADDED
File without changes
Binary file
package/jest.config.js ADDED
@@ -0,0 +1,5 @@
1
+ /** @type {import('ts-jest').JestConfigWithTsJest} */
2
+ module.exports = {
3
+ preset: 'ts-jest',
4
+ testEnvironment: 'node',
5
+ };
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "@realfavicongenerator/image-adapter-node",
3
- "version": "0.5.0",
3
+ "version": "0.6.1",
4
4
  "description": "Image adapter for Node",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
+ "test": "NODE_OPTIONS='--experimental-vm-modules' jest src",
9
+ "test:watch": "NODE_OPTIONS='--experimental-vm-modules' jest src --watch",
8
10
  "build": "tsc",
9
11
  "prepublishOnly": "npm run build"
10
12
  },
@@ -26,15 +28,18 @@
26
28
  },
27
29
  "homepage": "https://github.com/RealFaviconGenerator/core#readme",
28
30
  "devDependencies": {
31
+ "@types/jest": "^29.5.12",
29
32
  "@types/node": "^20.11.30",
30
33
  "@types/svgdom": "^0.1.2",
34
+ "jest": "^29.7.0",
35
+ "ts-jest": "^29.1.2",
31
36
  "typescript": "^5.4.3"
32
37
  },
33
38
  "dependencies": {
34
- "@realfavicongenerator/generate-favicon": "^0.5.0",
39
+ "@realfavicongenerator/generate-favicon": "^0.6.0",
35
40
  "@svgdotjs/svg.js": "^3.2.0",
36
- "sharp": "^0.32.6",
41
+ "sharp": "^0.34.5",
37
42
  "svgdom": "^0.1.19"
38
43
  },
39
- "gitHead": "f0e219642ecea42c14a567ba6d64bf639871974c"
44
+ "gitHead": "ddbf0ed88b771c63631a90d94023bf5008911baf"
40
45
  }
@@ -0,0 +1,79 @@
1
+ import { getNodeImageAdapter } from './adapter';
2
+ import * as fs from 'fs';
3
+ import * as path from 'path';
4
+ import sharp from 'sharp';
5
+
6
+ describe('NodeImageAdapter', () => {
7
+ describe('getImageSize', () => {
8
+ it('should return correct dimensions for a PNG image', async () => {
9
+ const adapter = await getNodeImageAdapter();
10
+
11
+ // Read the test image from fixtures
12
+ const imagePath = path.join(__dirname, '../fixtures/test-image.png');
13
+ const imageBuffer = fs.readFileSync(imagePath);
14
+
15
+ // Convert to data URL
16
+ const base64 = imageBuffer.toString('base64');
17
+ const dataUrl = `data:image/png;base64,${base64}`;
18
+
19
+ // Get image size
20
+ const size = await adapter.getImageSize(dataUrl);
21
+
22
+ // Assert dimensions match the test image (100x50)
23
+ expect(size.width).toBe(100);
24
+ expect(size.height).toBe(50);
25
+ });
26
+ });
27
+
28
+ describe('convertSvgToPng', () => {
29
+ it('should convert an SVG to a PNG buffer', async () => {
30
+ const adapter = await getNodeImageAdapter();
31
+
32
+ // Create an SVG with a blue rectangle
33
+ const svg = adapter.createSvg();
34
+ svg.size(200, 150);
35
+ svg.rect(200, 150).fill('#0000ff');
36
+
37
+ // Convert to PNG
38
+ const pngBuffer = await adapter.convertSvgToPng(svg);
39
+
40
+ // Verify it's a valid PNG buffer
41
+ expect(Buffer.isBuffer(pngBuffer)).toBe(true);
42
+
43
+ // Verify the PNG has correct dimensions using sharp
44
+ const metadata = await sharp(pngBuffer).metadata();
45
+ expect(metadata.format).toBe('png');
46
+ expect(metadata.width).toBe(200);
47
+ expect(metadata.height).toBe(150);
48
+ });
49
+ });
50
+
51
+ describe('getImageData', () => {
52
+ it('should resize an image and return raw pixel data', async () => {
53
+ const adapter = await getNodeImageAdapter();
54
+
55
+ // Read the test image from fixtures
56
+ const imagePath = path.join(__dirname, '../fixtures/test-image.png');
57
+ const imageBuffer = fs.readFileSync(imagePath);
58
+
59
+ // Convert to data URL
60
+ const base64 = imageBuffer.toString('base64');
61
+ const dataUrl = `data:image/png;base64,${base64}`;
62
+
63
+ // Get raw image data resized to 80x80
64
+ const rawData = await adapter.getImageData(dataUrl, 80);
65
+
66
+ // Read the reference image and convert to raw data
67
+ const referencePath = path.join(__dirname, '../fixtures/test-image-small-square.png');
68
+ const referenceBuffer = fs.readFileSync(referencePath);
69
+ const referenceRawData = await sharp(referenceBuffer)
70
+ .raw()
71
+ .toBuffer();
72
+
73
+ // Compare the raw data
74
+ expect(Buffer.isBuffer(rawData)).toBe(true);
75
+ expect(rawData.length).toBe(referenceRawData.length);
76
+ expect((rawData as Buffer).toString('hex')).toBe(referenceRawData.toString('hex'));
77
+ });
78
+ });
79
+ });
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 RealFaviconGenerator
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.