This repository was 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
/
Copy pathls.js
228 lines (179 loc) · 6.71 KB
/
ls.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* eslint-env mocha */
'use strict'
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { CID } = require('multiformats/cid')
const createShardedDirectory = require('../utils/create-sharded-directory')
const all = require('it-all')
const { randomBytes } = require('iso-random-stream')
const raw = require('multiformats/codecs/raw')
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {Object} options
*/
module.exports = (factory, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const largeFile = randomBytes(490668)
describe('.files.ls', function () {
this.timeout(120 * 1000)
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async () => { ipfs = (await factory.spawn()).api })
after(() => factory.clean())
it('should require a path', () => {
// @ts-expect-error invalid args
expect(all(ipfs.files.ls())).to.eventually.be.rejected()
})
it('lists the root directory', async () => {
const fileName = `small-file-${Math.random()}.txt`
const content = uint8ArrayFromString('Hello world')
await ipfs.files.write(`/${fileName}`, content, {
create: true
})
const files = await all(ipfs.files.ls('/'))
expect(files).to.have.lengthOf(1).and.to.containSubset([{
cid: CID.parse('Qmetpc7cZmN25Wcc6R27cGCAvCDqCS5GjHG4v7xABEfpmJ'),
name: fileName,
size: content.length,
type: 'file'
}])
})
it('refuses to lists files with an empty path', async () => {
await expect(all(ipfs.files.ls(''))).to.eventually.be.rejected()
})
it('refuses to lists files with an invalid path', async () => {
await expect(all(ipfs.files.ls('not-valid'))).to.eventually.be.rejected()
})
it('lists files in a directory', async () => {
const dirName = `dir-${Math.random()}`
const fileName = `small-file-${Math.random()}.txt`
const content = uint8ArrayFromString('Hello world')
await ipfs.files.write(`/${dirName}/${fileName}`, content, {
create: true,
parents: true
})
const files = await all(ipfs.files.ls(`/${dirName}`))
expect(files).to.have.lengthOf(1).and.to.containSubset([{
cid: CID.parse('Qmetpc7cZmN25Wcc6R27cGCAvCDqCS5GjHG4v7xABEfpmJ'),
name: fileName,
size: content.length,
type: 'file'
}])
})
it('lists a file', async () => {
const fileName = `small-file-${Math.random()}.txt`
const content = uint8ArrayFromString('Hello world')
await ipfs.files.write(`/${fileName}`, content, {
create: true
})
const files = await all(ipfs.files.ls(`/${fileName}`))
expect(files).to.have.lengthOf(1).and.to.containSubset([{
cid: CID.parse('Qmetpc7cZmN25Wcc6R27cGCAvCDqCS5GjHG4v7xABEfpmJ'),
name: fileName,
size: content.length,
type: 'file'
}])
})
it('fails to list non-existent file', async () => {
await expect(all(ipfs.files.ls('/i-do-not-exist'))).to.eventually.be.rejected()
})
it('lists a raw node', async () => {
const filePath = '/stat/large-file.txt'
await ipfs.files.write(filePath, largeFile, {
create: true,
parents: true,
rawLeaves: true
})
const stats = await ipfs.files.stat(filePath)
const { value: node } = await ipfs.dag.get(stats.cid)
expect(node).to.have.nested.property('Links[0].Hash.code', raw.code)
const child = node.Links[0]
const files = await all(ipfs.files.ls(`/ipfs/${child.Hash}`))
expect(files).to.have.lengthOf(1).and.to.containSubset([{
cid: child.Hash,
name: child.Hash.toString(),
size: 262144,
type: 'file'
}])
})
it('lists a raw node in an mfs directory', async () => {
const filePath = '/stat/large-file.txt'
await ipfs.files.write(filePath, largeFile, {
create: true,
parents: true,
rawLeaves: true
})
const stats = await ipfs.files.stat(filePath)
const cid = stats.cid
const { value: node } = await ipfs.dag.get(cid)
expect(node).to.have.nested.property('Links[0].Hash.code', raw.code)
const child = node.Links[0]
const dir = `/dir-with-raw-${Math.random()}`
const path = `${dir}/raw-${Math.random()}`
await ipfs.files.mkdir(dir)
await ipfs.files.cp(`/ipfs/${child.Hash}`, path)
const files = await all(ipfs.files.ls(`/ipfs/${child.Hash}`))
expect(files).to.have.lengthOf(1).and.to.containSubset([{
cid: child.Hash,
name: child.Hash.toString(),
size: 262144,
type: 'file'
}])
})
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
Experimental: {
ShardingEnabled: true
}
}
}
})
ipfs = ipfsd.api
})
it('lists a sharded directory contents', async () => {
const fileCount = 1001
const dirPath = await createShardedDirectory(ipfs, fileCount)
const files = await all(ipfs.files.ls(dirPath))
expect(files.length).to.equal(fileCount)
files.forEach(file => {
// should be a file
expect(file.type).to.equal('file')
})
})
it('lists a file inside a sharded directory directly', async () => {
const dirPath = await createShardedDirectory(ipfs)
const files = await all(ipfs.files.ls(dirPath))
const filePath = `${dirPath}/${files[0].name}`
// should be able to ls new file directly
const file = await all(ipfs.files.ls(filePath))
expect(file).to.have.lengthOf(1).and.to.containSubset([files[0]])
})
it('lists the contents of a directory inside a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const dirPath = `${shardedDirPath}/subdir-${Math.random()}`
const fileName = `small-file-${Math.random()}.txt`
await ipfs.files.mkdir(`${dirPath}`)
await ipfs.files.write(`${dirPath}/${fileName}`, Uint8Array.from([0, 1, 2, 3]), {
create: true
})
const files = await all(ipfs.files.ls(dirPath))
expect(files.length).to.equal(1)
expect(files.filter(file => file.name === fileName)).to.be.ok()
})
})
})
}