@nodepit/svg-to-img 3.1.0 → 4.0.0-beta.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/README.md +17 -2
- package/dist/helpers.d.ts +0 -1
- package/dist/helpers.js +3 -12
- package/dist/index.d.ts +2 -2
- package/dist/index.js +28 -9
- package/dist/typings/index.js +2 -0
- package/package.json +70 -28
- package/changelog.md +0 -32
package/README.md
CHANGED
|
@@ -18,9 +18,11 @@
|
|
|
18
18
|
To use svg-to-img in your project, run:
|
|
19
19
|
|
|
20
20
|
```bash
|
|
21
|
-
npm install @nodepit/svg-to-img
|
|
21
|
+
npm install @nodepit/svg-to-img
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
+
Starting from v4.0.0, svg-to-img requires Node 22.12 or later and Puppeteer 25, which in turn needs a recent Chrome (it does not work with Chrome 78, as shipped in `browserless/chrome:1.x`; use e.g. `ghcr.io/browserless/chromium`).
|
|
25
|
+
|
|
24
26
|
Starting from v3.0.0, Chromium will **no longer** automatically be downloaded when installing this dependency (instead of depending on `puppetteer`, the library now depends on `puppeteer-core`; see [here](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#puppeteer-vs-puppeteer-core)). We assume that you either have an existing local or remote Chromium, or that you explicitly install `puppeteer` within your project to to have Chromium downloaded.
|
|
25
27
|
|
|
26
28
|
#### Debian
|
|
@@ -155,6 +157,19 @@ This method is simply a shorthand for the `to` method.
|
|
|
155
157
|
|
|
156
158
|
This method is simply a shorthand for the `to` method.
|
|
157
159
|
|
|
160
|
+
### Errors
|
|
161
|
+
|
|
162
|
+
All conversion methods return a promise that rejects with an `Error` in these cases:
|
|
163
|
+
|
|
164
|
+
| Case | Error |
|
|
165
|
+
| --- | --- |
|
|
166
|
+
| The input is not a valid SVG | message `Malformed SVG` |
|
|
167
|
+
| The browser cannot be reached (`connect`) | message lists the refused addresses, `code` is `ECONNREFUSED`; the next call tries to connect again |
|
|
168
|
+
| `puppeteer` is not installed, and you use `from` (which launches a local browser) | message ``NPM module `puppeteer` is not available…`` |
|
|
169
|
+
| The `path` option cannot be written | the error of the file system, e.g. `ENOENT` |
|
|
170
|
+
|
|
171
|
+
Errors of the browser itself (e.g. the browser crashes during the conversion) are passed on unchanged.
|
|
172
|
+
|
|
158
173
|
## Built with
|
|
159
174
|
|
|
160
175
|
* [node.js](https://nodejs.org/en/) - Cross-platform JavaScript run-time environment for executing JavaScript code server-side.
|
|
@@ -168,7 +183,7 @@ When contributing to this project, please first discuss the change you wish to m
|
|
|
168
183
|
|
|
169
184
|
Update the [README.md](https://github.com/etienne-martin/svg-to-img/blob/master/README.md) with details of changes to the library.
|
|
170
185
|
|
|
171
|
-
Execute `
|
|
186
|
+
Execute `pnpm run test` and update the [tests](https://github.com/etienne-martin/svg-to-img/tree/master/src/tests) if needed.
|
|
172
187
|
|
|
173
188
|
## Authors
|
|
174
189
|
|
package/dist/helpers.d.ts
CHANGED
package/dist/helpers.js
CHANGED
|
@@ -6,7 +6,7 @@ const getFileTypeFromPath = (path) => {
|
|
|
6
6
|
return path.toLowerCase().replace(new RegExp("jpg", "g"), "jpeg").split(".").reverse()[0];
|
|
7
7
|
};
|
|
8
8
|
exports.getFileTypeFromPath = getFileTypeFromPath;
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
10
10
|
const stringifyFunction = (func, ...argsArray) => {
|
|
11
11
|
// Remove istanbul coverage instruments
|
|
12
12
|
const functionString = func.toString().replace(/cov_(.+?)\+\+[,;]?/g, "");
|
|
@@ -26,16 +26,7 @@ const stringifyFunction = (func, ...argsArray) => {
|
|
|
26
26
|
return `(${functionString})(${args.join(",")})`;
|
|
27
27
|
};
|
|
28
28
|
exports.stringifyFunction = stringifyFunction;
|
|
29
|
-
const writeFileAsync =
|
|
30
|
-
return new Promise((resolve, reject) => {
|
|
31
|
-
fs.writeFile(path, data, (err) => {
|
|
32
|
-
if (err) {
|
|
33
|
-
return reject(err);
|
|
34
|
-
}
|
|
35
|
-
resolve();
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
};
|
|
29
|
+
const writeFileAsync = (path, data) => fs.promises.writeFile(path, data);
|
|
39
30
|
exports.writeFileAsync = writeFileAsync;
|
|
40
31
|
const renderSvg = async (svg, options) => {
|
|
41
32
|
return new Promise((resolve, reject) => {
|
|
@@ -87,7 +78,7 @@ const renderSvg = async (svg, options) => {
|
|
|
87
78
|
ctx.drawImage(img, 0, 0, imageWidth, imageHeight);
|
|
88
79
|
}
|
|
89
80
|
const dataURI = canvas.toDataURL("image/" + options.type, options.quality);
|
|
90
|
-
const base64 = dataURI.
|
|
81
|
+
const base64 = dataURI.slice(`data:image/${options.type};base64,`.length);
|
|
91
82
|
document.body.removeChild(img);
|
|
92
83
|
resolve(base64);
|
|
93
84
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import * as puppeteer from "puppeteer-core";
|
|
1
|
+
/// <reference types="node" preserve="true" />
|
|
2
|
+
import type * as puppeteer from "puppeteer-core" with { "resolution-mode": "import" };
|
|
3
3
|
import { IOptions, IShorthandOptions, IConnectOptions } from "./typings";
|
|
4
4
|
export declare class BrowserSource {
|
|
5
5
|
private readonly factory;
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.connect = exports.from = exports.SvgToImg = exports.Svg = exports.BrowserSource = void 0;
|
|
4
|
-
const puppeteer = require("puppeteer-core");
|
|
5
4
|
const helpers_1 = require("./helpers");
|
|
6
5
|
const constants_1 = require("./constants");
|
|
7
6
|
class BrowserSource {
|
|
7
|
+
factory;
|
|
8
|
+
queue = [];
|
|
9
|
+
browserDestructionTimeout;
|
|
10
|
+
browserInstance;
|
|
11
|
+
browserState = "closed";
|
|
8
12
|
constructor(factory) {
|
|
9
13
|
this.factory = factory;
|
|
10
|
-
this.queue = [];
|
|
11
|
-
this.browserState = "closed";
|
|
12
14
|
}
|
|
13
15
|
async getBrowser() {
|
|
14
16
|
return new Promise((resolve, reject) => {
|
|
@@ -29,7 +31,7 @@ class BrowserSource {
|
|
|
29
31
|
})
|
|
30
32
|
.catch((error) => {
|
|
31
33
|
this.browserState = "closed";
|
|
32
|
-
reject(error);
|
|
34
|
+
reject(unwrapConnectionError(error));
|
|
33
35
|
});
|
|
34
36
|
return;
|
|
35
37
|
}
|
|
@@ -70,6 +72,8 @@ class BrowserSource {
|
|
|
70
72
|
}
|
|
71
73
|
exports.BrowserSource = BrowserSource;
|
|
72
74
|
class Svg {
|
|
75
|
+
svg;
|
|
76
|
+
browserSource;
|
|
73
77
|
constructor(svg, browserSource) {
|
|
74
78
|
this.svg = svg;
|
|
75
79
|
this.browserSource = browserSource;
|
|
@@ -100,7 +104,7 @@ class Svg {
|
|
|
100
104
|
options.type = fileType;
|
|
101
105
|
}
|
|
102
106
|
}
|
|
103
|
-
const base64 = await page.evaluate((0, helpers_1.stringifyFunction)(helpers_1.renderSvg, svg, {
|
|
107
|
+
const base64 = (await page.evaluate((0, helpers_1.stringifyFunction)(helpers_1.renderSvg, svg, {
|
|
104
108
|
width: options.width,
|
|
105
109
|
height: options.height,
|
|
106
110
|
type: options.type,
|
|
@@ -108,7 +112,7 @@ class Svg {
|
|
|
108
112
|
background: options.background,
|
|
109
113
|
clip: options.clip,
|
|
110
114
|
jpegBackground: constants_1.config.jpegBackground
|
|
111
|
-
}));
|
|
115
|
+
})));
|
|
112
116
|
browserSource.scheduleBrowserForDestruction();
|
|
113
117
|
const buffer = Buffer.from(base64, "base64");
|
|
114
118
|
if (options.path) {
|
|
@@ -124,7 +128,18 @@ class Svg {
|
|
|
124
128
|
}
|
|
125
129
|
}
|
|
126
130
|
exports.Svg = Svg;
|
|
131
|
+
// Puppeteer 25 rejects a failed connection with an `ErrorEvent` wrapping Node's `AggregateError` (one error per
|
|
132
|
+
// resolved address), which has an empty message
|
|
133
|
+
/* istanbul ignore next */
|
|
134
|
+
const unwrapConnectionError = (error) => {
|
|
135
|
+
const cause = error?.error ?? error;
|
|
136
|
+
const { message, errors, code } = cause;
|
|
137
|
+
if (message || !Array.isArray(errors))
|
|
138
|
+
return cause;
|
|
139
|
+
return Object.assign(new Error(errors.map((e) => e.message).join("; "), { cause }), { code });
|
|
140
|
+
};
|
|
127
141
|
class SvgToImg {
|
|
142
|
+
browserSource;
|
|
128
143
|
constructor(browserSource) {
|
|
129
144
|
this.browserSource = browserSource;
|
|
130
145
|
}
|
|
@@ -135,11 +150,11 @@ class SvgToImg {
|
|
|
135
150
|
exports.SvgToImg = SvgToImg;
|
|
136
151
|
const defaultBrowserSource = new BrowserSource(async () => {
|
|
137
152
|
try {
|
|
138
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
153
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
|
|
139
154
|
const puppeteerFull = require("puppeteer");
|
|
140
155
|
return puppeteerFull.launch(constants_1.config.puppeteer);
|
|
141
156
|
}
|
|
142
|
-
catch
|
|
157
|
+
catch {
|
|
143
158
|
/* istanbul ignore next */
|
|
144
159
|
throw new Error("NPM module `puppeteer` is not available. Either add it as " +
|
|
145
160
|
" dependency to your project, or use the `connect` function and supply " +
|
|
@@ -152,6 +167,10 @@ const from = (svg) => {
|
|
|
152
167
|
exports.from = from;
|
|
153
168
|
/* istanbul ignore next */
|
|
154
169
|
const connect = (options) => {
|
|
155
|
-
return new SvgToImg(new BrowserSource(async () =>
|
|
170
|
+
return new SvgToImg(new BrowserSource(async () => {
|
|
171
|
+
// puppeteer-core is ESM-only, load it when the first conversion needs it and not when this library is imported (CJS tooling like Jest cannot load it)
|
|
172
|
+
const { connect } = await Promise.resolve().then(() => require("puppeteer-core"));
|
|
173
|
+
return connect(options);
|
|
174
|
+
}));
|
|
156
175
|
};
|
|
157
176
|
exports.connect = connect;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nodepit/svg-to-img",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-beta.1",
|
|
4
4
|
"description": "A node.js library to convert SVGs to images built with Puppeteer.",
|
|
5
5
|
"homepage": "https://github.com/NodePit/svg-to-img",
|
|
6
6
|
"keywords": [
|
|
@@ -14,6 +14,15 @@
|
|
|
14
14
|
],
|
|
15
15
|
"main": "dist/index.js",
|
|
16
16
|
"types": "dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
17
26
|
"author": {
|
|
18
27
|
"name": "Etienne Martin",
|
|
19
28
|
"url": "http://etiennemartin.ca/"
|
|
@@ -24,20 +33,30 @@
|
|
|
24
33
|
"scripts": {
|
|
25
34
|
"dev": "tsc --pretty --watch",
|
|
26
35
|
"lint": "eslint --fix ./src",
|
|
27
|
-
"pretest": "
|
|
36
|
+
"pretest": "pnpm run lint",
|
|
28
37
|
"test": "jest src --coverage --verbose",
|
|
29
38
|
"test:watch": "jest src --coverage --verbose --watch",
|
|
30
|
-
"
|
|
31
|
-
"prebuild": "rm -rf dist/ && npm run lint",
|
|
39
|
+
"prebuild": "rm -rf dist/ && pnpm run lint",
|
|
32
40
|
"build": "tsc --pretty",
|
|
33
|
-
"prepare": "
|
|
41
|
+
"prepare": "pnpm run build && husky",
|
|
42
|
+
"typecheck": "tsc --noEmit",
|
|
43
|
+
"prepublishOnly": "pnpm run typecheck && pnpm test",
|
|
44
|
+
"postversion": "git push --follow-tags"
|
|
34
45
|
},
|
|
35
46
|
"jest": {
|
|
36
47
|
"setupFiles": [
|
|
37
48
|
"jest-canvas-mock"
|
|
38
49
|
],
|
|
39
50
|
"transform": {
|
|
40
|
-
"^.+\\.tsx?$": "ts-jest"
|
|
51
|
+
"^.+\\.tsx?$": "ts-jest",
|
|
52
|
+
"^.+\\.js$": [
|
|
53
|
+
"@swc/jest",
|
|
54
|
+
{
|
|
55
|
+
"module": {
|
|
56
|
+
"type": "commonjs"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
]
|
|
41
60
|
},
|
|
42
61
|
"moduleFileExtensions": [
|
|
43
62
|
"ts",
|
|
@@ -68,40 +87,63 @@
|
|
|
68
87
|
"collectCoverageFrom": [
|
|
69
88
|
"src/**/*.(tsx|ts)"
|
|
70
89
|
],
|
|
71
|
-
"
|
|
90
|
+
"transformIgnorePatterns": [
|
|
91
|
+
"/node_modules/(?!(?:\\.pnpm/[^/]+/node_modules/)?(?:puppeteer-core|puppeteer|@puppeteer/browsers)/)"
|
|
92
|
+
]
|
|
72
93
|
},
|
|
73
94
|
"repository": {
|
|
74
95
|
"type": "git",
|
|
75
|
-
"url": "https://github.com/NodePit/svg-to-img"
|
|
96
|
+
"url": "git+https://github.com/NodePit/svg-to-img.git"
|
|
76
97
|
},
|
|
77
98
|
"bugs": {
|
|
78
99
|
"url": "https://github.com/NodePit/svg-to-img/issues"
|
|
79
100
|
},
|
|
80
101
|
"engines": {
|
|
81
|
-
"node": ">=
|
|
102
|
+
"node": ">= 22.12.0"
|
|
82
103
|
},
|
|
83
104
|
"license": "MIT",
|
|
84
105
|
"devDependencies": {
|
|
85
|
-
"@
|
|
86
|
-
"@
|
|
87
|
-
"@types/
|
|
88
|
-
"@
|
|
89
|
-
"@
|
|
90
|
-
"
|
|
91
|
-
"eslint": "^8.
|
|
92
|
-
"eslint
|
|
93
|
-
"
|
|
94
|
-
"
|
|
95
|
-
"
|
|
96
|
-
"jest
|
|
97
|
-
"
|
|
98
|
-
"
|
|
99
|
-
"
|
|
106
|
+
"@swc/core": "^1.16.18",
|
|
107
|
+
"@swc/jest": "^0.2.39",
|
|
108
|
+
"@types/jest": "^29.5.14",
|
|
109
|
+
"@types/node": "~22.12.0",
|
|
110
|
+
"@types/pngjs": "^6.0.5",
|
|
111
|
+
"@typescript-eslint/eslint-plugin": "^8.71.1",
|
|
112
|
+
"@typescript-eslint/parser": "^8.71.1",
|
|
113
|
+
"eslint": "^8.57.1",
|
|
114
|
+
"eslint-plugin-prettier": "^5.5.6",
|
|
115
|
+
"husky": "^9.1.7",
|
|
116
|
+
"image-size": "^2.0.4",
|
|
117
|
+
"jest": "^29.7.0",
|
|
118
|
+
"jest-canvas-mock": "^2.5.8",
|
|
119
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
120
|
+
"pngjs": "^7.0.0",
|
|
121
|
+
"prettier": "^3.9.10",
|
|
122
|
+
"puppeteer": "^25.13.0",
|
|
100
123
|
"ts-expect": "^1.3.0",
|
|
101
|
-
"ts-jest": "
|
|
102
|
-
"typescript": "
|
|
124
|
+
"ts-jest": "^29.4.14",
|
|
125
|
+
"typescript": "^5.9.3"
|
|
103
126
|
},
|
|
104
127
|
"dependencies": {
|
|
105
|
-
"puppeteer-core": "13.
|
|
106
|
-
}
|
|
128
|
+
"puppeteer-core": "^25.13.0"
|
|
129
|
+
},
|
|
130
|
+
"peerDependencies": {
|
|
131
|
+
"puppeteer": ">=25.13.0"
|
|
132
|
+
},
|
|
133
|
+
"peerDependenciesMeta": {
|
|
134
|
+
"puppeteer": {
|
|
135
|
+
"optional": true
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
"devEngines": {
|
|
139
|
+
"runtime": {
|
|
140
|
+
"name": "node",
|
|
141
|
+
"version": "24.19.0",
|
|
142
|
+
"onFail": "warn"
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
"publishConfig": {
|
|
146
|
+
"access": "public"
|
|
147
|
+
},
|
|
148
|
+
"packageManager": "pnpm@12.12.1"
|
|
107
149
|
}
|
package/changelog.md
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file.
|
|
4
|
-
|
|
5
|
-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
-
|
|
8
|
-
## [3.1.0] – 2022-07-30
|
|
9
|
-
### Changes
|
|
10
|
-
* Return types depending on whether a `Buffer` (no `encoding` specified) or `string` (`encoding` specified) is returned
|
|
11
|
-
|
|
12
|
-
## [3.0.0] – 2022-04-30
|
|
13
|
-
### Changed
|
|
14
|
-
* Set up GitHub CI
|
|
15
|
-
* Upgrade development dependencies (Typescript, ESLint, etc.) and related fixes
|
|
16
|
-
* (breaking) Depend on [`puppeteer-core`](https://github.com/puppeteer/puppeteer#puppeteer-core) to avoid Chromium download. If you rely on the included Chromium, simply add a dependency to `puppeteer@2.0.0` to your project
|
|
17
|
-
* Upgrade to latest Puppeteer library (v13.7.0)
|
|
18
|
-
|
|
19
|
-
### Breaking
|
|
20
|
-
* Require at least Node version 10.18.1
|
|
21
|
-
|
|
22
|
-
## [2.2.0] – 2020-12-14
|
|
23
|
-
### Added
|
|
24
|
-
* Export TS interfaces `IOptions`, `IShorthandOptions`, and `IConnectOptions`
|
|
25
|
-
|
|
26
|
-
## [2.1.1] – 2020-04-15
|
|
27
|
-
### Fixed
|
|
28
|
-
* Update README.md to reflect fork
|
|
29
|
-
|
|
30
|
-
## [2.1.0] – 2020-04-15
|
|
31
|
-
### Added
|
|
32
|
-
* Forked from Etienne Martin’s <a href="https://github.com/etienne-martin/svg-to-img">svg-to-img</a> and added possibility to connect to a Puppeteer endpoint (see <a href="https://github.com/etienne-martin/svg-to-img/pull/19">here</a>)
|