Skip to content

Commit 20bb8d7

Browse files
committed
rec pending:
- track how many in-flight pending requests there are - add api to get num pending requests deps: bump to wabac.js 2.14.1 bump to 0.2.0
1 parent ed11bce commit 20bb8d7

File tree

4 files changed

+71
-26
lines changed

4 files changed

+71
-26
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "git",
88
"url": "git+https://github.com/webrecorder/awp-sw.git"
99
},
10-
"version": "0.1.9",
10+
"version": "0.2.0",
1111
"license": "AGPL-3.0-or-later",
1212
"exports": {
1313
".": "./src/index.js"
@@ -18,7 +18,7 @@
1818
"dependencies": {
1919
"@ipld/car": "^5.0.1",
2020
"@ipld/unixfs": "^2.0.0",
21-
"@webrecorder/wabac": "^2.14.0",
21+
"@webrecorder/wabac": "^2.14.1",
2222
"client-zip": "^2.3.0",
2323
"hash-wasm": "^4.9.0",
2424
"idb": "^7.1.1",

src/api.js

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { tsToDate } from "@webrecorder/wabac/src/utils.js";
44
import { Downloader } from "./downloader.js";
55
import { Signer } from "./keystore.js";
66
import { ipfsAdd, ipfsRemove, setAutoIPFSUrl } from "./ipfsutils.js";
7+
import { RecProxy } from "./recproxy.js";
78

89
// eslint-disable-next-line no-undef
910
const DEFAULT_SOFTWARE_STRING = `Webrecorder ArchiveWeb.page ${__AWP_VERSION__}, using warcio.js ${__WARCIO_VERSION__}`;
@@ -20,6 +21,7 @@ class ExtAPI extends API
2021
return {
2122
...super.routes,
2223
"downloadPages": "c/:coll/dl",
24+
"recPending": "c/:coll/recPending",
2325
"pageTitle": ["c/:coll/pageTitle", "POST"],
2426
"ipfsAdd": ["c/:coll/ipfs", "POST"],
2527
"ipfsRemove": ["c/:coll/ipfs", "DELETE"],
@@ -38,21 +40,11 @@ class ExtAPI extends API
3840

3941
async handleApi(request, params, event) {
4042
switch (params._route) {
41-
case "downloadPages": {
42-
const coll = await this.collections.loadColl(params.coll);
43-
if (!coll) {
44-
return {error: "collection_not_found"};
45-
}
46-
47-
const pageQ = params._query.get("pages");
48-
const pageList = pageQ === "all" ? null : pageQ.split(",");
49-
50-
const format = params._query.get("format") || "wacz";
51-
let filename = params._query.get("filename");
43+
case "downloadPages":
44+
return await this.handleDownload(params);
5245

53-
const dl = new Downloader({...this.downloaderOpts(), coll, format, filename, pageList});
54-
return dl.download();
55-
}
46+
case "recPending":
47+
return await this.recordingPending(params);
5648

5749
case "pageTitle":
5850
return await this.updatePageTitle(params.coll, request);
@@ -66,11 +58,42 @@ class ExtAPI extends API
6658
case "ipfsRemove":
6759
return await this.ipfsRemove(request, params.coll);
6860

69-
7061
default:
7162
return await super.handleApi(request, params);
7263
}
7364
}
65+
66+
async handleDownload(params) {
67+
const coll = await this.collections.loadColl(params.coll);
68+
if (!coll) {
69+
return {error: "collection_not_found"};
70+
}
71+
72+
const pageQ = params._query.get("pages");
73+
const pageList = pageQ === "all" ? null : pageQ.split(",");
74+
75+
const format = params._query.get("format") || "wacz";
76+
let filename = params._query.get("filename");
77+
78+
const dl = new Downloader({...this.downloaderOpts(), coll, format, filename, pageList});
79+
return dl.download();
80+
}
81+
82+
async recordingPending(params) {
83+
const coll = await this.collections.loadColl(params.coll);
84+
if (!coll) {
85+
return {error: "collection_not_found"};
86+
}
87+
88+
if (!(coll.store instanceof RecProxy)) {
89+
return {error: "invalid_collection"};
90+
}
91+
92+
const numPending = await coll.store.getCounter();
93+
94+
return { numPending };
95+
}
96+
7497
async prepareColl(collId, request) {
7598
const coll = await this.collections.loadColl(collId);
7699
if (!coll) {

src/recproxy.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { postToGetUrl } from "warcio";
66

77

88
// ===========================================================================
9-
class RecProxy extends ArchiveDB
9+
export class RecProxy extends ArchiveDB
1010
{
1111
constructor(config, collLoader) {
1212
super(config.dbname);
@@ -23,7 +23,22 @@ class RecProxy extends ArchiveDB
2323
this.isNew = true;
2424
this.firstPageOnly = config.extraConfig.firstPageOnly || false;
2525

26-
//this.cookie = "";
26+
this.counter = 0;
27+
}
28+
29+
_initDB(db, oldV, newV, tx) {
30+
super._initDB(db, oldV, newV, tx);
31+
db.createObjectStore("rec");
32+
}
33+
34+
async decCounter() {
35+
this.counter--;
36+
//console.log("rec counter", this.counter);
37+
await this.db.put("rec", this.counter, "numPending");
38+
}
39+
40+
async getCounter() {
41+
return await this.db.get("rec", "numPending");
2742
}
2843

2944
async getResource(request, prefix) {
@@ -35,21 +50,28 @@ class RecProxy extends ArchiveDB
3550
req = request.request;
3651
}
3752

38-
request.headers["User-Agent"] = "Mozilla/5.0 (Linux; Android 11; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.91 Mobile Safari/537.36";
53+
let response = null;
3954

40-
const response = await this.liveProxy.getResource(request, prefix);
55+
try {
56+
this.counter++;
57+
response = await this.liveProxy.getResource(request, prefix);
58+
} catch (e) {
59+
await this.decCounter();
60+
return null;
61+
}
4162

4263
//this.cookie = response.headers.get("x-wabac-preset-cookie");
4364

4465
// don't record content proxied from specified hosts
4566
if (!this.recordProxied && this.liveProxy.hostProxy) {
4667
const parsedUrl = new URL(request.url);
4768
if (this.liveProxy.hostProxy[parsedUrl.host]) {
69+
await this.decCounter();
4870
return response;
4971
}
5072
}
5173

52-
this.doRecord(response, req);
74+
this.doRecord(response, req).finally(() => this.decCounter());
5375

5476
return response;
5577
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,10 @@
495495
resolved "https://registry.yarnpkg.com/@webpack-cli/serve/-/serve-2.0.0.tgz#f08ea194e01ed45379383a8886e8c85a65a5f26a"
496496
integrity sha512-Rumq5mHvGXamnOh3O8yLk1sjx8dB30qF1OeR6VC00DIR6SLJ4bwwUGKC4pE7qBFoQyyh0H9sAg3fikYgAqVR0w==
497497

498-
"@webrecorder/wabac@^2.14.0":
499-
version "2.14.0"
500-
resolved "https://registry.yarnpkg.com/@webrecorder/wabac/-/wabac-2.14.0.tgz#b2fe5adcaefcdf3731a9e05cc11bbb2c63ae59c0"
501-
integrity sha512-KAO4n4sY1HtKeW98Gy88UlHlpy86x2D6b+KdKvhnHkwkwN/0IKOBUipYN7/45u7v5QRZyJf5CYI9mXZhrvUaUw==
498+
"@webrecorder/wabac@^2.14.1":
499+
version "2.14.1"
500+
resolved "https://registry.yarnpkg.com/@webrecorder/wabac/-/wabac-2.14.1.tgz#dd7c92e4c5892cc6d865a3cb568b8f16eb6d9761"
501+
integrity sha512-fADNPmUlqtwQA7jsomKjyTwMqjx6Le5YYLsyxHPVU1HzEIxzEh+7v0dazz3LYSjnPzlbjsG4jbB/jMlJtXncwg==
502502
dependencies:
503503
"@peculiar/asn1-ecc" "^2.3.4"
504504
"@peculiar/asn1-schema" "^2.3.3"

0 commit comments

Comments
 (0)