Socket
Socket
Sign inDemoInstall

@muil/templates-renderer

Package Overview
Dependencies
Maintainers
3
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@muil/templates-renderer - npm Package Compare versions

Comparing version 1.5.4 to 2.0.0

19

package.json
{
"name": "@muil/templates-renderer",
"version": "1.5.4",
"version": "2.0.0",
"license": "MIT",

@@ -17,23 +17,20 @@ "main": "src/index.js",

"@nestjs/common": "^7.5.3",
"@sparticuz/chromium": "^123.0.1",
"he": "^1.2.0",
"html-minifier": "^4.0.0",
"juice": "^7.0.0",
"puppeteer-core": "^14.0.0",
"puppeteer-core": "^22.6.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^6.5.4",
"uuid": "^8.3.1",
"vm2": "3.9.19"
"styled-components": "^6.1.11",
"uuid": "^8.3.1"
},
"peerDependencies": {
"react": "^17.0.1",
"react-dom": "^17.0.1",
"styled-components": "^5.2.0"
},
"devDependencies": {
"@types/he": "^1.1.2",
"@types/html-minifier": "^4.0.0",
"@types/puppeteer-core": "^5.4.0",
"@types/require-from-string": "^1.2.0"
},
"gitHead": "44c1d31698cb6988b295e7b5c660b62f4e173338"
"gitHead": "eaccfeb02fc6c121d295ee9533b0315be0f1aa45"
}

@@ -1,2 +0,2 @@

