polotno-node
Advanced tools
Comparing version 0.2.4 to 0.3.0
91
index.js
const chrome = require('chrome-aws-lambda'); | ||
const puppeteer = require('puppeteer'); | ||
const path = require('path'); | ||
const DEFAULT_CLIENT = `file:${path.join(__dirname, 'dist', 'client.html')}`; | ||
const { createInstance } = require('./instance'); | ||
const getNewPage = async (browser, url) => { | ||
const page = await browser.newPage(); | ||
page.on('console', (msg) => { | ||
msg.args().forEach((message) => { | ||
// skip style information | ||
if (message.toString().indexOf('margin') >= 0) { | ||
return; | ||
} | ||
const text = message | ||
.toString() | ||
.replace('JSHandle:', '') | ||
.replace('%c', ''); | ||
console.log(text); | ||
}); | ||
}); | ||
await page.goto(url); | ||
return page; | ||
}; | ||
module.exports.createInstance = async ({ | ||
@@ -53,62 +30,10 @@ key, | ||
}); | ||
const visitPage = url || `${DEFAULT_CLIENT}?key=${key}`; | ||
const firstPage = await getNewPage(browser, visitPage); | ||
const run = async (func, ...args) => { | ||
const page = useParallelPages | ||
? await getNewPage(browser, visitPage) | ||
: firstPage; | ||
const result = await page.evaluate(func, ...args); | ||
if (useParallelPages) { | ||
page.close(); | ||
} | ||
return result; | ||
}; | ||
const jsonToDataURL = async (json, attrs) => { | ||
return await run( | ||
async (json, attrs) => { | ||
const pixelRatio = attrs.pixelRatio || 1; | ||
store.loadJSON(json); | ||
store.setElementsPixelRatio(pixelRatio); | ||
await store.waitLoading(); | ||
return store.toDataURL({ ...attrs, pixelRatio }); | ||
}, | ||
json, | ||
attrs || {} | ||
); | ||
}; | ||
const jsonToImageBase64 = async (json, attrs) => { | ||
const url = await jsonToDataURL(json, attrs); | ||
return url.split('base64,')[1]; | ||
}; | ||
const jsonToPDFDataURL = async (json, attrs) => { | ||
return await run( | ||
async (json, attrs) => { | ||
store.loadJSON(json); | ||
await store.waitLoading(); | ||
return await store.toPDFDataURL(attrs); | ||
}, | ||
json, | ||
attrs || {} | ||
); | ||
}; | ||
const jsonToPDFBase64 = async (json, attrs) => { | ||
const url = await jsonToPDFDataURL(json, attrs); | ||
return url.split('base64,')[1]; | ||
}; | ||
return { | ||
close: async () => await browser.close(), | ||
run, | ||
jsonToDataURL, | ||
jsonToImageBase64, | ||
jsonToPDFDataURL, | ||
jsonToPDFBase64, | ||
}; | ||
return await createInstance({ | ||
browser, | ||
key, | ||
url, | ||
useParallelPages, | ||
browserArgs, | ||
}); | ||
}; |
{ | ||
"name": "polotno-node", | ||
"version": "0.2.4", | ||
"version": "0.3.0", | ||
"description": "Polotno workflow from NodeJS", | ||
@@ -13,2 +13,3 @@ "main": "index.js", | ||
"index.js", | ||
"instance.js", | ||
"README.md" | ||
@@ -15,0 +16,0 @@ ], |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2008495
6
8124