Skip to content

Commit 9cf9b56

Browse files
committed
optimizations:
- don't update page, size with every resource update, instead update periodically (every 10 secs) and when stopping - extension: close db instance when recording stopped - fixes slow replay after recording, ensuring less indexeddb connections and transactions total fixes: - update to warcio 1.4.4/wabac 2.7.0-beta.3: fix for json-to-query to include dupe params (fixes yahoo answers capture) - bump max url length to 4k to avoid truncating long URLs (especially from social media) - add autofocus to popup main button bump to 0.6.3
1 parent 14a5d3c commit 9cf9b56

File tree

13 files changed

+145
-94
lines changed

13 files changed

+145
-94
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "ArchiveWeb.page",
3-
"version": "0.6.2",
3+
"version": "0.6.3",
44
"main": "index.js",
55
"description": "Create Web Archives directly in your browser",
66
"repository": "https://github.com/webrecorder/archiveweb.page",
77
"author": "Webrecorder Software",
88
"license": "AGPL-3.0-or-later",
99
"dependencies": {
1010
"@fortawesome/fontawesome-free": "^5.13.0",
11-
"@webrecorder/wabac": "2.7.0-beta.2",
11+
"@webrecorder/wabac": "2.7.0-beta.3",
1212
"browsertrix-behaviors": "^0.2.0",
1313
"bulma": "^0.9.2",
1414
"flexsearch": "^0.6.32",
@@ -21,8 +21,8 @@
2121
"lodash": "^4.17.20",
2222
"node-fetch": "^2.6.1",
2323
"pretty-bytes": "^5.3.0",
24-
"replaywebpage": "^1.4.0-beta.0",
25-
"warcio": "^1.4.3"
24+
"replaywebpage": "1.4.0-beta.0",
25+
"warcio": "^1.4.4"
2626
},
2727
"devDependencies": {
2828
"copy-webpack-plugin": "^6.4.0",

src/electron/electron-rec-preload.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,26 @@ contextBridge.exposeInMainWorld('archivewebpage', {
2626

2727

2828
// ===========================================================================
29-
ipcRenderer.on('add-resource', async (event, data, pageInfo, collId) => {
29+
ipcRenderer.on('add-resource', async (event, data, collId) => {
3030
const db = await getDB(collId);
3131

32-
let writtenSize = 0;
3332
const payloadSize = data.payload.length;
33+
let writtenSize = 0;
3434

3535
try {
3636
if (await db.addResource(data)) {
37-
writtenSize = payloadSize;
37+
writtenSize += payloadSize;
3838
}
3939
} catch (e) {
4040
console.warn(`Commit error for ${data.url} @ ${data.ts} ${data.mime}`);
4141
console.warn(e);
4242
return;
4343
}
4444

45-
loader.updateSize(collId, payloadSize, writtenSize);
45+
//loader.updateSize(collId, payloadSize, writtenSize);
4646

4747
// increment page size
48-
db.addPage(pageInfo);
48+
//db.addPage(pageInfo);
4949

5050
if (writtenSize) {
5151
ipcRenderer.send("inc-size", writtenSize);
@@ -62,6 +62,14 @@ ipcRenderer.on('add-page', async (event, pageInfo, collId) => {
6262
});
6363

6464

65+
// ===========================================================================
66+
ipcRenderer.on('inc-sizes', async (event, totalSize, writtenSize, collId) => {
67+
if (totalSize > 0) {
68+
loader.updateSize(collId, totalSize, writtenSize);
69+
}
70+
});
71+
72+
6573
// ===========================================================================
6674
async function handleIpfsPin(collId) {
6775
const reqId = "pin-" + collId + (100 * Math.random());

src/electron/electron-recorder.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ class ElectronRecorder extends Recorder
3737
this.didNavigateInitPage(url);
3838
});
3939

40-
this.appWC.on("ipc-message", (event, channel, ...args) => {
40+
this.appWC.on("ipc-message", (event, channel, size) => {
4141
if (channel === "inc-size") {
42-
this.sizeNew += args[0];
42+
this.sizeNew += size;
43+
this._cacheSessionNew += size;
4344
}
4445
});
4546

@@ -217,15 +218,19 @@ class ElectronRecorder extends Recorder
217218

218219
//console.log("res", data.url);
219220

220-
// size incremented asynchronously
221-
this.appWC.send("add-resource", data, this.pageInfo, this.collId);
221+
// size updated asynchronously via 'inc-size' event
222+
this.appWC.send("add-resource", data, this.collId);
222223
return 0;
223224
}
224225

225226
_doAddPage(pageInfo) {
226227
this.appWC.send("add-page", this.pageInfo, this.collId);
227228
}
228229

230+
_doIncSizes(totalSize, writtenSize) {
231+
this.appWC.send("inc-sizes", totalSize, writtenSize, this.collId);
232+
}
233+
229234
_doSendCommand(method, params, promise) {
230235
if (DEBUG) {
231236
console.log(" => ", method, params);

src/ext/browser-recorder.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class BrowserRecorder extends Recorder {
6464
}
6565

6666
setCollId(collId) {
67-
if (collId !== this.collId) {
67+
if (collId !== this.collId || !this.db) {
6868
this.collId = collId;
6969
this.db = null;
7070
this._initDB = this.collLoader.loadColl(this.collId);
@@ -88,6 +88,12 @@ class BrowserRecorder extends Recorder {
8888
chrome.debugger.onDetach.removeListener(this._onDetached);
8989
chrome.debugger.onEvent.removeListener(this._onEvent);
9090

91+
if (this.db) {
92+
this.db.close();
93+
this.db = null;
94+
this._initDB = null;
95+
}
96+
9197
if (!this.tabId) {
9298
return;
9399
}
@@ -246,10 +252,10 @@ class BrowserRecorder extends Recorder {
246252
// increment size counter only if committed
247253
//incrArchiveSize('dedup', writtenSize);
248254
//incrArchiveSize('total', payloadSize);
249-
this.collLoader.updateSize(this.collId, payloadSize, writtenSize);
255+
// this.collLoader.updateSize(this.collId, payloadSize, writtenSize);
250256

251257
// increment page size
252-
await this._doAddPage(this.pageInfo);
258+
// await this._doAddPage(this.pageInfo);
253259

254260
return writtenSize;
255261
}
@@ -259,7 +265,13 @@ class BrowserRecorder extends Recorder {
259265
console.warn("Empty Page, Skipping");
260266
return;
261267
}
262-
return this.db.addPage(pageInfo);
268+
if (this.db) {
269+
return this.db.addPage(pageInfo);
270+
}
271+
}
272+
273+
_doIncSizes(totalSize, writtenSize) {
274+
this.collLoader.updateSize(this.collId, totalSize, writtenSize);
263275
}
264276

265277
_doSendCommand(method, params, promise) {

src/popup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ class RecPopup extends LitElement
488488
<div class="view-row">
489489
${this.canRecord ? html`
490490
${this.renderCollDropdown()}
491-
<button
491+
<button autofocus
492492
?disabled=${this.actionButtonDisabled}
493493
@click="${!this.recording ? this.onStart : this.onStop}" class="button">
494494
<span class="icon">

src/recorder.js

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ class Recorder {
6565
this.id = 1;
6666
this.sessionSet = new Set();
6767

68+
this._cachePageInfo = null;
69+
this._cacheSessionNew = 0;
70+
this._cacheSessionTotal = 0;
71+
6872
this.behaviorInitStr = JSON.stringify({
6973
autofetch: true,
7074
autoplay: true,
@@ -147,18 +151,24 @@ class Recorder {
147151
await this._stop(domNodes);
148152
}
149153

150-
_stop(domNodes = null) {
151-
clearInterval(this._updateId);
152-
clearInterval(this._cleanupId);
154+
async _stop(domNodes = null) {
155+
clearInterval(this._updateStatusId);
156+
clearInterval(this._loopId);
153157

154158
this.flushPending();
155159
this.running = false;
156160
this.pendingRequests = {};
157161
this.numPending = 0;
158162

159-
this._doStop();
163+
await this.commitPage(this.pageInfo, domNodes, true);
164+
165+
if (this._cleaningUp) {
166+
await this._cleanupStaleWait;
167+
} else {
168+
await this.doUpdateLoop();
169+
}
160170

161-
return this.commitPage(this.pageInfo, domNodes, true);
171+
this._doStop();
162172
}
163173

164174
async attach() {
@@ -172,24 +182,54 @@ class Recorder {
172182
this.running = true;
173183
this.stopping = false;
174184

175-
this._updateId = setInterval(() => this.updateStatus(), 1000);
185+
this._cachePageInfo = null;
186+
this._cacheSessionNew = 0;
187+
this._cacheSessionTotal = 0;
188+
this._cleaningUp = false;
189+
this._cleanupStaleWait = null;
176190

177-
this._cleanupId = setInterval(() => this.cleanupStale(), 10000);
191+
this._updateStatusId = setInterval(() => this.updateStatus(), 1000);
192+
193+
this._loopId = setInterval(() => this.updateLoop(), 10000);
178194
};
179195

180-
cleanupStale() {
181-
for (const key of Object.keys(this.pendingRequests)) {
182-
const reqresp = this.pendingRequests[key];
196+
updateLoop() {
197+
if (!this._cleaningUp) {
198+
this._cleanupStaleWait = this.doUpdateLoop();
199+
}
200+
}
183201

184-
if ((new Date() - reqresp._created) > 20000) {
185-
if (this.noResponseForStatus(reqresp.status)) {
186-
console.log("Dropping stale: " + key);
187-
} else {
188-
console.log(`Committing stale ${reqresp.status} ${reqresp.url}`);
189-
this.fullCommit(reqresp, []);
202+
async doUpdateLoop() {
203+
this._cleaningUp = true;
204+
205+
try {
206+
for (const key of Object.keys(this.pendingRequests)) {
207+
const reqresp = this.pendingRequests[key];
208+
209+
if ((new Date() - reqresp._created) > 20000) {
210+
if (this.noResponseForStatus(reqresp.status)) {
211+
console.log("Dropping stale: " + key);
212+
} else {
213+
console.log(`Committing stale ${reqresp.status} ${reqresp.url}`);
214+
await this.fullCommit(reqresp, []);
215+
}
216+
delete this.pendingRequests[key];
190217
}
191-
delete this.pendingRequests[key];
192218
}
219+
220+
if (this._cachePageInfo) {
221+
await this._doAddPage(this._cachePageInfo);
222+
this._cachePageInfo = null;
223+
}
224+
225+
if (this._cacheSessionTotal > 0) {
226+
await this._doIncSizes(this._cacheSessionTotal, this._cacheSessionNew);
227+
this._cacheSessionTotal = 0;
228+
this._cacheSessionNew = 0;
229+
}
230+
231+
} finally {
232+
this._cleaningUp = false;
193233
}
194234
}
195235

@@ -605,18 +645,28 @@ class Recorder {
605645

606646
currPage.finished = finished;
607647

608-
return this._doAddPage(currPage);
648+
const res = this._doAddPage(currPage);
649+
if (currPage === this._cachePageInfo) {
650+
this._cachePageInfo = null;
651+
}
652+
return res;
609653
}
610654

611-
commitResource(data, pageInfo) {
655+
async commitResource(data, pageInfo) {
612656
const payloadSize = data.payload.length;
613657
pageInfo = pageInfo || this.pageInfo;
614658
pageInfo.size += payloadSize;
615659

616660
this.sizeTotal += payloadSize;
617661
this.numUrls++;
618662

619-
this._doAddResource(data).then((writtenSize) => this.sizeNew += writtenSize);
663+
const writtenSize = await this._doAddResource(data);
664+
665+
this.sizeNew += writtenSize;
666+
667+
this._cachePageInfo = pageInfo;
668+
this._cacheSessionTotal += payloadSize;
669+
this._cacheSessionNew += writtenSize;
620670
}
621671

622672
receiveMessageFromTarget(params, sessions) {
@@ -965,7 +1015,7 @@ class Recorder {
9651015
}
9661016

9671017
async fullCommit(reqresp, sessions) {
968-
const requestId = reqresp.requestId;
1018+
//const requestId = reqresp.requestId;
9691019

9701020
// let doneResolve;
9711021

src/requestresponseinfo.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { getStatusText } from '@webrecorder/wabac/src/utils';
44

55
import { postToGetUrl } from 'warcio';
66

7+
// max URL length for post/put payload-converted URLs
8+
const MAX_URL_LENGTH = 4096;
9+
710

811
// ===========================================================================
912
class RequestResponseInfo
@@ -158,8 +161,8 @@ class RequestResponseInfo
158161
}
159162
if (postToGetUrl(convData)) {
160163
this.requestBody = convData.requestBody;
161-
// truncate to first 2048 to avoid extra long URLs
162-
this.url = convData.url.slice(0, 2048);
164+
// truncate to avoid extra long URLs
165+
this.url = convData.url.slice(0, MAX_URL_LENGTH);
163166
}
164167
}
165168

wr-ext/bg.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wr-ext/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Webrecorder ArchiveWeb.page",
33
"description": "Create high-fidelity web archives directly in your browser",
4-
"version": "0.6.2",
4+
"version": "0.6.3",
55
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
66
"permissions": [
77
"debugger",

wr-ext/popup.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)