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

Commit b2173cf

Browse files
committed
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 b2173cf

File tree

7 files changed

+29
-32
lines changed

7 files changed

+29
-32
lines changed

package.json

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,14 @@
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",
4444
"strftime": "^0.10.0"
4545
},
4646
"devDependencies": {
47-
"aegir": "^25.0.0",
48-
"chai": "^4.2.0",
49-
"chai-as-promised": "^7.1.1",
50-
"dirty-chai": "^2.0.1"
47+
"aegir": "^25.0.0"
5148
},
5249
"contributors": [
5350
"Volker Mische <[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

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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ exports.defaultHashAlg = multicodec.SHA1
2020
* Serialize internal representation into a binary Git block.
2121
*
2222
* @param {GitBlock} dagNode - Internal representation of a Git block
23-
* @returns {Buffer}
23+
* @returns {Uint8Array}
2424
*/
2525
exports.serialize = (dagNode) => {
2626
if (dagNode === null) {
2727
throw new Error('dagNode passed to serialize was null')
2828
}
2929

30-
if (Buffer.isBuffer(dagNode)) {
30+
if (dagNode instanceof Uint8Array) {
3131
if (dagNode.slice(0, 4).toString() === 'blob') {
3232
return dagNode
3333
} else {
@@ -49,10 +49,14 @@ exports.serialize = (dagNode) => {
4949
/**
5050
* Deserialize Git block into the internal representation.
5151
*
52-
* @param {Buffer} data - Binary representation of a Git block.
52+
* @param {Uint8Array} data - Binary representation of a Git block.
5353
* @returns {BitcoinBlock}
5454
*/
5555
exports.deserialize = (data) => {
56+
if (!Buffer.isBuffer(data)) {
57+
data = Buffer.from(data, data.byteOffset, data.byteLength)
58+
}
59+
5660
const headLen = gitUtil.find(data, 0)
5761
const head = data.slice(0, headLen).toString()
5862
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: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
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
@@ -39,6 +34,17 @@ describe('IPLD format util', () => {
3934
expect(deserialized).to.eql(expected)
4035
})
4136

37+
it('.serialize and .deserialize Uint8Array', () => {
38+
expect(Buffer.isBuffer(tagBlob)).to.be.true()
39+
const deserialized = ipldGit.util.deserialize(Uint8Array.from(tagBlob))
40+
41+
// The `gitType` is not enumerable, hence `eql()` would find it. Thus
42+
// remove that property so that that check passes
43+
const expected = Object.assign({}, tagNode)
44+
delete expected.gitType
45+
expect(deserialized).to.eql(expected)
46+
})
47+
4248
it('.cid', async () => {
4349
const cid = await ipldGit.util.cid(tagBlob)
4450
expect(cid.version).to.equal(1)

0 commit comments

Comments
 (0)