single-file-cli 2.0.60 → 2.0.62

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/build.sh CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env bash
2
2
 
3
- deno vendor "npm:single-file-core@1.5.27"
3
+ deno vendor "npm:single-file-core@1.5.30"
4
4
 
5
5
  echo "
6
6
  import { build } from 'npm:esbuild';
package/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@single-file/single-file-cli",
3
- "version": "2.0.60",
3
+ "version": "2.0.62",
4
4
  "description": "SingleFile CLI",
5
5
  "nodeModulesDir": true,
6
6
  "exports": {
package/lib/cdp-client.js CHANGED
@@ -33,7 +33,9 @@ const MINIMIZED_WINDOW_STATE = "minimized";
33
33
  const SINGLE_FILE_WORLD_NAME = "singlefile";
34
34
  const EMPTY_PAGE_URL = "about:blank";
35
35
  const CAPTURE_SCREENSHOT_FUNCTION_NAME = "captureScreenshot";
36
+ const CAPTURE_PDF_FUNCTION_NAME = "capturePdf";
36
37
  const SET_SCREENSHOT_FUNCTION_NAME = "setScreenshot";
38
+ const SET_PDF_FUNCTION_NAME = "setPdf";
37
39
  const SET_PAGE_DATA_FUNCTION_NAME = "setPageData";
38
40
  const REDIRECT_STATUS_CODES = [301, 302, 303, 307, 308];
39
41
 
@@ -203,14 +205,44 @@ async function getPageData(options) {
203
205
  });
204
206
  if (options.embedScreenshot && options.compressContent) {
205
207
  await Runtime.addBinding({ name: CAPTURE_SCREENSHOT_FUNCTION_NAME, executionContextId: contextId });
206
- Runtime.addEventListener("bindingCalled", ({ params }) => {
208
+ Runtime.addEventListener("bindingCalled", async ({ params }) => {
207
209
  if (params.name === CAPTURE_SCREENSHOT_FUNCTION_NAME) {
208
- onCaptureScreenshot({ Page, Runtime }, contextId);
210
+ try {
211
+ await onCaptureScreenshot({ Page, Runtime }, contextId);
212
+ } catch (error) {
213
+ await Runtime.evaluate({
214
+ expression: `globalThis.${SET_SCREENSHOT_FUNCTION_NAME}(${JSON.stringify([])})`,
215
+ contextId
216
+ });
217
+ }
218
+ }
219
+ });
220
+ }
221
+ if (options.embedPdf) {
222
+ await Runtime.addBinding({ name: CAPTURE_PDF_FUNCTION_NAME, executionContextId: contextId });
223
+ Runtime.addEventListener("bindingCalled", async ({ params }) => {
224
+ if (params.name === CAPTURE_PDF_FUNCTION_NAME) {
225
+ let pdfOptions;
226
+ if (options.embedPdfOptions) {
227
+ try {
228
+ pdfOptions = JSON.parse(options.embedPdfOptions);
229
+ } catch (error) {
230
+ // ignored
231
+ }
232
+ }
233
+ try {
234
+ await onCapturePdf({ Page, Runtime }, contextId, pdfOptions);
235
+ } catch (error) {
236
+ await Runtime.evaluate({
237
+ expression: `globalThis.${SET_PDF_FUNCTION_NAME}ƒƒ(${JSON.stringify([])})`,
238
+ contextId
239
+ });
240
+ }
209
241
  }
210
242
  });
211
243
  }
212
244
  const { result } = await Runtime.evaluate({
213
- expression: `(${getPageDataScriptSource.toString()})(${JSON.stringify(options)},${JSON.stringify([SET_SCREENSHOT_FUNCTION_NAME, SET_PAGE_DATA_FUNCTION_NAME])})`,
245
+ expression: `(${getPageDataScriptSource.toString()})(${JSON.stringify(options)},${JSON.stringify([SET_SCREENSHOT_FUNCTION_NAME, SET_PDF_FUNCTION_NAME, SET_PAGE_DATA_FUNCTION_NAME])})`,
214
246
  awaitPromise: true,
215
247
  returnByValue: true,
216
248
  contextId
@@ -248,15 +280,36 @@ async function onCaptureScreenshot({ Page, Runtime }, contextId) {
248
280
  });
249
281
  }
250
282
 
251
- function getPageDataScriptSource(options, [SET_SCREENSHOT_FUNCTION_NAME, SET_PAGE_DATA_FUNCTION_NAME]) {
252
- if (options.embedScreenshot && options.compressContent) {
253
- let screenshot = new Promise(resolve => globalThis[SET_SCREENSHOT_FUNCTION_NAME] = resolve);
283
+ async function onCapturePdf({ Page, Runtime }, contextId, options = {}) {
284
+ const { data } = await Page.printToPDF(options);
285
+ const pdfData = Array.from(new Uint8Array(await (await fetch("data:application/pdf;base64," + data)).arrayBuffer()));
286
+ await Runtime.evaluate({
287
+ expression: `globalThis.${SET_PDF_FUNCTION_NAME}(${JSON.stringify(pdfData)})`,
288
+ contextId
289
+ });
290
+ }
291
+
292
+ function getPageDataScriptSource(options, [SET_SCREENSHOT_FUNCTION_NAME, SET_PDF_FUNCTION_NAME, SET_PAGE_DATA_FUNCTION_NAME]) {
293
+ if ((options.embedScreenshot && options.compressContent) || options.embedPdf) {
294
+ let screenshot, pdf;
295
+ if (options.embedScreenshot && options.compressContent) {
296
+ screenshot = new Promise(resolve => globalThis[SET_SCREENSHOT_FUNCTION_NAME] = resolve);
297
+ }
298
+ if (options.embedPdf) {
299
+ pdf = new Promise(resolve => globalThis[SET_PDF_FUNCTION_NAME] = resolve);
300
+ }
254
301
  let pendingCapture;
255
302
  options.onprogress = async event => {
256
- if (event.type == event.RESOURCES_INITIALIZING && globalThis.captureScreenshot && window == top && !pendingCapture) {
303
+ if (event.type == event.RESOURCES_INITIALIZING && window == top && !pendingCapture) {
257
304
  pendingCapture = true;
258
- globalThis.captureScreenshot("");
259
- options.embeddedImage = await screenshot;
305
+ if (globalThis.captureScreenshot) {
306
+ globalThis.captureScreenshot("");
307
+ options.embeddedImage = await screenshot;
308
+ }
309
+ if (globalThis.capturePdf) {
310
+ globalThis.capturePdf("");
311
+ options.embeddedPdf = await pdf;
312
+ }
260
313
  }
261
314
  };
262
315
  }