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

Commit

Permalink
fix: replace node buffers with uint8arrays (#70)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
achingbrain authored Aug 4, 2020
1 parent a5d4094 commit 8c999ba
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 34 deletions.
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,15 @@
"homepage": "https://github.com/ipld/js-ipld-git",
"dependencies": {
"buffer": "^5.6.0",
"cids": "^0.8.3",
"multicodec": "^1.0.2",
"multihashing-async": "^1.0.0",
"cids": "^1.0.0",
"multicodec": "^2.0.0",
"multihashing-async": "^2.0.0",
"smart-buffer": "^4.1.0",
"strftime": "^0.10.0"
"strftime": "^0.10.0",
"uint8arrays": "^1.0.0"
},
"devDependencies": {
"aegir": "^25.0.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.1"
"aegir": "^25.0.0"
},
"contributors": [
"Volker Mische <volker.mische@gmail.com>",
Expand Down
7 changes: 3 additions & 4 deletions src/resolver.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const CID = require('cids')
const { Buffer } = require('buffer')

const util = require('./util')

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

const traverse = function * (node, path) {
// Traverse only objects and arrays
if (Buffer.isBuffer(node) || CID.isCID(node) || typeof node === 'string' ||
if (node instanceof Uint8Array || CID.isCID(node) || typeof node === 'string' ||
node === null) {
return
}
Expand All @@ -61,7 +60,7 @@ const traverse = function * (node, path) {
* Return all available paths of a block.
*
* @generator
* @param {Buffer} binaryBlob - Binary representation of a Bitcoin block
* @param {Uint8Array} binaryBlob - Binary representation of a Bitcoin block
* @yields {string} - A single path
*/
exports.tree = function * (binaryBlob) {
Expand Down
13 changes: 9 additions & 4 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const multihashing = require('multihashing-async')
const CID = require('cids')
const multicodec = require('multicodec')
const { Buffer } = require('buffer')
const uint8ArrayToString = require('uint8arrays/to-string')

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

Expand All @@ -20,15 +21,15 @@ exports.defaultHashAlg = multicodec.SHA1
* Serialize internal representation into a binary Git block.
*
* @param {GitBlock} dagNode - Internal representation of a Git block
* @returns {Buffer}
* @returns {Uint8Array}
*/
exports.serialize = (dagNode) => {
if (dagNode === null) {
throw new Error('dagNode passed to serialize was null')
}

if (Buffer.isBuffer(dagNode)) {
if (dagNode.slice(0, 4).toString() === 'blob') {
if (dagNode instanceof Uint8Array) {
if (uint8ArrayToString(dagNode.slice(0, 4)) === 'blob') {
return dagNode
} else {
throw new Error('unexpected dagNode passed to serialize')
Expand All @@ -49,10 +50,14 @@ exports.serialize = (dagNode) => {
/**
* Deserialize Git block into the internal representation.
*
* @param {Buffer} data - Binary representation of a Git block.
* @param {Uint8Array} data - Binary representation of a Git block.
* @returns {BitcoinBlock}
*/
exports.deserialize = (data) => {
if (!Buffer.isBuffer(data)) {
data = Buffer.from(data, data.byteOffset, data.byteLength)
}

const headLen = gitUtil.find(data, 0)
const head = data.slice(0, headLen).toString()
const typeLen = head.match(/([^ ]+) (\d+)/)
Expand Down
5 changes: 1 addition & 4 deletions test/mod.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const multicodec = require('multicodec')

const mod = require('../src')
Expand Down
5 changes: 1 addition & 4 deletions test/parse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const { Buffer } = require('buffer')
const loadFixture = require('aegir/fixtures')
const zlib = require('zlib')
Expand Down
5 changes: 1 addition & 4 deletions test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const { Buffer } = require('buffer')

const CID = require('cids')
Expand Down
26 changes: 20 additions & 6 deletions test/util.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const chaiAsProised = require('chai-as-promised')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(chaiAsProised)
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const ipldGit = require('../src')
const multicodec = require('multicodec')
const multihash = require('multihashing-async').multihash
const CID = require('cids')
const { Buffer } = require('buffer')
const uint8ArrayFromString = require('uint8arrays/from-string')

describe('IPLD format util', () => {
const tagNode = {
Expand All @@ -28,6 +24,13 @@ describe('IPLD format util', () => {
}
const tagBlob = ipldGit.util.serialize(tagNode)

it('.serialize from Uint8Array', () => {
const node = uint8ArrayFromString('blob-blob')
const blob = ipldGit.util.serialize(node)

expect(blob).to.deep.equal(node)
})

it('.serialize and .deserialize', () => {
expect(Buffer.isBuffer(tagBlob)).to.be.true()
const deserialized = ipldGit.util.deserialize(tagBlob)
Expand All @@ -39,6 +42,17 @@ describe('IPLD format util', () => {
expect(deserialized).to.eql(expected)
})

it('.serialize and .deserialize Uint8Array', () => {
expect(Buffer.isBuffer(Uint8Array.of(...tagBlob))).to.be.false()
const deserialized = ipldGit.util.deserialize(Uint8Array.of(...tagBlob))

// The `gitType` is not enumerable, hence `eql()` would find it. Thus
// remove that property so that that check passes
const expected = Object.assign({}, tagNode)
delete expected.gitType
expect(deserialized).to.eql(expected)
})

it('.cid', async () => {
const cid = await ipldGit.util.cid(tagBlob)
expect(cid.version).to.equal(1)
Expand Down

0 comments on commit 8c999ba

Please sign in to comment.