@@ -7,16 +7,16 @@ test('can write/read a file from a remote hyperdrive', async t => {
7
7
const { client, cleanup } = await createOne ( )
8
8
9
9
try {
10
- const { opts , id } = await client . drive . get ( )
11
- t . true ( opts . key )
12
- t . same ( id , 1 )
10
+ const drive = await client . drive . get ( )
11
+ t . true ( drive . key )
12
+ t . same ( drive . id , 1 )
13
13
14
- await client . drive . writeFile ( id , 'hello' , 'world' )
14
+ await drive . writeFile ( 'hello' , 'world' )
15
15
16
- const contents = await client . drive . readFile ( id , 'hello' )
16
+ const contents = await drive . readFile ( 'hello' )
17
17
t . same ( contents , Buffer . from ( 'world' ) )
18
18
19
- await client . drive . close ( id )
19
+ await drive . close ( )
20
20
} catch ( err ) {
21
21
t . fail ( err )
22
22
}
@@ -31,16 +31,16 @@ test('can write/read a large file from a remote hyperdrive', async t => {
31
31
const content = Buffer . alloc ( 3.9e6 * 10.11 ) . fill ( 'abcdefghi' )
32
32
33
33
try {
34
- const { opts , id } = await client . drive . get ( )
35
- t . true ( opts . key )
36
- t . same ( id , 1 )
34
+ const drive = await client . drive . get ( )
35
+ t . true ( drive . key )
36
+ t . same ( drive . id , 1 )
37
37
38
- await client . drive . writeFile ( id , 'hello' , content )
38
+ await drive . writeFile ( 'hello' , content )
39
39
40
- const contents = await client . drive . readFile ( id , 'hello' )
40
+ const contents = await drive . readFile ( 'hello' )
41
41
t . same ( contents , content )
42
42
43
- await client . drive . close ( id )
43
+ await drive . close ( )
44
44
} catch ( err ) {
45
45
t . fail ( err )
46
46
}
@@ -53,11 +53,11 @@ test('can write/read a file from a remote hyperdrive using stream methods', asyn
53
53
const { client, cleanup } = await createOne ( )
54
54
55
55
try {
56
- const { opts , id } = await client . drive . get ( )
57
- t . true ( opts . key )
58
- t . same ( id , 1 )
56
+ const drive = await client . drive . get ( )
57
+ t . true ( drive . key )
58
+ t . same ( drive . id , 1 )
59
59
60
- const writeStream = client . drive . createWriteStream ( id , 'hello' , { uid : 999 , gid : 999 } )
60
+ const writeStream = drive . createWriteStream ( 'hello' , { uid : 999 , gid : 999 } )
61
61
writeStream . write ( 'hello' )
62
62
writeStream . write ( 'there' )
63
63
writeStream . end ( 'friend' )
@@ -67,7 +67,7 @@ test('can write/read a file from a remote hyperdrive using stream methods', asyn
67
67
writeStream . on ( 'finish' , resolve )
68
68
} )
69
69
70
- const readStream = await client . drive . createReadStream ( id , 'hello' , { start : 5 , length : Buffer . from ( 'there' ) . length + 1 } )
70
+ const readStream = await drive . createReadStream ( 'hello' , { start : 5 , length : Buffer . from ( 'there' ) . length + 1 } )
71
71
const content = await new Promise ( ( resolve , reject ) => {
72
72
collectStream ( readStream , ( err , bufs ) => {
73
73
if ( err ) return reject ( err )
@@ -76,11 +76,11 @@ test('can write/read a file from a remote hyperdrive using stream methods', asyn
76
76
} )
77
77
t . same ( content , Buffer . from ( 'theref' ) )
78
78
79
- const stat = await client . drive . stat ( id , 'hello' )
79
+ const stat = await drive . stat ( 'hello' )
80
80
t . same ( stat . uid , 999 )
81
81
t . same ( stat . gid , 999 )
82
82
83
- await client . drive . close ( id )
83
+ await drive . close ( )
84
84
} catch ( err ) {
85
85
t . fail ( err )
86
86
}
@@ -93,18 +93,16 @@ test('can stat a file from a remote hyperdrive', async t => {
93
93
const { client, cleanup } = await createOne ( )
94
94
95
95
try {
96
- const { opts, id } = await client . drive . get ( )
97
- t . true ( opts . key )
98
- t . same ( id , 1 )
96
+ const drive = await client . drive . get ( )
99
97
100
- await client . drive . writeFile ( id , 'hello' , 'world' )
98
+ await drive . writeFile ( 'hello' , 'world' )
101
99
102
- const stat = await client . drive . stat ( id , 'hello' )
100
+ const stat = await drive . stat ( 'hello' )
103
101
t . same ( stat . size , Buffer . from ( 'world' ) . length )
104
102
t . same ( stat . uid , 0 )
105
103
t . same ( stat . gid , 0 )
106
104
107
- await client . drive . close ( id )
105
+ await drive . close ( )
108
106
} catch ( err ) {
109
107
t . fail ( err )
110
108
}
@@ -117,22 +115,20 @@ test('can list a directory from a remote hyperdrive', async t => {
117
115
const { client, cleanup } = await createOne ( )
118
116
119
117
try {
120
- const { opts, id } = await client . drive . get ( )
121
- t . true ( opts . key )
122
- t . same ( id , 1 )
118
+ const drive = await client . drive . get ( )
123
119
124
- await client . drive . writeFile ( id , 'hello' , 'world' )
125
- await client . drive . writeFile ( id , 'goodbye' , 'dog' )
126
- await client . drive . writeFile ( id , 'adios' , 'amigo' )
120
+ await drive . writeFile ( 'hello' , 'world' )
121
+ await drive . writeFile ( 'goodbye' , 'dog' )
122
+ await drive . writeFile ( 'adios' , 'amigo' )
127
123
128
- const files = await client . drive . readdir ( id , '' )
124
+ const files = await drive . readdir ( '' )
129
125
t . same ( files . length , 4 )
130
126
t . notEqual ( files . indexOf ( 'hello' ) , - 1 )
131
127
t . notEqual ( files . indexOf ( 'goodbye' ) , - 1 )
132
128
t . notEqual ( files . indexOf ( 'adios' ) , - 1 )
133
129
t . notEqual ( files . indexOf ( '.key' ) , - 1 )
134
130
135
- await client . drive . close ( id )
131
+ await drive . close ( )
136
132
} catch ( err ) {
137
133
t . fail ( err )
138
134
}
@@ -151,21 +147,23 @@ test('can read/write multiple remote hyperdrives on one server', async t => {
151
147
[ 'random' , 'file' ]
152
148
]
153
149
150
+ var drives = [ ]
154
151
for ( const [ file , content ] of files ) {
155
- await createAndWrite ( file , content )
152
+ drives . push ( await createAndWrite ( file , content ) )
156
153
}
157
154
158
- for ( let i = 1 ; i < files . length + 1 ; i ++ ) {
159
- const [ file , content ] = files [ i - 1 ]
160
- const readContent = await client . drive . readFile ( i , file )
155
+ for ( let i = 0 ; i < files . length ; i ++ ) {
156
+ const [ file , content ] = files [ i ]
157
+ const drive = drives [ i ]
158
+ const readContent = await drive . readFile ( file )
161
159
t . same ( readContent , Buffer . from ( content ) )
162
160
}
163
161
164
162
async function createAndWrite ( file , content ) {
165
- const { opts , id } = await client . drive . get ( )
166
- t . true ( opts . key )
167
- t . same ( id , startingId ++ )
168
- await client . drive . writeFile ( id , file , content )
163
+ const drive = await client . drive . get ( )
164
+ t . same ( drive . id , startingId ++ )
165
+ await drive . writeFile ( file , content )
166
+ return drive
169
167
}
170
168
171
169
await cleanup ( )
@@ -176,31 +174,25 @@ test('can mount a drive within a remote hyperdrive', async t => {
176
174
const { client, cleanup } = await createOne ( )
177
175
178
176
try {
179
- const { opts : opts1 , id : id1 } = await client . drive . get ( )
180
- t . true ( opts1 . key )
181
- t . same ( id1 , 1 )
177
+ const drive1 = await client . drive . get ( )
182
178
183
- const { opts : opts2 , id : id2 } = await client . drive . get ( )
184
- t . true ( opts2 . key )
185
- t . same ( id2 , 2 )
186
- t . notEqual ( opts1 . key , opts2 . key )
179
+ const drive2 = await client . drive . get ( )
180
+ t . notEqual ( drive1 . key , drive2 . key )
187
181
188
- const noVersion = { ... opts2 , version : null }
182
+ await drive1 . mount ( 'a' , { key : drive2 . key } )
189
183
190
- await client . drive . mount ( id1 , 'a' , noVersion )
184
+ await drive1 . writeFile ( 'a/hello' , 'world' )
185
+ await drive1 . writeFile ( 'a/goodbye' , 'dog' )
186
+ await drive1 . writeFile ( 'adios' , 'amigo' )
187
+ await drive2 . writeFile ( 'hamster' , 'wheel' )
191
188
192
- await client . drive . writeFile ( id1 , 'a/hello' , 'world' )
193
- await client . drive . writeFile ( id1 , 'a/goodbye' , 'dog' )
194
- await client . drive . writeFile ( id1 , 'adios' , 'amigo' )
195
- await client . drive . writeFile ( id2 , 'hamster' , 'wheel' )
189
+ t . same ( await drive1 . readFile ( 'adios' ) , Buffer . from ( 'amigo' ) )
190
+ t . same ( await drive1 . readFile ( 'a/hello' ) , Buffer . from ( 'world' ) )
191
+ t . same ( await drive2 . readFile ( 'hello' ) , Buffer . from ( 'world' ) )
192
+ t . same ( await drive2 . readFile ( 'hamster' ) , Buffer . from ( 'wheel' ) )
196
193
197
- t . same ( await client . drive . readFile ( id1 , 'adios' ) , Buffer . from ( 'amigo' ) )
198
- t . same ( await client . drive . readFile ( id1 , 'a/hello' ) , Buffer . from ( 'world' ) )
199
- t . same ( await client . drive . readFile ( id2 , 'hello' ) , Buffer . from ( 'world' ) )
200
- t . same ( await client . drive . readFile ( id2 , 'hamster' ) , Buffer . from ( 'wheel' ) )
201
-
202
- await client . drive . close ( id1 )
203
- await client . drive . close ( id2 )
194
+ await drive1 . close ( )
195
+ await drive2 . close ( )
204
196
} catch ( err ) {
205
197
t . fail ( err )
206
198
}
@@ -213,39 +205,32 @@ test('can unmount a drive within a remote hyperdrive', async t => {
213
205
const { client, cleanup } = await createOne ( )
214
206
215
207
try {
216
- const { opts : opts1 , id : id1 } = await client . drive . get ( )
217
- t . true ( opts1 . key )
218
- t . same ( id1 , 1 )
219
-
220
- const { opts : opts2 , id : id2 } = await client . drive . get ( )
221
- t . true ( opts2 . key )
222
- t . same ( id2 , 2 )
223
- t . notEqual ( opts1 . key , opts2 . key )
208
+ const drive1 = await client . drive . get ( )
209
+ const drive2 = await client . drive . get ( )
210
+ t . notEqual ( drive1 . key , drive2 . key )
224
211
225
- const noVersion = { ... opts2 , version : null }
212
+ await drive1 . mount ( 'a' , { key : drive2 . key } )
226
213
227
- await client . drive . mount ( id1 , 'a' , noVersion )
214
+ await drive1 . writeFile ( 'a/hello' , 'world' )
215
+ await drive1 . writeFile ( 'a/goodbye' , 'dog' )
216
+ await drive1 . writeFile ( 'adios' , 'amigo' )
217
+ await drive2 . writeFile ( 'hamster' , 'wheel' )
228
218
229
- await client . drive . writeFile ( id1 , 'a/hello' , 'world' )
230
- await client . drive . writeFile ( id1 , 'a/goodbye' , 'dog' )
231
- await client . drive . writeFile ( id1 , 'adios' , 'amigo' )
232
- await client . drive . writeFile ( id2 , 'hamster' , 'wheel' )
219
+ t . same ( await drive1 . readFile ( 'adios' ) , Buffer . from ( 'amigo' ) )
220
+ t . same ( await drive1 . readFile ( 'a/hello' ) , Buffer . from ( 'world' ) )
221
+ t . same ( await drive2 . readFile ( 'hello' ) , Buffer . from ( 'world' ) )
222
+ t . same ( await drive2 . readFile ( 'hamster' ) , Buffer . from ( 'wheel' ) )
233
223
234
- t . same ( await client . drive . readFile ( id1 , 'adios' ) , Buffer . from ( 'amigo' ) )
235
- t . same ( await client . drive . readFile ( id1 , 'a/hello' ) , Buffer . from ( 'world' ) )
236
- t . same ( await client . drive . readFile ( id2 , 'hello' ) , Buffer . from ( 'world' ) )
237
- t . same ( await client . drive . readFile ( id2 , 'hamster' ) , Buffer . from ( 'wheel' ) )
238
-
239
- await client . drive . unmount ( id1 , 'a' )
224
+ await drive1 . unmount ( 'a' )
240
225
try {
241
- await client . drive . readFile ( id1 , 'a/hello' )
226
+ await drive1 . readFile ( 'a/hello' )
242
227
} catch ( err ) {
243
228
t . true ( err )
244
229
t . same ( err . code , 2 )
245
230
}
246
231
247
- await client . drive . close ( id1 )
248
- await client . drive . close ( id2 )
232
+ await drive1 . close ( )
233
+ await drive2 . close ( )
249
234
} catch ( err ) {
250
235
t . fail ( err )
251
236
}
@@ -260,17 +245,17 @@ test('can watch a remote hyperdrive', async t => {
260
245
var triggered = 0
261
246
262
247
try {
263
- const { id } = await client . drive . get ( )
248
+ const drive = await client . drive . get ( )
264
249
265
- const unwatch = client . drive . watch ( id , '' , ( ) => {
250
+ const unwatch = drive . watch ( '' , ( ) => {
266
251
triggered ++
267
252
} )
268
253
269
- await client . drive . writeFile ( id , 'hello' , 'world' )
254
+ await drive . writeFile ( 'hello' , 'world' )
270
255
await unwatch ( )
271
- await client . drive . writeFile ( id , 'world' , 'hello' )
256
+ await drive . writeFile ( 'world' , 'hello' )
272
257
273
- await client . drive . close ( id )
258
+ await drive . close ( )
274
259
} catch ( err ) {
275
260
t . fail ( err )
276
261
}
0 commit comments