Skip to content
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

feat: add ts types #70

Merged
merged 14 commits into from
Dec 11, 2020
Merged
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v2
- run: yarn
- run: yarn lint
# - uses: gozala/typescript-error-reporter-action@v1.0.4
- uses: gozala/typescript-error-reporter-action@v1.0.4
- run: yarn build
- run: yarn aegir dep-check
- uses: ipfs/aegir/actions/bundle-size@master
Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@
> npm install multicodec
```

The type definitions for this package are available on http://definitelytyped.org/. To install just use:

```sh
$ npm install -D @types/multicodec
```

## Usage

### Example
Expand Down
9 changes: 0 additions & 9 deletions example.js

This file was deleted.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,9 @@
"Peter-Jan Brone <peter-jan@settlemint.com>",
"Vasco Santos <vasco.santos@moxy.studio>",
"Carson Farmer <carson.farmer@gmail.com>"
]
],
"types": "dist/src/index.d.ts",
"typesVersions": {
"*": { "src/*": ["dist/src/*", "dist/src/*/index"] }
}
hacdias marked this conversation as resolved.
Show resolved Hide resolved
}
9 changes: 6 additions & 3 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

const table = require('./base-table.json')

// map for codecConstant -> code
const constants = {}
const constants = /** @type {Record<CodecConstant, CodecNumber>} */({})

for (const [name, code] of Object.entries(table)) {
constants[name.toUpperCase().replace(/-/g, '_')] = code
const constant = /** @type {CodecConstant} */(name.toUpperCase().replace(/-/g, '_'))
constants[constant] = /** @type {CodecNumber} */(code)
}

module.exports = Object.freeze(constants)

/** @typedef {import('./types').CodecConstant} CodecConstant */
/** @typedef {import('./types').CodecNumber} CodecNumber */
56 changes: 34 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@ const codecNameToCodeVarint = require('./varint-table')
const util = require('./util')
const uint8ArrayConcat = require('uint8arrays/concat')

exports = module.exports

/**
* Prefix a buffer with a multicodec-packed.
*
* @param {string|number} multicodecStrOrCode
* @param {CodecName|Uint8Array} multicodecStrOrCode
* @param {Uint8Array} data
* @returns {Uint8Array}
*/
exports.addPrefix = (multicodecStrOrCode, data) => {
function addPrefix (multicodecStrOrCode, data) {
let prefix

// @ts-ignore: TS2358
if (multicodecStrOrCode instanceof Uint8Array) {
prefix = util.varintUint8ArrayEncode(multicodecStrOrCode)
} else {
Expand All @@ -48,7 +45,7 @@ exports.addPrefix = (multicodecStrOrCode, data) => {
* @param {Uint8Array} data
* @returns {Uint8Array}
*/
exports.rmPrefix = (data) => {
function rmPrefix (data) {
varint.decode(data)
return data.slice(varint.decode.bytes)
}
Expand All @@ -57,9 +54,9 @@ exports.rmPrefix = (data) => {
* Get the codec of the prefixed data.
*
* @param {Uint8Array} prefixedData
* @returns {string}
* @returns {CodecName}
*/
exports.getCodec = (prefixedData) => {
function getCodec (prefixedData) {
const code = varint.decode(prefixedData)
const codecName = intTable.get(code)
if (codecName === undefined) {
Expand All @@ -71,20 +68,20 @@ exports.getCodec = (prefixedData) => {
/**
* Get the name of the codec.
*
* @param {number} codec
* @returns {string}
* @param {CodecNumber} codec
* @returns {CodecName|undefined}
*/
exports.getName = (codec) => {
function getName (codec) {
return intTable.get(codec)
}

/**
* Get the code of the codec
*
* @param {string} name
* @returns {number}
* @param {CodecName} name
* @returns {CodecNumber}
*/
exports.getNumber = (name) => {
function getNumber (name) {
const code = codecNameToCodeVarint[name]
if (code === undefined) {
throw new Error('Codec `' + name + '` not found')
Expand All @@ -96,19 +93,19 @@ exports.getNumber = (name) => {
* Get the code of the prefixed data.
*
* @param {Uint8Array} prefixedData
* @returns {number}
* @returns {CodecNumber}
*/
exports.getCode = (prefixedData) => {
function getCode (prefixedData) {
return varint.decode(prefixedData)
}

/**
* Get the code as varint of a codec name.
*
* @param {string} codecName
* @param {CodecName} codecName
* @returns {Uint8Array}
*/
exports.getCodeVarint = (codecName) => {
function getCodeVarint (codecName) {
const code = codecNameToCodeVarint[codecName]
if (code === undefined) {
throw new Error('Codec `' + codecName + '` not found')
Expand All @@ -119,16 +116,31 @@ exports.getCodeVarint = (codecName) => {
/**
* Get the varint of a code.
*
* @param {number} code
* @param {CodecNumber} code
* @returns {Array.<number>}
*/
exports.getVarint = (code) => {
function getVarint (code) {
return varint.encode(code)
}

// Make the constants top-level constants
const constants = require('./constants')
Object.assign(exports, constants)

// Human friendly names for printing, e.g. in error messages
exports.print = require('./print')
const print = require('./print')

module.exports = {
addPrefix,
rmPrefix,
getCodec,
getName,
getNumber,
getCode,
getCodeVarint,
getVarint,
print,
...constants
}

/** @typedef {import('./types').CodecName} CodecName */
/** @typedef {import('./types').CodecNumber} CodecNumber */
hacdias marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 8 additions & 2 deletions src/int-table.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
'use strict'

const baseTable = require('./base-table.json')

// map for hexString -> codecName
/**
* @type {Map<CodecNumber,CodecName>}
*/
const nameTable = new Map()

for (const encodingName in baseTable) {
const code = baseTable[encodingName]
nameTable.set(code, encodingName)
nameTable.set(code, /** @type {CodecName} */(encodingName))
}

module.exports = Object.freeze(nameTable)

/** @typedef {import('./types').CodecName} CodecName */
/** @typedef {import('./types').CodecNumber} CodecNumber */
hacdias marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 7 additions & 3 deletions src/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

const table = require('./base-table.json')

// map for code -> print friendly name
const tableByCode = {}
const tableByCode = /** @type {Record<CodecNumber,CodecName>} */({})

for (const [name, code] of Object.entries(table)) {
if (tableByCode[code] === undefined) tableByCode[code] = name
if (tableByCode[code] === undefined) {
tableByCode[/** @type {CodecNumber} */(code)] = /** @type {CodecName} */(name)
}
}

module.exports = Object.freeze(tableByCode)

/** @typedef {import('./types').CodecName} CodecName */
/** @typedef {import('./types').CodecNumber} CodecNumber */
Loading