Skip to content

Commit fcee86e

Browse files
feat!: convert codebase to typescript (#251)
Fixes #249 - update aegir to get latest linter rules - minimal changes to vendored code - eslint-disable files, add inferred types as jsdocs, move vendor directory under src - convert all .js files to .ts - apply all jsdoc type annotations as typescript type annotations - fix all resulting linter and type issues - Special attention given to keep any helpful jsdocs BREAKING CHANGE: this module is now TypeScript - the API has not changed but releasing as a major for saftey --------- Co-authored-by: Alex Potsides <alex@achingbrain.net>
1 parent 139b3c2 commit fcee86e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1337
-1700
lines changed

.github/workflows/js-test-and-release.yml

+2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ on:
99

1010
permissions:
1111
contents: write
12+
id-token: write
1213
packages: write
14+
pull-requests: write
1315

1416
concurrency:
1517
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event_name == 'push' && github.sha || github.ref }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Semantic PR
2+
3+
on:
4+
pull_request_target:
5+
types:
6+
- opened
7+
- edited
8+
- synchronize
9+
10+
jobs:
11+
main:
12+
uses: pl-strflt/.github/.github/workflows/reusable-semantic-pull-request.yml@v0.3

.github/workflows/stale.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Close and mark stale issue
2+
3+
on:
4+
schedule:
5+
- cron: '0 0 * * *'
6+
7+
permissions:
8+
issues: write
9+
pull-requests: write
10+
11+
jobs:
12+
stale:
13+
uses: pl-strflt/.github/.github/workflows/reusable-stale-issue.yml@v0.3

.github/workflows/typecheck.yml

-29
This file was deleted.

.gitignore

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
.coverage
2-
package-lock.json
31
node_modules
4-
.DS_Store
5-
yarn.lock
6-
types
7-
test/ts-use/tsconfig.tsbuildinfo
8-
types/tsconfig.tsbuildinfo
9-
*.log
2+
build
103
dist
114
.docs
5+
.coverage
6+
node_modules
7+
package-lock.json
8+
yarn.lock
9+
.vscode

README.md

