Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit bef3f26

Browse files
achingbrainvmx
authored andcommitted
fix: replace node Buffers with Uint8Arrays
Relaxes checks on `.Data` to allow for `Uint8Array`s in place of node `Buffer`s. BREAKING CHANGES: - `dagNode.Data` can now be a `Uint8Array`
1 parent 01d6039 commit bef3f26

File tree

13 files changed

+99
-97
lines changed

13 files changed

+99
-97
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ dagPB.util
7777
#### Create a DAGNode
7878

7979
```JavaScript
80-
const node1 = new DAGNode(Buffer.from('some data'))
80+
const node1 = new DAGNode(new TextEncoder('utf8').encode('some data'))
8181

8282
// node2 will have the same data as node1
8383
const node2 = new DAGNode('some data')
@@ -114,7 +114,7 @@ const DAGNode = dagPB.DAGNode
114114

115115
#### DAGNode constructor
116116

117-
- `data` - type: Buffer
117+
- `data` - type: Uint8Array or String
118118
- `links`- (optional) type: Array of DAGLink instances or Array of DAGLink instances in its json format (link.toJSON)
119119
- `serializedSize`- (optional) type: Number of bytes the serialized node has. If none is given, it will automatically be calculated.
120120

@@ -198,7 +198,7 @@ node.rmLink('Link1')
198198

199199
#### `node.serialize()`
200200

201-
Serialize the DAGNode instance to its portable binary format. Yields the same result as `dagPB.util.serialize(node)`. Returns a `Buffer`.
201+
Serialize the DAGNode instance to its portable binary format. Yields the same result as `dagPB.util.serialize(node)`. Returns a `Uint8Array`.
202202

203203
### DAGLink functions
204204

@@ -238,7 +238,6 @@ const link = new DAGLink(
238238

239239
> See: https://github.com/ipld/interface-ipld-format#local-resolver-methods
240240
241-
242241
#### `dagPB.resolver.resolve`
243242

244243
#### `dagPB.resolver.tree`
@@ -251,7 +250,7 @@ const link = new DAGLink(
251250

252251
### `dagPB.util.serialize`
253252

254-
Serialize the DAGNode instance to its portable binary format. Yields the same result as `node.serialize()`. Returns a `Buffer`.
253+
Serialize the DAGNode instance to its portable binary format. Yields the same result as `node.serialize()`. Returns a `Uint8Array`.
255254

256255
### `dagPB.util.deserialize`
257256

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,24 @@
6565
"npm": ">=3.0.0"
6666
},
6767
"dependencies": {
68-
"buffer": "^5.6.0",
69-
"cids": "~0.8.3",
68+
"cids": "multiformats/js-cid#fix/support-uint8arrays",
7069
"class-is": "^1.1.0",
70+
"ipfs-utils": "ipfs/js-ipfs-utils#feat/add-utility-uint8array-functions",
7171
"multicodec": "^1.0.3",
7272
"multihashing-async": "^1.0.0",
73+
"npm": "^6.14.7",
7374
"protons": "^1.2.1",
75+
"reset": "^0.1.0",
76+
"run": "^1.4.0",
7477
"stable": "^0.1.8"
7578
},
7679
"devDependencies": {
7780
"aegir": "^23.0.0",
7881
"fs-extra": "^9.0.1",
79-
"ipfs-block-service": "~0.17.1",
82+
"ipfs-block-service": "^0.17.1",
8083
"ipfs-repo": "^4.0.0",
81-
"ipfs-utils": "^2.3.1",
82-
"ipld-block": "~0.9.2",
83-
"multibase": "^1.0.1",
84+
"ipld-block": "^0.9.2",
85+
"multibase": "^2.0.0",
8486
"multihashes": "^1.0.1"
8587
}
8688
}

src/dag-link/dagLink.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const CID = require('cids')
44
const withIs = require('class-is')
5-
const { Buffer } = require('buffer')
5+
const uint8ArrayFromString = require('ipfs-utils/src/uint8arrays/from-string')
66

77
// Link represents an IPFS Merkle DAG Link between Nodes.
88
class DAGLink {
@@ -39,15 +39,15 @@ class DAGLink {
3939
return Object.assign({}, this._json)
4040
}
4141

42-
// Memoize the Buffer representation of name
42+
// Memoize the Uint8Array representation of name
4343
// We need this to sort the links, otherwise
44-
// we will reallocate new buffers every time
44+
// we will reallocate new Uint8Arrays every time
4545
get nameAsBuffer () {
4646
if (this._nameBuf !== null) {
4747
return this._nameBuf
4848
}
4949

50-
this._nameBuf = Buffer.from(this.Name)
50+
this._nameBuf = uint8ArrayFromString(this.Name)
5151
return this._nameBuf
5252
}
5353
}

src/dag-node/dagNode.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
'use strict'
22

33
const withIs = require('class-is')
4-
const { Buffer } = require('buffer')
54
const sortLinks = require('./sortLinks')
65
const DAGLink = require('../dag-link/dagLink')
76
const { serializeDAGNode } = require('../serialize.js')
87
const toDAGLink = require('./toDagLink')
98
const addLink = require('./addLink')
109
const rmLink = require('./rmLink')
10+
const uint8ArrayFromString = require('ipfs-utils/src/uint8arrays/from-string')
11+
const uint8ArrayToString = require('ipfs-utils/src/uint8arrays/to-string')
1112

1213
class DAGNode {
1314
constructor (data, links = [], serializedSize = null) {
1415
if (!data) {
15-
data = Buffer.alloc(0)
16+
data = new Uint8Array(0)
1617
}
1718
if (typeof data === 'string') {
18-
data = Buffer.from(data)
19+
data = uint8ArrayFromString(data)
1920
}
20-
if (!Buffer.isBuffer(data)) {
21-
throw new Error('Passed \'data\' is not a buffer or a string!')
21+
22+
if (!(data instanceof Uint8Array)) {
23+
throw new Error('Passed \'data\' is not a Uint8Array or a String!')
2224
}
2325

2426
if (serializedSize !== null && typeof serializedSize !== 'number') {
@@ -53,7 +55,7 @@ class DAGNode {
5355
}
5456

5557
toString () {
56-
return `DAGNode <data: "${this.Data.toString('base64')}", links: ${this.Links.length}, size: ${this.size}>`
58+
return `DAGNode <data: "${uint8ArrayToString(this.Data, 'base64urlpad')}", links: ${this.Links.length}, size: ${this.size}>`
5759
}
5860

5961
_invalidateCached () {

src/dag-node/rmLink.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
'use strict'
22

33
const CID = require('cids')
4-
const { Buffer } = require('buffer')
4+
const uint8ArrayEquals = require('ipfs-utils/src/uint8arrays/equals')
55

66
const rmLink = (dagNode, nameOrCid) => {
77
let predicate = null
88

99
// It's a name
1010
if (typeof nameOrCid === 'string') {
1111
predicate = (link) => link.Name === nameOrCid
12-
} else if (Buffer.isBuffer(nameOrCid) || CID.isCID(nameOrCid)) {
13-
predicate = (link) => link.Hash.equals(nameOrCid)
12+
} else if (nameOrCid instanceof Uint8Array || CID.isCID(nameOrCid)) {
13+
predicate = (link) => uint8ArrayEquals(link.Hash, nameOrCid)
1414
}
1515

1616
if (predicate) {

src/dag-node/sortLinks.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const sort = require('stable')
4+
const uint8ArraySort = require('ipfs-utils/src/uint8arrays/sort')
55

66
const linkSort = (a, b) => {
7-
return Buffer.compare(a.nameAsBuffer, b.nameAsBuffer)
7+
const buf1 = a.nameAsBuffer
8+
const buf2 = b.nameAsBuffer
9+
10+
return uint8ArraySort(buf1, buf2)
811
}
912

1013
/**

src/resolver.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const util = require('./util')
1010
* Returns the value or a link and the partial mising path. This way the
1111
* IPLD Resolver can fetch the link and continue to resolve.
1212
*
13-
* @param {Buffer} binaryBlob - Binary representation of a PB block
13+
* @param {Uint8Array} binaryBlob - Binary representation of a PB block
1414
* @param {string} [path='/'] - Path that should be resolved
1515
* @returns {Object} result - Result of the path it it was resolved successfully
1616
* @returns {*} result.value - Value the path resolves to
@@ -58,7 +58,7 @@ exports.resolve = (binaryBlob, path) => {
5858
* Return all available paths of a block.
5959
*
6060
* @generator
61-
* @param {Buffer} binaryBlob - Binary representation of a PB block
61+
* @param {Uint8Array} binaryBlob - Binary representation of a PB block
6262
* @yields {string} - A single path
6363
*/
6464
exports.tree = function * (binaryBlob) {

src/serialize.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const toProtoBuf = (node) => {
1313
pbn.Data = node.Data
1414
} else {
1515
// NOTE: this has to be null in order to match go-ipfs serialization
16-
// `null !== new Buffer(0)`
16+
// `null !== new Uint8Array(0)`
1717
pbn.Data = null
1818
}
1919

@@ -35,7 +35,7 @@ const toProtoBuf = (node) => {
3535
* Serialize internal representation into a binary PB block.
3636
*
3737
* @param {Object} node - Internal representation of a PB block
38-
* @returns {Buffer} - The encoded binary representation
38+
* @returns {Uint8Array} - The encoded binary representation
3939
*/
4040
const serializeDAGNode = (node) => {
4141
const data = node.Data

src/util.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const protons = require('protons')
54
const proto = protons(require('./dag.proto'))
65
const DAGLink = require('./dag-link/dagLink')
@@ -30,7 +29,7 @@ const cid = (binaryBlob, userOptions) => {
3029
* Serialize internal representation into a binary PB block.
3130
*
3231
* @param {Object} node - Internal representation of a CBOR block
33-
* @returns {Buffer} - The encoded binary representation
32+
* @returns {Uint8Array} - The encoded binary representation
3433
*/
3534
const serialize = (node) => {
3635
if (DAGNode.isDAGNode(node)) {
@@ -43,7 +42,7 @@ const serialize = (node) => {
4342
/**
4443
* Deserialize PB block into the internal representation.
4544
*
46-
* @param {Buffer} buffer - Binary representation of a PB block
45+
* @param {Uint8Array} buffer - Binary representation of a PB block
4746
* @returns {Object} - An object that conforms to the IPLD Data Model
4847
*/
4948
const deserialize = (buffer) => {
@@ -53,9 +52,9 @@ const deserialize = (buffer) => {
5352
return new DAGLink(link.Name, link.Tsize, link.Hash)
5453
})
5554

56-
const data = pbn.Data == null ? Buffer.alloc(0) : pbn.Data
55+
const data = pbn.Data == null ? new Uint8Array(0) : pbn.Data
5756

58-
return new DAGNode(data, links, buffer.length)
57+
return new DAGNode(data, links, buffer.byteLength)
5958
}
6059

6160
exports.serialize = serialize

test/dag-link-test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
'use strict'
33

44
const chai = require('aegir/utils/chai')
5-
const { Buffer } = require('buffer')
65
const expect = chai.expect
76
const CID = require('cids')
87
const DAGLink = require('../src').DAGLink
8+
const uint8ArrayFromString = require('ipfs-utils/src/uint8arrays/from-string')
9+
const uint8ArrayToString = require('ipfs-utils/src/uint8arrays/to-string')
910

1011
module.exports = (repo) => {
1112
describe('DAGLink', () => {
1213
describe('create with multihash as b58 encoded string', () => {
1314
it('string', () => {
1415
const link = new DAGLink('hello', 3, 'QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U')
1516

16-
expect(link.Hash.buffer.toString('hex'))
17+
expect(uint8ArrayToString(link.Hash.buffer, 'base16'))
1718
.to.equal('12208ab7a6c5e74737878ac73863cb76739d15d4666de44e5756bf55a2f9e9ab5f43')
1819
})
1920

@@ -23,7 +24,7 @@ module.exports = (repo) => {
2324
})
2425

2526
it('create with multihash as a multihash Buffer', () => {
26-
const link = new DAGLink('hello', 3, Buffer.from('12208ab7a6c5e74737878ac73863cb76739d15d4666de44e5756bf55a2f9e9ab5f43', 'hex'))
27+
const link = new DAGLink('hello', 3, uint8ArrayFromString('12208ab7a6c5e74737878ac73863cb76739d15d4666de44e5756bf55a2f9e9ab5f43', 'base16'))
2728

2829
expect(new CID(link.Hash).toBaseEncodedString())
2930
.to.equal('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39U')

0 commit comments

Comments
 (0)