This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathls.js
180 lines (152 loc) · 5.61 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
/* eslint-env mocha */
'use strict'
const { fixtures } = require('./utils')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const CID = require('cids')
module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()
describe('.ls', function () {
this.timeout(40 * 1000)
let ipfs
before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)
common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
})
after((done) => common.teardown(done))
it('should ls with a base58 encoded CID', function (done) {
const content = (name) => ({
path: `test-folder/${name}`,
content: fixtures.directory.files[name]
})
const emptyDir = (name) => ({ path: `test-folder/${name}` })
const dirs = [
content('pp.txt'),
content('holmes.txt'),
content('jungle.txt'),
content('alice.txt'),
emptyDir('empty-folder'),
content('files/hello.txt'),
content('files/ipfs.txt'),
emptyDir('files/empty')
]
ipfs.add(dirs, (err, res) => {
expect(err).to.not.exist()
const root = res[res.length - 1]
expect(root.path).to.equal('test-folder')
expect(root.hash).to.equal(fixtures.directory.cid)
const cid = 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP'
ipfs.ls(cid, (err, files) => {
expect(err).to.not.exist()
expect(files).to.eql([
{ depth: 1,
name: 'alice.txt',
path: 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/alice.txt',
size: 11696,
hash: 'QmZyUEQVuRK3XV7L9Dk26pg6RVSgaYkiSTEdnT2kZZdwoi',
type: 'file' },
{ depth: 1,
name: 'empty-folder',
path: 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/empty-folder',
size: 4,
hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn',
type: 'dir' },
{ depth: 1,
name: 'files',
path: 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/files',
size: 183,
hash: 'QmZ25UfTqXGz9RsEJFg7HUAuBcmfx5dQZDXQd2QEZ8Kj74',
type: 'dir' },
{ depth: 1,
name: 'holmes.txt',
path: 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/holmes.txt',
size: 582072,
hash: 'QmR4nFjTu18TyANgC65ArNWp5Yaab1gPzQ4D8zp7Kx3vhr',
type: 'file' },
{ depth: 1,
name: 'jungle.txt',
path: 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/jungle.txt',
size: 2305,
hash: 'QmT6orWioMiSqXXPGsUi71CKRRUmJ8YkuueV2DPV34E9y9',
type: 'file' },
{ depth: 1,
name: 'pp.txt',
path: 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/pp.txt',
size: 4551,
hash: 'QmVwdDCY4SPGVFnNCiZnX5CtzwWDn6kAM98JXzKxE3kCmn',
type: 'file' }
])
done()
})
})
})
it('should ls files added as CIDv0 with a CIDv1', done => {
const randomName = prefix => `${prefix}${Math.round(Math.random() * 1000)}`
const dir = randomName('DIR')
const input = [
{ path: `${dir}/${randomName('F0')}`, content: Buffer.from(randomName('D0')) },
{ path: `${dir}/${randomName('F1')}`, content: Buffer.from(randomName('D1')) }
]
ipfs.add(input, { cidVersion: 0 }, (err, res) => {
expect(err).to.not.exist()
const cidv0 = new CID(res[res.length - 1].hash)
expect(cidv0.version).to.equal(0)
const cidv1 = cidv0.toV1()
ipfs.ls(cidv1, (err, output) => {
expect(err).to.not.exist()
expect(output.length).to.equal(input.length)
output.forEach(({ hash }) => {
expect(res.find(file => file.hash === hash)).to.exist()
})
done()
})
})
})
it('should ls files added as CIDv1 with a CIDv0', done => {
const randomName = prefix => `${prefix}${Math.round(Math.random() * 1000)}`
const dir = randomName('DIR')
const input = [
{ path: `${dir}/${randomName('F0')}`, content: Buffer.from(randomName('D0')) },
{ path: `${dir}/${randomName('F1')}`, content: Buffer.from(randomName('D1')) }
]
ipfs.add(input, { cidVersion: 1, rawLeaves: false }, (err, res) => {
expect(err).to.not.exist()
const cidv1 = new CID(res[res.length - 1].hash)
expect(cidv1.version).to.equal(1)
const cidv0 = cidv1.toV1()
ipfs.ls(cidv0, (err, output) => {
expect(err).to.not.exist()
expect(output.length).to.equal(input.length)
output.forEach(({ hash }) => {
expect(res.find(file => file.hash === hash)).to.exist()
})
done()
})
})
})
it('should correctly handle a non existing hash', (done) => {
ipfs.ls('surelynotavalidhashheh?', (err, res) => {
expect(err).to.exist()
expect(res).to.not.exist()
done()
})
})
it('should correctly handle a non exiting path', (done) => {
ipfs.ls('QmRNjDeKStKGTQXnJ2NFqeQ9oW/folder_that_isnt_there', (err, res) => {
expect(err).to.exist()
expect(res).to.not.exist()
done()
})
})
})
}