forked from ipld/js-ipld-block
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (51 loc) · 1.3 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
'use strict'
const { Buffer } = require('buffer')
const CID = require('cids')
const withIs = require('class-is')
/**
* Represents an immutable block of data that is uniquely referenced with a cid.
*
* @constructor
* @param {Buffer} data - The data to be stored in the block as a buffer.
* @param {CID} cid - The cid of the data
*
* @example
* const block = new Block(new Buffer('a012d83b20f9371...'))
*/
class Block {
constructor (data, cid) {
if (!data || !ArrayBuffer.isView(data)) {
throw new Error('first argument must be a buffer or typed array')
} else if (!Buffer.isBuffer(data)) {
data = Buffer.from(data.buffer, data.byteOffset, data.byteLength)
}
if (!cid || !CID.isCID(cid)) {
throw new Error('second argument must be a CID')
}
this._data = data
this._cid = cid
}
/**
* The data of this block.
*
* @type {Buffer}
*/
get data () {
return this._data
}
set data (val) {
throw new Error('Tried to change an immutable block')
}
/**
* The cid of the data this block represents.
*
* @type {CID}
*/
get cid () {
return this._cid
}
set cid (val) {
throw new Error('Tried to change an immutable block')
}
}
module.exports = withIs(Block, { className: 'Block', symbolName: '@ipld/js-ipld-block/block' })