polotno-node 0.2.4 → 0.3.3

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/dist/client.html CHANGED
@@ -1 +1 @@
1
- <html><head><title>Canvas editor</title><style>body{padding:0;margin:0}</style><meta name="viewport" content="width=device-width, initial-scale=1"></head><body class="bp3-dark"> <div id="root"></div> <script src="client.3b178911.js"></script> </body></html>
1
+ <html><head><title>Canvas editor</title><style>body{padding:0;margin:0}</style><meta name="viewport" content="width=device-width, initial-scale=1"></head><body class="bp3-dark"> <div id="root"></div> <script src="client.6ea1044c.js"></script> </body></html>
package/index.js CHANGED
@@ -1,30 +1,7 @@
1
1
  const chrome = require('chrome-aws-lambda');
2
2
  const puppeteer = require('puppeteer');
3
- const path = require('path');
4
3
 
5
- const DEFAULT_CLIENT = `file:${path.join(__dirname, 'dist', 'client.html')}`;
6
-
7
- const getNewPage = async (browser, url) => {
8
- const page = await browser.newPage();
9
-
10
- page.on('console', (msg) => {
11
- msg.args().forEach((message) => {
12
- // skip style information
13
- if (message.toString().indexOf('margin') >= 0) {
14
- return;
15
- }
16
- const text = message
17
- .toString()
18
- .replace('JSHandle:', '')
19
- .replace('%c', '');
20
- console.log(text);
21
- });
22
- });
23
-
24
- await page.goto(url);
25
-
26
- return page;
27
- };
4
+ const { createInstance } = require('./instance');
28
5
 
