-
Notifications
You must be signed in to change notification settings - Fork 136
/
test.js
1707 lines (1358 loc) · 48.9 KB
/
test.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const fs = require('fs')
const os = require('os')
const path = require('path')
const { once } = require('events')
const test = require('brittle')
const Corestore = require('corestore')
const RAM = require('random-access-memory')
const { discoveryKey } = require('hypercore-crypto')
const { pipelinePromise: pipeline, Writable, Readable } = require('streamx')
const testnet = require('hyperdht/testnet')
const DHT = require('hyperdht')
const Hyperswarm = require('hyperswarm')
const b4a = require('b4a')
const Hyperdrive = require('./index.js')
test('drive.core', async (t) => {
const { drive } = await testenv(t.teardown)
t.is(drive.db.feed, drive.core)
})
test('drive.version', async (t) => {
const { drive } = await testenv(t.teardown)
await drive.put(__filename, fs.readFileSync(__filename))
t.is(drive.db.feed.length, drive.version)
})
test('drive.key', async (t) => {
const { drive } = await testenv(t.teardown)
t.is(b4a.compare(drive.db.feed.key, drive.key), 0)
})
test('drive.discoveryKey', async (t) => {
const { drive } = await testenv(t.teardown)
t.is(b4a.compare(drive.discoveryKey, discoveryKey(drive.key)), 0)
})
test('drive.contentKey', async (t) => {
const { drive } = await testenv(t.teardown)
t.is(b4a.compare(drive.blobs.core.key, drive.contentKey), 0)
})
test('drive.getBlobs()', async (t) => {
const { drive } = await testenv(t.teardown)
const blobs = await drive.getBlobs()
t.is(blobs, drive.blobs)
})
test('drive.supportsMetadata', async (t) => {
const { drive } = await testenv(t.teardown)
t.is(true, drive.supportsMetadata)
})
test('Hyperdrive(corestore, key)', async (t) => {
t.plan(2)
const { corestore, drive } = await testenv(t.teardown)
const diskbuf = fs.readFileSync(__filename)
await drive.put(__filename, diskbuf)
const bndlbuf = await drive.get(__filename)
t.is(b4a.compare(diskbuf, bndlbuf), 0)
const mirror = new Hyperdrive(corestore.session({ writable: false }), drive.core.key)
await mirror.ready()
const mrrrbuf = await mirror.get(__filename)
t.is(b4a.compare(bndlbuf, mrrrbuf), 0)
})
test('drive.put(path, buf) and drive.get(path)', async (t) => {
{
const { drive } = await testenv(t.teardown)
const diskbuf = fs.readFileSync(__filename)
await drive.put(__filename, diskbuf)
const bndlbuf = await drive.get(__filename)
t.is(b4a.compare(diskbuf, bndlbuf), 0)
}
{
const { drive } = await testenv(t.teardown)
const tmppath = path.join(os.tmpdir(), 'hyperdrive-test-')
const dirpath = fs.mkdtempSync(tmppath)
const filepath = path.join(dirpath, 'hello-world.js')
const bndlbuf = b4a.from('module.exports = () => \'Hello, World!\'')
await drive.put(filepath, bndlbuf)
fs.writeFileSync(filepath, await drive.get(filepath))
const diskbuf = fs.readFileSync(filepath)
t.is(b4a.compare(diskbuf, bndlbuf), 0)
t.is(require(filepath)(), 'Hello, World!')
}
})
test('drive.get(path, { wait: false }) throws if entry exists but not found', async (t) => {
const { drive, mirror } = await testenv(t.teardown)
const otherDrive = mirror.drive
const s1 = drive.corestore.replicate(true)
const s2 = otherDrive.corestore.replicate(false)
s1.pipe(s2).pipe(s1)
await drive.put('/file', 'content')
await eventFlush()
await otherDrive.entry('/file') // Ensure in bee
await t.exception(() => otherDrive.get('/file', { wait: false }), /BLOCK_NOT_AVAILABLE/)
t.is(b4a.toString((await otherDrive.get('/file'))), 'content', 'sanity check: can actually get content')
})
test('drive.createWriteStream(path) and drive.createReadStream(path)', async (t) => {
{
const { drive } = await testenv(t.teardown)
const diskbuf = await fs.readFileSync(__filename)
await pipeline(
fs.createReadStream(__filename),
drive.createWriteStream(__filename)
)
let bndlbuf = null
await pipeline(
drive.createReadStream(__filename),
new Writable({
write (data, cb) {
if (bndlbuf) bndlbuf = b4a.concat(bndlbuf, data)
else bndlbuf = data
return cb(null)
}
})
)
t.is(b4a.compare(diskbuf, bndlbuf), 0)
}
{
const { drive } = await testenv(t.teardown)
const tmppath = path.join(os.tmpdir(), 'hyperdrive-test-')
const dirpath = fs.mkdtempSync(tmppath)
const filepath = path.join(dirpath, 'hello-world.js')
const bndlbuf = b4a.from('module.exports = () => \'Hello, World!\'')
await pipeline(
Readable.from(bndlbuf),
drive.createWriteStream(filepath)
)
await pipeline(
drive.createReadStream(filepath),
fs.createWriteStream(filepath)
)
const diskbuf = fs.readFileSync(filepath)
t.is(b4a.compare(diskbuf, bndlbuf), 0)
t.is(require(filepath)(), 'Hello, World!')
}
})
test('drive.createReadStream() with start/end options', async (t) => {
const { drive, paths } = await testenv(t.teardown)
const filepath = path.join(paths.tmp, 'hello-world.js')
const bndlbuf = b4a.from('module.exports = () => \'Hello, World!\'')
await pipeline(
Readable.from(bndlbuf),
drive.createWriteStream(filepath)
)
const stream = drive.createReadStream(filepath, {
start: 0,
end: 0
})
const drivebuf = await streamToBuffer(stream)
t.is(drivebuf.length, 1)
t.is(drivebuf.toString(), 'm')
const stream2 = drive.createReadStream(filepath, {
start: 5,
end: 7
})
const drivebuf2 = await streamToBuffer(stream2)
t.is(drivebuf2.length, 3)
t.is(drivebuf2.toString(), 'e.e')
})
test('drive.del() deletes entry at path', async (t) => {
t.plan(3)
const { drive } = await testenv(t.teardown)
await drive.put(__filename, fs.readFileSync(__filename))
let buf = await drive.get(__filename)
t.ok(b4a.isBuffer(buf))
await drive.del(__filename)
buf = await drive.get(__filename)
t.is(buf, null)
const entry = await drive.entry(__filename)
t.is(entry, null)
})
test('drive.symlink(from, to) updates the entry at <from> to include a reference for <to>', async (t) => {
const { drive } = await testenv(t.teardown)
const buf = fs.readFileSync(__filename)
await drive.put(__filename, buf)
await drive.symlink('pointer', __filename)
const result = await drive.get('pointer')
t.is(result, null)
const entry = await drive.entry('pointer')
t.is(entry.value.linkname, __filename)
t.is(b4a.compare(buf, await drive.get(entry.value.linkname)), 0)
})
test('drive.entry(path) gets entry at path', async (t) => {
const linkname = 'linkname'
{
const { drive } = await testenv(t.teardown)
const buf = fs.readFileSync(__filename)
await drive.put(__filename, buf)
const { value: entry } = await drive.entry(__filename)
t.ok(entry.blob)
t.is(entry.linkname, null)
t.is(entry.executable, false)
}
{
const { drive } = await testenv(t.teardown)
const buf = fs.readFileSync(__filename)
await drive.put(__filename, buf, { executable: false })
const { value: entry } = await drive.entry(__filename)
t.ok(entry.blob)
t.is(entry.linkname, null)
t.is(entry.executable, false)
}
{
const { drive } = await testenv(t.teardown)
const buf = fs.readFileSync(__filename)
await drive.put(__filename, buf, { executable: true })
const { value: entry } = await drive.entry(__filename)
t.ok(entry.blob)
t.is(entry.linkname, null)
t.is(entry.executable, true)
}
{
const { drive } = await testenv(t.teardown)
const buf = fs.readFileSync(__filename)
await drive.put(__filename, buf, { executable: false })
await drive.symlink(__filename, linkname)
const { value: entry } = await drive.entry(__filename)
t.is(entry.blob, null)
t.is(entry.executable, false)
t.is(entry.linkname, linkname)
}
{
const { drive } = await testenv(t.teardown)
const buf = fs.readFileSync(__filename)
await drive.put(__filename, buf, { executable: true })
await drive.symlink(__filename, linkname)
const { value: entry } = await drive.entry(__filename)
t.is(entry.blob, null)
t.is(entry.executable, false)
t.is(entry.linkname, linkname)
}
{
const { drive } = await testenv(t.teardown)
await drive.symlink(linkname, __filename)
const { value: entry } = await drive.entry(linkname)
t.is(entry.blob, null)
t.is(entry.executable, false)
t.is(entry.linkname, __filename)
}
{
const { drive } = await testenv(t.teardown)
const ws = drive.createWriteStream(__filename)
ws.write(fs.readFileSync(__filename))
ws.end()
await once(ws, 'finish')
const { value: entry } = await drive.entry(__filename)
t.ok(entry.blob)
t.is(entry.linkname, null)
t.is(entry.executable, false)
}
{
const { drive } = await testenv(t.teardown)
const ws = drive.createWriteStream(__filename, { executable: false })
ws.write(fs.readFileSync(__filename))
ws.end()
await once(ws, 'finish')
const { value: entry } = await drive.entry(__filename)
t.ok(entry.blob)
t.is(entry.linkname, null)
t.is(entry.executable, false)
}
{
const { drive } = await testenv(t.teardown)
const ws = drive.createWriteStream(__filename, { executable: true })
ws.write(fs.readFileSync(__filename))
ws.end()
await once(ws, 'finish')
const { value: entry } = await drive.entry(__filename)
t.ok(entry.blob)
t.is(entry.linkname, null)
t.is(entry.executable, true)
}
})
test('entry(key) resolve key path', async function (t) {
const { drive } = await testenv(t.teardown)
await drive.put('/README.md', b4a.from('# title'))
await drive.put('/examples/a.txt', b4a.from('a text'))
await drive.put('/examples/more/c.txt', b4a.from('c text'))
t.alike((await drive.entry('README.md')).key, '/README.md')
t.alike((await drive.entry('/examples/more/../a.txt')).key, '/examples/a.txt')
t.alike((await drive.entry('\\examples\\more\\c.txt')).key, '/examples/more/c.txt')
})
test('get(key) resolve key path', async function (t) {
const { drive } = await testenv(t.teardown)
await drive.put('/README.md', b4a.from('# title'))
await drive.put('/examples/a.txt', b4a.from('a text'))
await drive.put('/examples/more/c.txt', b4a.from('c text'))
const buffer = await drive.get('/README.md')
const a = await drive.get('/examples/a.txt')
const c = await drive.get('/examples/more/c.txt')
t.ok(buffer)
t.ok(a)
t.ok(c)
t.alike(await drive.get('README.md'), buffer)
t.alike(await drive.get('/examples/more/../a.txt'), a)
t.alike(await drive.get('\\examples\\more\\c.txt'), c)
})
test('entry(key) resolves object', async function (t) {
const { drive } = await testenv(t.teardown)
await drive.put('/README.md', b4a.from('# title'))
const entry = await drive.entry('/README.md')
t.alike(entry, await drive.entry(entry))
})
test('del(key) resolve key path', async function (t) {
const { drive } = await testenv(t.teardown)
const delAndEntry = async (key, expectedKey) => {
await drive.put(expectedKey, b4a.from('')) // pre-create
t.ok(await drive.entry(expectedKey))
await drive.del(key)
t.absent(await drive.entry(expectedKey))
}
await delAndEntry('README.md', '/README.md')
await delAndEntry('/examples/more/../a.txt', '/examples/a.txt')
await delAndEntry('\\examples\\more\\c.txt', '/examples/more/c.txt')
})
test('put(key, buffer) resolve key path', async function (t) {
const { drive } = await testenv(t.teardown)
const putAndEntry = async (key, expectedKey) => {
t.absent(await drive.entry(expectedKey))
await drive.put(key, b4a.from(''))
t.ok(await drive.entry(expectedKey))
}
await putAndEntry('b.txt', '/b.txt')
await putAndEntry('/examples/more/../f.txt', '/examples/f.txt')
await putAndEntry('\\examples\\more\\h.txt', '/examples/more/h.txt')
})
test('symlink(key, linkname) resolve key path', async function (t) {
const { drive } = await testenv(t.teardown)
await drive.put('/README.md', b4a.from('# title'))
const symlinkAndEntry = async (key, expectedKey) => {
t.absent(await drive.entry(expectedKey))
await drive.symlink(key, '/README.md')
t.ok(await drive.entry(expectedKey))
}
await symlinkAndEntry('b.txt', '/b.txt')
await symlinkAndEntry('/examples/more/../f.txt', '/examples/f.txt')
await symlinkAndEntry('\\examples\\more\\h.txt', '/examples/more/h.txt')
})
test('watch() basic', async function (t) {
t.plan(5)
const { drive } = await testenv(t.teardown)
const buf = b4a.from('hi')
const watcher = drive.watch()
eventFlush().then(async () => {
await drive.put('/a.txt', buf)
})
for await (const [current, previous] of watcher) { // eslint-disable-line no-unreachable-loop
t.ok(current instanceof Hyperdrive)
t.ok(previous instanceof Hyperdrive)
t.is(current.version, 2)
t.is(previous.version, 1)
t.alike(await current.get('/a.txt'), buf)
break
}
})
test('watch(folder) basic', async function (t) {
t.plan(1)
const { drive } = await testenv(t.teardown)
const buf = b4a.from('hi')
await drive.put('/README.md', buf)
await drive.put('/examples/a.txt', buf)
await drive.put('/examples/more/a.txt', buf)
const watcher = drive.watch('/examples')
let next = watcher.next()
let onchange = null
next.then(data => {
next = watcher.next()
onchange(data)
})
onchange = () => t.fail('should not trigger changes')
await drive.put('/b.txt', buf)
await eventFlush()
onchange = null
onchange = () => t.pass('change')
await drive.put('/examples/b.txt', buf)
await eventFlush()
onchange = null
})
test('watch(folder) should normalize folder', async function (t) {
t.plan(1)
const { drive } = await testenv(t.teardown)
const buf = b4a.from('hi')
const watcher = drive.watch('examples//more//')
let next = watcher.next()
let onchange = null
next.then(data => {
next = watcher.next()
onchange(data)
})
onchange = () => t.fail('should not trigger changes')
await drive.put('/examples/a.txt', buf)
await eventFlush()
onchange = null
onchange = () => t.pass('change')
await drive.put('/examples/more/a.txt', buf)
await eventFlush()
onchange = null
})
test('drive.diff(length)', async (t) => {
const { drive, paths: { root, tmp } } = await testenv(t.teardown)
const paths = []
for await (const _path of readdirator(root, { filter })) {
const buf = fs.readFileSync(_path)
const relpath = _path.replace(root, '')
const tmppath = path.join(tmp, relpath)
try {
fs.writeFileSync(tmppath, buf)
} catch {
fs.mkdirSync(path.dirname(tmppath), { recursive: true })
fs.writeFileSync(tmppath, buf)
}
await drive.put(relpath, buf)
paths.push([tmppath, relpath])
}
const [tmppath, relpath] = paths[Math.floor(Math.random() * paths.length)]
await drive.put(relpath + '.old', fs.readFileSync(tmppath))
await drive.del(relpath)
for await (const diff of drive.diff(drive.core.length - 2)) {
if (diff.right) t.is(diff.right.key, relpath.replace(/\\/g, '/'))
if (diff.left) t.is(diff.left.key, relpath.replace(/\\/g, '/') + '.old')
}
})
test('drive.entries()', async (t) => {
const { drive, paths: { root } } = await testenv(t.teardown)
const entries = new Set()
for await (const path of readdirator(root, { filter })) {
await drive.put(path, fs.readFileSync(path))
entries.add(await drive.entry(path))
}
for await (const entry of drive.entries()) {
for (const _entry of entries) {
if (JSON.stringify(_entry) === JSON.stringify(entry)) {
entries.delete(_entry)
break
}
}
}
t.is(entries.size, 0)
})
test('drive.entries() with explicit range, no opts', async (t) => {
const { drive } = await testenv(t.teardown)
await drive.put('/aFile', 'here')
await drive.put('/bFile', 'later')
await drive.put('/zFile', 'last')
const expected = ['/bFile', '/zFile']
const observed = []
for await (const entry of drive.entries({ gt: '/b', lte: '/zzz' })) {
observed.push(entry.key)
}
t.alike(expected, expected)
})
test('drive.entries() with explicit range and opts', async (t) => {
const { drive } = await testenv(t.teardown)
await drive.put('/aFile', 'here')
await drive.put('/bFile', 'later')
await drive.put('/zFile', 'last')
const expected = ['/zFile', '/bFile']
const observed = []
for await (const entry of drive.entries({ gt: '/b', lte: '/zzz' }, { reverse: true })) {
observed.push(entry.key)
}
t.alike(observed, expected)
})
test('drive.list(folder, { recursive })', async (t) => {
{
const { drive, paths: { root } } = await testenv(t.teardown)
for await (const path of readdirator(root, { filter })) {
await drive.put(path, fs.readFileSync(path))
}
for await (const entry of drive.list(root)) {
t.is(b4a.compare(fs.readFileSync(entry.key), await drive.get(entry.key)), 0)
}
}
{
const { drive, paths: { root } } = await testenv(t.teardown)
for await (const path of readdirator(root, { filter })) {
await drive.put(path, fs.readFileSync(path))
}
for await (const entry of drive.list(root, { recursive: true })) {
t.is(b4a.compare(fs.readFileSync(entry.key), await drive.get(entry.key)), 0)
}
}
{
const { drive, paths: { root } } = await testenv(t.teardown)
for await (const path of readdirator(root, { filter })) {
await drive.put(path, fs.readFileSync(path))
}
for await (const entry of drive.list(root, { recursive: false })) {
t.is(b4a.compare(fs.readFileSync(entry.key), await drive.get(entry.key)), 0)
}
}
{
const { drive } = await testenv(t.teardown)
const emptybuf = b4a.from('')
await drive.put('/grandparent', emptybuf)
await drive.put('/grandparent/parent', emptybuf)
await drive.put('/grandparent/parent/child', emptybuf)
await drive.put('/grandparent/parent/child/fst-grandchild.file', emptybuf)
await drive.put('/grandparent/parent/child/snd-grandchild.file', emptybuf)
const paths = ['/grandparent', '/grandparent/parent', '/grandparent/parent/child']
for (const [_idx, path] of Object.entries(paths)) {
const idx = parseInt(_idx)
const set = new Set()
for await (const entry of drive.list(path)) set.add(entry.key)
t.ok(paths.slice(0, idx).every((path) => !set.has(path)))
t.ok(paths.slice(idx, paths.length).every((path) => Array.from(set).some((_path) => _path.includes(path))))
}
}
})
test('drive.readdir(path)', async (t) => {
{
const { drive, paths: { root } } = await testenv(t.teardown)
const files = new Map()
for await (const path of readdirator(root, { filter })) {
const buf = fs.readFileSync(path)
await drive.put(path, buf)
files.set(path, buf)
}
const readdir = drive.readdir.bind(drive)
const isDirectory = async (x) => !(await drive.entry(x))?.value.blob
for await (const path of readdirator(root, { readdir, isDirectory })) {
t.is(b4a.compare(files.get(path), await drive.get(path)), 0)
}
}
{
const { drive } = await testenv(t.teardown)
await drive.put('/parent/child', b4a.from('child'))
await drive.put('/parent/sibling', b4a.from('sibling'))
await drive.put('/parent/sibling/grandchild', b4a.from('grandchild'))
const read = []
for await (const path of drive.readdir('/parent')) read.push(path)
t.is(read[0], 'child')
t.is(read[1], 'sibling')
t.is(read.length, 2)
}
{
const { drive } = await testenv(t.teardown)
await drive.put('/parent/child', b4a.from('child'))
await drive.put('/parent/sibling', b4a.from('sibling'))
await drive.put('/parent/sibling/grandchild', b4a.from('grandchild'))
const read = []
for await (const path of drive.readdir('/parent/sibling')) read.push(path)
t.is(read[0], 'grandchild')
t.is(read.length, 1)
}
})
test('drive.checkout(len)', async (t) => {
const { drive, paths: { root } } = await testenv(t.teardown)
const lens = new Map()
for await (const path of readdirator(root, { filter })) {
const buf = fs.readFileSync(path)
await drive.put(path, buf)
lens.set(drive.core.length, path)
}
for (const offset of lens.keys()) {
const snapshot = await drive.checkout(offset)
t.ok(snapshot.get(lens.get(offset)))
let low = offset
while (lens.has(--low)) t.ok(await snapshot.get(lens.get(low)))
let high = offset
while (lens.has(++high)) t.ok(!(await snapshot.get(lens.get(high))))
}
})
test('drive.download(folder, [options])', async (t) => {
t.plan(7)
const { corestore, drive, swarm, mirror } = await testenv(t.teardown)
swarm.on('connection', (conn) => corestore.replicate(conn))
swarm.join(drive.discoveryKey, { server: true, client: false })
await swarm.flush()
mirror.swarm.on('connection', (conn) => mirror.corestore.replicate(conn))
mirror.swarm.join(drive.discoveryKey, { server: false, client: true })
await mirror.swarm.flush()
const nil = b4a.from('nil')
let count = 0
let max = -Infinity
await drive.put('/parent/child/grandchild1', nil)
await drive.put('/parent/child/grandchild2', nil)
const blobs = await mirror.drive.getBlobs()
blobs.core.on('download', (offset) => {
count++
if (max < offset) max = offset
})
const l = drive.blobs.core.length
await drive.put('/parent/sibling/grandchild1', nil)
t.is(count, 0)
await mirror.drive.download('/parent/child')
t.is(max, l - 1)
const _count = count
t.ok(await mirror.drive.get('/parent/child/grandchild1'))
t.is(_count, count)
t.ok(await mirror.drive.get('/parent/child/grandchild2'))
t.is(_count, count)
const entry = await mirror.drive.entry('/parent/sibling/grandchild1')
await blobs.get(entry.value.blob)
t.is(count, _count + 1)
})
test('drive.download(filename, [options])', async (t) => {
const { corestore, drive, swarm, mirror } = await testenv(t.teardown)
swarm.on('connection', (conn) => corestore.replicate(conn))
swarm.join(drive.discoveryKey, { server: true, client: false })
await swarm.flush()
mirror.swarm.on('connection', (conn) => mirror.corestore.replicate(conn))
mirror.swarm.join(drive.discoveryKey, { server: false, client: true })
await mirror.swarm.flush()
const nil = b4a.from('nil')
await drive.put('/parent/grandchild1', nil)
await drive.put('/file', nil)
await drive.put('/parent/grandchild2', nil)
await mirror.drive.getBlobs()
await mirror.drive.download('/file')
t.ok(await mirror.drive.get('/file', { wait: false }))
try {
await mirror.drive.get('/file1', { wait: false })
} catch {
t.pass('not downloaded')
}
})
test.skip('drive.downloadRange(dbRanges, blobRanges)', async (t) => {
const { drive, swarm, mirror, corestore } = await testenv(t.teardown)
swarm.on('connection', (conn) => corestore.replicate(conn))
swarm.join(drive.discoveryKey, { server: true, client: false })
await swarm.flush()
mirror.swarm.on('connection', (conn) => mirror.corestore.replicate(conn))
mirror.swarm.join(drive.discoveryKey, { server: false, client: true })
await mirror.swarm.flush()
const blobs = await drive.getBlobs()
const nil = b4a.from('nil')
const fileBlocks = []
const blobBlocks = []
await drive.put('/0', nil)
fileBlocks.push(drive.core.length)
blobBlocks.push(blobs.core.length)
await drive.put('/1', nil)
await drive.put('/2', nil)
fileBlocks.push(drive.core.length)
blobBlocks.push(blobs.core.length)
const fileTelem = downloadShark(mirror.drive.core)
const blobTelem = downloadShark((await mirror.drive.getBlobs()).core)
const fileCount = fileTelem.count
const blobCount = blobTelem.count
await mirror.drive.get('/0')
t.is(blobCount, blobTelem.count)
t.is(fileCount, fileTelem.count)
})
test.skip('drive.downloadDiff(version, folder, [options])', async (t) => {
const { drive, swarm, mirror, corestore } = await testenv(t.teardown)
swarm.on('connection', (conn) => corestore.replicate(conn))
swarm.join(drive.discoveryKey, { server: true, client: false })
await swarm.flush()
mirror.swarm.on('connection', (conn) => mirror.corestore.replicate(conn))
mirror.swarm.join(drive.discoveryKey, { server: false, client: true })
await mirror.swarm.flush()
const nil = b4a.from('nil')
await drive.put('/parent/child/0', nil)
await drive.put('/parent/sibling/0')
await drive.put('/parent/child/1', nil)
const version = drive.version
const filestelem = downloadShark(mirror.drive.core)
const blobstelem = downloadShark((await mirror.drive.getBlobs()).core)
await mirror.drive.downloadDiff(version, '/parent/child')
let filescount = filestelem.count
let blobscount = blobstelem.count
await mirror.drive.get('/parent/child/1')
t.is(filescount, filestelem.count)
t.is(blobscount, blobstelem.count)
await drive.put('/parent/child/2', nil)
await mirror.drive.downloadDiff(version, '/parent/child')
t.is(blobscount + 1, blobstelem.count)
filescount = filestelem.count
blobscount = blobstelem.count
await mirror.drive.get('/parent/sibling/0')
t.is(filescount + 1, filestelem.count)
t.is(blobscount + 1, blobstelem.count)
})
test('drive.batch() & drive.flush()', async (t) => {
const { drive } = await testenv(t.teardown)
const batch = drive.batch()
await batch.put('/file.txt', b4a.from('abc'))
t.absent(await drive.get('/file.txt'))
await batch.flush()
t.ok(batch.blobs.core.closed)
t.absent(drive.blobs.core.closed)
t.absent(drive.db.closed)
t.absent(drive.db.core.closed)
t.ok(await drive.get('/file.txt'))
await drive.close()
t.ok(drive.blobs.core.closed)
t.ok(drive.db.closed)
t.ok(drive.db.core.closed)
})
test('batch.list()', async (t) => {
t.plan(1)
const { drive } = await testenv(t.teardown)
const nil = b4a.from('nil')
await drive.put('/x', nil)
const batch = drive.batch()
for await (const entry of batch.list()) {
t.is(entry.key, '/x')
}
await batch.flush()
})
test('drive.close()', async (t) => {
t.plan(2)
const { drive } = await testenv(t.teardown)
const blobs = await drive.getBlobs()
blobs.core.on('close', () => t.ok(true))
drive.core.on('close', () => t.ok(true))
await drive.close()
})
test('drive.close() on snapshots--does not close parent', async (t) => {
const { drive } = await testenv(t.teardown)
await drive.put('/foo', b4a.from('bar'))
const checkout = drive.checkout(2)
await checkout.get('/foo')
await checkout.close()
// Main test is that there is no session_closed error on drive.get
const res = await drive.get('/foo')
t.alike(res, b4a.from('bar'))
})
test('drive.batch() on non-ready drive', async (t) => {
const drive = new Hyperdrive(new Corestore(RAM))
const batch = drive.batch()
await batch.put('/x', 'something')
await batch.flush()
t.is(batch.blobs.core.closed, true)
t.ok(await drive.get('/x'))
})
test('drive.close() for future checkout', async (t) => {
const { drive } = await testenv(t.teardown)
await drive.put('some', 'thing')
const checkout = drive.checkout(drive.length + 1)
await checkout.close()
t.is(checkout.closed, true)
t.is(checkout.db.core.closed, true)
t.is(drive.closed, false)
t.is(drive.db.core.closed, false)
})
test('drive.close() with openBlobsFromHeader waiting in the background', async (t) => {
t.plan(3)
const corestore = new Corestore(RAM)
const disconnectedCoreKey = b4a.from('a'.repeat(64), 'hex')
const drive = new Hyperdrive(corestore, disconnectedCoreKey)
await drive.ready()
t.is(drive.core.length, 0) // Sanity check
// length 0 (unavailable), so _openBlobsFromHeader will be awaiting its header
// Testing against a regression where close silently errored and never finished
drive.core.on('close', () => t.ok(true))
await drive.close()
t.ok(drive.corestore.closed)
})
test.skip('drive.findingPeers()', async (t) => {
const { drive, corestore, swarm, mirror } = await testenv(t.teardown)
await drive.put('/', b4a.from('/'))
swarm.on('connection', (conn) => corestore.replicate(conn))
swarm.join(drive.discoveryKey, { server: true, client: false })
await swarm.flush()
mirror.swarm.on('connection', (conn) => mirror.corestore.replicate(conn))
mirror.swarm.join(drive.discoveryKey, { server: false, client: true })
const done = mirror.drive.findingPeers()
swarm.flush().then(done, done)
t.ok(await mirror.drive.get('/'))
})
test('drive.mirror()', async (t) => {
const { drive: a } = await testenv(t.teardown)
const { drive: b } = await testenv(t.teardown)
await a.put('/file.txt', 'hello world')
await a.mirror(b).done()
t.alike(await b.get('/file.txt'), b4a.from('hello world'))
})
test('blobs with writable drive', async (t) => {
t.plan(4)
const store = new Corestore(RAM)
const drive = new Hyperdrive(store)
drive.on('blobs', function (blobs) {
t.is(blobs, drive.blobs)
})
drive.on('content-key', function (key) {
t.alike(key, drive.blobs.core.key)
})
t.absent(drive.blobs)
await drive.ready()
t.ok(drive.blobs)
})
test('drive.clear(path)', async (t) => {
const { drive } = await testenv(t.teardown)
await drive.put('/loc', 'hello world')
const entry = await drive.entry('/loc')
const initContent = await drive.blobs.get(entry.value.blob, { wait: false })
t.alike(initContent, b4a.from('hello world'))
const cleared = await drive.clear('/loc')
t.is(cleared, null)
// Entry still exists (so file not deleted)
const nowEntry = await drive.entry('/loc')
t.alike(nowEntry, entry)
// But the blob is removed from storage
const nowContent = await drive.blobs.get(entry.value.blob, { wait: false })
t.is(nowContent, null)
})
test('drive.clear(path) with diff', async (t) => {
const storage = createTmpDir(t)
const a = new Hyperdrive(new Corestore(storage))
await a.put('/file', b4a.alloc(4 * 1024))
await a.close()
const b = new Hyperdrive(new Corestore(storage))
const cleared = await b.clear('/file', { diff: true })
t.ok(cleared.blocks > 0)
const cleared2 = await b.clear('/file', { diff: true })
t.is(cleared2.blocks, 0)
const cleared3 = await b.clear('/not-exists', { diff: true })
t.is(cleared3.blocks, 0)
await b.close()
})
test('drive.clear(path) on a checkout', async (t) => {
const { drive } = await testenv(t.teardown)
await drive.put('/loc', 'hello world')
const entry = await drive.entry('/loc')
const initContent = await drive.blobs.get(entry.value.blob, { wait: false })
t.alike(initContent, b4a.from('hello world'))
const checkout = drive.checkout(drive.version)