Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 34e1492

Browse files
Gozalaachingbrain
andauthored
chore: refactor common types (#3449)
Addresses #3442 (comment) by refactoring some of the common types used by root APIs as per #3413 Co-authored-by: achingbrain <[email protected]>
1 parent 18d6359 commit 34e1492

Some content is hidden

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

53 files changed

+650
-377
lines changed

examples/types-use-ipfs-from-ts/src/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default async function main() {
1414

1515
console.log('Added file:', file.path, file.cid.toString())
1616
try {
17+
// @ts-expect-error CID has no toUpperCase method
1718
file.cid.toUpperCase()
1819
} catch (error) {
1920

examples/types-use-ipfs-from-typed-js/src/main.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ async function main () {
1717

1818
console.log('Added file:', file.path, file.cid.toString())
1919
try {
20+
// @ts-expect-error CID has no toUpperCase method
2021
file.cid.toUpperCase()
2122
} catch(error) {
2223

packages/ipfs-core-types/COPYRIGHT

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This project is transitioning from an MIT-only license to a dual MIT/Apache-2.0 license.
2+
Unless otherwise noted, all code contributed prior to 2019-11-21 and not contributed by
3+
a user listed in [this signoff issue](https://github.com/ipfs/js-ipfs/issues/2624) is
4+
licensed under MIT-only. All new contributions (and past contributions since 2019-11-21)
5+
are licensed under a dual MIT/Apache-2.0 license.
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
2+
3+
http://www.apache.org/licenses/LICENSE-2.0
4+
5+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

packages/ipfs-core-types/LICENSE-MIT

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

packages/ipfs-core-types/README.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# ipfs-core-types <!-- omit in toc -->
2+
3+
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
4+
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
5+
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
6+
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
7+
[![Dependency Status](https://david-dm.org/ipfs/js-ipfs/status.svg?style=flat-square&path=packages/interface-ipfs-core)](https://david-dm.org/ipfs/js-ipfs?path=packages/interface-ipfs-core)
8+
9+
> IPFS interface definitions used by implementations for API compatibility.
10+
11+
## Lead Maintainer <!-- omit in toc -->
12+
13+
[Alex Potsides](http://github.com/achingbrain)
14+
15+
## Table of Contents <!-- omit in toc -->
16+
17+
- [Background](#background)
18+
- [Install](#install)
19+
- [Usage](#usage)
20+
- [Validation](#validation)
21+
- [Contribute](#contribute)
22+
- [Want to hack on IPFS?](#want-to-hack-on-ipfs)
23+
- [License](#license)
24+
25+
## Background
26+
27+
The primary goal of this module is to define and ensure that IPFS core implementations and their respective HTTP client libraries implement the same interface, so that developers can quickly change between a local and a remote node without having to change their applications.
28+
29+
It offers set of typescript interface definitions that implementations can claim compatibility with and use typescript to validate those claims.
30+
31+
## Install
32+
33+
In JavaScript land:
34+
35+
```console
36+
$ npm install ipfs-core-types
37+
```
38+
39+
## Usage
40+
41+
Install `ipfs-core-types` as one of the dependencies of your project and use it to ensure your implementations API compatibility:
42+
43+
### In [JSDoc syntax](https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html)
44+
45+
```js
46+
/**
47+
* @implements {import('ipfs-core-types').RootAPI}
48+
*/
49+
class Root {
50+
// your implementation goes here
51+
}
52+
```
53+
54+
### In Typescript
55+
56+
```ts
57+
import { RootAPI } from 'ipfs-core-types'
58+
class Root implements RootAPI {
59+
// your implementation goes here
60+
}
61+
```
62+
63+
## Validation
64+
65+
In order to validate API compatibility you can run [typescript](https://www.typescriptlang.org/) over your implementation which will point out all the API compatibilities if there are some.
66+
67+
68+
## Contribute
69+
70+
Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/js-ipfs/issues)!
71+
72+
This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
73+
74+
### Want to hack on IPFS?
75+
76+
[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md)
77+
78+
## License
79+
80+
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fipfs%2Fjs-ipfs.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fipfs%2Fjs-ipfs?ref=badge_large)
81+
82+
[![](https://github.com/ipfs/js-ipfs/raw/master/ipfs-core-types/img/badge.png)](https://github.com/ipfs/js-ipfs/tree/master/ipfs-core-types)

packages/ipfs-core-types/package.json

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "ipfs-core-types",
3+
"version": "0.1.0",
4+
"description": "IPFS interface definitions used by implementations for API compatibility.",
5+
"leadMaintainer": "Alex Potsides <[email protected]>",
6+
"types": "src/index.ts",
7+
"homepage": "https://github.com/ipfs/js-ipfs/tree/master/packages/interface-ipfs-core#readme",
8+
"bugs": "https://github.com/ipfs/js-ipfs/issues",
9+
"scripts": {
10+
"lint": "aegir lint",
11+
"test": "aegir ts -p check"
12+
},
13+
"types": "src/index.ts",
14+
"files": [
15+
"src"
16+
],
17+
"eslintConfig": {
18+
"extends": "ipfs"
19+
},
20+
"repository": {
21+
"type": "git",
22+
"url": "git+https://github.com/ipfs/js-ipfs.git"
23+
},
24+
"keywords": [
25+
"IPFS",
26+
"types",
27+
"interface",
28+
"API"
29+
],
30+
"license": "(Apache-2.0 OR MIT)",
31+
"dependencies": {
32+
"cids": "^1.0.0",
33+
"multiaddr": "^8.0.0",
34+
"peer-id": "^0.14.1"
35+
},
36+
"devDependencies": {
37+
"aegir": "^29.2.2",
38+
"typescript": "4.0.x"
39+
},
40+
"contributors": [
41+
"Irakli Gozalishvili <[email protected]>"
42+
]
43+
}

packages/ipfs-core-types/src/basic.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Represents a value that you can await on, which is either value or a promise
3+
* of one.
4+
*/
5+
export type Await<T> =
6+
| T
7+
| Promise<T>
8+
9+
/**
10+
* Represents an iterable that can be used in `for await` loops, that is either
11+
* iterable or an async iterable.
12+
*/
13+
export type AwaitIterable<T> =
14+
| Iterable<T>
15+
| AsyncIterable<T>
16+
17+
/**
18+
* Common options across all cancellable requests.
19+
*/
20+
export interface AbortOptions {
21+
/**
22+
* Can be provided to a function that starts a long running task, which will
23+
* be aborted when signal is triggered.
24+
*/
25+
signal?: AbortSignal
26+
/**
27+
* Can be provided to a function that starts a long running task, which will
28+
* be aborted after provided timeout (in ms).
29+
*/
30+
timeout?: number
31+
}
32+
33+
export type ToJSON =
34+
| null
35+
| string
36+
| number
37+
| boolean
38+
| ToJSON[]
39+
| { toJSON?: () => ToJSON } & { [key: string]: ToJSON }

packages/ipfs-core/src/interface/bitswap.ts renamed to packages/ipfs-core-types/src/bitswap.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { PeerId, CID, Block, Await, BigInteger, AbortOptions } from './basic'
2-
import { MovingAverage } from './moving-avarage'
1+
import BigInteger from 'bignumber.js'
2+
import PeerId from 'peer-id'
3+
import CID from 'cids'
4+
import { Block } from './block-service'
5+
import { AbortOptions, Await } from './basic'
6+
import { MovingAverage } from './bitswap/moving-avarage'
37
import { StoreReader, StoreExporter, StoreImporter } from './store'
48

59
export interface Bitswap extends

packages/ipfs-core/src/interface/block-service.ts renamed to packages/ipfs-core-types/src/block-service.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Block, CID, Await, AbortOptions } from './basic'
1+
import CID from 'cids'
2+
import { Await, AbortOptions } from './basic'
23
import { StoreReader, StoreImporter, StoreExporter, StoreEraser } from './store'
34
import { Bitswap } from './bitswap'
45

@@ -18,3 +19,8 @@ export interface BlockService extends
1819
*/
1920
put(block: Block, options?:AbortOptions): Await<Block>
2021
}
22+
23+
export interface Block {
24+
cid: CID
25+
data: Uint8Array
26+
}

0 commit comments

Comments
 (0)