This repository was archived by the owner on Sep 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
114 lines (97 loc) · 2.72 KB
/
index.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
'use strict'
const Block = require('multiformats/block')
const { CID } = require('multiformats/cid')
const dagPb = require('@ipld/dag-pb')
const dagCbor = require('@ipld/dag-cbor')
const { sha256: hasher } = require('multiformats/hashes/sha2')
const mhtype = 'sha2-256'
const { base58btc } = require('multiformats/bases/base58')
const defaultBase = base58btc
const unsupportedCodecError = () => new Error('unsupported codec')
const cidifyString = (str) => {
if (!str) {
return str
}
if (Array.isArray(str)) {
return str.map(cidifyString)
}
return CID.parse(str)
}
const stringifyCid = (cid, options = {}) => {
if (!cid || typeof cid === 'string') {
return cid
}
if (Array.isArray(cid)) {
return cid.map(stringifyCid)
}
if (cid['/']) {
return cid['/']
}
const base = options.base || defaultBase
return cid.toString(base)
}
const codecCodes = {
[dagPb.code]: dagPb,
[dagCbor.code]: dagCbor
}
const codecMap = {
// staying backward compatible
// old writeObj function was never raw codec; defaulted to cbor via ipfs.dag
raw: dagCbor,
'dag-pb': dagPb,
'dag-cbor': dagCbor
}
async function read (ipfs, cid, options = {}) {
cid = cidifyString(stringifyCid(cid))
const codec = codecCodes[cid.code]
if (!codec) throw unsupportedCodecError()
const bytes = await ipfs.block.get(cid, { timout: options.timeout })
const block = await Block.decode({ bytes, codec, hasher })
if (block.cid.code === dagPb.code) {
return JSON.parse(new TextDecoder().decode(block.value.Data))
}
if (block.cid.code === dagCbor.code) {
const value = block.value
const links = options.links || []
links.forEach((prop) => {
if (value[prop]) {
value[prop] = stringifyCid(value[prop], options)
}
})
return value
}
}
async function write (ipfs, format, value, options = {}) {
if (options.format === 'dag-pb') format = options.format
const codec = codecMap[format]
if (!codec) throw unsupportedCodecError()
if (codec.code === dagPb.code) {
value = typeof value === 'string' ? value : JSON.stringify(value)
value = { Data: new TextEncoder().encode(value), Links: [] }
}
if (codec.code === dagCbor.code) {
const links = options.links || []
links.forEach((prop) => {
if (value[prop]) {
value[prop] = cidifyString(value[prop])
}
})
}
const block = await Block.encode({ value, codec, hasher })
await ipfs.block.put(block.bytes, {
cid: block.cid.bytes,
version: block.cid.version,
format,
mhtype,
pin: options.pin,
timeout: options.timeout
})
const cid = codec.code === dagPb.code
? block.cid.toV0()
: block.cid
return cid.toString(options.base || defaultBase)
}
module.exports = {
read,
write
}