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

Commit 6e083f0

Browse files
hugomrdiasGozala
andauthored
feat: add ts types (#72)
Co-authored-by: Irakli Gozalishvili <[email protected]>
1 parent 40d2657 commit 6e083f0

16 files changed

+171
-111
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
- uses: actions/checkout@v2
1515
- run: yarn
1616
- run: yarn lint
17+
- run: yarn build
18+
- uses: gozala/[email protected]
1719
- run: yarn aegir dep-check -- -i aegir
1820
- uses: ipfs/aegir/actions/bundle-size@master
1921
name: size

README.md

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,27 @@
1-
js-multibase
1+
js-multibase <!-- omit in toc -->
22
============
33

44
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai)
55
[![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats)
66
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23ipfs)
7-
[![Dependency Status](https://david-dm.org/multiformats/js-multibase.svg?style=flat-square)](https://david-dm.org/multiformats/js-multibase)
87
[![codecov](https://img.shields.io/codecov/c/github/multiformats/js-multibase.svg?style=flat-square)](https://codecov.io/gh/multiformats/js-multibase)
9-
[![Travis CI](https://flat.badgen.net/travis/multiformats/js-multibase)](https://travis-ci.com/multiformats/js-multibase)
8+
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/multiformats/js-multibase/ci?label=ci&style=flat-square)](https://github.com/multiformats/js-multibase/actions?query=branch%3Amaster+workflow%3Aci+)
109

1110
> JavaScript implementation of the [multibase](https://github.com/multiformats/multibase) specification
1211
13-
## Lead Maintainer
12+
## Lead Maintainer <!-- omit in toc -->
1413

1514
[Hugo Dias](https://github.com/hugomrdias)
1615

17-
## Table of Contents
16+
## Table of Contents <!-- omit in toc -->
1817

1918
- [Install](#install)
2019
- [NPM](#npm)
21-
- [In the Browser through `<script>` tag](#in-the-browser-through-script-tag)
20+
- [Browser through `<script>` tag](#browser-through-script-tag)
2221
- [Usage](#usage)
2322
- [Example](#example)
2423
- [API](#api)
25-
- [`multibase` - Prefixes an encoded Uint8Array with its multibase code](#multibase---prefixes-an-encoded-Uint8Array-with-its-multibase-code)
26-
- [`multibase.encode` - Encodes Uint8Array into one of the supported encodings, prefixing it with the multibase code](#multibaseencode---encodes-Uint8Array-into-one-of-the-supported-encodings-prefixing-it-with-the-multibase-code)
27-
- [`multibase.decode` - Decodes Uint8Array or string](#multibasedecode---decodes-Uint8Array-or-string)
28-
- [`multibase.isEncoded` - Checks if Uint8Array or string is encoded](#multibaseisencoded---checks-if-Uint8Array-or-string-is-encoded)
29-
- [`multibase.names` - Supported base encoding names](#multibasenames)
30-
- [`multibase.codes` - Supported base encoding codes](#multibasecodes)
31-
- [Supported Encodings, see `src/constants.js`](#supported-encodings-see-srcconstantsjs)
32-
- [Architecture and Encoding/Decoding](#architecture-and-encodingdecoding)
33-
- [Adding additional bases](#adding-additional-bases)
24+
- [Contribute](#contribute)
3425
- [License](#license)
3526

3627
## Install
@@ -41,15 +32,9 @@ js-multibase
4132
$ npm install --save multibase
4233
```
4334

44-
The type definitions for this package are available on http://definitelytyped.org/. To install just use:
35+
### Browser through `<script>` tag
4536

46-
```sh
47-
$ npm install -D @types/multibase
48-
```
49-
50-
### In the Browser through `<script>` tag
51-
52-
Loading this module through a script tag will make the ```Multibase``` obj available in the global namespace.
37+
Loading this module through a script tag will make the `Multibase` obj available in the global namespace.
5338

5439
```html
5540
<script src="https://unpkg.com/multibase/dist/index.min.js"></script>
@@ -72,55 +57,6 @@ console.log(decodedBytes.toString())
7257
## API
7358
https://multiformats.github.io/js-multibase/
7459

75-
#### `multibase` - Prefixes an encoded Uint8Array with its multibase code
76-
77-
```
78-
const multibased = multibase(<nameOrCode>, encodedBytes)
79-
```
80-
81-
#### `multibase.encode` - Encodes Uint8Array into one of the supported encodings, prefixing it with the multibase code
82-
83-
```JavaScript
84-
const encodedBuf = multibase.encode(<nameOrCode>, <bytes>)
85-
```
86-
87-
#### `multibase.decode` - Decodes Uint8Array or string
88-
89-
```JavaScript
90-
const decodedBuf = multibase.decode(bufOrString)
91-
```
92-
93-
#### `multibase.isEncoded` - Checks if Uint8Array or string is encoded
94-
95-
```JavaScript
96-
const value = multibase.isEncoded(bytesOrString)
97-
// value is the name of the encoding if it is encoded, false otherwise
98-
```
99-
100-
#### `multibase.encoding` - Get the encoding by name or code
101-
102-
```JavaScript
103-
const value = multibase.encoding(nameOrCode)
104-
// value is an instance of the corresponding `Base`
105-
```
106-
107-
#### `multibase.encodingFromData` - Get the encoding from data either a `string` or `Uint8Array`
108-
109-
```JavaScript
110-
const value = multibase.encodingFromData(data)
111-
// value is an instance of the corresponding `Base`
112-
```
113-
114-
#### `multibase.names`
115-
116-
A frozen `Object` of supported base encoding names mapped to the corresponding `Base` instance.
117-
118-
#### `multibase.codes`
119-
120-
A frozen `Object` of supported base encoding codes mapped to the corresponding `Base` instance.
121-
122-
### Supported Encodings, see [`src/constants.js`](/src/constants.js)
123-
12460
## Contribute
12561

12662
Contributions welcome. Please check out [the issues](https://github.com/multiformats/js-multibase/issues).

package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,27 @@
3535
},
3636
"dependencies": {
3737
"@multiformats/base-x": "^4.0.1",
38-
"web-encoding": "^1.0.2"
38+
"web-encoding": "^1.0.4"
3939
},
4040
"devDependencies": {
41-
"aegir": "^26.0.0",
41+
"aegir": "^29.0.1",
4242
"benchmark": "^2.1.4"
4343
},
4444
"engines": {
4545
"node": ">=10.0.0",
4646
"npm": ">=6.0.0"
4747
},
4848
"eslintConfig": {
49-
"extends": "./node_modules/aegir/src/config/eslintrc.js"
49+
"extends": "ipfs"
50+
},
51+
"types": "dist/src/index.d.ts",
52+
"typesVersions": {
53+
"*": {
54+
"src/*": [
55+
"dist/src/*",
56+
"dist/src/*/index"
57+
]
58+
}
5059
},
5160
"contributors": [
5261
"David Dias <[email protected]>",

src/base.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
1-
// @ts-check
21
'use strict'
32

43
const { encodeText } = require('./util')
54

5+
/** @typedef {import('./types').CodecFactory} CodecFactory */
6+
/** @typedef {import("./types").BaseName} BaseName */
7+
/** @typedef {import("./types").BaseCode} BaseCode */
8+
69
/**
7-
* @typedef {Object} Codec
8-
* @property {function(Uint8Array):string} encode
9-
* @property {function(string):Uint8Array} decode
10+
* Class to encode/decode in the supported Bases
1011
*
11-
* @typedef {function(string):Codec} CodecFactory
1212
*/
13-
1413
class Base {
1514
/**
16-
* @param {string} name
17-
* @param {string} code
18-
* @param {CodecFactory} implementation
15+
* @param {BaseName} name
16+
* @param {BaseCode} code
17+
* @param {CodecFactory} factory
1918
* @param {string} alphabet
2019
*/
21-
constructor (name, code, implementation, alphabet) {
20+
constructor (name, code, factory, alphabet) {
2221
this.name = name
2322
this.code = code
2423
this.codeBuf = encodeText(this.code)
2524
this.alphabet = alphabet
26-
this.engine = implementation(alphabet)
25+
this.codec = factory(alphabet)
2726
}
2827

2928
/**
3029
* @param {Uint8Array} buf
3130
* @returns {string}
3231
*/
3332
encode (buf) {
34-
return this.engine.encode(buf)
33+
return this.codec.encode(buf)
3534
}
3635

3736
/**
@@ -44,7 +43,7 @@ class Base {
4443
throw new Error(`invalid character '${char}' in '${string}'`)
4544
}
4645
}
47-
return this.engine.decode(string)
46+
return this.codec.decode(string)
4847
}
4948
}
5049

src/constants.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
// @ts-check
21
'use strict'
32

43
const baseX = require('@multiformats/base-x')
54
const Base = require('./base.js')
6-
const rfc4648 = require('./rfc4648')
5+
const { rfc4648 } = require('./rfc4648')
76
const { decodeText, encodeText } = require('./util')
87

8+
/** @typedef {import('./types').CodecFactory} CodecFactory */
9+
/** @typedef {import('./types').Codec} Codec */
10+
/** @typedef {import('./types').BaseName} BaseName */
11+
/** @typedef {import('./types').BaseCode} BaseCode */
12+
13+
/** @type {CodecFactory} */
914
const identity = () => {
1015
return {
1116
encode: decodeText,
@@ -14,10 +19,10 @@ const identity = () => {
1419
}
1520

1621
/**
17-
* @typedef {import('./base').CodecFactory} CodecFactory
1822
*
1923
* name, code, implementation, alphabet
20-
* @type {Array<[string, string, CodecFactory, string]>}
24+
*
25+
* @type {Array<[BaseName, BaseCode, CodecFactory, string]>}
2126
*/
2227
const constants = [
2328
['identity', '\x00', identity, ''],
@@ -45,15 +50,17 @@ const constants = [
4550
['base64urlpad', 'U', rfc4648(6), 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=']
4651
]
4752

53+
/** @type {Record<BaseName,Base>} */
4854
const names = constants.reduce((prev, tupple) => {
4955
prev[tupple[0]] = new Base(tupple[0], tupple[1], tupple[2], tupple[3])
5056
return prev
51-
}, {})
57+
}, /** @type {Record<BaseName,Base>} */({}))
5258

59+
/** @type {Record<BaseCode,Base>} */
5360
const codes = constants.reduce((prev, tupple) => {
5461
prev[tupple[1]] = names[tupple[0]]
5562
return prev
56-
}, {})
63+
}, /** @type {Record<BaseCode,Base>} */({}))
5764

5865
module.exports = {
5966
names,

src/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
// @ts-check
21
/**
32
* Implementation of the [multibase](https://github.com/multiformats/multibase) specification.
43
*
5-
* @module Multibase
64
*/
75
'use strict'
86

97
const constants = require('./constants')
108
const { encodeText, decodeText, concat } = require('./util')
119

12-
/** @typedef {import("./base")} Base */
10+
/** @typedef {import('./base')} Base */
11+
/** @typedef {import("./types").BaseNameOrCode} BaseNameOrCode */
12+
/** @typedef {import("./types").BaseCode} BaseCode */
1313

1414
/**
1515
* Create a new Uint8Array with the multibase varint+code.
1616
*
17-
* @param {string|number} nameOrCode - The multibase name or code number.
17+
* @param {BaseNameOrCode} nameOrCode - The multibase name or code number.
1818
* @param {Uint8Array} buf - The data to be prefixed with multibase.
1919
* @returns {Uint8Array}
2020
* @throws {Error} Will throw if the encoding is not supported
@@ -32,7 +32,7 @@ function multibase (nameOrCode, buf) {
3232
/**
3333
* Encode data with the specified base and add the multibase prefix.
3434
*
35-
* @param {string|number} nameOrCode - The multibase name or code number.
35+
* @param {BaseNameOrCode} nameOrCode - The multibase name or code number.
3636
* @param {Uint8Array} buf - The data to be encoded.
3737
* @returns {Uint8Array}
3838
* @throws {Error} Will throw if the encoding is not supported
@@ -64,15 +64,15 @@ function decode (data) {
6464
if (['f', 'F', 'v', 'V', 't', 'T', 'b', 'B', 'c', 'C', 'h', 'k', 'K'].includes(prefix)) {
6565
data = data.toLowerCase()
6666
}
67-
const enc = encoding(data[0])
67+
const enc = encoding(/** @type {BaseCode} */(data[0]))
6868
return enc.decode(data.substring(1))
6969
}
7070

7171
/**
7272
* Is the given data multibase encoded?
7373
*
7474
* @param {Uint8Array|string} data
75-
* @returns {false|string}
75+
* @returns {false | string}
7676
*/
7777
function isEncoded (data) {
7878
if (data instanceof Uint8Array) {
@@ -85,7 +85,7 @@ function isEncoded (data) {
8585
}
8686

8787
try {
88-
const enc = encoding(data[0])
88+
const enc = encoding(/** @type {BaseCode} */(data[0]))
8989
return enc.name
9090
} catch (err) {
9191
return false
@@ -95,7 +95,7 @@ function isEncoded (data) {
9595
/**
9696
* Validate encoded data
9797
*
98-
* @param {string} name
98+
* @param {BaseNameOrCode} name
9999
* @param {Uint8Array} buf
100100
* @returns {void}
101101
* @throws {Error} Will throw if the encoding is not supported
@@ -108,7 +108,7 @@ function validEncode (name, buf) {
108108
/**
109109
* Get the encoding by name or code
110110
*
111-
* @param {string|number} nameOrCode
111+
* @param {BaseNameOrCode} nameOrCode
112112
* @returns {Base}
113113
* @throws {Error} Will throw if the encoding is not supported
114114
*/
@@ -134,7 +134,7 @@ function encodingFromData (data) {
134134
data = decodeText(data)
135135
}
136136

137-
return encoding(data[0])
137+
return encoding(/** @type {BaseCode} */(data[0]))
138138
}
139139

140140
exports = module.exports = multibase

src/rfc4648.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// @ts-check
21
'use strict'
32

4-
/** @typedef {import('./base').CodecFactory} CodecFactory */
3+
/** @typedef {import('./types').CodecFactory} CodecFactory */
54

65
/**
76
* @param {string} string
@@ -96,10 +95,12 @@ const encode = (data, alphabet, bitsPerChar) => {
9695
}
9796

9897
/**
98+
* RFC4648 Factory
99+
*
99100
* @param {number} bitsPerChar
100101
* @returns {CodecFactory}
101102
*/
102-
module.exports = (bitsPerChar) => (alphabet) => {
103+
const rfc4648 = (bitsPerChar) => (alphabet) => {
103104
return {
104105
/**
105106
* @param {Uint8Array} input
@@ -117,3 +118,5 @@ module.exports = (bitsPerChar) => (alphabet) => {
117118
}
118119
}
119120
}
121+
122+
module.exports = { rfc4648 }

0 commit comments

Comments
 (0)