forked from RangerMauve/hypercore-fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
106 lines (72 loc) · 2.4 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
const storage = require('random-access-memory')
const SDK = require('dat-sdk')
async function test () {
const { Hyperdrive, resolveName, close } = await SDK({
storage
})
const archive = Hyperdrive('dat fetch test')
const FILE_LOCATION = '/index.html'
await archive.writeFile(FILE_LOCATION, '<h1>Hello World!</h1>')
const fetch = require('./')({
Hyperdrive,
resolveName,
writable: true
})
const url = `dat://${archive.key.toString('hex')}${FILE_LOCATION}`
console.log('Fetching from', url)
const response = await fetch(url)
const text = await response.text()
const contentType = response.headers.get('content-type')
console.log(contentType)
console.log(text)
console.log([...response.headers.entries()])
const url2 = 'hyper://example/example.txt'
const contents = 'Hello World'
console.log('Putting into', url2, contents)
await checkOK(await fetch(url2, { method: 'PUT', body: contents }))
console.log('Wrote to archive')
const response2 = await fetch(url2)
await checkOK(response2)
const text2 = await response2.text()
console.log('Read written data')
console.log(text2)
const url3 = 'hyper://example/'
const response3 = await fetch(url3, { headers: { Accept: 'application/json' } })
await checkOK(response3)
const text3 = await response3.text()
console.log('Directory listing after write')
console.log(text3)
await checkOK(await fetch(url2, { method: 'DELETE' }))
console.log('Deleted file')
const response4 = await fetch(url3)
await checkOK(response4)
const text4 = await response4.text()
console.log('Directory after delete')
console.log(text4)
const url4 = 'hyper://example/.well-known/dat'
const response5 = await fetch(url4)
await checkOK(response5)
const json = await response5.text()
console.log('Archive well-known URL', json)
const url5 = 'hyper://example/foo/bar/'
await checkOK(await fetch(url5, { method: 'PUT' }))
console.log('Created multiple folders')
const url6 = 'hyper://example/fizz/buzz/example.txt'
await checkOK(await fetch(url6, { method: 'PUT', contents }))
console.log('Created file along with parent folders')
await close()
}
test().then(() => {
process.exit(0)
}, (e) => {
process.nextTick(() => {
throw e
})
})
async function checkOK (response) {
if (!response.ok) {
const message = await response.text()
throw new Error(message)
}
return response
}