@cellaware/utils 8.3.23 → 8.4.1
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/azure/cosmos.js +1 -1
- package/dist/browser.d.ts +129 -0
- package/dist/browser.js +70 -0
- package/package.json +3 -2
package/dist/azure/cosmos.js
CHANGED
|
@@ -243,7 +243,7 @@ export async function cosmosDelete(databaseId, collectionId, partitionKey, query
|
|
|
243
243
|
export async function _cosmosUpdate(endpoint, key, databaseId, collectionId, partitionKey, query, data) {
|
|
244
244
|
try {
|
|
245
245
|
if (Array.isArray(data)) {
|
|
246
|
-
throw new Error('Input `data` cannot be an array');
|
|
246
|
+
throw new Error('COSMOS: Input `data` cannot be an array');
|
|
247
247
|
}
|
|
248
248
|
const cosmosClient = new CosmosClient({
|
|
249
249
|
endpoint,
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { Browser } from "playwright-core";
|
|
2
|
+
/**
|
|
3
|
+
* Browserless (hosted) performance & reliability notes
|
|
4
|
+
*
|
|
5
|
+
* 1) Prefer connect-per-request (default)
|
|
6
|
+
* - We use chromium.connectOverCDP() instead of launching a local browser.
|
|
7
|
+
* - In serverless, connecting + closing per request is the safest and most cost-predictable option.
|
|
8
|
+
* - Hosted Browserless usage is typically metered by session/connection time, so leaving connections
|
|
9
|
+
* open while idle can increase cost.
|
|
10
|
+
*
|
|
11
|
+
* 2) Always isolate work in a fresh BrowserContext per PDF
|
|
12
|
+
* - Never reuse BrowserContext or Page across requests.
|
|
13
|
+
* - Context-per-request avoids cookie/storage leaks, cross-request interference, and memory growth.
|
|
14
|
+
*
|
|
15
|
+
* 3) Close deterministically (inner → outer)
|
|
16
|
+
* - Always close Page, then Context, then Browser (the CDP connection).
|
|
17
|
+
* - This releases remote resources quickly and prevents “dangling sessions” on the provider side.
|
|
18
|
+
*
|
|
19
|
+
* 4) Waiting strategy
|
|
20
|
+
* - Default waitUntil: "load" to avoid hanging on long-lived network activity (analytics/beacons).
|
|
21
|
+
* - If your HTML depends on external assets (remote CSS/fonts/images), consider "networkidle",
|
|
22
|
+
* or better: emit an explicit readiness signal and waitForFunction() for deterministic completion.
|
|
23
|
+
*
|
|
24
|
+
* 5) Biggest speed wins usually come from reducing network work
|
|
25
|
+
* - Inline critical CSS and avoid loading third-party scripts if possible.
|
|
26
|
+
* - Host/inline fonts and images where feasible to reduce variability and speed up rendering.
|
|
27
|
+
* - The less the page has to fetch, the faster and more consistent PDF generation becomes.
|
|
28
|
+
*
|
|
29
|
+
* 6) Optional burst optimization (only if needed)
|
|
30
|
+
* - If you generate many PDFs back-to-back on the same warm instance, you can reuse the CDP
|
|
31
|
+
* connection briefly (with a short idle timeout).
|
|
32
|
+
* - Still keep Context-per-request. Avoid keeping the connection open indefinitely to prevent idle cost.
|
|
33
|
+
*/
|
|
34
|
+
export declare function getBrowser(): Promise<Browser>;
|
|
35
|
+
export interface PdfOptions {
|
|
36
|
+
/**
|
|
37
|
+
* Display header and footer. Defaults to `false`.
|
|
38
|
+
*/
|
|
39
|
+
displayHeaderFooter?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* HTML template for the print footer. Should use the same format as the
|
|
42
|
+
* [`headerTemplate`](https://playwright.dev/docs/api/class-page#page-pdf-option-header-template).
|
|
43
|
+
*/
|
|
44
|
+
footerTemplate?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Paper format. If set, takes priority over
|
|
47
|
+
* [`width`](https://playwright.dev/docs/api/class-page#page-pdf-option-width) or
|
|
48
|
+
* [`height`](https://playwright.dev/docs/api/class-page#page-pdf-option-height) options. Defaults to 'Letter'.
|
|
49
|
+
*/
|
|
50
|
+
format?: string;
|
|
51
|
+
/**
|
|
52
|
+
* HTML template for the print header. Should be valid HTML markup with following classes used to inject printing
|
|
53
|
+
* values into them:
|
|
54
|
+
* - `'date'` formatted print date
|
|
55
|
+
* - `'title'` document title
|
|
56
|
+
* - `'url'` document location
|
|
57
|
+
* - `'pageNumber'` current page number
|
|
58
|
+
* - `'totalPages'` total pages in the document
|
|
59
|
+
*/
|
|
60
|
+
headerTemplate?: string;
|
|
61
|
+
/**
|
|
62
|
+
* Paper height, accepts values labeled with units.
|
|
63
|
+
*/
|
|
64
|
+
height?: string | number;
|
|
65
|
+
/**
|
|
66
|
+
* Paper orientation. Defaults to `false`.
|
|
67
|
+
*/
|
|
68
|
+
landscape?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Paper margins, defaults to none.
|
|
71
|
+
*/
|
|
72
|
+
margin?: {
|
|
73
|
+
/**
|
|
74
|
+
* Top margin, accepts values labeled with units. Defaults to `0`.
|
|
75
|
+
*/
|
|
76
|
+
top?: string | number;
|
|
77
|
+
/**
|
|
78
|
+
* Right margin, accepts values labeled with units. Defaults to `0`.
|
|
79
|
+
*/
|
|
80
|
+
right?: string | number;
|
|
81
|
+
/**
|
|
82
|
+
* Bottom margin, accepts values labeled with units. Defaults to `0`.
|
|
83
|
+
*/
|
|
84
|
+
bottom?: string | number;
|
|
85
|
+
/**
|
|
86
|
+
* Left margin, accepts values labeled with units. Defaults to `0`.
|
|
87
|
+
*/
|
|
88
|
+
left?: string | number;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Whether or not to embed the document outline into the PDF. Defaults to `false`.
|
|
92
|
+
*/
|
|
93
|
+
outline?: boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
|
|
96
|
+
*/
|
|
97
|
+
pageRanges?: string;
|
|
98
|
+
/**
|
|
99
|
+
* The file path to save the PDF to. If [`path`](https://playwright.dev/docs/api/class-page#page-pdf-option-path) is a
|
|
100
|
+
* relative path, then it is resolved relative to the current working directory. If no path is provided, the PDF won't
|
|
101
|
+
* be saved to the disk.
|
|
102
|
+
*/
|
|
103
|
+
path?: string;
|
|
104
|
+
/**
|
|
105
|
+
* Give any CSS `@page` size declared in the page priority over what is declared in
|
|
106
|
+
* [`width`](https://playwright.dev/docs/api/class-page#page-pdf-option-width) and
|
|
107
|
+
* [`height`](https://playwright.dev/docs/api/class-page#page-pdf-option-height) or
|
|
108
|
+
* [`format`](https://playwright.dev/docs/api/class-page#page-pdf-option-format) options. Defaults to `false`, which
|
|
109
|
+
* will scale the content to fit the paper size.
|
|
110
|
+
*/
|
|
111
|
+
preferCSSPageSize?: boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Print background graphics. Defaults to `false`.
|
|
114
|
+
*/
|
|
115
|
+
printBackground?: boolean;
|
|
116
|
+
/**
|
|
117
|
+
* Scale of the webpage rendering. Defaults to `1`. Scale amount must be between 0.1 and 2.
|
|
118
|
+
*/
|
|
119
|
+
scale?: number;
|
|
120
|
+
/**
|
|
121
|
+
* Whether or not to generate tagged (accessible) PDF. Defaults to `false`.
|
|
122
|
+
*/
|
|
123
|
+
tagged?: boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Paper width, accepts values labeled with units.
|
|
126
|
+
*/
|
|
127
|
+
width?: string | number;
|
|
128
|
+
}
|
|
129
|
+
export declare function generatePdf(html: string, pdfOptions?: PdfOptions): Promise<string>;
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { chromium } from "playwright-core";
|
|
2
|
+
/**
|
|
3
|
+
* Browserless (hosted) performance & reliability notes
|
|
4
|
+
*
|
|
5
|
+
* 1) Prefer connect-per-request (default)
|
|
6
|
+
* - We use chromium.connectOverCDP() instead of launching a local browser.
|
|
7
|
+
* - In serverless, connecting + closing per request is the safest and most cost-predictable option.
|
|
8
|
+
* - Hosted Browserless usage is typically metered by session/connection time, so leaving connections
|
|
9
|
+
* open while idle can increase cost.
|
|
10
|
+
*
|
|
11
|
+
* 2) Always isolate work in a fresh BrowserContext per PDF
|
|
12
|
+
* - Never reuse BrowserContext or Page across requests.
|
|
13
|
+
* - Context-per-request avoids cookie/storage leaks, cross-request interference, and memory growth.
|
|
14
|
+
*
|
|
15
|
+
* 3) Close deterministically (inner → outer)
|
|
16
|
+
* - Always close Page, then Context, then Browser (the CDP connection).
|
|
17
|
+
* - This releases remote resources quickly and prevents “dangling sessions” on the provider side.
|
|
18
|
+
*
|
|
19
|
+
* 4) Waiting strategy
|
|
20
|
+
* - Default waitUntil: "load" to avoid hanging on long-lived network activity (analytics/beacons).
|
|
21
|
+
* - If your HTML depends on external assets (remote CSS/fonts/images), consider "networkidle",
|
|
22
|
+
* or better: emit an explicit readiness signal and waitForFunction() for deterministic completion.
|
|
23
|
+
*
|
|
24
|
+
* 5) Biggest speed wins usually come from reducing network work
|
|
25
|
+
* - Inline critical CSS and avoid loading third-party scripts if possible.
|
|
26
|
+
* - Host/inline fonts and images where feasible to reduce variability and speed up rendering.
|
|
27
|
+
* - The less the page has to fetch, the faster and more consistent PDF generation becomes.
|
|
28
|
+
*
|
|
29
|
+
* 6) Optional burst optimization (only if needed)
|
|
30
|
+
* - If you generate many PDFs back-to-back on the same warm instance, you can reuse the CDP
|
|
31
|
+
* connection briefly (with a short idle timeout).
|
|
32
|
+
* - Still keep Context-per-request. Avoid keeping the connection open indefinitely to prevent idle cost.
|
|
33
|
+
*/
|
|
34
|
+
export async function getBrowser() {
|
|
35
|
+
const token = process.env.BROWSERLESS_TOKEN;
|
|
36
|
+
if (!token) {
|
|
37
|
+
throw new Error('BROWSER: `BROWSER_TOKEN` environment variable is not set');
|
|
38
|
+
}
|
|
39
|
+
const wsEndpoint = `wss://chrome.browserless.io?token=${encodeURIComponent(token)}`;
|
|
40
|
+
return await chromium.connectOverCDP(wsEndpoint);
|
|
41
|
+
}
|
|
42
|
+
export async function generatePdf(html, pdfOptions) {
|
|
43
|
+
let browser;
|
|
44
|
+
let context;
|
|
45
|
+
let page;
|
|
46
|
+
try {
|
|
47
|
+
browser = await getBrowser();
|
|
48
|
+
context = await browser.newContext();
|
|
49
|
+
page = await context.newPage();
|
|
50
|
+
await page.setContent(html, {
|
|
51
|
+
waitUntil: "load",
|
|
52
|
+
});
|
|
53
|
+
const pdfBytes = await page.pdf(pdfOptions ??
|
|
54
|
+
{
|
|
55
|
+
format: "Letter",
|
|
56
|
+
printBackground: true,
|
|
57
|
+
preferCSSPageSize: true,
|
|
58
|
+
});
|
|
59
|
+
return pdfBytes.toString("base64");
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
console.log(`BROWSER: PDF generation error: ${err.message}`);
|
|
63
|
+
return '';
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
await page?.close().catch(() => { });
|
|
67
|
+
await context?.close().catch(() => { });
|
|
68
|
+
await browser?.close().catch(() => { });
|
|
69
|
+
}
|
|
70
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cellaware/utils",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.4.1",
|
|
4
4
|
"description": "Cellaware Utilities for Node.js",
|
|
5
5
|
"author": "Cellaware Technologies",
|
|
6
6
|
"type": "module",
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"@azure/functions": "^4.5.1",
|
|
22
22
|
"@azure/storage-blob": "^12.16.0",
|
|
23
23
|
"dotenv": "^16.3.1",
|
|
24
|
-
"langchain": "^0.2.19"
|
|
24
|
+
"langchain": "^0.2.19",
|
|
25
|
+
"playwright-core": "^1.57.0"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"typescript": "^5.3.2"
|