declare type Options = {
type Options = {
css?: string;

@@ -3,0 +3,0 @@ styles?: string;

/// <reference types="node" />
import puppeteer from 'puppeteer-core';
import * as puppeteer from 'puppeteer-core';
export declare const generatePdf: (html: string, format?: puppeteer.PaperFormat, orientation?: 'portrait' | 'landscape') => Promise<Buffer>;
export declare const generatePng: (html: string) => Promise<string | Buffer>;
export declare const generatePng: (html: string) => Promise<Buffer>;
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {

@@ -26,4 +30,4 @@ if (k2 === undefined) k2 = k;

exports.generatePng = exports.generatePdf = void 0;
const fs = __importStar(require("fs"));
const puppeteer_core_1 = __importDefault(require("puppeteer-core"));
const chromium_1 = __importDefault(require("@sparticuz/chromium"));
const puppeteer = __importStar(require("puppeteer-core"));
const pageSize = {

@@ -42,28 +46,22 @@ a0: { height: '118.9cm', width: '84.1cm' },

};
let executablePath = '/usr/bin/chromium-browser';
let product = 'chrome';
if (process.platform === 'win32') {
if (fs.existsSync('C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe')) {
executablePath = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe';
const getBrowser = async () => {
const isProd = process.env.NODE_ENV === 'production';
let browser;
if (isProd) {
browser = await puppeteer.launch({
args: chromium_1.default.args,
defaultViewport: chromium_1.default.defaultViewport,
executablePath: await chromium_1.default.executablePath(),
ignoreHTTPSErrors: true,
});
}
else if (fs.existsSync('C:\\Program Files\\Mozilla Firefox\\firefox.exe')) {
product = 'firefox';
executablePath = 'C:\\Program Files\\Mozilla Firefox\\firefox.exe';
else {
browser = await puppeteer.launch({
executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
});
}
}
else if (process.platform === 'darwin') {
if (fs.existsSync('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome')) {
executablePath = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
}
else if (fs.existsSync('/Applications/Firefox.app/Contents/MacOS/firefox')) {
product = 'firefox';
executablePath = '/Applications/Firefox.app/Contents/MacOS/firefox';
}
}
return browser;
};
const generatePdf = async (html, format = 'a4', orientation = 'portrait') => {
const browser = await puppeteer_core_1.default.launch({
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || executablePath,
product,
args: product === 'chrome' ? ['--no-sandbox'] : undefined,
});
const browser = await getBrowser();
try {

@@ -86,7 +84,3 @@ const page = await browser.newPage();

const generatePng = async (html) => {
const browser = await puppeteer_core_1.default.launch({
executablePath: process.env.PUPPETEER_EXECUTABLE_PATH || executablePath,
product,
args: product === 'chrome' ? ['--no-sandbox'] : undefined,
});
const browser = await getBrowser();
try {

@@ -93,0 +87,0 @@ const page = await browser.newPage();

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {

@@ -25,2 +29,3 @@ if (k2 === undefined) k2 = k;

Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const fs = __importStar(require("fs"));

@@ -30,4 +35,4 @@ const he_1 = __importDefault(require("he"));

const juice_1 = __importDefault(require("juice"));
const server_1 = require("react-dom/server");
const uuid_1 = require("uuid");
const vm2_1 = require("vm2");
const emailTemplate_1 = __importDefault(require("./emailTemplate"));

@@ -37,42 +42,11 @@ const puppeteer_1 = require("./puppeteer");

const createReactElement = async (templatePath, props) => {
const reactElementVm = new vm2_1.NodeVM({
timeout: 5000,
sandbox: {
props,
},
require: {
external: ['react', 'react-dom'],
},
});
const tempFileName = `./.muil/temp/${uuid_1.v4()}.js`;
var _a;
const tempFileName = `./.muil/temp/${(0, uuid_1.v4)()}.js`;
await fs.promises.mkdir('./.muil/temp', { recursive: true });
await fs.promises.copyFile(templatePath, tempFileName);
const ReactElement = reactElementVm.run(`
const {createElement} = require('react');
const ReactComponent = require('${tempFileName}');
const ReactElement = createElement(ReactComponent.default, props);
module.exports = ReactElement;
`, 'renderTemplate.js');
const ReactComponent = await (_a = tempFileName, Promise.resolve().then(() => __importStar(require(_a))));
const ReactElement = (0, react_1.createElement)(ReactComponent.default, props);
await fs.promises.unlink(tempFileName);
return ReactElement;
};
const renderToStaticMarkup = (ReactElement) => {
const contentVm = new vm2_1.NodeVM({
timeout: 5000,
sandbox: {
ReactElement,
},
require: {
external: ['react-dom'],
},
});
const content = contentVm.run(`
const { renderToStaticMarkup } = require('react-dom/server');
const content = renderToStaticMarkup(ReactElement);
module.exports = content;
`, 'renderTemplate.js');
return content;
};
const renderTemplate = async ({ type = 'html', templatePath, templateCssPath = null, props = {}, styleCollectors = [styleCollectors_1.styledComponentsStyleCollector], shadowSupport = false, inlineCss = true, minifyHtml = true, pdfFormat = 'a4', pdfOrientation = 'portrait', }) => {

@@ -82,7 +56,7 @@ if (!templatePath)

const ReactElement = await createReactElement(templatePath, props);
const content = renderToStaticMarkup(ReactElement);
const content = (0, server_1.renderToStaticMarkup)(ReactElement);
const templateCss = templateCssPath && fs.existsSync(templateCssPath)
? fs.readFileSync(templateCssPath, 'utf-8')
: null;
let html = emailTemplate_1.default({
let html = (0, emailTemplate_1.default)({
css: templateCss,

@@ -94,3 +68,3 @@ styles: (await Promise.all(styleCollectors.map((collector) => collector(ReactElement)))).join('\n'),

if (type === 'html' && inlineCss) {
html = juice_1.default(html);
html = (0, juice_1.default)(html);
}

@@ -101,3 +75,3 @@ html = he_1.default.encode(html.toString(), {

if (minifyHtml) {
html = html_minifier_1.minify(html, {
html = (0, html_minifier_1.minify)(html, {
preventAttributesEscaping: true,

@@ -112,6 +86,6 @@ minifyCSS: true,

case 'png': {
return puppeteer_1.generatePng(html);
return (0, puppeteer_1.generatePng)(html);
}
case 'pdf': {
return puppeteer_1.generatePdf(html, pdfFormat, pdfOrientation);
return (0, puppeteer_1.generatePdf)(html, pdfFormat, pdfOrientation);
}

@@ -118,0 +92,0 @@ default: {

import { ReactNode } from 'react';
export declare const styledComponentsStyleCollector: (ReactElement: ReactNode) => any;
export declare const styledComponentsStyleCollector: (ReactElement: ReactNode) => string;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.styledComponentsStyleCollector = void 0;
const vm2_1 = require("vm2");
const styled_components_1 = require("styled-components");
const server_1 = require("react-dom/server");
const styledComponentsStyleCollector = (ReactElement) => {
const vm = new vm2_1.NodeVM({
timeout: 5000,
sandbox: {
ReactElement,
},
require: {
external: ['react', 'react-dom', 'styled-components'],
},
});
const styleTags = vm.run(`
const { renderToStaticMarkup } = require('react-dom/server');
const { ServerStyleSheet } = require('styled-components');
const sheet = new ServerStyleSheet();
renderToStaticMarkup(sheet.collectStyles(ReactElement));
module.exports = sheet.getStyleTags();
`, 'styleCollectors.js');
return styleTags;
const sheet = new styled_components_1.ServerStyleSheet();
(0, server_1.renderToStaticMarkup)(sheet.collectStyles(ReactElement));
return sheet.getStyleTags();
};
exports.styledComponentsStyleCollector = styledComponentsStyleCollector;
//# sourceMappingURL=styleCollectors.js.map

@@ -15,3 +15,3 @@ "use strict";

TemplatesRendererModule = __decorate([
common_1.Module({
(0, common_1.Module)({
providers: [templates_renderer_service_1.TemplatesRendererService],

@@ -18,0 +18,0 @@ exports: [templates_renderer_service_1.TemplatesRendererService],

@@ -17,3 +17,3 @@ "use strict";

async render({ type = 'html', templatePath, templateCssPath = null, props = {}, shadowSupport = false, inlineCss = true, minifyHtml = true, pdfFormat = 'a4', pdfOrientation = 'portrait', }) {
return renderTemplate_1.default({
return (0, renderTemplate_1.default)({
type,

@@ -32,5 +32,5 @@ templatePath,

TemplatesRendererService = __decorate([
common_1.Injectable()
(0, common_1.Injectable)()
], TemplatesRendererService);
exports.TemplatesRendererService = TemplatesRendererService;
//# sourceMappingURL=templates-renderer.service.js.map
import { PaperFormat } from 'puppeteer-core';
export declare type RenderOptions = {
export type RenderOptions = {
type?: 'html' | 'png' | 'pdf';

@@ -4,0 +4,0 @@ templatePath: string;

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc