1
- import { initAutoIPFS } from "@webrecorder/wabac/src/ipfs.js" ;
2
1
import { Downloader } from "./downloader.js" ;
3
2
3
+ import { create as createAutoIPFS } from "auto-js-ipfs" ;
4
+
4
5
import * as UnixFS from "@ipld/unixfs" ;
5
6
import { CarWriter } from "@ipld/car" ;
6
7
import Queue from "p-queue" ;
7
8
8
9
// eslint-disable-next-line no-undef
9
10
const autoipfsOpts = { web3StorageToken : __WEB3_STORAGE_TOKEN__ } ;
10
11
12
+ let autoipfs = null ;
13
+
11
14
export async function setAutoIPFSUrl ( url ) {
15
+ if ( autoipfsOpts . daemonURL !== url ) {
16
+ autoipfs = null ;
17
+ }
12
18
autoipfsOpts . daemonURL = url ;
13
19
}
14
20
15
21
export async function ipfsAdd ( coll , downloaderOpts = { } , replayOpts = { } , progress = null ) {
16
- const autoipfs = await initAutoIPFS ( autoipfsOpts ) ;
22
+ if ( ! autoipfs ) {
23
+ autoipfs = await createAutoIPFS ( autoipfsOpts ) ;
24
+ }
17
25
18
- const filename = "webarchive.wacz" ;
26
+ const filename = replayOpts . filename || "webarchive.wacz" ;
19
27
20
28
if ( replayOpts . customSplits ) {
21
29
const ZIP = new Uint8Array ( [ ] ) ;
@@ -27,7 +35,7 @@ export async function ipfsAdd(coll, downloaderOpts = {}, replayOpts = {}, progre
27
35
const gzip = replayOpts . gzip !== undefined ? replayOpts . gzip : true ;
28
36
29
37
const dl = new Downloader ( { ...downloaderOpts , coll, filename, gzip} ) ;
30
- const dlResponse = await dl . download ( progress ) ;
38
+ const dlResponse = await dl . download ( ) ;
31
39
32
40
if ( ! coll . config . metadata . ipfsPins ) {
33
41
coll . config . metadata . ipfsPins = [ ] ;
@@ -44,9 +52,10 @@ export async function ipfsAdd(coll, downloaderOpts = {}, replayOpts = {}, progre
44
52
capacity = 1048576 * 200 ;
45
53
} else {
46
54
concur = 3 ;
47
- shardSize = 1024 * 1024 * 10 ;
55
+ shardSize = 1024 * 1024 * 5 ;
48
56
// use default capacity
49
- capacity = undefined ;
57
+ // capacity = undefined;
58
+ capacity = 1048576 * 200 ;
50
59
}
51
60
52
61
const { readable, writable } = new TransformStream (
@@ -65,6 +74,17 @@ export async function ipfsAdd(coll, downloaderOpts = {}, replayOpts = {}, progre
65
74
console . warn ( "Couldn't load favicon" ) ;
66
75
}
67
76
77
+ const htmlContent = getReplayHtml ( dlResponse . filename , replayOpts ) ;
78
+
79
+ let totalSize = 0 ;
80
+
81
+ if ( coll . config && coll . config . metadata && coll . config . metadata . size ) {
82
+ totalSize = coll . config . metadata . size +
83
+ swContent . length + uiContent . length + favicon . length + htmlContent . length ;
84
+ }
85
+
86
+ progress ( 0 , totalSize ) ;
87
+
68
88
let url , cid ;
69
89
70
90
const p = readable
@@ -73,16 +93,21 @@ export async function ipfsAdd(coll, downloaderOpts = {}, replayOpts = {}, progre
73
93
. pipeTo (
74
94
new WritableStream ( {
75
95
write : ( res ) => {
76
- url = res . url ;
77
- cid = res . cid ;
96
+ if ( res . url && res . cid ) {
97
+ url = res . url ;
98
+ cid = res . cid ;
99
+ }
100
+ if ( res . size ) {
101
+ progress ( res . size , totalSize ) ;
102
+ }
78
103
} ,
79
104
} )
80
105
) ;
81
106
82
107
ipfsGenerateCar (
83
108
writable ,
84
109
dlResponse . filename , dlResponse . body ,
85
- swContent , uiContent , replayOpts ,
110
+ swContent , uiContent , htmlContent , replayOpts ,
86
111
downloaderOpts . markers , favicon ,
87
112
) ;
88
113
@@ -98,7 +123,9 @@ export async function ipfsAdd(coll, downloaderOpts = {}, replayOpts = {}, progre
98
123
}
99
124
100
125
export async function ipfsRemove ( coll ) {
101
- const autoipfs = await initAutoIPFS ( autoipfsOpts ) ;
126
+ if ( ! autoipfs ) {
127
+ autoipfs = await createAutoIPFS ( autoipfsOpts ) ;
128
+ }
102
129
103
130
if ( coll . config . metadata . ipfsPins ) {
104
131
@@ -138,16 +165,14 @@ async function ipfsWriteBuff(writer, name, content, dir) {
138
165
139
166
// ===========================================================================
140
167
export async function ipfsGenerateCar ( writable , waczPath ,
141
- waczContent , swContent , uiContent , replayOpts , markers , favicon ) {
168
+ waczContent , swContent , uiContent , htmlContent , replayOpts , markers , favicon ) {
142
169
143
170
const writer = UnixFS . createWriter ( { writable } ) ;
144
171
145
172
const rootDir = UnixFS . createDirectoryWriter ( writer ) ;
146
173
147
174
const encoder = new TextEncoder ( ) ;
148
175
149
- const htmlContent = getReplayHtml ( waczPath , replayOpts ) ;
150
-
151
176
await ipfsWriteBuff ( writer , "ui.js" , uiContent , rootDir ) ;
152
177
153
178
if ( replayOpts . showEmbed ) {
@@ -388,7 +413,7 @@ function getReplayHtml(waczPath, replayOpts = {}) {
388
413
</style>
389
414
</head>
390
415
<body>${ showEmbed ? `
391
- <replay-web-page ${ deepLink ? "deepLink=\"true\" " : "" } url="${ pageUrl } " loading="${ loading || "" } " embed="replay-with-info" src="${ waczPath } "></replay-web-page>` : `
416
+ <replay-web-page ${ deepLink ? "deepLink=\"true\" " : "" } ${ pageUrl ? ` url="${ pageUrl } "` : "" } loading="${ loading || "" } " embed="replay-with-info" src="${ waczPath } "></replay-web-page>` : `
392
417
<replay-app-main source="${ waczPath } "></replay-app-main>`
393
418
}
394
419
</body>
@@ -399,9 +424,6 @@ function getReplayHtml(waczPath, replayOpts = {}) {
399
424
400
425
// Copied from https://github.com/web3-storage/w3protocol/blob/main/packages/upload-client/src/sharding.js
401
426
402
- const SHARD_SIZE = 1024 * 1024 * 10 ;
403
- const CONCURRENT_UPLOADS = 3 ;
404
-
405
427
/**
406
428
* Shard a set of blocks into a set of CAR files. The last block is assumed to
407
429
* be the DAG root and becomes the CAR root CID for the last CAR output.
@@ -412,36 +434,45 @@ export class ShardingStream extends TransformStream {
412
434
/**
413
435
* @param {import('./types').ShardingOptions } [options]
414
436
*/
415
- constructor ( shardSize = SHARD_SIZE ) {
437
+ constructor ( shardSize ) {
416
438
/** @type {import('@ipld/unixfs').Block[] } */
417
439
let shard = [ ] ;
418
440
/** @type {import('@ipld/unixfs').Block[] | null } */
419
441
let readyShard = null ;
420
- let size = 0 ;
442
+ let readySize = 0 ;
443
+
444
+ let currSize = 0 ;
421
445
422
446
super ( {
423
447
async transform ( block , controller ) {
424
448
if ( readyShard != null ) {
425
- controller . enqueue ( await encodeBlocks ( readyShard ) ) ;
449
+ const blocks = await encodeBlocks ( readyShard ) ;
450
+ const size = readySize ;
451
+ controller . enqueue ( { blocks, size} ) ;
426
452
readyShard = null ;
427
453
}
428
- if ( shard . length && size + block . bytes . length > shardSize ) {
454
+ if ( shard . length && currSize + block . bytes . length > shardSize ) {
429
455
readyShard = shard ;
456
+ readySize = currSize ;
430
457
shard = [ ] ;
431
- size = 0 ;
458
+ currSize = 0 ;
432
459
}
433
460
shard . push ( block ) ;
434
- size += block . bytes . length ;
461
+ currSize += block . bytes . length ;
435
462
} ,
436
463
437
464
async flush ( controller ) {
438
465
if ( readyShard != null ) {
439
- controller . enqueue ( await encodeBlocks ( readyShard ) ) ;
466
+ const blocks = await encodeBlocks ( readyShard ) ;
467
+ const size = readySize ;
468
+ controller . enqueue ( { blocks, size} ) ;
440
469
}
441
470
442
471
const rootBlock = shard . at ( - 1 ) ;
443
472
if ( rootBlock != null ) {
444
- controller . enqueue ( await encodeBlocks ( shard , rootBlock . cid ) ) ;
473
+ const blocks = await encodeBlocks ( shard , rootBlock . cid ) ;
474
+ const size = currSize ;
475
+ controller . enqueue ( { blocks, size} ) ;
445
476
}
446
477
} ,
447
478
} ) ;
@@ -460,19 +491,20 @@ export class ShardingStream extends TransformStream {
460
491
* @extends {TransformStream<import('./types').CARFile, import('./types').CARMetadata> }
461
492
*/
462
493
export class ShardStoringStream extends TransformStream {
463
- constructor ( autoipfs , concurrency = CONCURRENT_UPLOADS ) {
494
+ constructor ( autoipfs , concurrency ) {
464
495
const queue = new Queue ( { concurrency } ) ;
465
496
const abortController = new AbortController ( ) ;
466
497
super ( {
467
- async transform ( car , controller ) {
498
+ async transform ( { blocks , size } , controller ) {
468
499
void queue . add (
469
500
async ( ) => {
470
501
try {
471
- //const opts = { ...options, signal: abortController.signal };
472
- //const cid = await add(conf, car, opts)
473
- const resUrls = await autoipfs . uploadCAR ( car ) ;
502
+ const cid = blocks . roots [ 0 ] ;
503
+
504
+ const resUrls = await autoipfs . uploadCAR ( blocks ) ;
505
+ const url = resUrls [ 0 ] ;
474
506
475
- controller . enqueue ( { cid : car . roots [ 0 ] , url : resUrls [ 0 ] } ) ;
507
+ controller . enqueue ( { cid, url, size } ) ;
476
508
477
509
//const { version, roots, size } = car
478
510
//controller.enqueue({ version, roots, cid, size })
0 commit comments