Skip to content
This repository was archived by the owner on Oct 4, 2025. It is now read-only.

Commit 91acca1

Browse files
committed
cleanup: simplify ipfs upload function
signer: add option to cache sig for hash, to get consistent wacz results when downloading / saving to ipfs bump to 0.1.5
1 parent c28d275 commit 91acca1

File tree

5 files changed

+50
-53
lines changed

5 files changed

+50
-53
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.4",
10+
"version": "0.1.5",
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.13.9",
21+
"@webrecorder/wabac": "^2.13.10",
2222
"client-zip": "^2.3.0",
2323
"hash-wasm": "^4.9.0",
2424
"idb": "^7.1.1",

src/api.js

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class ExtAPI extends API
2828
};
2929
}
3030

31-
get downloaderOpts() {
31+
downloaderOpts() {
3232
const softwareString = this.softwareString;
3333

34-
const signer = new Signer(softwareString);
34+
const signer = new Signer(softwareString, {cacheSig: true});
3535

3636
return {softwareString, signer};
3737
}
@@ -50,7 +50,7 @@ class ExtAPI extends API
5050
const format = params._query.get("format") || "wacz";
5151
let filename = params._query.get("filename");
5252

53-
const dl = new Downloader({...this.downloaderOpts, coll, format, filename, pageList});
53+
const dl = new Downloader({...this.downloaderOpts(), coll, format, filename, pageList});
5454
return dl.download();
5555
}
5656

@@ -96,9 +96,11 @@ class ExtAPI extends API
9696

9797
const client = await self.clients.get(event.clientId);
9898

99-
const opts = {...this.downloaderOpts, customSplits: body.customSplits};
99+
const p = runIPFSAdd(collId, coll, client, this.downloaderOpts(), this.collections, body);
100100

101-
new IPFSAdd(collId, coll, client, opts, this.collections).run(body);
101+
if (event.waitUntil) {
102+
event.waitUntil(p);
103+
}
102104

103105
return {collId};
104106
}
@@ -150,7 +152,7 @@ class ExtAPI extends API
150152
}
151153

