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

Commit 41587e7

Browse files
authored
fix: replace node buffers with uint8arrays (#134)
BREAKING CHANGE: - `util.serialize` now returns a Uint8Array
1 parent 4646b62 commit 41587e7

File tree

8 files changed

+38
-50
lines changed

8 files changed

+38
-50
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# js-ipld-dag-cbor
1+
# js-ipld-dag-cbor <!-- omit in toc -->
22

33
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
44
[![](https://img.shields.io/badge/project-IPLD-blue.svg?style=flat-square)](http://github.com/ipld/ipld)
@@ -14,11 +14,11 @@
1414

1515
> JavaScript implementation of the [IPLD spec](https://github.com/ipfs/specs/tree/master/ipld).
1616
17-
## Lead Maintainer
17+
## Lead Maintainer <!-- omit in toc -->
1818

1919
[Volker Mische](https://github.com/vmx)
2020

21-
## Table of Contents
21+
## Table of Contents <!-- omit in toc -->
2222

2323
- [Install](#install)
2424
- [npm](#npm)
@@ -27,6 +27,10 @@
2727
- [Use in a browser Using a script tag](#use-in-a-browser-using-a-script-tag)
2828
- [Usage](#usage)
2929
- [API](#api)
30+
- [`dagCBOR.util.serialize(obj)`](#dagcborutilserializeobj)
31+
- [`dagCBOR.util.deserialize(serialized)`](#dagcborutildeserializeserialized)
32+
- [`dagCBOR.util.configureDecoder([options])`](#dagcborutilconfiguredecoderoptions)
33+
- [`dagCBOR.util.cid(obj[, options,])`](#dagcborutilcidobj-options)
3034
- [Contribute](#contribute)
3135
- [License](#license)
3236

@@ -73,13 +77,13 @@ const file = {
7377
}
7478

7579
const serialized = dagCBOR.util.serialize(file)
76-
console.log(`Encoded as a ${serialized.length} byte Buffer`)
80+
console.log(`Encoded as a ${serialized.length} byte Uint8Array`)
7781

7882
const node = dagCBOR.util.deserialize(serialized)
7983
console.log('Decoded as:', node)
8084
require('assert').deepEqual(node, file) // should match
8185

82-
// → Encoded as a 22 byte Buffer
86+
// → Encoded as a 22 byte Uint8Array
8387
// → Decoded as: { name: 'hello.txt', size: 11 }
8488
```
8589

@@ -97,7 +101,7 @@ Returns the serialized node.
97101

98102
Decodes an IPLD CBOR encoded representation, restoring any CBOR tags (id `42`) to CIDs.
99103

100-
- `serialized` (`Buffer` or `String`): a binary blob representing an IPLD CBOR encoded object.
104+
- `serialized` (`Uint8Array` or `String`): a binary blob representing an IPLD CBOR encoded object.
101105

102106
Returns the deserialized object.
103107

package.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,17 @@
3838
"homepage": "https://github.com/ipld/js-ipld-dag-cbor",
3939
"dependencies": {
4040
"borc": "^2.1.2",
41-
"buffer": "^5.6.0",
42-
"cids": "~0.8.3",
41+
"cids": "^1.0.0",
4342
"is-circular": "^1.0.2",
44-
"multicodec": "^1.0.3",
45-
"multihashing-async": "^1.0.0"
43+
"multicodec": "^2.0.0",
44+
"multihashing-async": "^2.0.0",
45+
"uint8arrays": "^1.0.0"
4646
},
4747
"devDependencies": {
4848
"aegir": "^25.0.0",
49-
"chai": "^4.2.0",
5049
"detect-node": "^2.0.4",
51-
"dirty-chai": "^2.0.1",
5250
"garbage": "0.0.0",
53-
"multihashes": "^1.0.1"
51+
"multihashes": "^3.0.1"
5452
},
5553
"contributors": [
5654
"David Dias <[email protected]>",

src/resolver.js

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

33
const CID = require('cids')
4-
const { Buffer } = require('buffer')
54
const util = require('./util')
65

76
/**
@@ -10,7 +9,7 @@ const util = require('./util')
109
* Returns the value or a link and the partial mising path. This way the
1110
* IPLD Resolver can fetch the link and continue to resolve.
1211
*
13-
* @param {Buffer} binaryBlob - Binary representation of a CBOR block
12+
* @param {Uint8Array} binaryBlob - Binary representation of a CBOR block
1413
* @param {string} [path='/'] - Path that should be resolved
1514
* @returns {Object} result - Result of the path it it was resolved successfully
1615
* @returns {*} result.value - Value the path resolves to
@@ -45,7 +44,7 @@ exports.resolve = (binaryBlob, path) => {
4544

4645
const traverse = function * (node, path) {
4746
// Traverse only objects and arrays
48-
if (Buffer.isBuffer(node) || CID.isCID(node) || typeof node === 'string' ||
47+
if (node instanceof Uint8Array || CID.isCID(node) || typeof node === 'string' ||
4948
node === null) {
5049
return
5150
}
@@ -60,7 +59,7 @@ const traverse = function * (node, path) {
6059
* Return all available paths of a block.
6160
*
6261
* @generator
63-
* @param {Buffer} binaryBlob - Binary representation of a CBOR block
62+
* @param {Uint8Array} binaryBlob - Binary representation of a CBOR block
6463
* @yields {string} - A single path
6564
*/
6665
exports.tree = function * (binaryBlob) {

src/util.js

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

33
const cbor = require('borc')
4-
const { Buffer } = require('buffer')
54
const multicodec = require('multicodec')
65
const multihashing = require('multihashing-async')
76
const CID = require('cids')
87
const isCircular = require('is-circular')
8+
const uint8ArrayConcat = require('uint8arrays/concat')
9+
const uint8ArrayFromString = require('uint8arrays/from-string')
910

1011
// https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692
1112
const CID_CBOR_TAG = 42
1213

1314
function tagCID (cid) {
1415
if (typeof cid === 'string') {
15-
cid = new CID(cid).buffer
16+
cid = new CID(cid).bytes
1617
} else if (CID.isCID(cid)) {
17-
cid = cid.buffer
18+
cid = cid.bytes
1819
}
1920

20-
return new cbor.Tagged(CID_CBOR_TAG, Buffer.concat([
21-
Buffer.from('00', 'hex'), // thanks jdag
21+
return new cbor.Tagged(CID_CBOR_TAG, uint8ArrayConcat([
22+
uint8ArrayFromString('00', 'base16'), // thanks jdag
2223
cid
23-
]))
24+
], 1 + cid.length))
2425
}
2526

2627
function replaceCIDbyTAG (dagNode) {
@@ -129,7 +130,7 @@ exports.configureDecoder() // Setup default cbor.Decoder
129130
* Serialize internal representation into a binary CBOR block.
130131
*
131132
* @param {Object} node - Internal representation of a CBOR block
132-
* @returns {Buffer} - The encoded binary representation
133+
* @returns {Uint8Array} - The encoded binary representation
133134
*/
134135
exports.serialize = (node) => {
135136
const nodeTagged = replaceCIDbyTAG(node)
@@ -141,7 +142,7 @@ exports.serialize = (node) => {
141142
/**
142143
* Deserialize CBOR block into the internal representation.
143144
*
144-
* @param {Buffer} data - Binary representation of a CBOR block
145+
* @param {Uint8Array} data - Binary representation of a CBOR block
145146
* @returns {Object} - An object that conforms to the IPLD Data Model
146147
*/
147148
exports.deserialize = (data) => {

test/interop.spec.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const chai = require('chai')
5-
const dirtyChai = require('dirty-chai')
6-
const expect = chai.expect
7-
chai.use(dirtyChai)
4+
const { expect } = require('aegir/utils/chai')
85
const dagCBOR = require('../src')
96
const loadFixture = require('aegir/fixtures')
107
const isNode = require('detect-node')

test/mod.spec.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const chai = require('chai')
5-
const dirtyChai = require('dirty-chai')
6-
const expect = chai.expect
7-
chai.use(dirtyChai)
4+
const { expect } = require('aegir/utils/chai')
85
const multicodec = require('multicodec')
9-
106
const mod = require('../src')
117

128
describe('IPLD Format', () => {

test/resolver.spec.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22
/* eslint max-nested-callbacks: ["error", 5] */
33
'use strict'
44

5-
const chai = require('chai')
6-
const dirtyChai = require('dirty-chai')
7-
const expect = chai.expect
8-
chai.use(dirtyChai)
9-
5+
const { expect } = require('aegir/utils/chai')
106
const CID = require('cids')
11-
127
const dagCBOR = require('../src')
138
const resolver = dagCBOR.resolver
149

test/util.spec.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
5-
const chai = require('chai')
6-
const dirtyChai = require('dirty-chai')
7-
const expect = chai.expect
8-
chai.use(dirtyChai)
4+
const { expect } = require('aegir/utils/chai')
95
const garbage = require('garbage')
106
const dagCBOR = require('../src')
117
const multihash = require('multihashes')
128
const CID = require('cids')
9+
const uint8ArrayFromString = require('uint8arrays/from-string')
10+
const uint8ArrayConcat = require('uint8arrays/concat')
1311

1412
describe('util', () => {
1513
const obj = {
@@ -27,7 +25,7 @@ describe('util', () => {
2725
const serializedObj = dagCBOR.util.serialize(obj)
2826

2927
it('.serialize and .deserialize', () => {
30-
expect(Buffer.isBuffer(serializedObj)).to.equal(true)
28+
expect(serializedObj).to.be.a('Uint8Array')
3129

3230
// Check for the tag 42
3331
// d8 = tag, 2a = 42
@@ -45,7 +43,7 @@ describe('util', () => {
4543
const largeObj = { someKey: [].slice.call(new Uint8Array(dataSize)) }
4644

4745
const serialized = dagCBOR.util.serialize(largeObj)
48-
expect(Buffer.isBuffer(serialized)).to.be.true()
46+
expect(serialized).to.be.a('Uint8Array')
4947

5048
const deserialized = dagCBOR.util.deserialize(serialized)
5149
expect(largeObj).to.eql(deserialized)
@@ -60,7 +58,7 @@ describe('util', () => {
6058

6159
dagCBOR.util.configureDecoder({ size: 64 * 1024, maxSize: 128 * 1024 }) // 64 Kb start, 128 Kb max
6260
const serialized = dagCBOR.util.serialize(largeObj)
63-
expect(Buffer.isBuffer(serialized)).to.be.true()
61+
expect(serialized).to.be.a('Uint8Array')
6462

6563
expect(() => dagCBOR.util.deserialize(serialized)).to.throw(
6664
'Data is too large to deserialize with current decoder')
@@ -111,7 +109,7 @@ describe('util', () => {
111109
})
112110

113111
it('.serialize and .deserialize object with Uint8Array field', () => {
114-
const buffer = Buffer.from('some data')
112+
const buffer = uint8ArrayFromString('some data')
115113
const bytes = Uint8Array.from(buffer)
116114

117115
const s1 = dagCBOR.util.serialize({ data: buffer })
@@ -127,7 +125,7 @@ describe('util', () => {
127125
expect(() =>
128126
// two top-level CBOR objects, the original and a single uint=0, valid if using
129127
// CBOR in streaming mode, not valid here
130-
dagCBOR.util.deserialize(Buffer.concat([serializedObj, Buffer.alloc(1)]))
128+
dagCBOR.util.deserialize(uint8ArrayConcat([serializedObj, new Uint8Array(1)]))
131129
).to.throw(Error, 'Extraneous CBOR data found beyond initial top-level object')
132130
})
133131
})

0 commit comments

Comments
 (0)