This repository has been 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.3k
/
Copy pathput.js
117 lines (97 loc) · 3.34 KB
/
put.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
/* eslint-env mocha */
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import * as dagCBOR from '@ipld/dag-cbor'
import { CID } from 'multiformats/cid'
import { sha256, sha512 } from 'multiformats/hashes/sha2'
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {object} options
*/
export function testPut (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.dag.put', () => {
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async () => { ipfs = (await factory.spawn()).api })
after(() => factory.clean())
const pbNode = {
Data: uint8ArrayFromString('some data'),
Links: []
}
const cborNode = {
data: uint8ArrayFromString('some other data')
}
const joseNode = 'eyJhbGciOiJFUzI1NksifQ.AXESICjDGMg3fEBSX7_fpbBUYF4E61TXLysmLJgfGEpFG8Pu.z7a2MvPWLsd7leOeHyfeA1OcAFC9yy5rn1HD8xCeHz3nFrwyn_Su5xXUoaIxAre3fXhGjPkVSNiCE36AKiaMng'
it('should put dag-pb with default hash func (sha2-256)', () => {
return ipfs.dag.put(pbNode, {
storeCodec: 'dag-pb',
hashAlg: 'sha2-256'
})
})
it('should put dag-pb with non-default hash func (sha2-512)', () => {
return ipfs.dag.put(pbNode, {
storeCodec: 'dag-pb',
hashAlg: 'sha2-512'
})
})
it('should put dag-cbor with default hash func (sha2-256)', () => {
return ipfs.dag.put(cborNode, {
storeCodec: 'dag-cbor',
hashAlg: 'sha2-256'
})
})
it('should put dag-cbor with non-default hash func (sha2-512)', () => {
return ipfs.dag.put(cborNode, {
storeCodec: 'dag-cbor',
hashAlg: 'sha2-512'
})
})
it('should put dag-jose with default hash func (sha2-256)', () => {
return ipfs.dag.put(joseNode, {
storeCodec: 'dag-jose',
hashAlg: 'sha2-256'
})
})
it('should put dag-jose with non-default hash func (sha2-512)', () => {
return ipfs.dag.put(joseNode, {
storeCodec: 'dag-jose',
hashAlg: 'sha2-512'
})
})
it('should return the cid', async () => {
const cid = await ipfs.dag.put(cborNode, {
storeCodec: 'dag-cbor',
hashAlg: 'sha2-256'
})
expect(cid).to.exist()
expect(cid).to.be.an.instanceOf(CID)
const bytes = dagCBOR.encode(cborNode)
const hash = await sha256.digest(bytes)
const _cid = CID.createV1(dagCBOR.code, hash)
expect(cid.bytes).to.eql(_cid.bytes)
})
it('should not fail when calling put without options', () => {
return ipfs.dag.put(cborNode)
})
it('should set defaults when calling put without options', async () => {
const cid = await ipfs.dag.put(cborNode)
expect(cid.code).to.equal(dagCBOR.code)
expect(cid.multihash.code).to.equal(sha256.code)
})
it('should override hash algorithm default and resolve with it', async () => {
const cid = await ipfs.dag.put(cborNode, {
storeCodec: 'dag-cbor',
hashAlg: 'sha2-512'
})
expect(cid.code).to.equal(dagCBOR.code)
expect(cid.multihash.code).to.equal(sha512.code)
})
it.skip('should put by passing the cid instead of format and hashAlg', (done) => {})
})
}