Skip to content

Commit

Permalink
fix: use is-buffer
Browse files Browse the repository at this point in the history
Buffer is not available in the browser and bundlers are going to stop injecting node globals in the bundles.

Included is also some repo improvements to use the latest version of ipfs tooling.
  • Loading branch information
hugomrdias authored and daviddias committed Apr 24, 2020
1 parent f046f3a commit fefa84b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 66 deletions.
79 changes: 47 additions & 32 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,38 +1,53 @@
sudo: false
language: node_js
anguage: node_js
cache: npm
stages:
- check
- test
- cov

matrix:
include:
- node_js: 4
env: CXX=g++-4.8
- node_js: 6
env:
- SAUCE=true
- CXX=g++-4.8
- node_js: stable
env: CXX=g++-4.8
node_js:
- '12'
- '10'

os:
- linux
- osx
- windows

# Make sure we have new NPM.
before_install:
- npm install -g npm
script: npx nyc -s npm run test:node -- --bail
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov

script:
- npm run lint
- npm test
- npm run coverage
- make test
jobs:
include:
- stage: check
script:
- npx aegir commitlint --travis
- npx aegir dep-check
- npm run lint

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- stage: test
name: chrome
addons:
chrome: stable
script: npx aegir test -t browser -t webworker

after_success:
- npm run coverage-publish
- stage: test
name: firefox
addons:
firefox: latest
script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless

- stage: test
name: electron-main
os: osx
script:
- npx aegir test -t electron-main --bail

- stage: test
name: electron-renderer
os: osx
script:
- npx aegir test -t electron-renderer --bail

addons:
firefox: 'latest'
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
notifications:
email: false
12 changes: 0 additions & 12 deletions circle.yml

This file was deleted.

28 changes: 15 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"description": "Parse all the varints in a Buffer (for when there are varints everywhere)",
"main": "src/index.js",
"scripts": {
"lint": "aegir-lint",
"build": "aegir-build",
"test": "aegir-test",
"test:node": "aegir-test --env node",
"test:browser": "aegir-test --env browser",
"release": "aegir-release",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
"lint": "aegir lint",
"build": "aegir build",
"test": "aegir test",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser",
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
"coverage": "aegir coverage",
"coverage-publish": "aegir coverage publish"
},
"pre-commit": [
"lint",
Expand All @@ -24,11 +24,13 @@
"npm": ">=3.0.0"
},
"dependencies": {
"is-buffer": "^2.0.4",
"varint": "^5.0.0"
},
"devDependencies": {
"aegir": "^9.3.0",
"chai": "^3.5.0",
"aegir": "^21.9.0",
"buffer": "^5.6.0",
"chai": "^4.2.0",
"pre-commit": "^1.2.2"
},
"repository": {
Expand All @@ -48,4 +50,4 @@
"contributors": [
"David Dias <daviddias.p@gmail.com>"
]
}
}
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict'

const varint = require('varint')
const isBuffer = require('is-buffer')

module.exports = (buf) => {
if (!Buffer.isBuffer(buf)) {
if (!isBuffer(buf)) {
throw new Error('arg needs to be a buffer')
}

let result = []
const result = []

while (buf.length > 0) {
const num = varint.decode(buf)
Expand Down
13 changes: 6 additions & 7 deletions test/varint-decoder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,43 @@
'use strict'

const expect = require('chai').expect

const { Buffer } = require('buffer')
const vd = require('../src')

describe('varint-decoder', () => {
it('decode 1 varint', () => {
const buf = new Buffer('05', 'hex')
const buf = Buffer.from('05', 'hex')
const arr = vd(buf)
expect(arr[0]).to.equal(5)
})

it('decode 2 varints', () => {
const buf = new Buffer('000a', 'hex')
const buf = Buffer.from('000a', 'hex')
const arr = vd(buf)
expect(arr[0]).to.equal(0)
expect(arr[1]).to.equal(10)
})

it('decode 3 varints', () => {
const buf = new Buffer('0b0c03', 'hex')
const buf = Buffer.from('0b0c03', 'hex')
const arr = vd(buf)
expect(arr[0]).to.equal(11)
expect(arr[1]).to.equal(12)
expect(arr[2]).to.equal(3)
})

it('decode 1 long varint', () => {
const buf = new Buffer('c801', 'hex')
const buf = Buffer.from('c801', 'hex')
const arr = vd(buf)
expect(arr[0]).to.equal(200)
})

it('decode a mix of long and short', () => {
const buf = new Buffer('96130208b90a', 'hex')
const buf = Buffer.from('96130208b90a', 'hex')
const arr = vd(buf)
expect(arr[0]).to.equal(2454)
expect(arr[1]).to.equal(2)
expect(arr[2]).to.equal(8)
expect(arr[3]).to.equal(1337)
})
})

0 comments on commit fefa84b

Please sign in to comment.