From 0e9d9f392d988fb46eb9be7fa562311116d2a2eb Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 19 Nov 2021 12:37:15 +0000 Subject: [PATCH] feat: convert to typescript Converts to typescript, adds github actions. BREAKING CHANGE: ESM and named exports only --- .github/workflows/main.yml | 57 +++++++++++++++++++ .travis.yml | 53 ----------------- README.md | 13 ++--- package.json | 35 ++++++------ src/{index.js => index.ts} | 7 ++- ...decoder.spec.js => varint-decoder.spec.ts} | 25 ++++---- tsconfig.json | 12 ++++ 7 files changed, 107 insertions(+), 95 deletions(-) create mode 100644 .github/workflows/main.yml delete mode 100644 .travis.yml rename src/{index.js => index.ts} (65%) rename test/{varint-decoder.spec.js => varint-decoder.spec.ts} (65%) create mode 100644 tsconfig.json diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..6c22e94 --- /dev/null +++ b/.github/workflows/main.yml @@ -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 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 534297f..0000000 --- a/.travis.yml +++ /dev/null @@ -1,53 +0,0 @@ -anguage: node_js -cache: npm -stages: - - check - - test - - cov - -node_js: - - '12' - - '10' - -os: - - linux - - osx - - windows - -script: npx nyc -s npm run test:node -- --bail -after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov - -jobs: - include: - - stage: check - script: - - npx aegir commitlint --travis - - npx aegir dep-check - - npm run lint - - - stage: test - name: chrome - addons: - chrome: stable - script: npx aegir test -t browser -t webworker - - - 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 - -notifications: - email: false \ No newline at end of file diff --git a/README.md b/README.md index 5dcaadc..462f958 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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] diff --git a/package.json b/package.json index cdf4d6d..c68153d 100644 --- a/package.json +++ b/package.json @@ -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", @@ -38,9 +37,9 @@ "author": "David Dias (mail@daviddias.me)", "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 ", "achingbrain ", diff --git a/src/index.js b/src/index.ts similarity index 65% rename from src/index.js rename to src/index.ts index ca155a1..2fffc9c 100644 --- a/src/index.js +++ b/src/index.ts @@ -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') } diff --git a/test/varint-decoder.spec.js b/test/varint-decoder.spec.ts similarity index 65% rename from test/varint-decoder.spec.js rename to test/varint-decoder.spec.ts index f2977fc..fbc8db5 100644 --- a/test/varint-decoder.spec.js +++ b/test/varint-decoder.spec.ts @@ -1,37 +1,34 @@ -/* 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) @@ -39,13 +36,13 @@ types.forEach(({ name, fromHexString }) => { 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) diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..f296f99 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "aegir/src/config/tsconfig.aegir.json", + "compilerOptions": { + "outDir": "dist", + "emitDeclarationOnly": false, + "module": "ES2020" + }, + "include": [ + "src", + "test" + ] +}