@nodepit/svg-to-img 2.2.0 → 3.0.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.
@@ -0,0 +1,46 @@
1
+ name: CI
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
10
+ - uses: actions/checkout@v2
11
+
12
+ - name: Setup Node.js environment
13
+ uses: actions/setup-node@v2
14
+ with:
15
+ # Version Spec of the version to use. Examples: 10.x, 10.15.1, >=10.15.0
16
+ node-version: ">=8.9.4"
17
+
18
+ # copy/paste from https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows#example-using-the-cache-action
19
+ - name: Cache node_modules
20
+ uses: actions/cache@v2
21
+ env:
22
+ cache-name: cache-node-modules
23
+ with:
24
+ path: ~/.npm
25
+ key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
26
+ restore-keys: |
27
+ ${{ runner.os }}-build-${{ env.cache-name }}-
28
+ ${{ runner.os }}-build-
29
+ ${{ runner.os }}-
30
+
31
+ - name: Install node dependencies
32
+ run: npm prune && npm install
33
+
34
+ - name: Install puppeteer dependencies
35
+ run: sudo apt-get update && sudo apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
36
+
37
+ - name: Build project
38
+ run: npm run build
39
+
40
+ - name: Run tests
41
+ run: npm run test -- --forceExit
42
+
43
+ - uses: codecov/codecov-action@v1
44
+ with:
45
+ file: ./coverage/coverage-final.json
46
+ fail_ci_if_error: true
@@ -0,0 +1,4 @@
1
+ #!/bin/sh
2
+ . "$(dirname "$0")/_/husky.sh"
3
+
4
+ npm run build && npm run test
@@ -0,0 +1,4 @@
1
+ #!/bin/sh
2
+ . "$(dirname "$0")/_/husky.sh"
3
+
4
+ npm run build && npm run test
package/README.md CHANGED
@@ -21,7 +21,7 @@ To use svg-to-img in your project, run:
21
21
  npm install @nodepit/svg-to-img -S
22
22
  ```
23
23
 
24
- Note: When you install svg-to-img, it downloads a recent version of Chromium (~170Mb Mac, ~282Mb Linux, ~280Mb Win) that is guaranteed to work with the library. If you want to skip downloading Chromium and instead [connect to a browser websocket endpoint](#svgtoimgconnectoptions), set the [`PUPPETEER_SKIP_CHROMIUM_DOWNLOAD`](https://github.com/puppeteer/puppeteer/blob/v2.0.0/docs/api.md#environment-variables) environment variable.
24
+ 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
25
 
26
26
  #### Debian
27
27
 
@@ -40,8 +40,6 @@ apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups
40
40
 
41
41
  ### Usage
42
42
 
43
- Caution: svg-to-img uses async/await which is only supported in Node v7.6.0 or greater.
44
-
45
43
  **Example** - converting a `svg` to `png`:
46
44
 
47
45
  ```javascript
