-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipfsutils.js
497 lines (402 loc) · 13 KB
/
ipfsutils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import { initAutoIPFS } from "@webrecorder/wabac/src/ipfs.js";
import { Downloader } from "./downloader.js";
import * as UnixFS from "@ipld/unixfs";
import { CarWriter } from "@ipld/car";
import Queue from "p-queue";
// eslint-disable-next-line no-undef
const autoipfsOpts = {web3StorageToken: __WEB3_STORAGE_TOKEN__};
export async function setAutoIPFSUrl(url) {
autoipfsOpts.daemonURL = url;
}
export async function ipfsAdd(coll, downloaderOpts = {}, replayOpts = {}, progress = null) {
const autoipfs = await initAutoIPFS(autoipfsOpts);
const filename = "webarchive.wacz";
if (replayOpts.customSplits) {
const ZIP = new Uint8Array([]);
const WARC_PAYLOAD = new Uint8Array([]);
const WARC_GROUP = new Uint8Array([]);
downloaderOpts.markers = {ZIP, WARC_PAYLOAD, WARC_GROUP};
}
const gzip = replayOpts.gzip !== undefined ? replayOpts.gzip : true;
const dl = new Downloader({...downloaderOpts, coll, filename, gzip});
const dlResponse = await dl.download(progress);
if (!coll.config.metadata.ipfsPins) {
coll.config.metadata.ipfsPins = [];
}
let concur;
let shardSize;
let capacity;
if (autoipfs.type === "web3.storage") {
// for now, web3storage only allows a single-shard uploads, so set this high.
concur = 1;
shardSize = 1024 * 1024 * 10000;
capacity = 1048576 * 200;
} else {
concur = 3;
shardSize = 1024 * 1024 * 10;
// use default capacity
capacity = undefined;
}
const { readable, writable } = new TransformStream(
{},
UnixFS.withCapacity(capacity)
);
const swContent = await fetchBuffer("sw.js", replayOpts.replayBaseUrl || self.location.href);
const uiContent = await fetchBuffer("ui.js", replayOpts.replayBaseUrl || self.location.href);
let favicon = null;
try {
favicon = await fetchBuffer("https://replayweb.page/build/icon.png");
} catch (e) {
console.warn("Couldn't load favicon");
}
let url, cid;
const p = readable
.pipeThrough(new ShardingStream(shardSize))
.pipeThrough(new ShardStoringStream(autoipfs, concur))
.pipeTo(
new WritableStream({
write: (res) => {
url = res.url;
cid = res.cid;
},
})
);
ipfsGenerateCar(
writable,
dlResponse.filename, dlResponse.body,
swContent, uiContent, replayOpts,
downloaderOpts.markers, favicon,
);
await p;
const res = {cid: cid.toString(), url};
coll.config.metadata.ipfsPins.push(res);
console.log("ipfs cid added " + url);
return res;
}
export async function ipfsRemove(coll) {
const autoipfs = await initAutoIPFS(autoipfsOpts);
if (coll.config.metadata.ipfsPins) {
for (const {url} of coll.config.metadata.ipfsPins) {
try {
await autoipfs.clear(url);
} catch (e) {
console.log("Removal from this IPFS backend not yet implemented");
}
}
coll.config.metadata.ipfsPins = null;
return true;
}
return false;
}
async function fetchBuffer(filename, replayBaseUrl) {
const resp = await fetch(new URL(filename, replayBaseUrl).href);
return new Uint8Array(await resp.arrayBuffer());
}
async function ipfsWriteBuff(writer, name, content, dir) {
const file = UnixFS.createFileWriter(writer);
if (content instanceof Uint8Array) {
file.write(content);
} else if (content[Symbol.asyncIterator]) {
for await (const chunk of content) {
file.write(chunk);
}
}
const link = await file.close();
dir.set(name, link);
}
// ===========================================================================
export async function ipfsGenerateCar(writable, waczPath,
waczContent, swContent, uiContent, replayOpts, markers, favicon) {
const writer = UnixFS.createWriter({ writable });
const rootDir = UnixFS.createDirectoryWriter(writer);
const encoder = new TextEncoder();
const htmlContent = getReplayHtml(waczPath, replayOpts);
await ipfsWriteBuff(writer, "ui.js", uiContent, rootDir);
if (replayOpts.showEmbed) {
const replayDir = UnixFS.createDirectoryWriter(writer);
await ipfsWriteBuff(writer, "sw.js", swContent, replayDir);
await rootDir.set("replay", await replayDir.close());
} else {
await ipfsWriteBuff(writer, "sw.js", swContent, rootDir);
}
if (favicon) {
await ipfsWriteBuff(writer, "favicon.ico", favicon, rootDir);
}
await ipfsWriteBuff(writer, "index.html", encoder.encode(htmlContent), rootDir);
if (!markers) {
await ipfsWriteBuff(writer, waczPath, iterate(waczContent), rootDir);
} else {
await splitByWarcRecordGroup(writer, waczPath, iterate(waczContent), rootDir, markers);
}
const {cid} = await rootDir.close();
writer.close();
return cid;
}
async function splitByWarcRecordGroup(writer, waczPath, warcIter, rootDir, markers) {
let links = [];
const fileLinks = [];
let secondaryLinks = [];
let inZipFile = false;
let lastChunk = null;
let currName = null;
const decoder = new TextDecoder();
const dirs = {};
const {ZIP, WARC_PAYLOAD, WARC_GROUP} = markers;
let file = UnixFS.createFileWriter(writer);
function getDirAndName(fullpath) {
const parts = fullpath.split("/");
const filename = parts.pop();
return [parts.join("/"), filename];
}
const waczDir = UnixFS.createDirectoryWriter(writer);
let count = 0;
for await (const chunk of warcIter) {
if (chunk === ZIP && !inZipFile) {
if (lastChunk) {
currName = decoder.decode(lastChunk);
console.log("name", currName);
}
inZipFile = true;
if (count) {
fileLinks.push(await file.close());
count = 0;
file = UnixFS.createFileWriter(writer);
}
} else if (chunk === ZIP && inZipFile) {
if (count) {
links.push(await file.close());
count = 0;
file = UnixFS.createFileWriter(writer);
}
let link;
if (secondaryLinks.length) {
if (links.length) {
throw new Error("invalid state, secondaryLinks + links?");
}
link = await concat(writer, secondaryLinks);
secondaryLinks = [];
} else {
link = await concat(writer, links);
links = [];
}
fileLinks.push(link);
const [dirName, filename] = getDirAndName(currName);
currName = null;
let dir;
if (!dirName) {
dir = waczDir;
} else {
if (!dirs[dirName]) {
dirs[dirName] = UnixFS.createDirectoryWriter(writer);
}
dir = dirs[dirName];
}
dir.set(filename, link);
inZipFile = false;
} else if (chunk === WARC_PAYLOAD || chunk === WARC_GROUP) {
if (!inZipFile) {
throw new Error("invalid state");
}
if (count) {
links.push(await file.close());
count = 0;
file = UnixFS.createFileWriter(writer);
if (chunk === WARC_GROUP) {
secondaryLinks.push(await concat(writer, links));
links = [];
}
}
} else if (chunk.length > 0) {
if (!inZipFile) {
lastChunk = chunk;
}
file.write(chunk);
count++;
}
}
fileLinks.push(await file.close());
for (const [name, dir] of Object.entries(dirs)) {
waczDir.set(name, await dir.close());
}
// for await (const chunk of iterate(waczContent)) {
// if (chunk === splitMarker) {
// links.push(await file.close());
// file = UnixFS.createFileWriter(writer);
// } else {
// file.write(chunk);
// }
// }
// const rootDir = UnixFS.createDirectoryWriter(writer);
// await ipfsWriteBuff(writer, "ui.js", uiContent, rootDir);
// await ipfsWriteBuff(writer, "sw.js", swContent, rootDir);
// await ipfsWriteBuff(writer, "index.html", encoder.encode(htmlContent), rootDir);
rootDir.set("webarchive", await waczDir.close());
rootDir.set(waczPath, await concat(writer, fileLinks));
}
async function concat(writer, links) {
//TODO: is this the right way to do this?
const {fileEncoder, hasher, linker} = writer.settings;
const advanced = fileEncoder.createAdvancedFile(links);
const bytes = fileEncoder.encode(advanced);
const hash = await hasher.digest(bytes);
const cid = linker.createLink(fileEncoder.code, hash);
const block = { bytes, cid };
writer.writer.write(block);
const link = {
cid,
contentByteLength: fileEncoder.cumulativeContentByteLength(links),
dagByteLength: fileEncoder.cumulativeDagByteLength(bytes, links),
};
return link;
}
export const iterate = async function* (stream) {
const reader = stream.getReader();
while (true) {
const next = await reader.read();
if (next.done) {
return;
} else {
yield next.value;
}
}
};
export async function encodeBlocks(blocks, root) {
// @ts-expect-error
const { writer, out } = CarWriter.create(root);
/** @type {Error?} */
let error;
void (async () => {
try {
for await (const block of blocks) {
// @ts-expect-error
await writer.put(block);
}
} catch (/** @type {any} */ err) {
error = err;
} finally {
await writer.close();
}
})();
const chunks = [];
for await (const chunk of out) chunks.push(chunk);
// @ts-expect-error
if (error != null) throw error;
const roots = root != null ? [root] : [];
console.log("chunks", chunks.length);
return Object.assign(new Blob(chunks), { version: 1, roots });
}
function getReplayHtml(waczPath, replayOpts = {}) {
const { showEmbed, pageUrl, pageTitle, deepLink, loading } = replayOpts;
return `
<!doctype html>
<html class="no-overflow">
<head>
<title>${pageTitle || "ReplayWeb.page"}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="./ui.js"></script>
<style>
html, body, replay-web-page, replay-app-main {
width: 100%;
height: 100%;
overflow: hidden;
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>${showEmbed ? `
<replay-web-page ${deepLink ? "deepLink=\"true\" " : ""}url="${pageUrl}" loading="${loading || ""}" embed="replay-with-info" src="${waczPath}"></replay-web-page>` : `
<replay-app-main source="${waczPath}"></replay-app-main>`
}
</body>
</html>`;
}
// Copied from https://github.com/web3-storage/w3protocol/blob/main/packages/upload-client/src/sharding.js
const SHARD_SIZE = 1024 * 1024 * 10;
const CONCURRENT_UPLOADS = 3;
/**
* Shard a set of blocks into a set of CAR files. The last block is assumed to
* be the DAG root and becomes the CAR root CID for the last CAR output.
*
* @extends {TransformStream<import('@ipld/unixfs').Block, import('./types').CARFile>}
*/
export class ShardingStream extends TransformStream {
/**
* @param {import('./types').ShardingOptions} [options]
*/
constructor(shardSize = SHARD_SIZE) {
/** @type {import('@ipld/unixfs').Block[]} */
let shard = [];
/** @type {import('@ipld/unixfs').Block[] | null} */
let readyShard = null;
let size = 0;
super({
async transform(block, controller) {
if (readyShard != null) {
controller.enqueue(await encodeBlocks(readyShard));
readyShard = null;
}
if (shard.length && size + block.bytes.length > shardSize) {
readyShard = shard;
shard = [];
size = 0;
}
shard.push(block);
size += block.bytes.length;
},
async flush(controller) {
if (readyShard != null) {
controller.enqueue(await encodeBlocks(readyShard));
}
const rootBlock = shard.at(-1);
if (rootBlock != null) {
controller.enqueue(await encodeBlocks(shard, rootBlock.cid));
}
},
});
}
}
/**
* Upload multiple DAG shards (encoded as CAR files) to the service.
*
* Note: an "upload" must be registered in order to link multiple shards
* together as a complete upload.
*
* The writeable side of this transform stream accepts CAR files and the
* readable side yields `CARMetadata`.
*
* @extends {TransformStream<import('./types').CARFile, import('./types').CARMetadata>}
*/
export class ShardStoringStream extends TransformStream {
constructor(autoipfs, concurrency = CONCURRENT_UPLOADS) {
const queue = new Queue({ concurrency });
const abortController = new AbortController();
super({
async transform(car, controller) {
void queue.add(
async () => {
try {
//const opts = { ...options, signal: abortController.signal };
//const cid = await add(conf, car, opts)
const resUrls = await autoipfs.uploadCAR(car);
controller.enqueue({cid: car.roots[0], url: resUrls[0]});
//const { version, roots, size } = car
//controller.enqueue({ version, roots, cid, size })
} catch (err) {
controller.error(err);
abortController.abort(err);
}
},
{ signal: abortController.signal }
);
// retain backpressure by not returning until no items queued to be run
await queue.onSizeLessThan(1);
},
async flush() {
// wait for queue empty AND pending items complete
await queue.onIdle();
},
});
}
}