This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
put.js
68 lines (60 loc) · 1.98 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
'use strict'
const Block = require('ipfs-block')
const CID = require('cids')
const multihash = require('multihashes')
const configure = require('../lib/configure')
const toFormData = require('../lib/buffer-to-form-data')
module.exports = configure(({ ky }) => {
async function put (data, options) {
options = options || {}
if (Block.isBlock(data)) {
const { name, length } = multihash.decode(data.cid.multihash)
options = {
...options,
format: data.cid.codec,
mhtype: name,
mhlen: length,
version: data.cid.version
}
data = data.data
} else if (options.cid) {
const cid = new CID(options.cid)
const { name, length } = multihash.decode(cid.multihash)
options = {
...options,
format: cid.codec,
mhtype: name,
mhlen: length,
version: cid.version
}
delete options.cid
}
const searchParams = new URLSearchParams(options.searchParams)
if (options.format) searchParams.set('format', options.format)
if (options.mhtype) searchParams.set('mhtype', options.mhtype)
if (options.mhlen) searchParams.set('mhlen', options.mhlen)
if (options.pin != null) searchParams.set('pin', options.pin)
if (options.version != null) searchParams.set('version', options.version)
let res
try {
res = await ky.post('block/put', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams,
body: toFormData(data)
}).json()
} catch (err) {
// Retry with "protobuf"/"cbor" format for go-ipfs
// TODO: remove when https://github.com/ipfs/go-cid/issues/75 resolved
if (options.format === 'dag-pb') {
return put(data, { ...options, format: 'protobuf' })
} else if (options.format === 'dag-cbor') {
return put(data, { ...options, format: 'cbor' })
}
throw err
}
return new Block(data, new CID(res.Key))
}
return put
})