This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
read.js
161 lines (126 loc) · 4.53 KB
/
read.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
/* eslint-env mocha */
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
import drain from 'it-drain'
import all from 'it-all'
import { fixtures } from '../utils/index.js'
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
import { createShardedDirectory } from '../utils/create-sharded-directory.js'
import { randomBytes } from 'iso-random-stream'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {object} options
*/
export function testRead (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
const smallFile = randomBytes(13)
describe('.files.read', function () {
this.timeout(120 * 1000)
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async () => { ipfs = (await factory.spawn()).api })
after(() => factory.clean())
it('reads a small file', async () => {
const filePath = '/small-file.txt'
await ipfs.files.write(filePath, smallFile, {
create: true
})
const bytes = uint8ArrayConcat(await all(ipfs.files.read(filePath)))
expect(bytes).to.deep.equal(smallFile)
})
it('reads a file with an offset', async () => {
const path = `/some-file-${Math.random()}.txt`
const data = randomBytes(100)
const offset = 10
await ipfs.files.write(path, data, {
create: true
})
const bytes = uint8ArrayConcat(await all(ipfs.files.read(path, {
offset
})))
expect(bytes).to.deep.equal(data.slice(offset))
})
it('reads a file with a length', async () => {
const path = `/some-file-${Math.random()}.txt`
const data = randomBytes(100)
const length = 10
await ipfs.files.write(path, data, {
create: true
})
const bytes = uint8ArrayConcat(await all(ipfs.files.read(path, {
length
})))
expect(bytes).to.deep.equal(data.slice(0, length))
})
it('reads a file with an offset and a length', async () => {
const path = `/some-file-${Math.random()}.txt`
const data = randomBytes(100)
const offset = 10
const length = 10
await ipfs.files.write(path, data, {
create: true
})
const buffer = uint8ArrayConcat(await all(ipfs.files.read(path, {
offset,
length
})))
expect(buffer).to.deep.equal(data.slice(offset, offset + length))
})
it('refuses to read a directory', async () => {
const path = '/'
await expect(drain(ipfs.files.read(path))).to.eventually.be.rejectedWith(/not a file/)
})
it('refuses to read a non-existent file', async () => {
const path = `/file-${Math.random()}.txt`
await expect(drain(ipfs.files.read(path))).to.eventually.be.rejectedWith(/does not exist/)
})
it('should read from outside of mfs', async () => {
const { cid } = await ipfs.add(fixtures.smallFile.data)
const testFileData = uint8ArrayConcat(await all(ipfs.files.read(`/ipfs/${cid}`)))
expect(testFileData).to.eql(fixtures.smallFile.data)
})
it('should be able to read rawLeaves files', async () => {
const { cid } = await ipfs.add(fixtures.smallFile.data, {
rawLeaves: true
})
await ipfs.files.cp(`/ipfs/${cid}`, '/raw-leaves.txt')
const testFileData = uint8ArrayConcat(await all(ipfs.files.read('/raw-leaves.txt')))
expect(testFileData).to.eql(fixtures.smallFile.data)
})
describe('with sharding', () => {
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async function () {
const ipfsd = await factory.spawn({
ipfsOptions: {
EXPERIMENTAL: {
// enable sharding for js
sharding: true
},
config: {
// enable sharding for go with automatic threshold dropped to the minimum so it shards everything
Internal: {
UnixFSShardingSizeThreshold: '1B'
}
}
}
})
ipfs = ipfsd.api
})
it('reads file from inside a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const filePath = `${shardedDirPath}/file-${Math.random()}.txt`
const content = Uint8Array.from([0, 1, 2, 3, 4])
await ipfs.files.write(filePath, content, {
create: true
})
const bytes = uint8ArrayConcat(await all(ipfs.files.read(filePath)))
expect(bytes).to.deep.equal(content)
})
})
})
}