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

Commit 8c999ba

Browse files
authored
fix: replace node buffers with uint8arrays (#70)
This module now accepts Uint8Arrays as well as node Buffers and returns Uint8Arrays. Internally it converts non-Buffers into Buffers because the ethereum libs require that. BREAKING CHANGES: - `util.serialize` returns a `Uint8Array` - `util.cid` returns `CID`s with a breaking API change - see multiformats/js-cid#117 for changes
1 parent a5d4094 commit 8c999ba

File tree

7 files changed

+41
-34
lines changed

7 files changed

+41
-34
lines changed

package.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,15 @@
3737
"homepage": "https://github.com/ipld/js-ipld-git",
3838
"dependencies": {
3939
"buffer": "^5.6.0",
40-
"cids": "^0.8.3",
41-
"multicodec": "^1.0.2",
42-
"multihashing-async": "^1.0.0",
40+
"cids": "^1.0.0",
41+
"multicodec": "^2.0.0",
42+
"multihashing-async": "^2.0.0",
4343
"smart-buffer": "^4.1.0",
44-
"strftime": "^0.10.0"
44+
"strftime": "^0.10.0",
45+
"uint8arrays": "^1.0.0"
4546
},
4647
"devDependencies": {
47-
"aegir": "^25.0.0",
48-
"chai": "^4.2.0",
49-
"chai-as-promised": "^7.1.1",
50-
"dirty-chai": "^2.0.1"
48+
"aegir": "^25.0.0"
5149
},
5250
"contributors": [
5351
"Volker Mische <volker.mische@gmail.com>",

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

65
const util = require('./util')
76

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

4746
const traverse = function * (node, path) {
4847
// Traverse only objects and arrays
49-
if (Buffer.isBuffer(node) || CID.isCID(node) || typeof node === 'string' ||
48+
if (node instanceof Uint8Array || CID.isCID(node) || typeof node === 'string' ||
5049
node === null) {
5150
return
5251
}
@@ -61,7 +60,7 @@ const traverse = function * (node, path) {
6160
* Return all available paths of a block.
6261
*
6362
* @generator
64-
* @param {Buffer} binaryBlob - Binary representation of a Bitcoin block
63+
* @param {Uint8Array} binaryBlob - Binary representation of a Bitcoin block
6564
* @yields {string} - A single path
6665
*/
6766
exports.tree = function * (binaryBlob) {

src/util.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const multihashing = require('multihashing-async')
44
const CID = require('cids')
55
const multicodec = require('multicodec')
66
const { Buffer } = require('buffer')
7+
const uint8ArrayToString = require('uint8arrays/to-string')
78

89
const gitUtil = require('./util/util')
910

@@ -20,15 +21,15 @@ exports.defaultHashAlg = multicodec.SHA1
2021
* Serialize internal representation into a binary Git block.
2122
*
2223
* @param {GitBlock} dagNode - Internal representation of a Git block
23-
* @returns {Buffer}
24+
* @returns {Uint8Array}
2425
*/
2526
exports.serialize = (dagNode) => {
2627
if (dagNode === null) {
2728
throw new Error('dagNode passed to serialize was null')
2829
}
2930

30-
if (Buffer.isBuffer(dagNode)) {
31-
if (dagNode.slice(0, 4).toString() === 'blob') {
31+
if (dagNode instanceof Uint8Array) {
32+
if (uint8ArrayToString(dagNode.slice(0, 4)) === 'blob') {
3233
return dagNode
3334
} else {
3435
throw new Error('unexpected dagNode passed to serialize')
@@ -49,10 +50,14 @@ exports.serialize = (dagNode) => {
4950
/**
5051
* Deserialize Git block into the internal representation.
5152
*
52-
* @param {Buffer} data - Binary representation of a Git block.
53+
* @param {Uint8Array} data - Binary representation of a Git block.
5354
* @returns {BitcoinBlock}
5455
*/
5556
exports.deserialize = (data) => {
57+
if (!Buffer.isBuffer(data)) {
58+
data = Buffer.from(data, data.byteOffset, data.byteLength)
59+
}
60+
5661
const headLen = gitUtil.find(data, 0)
5762
const head = data.slice(0, headLen).toString()
5863
const typeLen = head.match(/([^ ]+) (\d+)/)

test/mod.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 multicodec = require('multicodec')
96

107
const mod = require('../src')

test/parse.spec.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
'use strict'
55

6-
const chai = require('chai')
7-
const dirtyChai = require('dirty-chai')
8-
const expect = chai.expect
9-
chai.use(dirtyChai)
6+
const { expect } = require('aegir/utils/chai')
107
const { Buffer } = require('buffer')
118
const loadFixture = require('aegir/fixtures')
129
const zlib = require('zlib')

test/resolver.spec.js

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

5-
const chai = require('chai')
6-
const dirtyChai = require('dirty-chai')
7-
const expect = chai.expect
8-
chai.use(dirtyChai)
5+
const { expect } = require('aegir/utils/chai')
96
const { Buffer } = require('buffer')
107

118
const CID = require('cids')

test/util.spec.js

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

4-
const chai = require('chai')
5-
const chaiAsProised = require('chai-as-promised')
6-
const dirtyChai = require('dirty-chai')
7-
const expect = chai.expect
8-
chai.use(chaiAsProised)
9-
chai.use(dirtyChai)
4+
const { expect } = require('aegir/utils/chai')
105
const ipldGit = require('../src')
116
const multicodec = require('multicodec')
127
const multihash = require('multihashing-async').multihash
138
const CID = require('cids')
149
const { Buffer } = require('buffer')
10+
const uint8ArrayFromString = require('uint8arrays/from-string')
1511

1612
describe('IPLD format util', () => {
1713
const tagNode = {
@@ -28,6 +24,13 @@ describe('IPLD format util', () => {
2824
}
2925
const tagBlob = ipldGit.util.serialize(tagNode)
3026

27+
it('.serialize from Uint8Array', () => {
28+
const node = uint8ArrayFromString('blob-blob')
29+
const blob = ipldGit.util.serialize(node)
30+
31+
expect(blob).to.deep.equal(node)
32+
})
33+
3134
it('.serialize and .deserialize', () => {
3235
expect(Buffer.isBuffer(tagBlob)).to.be.true()
3336
const deserialized = ipldGit.util.deserialize(tagBlob)
@@ -39,6 +42,17 @@ describe('IPLD format util', () => {
3942
expect(deserialized).to.eql(expected)
4043
})
4144

45+
it('.serialize and .deserialize Uint8Array', () => {
46+
expect(Buffer.isBuffer(Uint8Array.of(...tagBlob))).to.be.false()
47+
const deserialized = ipldGit.util.deserialize(Uint8Array.of(...tagBlob))
48+
49+
// The `gitType` is not enumerable, hence `eql()` would find it. Thus
50+
// remove that property so that that check passes
51+
const expected = Object.assign({}, tagNode)
52+
delete expected.gitType
53+
expect(deserialized).to.eql(expected)
54+
})
55+
4256
it('.cid', async () => {
4357
const cid = await ipldGit.util.cid(tagBlob)
4458
expect(cid.version).to.equal(1)

0 commit comments

Comments
 (0)