+32-57
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,16 @@
1-
# multiformats <!-- omit in toc -->
2-
31
[![multiformats.io](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://multiformats.io)
42
[![codecov](https://img.shields.io/codecov/c/github/multiformats/js-multiformats.svg?style=flat-square)](https://codecov.io/gh/multiformats/js-multiformats)
53
[![CI](https://img.shields.io/github/actions/workflow/status/multiformats/js-multiformats/js-test-and-release.yml?branch=master\&style=flat-square)](https://github.com/multiformats/js-multiformats/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)
64

75
> Interface for multihash, multicodec, multibase and CID
86
9-
## Table of contents <!-- omit in toc -->
10-
11-
- [Install](#install)
12-
- [Browser `<script>` tag](#browser-script-tag)
13-
- [Interfaces](#interfaces)
14-
- [Creating Blocks](#creating-blocks)
15-
- [Multibase Encoders / Decoders / Codecs](#multibase-encoders--decoders--codecs)
16-
- [Multicodec Encoders / Decoders / Codecs](#multicodec-encoders--decoders--codecs)
17-
- [Multihash Hashers](#multihash-hashers)
18-
- [Traversal](#traversal)
19-
- [Legacy interface](#legacy-interface)
20-
- [Implementations](#implementations)
21-
- [Multibase codecs](#multibase-codecs)
22-
- [Multihash hashers](#multihash-hashers-1)
23-
- [IPLD codecs (multicodec)](#ipld-codecs-multicodec)
24-
- [API Docs](#api-docs)
25-
- [License](#license)
26-
- [Contribution](#contribution)
27-
28-
## Install
29-
30-
```console
31-
$ npm i multiformats
32-
```
33-
34-
### Browser `<script>` tag
35-
36-
Loading this module through a script tag will make it's exports available as `Multiformats` in the global namespace.
37-
38-
```html
39-
<script src="https://unpkg.com/multiformats/dist/index.min.js"></script>
40-
```
41-
42-
## Interfaces
7+
# About
438

449
This library defines common interfaces and low level building blocks for various interrelated multiformat technologies (multicodec, multihash, multibase, and CID). They can be used to implement custom base encoders / decoders / codecs, codec encoders /decoders and multihash hashers that comply to the interface that layers above assume.
4510

4611
This library provides implementations for most basics and many others can be found in linked repositories.
4712

48-
```js
13+
```TypeScript
4914
import { CID } from 'multiformats/cid'
5015
import * as json from 'multiformats/codecs/json'
5116
import { sha256 } from 'multiformats/hashes/sha2'
@@ -57,9 +22,9 @@ const cid = CID.create(1, json.code, hash)
5722
//> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)
5823
```
5924

60-
### Creating Blocks
25+
## Creating Blocks
6126

62-
```js
27+
```TypeScript
6328
import * as Block from 'multiformats/block'
6429
import * as codec from '@ipld/dag-cbor'
6530
import { sha256 as hasher } from 'multiformats/hashes/sha2'
@@ -80,19 +45,19 @@ block = await Block.decode({ bytes: block.bytes, codec, hasher })
8045
block = await Block.create({ bytes: block.bytes, cid: block.cid, codec, hasher })
8146
```
8247

83-
### Multibase Encoders / Decoders / Codecs
48+
## Multibase Encoders / Decoders / Codecs
8449

8550
CIDs can be serialized to string representation using multibase encoders that implement [`MultibaseEncoder`](https://github.com/multiformats/js-multiformats/blob/master/src/bases/interface.ts) interface. This library provides quite a few implementations that can be imported:
8651

87-
```js
52+
```TypeScript
8853
import { base64 } from "multiformats/bases/base64"
8954
cid.toString(base64.encoder)
9055
//> 'mAYAEEiCTojlxqRTl6svwqNJRVM2jCcPBxy+7mRTUfGDzy2gViA'
9156
```
9257

9358
Parsing CID string serialized CIDs requires multibase decoder that implements [`MultibaseDecoder`](https://github.com/multiformats/js-multiformats/blob/master/src/bases/interface.ts) interface. This library provides a decoder for every encoder it provides:
9459

95-
```js
60+
```TypeScript
9661
CID.parse('mAYAEEiCTojlxqRTl6svwqNJRVM2jCcPBxy+7mRTUfGDzy2gViA', base64.decoder)
9762
//> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)
9863
```
@@ -102,7 +67,7 @@ them as `encoder` and `decoder` properties. For added convenience codecs also
10267
implement `MultibaseEncoder` and `MultibaseDecoder` interfaces so they could be
10368
used as either or both:
10469

105-
```js
70+
```TypeScript
10671
cid.toString(base64)
10772
CID.parse(cid.toString(base64), base64)
10873
```
@@ -111,7 +76,7 @@ CID.parse(cid.toString(base64), base64)
11176
multibase codecs so that CIDs can be base serialized to (version specific)
11277
default base encoding and parsed without having to supply base encoders/decoders:
11378

114-
```js
79+
```TypeScript
11580
const v1 = CID.parse('bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea')
11681
v1.toString()
11782
//> 'bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea'
@@ -123,17 +88,13 @@ v0.toV1().toString()
12388
//> 'bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku'
12489
```
12590

126-
### Multicodec Encoders / Decoders / Codecs
91+
## Multicodec Encoders / Decoders / Codecs
12792

12893
This library defines [`BlockEncoder`, `BlockDecoder` and `BlockCodec` interfaces](https://github.com/multiformats/js-multiformats/blob/master/src/codecs/interface.ts).
12994
Codec implementations should conform to the `BlockCodec` interface which implements both `BlockEncoder` and `BlockDecoder`.
13095
Here is an example implementation of JSON `BlockCodec`.
13196

132-
```js
133-
/**
134-
* @template T
135-
* @type {BlockCodec<0x0200, T>}
136-
*/
97+
```TypeScript
13798
export const { name, code, encode, decode } = {
13899
name: 'json',
139100
code: 0x0200,
@@ -142,11 +103,11 @@ export const { name, code, encode, decode } = {
142103
}
143104
```
144105

145-
### Multihash Hashers
106+
## Multihash Hashers
146107

147108
This library defines [`MultihashHasher` and `MultihashDigest` interfaces](https://github.com/multiformats/js-multiformats/blob/master/src/hashes/interface.ts) and convinient function for implementing them:
148109

149-
```js
110+
```TypeScript
150111
import * as hasher from 'multiformats/hashes/hasher'
151112

152113
const sha256 = hasher.from({
@@ -164,13 +125,13 @@ CID.create(1, json.code, hash)
164125
//> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)
165126
```
166127

167-
### Traversal
128+
## Traversal
168129

169130
This library contains higher-order functions for traversing graphs of data easily.
170131

171132
`walk()` walks through the links in each block of a DAG calling a user-supplied loader function for each one, in depth-first order with no duplicate block visits. The loader should return a `Block` object and can be used to inspect and collect block ordering for a full DAG walk. The loader should `throw` on error, and return `null` if a block should be skipped by `walk()`.
172133

173-
```js
134+
```TypeScript
174135
import { walk } from 'multiformats/traversal'
175136
import * as Block from 'multiformats/block'
176137
import * as codec from 'multiformats/codecs/json'
@@ -243,17 +204,31 @@ Other (less useful) bases implemented in [multiformats/js-multiformats](https://
243204
| `dag-pb` | `@ipld/dag-pb` | [ipld/js-dag-pb](https://github.com/ipld/js-dag-pb) |
244205
| `dag-jose` | `dag-jose` | [ceramicnetwork/js-dag-jose](https://github.com/ceramicnetwork/js-dag-jose) |
245206

246-
## API Docs
207+
# Install
208+
209+
```console
210+
$ npm i multiformats
211+
```
212+
213+
## Browser `<script>` tag
214+
215+
Loading this module through a script tag will make it's exports available as `Multiformats` in the global namespace.
216+
217+
```html
218+
<script src="https://unpkg.com/multiformats/dist/index.min.js"></script>
219+
```
220+
221+
# API Docs
247222

248223
- <https://multiformats.github.io/js-multiformats>
249224

250-
## License
225+
# License
251226

252227
Licensed under either of
253228

254229
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
255230
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
256231

257-
## Contribution
232+
# Contribution
258233

259234
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

0 commit comments

Comments
 (0)