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.02 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 */
'use strict'
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
const { base58btc } = require('multiformats/bases/base58')
const { CID } = require('multiformats/cid')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const all = require('it-all')
const raw = require('multiformats/codecs/raw')
const { sha512 } = require('multiformats/hashes/sha2')
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {Object} options
*/
module.exports = (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)
})
})
}