@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.
- package/dist/chatwms/pdf.d.ts +1 -1
- package/dist/chatwms/pdf.js +20 -5
- package/package.json +1 -1
package/dist/chatwms/pdf.d.ts
CHANGED
package/dist/chatwms/pdf.js
CHANGED
|
@@ -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,
|
|
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(() =>
|
|
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:
|
|
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
|
|
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
|
-
|
|
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;
|