29
6
  module.exports.createInstance = async ({
30
7
  key,
@@ -50,64 +27,12 @@ module.exports.createInstance = async ({
50
27
  headless: true,
51
28
  ignoreHTTPSErrors: true,
52
29
  });
53
- const visitPage = url || `${DEFAULT_CLIENT}?key=${key}`;
54
-
55
- const firstPage = await getNewPage(browser, visitPage);
56
30
 
57
- const run = async (func, ...args) => {
58
- const page = useParallelPages
59
- ? await getNewPage(browser, visitPage)
60
- : firstPage;
61
-
62
- const result = await page.evaluate(func, ...args);
63
- if (useParallelPages) {
64
- page.close();
65
- }
66
- return result;
67
- };
68
-
69
- const jsonToDataURL = async (json, attrs) => {
70
- return await run(
71
- async (json, attrs) => {
72
- const pixelRatio = attrs.pixelRatio || 1;
73
- store.loadJSON(json);
74
- store.setElementsPixelRatio(pixelRatio);
75
- await store.waitLoading();
76
- return store.toDataURL({ ...attrs, pixelRatio });
77
- },
78
- json,
79
- attrs || {}
80
- );
81
- };
82
-
83
- const jsonToImageBase64 = async (json, attrs) => {
84
- const url = await jsonToDataURL(json, attrs);
85
- return url.split('base64,')[1];
86
- };
87
-
88
- const jsonToPDFDataURL = async (json, attrs) => {
89
- return await run(
90
- async (json, attrs) => {
91
- store.loadJSON(json);
92
- await store.waitLoading();
93
- return await store.toPDFDataURL(attrs);
94
- },
95
- json,
96
- attrs || {}
97
- );
98
- };
99
-
100
- const jsonToPDFBase64 = async (json, attrs) => {
101
- const url = await jsonToPDFDataURL(json, attrs);
102
- return url.split('base64,')[1];
103
- };
104
-
105
- return {
106
- close: async () => await browser.close(),
107
- run,
108
- jsonToDataURL,
109
- jsonToImageBase64,
110
- jsonToPDFDataURL,
111
- jsonToPDFBase64,
112
- };
31
+ return await createInstance({
32
+ browser,
33
+ key,
34
+ url,
35
+ useParallelPages,
36
+ browserArgs,
37
+ });
113
38
  };
package/instance.js ADDED
@@ -0,0 +1,123 @@
1
+ const path = require('path');
2
+ const DEFAULT_CLIENT = `file:${path.join(__dirname, 'dist', 'client.html')}`;
3
+
4
+ module.exports.createPage = async (browser, url) => {
5
+ const page = await browser.newPage();
6
+
7
+ page.on('console', (msg) => {
8
+ msg.args().forEach((message) => {
9
+ // skip style information
10
+ if (message.toString().indexOf('margin') >= 0) {
11
+ return;
12
+ }
13
+ const text = message
14
+ .toString()
15
+ .replace('JSHandle:', '')
16
+ .replace('%c', '');
17
+ console.log(text);
18
+ });
19
+ });
20
+
21
+ await page.goto(url);
22
+
23
+ return page;
24
+ };
25
+
26
+ module.exports.run = async (page, func, args) => {
27
+ return await page.evaluate(func, ...args);
28
+ };
29
+
30
+ module.exports.jsonToDataURL = async (page, json, attrs) => {
31
+ return await page.evaluate(
32
+ async (json, attrs) => {
33
+ const pixelRatio = attrs.pixelRatio || 1;
34
+ store.loadJSON(json);
35
+ store.setElementsPixelRatio(pixelRatio);
36
+ await store.waitLoading();
37
+ return store.toDataURL({ ...attrs, pixelRatio });
38
+ },
39
+ json,
40
+ attrs || {}
41
+ );
42
+ };
43
+
44
+ module.exports.jsonToPDFDataURL = async (page, json, attrs) => {
45
+ return page.evaluate(
46
+ async (json, attrs) => {
47
+ store.loadJSON(json);
48
+ await store.waitLoading();
49
+ return await store.toPDFDataURL(attrs);
50
+ },
51
+ json,
52
+ attrs || {}
53
+ );
54
+ };
55
+
56
+ module.exports.createInstance = async ({
57
+ key,
58
+ url,
59
+ useParallelPages,
60
+ browser,
61
+ } = {}) => {
62
+ const visitPage = url || `${DEFAULT_CLIENT}?key=${key}`;
63
+ const firstPage = useParallelPages
64
+ ? null
65
+ : await module.exports.createPage(browser, visitPage);
66
+
67
+ const run = async (func, ...args) => {
68
+ const page = useParallelPages
69
+ ? await module.exports.createPage(browser, visitPage)
70
+ : firstPage;
71
+
72
+ const result = await page.evaluate(func, ...args);
73
+ if (useParallelPages) {
74
+ page.close();
75
+ }
76
+ return result;
77
+ };
78
+
79
+ const jsonToDataURL = async (json, attrs) => {
80
+ return await run(
81
+ async (json, attrs) => {
82
+ const pixelRatio = attrs.pixelRatio || 1;
83
+ store.loadJSON(json);
84
+ store.setElementsPixelRatio(pixelRatio);
85
+ await store.waitLoading();
86
+ return store.toDataURL({ ...attrs, pixelRatio });
87
+ },
88
+ json,
89
+ attrs || {}
90
+ );
91
+ };
92
+
93
+ const jsonToImageBase64 = async (json, attrs) => {
94
+ const url = await jsonToDataURL(json, attrs);
95
+ return url.split('base64,')[1];
96
+ };
97
+
98
+ const jsonToPDFDataURL = async (json, attrs) => {
99
+ return await run(
100
+ async (json, attrs) => {
101
+ store.loadJSON(json);
102
+ await store.waitLoading();
103
+ return await store.toPDFDataURL(attrs);
104
+ },
105
+ json,
106
+ attrs || {}
107
+ );
108
+ };
109
+
110
+ const jsonToPDFBase64 = async (json, attrs) => {
111
+ const url = await jsonToPDFDataURL(json, attrs);
112
+ return url.split('base64,')[1];
113
+ };
114
+
115
+ return {
116
+ close: async () => await browser.close(),
117
+ run,
118
+ jsonToDataURL,
119
+ jsonToImageBase64,
120
+ jsonToPDFDataURL,
121
+ jsonToPDFBase64,
122
+ };
123
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polotno-node",
3
- "version": "0.2.4",
3
+ "version": "0.3.3",
4
4
  "description": "Polotno workflow from NodeJS",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,11 +10,12 @@
10
10
  "files": [
11
11
  "dist",
12
12
  "index.js",
13
+ "instance.js",
13
14
  "README.md"
14
15
  ],
15
16
  "devDependencies": {
16
17
  "parcel-bundler": "^1.12.4",
17
- "polotno": "^0.38.2",
18
+ "polotno": "^0.39.3",
18
19
  "react": "^17.0.2",
19
20
  "react-dom": "^17.0.2"
20
21
  },