@cellaware/utils 8.5.0 → 8.5.2

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.
@@ -92,4 +92,4 @@ export interface PdfOptions {
92
92
  */
93
93
  width?: string | number;
94
94
  }
95
- export declare function generatePdf(html: string, pdfOptions: PdfOptions): Promise<Buffer>;
95
+ export declare function generatePdf(html: string, options?: PdfOptions): Promise<Buffer>;
@@ -66,7 +66,7 @@ function backoffMs(attempt) {
66
66
  function shouldRetryStatus(status) {
67
67
  return status === 429 || status === 503 || status === 502 || status === 504;
68
68
  }
69
- export async function generatePdf(html, pdfOptions) {
69
+ export async function generatePdf(html, options) {
70
70
  const token = process.env.BROWSERLESS_TOKEN;
71
71
  if (!token) {
72
72
  throw new Error('PDF: `BROWSER_TOKEN` environment variable is not set');
@@ -76,7 +76,10 @@ export async function generatePdf(html, pdfOptions) {
76
76
  const totalAttempts = 1 + RETRIES;
77
77
  for (let attempt = 1; attempt <= totalAttempts; attempt++) {
78
78
  const controller = new AbortController();
79
- const timeout = setTimeout(() => controller.abort(), TIMEOUT_MS);
79
+ const timeout = setTimeout(() => {
80
+ console.log(`PDF: attempt ${attempt} timed out after ${TIMEOUT_MS}ms`);
81
+ controller.abort();
82
+ }, TIMEOUT_MS);
80
83
  try {
81
84
  const res = await fetch(url.toString(), {
82
85
  method: "POST",
@@ -86,7 +89,7 @@ export async function generatePdf(html, pdfOptions) {
86
89
  },
87
90
  body: JSON.stringify({
88
91
  html,
89
- options: pdfOptions ??
92
+ options: options ??
90
93
  {
91
94
  format: "Letter",
92
95
  printBackground: true,
@@ -99,10 +102,14 @@ export async function generatePdf(html, pdfOptions) {
99
102
  const ab = await res.arrayBuffer();
100
103
  return Buffer.from(ab);
101
104
  }
105
+ console.log(`PDF: attempt ${attempt} failed with status ${res.status} ${res.statusText}`);
102
106
  const retryable = shouldRetryStatus(res.status);
103
107
  if (retryable && attempt < totalAttempts) {
104
108
  const retryAfter = parseRetryAfterMs(res.headers.get("retry-after"));
105
- const delay = retryAfter !== null ? retryAfter + Math.floor(Math.random() * (JITTER_MS + 1)) : backoffMs(attempt);
109
+ const delay = retryAfter !== null
110
+ ? retryAfter + Math.floor(Math.random() * (JITTER_MS + 1))
111
+ : backoffMs(attempt);
112
+ console.log(`PDF: retryable failure (status ${res.status}), waiting ${delay}ms before retry`);
106
113
  await sleep(delay);
107
114
  continue;
108
115
  }
@@ -118,8 +125,16 @@ export async function generatePdf(html, pdfOptions) {
118
125
  err?.code === "EAI_AGAIN" ||
119
126
  err?.cause?.code === "ECONNRESET" ||
120
127
  err?.cause?.code === "ETIMEDOUT";
128
+ if (isAbort) {
129
+ console.log(`PDF: attempt ${attempt} aborted`);
130
+ }
131
+ else {
132
+ console.log(`PDF: attempt ${attempt} threw error`, err);
133
+ }
121
134
  if (isTransientNetwork && attempt < totalAttempts) {
122
- await sleep(backoffMs(attempt));
135
+ const delay = backoffMs(attempt);
136
+ console.log(`PDF: transient error, waiting ${delay}ms before retry`);
137
+ await sleep(delay);
123
138
  continue;
124
139
  }
125
140
  throw err;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cellaware/utils",
3
- "version": "8.5.0",
3
+ "version": "8.5.2",
4
4
  "description": "Cellaware Utilities for Node.js",
5
5
  "author": "Cellaware Technologies",
6
6
  "type": "module",