@@ -118,7 +116,7 @@ const convert = svgToImg.connect({
118
116
  The method returns a svg instance based on the given argument.
119
117
 
120
118
  ### svgToImg.connect(options)
121
- - `options` <[Object]> Pupetteer [connect options](https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#puppeteerconnectoptions) object which will typically contain a `browserWSEndpoint` property.
119
+ - `options` <[Object]> Puppeteer [connect options](https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#puppeteerconnectoptions) object which will typically contain a `browserWSEndpoint` property.
122
120
  - returns: <[SvgToImg]> an instance which is connected with the specified Puppeteer endpoint.
123
121
 
124
122
  Use this method if you want to connect to a running Puppeteer endpoint (see [here](https://github.com/etienne-martin/svg-to-img/issues/9)). Note that the actual connection is established lazily; this means this method will always return, even when the given `browserWSEndpoint` cannot be reached -- in this case, errors will be thrown later when calling one of the `to` functions.
package/changelog.md ADDED
@@ -0,0 +1,28 @@
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.0.0] – 2022-04-30
9
+ ### Changed
10
+ * Set up GitHub CI
11
+ * Upgrade development dependencies (Typescript, ESLint, etc.) and related fixes
12
+ * (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
13
+ * Upgrade to latest Puppeteer library (v13.7.0)
14
+
15
+ ### Breaking
16
+ * Require at least Node version 10.18.1
17
+
18
+ ## [2.2.0] – 2020-12-14
19
+ ### Added
20
+ * Export TS interfaces `IOptions`, `IShorthandOptions`, and `IConnectOptions`
21
+
22
+ ## [2.1.1] – 2020-04-15
23
+ ### Fixed
24
+ * Update README.md to reflect fork
25
+
26
+ ## [2.1.0] – 2020-04-15
27
+ ### Added
28
+ * 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>)
package/dist/constants.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultWebpShorthandOptions = exports.defaultJpegShorthandOptions = exports.defaultPngShorthandOptions = exports.defaultOptions = exports.config = void 0;
3
4
  exports.config = {
4
5
  supportedImageTypes: ["jpeg", "png", "webp"],
5
6
  jpegBackground: "#fff",
package/dist/helpers.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  /// <reference types="node" />
2
- import { IBoundingBox } from "./typings";
2
+ import { IBoundingBox, IOptions } from "./typings";
3
3
  export declare const getFileTypeFromPath: (path: string) => string;
4
- export declare const stringifyFunction: (func: any, ...argsArray: any[]) => string;
5
- export declare const writeFileAsync: (path: string, data: Buffer) => Promise<{}>;
4
+ export declare const stringifyFunction: (func: Function, ...argsArray: (string | object | number)[]) => string;
5
+ export declare const writeFileAsync: (path: string, data: Buffer) => Promise<void>;
6
6
  export declare const renderSvg: (svg: string, options: {
7
- width?: number | undefined;
8
- height?: number | undefined;
9
- type: "jpeg" | "png" | "webp" | undefined;
10
- quality: number | undefined;
11
- background?: string | undefined;
12
- clip?: IBoundingBox | undefined;
7
+ width?: IOptions["width"];
8
+ height?: IOptions["height"];
9
+ type: IOptions["type"];
10
+ quality: IOptions["quality"];
11
+ background?: IOptions["background"];
12
+ clip?: IBoundingBox;
13
13
  jpegBackground: string;
14
- }) => Promise<{}>;
14
+ }) => Promise<unknown>;
package/dist/helpers.js CHANGED
@@ -1,10 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.renderSvg = exports.writeFileAsync = exports.stringifyFunction = exports.getFileTypeFromPath = void 0;
3
4
  const fs = require("fs");
4
- exports.getFileTypeFromPath = (path) => {
5
+ const getFileTypeFromPath = (path) => {
5
6
  return path.toLowerCase().replace(new RegExp("jpg", "g"), "jpeg").split(".").reverse()[0];
6
7
  };
7
- exports.stringifyFunction = (func, ...argsArray) => {
8
+ exports.getFileTypeFromPath = getFileTypeFromPath;
9
+ // eslint-disable-next-line @typescript-eslint/ban-types
10
+ const stringifyFunction = (func, ...argsArray) => {
8
11
  // Remove istanbul coverage instruments
9
12
  const functionString = func.toString().replace(/cov_(.+?)\+\+[,;]?/g, "");
10
13
  const args = [];
@@ -22,7 +25,8 @@ exports.stringifyFunction = (func, ...argsArray) => {
22
25
  }
23
26
  return `(${functionString})(${args.join(",")})`;
24
27
  };
25
- exports.writeFileAsync = async (path, data) => {
28
+ exports.stringifyFunction = stringifyFunction;
29
+ const writeFileAsync = async (path, data) => {
26
30
  return new Promise((resolve, reject) => {
27
31
  fs.writeFile(path, data, (err) => {
28
32
  if (err) {
@@ -32,7 +36,8 @@ exports.writeFileAsync = async (path, data) => {
32
36
  });
33
37
  });
34
38
  };
35
- exports.renderSvg = async (svg, options) => {
39
+ exports.writeFileAsync = writeFileAsync;
40
+ const renderSvg = async (svg, options) => {
36
41
  return new Promise((resolve, reject) => {
37
42
  const canvas = document.createElement("canvas");
38
43
  const ctx = canvas.getContext("2d");
@@ -104,3 +109,4 @@ exports.renderSvg = async (svg, options) => {
104
109
  img.src = "data:image/svg+xml;base64," + base64Svg;
105
110
  });
106
111
  };
112
+ exports.renderSvg = renderSvg;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import * as puppeteer from "puppeteer";
2
+ import * as puppeteer from "puppeteer-core";
3
3
  import { IOptions, IShorthandOptions, IConnectOptions } from "./typings";
4
4
  export declare class BrowserSource {
5
5
  private readonly factory;
@@ -27,6 +27,6 @@ export declare class SvgToImg {
27
27
  constructor(browserSource: BrowserSource);
28
28
  from(svg: Buffer | string): Svg;
29
29
  }
30
- export declare const from: (svg: string | Buffer) => Svg;
30
+ export declare const from: (svg: Buffer | string) => Svg;
31
31
  export declare const connect: (options: IConnectOptions) => SvgToImg;
32
32
  export { IOptions, IShorthandOptions, IConnectOptions };
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const puppeteer = require("puppeteer");
3
+ exports.connect = exports.from = exports.SvgToImg = exports.Svg = exports.BrowserSource = void 0;
4
+ const puppeteer = require("puppeteer-core");
4
5
  const helpers_1 = require("./helpers");
5
6
  const constants_1 = require("./constants");
6
7
  class BrowserSource {
@@ -10,7 +11,7 @@ class BrowserSource {
10
11
  this.browserState = "closed";
11
12
  }
12
13
  async getBrowser() {
13
- return new Promise(async (resolve, reject) => {
14
+ return new Promise((resolve, reject) => {
14
15
  /* istanbul ignore if */
15
16
  if (this.browserDestructionTimeout) {
16
17
  clearTimeout(this.browserDestructionTimeout);
@@ -20,15 +21,17 @@ class BrowserSource {
20
21
  // Browser is closed
21
22
  this.queue.push(resolve);
22
23
  this.browserState = "opening";
23
- try {
24
- this.browserInstance = await this.factory();
24
+ this.factory()
25
+ .then((browserInstance) => {
26
+ this.browserInstance = browserInstance;
25
27
  this.browserState = "open";
26
- return this.executeQueuedRequests(this.browserInstance);
27
- }
28
- catch (error) {
28
+ this.executeQueuedRequests(this.browserInstance);
29
+ })
30
+ .catch((error) => {
29
31
  this.browserState = "closed";
30
- return reject(error);
31
- }
32
+ reject(error);
33
+ });
34
+ return;
32
35
  }
33
36
  /* istanbul ignore next */
34
37
  if (this.browserState === "opening") {
@@ -44,13 +47,12 @@ class BrowserSource {
44
47
  }
45
48
  });
46
49
  }
47
- ;
48
50
  scheduleBrowserForDestruction() {
49
51
  /* istanbul ignore if */
50
52
  if (this.browserDestructionTimeout) {
51
53
  clearTimeout(this.browserDestructionTimeout);
52
54
  }
53
- this.browserDestructionTimeout = setTimeout(async () => {
55
+ this.browserDestructionTimeout = global.setTimeout(async () => {
54
56
  /* istanbul ignore next */
55
57
  if (this.browserInstance) {
56
58
  this.browserState = "closed";
@@ -58,7 +60,6 @@ class BrowserSource {
58
60
  }
59
61
  }, 500);
60
62
  }
61
- ;
62
63
  executeQueuedRequests(browser) {
63
64
  for (const resolve of this.queue) {
64
65
  resolve(browser);
@@ -66,10 +67,8 @@ class BrowserSource {
66
67
  // Clear items from the queue
67
68
  this.queue.length = 0;
68
69
  }
69
- ;
70
70
  }
71
71
  exports.BrowserSource = BrowserSource;
72
- ;
73
72
  class Svg {
74
73
  constructor(svg, browserSource) {
75
74
  this.svg = svg;
@@ -78,34 +77,30 @@ class Svg {
78
77
  async to(options) {
79
78
  return this.convertSvg(this.svg, options, this.browserSource);
80
79
  }
81
- ;
82
80
  async toPng(options) {
83
- return this.convertSvg(this.svg, Object.assign({}, constants_1.defaultPngShorthandOptions, options), this.browserSource);
81
+ return this.convertSvg(this.svg, { ...constants_1.defaultPngShorthandOptions, ...options }, this.browserSource);
84
82
  }
85
- ;
86
83
  async toJpeg(options) {
87
- return this.convertSvg(this.svg, Object.assign({}, constants_1.defaultJpegShorthandOptions, options), this.browserSource);
84
+ return this.convertSvg(this.svg, { ...constants_1.defaultJpegShorthandOptions, ...options }, this.browserSource);
88
85
  }
89
- ;
90
86
  async toWebp(options) {
91
- return this.convertSvg(this.svg, Object.assign({}, constants_1.defaultWebpShorthandOptions, options), this.browserSource);
87
+ return this.convertSvg(this.svg, { ...constants_1.defaultWebpShorthandOptions, ...options }, this.browserSource);
92
88
  }
93
- ;
94
89
  async convertSvg(inputSvg, passedOptions, browserSource) {
95
90
  const svg = Buffer.isBuffer(inputSvg) ? inputSvg.toString("utf8") : inputSvg;
96
- const options = Object.assign({}, constants_1.defaultOptions, passedOptions);
91
+ const options = { ...constants_1.defaultOptions, ...passedOptions };
97
92
  const browser = await browserSource.getBrowser();
98
93
  const page = (await browser.pages())[0];
99
94
  // ⚠️ Offline mode is enabled to prevent any HTTP requests over the network
100
95
  await page.setOfflineMode(true);
101
96
  // Infer the file type from the file path if no type is provided
102
97
  if (!passedOptions.type && options.path) {
103
- const fileType = helpers_1.getFileTypeFromPath(options.path);
98
+ const fileType = (0, helpers_1.getFileTypeFromPath)(options.path);
104
99
  if (constants_1.config.supportedImageTypes.includes(fileType)) {
105
100
  options.type = fileType;
106
101
  }
107
102
  }
108
- const base64 = await page.evaluate(helpers_1.stringifyFunction(helpers_1.renderSvg, svg, {
103
+ const base64 = await page.evaluate((0, helpers_1.stringifyFunction)(helpers_1.renderSvg, svg, {
109
104
  width: options.width,
110
105
  height: options.height,
111
106
  type: options.type,
@@ -117,7 +112,7 @@ class Svg {
117
112
  browserSource.scheduleBrowserForDestruction();
118
113
  const buffer = Buffer.from(base64, "base64");
119
114
  if (options.path) {
120
- await helpers_1.writeFileAsync(options.path, buffer);
115
+ await (0, helpers_1.writeFileAsync)(options.path, buffer);
121
116
  }
122
117
  if (options.encoding === "base64") {
123
118
  return base64;
@@ -127,7 +122,6 @@ class Svg {
127
122
  }
128
123
  return buffer.toString(options.encoding);
129
124
  }
130
- ;
131
125
  }
132
126
  exports.Svg = Svg;
133
127
  class SvgToImg {
@@ -137,14 +131,27 @@ class SvgToImg {
137
131
  from(svg) {
138
132
  return new Svg(svg, this.browserSource);
139
133
  }
140
- ;
141
134
  }
142
135
  exports.SvgToImg = SvgToImg;
143
- const defaultBrowserSource = new BrowserSource(async () => puppeteer.launch(constants_1.config.puppeteer));
144
- exports.from = (svg) => {
136
+ const defaultBrowserSource = new BrowserSource(async () => {
137
+ try {
138
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
139
+ const puppeteerFull = require("puppeteer");
140
+ return puppeteerFull.launch(constants_1.config.puppeteer);
141
+ }
142
+ catch (err) {
143
+ /* istanbul ignore next */
144
+ throw new Error("NPM module `puppeteer` is not available. Either add it as " +
145
+ " dependency to your project, or use the `connect` function and supply " +
146
+ "`browserWSEndpoint` if you want to use a running Puppeteer endpoint.");
147
+ }
148
+ });
149
+ const from = (svg) => {
145
150
  return new SvgToImg(defaultBrowserSource).from(svg);
146
151
  };
152
+ exports.from = from;
147
153
  /* istanbul ignore next */
148
- exports.connect = (options) => {
154
+ const connect = (options) => {
149
155
  return new SvgToImg(new BrowserSource(async () => puppeteer.connect(options)));
150
156
  };
157
+ exports.connect = connect;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodepit/svg-to-img",
3
- "version": "2.2.0",
3
+ "version": "3.0.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": [
@@ -23,20 +23,14 @@
23
23
  ],
24
24
  "scripts": {
25
25
  "dev": "tsc --pretty --watch",
26
- "lint": "tslint -c tslint.json -p tsconfig.json --fix",
26
+ "lint": "eslint --fix ./src",
27
27
  "pretest": "npm run lint",
28
28
  "test": "jest src --coverage --verbose",
29
29
  "test:watch": "jest src --coverage --verbose --watch",
30
30
  "coverage": "coveralls < ./coverage/lcov.info",
31
31
  "prebuild": "rm -rf dist/ && npm run lint",
32
32
  "build": "tsc --pretty",
33
- "prepare": "npm run build"
34
- },
35
- "husky": {
36
- "hooks": {
37
- "pre-commit": "npm run build && npm run test",
38
- "pre-push": "npm run build && npm run test"
39
- }
33
+ "prepare": "npm run build && husky install"
40
34
  },
41
35
  "jest": {
42
36
  "setupFiles": [
@@ -84,27 +78,29 @@
84
78
  "url": "https://github.com/NodePit/svg-to-img/issues"
85
79
  },
86
80
  "engines": {
87
- "node": ">= 7.6.0"
81
+ "node": ">= 10.18.1"
88
82
  },
89
83
  "license": "MIT",
90
84
  "devDependencies": {
91
- "@types/image-size": "0.0.29",
92
- "@types/jest": "22.2.3",
93
- "@types/puppeteer": "1.10.1",
94
- "@types/rimraf": "2.0.2",
95
- "coveralls": "3.0.2",
96
- "husky": "0.15.0-rc.13",
97
- "image-size": "0.6.3",
98
- "jest": "22.4.4",
99
- "jest-canvas-mock": "1.1.0",
100
- "rimraf": "2.6.2",
101
- "ts-jest": "22.4.6",
102
- "tslint": "5.11.0",
103
- "tslint-config-prettier": "1.17.0",
104
- "tslint-eslint-rules": "4.1.1",
105
- "typescript": "2.9.2"
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",
100
+ "ts-jest": "27.1.4",
101
+ "typescript": "4.6.4"
106
102
  },
107
103
  "dependencies": {
108
- "puppeteer": "2.0.0"
104
+ "puppeteer-core": "13.7.0"
109
105
  }
110
106
  }