Skip to content
This repository has been archived by the owner on Aug 24, 2021. It is now read-only.

feat: add ts support #54

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:
include:
- stage: check
script:
- npx aegir commitlint --travis
- npx aegir dep-check
- npm run lint
- npx aegir ts --preset check
- npx aegir dep-check

- stage: test
name: chrome
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ js-multibase
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai)
[![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23ipfs)
[![Dependency Status](https://david-dm.org/multiformats/js-multibase.svg?style=flat-square)](https://david-dm.org/multiformats/js-multibase)
[![codecov](https://img.shields.io/codecov/c/github/multiformats/js-multibase.svg?style=flat-square)](https://codecov.io/gh/multiformats/js-multibase)
[![Travis CI](https://flat.badgen.net/travis/multiformats/js-multibase)](https://travis-ci.com/multiformats/js-multibase)

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"formats"
],
"devDependencies": {
"aegir": "^22.0.0",
"aegir": "ipfs/aegir#feat/typescript-support",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"pre-commit": "^1.2.2"
Expand All @@ -43,6 +43,9 @@
"base-x": "^3.0.8",
"buffer": "^5.5.0"
},
"eslintConfig": {
"extends": "./node_modules/aegir/src/config/eslintrc.js"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/multiformats/js-multibase/issues"
Expand Down
42 changes: 34 additions & 8 deletions src/base.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
'use strict'
const { Buffer } = require('buffer')

/** @typedef {import("./types").BaseConstructor} BaseConstructor */
/** @typedef {import("./types").BaseNames} BaseNames */
/** @typedef {import("./types").BaseCodes} BaseCodes */

/**
* Class to handle base encode/decode
*/
class Base {
/**
* Base class
*
* @param {BaseCodes | BaseNames} name - base name
* @param {string} code - base code
* @param {BaseConstructor} implementation - base engine
* @param {string} alphabet
*/
constructor (name, code, implementation, alphabet) {
this.name = name
this.code = code
/** @internal */
this.codeBuffer = Buffer.from(code)
this.alphabet = alphabet
if (implementation && alphabet) {
this.engine = implementation(alphabet)
}
}

encode (stringOrBuffer) {
return this.engine.encode(stringOrBuffer)
}

decode (stringOrBuffer) {
return this.engine.decode(stringOrBuffer)
/**
* Encode value
*
* @param {Buffer | Uint8Array} value - Value to encode.
* @returns {string} Encoded value.
*/
encode (value) {
return this.engine.encode(value)
}

isImplemented () {
return this.engine
/**
* Decode value
*
* @param {string} value - Value to decode.
* @returns {Buffer | Uint8Array} Value decoded.
*/
decode (value) {
return this.engine.decode(value)
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/base16.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
'use strict'
const { Buffer } = require('buffer')

module.exports = function base16 (alphabet) {
/** @typedef {import("./types").BaseInterface} BaseInterface */

/**
* Base 16
*
* @internal
* @param {string} alphabet
* @returns {BaseInterface}
*/
const base16 = (alphabet) => {
return {
encode (input) {
if (typeof input === 'string') {
Expand All @@ -19,3 +28,5 @@ module.exports = function base16 (alphabet) {
}
}
}

module.exports = base16
38 changes: 32 additions & 6 deletions src/base32.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
'use strict'

/** @typedef {import("./types").BaseInterface} BaseInterface */
/** @typedef {import("./types").BaseConstructor} BaseConstructor */

/**
*
* @internal
* @param {string} input
* @param {string} alphabet
* @returns {Uint8Array}
*/
function decode (input, alphabet) {
input = input.replace(new RegExp('=', 'g'), '')
const length = input.length
Expand All @@ -20,9 +29,17 @@ function decode (input, alphabet) {
}
}

return output.buffer
return output
}

/**
* Encode
*
* @internal
* @param {Buffer|Uint8Array} buffer
* @param {string} alphabet
* @returns {string}
*/
function encode (buffer, alphabet) {
const length = buffer.byteLength
const view = new Uint8Array(buffer)
Expand Down Expand Up @@ -59,12 +76,19 @@ function encode (buffer, alphabet) {
return output
}

module.exports = function base32 (alphabet) {
/**
*
* Base 32
*
* @type {BaseConstructor}
* @internal
*/
const base32 = (alphabet) => {
return {
encode (input) {
if (typeof input === 'string') {
return encode(Uint8Array.from(input), alphabet)
}
// if (typeof input === 'string') {
// return encode(Uint8Array.from(input), alphabet)
// }

return encode(input, alphabet)
},
Expand All @@ -79,3 +103,5 @@ module.exports = function base32 (alphabet) {
}
}
}

module.exports = base32
4 changes: 3 additions & 1 deletion src/base64.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'
const { Buffer } = require('buffer')

module.exports = function base64 (alphabet) {
const base64 = (alphabet) => {
// The alphabet is only used to know:
// 1. If padding is enabled (must contain '=')
// 2. If the output must be url-safe (must contain '-' and '_')
Expand Down Expand Up @@ -42,3 +42,5 @@ module.exports = function base64 (alphabet) {
}
}
}

module.exports = base64
31 changes: 20 additions & 11 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
'use strict'

const Base = require('./base.js')
const baseX = require('base-x')
const base16 = require('./base16')
const base32 = require('./base32')
const base64 = require('./base64')

// name, code, implementation, alphabet
const constants = [
['base1', '1', '', '1'],
/** @typedef {import("./types").BaseNames} BaseNames */
/** @typedef {import("./types").BaseCodes} BaseCodes */
/** @typedef {import("./types").BaseCodesMap} BaseCodesMap */
/** @typedef {import("./types").BaseNamesMap} BaseNamesMap */
/** @typedef {import("./types").BaseConstructor} BaseConstructor */

/**
* @internal
* @type {Array<[BaseNames, BaseCodes, BaseConstructor, string]>} - Name, Code, Engine, Alphabet
*/
const bases = [
['base2', '0', baseX, '01'],
['base8', '7', baseX, '01234567'],
['base10', '9', baseX, '0123456789'],
Expand All @@ -26,17 +33,19 @@ const constants = [
['base64urlpad', 'U', base64, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=']
]

const names = constants.reduce((prev, tupple) => {
prev[tupple[0]] = new Base(tupple[0], tupple[1], tupple[2], tupple[3])
/** @type {BaseNamesMap} */
const names = bases.reduce((prev, value) => {
prev[value[0]] = new Base(value[0], value[1], value[2], value[3])
return prev
}, {})
}, /** @type {BaseNamesMap} */({}))

const codes = constants.reduce((prev, tupple) => {
/** @type {BaseCodesMap} */
const codes = bases.reduce((prev, tupple) => {
prev[tupple[1]] = names[tupple[0]]
return prev
}, {})
}, /** @type {BaseCodesMap} */({}))

module.exports = {
names: names,
codes: codes
names,
codes
}
54 changes: 31 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
/**
* Implementation of the [multibase](https://github.com/multiformats/multibase) specification.
*
* @packageDocumentation
* @module Multibase
*/
'use strict'

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

exports = module.exports = multibase
exports.encode = encode
exports.decode = decode
exports.isEncoded = isEncoded
exports.names = Object.freeze(Object.keys(constants.names))
exports.codes = Object.freeze(Object.keys(constants.codes))
/** @typedef {import("./base")} Base */
/** @typedef {import("./types").BaseCodes} BaseCodes */
/** @typedef {import("./types").BaseNames} BaseNames */

/**
* Create a new buffer with the multibase varint+code.
*
* @param {string|number} nameOrCode - The multibase name or code number.
* @param {BaseCodes|BaseNames} nameOrCode - The multibase name or code number.
* @param {Buffer} buf - The data to be prefixed with multibase.
* @memberof Multibase
* @returns {Buffer}
*/
function multibase (nameOrCode, buf) {
Expand All @@ -37,10 +35,9 @@ function multibase (nameOrCode, buf) {
/**
* Encode data with the specified base and add the multibase prefix.
*
* @param {string|number} nameOrCode - The multibase name or code number.
* @param {BaseCodes|BaseNames} nameOrCode - The multibase name or code number.
* @param {Buffer} buf - The data to be encoded.
* @returns {Buffer}
* @memberof Multibase
*/
function encode (nameOrCode, buf) {
const base = getBase(nameOrCode)
Expand All @@ -55,15 +52,13 @@ function encode (nameOrCode, buf) {
*
* @param {Buffer|string} bufOrString
* @returns {Buffer}
* @memberof Multibase
*
*/
function decode (bufOrString) {
if (Buffer.isBuffer(bufOrString)) {
bufOrString = bufOrString.toString()
}

const code = bufOrString.substring(0, 1)
const code = /** @type {BaseCodes} */(bufOrString.substring(0, 1))
bufOrString = bufOrString.substring(1, bufOrString.length)

if (typeof bufOrString === 'string') {
Expand All @@ -78,8 +73,7 @@ function decode (bufOrString) {
* Is the given data multibase encoded?
*
* @param {Buffer|string} bufOrString
* @returns {boolean}
* @memberof Multibase
* @returns {boolean|string}
*/
function isEncoded (bufOrString) {
if (Buffer.isBuffer(bufOrString)) {
Expand All @@ -91,7 +85,7 @@ function isEncoded (bufOrString) {
return false
}

const code = bufOrString.substring(0, 1)
const code = /** @type {BaseCodes} */(bufOrString.substring(0, 1))
try {
const base = getBase(code)
return base.name
Expand All @@ -101,30 +95,44 @@ function isEncoded (bufOrString) {
}

/**
* @param {string} name
* Check if encoding is valid
*
* @param {BaseCodes|BaseNames} name
* @param {Buffer} buf
* @private
* @returns {undefined}
* @returns {void}
*/
function validEncode (name, buf) {
const base = getBase(name)
base.decode(buf.toString())
}

/**
* Get base to encode/decode without prefix
*
* @param {BaseCodes|BaseNames} nameOrCode
* @returns { Base }
*/
function getBase (nameOrCode) {
/** @type {Base} */
let base

if (constants.names[nameOrCode]) {
base = constants.names[nameOrCode]
} else if (constants.codes[nameOrCode]) {
base = constants.codes[nameOrCode]
} else {
throw new Error('Unsupported encoding')
}

if (!base.isImplemented()) {
throw new Error('Base ' + nameOrCode + ' is not implemented yet')
throw new Error(`Unsupported encoding: ${nameOrCode}`)
}

return base
}

module.exports = multibase

multibase.encode = encode
multibase.decode = decode
multibase.isEncoded = isEncoded
multibase.names = Object.freeze(Object.keys(constants.names))
multibase.codes = Object.freeze(Object.keys(constants.codes))
multibase.getBase = getBase
Loading