Skip to content

feat: convert to typescript #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: ci
on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- run: npm install
- run: npm run build
- run: npm run lint
- run: npm run dep-check
test-node:
needs: check
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
node: [16]
fail-fast: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm run test:node -- -- --bail
- uses: codecov/codecov-action@v1
test-chrome:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- run: npm install
- run: npm run test:browser -- -- --bail
test-firefox:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- run: npm install
- run: npm run test:browser -- -- --bail --browser firefox
53 changes: 0 additions & 53 deletions .travis.yml

This file was deleted.

13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ varint-decoder
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Coverage Status](https://coveralls.io/repos/github/diasdavid/varint-decoder/badge.svg?branch=master)](https://coveralls.io/github/diasdavid/varint-decoder?branch=master)
[![Travis CI](https://travis-ci.org/diasdavid/varint-decoder.svg?branch=master)](https://travis-ci.org/diasdavid/varint-decoder)
[![Circle CI](https://circleci.com/gh/diasdavid/varint-decoder.svg?style=svg)](https://circleci.com/gh/diasdavid/varint-decoder)
[![Dependency Status](https://david-dm.org/diasdavid/varint-decoder.svg?style=flat-square)](https://david-dm.org/diasdavid/varint-decoder) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
[![Coverage Status](https://coveralls.io/repos/github/ipfs-shipyard/varint-decoder/badge.svg?branch=master)](https://coveralls.io/github/ipfs-shipyard/varint-decoder?branch=master)
[![Dependency Status](https://david-dm.org/ipfs-shipyard/varint-decoder.svg?style=flat-square)](https://david-dm.org/ipfs-shipyard/varint-decoder) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)

Expand All @@ -18,10 +16,11 @@ varint-decoder
## API

```JavaScript
const vd = require('varint-decoder')
const buf = new Buffer('000110', 'hex')
import { varintDecoder } from 'varint-decoder'
import { Buffer } from 'buffer'

const decoded = vd(buf)
const buf = new Buffer('000110', 'hex')
const decoded = varintDecoder(buf)

console.log(decoded)
// [0, 1, 16]
Expand Down
35 changes: 17 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,33 @@
"name": "varint-decoder",
"version": "1.0.0",
"description": "Parse all the varints in a Buffer (for when there are varints everywhere)",
"main": "src/index.js",
"type": "module",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"scripts": {
"lint": "aegir lint",
"build": "aegir build",
"test": "aegir test",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser",
"build": "tsc",
"pretest": "npm run build",
"test": "aegir test -f ./dist/test/*.js",
"test:node": "aegir test -t node -f ./dist/test/*.js",
"test:browser": "aegir test -t browser-f ./dist/test/*.js ",
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
"coverage": "aegir coverage",
"coverage-publish": "aegir coverage publish"
},
"engines": {
"node": ">=4.0.0",
"npm": ">=3.0.0"
"coverage": "nyc --reporter=text --reporter=lcov npm run test:node"
},
"dependencies": {
"varint": "^5.0.0"
"varint": "^6.0.0"
},
"devDependencies": {
"aegir": "^25.0.0",
"buffer": "^5.6.0",
"uint8arrays": "^1.1.0"
"@types/varint": "^6.0.0",
"aegir": "^36.0.0",
"buffer": "^6.0.3",
"uint8arrays": "^3.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/diasdavid/varint-decoder.git"
"url": "git+https://github.com/ipfs-shipyard/varint-decoder.git"
},
"keywords": [
"IPFS",
Expand All @@ -38,9 +37,9 @@
"author": "David Dias ([email protected])",
"license": "MIT",
"bugs": {
"url": "https://github.com/diasdavid/varint-decoder/issues"
"url": "https://github.com/ipfs-shipyard/varint-decoder/issues"
},
"homepage": "https://github.com/diasdavid/varint-decoder#readme",
"homepage": "https://github.com/ipfs-shipyard/varint-decoder#readme",
"contributors": [
"David Dias <[email protected]>",
"achingbrain <[email protected]>",
Expand Down
7 changes: 4 additions & 3 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict'
import * as v from 'varint'

const varint = require('varint')
// @ts-expect-error types are wrong
const varint = v.default

module.exports = (buf) => {
export function varintDecoder (buf: Uint8Array) {
if (!(buf instanceof Uint8Array)) {
throw new Error('arg needs to be a Uint8Array')
}
Expand Down
25 changes: 11 additions & 14 deletions test/varint-decoder.spec.js → test/varint-decoder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
/* eslint-env mocha */
'use strict'

const { expect } = require('aegir/utils/chai')
const { Buffer } = require('buffer')
const uint8ArrayFromString = require('uint8arrays/from-string')
const vd = require('../src')
import { expect } from 'aegir/utils/chai.js'
import { Buffer } from 'buffer'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { varintDecoder } from '../src/index.js'

const types = [{
name: 'Buffer',
fromHexString: (hex) => Buffer.from(hex, 'hex')
fromHexString: (hex: string) => Buffer.from(hex, 'hex')
}, {
name: 'Uint8Array',
fromHexString: (hex) => uint8ArrayFromString(hex, 'base16')
fromHexString: (hex: string) => uint8ArrayFromString(hex, 'base16')
}]

types.forEach(({ name, fromHexString }) => {
describe(`varint-decoder (${name})`, () => {
it('decode 1 varint', () => {
const buf = fromHexString('05')
const arr = vd(buf)
const arr = varintDecoder(buf)
expect(arr[0]).to.equal(5)
})

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

it('decode 3 varints', () => {
const buf = fromHexString('0b0c03')
const arr = vd(buf)
const arr = varintDecoder(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 = fromHexString('c801')
const arr = vd(buf)
const arr = varintDecoder(buf)
expect(arr[0]).to.equal(200)
})

it('decode a mix of long and short', () => {
const buf = fromHexString('96130208b90a')
const arr = vd(buf)
const arr = varintDecoder(buf)
expect(arr[0]).to.equal(2454)
expect(arr[1]).to.equal(2)
expect(arr[2]).to.equal(8)
Expand Down
12 changes: 12 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist",
"emitDeclarationOnly": false,
"module": "ES2020"
},
"include": [
"src",
"test"
]
}