@helia/interop 3.0.1 → 4.0.0-e554493
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.aegir.js +46 -0
- package/README.md +11 -0
- package/dist/src/bin.d.ts +3 -0
- package/dist/src/bin.d.ts.map +1 -0
- package/dist/src/bin.js +20 -0
- package/dist/src/bin.js.map +1 -0
- package/dist/src/car.spec.d.ts +2 -0
- package/dist/src/car.spec.d.ts.map +1 -0
- package/dist/src/car.spec.js +77 -0
- package/dist/src/car.spec.js.map +1 -0
- package/dist/src/dag-cbor.spec.d.ts +2 -0
- package/dist/src/dag-cbor.spec.d.ts.map +1 -0
- package/dist/src/dag-cbor.spec.js +49 -0
- package/dist/src/dag-cbor.spec.js.map +1 -0
- package/dist/src/dag-json.spec.d.ts +2 -0
- package/dist/src/dag-json.spec.d.ts.map +1 -0
- package/dist/src/dag-json.spec.js +49 -0
- package/dist/src/dag-json.spec.js.map +1 -0
- package/dist/src/fixtures/connect.d.ts +7 -0
- package/dist/src/fixtures/connect.d.ts.map +1 -0
- package/dist/src/fixtures/connect.js +17 -0
- package/dist/src/fixtures/connect.js.map +1 -0
- package/dist/src/fixtures/create-helia-http.d.ts +4 -0
- package/dist/src/fixtures/create-helia-http.d.ts.map +1 -0
- package/dist/src/fixtures/create-helia-http.js +7 -0
- package/dist/src/fixtures/create-helia-http.js.map +1 -0
- package/dist/src/fixtures/create-helia.browser.d.ts +6 -0
- package/dist/src/fixtures/create-helia.browser.d.ts.map +1 -0
- package/dist/src/fixtures/create-helia.browser.js +56 -0
- package/dist/src/fixtures/create-helia.browser.js.map +1 -0
- package/dist/src/fixtures/create-helia.d.ts +6 -0
- package/dist/src/fixtures/create-helia.d.ts.map +1 -0
- package/dist/src/fixtures/create-helia.js +46 -0
- package/dist/src/fixtures/create-helia.js.map +1 -0
- package/dist/src/fixtures/create-kubo.browser.d.ts +3 -0
- package/dist/src/fixtures/create-kubo.browser.d.ts.map +1 -0
- package/dist/src/fixtures/create-kubo.browser.js +30 -0
- package/dist/src/fixtures/create-kubo.browser.js.map +1 -0
- package/dist/src/fixtures/create-kubo.d.ts +3 -0
- package/dist/src/fixtures/create-kubo.d.ts.map +1 -0
- package/dist/src/fixtures/create-kubo.js +32 -0
- package/dist/src/fixtures/create-kubo.js.map +1 -0
- package/dist/src/fixtures/create-peer-ids.d.ts +14 -0
- package/dist/src/fixtures/create-peer-ids.d.ts.map +1 -0
- package/dist/src/fixtures/create-peer-ids.js +37 -0
- package/dist/src/fixtures/create-peer-ids.js.map +1 -0
- package/dist/src/fixtures/key-types.d.ts +3 -0
- package/dist/src/fixtures/key-types.d.ts.map +1 -0
- package/dist/src/fixtures/key-types.js +6 -0
- package/dist/src/fixtures/key-types.js.map +1 -0
- package/dist/src/fixtures/memory-car.d.ts +7 -0
- package/dist/src/fixtures/memory-car.d.ts.map +1 -0
- package/dist/src/fixtures/memory-car.js +26 -0
- package/dist/src/fixtures/memory-car.js.map +1 -0
- package/dist/src/fixtures/wait-for.d.ts +7 -0
- package/dist/src/fixtures/wait-for.d.ts.map +1 -0
- package/dist/src/fixtures/wait-for.js +17 -0
- package/dist/src/fixtures/wait-for.js.map +1 -0
- package/dist/src/helia-blockstore.spec.d.ts +2 -0
- package/dist/src/helia-blockstore.spec.d.ts.map +1 -0
- package/dist/src/helia-blockstore.spec.js +48 -0
- package/dist/src/helia-blockstore.spec.js.map +1 -0
- package/dist/src/helia-hashes.spec.d.ts +2 -0
- package/dist/src/helia-hashes.spec.d.ts.map +1 -0
- package/dist/src/helia-hashes.spec.js +50 -0
- package/dist/src/helia-hashes.spec.js.map +1 -0
- package/dist/src/helia-pins.spec.d.ts +2 -0
- package/dist/src/helia-pins.spec.d.ts.map +1 -0
- package/dist/src/helia-pins.spec.js +48 -0
- package/dist/src/helia-pins.spec.js.map +1 -0
- package/dist/src/index.d.ts +12 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +12 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/ipns-http.spec.d.ts +2 -0
- package/dist/src/ipns-http.spec.d.ts.map +1 -0
- package/dist/src/ipns-http.spec.js +55 -0
- package/dist/src/ipns-http.spec.js.map +1 -0
- package/dist/src/ipns-pubsub.spec.d.ts +2 -0
- package/dist/src/ipns-pubsub.spec.d.ts.map +1 -0
- package/dist/src/ipns-pubsub.spec.js +151 -0
- package/dist/src/ipns-pubsub.spec.js.map +1 -0
- package/dist/src/ipns.spec.d.ts +2 -0
- package/dist/src/ipns.spec.d.ts.map +1 -0
- package/dist/src/ipns.spec.js +146 -0
- package/dist/src/ipns.spec.js.map +1 -0
- package/dist/src/json.spec.d.ts +2 -0
- package/dist/src/json.spec.d.ts.map +1 -0
- package/dist/src/json.spec.js +49 -0
- package/dist/src/json.spec.js.map +1 -0
- package/dist/src/mfs.spec.d.ts +2 -0
- package/dist/src/mfs.spec.d.ts.map +1 -0
- package/dist/src/mfs.spec.js +85 -0
- package/dist/src/mfs.spec.js.map +1 -0
- package/dist/src/strings.spec.d.ts +2 -0
- package/dist/src/strings.spec.d.ts.map +1 -0
- package/dist/src/strings.spec.js +51 -0
- package/dist/src/strings.spec.js.map +1 -0
- package/dist/src/unixfs-bitswap.spec.d.ts +2 -0
- package/dist/src/unixfs-bitswap.spec.d.ts.map +1 -0
- package/dist/src/unixfs-bitswap.spec.js +65 -0
- package/dist/src/unixfs-bitswap.spec.js.map +1 -0
- package/dist/src/unixfs-files.spec.d.ts +2 -0
- package/dist/src/unixfs-files.spec.d.ts.map +1 -0
- package/dist/src/unixfs-files.spec.js +69 -0
- package/dist/src/unixfs-files.spec.js.map +1 -0
- package/package.json +33 -20
- package/src/bin.ts +25 -0
- package/src/car.spec.ts +102 -0
- package/src/dag-cbor.spec.ts +65 -0
- package/src/dag-json.spec.ts +65 -0
- package/src/fixtures/connect.ts +19 -0
- package/src/fixtures/create-helia-http.ts +8 -0
- package/src/fixtures/create-helia.browser.ts +68 -0
- package/src/fixtures/create-helia.ts +55 -0
- package/src/fixtures/create-kubo.browser.ts +30 -0
- package/src/fixtures/create-kubo.ts +32 -0
- package/src/fixtures/create-peer-ids.ts +46 -0
- package/src/fixtures/key-types.ts +7 -0
- package/src/fixtures/memory-car.ts +33 -0
- package/src/fixtures/wait-for.ts +26 -0
- package/src/helia-blockstore.spec.ts +59 -0
- package/src/helia-hashes.spec.ts +61 -0
- package/src/helia-pins.spec.ts +64 -0
- package/src/index.ts +13 -0
- package/src/ipns-http.spec.ts +68 -0
- package/src/ipns-pubsub.spec.ts +187 -0
- package/src/ipns.spec.ts +183 -0
- package/src/json.spec.ts +65 -0
- package/src/mfs.spec.ts +105 -0
- package/src/strings.spec.ts +67 -0
- package/src/unixfs-bitswap.spec.ts +86 -0
- package/src/unixfs-files.spec.ts +89 -0
package/src/json.spec.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
import { json, type JSON, type AddOptions } from '@helia/json'
|
|
4
|
+
import { expect } from 'aegir/chai'
|
|
5
|
+
import { CID } from 'multiformats/cid'
|
|
6
|
+
import * as jsonCodec from 'multiformats/codecs/json'
|
|
7
|
+
import { createHeliaNode } from './fixtures/create-helia.js'
|
|
8
|
+
import { createKuboNode } from './fixtures/create-kubo.js'
|
|
9
|
+
import type { HeliaLibp2p } from 'helia'
|
|
10
|
+
import type { PutOptions as KuboAddOptions } from 'ipfs-core-types/src/block/index.js'
|
|
11
|
+
import type { Controller } from 'ipfsd-ctl'
|
|
12
|
+
|
|
13
|
+
describe('@helia/json', () => {
|
|
14
|
+
let helia: HeliaLibp2p
|
|
15
|
+
let j: JSON
|
|
16
|
+
let kubo: Controller
|
|
17
|
+
|
|
18
|
+
async function expectSameCid (data: () => any, heliaOpts: Partial<AddOptions> = {}, kuboOpts: KuboAddOptions = { format: 'json' }): Promise<void> {
|
|
19
|
+
const heliaCid = await j.add(data(), heliaOpts)
|
|
20
|
+
const kuboCid = await kubo.api.block.put(jsonCodec.encode(data()), kuboOpts)
|
|
21
|
+
|
|
22
|
+
expect(heliaCid.toString()).to.equal(kuboCid.toString())
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
beforeEach(async () => {
|
|
26
|
+
helia = await createHeliaNode()
|
|
27
|
+
j = json(helia)
|
|
28
|
+
kubo = await createKuboNode()
|
|
29
|
+
|
|
30
|
+
await helia.libp2p.dial((await (kubo.api.id())).addresses)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
afterEach(async () => {
|
|
34
|
+
if (helia != null) {
|
|
35
|
+
await helia.stop()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (kubo != null) {
|
|
39
|
+
await kubo.stop()
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('should create the same CID for a string', async () => {
|
|
44
|
+
const candidate = (): any => ({ hello: 'world' })
|
|
45
|
+
|
|
46
|
+
await expectSameCid(candidate)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('should add to helia and fetch from kubo', async () => {
|
|
50
|
+
const input = { hello: 'world' }
|
|
51
|
+
const cid = await j.add(input)
|
|
52
|
+
const block = await kubo.api.block.get(cid)
|
|
53
|
+
const output = jsonCodec.decode(block)
|
|
54
|
+
|
|
55
|
+
expect(output).to.deep.equal(input)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('should add to kubo and fetch from helia', async () => {
|
|
59
|
+
const input = { hello: 'world' }
|
|
60
|
+
const cid = await kubo.api.block.put(jsonCodec.encode(input))
|
|
61
|
+
const output = await j.get(CID.parse(cid.toString()))
|
|
62
|
+
|
|
63
|
+
expect(output).to.deep.equal(input)
|
|
64
|
+
})
|
|
65
|
+
})
|
package/src/mfs.spec.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
import { type MFS, mfs } from '@helia/mfs'
|
|
4
|
+
import { expect } from 'aegir/chai'
|
|
5
|
+
import { createHeliaNode } from './fixtures/create-helia.js'
|
|
6
|
+
import { createKuboNode } from './fixtures/create-kubo.js'
|
|
7
|
+
import type { HeliaLibp2p } from 'helia'
|
|
8
|
+
import type { Controller } from 'ipfsd-ctl'
|
|
9
|
+
|
|
10
|
+
describe('@helia/mfs', () => {
|
|
11
|
+
let helia: HeliaLibp2p
|
|
12
|
+
let fs: MFS
|
|
13
|
+
let kubo: Controller
|
|
14
|
+
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
helia = await createHeliaNode()
|
|
17
|
+
fs = mfs(helia)
|
|
18
|
+
kubo = await createKuboNode()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
afterEach(async () => {
|
|
22
|
+
if (helia != null) {
|
|
23
|
+
await helia.stop()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (kubo != null) {
|
|
27
|
+
await kubo.stop()
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('should have the same CID initially', async () => {
|
|
32
|
+
const heliaStat = await fs.stat('/')
|
|
33
|
+
const kuboStat = await kubo.api.files.stat('/')
|
|
34
|
+
|
|
35
|
+
expect(heliaStat.cid.toV1().toString()).to.equal(kuboStat.cid.toV1().toString())
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('should have the same CID after creating a directory', async () => {
|
|
39
|
+
const dirPath = '/foo'
|
|
40
|
+
await fs.mkdir(dirPath)
|
|
41
|
+
await kubo.api.files.mkdir(dirPath, {
|
|
42
|
+
cidVersion: 1
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
const heliaStat = await fs.stat('/')
|
|
46
|
+
const kuboStat = await kubo.api.files.stat('/')
|
|
47
|
+
|
|
48
|
+
expect(heliaStat.cid.toV1().toString()).to.equal(kuboStat.cid.toV1().toString())
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('should have the same CID after removing a directory', async () => {
|
|
52
|
+
const dirPath = '/foo'
|
|
53
|
+
await fs.mkdir(dirPath)
|
|
54
|
+
await fs.rm(dirPath)
|
|
55
|
+
await kubo.api.files.mkdir(dirPath, {
|
|
56
|
+
cidVersion: 1
|
|
57
|
+
})
|
|
58
|
+
await kubo.api.files.rm(dirPath, {
|
|
59
|
+
recursive: true
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const heliaStat = await fs.stat('/')
|
|
63
|
+
const kuboStat = await kubo.api.files.stat('/')
|
|
64
|
+
|
|
65
|
+
expect(heliaStat.cid.toV1().toString()).to.equal(kuboStat.cid.toV1().toString())
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('should have the same CID after creating a file', async () => {
|
|
69
|
+
const filePath = '/foo.txt'
|
|
70
|
+
const fileData = Uint8Array.from([0, 1, 2, 3, 4])
|
|
71
|
+
await fs.writeBytes(fileData, filePath, {
|
|
72
|
+
rawLeaves: true,
|
|
73
|
+
reduceSingleLeafToSelf: false
|
|
74
|
+
})
|
|
75
|
+
await kubo.api.files.write(filePath, fileData, {
|
|
76
|
+
cidVersion: 1,
|
|
77
|
+
create: true
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
const heliaStat = await fs.stat('/')
|
|
81
|
+
const kuboStat = await kubo.api.files.stat('/')
|
|
82
|
+
|
|
83
|
+
expect(heliaStat.cid.toV1().toString()).to.equal(kuboStat.cid.toV1().toString())
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('should have the same CID after removing a file', async () => {
|
|
87
|
+
const filePath = '/foo.txt'
|
|
88
|
+
const fileData = Uint8Array.from([0, 1, 2, 3, 4])
|
|
89
|
+
await fs.writeBytes(fileData, filePath, {
|
|
90
|
+
rawLeaves: true,
|
|
91
|
+
reduceSingleLeafToSelf: false
|
|
92
|
+
})
|
|
93
|
+
await fs.rm(filePath)
|
|
94
|
+
await kubo.api.files.write(filePath, fileData, {
|
|
95
|
+
cidVersion: 1,
|
|
96
|
+
create: true
|
|
97
|
+
})
|
|
98
|
+
await kubo.api.files.rm(filePath)
|
|
99
|
+
|
|
100
|
+
const heliaStat = await fs.stat('/')
|
|
101
|
+
const kuboStat = await kubo.api.files.stat('/')
|
|
102
|
+
|
|
103
|
+
expect(heliaStat.cid.toV1().toString()).to.equal(kuboStat.cid.toV1().toString())
|
|
104
|
+
})
|
|
105
|
+
})
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
import { strings, type Strings, type AddOptions } from '@helia/strings'
|
|
4
|
+
import { expect } from 'aegir/chai'
|
|
5
|
+
import { CID } from 'multiformats/cid'
|
|
6
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
|
+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
8
|
+
import { createHeliaNode } from './fixtures/create-helia.js'
|
|
9
|
+
import { createKuboNode } from './fixtures/create-kubo.js'
|
|
10
|
+
import type { HeliaLibp2p } from 'helia'
|
|
11
|
+
import type { PutOptions as KuboAddOptions } from 'ipfs-core-types/src/block/index.js'
|
|
12
|
+
import type { Controller } from 'ipfsd-ctl'
|
|
13
|
+
|
|
14
|
+
describe('@helia/strings', () => {
|
|
15
|
+
let helia: HeliaLibp2p
|
|
16
|
+
let str: Strings
|
|
17
|
+
let kubo: Controller
|
|
18
|
+
|
|
19
|
+
async function expectSameCid (data: () => string, heliaOpts: Partial<AddOptions> = {}, kuboOpts: KuboAddOptions = {}): Promise<void> {
|
|
20
|
+
const heliaCid = await str.add(data(), heliaOpts)
|
|
21
|
+
const kuboCid = await kubo.api.block.put(uint8ArrayFromString(data()), kuboOpts)
|
|
22
|
+
|
|
23
|
+
expect(heliaCid.toString()).to.equal(kuboCid.toString())
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
beforeEach(async () => {
|
|
27
|
+
helia = await createHeliaNode()
|
|
28
|
+
str = strings(helia)
|
|
29
|
+
kubo = await createKuboNode()
|
|
30
|
+
|
|
31
|
+
const id = await kubo.api.id()
|
|
32
|
+
await helia.libp2p.dial(id.addresses)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
afterEach(async () => {
|
|
36
|
+
if (helia != null) {
|
|
37
|
+
await helia.stop()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (kubo != null) {
|
|
41
|
+
await kubo.stop()
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('should create the same CID for a string', async () => {
|
|
46
|
+
const candidate = (): string => 'hello world'
|
|
47
|
+
|
|
48
|
+
await expectSameCid(candidate)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('should add to helia and fetch from kubo', async () => {
|
|
52
|
+
const input = 'hello world'
|
|
53
|
+
const cid = await str.add(input)
|
|
54
|
+
const block = await kubo.api.block.get(cid)
|
|
55
|
+
const output = uint8ArrayToString(block)
|
|
56
|
+
|
|
57
|
+
expect(output).to.equal(input)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('should add to kubo and fetch from helia', async () => {
|
|
61
|
+
const input = 'hello world'
|
|
62
|
+
const cid = await kubo.api.block.put(uint8ArrayFromString(input))
|
|
63
|
+
const output = await str.get(CID.parse(cid.toString()))
|
|
64
|
+
|
|
65
|
+
expect(output).to.equal(input)
|
|
66
|
+
})
|
|
67
|
+
})
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
import { type UnixFS, unixfs } from '@helia/unixfs'
|
|
4
|
+
import { expect } from 'aegir/chai'
|
|
5
|
+
import toBuffer from 'it-to-buffer'
|
|
6
|
+
import { CID } from 'multiformats/cid'
|
|
7
|
+
import { createHeliaNode } from './fixtures/create-helia.js'
|
|
8
|
+
import { createKuboNode } from './fixtures/create-kubo.js'
|
|
9
|
+
import type { HeliaLibp2p } from 'helia'
|
|
10
|
+
import type { FileCandidate } from 'ipfs-unixfs-importer'
|
|
11
|
+
import type { Controller } from 'ipfsd-ctl'
|
|
12
|
+
|
|
13
|
+
describe('@helia/unixfs - bitswap', () => {
|
|
14
|
+
let helia: HeliaLibp2p
|
|
15
|
+
let unixFs: UnixFS
|
|
16
|
+
let kubo: Controller
|
|
17
|
+
|
|
18
|
+
beforeEach(async () => {
|
|
19
|
+
helia = await createHeliaNode()
|
|
20
|
+
unixFs = unixfs(helia)
|
|
21
|
+
kubo = await createKuboNode()
|
|
22
|
+
|
|
23
|
+
// connect helia to kubo
|
|
24
|
+
await helia.libp2p.dial(kubo.peer.addresses)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
afterEach(async () => {
|
|
28
|
+
if (helia != null) {
|
|
29
|
+
await helia.stop()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (kubo != null) {
|
|
33
|
+
await kubo.stop()
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should add a large file to helia and fetch it from kubo', async () => {
|
|
38
|
+
const chunkSize = 1024 * 1024
|
|
39
|
+
const size = chunkSize * 10
|
|
40
|
+
const input: Uint8Array[] = []
|
|
41
|
+
|
|
42
|
+
const candidate: FileCandidate = {
|
|
43
|
+
content: (async function * () {
|
|
44
|
+
for (let i = 0; i < size; i += chunkSize) {
|
|
45
|
+
const buf = new Uint8Array(chunkSize)
|
|
46
|
+
input.push(buf)
|
|
47
|
+
|
|
48
|
+
yield buf
|
|
49
|
+
}
|
|
50
|
+
}())
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const cid = await unixFs.addFile(candidate)
|
|
54
|
+
|
|
55
|
+
const output: Uint8Array[] = []
|
|
56
|
+
|
|
57
|
+
for await (const b of kubo.api.cat(cid)) {
|
|
58
|
+
output.push(b)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
expect(toBuffer(output)).to.equalBytes(toBuffer(input))
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it('should add a large file to kubo and fetch it from helia', async () => {
|
|
65
|
+
const chunkSize = 1024 * 1024
|
|
66
|
+
const size = chunkSize * 10
|
|
67
|
+
const input: Uint8Array[] = []
|
|
68
|
+
|
|
69
|
+
const candidate: FileCandidate = {
|
|
70
|
+
content: (async function * () {
|
|
71
|
+
for (let i = 0; i < size; i += chunkSize) {
|
|
72
|
+
const buf = new Uint8Array(chunkSize)
|
|
73
|
+
input.push(buf)
|
|
74
|
+
|
|
75
|
+
yield buf
|
|
76
|
+
}
|
|
77
|
+
}())
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const { cid } = await kubo.api.add(candidate.content)
|
|
81
|
+
|
|
82
|
+
const bytes = await toBuffer(unixFs.cat(CID.parse(cid.toString())))
|
|
83
|
+
|
|
84
|
+
expect(bytes).to.equalBytes(toBuffer(input))
|
|
85
|
+
})
|
|
86
|
+
})
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/* eslint-env mocha */
|
|
2
|
+
|
|
3
|
+
import { type AddOptions, type UnixFS, unixfs } from '@helia/unixfs'
|
|
4
|
+
import { expect } from 'aegir/chai'
|
|
5
|
+
import { fixedSize } from 'ipfs-unixfs-importer/chunker'
|
|
6
|
+
import { balanced } from 'ipfs-unixfs-importer/layout'
|
|
7
|
+
import { CID } from 'multiformats/cid'
|
|
8
|
+
import { createHeliaNode } from './fixtures/create-helia.js'
|
|
9
|
+
import { createKuboNode } from './fixtures/create-kubo.js'
|
|
10
|
+
import type { HeliaLibp2p } from 'helia'
|
|
11
|
+
import type { AddOptions as KuboAddOptions } from 'ipfs-core-types/src/root.js'
|
|
12
|
+
import type { FileCandidate } from 'ipfs-unixfs-importer'
|
|
13
|
+
import type { Controller } from 'ipfsd-ctl'
|
|
14
|
+
|
|
15
|
+
describe('@helia/unixfs - files', () => {
|
|
16
|
+
let helia: HeliaLibp2p
|
|
17
|
+
let unixFs: UnixFS
|
|
18
|
+
let kubo: Controller
|
|
19
|
+
|
|
20
|
+
async function importToHelia (data: FileCandidate, opts?: Partial<AddOptions>): Promise<CID> {
|
|
21
|
+
const cid = await unixFs.addFile(data, opts)
|
|
22
|
+
|
|
23
|
+
return cid
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function importToKubo (data: FileCandidate, opts?: KuboAddOptions): Promise<CID> {
|
|
27
|
+
const result = await kubo.api.add(data.content, opts)
|
|
28
|
+
|
|
29
|
+
return CID.parse(result.cid.toString())
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function expectSameCid (data: () => FileCandidate, heliaOpts: Partial<AddOptions> = {}, kuboOpts: KuboAddOptions = {}): Promise<void> {
|
|
33
|
+
const heliaCid = await importToHelia(data(), {
|
|
34
|
+
// these are the default kubo options
|
|
35
|
+
cidVersion: 0,
|
|
36
|
+
rawLeaves: false,
|
|
37
|
+
layout: balanced({
|
|
38
|
+
maxChildrenPerNode: 174
|
|
39
|
+
}),
|
|
40
|
+
chunker: fixedSize({
|
|
41
|
+
chunkSize: 262144
|
|
42
|
+
}),
|
|
43
|
+
|
|
44
|
+
...heliaOpts
|
|
45
|
+
})
|
|
46
|
+
const kuboCid = await importToKubo(data(), kuboOpts)
|
|
47
|
+
|
|
48
|
+
expect(heliaCid.toString()).to.equal(kuboCid.toString())
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
beforeEach(async () => {
|
|
52
|
+
helia = await createHeliaNode()
|
|
53
|
+
unixFs = unixfs(helia)
|
|
54
|
+
kubo = await createKuboNode()
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
afterEach(async () => {
|
|
58
|
+
if (helia != null) {
|
|
59
|
+
await helia.stop()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (kubo != null) {
|
|
63
|
+
await kubo.stop()
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('should create the same CID for a small file', async () => {
|
|
68
|
+
const candidate = (): FileCandidate => ({
|
|
69
|
+
content: Uint8Array.from([0, 1, 2, 3, 4])
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
await expectSameCid(candidate)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('should create the same CID for a large file', async () => {
|
|
76
|
+
const chunkSize = 1024 * 1024
|
|
77
|
+
const size = chunkSize * 10
|
|
78
|
+
|
|
79
|
+
const candidate = (): FileCandidate => ({
|
|
80
|
+
content: (async function * () {
|
|
81
|
+
for (let i = 0; i < size; i += chunkSize) {
|
|
82
|
+
yield new Uint8Array(chunkSize)
|
|
83
|
+
}
|
|
84
|
+
}())
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
await expectSameCid(candidate)
|
|
88
|
+
})
|
|
89
|
+
})
|