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 pathput.js
71 lines (55 loc) · 2.01 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
/* eslint-env mocha */
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { base58btc } from 'multiformats/bases/base58'
import { CID } from 'multiformats/cid'
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
import all from 'it-all'
import * as raw from 'multiformats/codecs/raw'
import { sha512 } from 'multiformats/hashes/sha2'
/**
* @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('.block.put', () => {
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async () => {
ipfs = (await factory.spawn()).api
})
after(() => factory.clean())
it('should put a buffer, using defaults', async () => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const blob = uint8ArrayFromString('blorb')
const cid = await ipfs.block.put(blob)
expect(cid.toString()).to.equal(expectedHash)
expect(cid.bytes).to.equalBytes(base58btc.decode(`z${expectedHash}`))
})
it('should put a buffer, using options', async () => {
const blob = uint8ArrayFromString(`TEST${Math.random()}`)
const cid = await ipfs.block.put(blob, {
format: 'raw',
mhtype: 'sha2-512',
version: 1,
pin: true
})
expect(cid.version).to.equal(1)
expect(cid.code).to.equal(raw.code)
expect(cid.multihash.code).to.equal(sha512.code)
expect(await all(ipfs.pin.ls({ paths: cid }))).to.have.lengthOf(1)
})
it('should put a Block instance', async () => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const expectedCID = CID.parse(expectedHash)
const b = uint8ArrayFromString('blorb')
const cid = await ipfs.block.put(b)
expect(cid.multihash.bytes).to.equalBytes(expectedCID.multihash.bytes)
})
})
}