From 8774a4fd0f88280955a6cb4be38bad2d1d6d3023 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Thu, 7 Sep 2017 11:45:24 +0200 Subject: [PATCH] feat: replace protocol-buffers with protons (#5) * feat: replace protocol-buffers with protons Ref https://github.com/ipfs/js-ipfs/issues/991 * feat: upgrade to aegir@12 * chore: fix coverage command --- .gitignore | 3 +++ .travis.yml | 6 +----- package.json | 33 +++++++++++++++------------------ src/record.js | 5 ++--- src/selection.js | 2 +- src/validator.js | 4 ++-- test/fixtures/go-key-records.js | 2 +- test/fixtures/go-record.js | 4 ++-- test/selection.spec.js | 12 ++++++------ test/validator.spec.js | 1 - 10 files changed, 33 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index e4c5b0e..d467d2b 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,6 @@ test/test-data/go-ipfs-repo/LOG.old # while testing npm5 package-lock.json +yarn.lock + +docs \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 584f308..5a9d289 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,16 +13,12 @@ matrix: script: - npm run lint - npm run test - - npm run coverage - - make test + - npm run coverage -- --upload before_script: - export DISPLAY=:99.0 - sh -e /etc/init.d/xvfb start -after_success: - - npm run coverage-publish - addons: firefox: 'latest' apt: diff --git a/package.json b/package.json index ec64a8f..49adb41 100644 --- a/package.json +++ b/package.json @@ -4,17 +4,16 @@ "description": "libp2p record implementation", "main": "src/index.js", "scripts": { - "test": "aegir-test", - "lint": "aegir-lint", - "test:node": "aegir-test node", - "test:browser": "aegir-test browser", - "build": "aegir-build", - "docs": "aegir-docs", - "release": "aegir-release --docs", - "release-minor": "aegir-release --type minor --docs", - "release-major": "aegir-release --type major --docs", - "coverage": "aegir-coverage", - "coverage-publish": "aegir-coverage publish" + "test": "aegir test", + "lint": "aegir lint", + "test:node": "aegir test -t node", + "test:browser": "aegir test -t browser -t webworker", + "build": "aegir build", + "docs": "aegir docs", + "release": "aegir release", + "release-minor": "aegir release --type minor", + "release-major": "aegir release --type major", + "coverage": "aegir coverage" }, "repository": { "type": "git", @@ -38,12 +37,11 @@ }, "homepage": "https://github.com/libp2p/js-libp2p-record", "devDependencies": { - "aegir": "^11.0.2", + "aegir": "^12.0.6", "chai": "^4.1.2", "dirty-chai": "^2.0.1", - "libp2p-crypto": "~0.10.0", - "pre-commit": "^1.2.2", - "safe-buffer": "^5.1.1" + "libp2p-crypto": "~0.10.2", + "pre-commit": "^1.2.2" }, "dependencies": { "async": "^2.5.0", @@ -52,11 +50,10 @@ "multihashes": "~0.4.9", "multihashing-async": "~0.4.6", "peer-id": "~0.10.0", - "protocol-buffers": "^3.2.1", - "safe-buffer": "^5.1.1" + "protons": "^1.0.0" }, "contributors": [ "David Dias ", "Friedel Ziegelmayer " ] -} \ No newline at end of file +} diff --git a/src/record.js b/src/record.js index c6749ec..b160052 100644 --- a/src/record.js +++ b/src/record.js @@ -1,11 +1,10 @@ 'use strict' -const protobuf = require('protocol-buffers') -const Buffer = require('safe-buffer').Buffer +const protons = require('protons') const assert = require('assert') const PeerId = require('peer-id') -const pb = protobuf(require('./record.proto')).Record +const pb = protons(require('./record.proto')).Record const utils = require('./utils') class Record { diff --git a/src/selection.js b/src/selection.js index 6b6dcac..06d7365 100644 --- a/src/selection.js +++ b/src/selection.js @@ -15,7 +15,7 @@ const bestRecord = (selectors, k, records) => { throw new Error('No records given') } - const parts = bsplit(k, new Buffer('/')) + const parts = bsplit(k, Buffer.from('/')) if (parts.length < 3) { throw new Error('Record key does not have a selector function') diff --git a/src/validator.js b/src/validator.js index 2cbccdb..951bfa6 100644 --- a/src/validator.js +++ b/src/validator.js @@ -13,7 +13,7 @@ const bsplit = require('buffer-split') */ const verifyRecord = (validators, record, callback) => { const key = record.key - const parts = bsplit(key, new Buffer('/')) + const parts = bsplit(key, Buffer.from('/')) if (parts.length < 3) { // No validator available @@ -37,7 +37,7 @@ const verifyRecord = (validators, record, callback) => { * @returns {boolean} */ const isSigned = (validators, key) => { - const parts = bsplit(key, new Buffer('/')) + const parts = bsplit(key, Buffer.from('/')) if (parts.length < 3) { // No validator available diff --git a/test/fixtures/go-key-records.js b/test/fixtures/go-key-records.js index ef0a2e1..4df45e1 100644 --- a/test/fixtures/go-key-records.js +++ b/test/fixtures/go-key-records.js @@ -1,7 +1,7 @@ 'use strict' module.exports = { - publicKey: new Buffer( + publicKey: Buffer.from( 'CAASXjBcMA0GCSqGSIb3DQEBAQUAA0sAMEgCQQDjXAQQMal4SB2tSnX6NJIPmC69/BT8A8jc7/gDUZNkEhdhYHvc7k7S4vntV/c92nJGxNdop9fKJyevuNMuXhhHAgMBAAE=', 'base64' ) diff --git a/test/fixtures/go-record.js b/test/fixtures/go-record.js index 6f5c380..d91b84d 100644 --- a/test/fixtures/go-record.js +++ b/test/fixtures/go-record.js @@ -18,11 +18,11 @@ // ioutil.WriteFile("js-libp2p-record/test/fixtures/record.bin", enc, 0644) // ioutil.WriteFile("js-libp2p-record/test/fixtures/record-signed.bin", enc2, 0644) module.exports = { - serialized: new Buffer( + serialized: Buffer.from( '0a0568656c6c6f1205776f726c641a2212201bd5175b1d4123ee29665348c60ea5cf5ac62e2e05215b97a7b9a9b0cf71d116', 'hex' ), - serializedSigned: new Buffer( + serializedSigned: Buffer.from( '0a0568656c6c6f1205776f726c641a2212201bd5175b1d4123ee29665348c60ea5cf5ac62e2e05215b97a7b9a9b0cf71d116228001500fe7505698b8a873ccde6f1d36a2be662d57807490d9a9959540f2645a454bf615215092e10123f6ffc4ed694711bfbb1d5ccb62f3da83cf4528ee577a96b6cf0272eef9a920bd56459993690060353b72c22b8c03ad2a33894522dac338905b201179a85cb5e2fc68ed58be96cf89beec6dc0913887dddc10f202a2a1b117', 'hex' ) diff --git a/test/selection.spec.js b/test/selection.spec.js index 0fe4768..85b3999 100644 --- a/test/selection.spec.js +++ b/test/selection.spec.js @@ -7,13 +7,13 @@ var expect = require('chai').expect const libp2pRecord = require('../src') const selection = libp2pRecord.selection -const records = [new Buffer(0), new Buffer('hello')] +const records = [Buffer.alloc(0), Buffer.from('hello')] describe('selection', () => { describe('bestRecord', () => { it('throws on missing selector in the record key', () => { expect( - () => selection.bestRecord({}, new Buffer('/'), records) + () => selection.bestRecord({}, Buffer.from('/'), records) ).to.throw( /Record key does not have a selector function/ ) @@ -21,7 +21,7 @@ describe('selection', () => { it('throws on unknown key prefix', () => { expect( - () => selection.bestRecord({world () {}}, new Buffer('/hello/'), records) + () => selection.bestRecord({world () {}}, Buffer.from('/hello/'), records) ).to.throw( /Unrecognized key prefix: hello/ ) @@ -30,7 +30,7 @@ describe('selection', () => { it('returns the index from the matching selector', () => { const selectors = { hello (k, recs) { - expect(k).to.be.eql(new Buffer('/hello/world')) + expect(k).to.be.eql(Buffer.from('/hello/world')) expect(recs).to.be.eql(records) return 1 @@ -38,7 +38,7 @@ describe('selection', () => { } expect( - selection.bestRecord(selectors, new Buffer('/hello/world'), records) + selection.bestRecord(selectors, Buffer.from('/hello/world'), records) ).to.equal( 1 ) @@ -48,7 +48,7 @@ describe('selection', () => { describe('selectors', () => { it('public key', () => { expect( - selection.selectors.pk(new Buffer('/hello/world'), records) + selection.selectors.pk(Buffer.from('/hello/world'), records) ).to.equal( 0 ) diff --git a/test/validator.spec.js b/test/validator.spec.js index 8b19919..6d5a667 100644 --- a/test/validator.spec.js +++ b/test/validator.spec.js @@ -9,7 +9,6 @@ const waterfall = require('async/waterfall') const each = require('async/each') const crypto = require('libp2p-crypto') const PeerId = require('peer-id') -const Buffer = require('safe-buffer').Buffer const libp2pRecord = require('../src') const validator = libp2pRecord.validator