1 | const tape = require('tape')
|
2 | const tmp = require('temporary-directory')
|
3 | const create = require('./helpers/create')
|
4 | const Replicator = require('./helpers/replicator')
|
5 | const hyperdrive = require('..')
|
6 |
|
7 | tape('ram storage', function (t) {
|
8 | var drive = create()
|
9 |
|
10 | drive.ready(function () {
|
11 | t.ok(drive.metadata.writable, 'drive metadata is writable')
|
12 | t.ok(drive.contentWritable, 'drive content is writable')
|
13 | t.end()
|
14 | })
|
15 | })
|
16 |
|
17 | tape('dir storage with resume', function (t) {
|
18 | tmp(function (err, dir, cleanup) {
|
19 | t.ifError(err)
|
20 | var drive = hyperdrive(dir)
|
21 | drive.ready(function () {
|
22 | t.ok(drive.metadata.writable, 'drive metadata is writable')
|
23 | t.ok(drive.contentWritable, 'drive content is writable')
|
24 | t.same(drive.version, 1, 'drive has version 1')
|
25 | drive.close(function (err) {
|
26 | t.ifError(err)
|
27 |
|
28 | var drive2 = hyperdrive(dir)
|
29 | drive2.ready(function (err) {
|
30 | t.error(err, 'no error')
|
31 | t.ok(drive2.metadata.writable, 'drive2 metadata is writable')
|
32 | t.ok(drive2.contentWritable, 'drive2 content is writable')
|
33 | t.same(drive2.version, 1, 'drive has version 1')
|
34 |
|
35 | cleanup(function (err) {
|
36 | t.ifError(err)
|
37 | t.end()
|
38 | })
|
39 | })
|
40 | })
|
41 | })
|
42 | })
|
43 | })
|
44 |
|
45 | tape('dir storage for non-writable drive', function (t) {
|
46 | const r = new Replicator(t)
|
47 | var src = create()
|
48 | src.ready(function () {
|
49 | tmp(function (err, dir, cleanup) {
|
50 | t.ifError(err)
|
51 |
|
52 | var clone = hyperdrive(dir, src.key)
|
53 | clone.ready(function () {
|
54 | t.ok(!clone.metadata.writable, 'clone metadata not writable')
|
55 | t.ok(!clone.contentWritable, 'clone content not writable')
|
56 | t.same(clone.key, src.key, 'keys match')
|
57 | cleanup(function (err) {
|
58 | t.ifError(err)
|
59 | r.end()
|
60 | })
|
61 | })
|
62 |
|
63 | r.replicate(src, clone)
|
64 | })
|
65 | })
|
66 | })
|
67 |
|
68 | tape('dir storage without permissions emits error', function (t) {
|
69 |
|
70 | t.plan(1)
|
71 | var drive = hyperdrive('/')
|
72 | drive.on('error', function (err) {
|
73 | t.ok(err, 'got error')
|
74 | })
|
75 | })
|
76 |
|
77 | tape('write and read (sparse)', function (t) {
|
78 | const r = new Replicator(t)
|
79 | t.plan(3)
|
80 |
|
81 | tmp(function (err, dir, cleanup) {
|
82 | t.ifError(err)
|
83 | var drive = hyperdrive(dir)
|
84 | drive.on('ready', function () {
|
85 | var clone = create(drive.key, { sparse: true })
|
86 | clone.on('ready', function () {
|
87 | drive.writeFile('/hello.txt', 'world', function (err) {
|
88 | t.error(err, 'no error')
|
89 | r.replicate(clone, drive)
|
90 | setTimeout(() => {
|
91 | var readStream = clone.createReadStream('/hello.txt')
|
92 | readStream.on('error', function (err) {
|
93 | t.error(err, 'no error')
|
94 | })
|
95 | readStream.on('data', function (data) {
|
96 | t.same(data.toString(), 'world')
|
97 | r.end()
|
98 | })
|
99 | }, 50)
|
100 | })
|
101 | })
|
102 | })
|
103 | })
|
104 | })
|
105 |
|
106 | tape('sparse read/write two files', function (t) {
|
107 | const r = new Replicator(t)
|
108 | var drive = create()
|
109 | drive.on('ready', function () {
|
110 | var clone = create(drive.key, { sparse: true })
|
111 | clone.ready(err => {
|
112 | t.error(err, 'no error')
|
113 | drive.writeFile('/hello.txt', 'world', function (err) {
|
114 | t.error(err, 'no error')
|
115 | drive.writeFile('/hello2.txt', 'world', function (err) {
|
116 | t.error(err, 'no error')
|
117 | r.replicate(drive, clone)
|
118 | clone.metadata.update(start)
|
119 | })
|
120 | })
|
121 | })
|
122 |
|
123 | function start () {
|
124 | clone.stat('/hello.txt', function (err, stat) {
|
125 | t.error(err, 'no error')
|
126 | t.ok(stat, 'has stat')
|
127 | clone.readFile('/hello.txt', function (err, data) {
|
128 | t.error(err, 'no error')
|
129 | t.same(data.toString(), 'world', 'data ok')
|
130 | clone.stat('/hello2.txt', function (err, stat) {
|
131 | t.error(err, 'no error')
|
132 | t.ok(stat, 'has stat')
|
133 | clone.readFile('/hello2.txt', function (err, data) {
|
134 | t.error(err, 'no error')
|
135 | t.same(data.toString(), 'world', 'data ok')
|
136 | r.end()
|
137 | })
|
138 | })
|
139 | })
|
140 | })
|
141 | }
|
142 | })
|
143 | })
|
144 |
|
145 |
|
146 | tape('destroying the drive destroys its data', function (t) {
|
147 | tmp(function (err, dir, cleanup) {
|
148 | t.ifError(err)
|
149 | const initial = hyperdrive(dir)
|
150 | initial.writeFile('/example.txt', 'Hello World!', function (err) {
|
151 | t.ifError(err)
|
152 | initial.destroyStorage(function (err) {
|
153 | t.ifError(err)
|
154 | const copy = hyperdrive(dir)
|
155 |
|
156 | copy.readdir('/', function (err, files) {
|
157 | t.ifError(err)
|
158 | t.deepEqual(files, [], 'archive now empty')
|
159 | copy.close(function (err) {
|
160 | t.ifError(err)
|
161 | cleanup(function(err) {
|
162 | t.ifError(err)
|
163 | t.end()
|
164 | })
|
165 | })
|
166 | })
|
167 | })
|
168 | })
|
169 | })
|
170 | })
|