@paprize/puppeteer 0.4.2 → 0.4.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @paprize/puppeteer
2
2
 
3
+ ## 0.4.3
4
+
5
+ ### Patch Changes
6
+
7
+ - b944811: Fix CommonJS entry points so `require("@paprize/react")` and `require("@paprize/puppeteer")` resolve correctly (fixes #46). Declare Node.js >=22.12.0 as the supported runtime for `@paprize/puppeteer`.
8
+
3
9
  ## 0.4.2
4
10
 
5
11
  ### Patch Changes
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ openReport: () => openReport,
34
+ pageToPdf: () => pageToPdf,
35
+ reportToPdf: () => reportToPdf,
36
+ serveReport: () => serveReport
37
+ });
38
+ module.exports = __toCommonJS(index_exports);
39
+
40
+ // src/options.ts
41
+ var paprizeOptions = {
42
+ waitForFonts: true,
43
+ printBackground: true,
44
+ displayHeaderFooter: false,
45
+ headerTemplate: void 0,
46
+ footerTemplate: void 0,
47
+ preferCSSPageSize: true,
48
+ margin: {
49
+ bottom: "0px",
50
+ left: "0px",
51
+ right: "0px",
52
+ top: "0px"
53
+ }
54
+ };
55
+
56
+ // src/pageToPdf.ts
57
+ function pageToPdf(page, options) {
58
+ return page.pdf({
59
+ ...paprizeOptions,
60
+ ...options
61
+ });
62
+ }
63
+
64
+ // src/openReport.ts
65
+ var import_core2 = require("@paprize/core");
66
+
67
+ // src/setupLogger.ts
68
+ var import_core = require("@paprize/core");
69
+ function setupLogger(page) {
70
+ page.on("console", (msg) => {
71
+ const type = msg.type();
72
+ const text = msg.text();
73
+ switch (type) {
74
+ case "error":
75
+ import_core.logger.error(`error: ${text}`);
76
+ break;
77
+ case "warn":
78
+ import_core.logger.warn(`warn: ${text}`);
79
+ break;
80
+ case "debug":
81
+ import_core.logger.debug(`debug: ${text}`);
82
+ break;
83
+ case "info":
84
+ import_core.logger.info(`info: ${text}`);
85
+ break;
86
+ default:
87
+ import_core.logger.log(`log: ${text}`);
88
+ }
89
+ });
90
+ }
91
+
92
+ // src/openReport.ts
93
+ async function openReport(page, reportUrl, jsonData, timeout = 3e4) {
94
+ setupLogger(page);
95
+ const jsonDataStr = jsonData ? JSON.stringify(jsonData) : null;
96
+ await page.exposeFunction(import_core2.paprize_readJsonDataFile, () => {
97
+ return jsonDataStr;
98
+ });
99
+ await page.goto(reportUrl.toString());
100
+ const task1 = page.addScriptTag({
101
+ type: "module",
102
+ path: `${__dirname}/paprize-zero.js`
103
+ });
104
+ const task2 = page.waitForNetworkIdle({
105
+ idleTime: 500,
106
+ timeout
107
+ });
108
+ await Promise.all([task1, task2]);
109
+ await page.waitForFunction(`window.${import_core2.paprize_isInitialized} === true`, {
110
+ timeout: 100
111
+ }).catch(() => {
112
+ import_core2.logger.info(
113
+ "Using Zero report mode (standard Paprize report format not detected)"
114
+ );
115
+ return page.evaluate(() => {
116
+ return window.runZeroReport();
117
+ });
118
+ });
119
+ await page.waitForFunction(`window.${import_core2.paprize_isReady} === true`, {
120
+ timeout
121
+ }).catch(() => {
122
+ throw new Error(
123
+ "The report did not become ready within the specified timeout."
124
+ );
125
+ });
126
+ }
127
+
128
+ // src/serveReport.ts
129
+ var import_http = __toESM(require("http"), 1);
130
+ var import_serve_handler = __toESM(require("serve-handler"), 1);
131
+ var import_fs = require("fs");
132
+ async function serveReport(dir, port) {
133
+ const staticDirectories = (await import_fs.promises.readdir(dir, { withFileTypes: true })).filter((x) => x.isDirectory()).map((x) => ({ source: x.name, destination: `/${x.name}` }));
134
+ const server = import_http.default.createServer(
135
+ async (req, res) => {
136
+ await (0, import_serve_handler.default)(req, res, {
137
+ public: dir,
138
+ rewrites: [
139
+ ...staticDirectories,
140
+ { source: "**", destination: "/index.html" }
141
+ ]
142
+ });
143
+ }
144
+ );
145
+ server.listen(port);
146
+ const address = server.address();
147
+ return {
148
+ server,
149
+ close: () => server.close(),
150
+ getHost: () => `http://localhost:${address.port}`
151
+ };
152
+ }
153
+
154
+ // src/reportToPdf.ts
155
+ async function reportToPdf(page, reportUrl, jsonData, pdfOptions) {
156
+ await openReport(page, reportUrl, jsonData, pdfOptions?.timeout);
157
+ return await pageToPdf(page, pdfOptions);
158
+ }
159
+ // Annotate the CommonJS export names for ESM import in node:
160
+ 0 && (module.exports = {
161
+ openReport,
162
+ pageToPdf,
163
+ reportToPdf,
164
+ serveReport
165
+ });
166
+ //# sourceMappingURL=paprize-puppeteer.umd.cjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts", "../src/options.ts", "../src/pageToPdf.ts", "../src/openReport.ts", "../src/setupLogger.ts", "../src/serveReport.ts", "../src/reportToPdf.ts"],
4
+ "sourcesContent": ["export * from './pageToPdf';\nexport * from './openReport';\nexport * from './serveReport';\nexport * from './reportToPdf';\nexport type * from './options';\n", "import type { PDFOptions } from 'puppeteer-core';\n\n/**\n * Predefined static Paprize PDF options.\n *\n * including:\n * - waitForFonts: true\n * - printBackground: true\n * - displayHeaderFooter: false\n * - preferCSSPageSize: true\n * - margin: all sides 0px\n */\nexport const paprizeOptions: PDFOptions = {\n waitForFonts: true,\n printBackground: true,\n displayHeaderFooter: false,\n headerTemplate: undefined,\n footerTemplate: undefined,\n preferCSSPageSize: true,\n margin: {\n bottom: '0px',\n left: '0px',\n right: '0px',\n top: '0px',\n },\n} as const;\n\n/**\n * Options for generating a PDF report with Paprize.\n * extends Puppeteer's [PDFOptions](https://pptr.dev/api/puppeteer.pdfoptions)\n * , excluding options that are already set by Paprize {@link paprizeOptions}\n */\nexport type ReportOptions = Omit<PDFOptions, keyof typeof paprizeOptions>;\n", "import type { Page } from 'puppeteer-core';\nimport { paprizeOptions, type ReportOptions } from './options';\n\n/**\n * Generates a PDF from the given Puppeteer `Page` instance using the specified options.\n *\n * @param page - The Puppeteer `Page` instance to generate the PDF from.\n * @param options - Optional additional options to customize the PDF generation. These options\n * will be merged with the default settings.\n * @returns A promise that resolves to a `Uint8Array` containing the generated PDF data.\n */\nexport function pageToPdf(\n page: Page,\n options?: ReportOptions\n): Promise<Uint8Array> {\n return page.pdf({\n ...paprizeOptions,\n ...options,\n });\n}\n", "import type { Page } from 'puppeteer-core';\nimport {\n logger,\n paprize_isInitialized,\n paprize_isReady,\n paprize_readJsonDataFile,\n} from '@paprize/core';\nimport { setupLogger } from './setupLogger';\n\n/**\n * Opens a report in the given Puppeteer page, injects a JSON data file if provided,\n * and waits for the report to be ready.\n *\n * @param page - The Puppeteer `Page` instance where the report will be opened.\n * @param reportUrl - The URL of the report to be opened.\n * @param jsonData - Optional JSON data to be injected into the page.\n * @param timeout - The maximum time to wait for the report to be ready, in milliseconds. Defaults to 30,000 ms.\n * @returns A promise that resolves when the report is ready.\n * @throws Will throw an error if the report does not become ready within the specified timeout.\n */\nexport async function openReport(\n page: Page,\n reportUrl: URL,\n jsonData: unknown | undefined,\n timeout: number = 30000\n): Promise<void> {\n setupLogger(page);\n\n const jsonDataStr = jsonData ? JSON.stringify(jsonData) : null;\n await page.exposeFunction(paprize_readJsonDataFile, () => {\n return jsonDataStr;\n });\n\n await page.goto(reportUrl.toString());\n\n // preload the paprize-zero.js\n const task1 = page.addScriptTag({\n type: 'module',\n path: `${import.meta.dirname}/paprize-zero.js`,\n });\n\n const task2 = page.waitForNetworkIdle({\n idleTime: 500,\n timeout: timeout,\n });\n\n await Promise.all([task1, task2]);\n\n await page\n .waitForFunction(`window.${paprize_isInitialized} === true`, {\n timeout: 100,\n })\n .catch(() => {\n logger.info(\n 'Using Zero report mode (standard Paprize report format not detected)'\n );\n\n return page.evaluate(() => {\n return window.runZeroReport();\n });\n });\n\n await page\n .waitForFunction(`window.${paprize_isReady} === true`, {\n timeout,\n })\n .catch(() => {\n throw new Error(\n 'The report did not become ready within the specified timeout.'\n );\n });\n}\n", "import { logger } from '@paprize/core';\nimport type { Page } from 'puppeteer-core';\n\nexport function setupLogger(page: Page) {\n page.on('console', (msg) => {\n const type = msg.type();\n const text = msg.text();\n\n switch (type) {\n case 'error':\n logger.error(`error: ${text}`);\n break;\n case 'warn':\n logger.warn(`warn: ${text}`);\n break;\n case 'debug':\n logger.debug(`debug: ${text}`);\n break;\n case 'info':\n logger.info(`info: ${text}`);\n break;\n default:\n logger.log(`log: ${text}`);\n }\n });\n}\n", "import http from 'http';\nimport type { AddressInfo } from 'node:net';\nimport handler from 'serve-handler';\nimport { promises as fs } from 'fs';\n\nexport interface ReportServer {\n server: http.Server;\n close: () => void;\n getHost: () => string;\n}\n\n/**\n * Serves a static report from the specified directory over HTTP.\n *\n * @param dir - The directory containing the static files to serve.\n * @param port - Optional. The port on which the server will listen. If not provided, a random available port will be used.\n * @returns A promise that resolves to a `ReportServer` object, which provides methods to close the server and retrieve its host URL.\n *\n * The server serves files from the specified directory and automatically rewrites requests\n * to `/index.html` for any unmatched routes. Subdirectories in the specified directory are\n * mapped to corresponding routes.\n */\nexport async function serveReport(\n dir: string,\n port?: number\n): Promise<ReportServer> {\n const staticDirectories = (await fs.readdir(dir, { withFileTypes: true }))\n .filter((x) => x.isDirectory())\n .map((x) => ({ source: x.name, destination: `/${x.name}` }));\n\n const server = http.createServer(\n async (req: http.IncomingMessage, res: http.ServerResponse) => {\n await handler(req, res, {\n public: dir,\n rewrites: [\n ...staticDirectories,\n { source: '**', destination: '/index.html' },\n ],\n });\n }\n );\n\n server.listen(port);\n const address = server.address() as AddressInfo;\n\n return {\n server,\n close: () => server.close(),\n getHost: () => `http://localhost:${address.port}`,\n };\n}\n", "import type { Page, PDFOptions } from 'puppeteer-core';\nimport { openReport } from './openReport';\nimport { pageToPdf } from './pageToPdf';\n\n/**\n * Generates a PDF from a report by navigating to the specified report URL\n * and optionally sending JSON data to the page.\n *\n * @param page - The Puppeteer `Page` instance used to interact with the report.\n * @param reportUrl - The URL of the report to be converted to a PDF.\n * @param jsonData - Optional JSON data to be sent to the report page.\n * @param pdfOptions - Optional configuration for generating the PDF, including timeout and other settings.\n * @returns A promise that resolves to a `Uint8Array` containing the generated PDF data.\n */\nexport async function reportToPdf(\n page: Page,\n reportUrl: URL,\n jsonData?: unknown,\n pdfOptions?: PDFOptions\n): Promise<Uint8Array> {\n await openReport(page, reportUrl, jsonData, pdfOptions?.timeout);\n return await pageToPdf(page, pdfOptions);\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACYO,IAAM,iBAA6B;AAAA,EACtC,cAAc;AAAA,EACd,iBAAiB;AAAA,EACjB,qBA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AM,UAAU;AAC3C;",
6
+ "names": ["import_core", "fs", "http", "handler"]
7
+ }
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@paprize/puppeteer",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "Convert HTML reports to print-ready PDFs",
5
5
  "type": "module",
6
+ "engines": {
7
+ "node": ">=22.12.0"
8
+ },
6
9
  "files": [
7
10
  "dist",
8
11
  "CHANGELOG.md"