152154
async getPublicKey() {
153-
const signer = new Signer();
155+
const { signer } = this.downloaderOpts();
154156
const keys = await signer.loadKeys();
155157
if (!keys || !keys.public) {
156158
return {};
@@ -161,44 +163,27 @@ class ExtAPI extends API
161163
}
162164

163165
// ===========================================================================
164-
class IPFSAdd
165-
{
166-
constructor(collId, coll, client, opts, collections) {
167-
this.collId = collId;
168-
this.coll = coll;
169-
this.client = client;
170-
this.opts = opts;
171-
this.collections = collections;
172-
173-
this.size = 0;
174-
}
166+
async function runIPFSAdd(collId, coll, client, opts, collections, replayOpts) {
167+
let size = 0;
175168

176-
async run(replayOpts) {
177-
const {url, cid} = await ipfsAdd(this.coll, this.opts, replayOpts, (size) => this.progress(size));
178-
const result = {cid, ipfsURL: url};
179-
180-
if (this.client) {
181-
this.client.postMessage({
182-
type: "ipfsAdd",
183-
collId: this.collId,
184-
size: this.size,
185-
result
169+
const sendMessage = (type, result = null) => {
170+
if (client) {
171+
client.postMessage({
172+
type, collId, size, result
186173
});
187174
}
175+
};
188176

189-
await this.collections.updateMetadata(this.coll.name, this.coll.config.metadata);
190-
}
177+
const {url, cid} = await ipfsAdd(coll, opts, replayOpts, (incSize) => {
178+
size += incSize;
179+
sendMessage("ipfsProgress");
180+
});
191181

192-
progress(incSize) {
193-
this.size += incSize;
194-
if (this.client) {
195-
this.client.postMessage({
196-
type: "ipfsProgress",
197-
collId: this.collId,
198-
size: this.size,
199-
});
200-
}
201-
}
182+
const result = {cid, ipfsURL: url};
183+
184+
sendMessage("ipfsAdd", result);
185+
186+
await collections.updateMetadata(coll.name, coll.config.metadata);
202187
}
203188

204189
export { ExtAPI };

src/ipfsutils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export async function ipfsAdd(coll, downloaderOpts = {}, replayOpts = {}, progre
1818

1919
const filename = "webarchive.wacz";
2020

21-
if (downloaderOpts.customSplits) {
21+
if (replayOpts.customSplits) {
2222
const ZIP = new Uint8Array([]);
2323
const WARC_PAYLOAD = new Uint8Array([]);
2424
const WARC_GROUP = new Uint8Array([]);
@@ -397,7 +397,7 @@ function getReplayHtml(waczPath, replayOpts = {}) {
397397
</style>
398398
</head>
399399
<body>${showEmbed ? `
400-
<replay-web-page ${deepLink ? "deepLink=\"true\" " : ""}url="${pageUrl}" loading="${loading || ''}" embed="replay-with-info" src="${waczPath}"></replay-web-page>` : `
400+
<replay-web-page ${deepLink ? "deepLink=\"true\" " : ""}url="${pageUrl}" loading="${loading || ""}" embed="replay-with-info" src="${waczPath}"></replay-web-page>` : `
401401
<replay-app-main source="${waczPath}"></replay-app-main>`
402402
}
403403
</body>

src/keystore.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ export class KeyStore
5959
}
6060
}
6161

62-
6362
// ====================================================================
6463
export class Signer
6564
{
66-
constructor(softwareString) {
65+
constructor(softwareString, opts = {}) {
6766
this._store = new KeyStore();
6867
this.softwareString = softwareString || "ArchiveWeb.page";
68+
this.cacheSig = opts.cacheSig;
6969
}
7070

7171
close() {
@@ -112,14 +112,17 @@ export class Signer
112112
keyPair.publicKey = await crypto.subtle.importKey("spki", publicKey, ecdsaImportParams, true, ["verify"]);
113113
}
114114

115-
const data = new TextEncoder().encode(string);
115+
let signature = this.cacheSig ? await this.loadSig(string) : null;
116116

117-
let signature = await crypto.subtle.sign(ecdsaSignParams, keyPair.privateKey, data);
117+
if (!signature) {
118+
const data = new TextEncoder().encode(string);
119+
signature = await crypto.subtle.sign(ecdsaSignParams, keyPair.privateKey, data);
120+
signature = encodeBase64(new Uint8Array(signature));
121+
await this.saveSig(string, signature);
122+
}
118123

119124
//console.log("verify", await crypto.subtle.verify(ecdsaSignParams, keyPair.publicKey, signature, data));
120125

121-
signature = encodeBase64(new Uint8Array(signature));
122-
123126
return {
124127
hash: string,
125128
signature,
@@ -129,6 +132,15 @@ export class Signer
129132
};
130133
}
131134

135+
async saveSig(id, sig) {
136+
return await this._store.put({id, sig});
137+
}
138+
139+
async loadSig(id) {
140+
const res = await this._store.get(id);
141+
return res && res.sig;
142+
}
143+
132144
async saveKeys(keys, id = "_userkey") {
133145
return await this._store.put({id, keys});
134146
}

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.13.9":
499-
version "2.13.9"
500-
resolved "https://registry.yarnpkg.com/@webrecorder/wabac/-/wabac-2.13.9.tgz#7c01777231386c714a984fadfd55340848bfbfca"
501-
integrity sha512-ozaO4fs4XwgbueQkwS+8ghHjhpp/iP3R7dCZJ09BWgowI5hMwjJY0KAKjWbFVDH1RQ4cPkh8KPQWAdI+x2wR2g==
498+
"@webrecorder/wabac@^2.13.10":
499+
version "2.13.10"
500+
resolved "https://registry.yarnpkg.com/@webrecorder/wabac/-/wabac-2.13.10.tgz#7319cf18ff9198d6d4760aed6d2ad271e2472bfc"
501+
integrity sha512-tce4CmMZqPqZH1ZqLbKGb6+2QcdzwZbdMr94PtLcMvkCarXLs7oE2jnHQ3kDDXByDqp4JCS554WviwToACNOaQ==
502502
dependencies:
503503
"@peculiar/asn1-ecc" "^2.3.0"
504504
"@peculiar/asn1-schema" "^2.3.0"

0 commit comments

Comments
 (0)