This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdagLink.js
61 lines (51 loc) · 1.47 KB
/
dagLink.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
'use strict'
const CID = require('cids')
const uint8ArrayFromString = require('uint8arrays/from-string')
/**
* Link represents an IPFS Merkle DAG Link between Nodes.
*/
class DAGLink {
/**
* @param {string | undefined | null} name
* @param {number} size
* @param {CID | string | Uint8Array} cid
*/
constructor (name, size, cid) {
if (!cid) {
throw new Error('A link requires a cid to point to')
}
// assert(size, 'A link requires a size')
// note - links should include size, but this assert is disabled
// for now to maintain consistency with go-ipfs pinset
this.Name = name || ''
this.Tsize = size
this.Hash = new CID(cid)
Object.defineProperties(this, {
_nameBuf: { value: null, writable: true, enumerable: false }
})
}
toString () {
return `DAGLink <${this.Hash.toBaseEncodedString()} - name: "${this.Name}", size: ${this.Tsize}>`
}
toJSON () {
if (!this._json) {
this._json = Object.freeze({
name: this.Name,
size: this.Tsize,
cid: this.Hash.toBaseEncodedString()
})
}
return Object.assign({}, this._json)
}
// Memoize the Uint8Array representation of name
// We need this to sort the links, otherwise
// we will reallocate new Uint8Arrays every time
get nameAsBuffer () {
if (this._nameBuf != null) {
return this._nameBuf
}
this._nameBuf = uint8ArrayFromString(this.Name)
return this._nameBuf
}
}
module.exports = DAGLink