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
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 committed Aug 4, 2020
1 parent a5d4094 commit 759e76d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 32 deletions.
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,14 @@
"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"
},
"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
10 changes: 7 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ 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 instanceof Uint8Array) {
if (dagNode.slice(0, 4).toString() === 'blob') {
return dagNode
} else {
Expand All @@ -49,10 +49,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
18 changes: 12 additions & 6 deletions test/util.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/* 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
Expand Down Expand Up @@ -39,6 +34,17 @@ describe('IPLD format util', () => {
expect(deserialized).to.eql(expected)
})

it('.serialize and .deserialize Uint8Array', () => {
expect(Buffer.isBuffer(tagBlob)).to.be.true()
const deserialized = ipldGit.util.deserialize(Uint8Array.from(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 759e76d

Please sign in to comment.