@nodepit/svg-to-img 3.1.0 → 4.0.0-beta.0

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 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 -S
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 `npm run test` and update the [tests](https://github.com/etienne-martin/svg-to-img/tree/master/src/tests) if needed.
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
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { IBoundingBox, IOptions } from "./typings";
3
2
  export declare const getFileTypeFromPath: (path: string) => string;
4
3
  export declare const stringifyFunction: (func: Function, ...argsArray: (string | object | number)[]) => string;
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/ban-types
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 = async (path, data) => {
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.substr(`data:image/${options.type};base64,`.length);
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,18 @@
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");
4
+ /// <reference types="node" preserve="true" />
5
+ const puppeteer_core_1 = require("puppeteer-core");
5
6
  const helpers_1 = require("./helpers");
6
7
  const constants_1 = require("./constants");
7
8
  class BrowserSource {
9
+ factory;
10
+ queue = [];
11
+ browserDestructionTimeout;
12
+ browserInstance;
13
+ browserState = "closed";
8
14
  constructor(factory) {
9
15
  this.factory = factory;
10
- this.queue = [];
11
- this.browserState = "closed";
12
16
  }
13
17
  async getBrowser() {
14
18
  return new Promise((resolve, reject) => {
@@ -29,7 +33,7 @@ class BrowserSource {
29
33
  })
30
34
  .catch((error) => {
31
35
  this.browserState = "closed";
32
- reject(error);
36
+ reject(unwrapConnectionError(error));
33
37
  });
34
38
  return;
35
39
  }
@@ -70,6 +74,8 @@ class BrowserSource {
70
74
  }
71
75
  exports.BrowserSource = BrowserSource;
72
76
  class Svg {
77
+ svg;
78
+ browserSource;
73
79
  constructor(svg, browserSource) {
74
80
  this.svg = svg;
75
81
  this.browserSource = browserSource;
@@ -100,7 +106,7 @@ class Svg {
100
106
  options.type = fileType;
101
107
  }
102
108
  }
103
- const base64 = await page.evaluate((0, helpers_1.stringifyFunction)(helpers_1.renderSvg, svg, {
109
+ const base64 = (await page.evaluate((0, helpers_1.stringifyFunction)(helpers_1.renderSvg, svg, {
104
110
  width: options.width,
105
111
  height: options.height,
106
112
  type: options.type,
@@ -108,7 +114,7 @@ class Svg {
108
114
  background: options.background,
109
115
  clip: options.clip,
110
116
  jpegBackground: constants_1.config.jpegBackground
111
- }));
117
+ })));
112
118
  browserSource.scheduleBrowserForDestruction();
113
119
  const buffer = Buffer.from(base64, "base64");
114
120
  if (options.path) {
@@ -124,7 +130,18 @@ class Svg {
124
130
  }
125
131
  }
126
132
  exports.Svg = Svg;
133
+ // Puppeteer 25 rejects a failed connection with an `ErrorEvent` wrapping Node's `AggregateError` (one error per
134
+ // resolved address), which has an empty message
135
+ /* istanbul ignore next */
136
+ const unwrapConnectionError = (error) => {
137
+ const cause = error?.error ?? error;
138
+ const { message, errors, code } = cause;
139
+ if (message || !Array.isArray(errors))
140
+ return cause;
141
+ return Object.assign(new Error(errors.map((e) => e.message).join("; "), { cause }), { code });
142
+ };
127
143
  class SvgToImg {
144
+ browserSource;
128
145
  constructor(browserSource) {
129
146
  this.browserSource = browserSource;
130
147
  }
@@ -135,11 +152,11 @@ class SvgToImg {
135
152
  exports.SvgToImg = SvgToImg;
136
153
  const defaultBrowserSource = new BrowserSource(async () => {
137
154
  try {
138
- // eslint-disable-next-line @typescript-eslint/no-var-requires
155
+ // eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
139
156
  const puppeteerFull = require("puppeteer");
140
157
  return puppeteerFull.launch(constants_1.config.puppeteer);
141
158
  }
142
- catch (err) {
159
+ catch {
143
160
  /* istanbul ignore next */
144
161
  throw new Error("NPM module `puppeteer` is not available. Either add it as " +
145
162
  " dependency to your project, or use the `connect` function and supply " +
@@ -152,6 +169,6 @@ const from = (svg) => {
152
169
  exports.from = from;
153
170
  /* istanbul ignore next */
154
171
  const connect = (options) => {
155
- return new SvgToImg(new BrowserSource(async () => puppeteer.connect(options)));
172
+ return new SvgToImg(new BrowserSource(async () => (0, puppeteer_core_1.connect)(options)));
156
173
  };
157
174
  exports.connect = connect;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodepit/svg-to-img",
3
- "version": "3.1.0",
3
+ "version": "4.0.0-beta.0",
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": "npm run lint",
36
+ "pretest": "pnpm run lint",
28
37
  "test": "jest src --coverage --verbose",
29
38
  "test:watch": "jest src --coverage --verbose --watch",
30
- "coverage": "coveralls < ./coverage/lcov.info",
31
- "prebuild": "rm -rf dist/ && npm run lint",
39
+ "prebuild": "rm -rf dist/ && pnpm run lint",
32
40
  "build": "tsc --pretty",
33
- "prepare": "npm run build && husky install"
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
- "testURL": "http://localhost"
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": ">= 10.18.1"
102
+ "node": ">= 22.12.0"
82
103
  },
83
104
  "license": "MIT",
84
105
  "devDependencies": {
85
- "@types/jest": "27.4.1",
86
- "@types/node": "10.17.60",
87
- "@types/rimraf": "3.0.2",
88
- "@typescript-eslint/eslint-plugin": "^5.21.0",
89
- "@typescript-eslint/parser": "^5.21.0",
90
- "coveralls": "3.1.1",
91
- "eslint": "^8.14.0",
92
- "eslint-plugin-prettier": "^4.0.0",
93
- "husky": "7.0.4",
94
- "image-size": "1.0.1",
95
- "jest": "27.5.1",
96
- "jest-canvas-mock": "2.4.0",
97
- "prettier": "2.6.2",
98
- "puppeteer": "13.7.0",
99
- "rimraf": "3.0.2",
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": "27.1.4",
102
- "typescript": "4.6.4"
124
+ "ts-jest": "^29.4.14",
125
+ "typescript": "^5.9.3"
103
126
  },
104
127
  "dependencies": {
105
- "puppeteer-core": "13.7.0"
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>)