single-file-cli 2.0.35 → 2.0.36

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/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@single-file/single-file-cli",
3
- "version": "2.0.35",
3
+ "version": "2.0.36",
4
4
  "description": "SingleFile CLI",
5
5
  "nodeModulesDir": true,
6
6
  "exports": {
package/lib/cdp-client.js CHANGED
@@ -34,6 +34,7 @@ const SINGLE_FILE_WORLD_NAME = "singlefile";
34
34
  const EMPTY_PAGE_URL = "about:blank";
35
35
  const CAPTURE_SCREENSHOT_FUNCTION_NAME = "captureScreenshot";
36
36
  const SET_SCREENSHOT_FUNCTION_NAME = "setScreenshot";
37
+ const SET_PAGE_DATA_FUNCTION_NAME = "setPageData";
37
38
 
38
39
  export { initialize, getPageData, closeBrowser };
39
40
 
@@ -105,8 +106,26 @@ async function getPageData(options) {
105
106
  options.browserDebug ? waitForDebuggerReady({ Debugger }) : Promise.resolve()
106
107
  ]);
107
108
  if (options.browserWaitDelay) {
108
- setTimeout(() => resolve, options.browserWaitDelay);
109
+ await new Promise(resolve => setTimeout(resolve, options.browserWaitDelay));
109
110
  }
111
+ await Runtime.addBinding({ name: SET_PAGE_DATA_FUNCTION_NAME, executionContextId: contextId });
112
+ const pageDataPromise = new Promise(resolve => {
113
+ let pageDataResponse = "";
114
+ Runtime.addEventListener("bindingCalled", async ({ params }) => {
115
+ if (params.name === SET_PAGE_DATA_FUNCTION_NAME) {
116
+ const { payload } = params;
117
+ if (payload.length) {
118
+ pageDataResponse += payload;
119
+ } else {
120
+ const result = JSON.parse(pageDataResponse);
121
+ if (result.content instanceof Array) {
122
+ result.content = new Uint8Array(result.content);
123
+ }
124
+ resolve(result);
125
+ }
126
+ }
127
+ });
128
+ });
110
129
  if (options.embedScreenshot && options.compressContent) {
111
130
  await Runtime.addBinding({ name: CAPTURE_SCREENSHOT_FUNCTION_NAME, executionContextId: contextId });
112
131
  Runtime.addEventListener("bindingCalled", ({ params }) => {
@@ -116,19 +135,16 @@ async function getPageData(options) {
116
135
  });
117
136
  }
118
137
  const { result } = await Runtime.evaluate({
119
- expression: `(${getPageDataScriptSource.toString()})(${JSON.stringify(options)},${JSON.stringify(SET_SCREENSHOT_FUNCTION_NAME)})`,
138
+ expression: `(${getPageDataScriptSource.toString()})(${JSON.stringify(options)},${JSON.stringify([SET_SCREENSHOT_FUNCTION_NAME, SET_PAGE_DATA_FUNCTION_NAME])})`,
120
139
  awaitPromise: true,
121
140
  returnByValue: true,
122
141
  contextId
123
142
  });
124
- const { value, subtype, description } = result;
143
+ const { subtype, description } = result;
125
144
  if (subtype === "error") {
126
145
  throw new Error(description);
127
146
  }
128
- if (Array.isArray(value.content)) {
129
- value.content = new Uint8Array(value.content);
130
- }
131
- return value;
147
+ return pageDataPromise;
132
148
  } catch (error) {
133
149
  if (error.code === LOAD_TIMEOUT_ERROR && options.browserWaitUntilFallback && options.browserWaitUntil) {
134
150
  const browserWaitUntil = NETWORK_STATES[(NETWORK_STATES.indexOf(options.browserWaitUntil) + 1)];
@@ -155,7 +171,7 @@ async function onCaptureScreenshot({ Page, Runtime }, contextId) {
155
171
  });
156
172
  }
157
173
 
158
- function getPageDataScriptSource(options, SET_SCREENSHOT_FUNCTION_NAME) {
174
+ function getPageDataScriptSource(options, [SET_SCREENSHOT_FUNCTION_NAME, SET_PAGE_DATA_FUNCTION_NAME]) {
159
175
  if (options.embedScreenshot && options.compressContent) {
160
176
  let screenshot = new Promise(resolve => globalThis[SET_SCREENSHOT_FUNCTION_NAME] = resolve);
161
177
  let pendingCapture;
@@ -167,7 +183,19 @@ function getPageDataScriptSource(options, SET_SCREENSHOT_FUNCTION_NAME) {
167
183
  }
168
184
  };
169
185
  }
170
- return singlefile.getPageData(options);
186
+ const MAX_CONTENT_SIZE = 32 * 1024 * 1024;
187
+ return singlefile.getPageData(options).then(data => {
188
+ if (data.content instanceof Uint8Array) {
189
+ data.content = Array.from(data.content);
190
+ }
191
+ data = JSON.stringify(data);
192
+ let indexData = 0;
193
+ do {
194
+ globalThis[SET_PAGE_DATA_FUNCTION_NAME](data.slice(indexData, indexData + MAX_CONTENT_SIZE));
195
+ indexData += MAX_CONTENT_SIZE;
196
+ } while (indexData < data.length);
197
+ globalThis[SET_PAGE_DATA_FUNCTION_NAME]("");
198
+ });
171
199
  }
172
200
 
173
201
  async function loadPage({ Page, Runtime }, options) {
package/lib/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "2.0.35";
1
+ export const version = "2.0.36";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-cli",
3
- "version": "2.0.35",
3
+ "version": "2.0.36",
4
4
  "description": "SingleFile CLI",
5
5
  "author": "Gildas Lormeau",
6
6
  "engines": {