polotno-node 1.1.0 → 2.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.
Files changed (3) hide show
  1. package/README.md +27 -0
  2. package/instance.js +38 -5
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -37,6 +37,33 @@ run();
37
37
 
38
38
  ## API
39
39
 
40
+ ### `createInstance(options)`
41
+
42
+ Create working instance of Polotno Node.
43
+
44
+ ```js
45
+ const { createInstance } = require('polotno-node');
46
+ const instance = await createInstance({
47
+ // this is a demo key just for that project
48
+ // (!) please don't use it in your projects
49
+ // to create your own API key please go here: https://polotno.dev/cabinet
50
+ key: 'nFA5H9elEytDyPyvKL7T',
51
+ // useParallelPages - use parallel pages to speed up rendering
52
+ // you can use false only for sequential calls
53
+ // it may break rendering if you call many parallel requests
54
+ // default is true
55
+ useParallelPages: false,
56
+ // url - url of the Polotno Client Editor
57
+ // client editor is just simple public html page that have `store` as global variable
58
+ // by default it will run local build
59
+ url: 'https://yourappdomain.com/client',
60
+ // browser - puppeteer browser instance
61
+ // by default it will use chrome-aws-lambda
62
+ // useful to set your own rendering props or use browserless
63
+ browser: browser,
64
+ });
65
+ ```
66
+
40
67
  ### `instance.jsonToDataURL(json, attrs)`
41
68
 
42
69
  Export json into data URL.
package/instance.js CHANGED
@@ -53,10 +53,22 @@ module.exports.jsonToPDFDataURL = async (page, json, attrs) => {
53
53
  );
54
54
  };
55
55
 
56
+ module.exports.jsonToBlob = async (page, json, attrs) => {
57
+ return page.evaluate(
58
+ async (json, attrs) => {
59
+ store.loadJSON(json);
60
+ await store.waitLoading();
61
+ return await store.toBlob(attrs);
62
+ },
63
+ json,
64
+ attrs || {}
65
+ );
66
+ };
67
+
56
68
  module.exports.createInstance = async ({
57
69
  key,
58
70
  url,
59
- useParallelPages,
71
+ useParallelPages = true,
60
72
  browser,
61
73
  } = {}) => {
62
74
  const visitPage = url || `${DEFAULT_CLIENT}?key=${key}`;
@@ -69,11 +81,18 @@ module.exports.createInstance = async ({
69
81
  ? await module.exports.createPage(browser, visitPage)
70
82
  : firstPage;
71
83
 
72
- const result = await page.evaluate(func, ...args);
73
- if (useParallelPages) {
74
- page.close();
84
+ try {
85
+ const result = await page.evaluate(func, ...args);
86
+ if (useParallelPages) {
87
+ page.close();
88
+ }
89
+ return result;
90
+ } catch (e) {
91
+ if (useParallelPages) {
92
+ page.close();
93
+ }
94
+ throw e;
75
95
  }
76
- return result;
77
96
  };
78
97
 
79
98
  const jsonToDataURL = async (json, attrs) => {
@@ -107,6 +126,18 @@ module.exports.createInstance = async ({
107
126
  );
108
127
  };
109
128
 
129
+ const jsonToBlob = async (json, attrs) => {
130
+ return await run(
131
+ async (json, attrs) => {
132
+ store.loadJSON(json);
133
+ await store.waitLoading();
134
+ return await store.toBlob(attrs);
135
+ },
136
+ json,
137
+ attrs || {}
138
+ );
139
+ };
140
+
110
141
  const jsonToPDFBase64 = async (json, attrs) => {
111
142
  const url = await jsonToPDFDataURL(json, attrs);
112
143
  return url.split('base64,')[1];
@@ -115,10 +146,12 @@ module.exports.createInstance = async ({
115
146
  return {
116
147
  close: async () => await browser.close(),
117
148
  firstPage,
149
+ browser,
118
150
  run,
119
151
  jsonToDataURL,
120
152
  jsonToImageBase64,
121
153
  jsonToPDFDataURL,
122
154
  jsonToPDFBase64,
155
+ jsonToBlob,
123
156
  };
124
157
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polotno-node",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "description": "Polotno workflow from NodeJS",
5
5
  "main": "index.js",
6
6
  "scripts": {