forked from holepunchto/hypercore-blob-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblobs.js
113 lines (76 loc) · 3.07 KB
/
blobs.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
const test = require('brittle')
const RAM = require('random-access-memory')
const Corestore = require('corestore')
const { testBlobServer, request, testHyperblobs } = require('./helpers')
test('can get blob from hypercore', async function (t) {
const store = new Corestore(RAM)
const blobs = testHyperblobs(t, store)
const id = await blobs.put(Buffer.from('Hello World'))
const server = testBlobServer(t, store)
await server.listen()
const res = await request(server, blobs.core.key, { blob: id })
t.is(res.status, 200)
t.is(res.data, 'Hello World')
})
test('can get blob from hypercore - multiple blocks', async function (t) {
const store = new Corestore(RAM)
const blobs = testHyperblobs(t, store)
blobs.blockSize = 4 // force multiple blocks
const id = await blobs.put(Buffer.from('Hello World'))
t.is(id.blockLength, 3) // 3 blocks
const server = testBlobServer(t, store)
await server.listen()
const res = await request(server, blobs.core.key, { blob: id })
t.is(res.status, 200)
t.is(res.data, 'Hello World')
})
test('can get a partial blob from hypercore', async function (t) {
const store = new Corestore(RAM)
const blobs = testHyperblobs(t, store)
const id = await blobs.put(Buffer.from('Hello World'))
const server = testBlobServer(t, store)
await server.listen()
const res = await request(server, blobs.core.key, { blob: id, range: 'bytes=3-7' })
t.is(res.status, 206)
t.is(res.data, 'lo Wo')
})
test('can get a partial blob from hypercore, but request the whole data', async function (t) {
const store = new Corestore(RAM)
const blobs = testHyperblobs(t, store)
const id = await blobs.put(Buffer.from('Hello World'))
const server = testBlobServer(t, store)
await server.listen()
const res = await request(server, blobs.core.key, { blob: id, range: 'bytes=0-10' })
t.is(res.status, 206)
t.is(res.data, 'Hello World')
})
test('handle out of range header end', async function (t) {
const store = new Corestore(RAM)
const blobs = testHyperblobs(t, store)
const id = await blobs.put(Buffer.from('Hello World'))
const server = testBlobServer(t, store)
await server.listen()
const res = await request(server, blobs.core.key, { blob: id, range: 'bytes=0-20' })
t.is(res.status, 206)
t.is(res.data, 'Hello World')
})
test('handle range header without end', async function (t) {
const store = new Corestore(RAM)
const blobs = testHyperblobs(t, store)
const id = await blobs.put(Buffer.from('Hello World'))
const server = testBlobServer(t, store)
await server.listen()
const res = await request(server, blobs.core.key, { blob: id, range: 'bytes=2-' })
t.is(res.status, 206)
t.is(res.data, 'llo World')
})
test('handle invalid range header', async function (t) {
const store = new Corestore(RAM)
const blobs = testHyperblobs(t, store)
const id = await blobs.put(Buffer.from('Hello World'))
const server = testBlobServer(t, store)
await server.listen()
const res = await request(server, blobs.core.key, { blob: id, range: 'testing' })
t.is(res.status, 200)
t.is(res.data, 'Hello World')
})