Skip to content

Commit 570dae3

Browse files
committed
fix: add cross-impl shard test, fix path cleaner
Ref: ipfs/go-unixfsnode#66
1 parent 96c2af5 commit 570dae3

File tree

2 files changed

+76
-32
lines changed

2 files changed

+76
-32
lines changed

packages/ipfs-unixfs-exporter/test/exporter-sharded.spec.ts

+75-28
Original file line numberDiff line numberDiff line change
@@ -256,40 +256,87 @@ describe('exporter sharded', function () {
256256
expect(exported.name).to.deep.equal('file-1')
257257
})
258258

259-
it('exports a shard with a different fanout size', async () => {
260-
const files: ImportCandidate[] = [{
261-
path: '/baz.txt',
262-
content: Uint8Array.from([0, 1, 2, 3, 4])
263-
}, {
264-
path: '/foo.txt',
265-
content: Uint8Array.from([0, 1, 2, 3, 4])
266-
}, {
267-
path: '/bar.txt',
268-
content: Uint8Array.from([0, 1, 2, 3, 4])
269-
}]
259+
describe('alternate fanout size', function () {
260+
it('exports a shard with a fanout of 16', async () => {
261+
const files: ImportCandidate[] = [{
262+
path: '/baz.txt',
263+
content: Uint8Array.from([0, 1, 2, 3, 4])
264+
}, {
265+
path: '/foo.txt',
266+
content: Uint8Array.from([0, 1, 2, 3, 4])
267+
}, {
268+
path: '/bar.txt',
269+
content: Uint8Array.from([0, 1, 2, 3, 4])
270+
}]
270271

271-
const result = await last(importer(files, block, {
272-
shardSplitThresholdBytes: 0,
273-
shardFanoutBits: 4, // 2**4 = 16 children max
274-
wrapWithDirectory: true
275-
}))
272+
const result = await last(importer(files, block, {
273+
shardSplitThresholdBytes: 0,
274+
shardFanoutBits: 4, // 2**4 = 16 children max
275+
wrapWithDirectory: true
276+
}))
276277

277-
if (result == null) {
278-
throw new Error('Import failed')
279-
}
278+
if (result == null) {
279+
throw new Error('Import failed')
280+
}
280281

281-
const { cid } = result
282-
const dir = await exporter(cid, block)
282+
const { cid } = result
283+
const dir = await exporter(cid, block)
284+
285+
expect(dir).to.have.nested.property('unixfs.fanout', 16n)
286+
287+
const contents = await all(dir.content())
283288

284-
expect(dir).to.have.nested.property('unixfs.fanout', 16n)
289+
expect(contents.map(entry => ({
290+
path: `/${entry.name}`,
291+
content: entry.node
292+
})))
293+
.to.deep.equal(files)
294+
})
295+
296+
// Cross-impl reference test: directory of files with single character
297+
// names, starting from ' ' and ending with '~', but excluding the special
298+
// characters '/' and '.'. Each file should contain a single byte with the
299+
// same value as the character in its name. Files are added to a sharded
300+
// directory with a fanout of 16, using CIDv1 throughout, and should result
301+
// in the root CID of:
302+
// bafybeihnipspiyy3dctpcx7lv655qpiuy52d7b2fzs52dtrjqwmvbiux44
303+
it.only('reference shard with fanout of 16', async () => {
304+
const files: ImportCandidate[] = []
305+
for (let ch = ' '.charCodeAt(0); ch <= '~'.charCodeAt(0); ch++) {
306+
if (ch == 47 || ch == 46) { // skip '/' and '.'
307+
continue
308+
}
309+
files.push({
310+
path: String.fromCharCode(ch),
311+
content: Uint8Array.from([ch])
312+
})
313+
}
285314

286-
const contents = await all(dir.content())
315+
const result = await last(importer(files, block, {
316+
shardSplitThresholdBytes: 0,
317+
shardFanoutBits: 4,
318+
wrapWithDirectory: true
319+
}))
287320

288-
expect(contents.map(entry => ({
289-
path: `/${entry.name}`,
290-
content: entry.node
291-
})))
292-
.to.deep.equal(files)
321+
if (result == null) {
322+
throw new Error('Import failed')
323+
}
324+
325+
const { cid } = result
326+
expect(cid.toString()).to.equal('bafybeihnipspiyy3dctpcx7lv655qpiuy52d7b2fzs52dtrjqwmvbiux44')
327+
328+
const dir = await exporter(cid, block)
329+
330+
expect(dir).to.have.nested.property('unixfs.fanout', 16n)
331+
332+
let contents = await all(dir.content())
333+
contents = contents.map(entry => ({
334+
path: `${entry.name}`,
335+
content: entry.node
336+
}))
337+
contents.sort((a, b) => a.content[0] < b.content[0] ? -1 : 1)
338+
expect(contents).to.deep.equal(files)
339+
})
293340
})
294341

295342
it('walks path of a HAMT with a different fanout size', async () => {
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
export const toPathComponents = (path: string = ''): string[] => {
22
// split on / unless escaped with \
3-
return (path
4-
.trim()
5-
.match(/([^\\/]|\\\/)+/g) ?? [])
6-
.filter(Boolean)
3+
return path.split(/(?<!\\)\//).filter(Boolean)
74
}

0 commit comments

Comments
 (0)