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 pathmkdir.js
262 lines (206 loc) · 7.51 KB
/
mkdir.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* eslint-env mocha */
import { nanoid } from 'nanoid'
import { expect } from 'aegir/utils/chai.js'
import { getDescribe, getIt } from '../utils/mocha.js'
import { sha512 } from 'multiformats/hashes/sha2'
import { createShardedDirectory } from '../utils/create-sharded-directory.js'
import all from 'it-all'
import isShardAtPath from '../utils/is-shard-at-path.js'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {Object} options
*/
export function testMkdir (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.files.mkdir', function () {
this.timeout(120 * 1000)
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
/**
* @param {number | string | undefined} mode
* @param {number} expectedMode
*/
async function testMode (mode, expectedMode) {
const testPath = `/test-${nanoid()}`
await ipfs.files.mkdir(testPath, {
mode
})
const stats = await ipfs.files.stat(testPath)
expect(stats).to.have.property('mode', expectedMode)
}
/**
* @param {import('ipfs-unixfs').MtimeLike} mtime
* @param {import('ipfs-unixfs').MtimeLike} expectedMtime
*/
async function testMtime (mtime, expectedMtime) {
const testPath = `/test-${nanoid()}`
await ipfs.files.mkdir(testPath, {
mtime
})
const stats = await ipfs.files.stat(testPath)
expect(stats).to.have.deep.property('mtime', expectedMtime)
}
before(async () => { ipfs = (await factory.spawn()).api })
after(() => factory.clean())
it('requires a directory', async () => {
await expect(ipfs.files.mkdir('')).to.eventually.be.rejected()
})
it('refuses to create a directory without a leading slash', async () => {
await expect(ipfs.files.mkdir('foo')).to.eventually.be.rejected()
})
it('refuses to recreate the root directory when -p is false', async () => {
await expect(ipfs.files.mkdir('/', {
parents: false
})).to.eventually.be.rejected()
})
it('refuses to create a nested directory when -p is false', async () => {
await expect(ipfs.files.mkdir('/foo/bar/baz', {
parents: false
})).to.eventually.be.rejected()
})
it('creates a directory', async () => {
const path = '/foo'
await ipfs.files.mkdir(path, {})
const stats = await ipfs.files.stat(path)
expect(stats.type).to.equal('directory')
const files = await all(ipfs.files.ls(path))
expect(files.length).to.equal(0)
})
it('refuses to create a directory that already exists', async () => {
const path = '/qux/quux/quuux'
await ipfs.files.mkdir(path, {
parents: true
})
await expect(ipfs.files.mkdir(path, {
parents: false
})).to.eventually.be.rejected()
})
it('does not error when creating a directory that already exists and parents is true', async () => {
const path = '/qux/quux/quuux'
await ipfs.files.mkdir(path, {
parents: true
})
await ipfs.files.mkdir(path, {
parents: true
})
})
it('creates a nested directory when -p is true', async () => {
const path = '/foo/bar/baz'
await ipfs.files.mkdir(path, {
parents: true
})
const files = await all(ipfs.files.ls(path))
expect(files.length).to.equal(0)
})
it('creates nested directories', async () => {
await ipfs.files.mkdir('/nested-dir')
await ipfs.files.mkdir('/nested-dir/baz')
const files = await all(ipfs.files.ls('/nested-dir'))
expect(files.length).to.equal(1)
})
it('creates a nested directory with a different CID version to the parent', async () => {
const directory = `cid-versions-${Math.random()}`
const directoryPath = `/${directory}`
const subDirectory = `cid-versions-${Math.random()}`
const subDirectoryPath = `${directoryPath}/${subDirectory}`
await ipfs.files.mkdir(directoryPath, {
cidVersion: 0
})
await expect(ipfs.files.stat(directoryPath)).to.eventually.have.nested.property('cid.version', 0)
await ipfs.files.mkdir(subDirectoryPath, {
cidVersion: 1
})
await expect(ipfs.files.stat(subDirectoryPath)).to.eventually.have.nested.property('cid.version', 1)
})
it('creates a nested directory with a different hash function to the parent', async () => {
const directory = `cid-versions-${Math.random()}`
const directoryPath = `/${directory}`
const subDirectory = `cid-versions-${Math.random()}`
const subDirectoryPath = `${directoryPath}/${subDirectory}`
await ipfs.files.mkdir(directoryPath, {
cidVersion: 0
})
await expect(ipfs.files.stat(directoryPath)).to.eventually.have.nested.property('cid.version', 0)
await ipfs.files.mkdir(subDirectoryPath, {
cidVersion: 1,
hashAlg: 'sha2-512'
})
await expect(ipfs.files.stat(subDirectoryPath)).to.eventually.have.nested.property('cid.multihash.code', sha512.code)
})
it('should make directory and have default mode', async function () {
await testMode(undefined, parseInt('0755', 8))
})
it('should make directory and specify mode as string', async function () {
const mode = '0321'
await testMode(mode, parseInt(mode, 8))
})
it('should make directory and specify mode as number', async function () {
const mode = parseInt('0321', 8)
await testMode(mode, mode)
})
it('should make directory and specify mtime as Date', async function () {
const mtime = new Date(5000)
await testMtime(mtime, {
secs: 5,
nsecs: 0
})
})
it('should make directory and specify mtime as { nsecs, secs }', async function () {
const mtime = {
secs: 5,
nsecs: 0
}
await testMtime(mtime, mtime)
})
it('should make directory and specify mtime as timespec', async function () {
await testMtime({
Seconds: 5,
FractionalNanoseconds: 0
}, {
secs: 5,
nsecs: 0
})
})
it('should make directory and specify mtime as hrtime', async function () {
const mtime = process.hrtime()
await testMtime(mtime, {
secs: mtime[0],
nsecs: mtime[1]
})
})
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('makes a directory inside a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const dirPath = `${shardedDirPath}/subdir-${Math.random()}`
await ipfs.files.mkdir(`${dirPath}`)
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
await expect(ipfs.files.stat(shardedDirPath)).to.eventually.have.property('type', 'directory')
await expect(isShardAtPath(dirPath, ipfs)).to.eventually.be.false()
await expect(ipfs.files.stat(dirPath)).to.eventually.have.property('type', 'directory')
})
})
})
}