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

Commit 2b24f59

Browse files
authored
feat: cancellable api calls (#2993)
Passes a [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to every API call which emits a `abort` event when the caller is no longer interested in the result of the operation. Lower level code that creates resources or has other long-term effects should tear down those resources early if the `abort` event is received. Adds support for `timeout` options to every API call that will emit an `abort` event on the passed signal and throw a Timeout error. Finally `abort` events are triggered if the current request arrived via the HTTP API and the request was aborted from the client - that is, a `disconnect` event is fired by Hapi. - Updates the core-api docs to add these new options. - Refactors HTTP API to replace custom args parsing with Joi - Tests all HTTP API endpoints - Adds pin support to `ipfs.block.put`- fixes #3015
1 parent cb5b9ec commit 2b24f59

File tree

413 files changed

+14852
-6199
lines changed

Some content is hidden

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

413 files changed

+14852
-6199
lines changed

docs/CONFIG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ The "basic" connection manager tries to keep between `LowWater` and `HighWater`
252252
1. Keeping all connections until `HighWater` connections is reached.
253253
2. Once `HighWater` is reached, it closes connections until `LowWater` is reached.
254254

255-
**Example:**
255+
### Example
256256

257257
```json
258258
{

docs/core-api/BITSWAP.md

+94-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
1-
# Bitswap API
1+
# Bitswap API <!-- omit in toc -->
2+
3+
- [`ipfs.bitswap.wantlist([peerId,] [options])`](#ipfsbitswapwantlistpeerid-options)
4+
- [Parameters](#parameters)
5+
- [Options](#options)
6+
- [Returns](#returns)
7+
- [Example](#example)
8+
- [`ipfs.bitswap.unwant(cids, [options])`](#ipfsbitswapunwantcids-options)
9+
- [Parameters](#parameters-1)
10+
- [Options](#options-1)
11+
- [Returns](#returns-1)
12+
- [Example](#example-1)
13+
- [`ipfs.bitswap.stat([options])`](#ipfsbitswapstatoptions)
14+
- [Parameters](#parameters-2)
15+
- [Options](#options-2)
16+
- [Returns](#returns-2)
17+
- [Example](#example-2)
18+
19+
## `ipfs.bitswap.wantlist([peerId,] [options])`
220

3-
* [bitswap.wantlist](#bitswapwantlist)
4-
* [bitswap.stat](#bitswapstat)
21+
> Returns the wantlist, optionally filtered by peer ID
522
6-
### `bitswap.wantlist`
23+
### Parameters
724

8-
> Returns the wantlist, optionally filtered by peer ID
25+
| Name | Type | Description |
26+
| ---- | ---- | ----------- |
27+
| peerId | [PeerId][], [CID][], `String` or `Buffer` | An optional peer ID to return the wantlist for |
28+
29+
### Options
30+
31+
An optional object which may have the following keys:
932

10-
#### `ipfs.bitswap.wantlist([peerId])`
33+
| Name | Type | Default | Description |
34+
| ---- | ---- | ------- | ----------- |
35+
| timeout | `Number` | `undefined` | A timeout in ms |
36+
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
1137

12-
**Returns**
38+
### Returns
1339

1440
| Type | Description |
1541
| -------- | -------- |
16-
| `Promise<CID[]>` | An array of [CID][cid]s currently in the wantlist |
42+
| `Promise<CID[]>` | An array of [CID][]s currently in the wantlist |
1743

18-
**Example:**
44+
### Example
1945

2046
```JavaScript
2147
const list = await ipfs.bitswap.wantlist()
@@ -29,15 +55,67 @@ console.log(list2)
2955

3056
A great source of [examples][] can be found in the tests for this API.
3157

32-
#### `bitswap.stat`
58+
## `ipfs.bitswap.unwant(cids, [options])`
3359

34-
> Show diagnostic information on the bitswap agent.
60+
> Removes one or more CIDs from the wantlist
61+
62+
### Parameters
63+
64+
| Name | Type | Description |
65+
| ---- | ---- | ----------- |
66+
| cids | A [CID][] or Array of [CID][]s | The CIDs to remove from the wantlist |
67+
68+
### Options
69+
70+
An optional object which may have the following keys:
71+
72+
| Name | Type | Default | Description |
73+
| ---- | ---- | ------- | ----------- |
74+
| timeout | `Number` | `undefined` | A timeout in ms |
75+
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
76+
77+
### Returns
78+
79+
| Type | Description |
80+
| -------- | -------- |
81+
| `Promise<void>` | A promise that resolves once the request is complete |
82+
83+
### Example
84+
85+
```JavaScript
86+
let list = await ipfs.bitswap.wantlist()
87+
console.log(list)
88+
// [ CID('QmHash') ]
89+
90+
await ipfs.bitswap.unwant(cid)
91+
92+
list = await ipfs.bitswap.wantlist()
93+
console.log(list)
94+
// []
95+
```
96+
97+
A great source of [examples][] can be found in the tests for this API.
98+
99+
## `ipfs.bitswap.stat([options])`
35100

36-
##### `ipfs.bitswap.stat()`
101+
> Show diagnostic information on the bitswap agent.
37102
38103
Note: `bitswap.stat` and `stats.bitswap` can be used interchangeably.
39104

40-
**Returns**
105+
### Parameters
106+
107+
None
108+
109+
### Options
110+
111+
An optional object which may have the following keys:
112+
113+
| Name | Type | Default | Description |
114+
| ---- | ---- | ------- | ----------- |
115+
| timeout | `Number` | `undefined` | A timeout in ms |
116+
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
117+
118+
### Returns
41119

42120
| Type | Description |
43121
| -------- | -------- |
@@ -55,7 +133,7 @@ The returned object contains the following keys:
55133
- `dupBlksReceived` is a [BigNumber Int][1]
56134
- `dupDataReceived` is a [BigNumber Int][1]
57135

58-
**Example:**
136+
### Example
59137

60138
```JavaScript
61139
const stats = await ipfs.bitswap.stat()
@@ -81,3 +159,5 @@ A great source of [examples][] can be found in the tests for this API.
81159
[1]: https://github.com/MikeMcl/bignumber.js/
82160
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/bitswap
83161
[cid]: https://www.npmjs.com/package/cids
162+
[peerid]: https://www.npmjs.com/package/peer-id
163+
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal

docs/core-api/BLOCK.md

+89-54
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,52 @@
1-
# Block API
1+
# Block API <!-- omit in toc -->
2+
3+
- [`ipfs.block.get(cid, [options])`](#ipfsblockgetcid-options)
4+
- [Parameters](#parameters)
5+
- [Options](#options)
6+
- [Returns](#returns)
7+
- [Example](#example)
8+
- [`ipfs.block.put(block, [options])`](#ipfsblockputblock-options)
9+
- [Parameters](#parameters-1)
10+
- [Options](#options-1)
11+
- [Returns](#returns-1)
12+
- [Example](#example-1)
13+
- [`ipfs.block.rm(cid, [options])`](#ipfsblockrmcid-options)
14+
- [Parameters](#parameters-2)
15+
- [Options](#options-2)
16+
- [Returns](#returns-2)
17+
- [Example](#example-2)
18+
- [`ipfs.block.stat(cid, [options])`](#ipfsblockstatcid-options)
19+
- [Parameters](#parameters-3)
20+
- [Options](#options-3)
21+
- [Returns](#returns-3)
22+
- [Example](#example-3)
23+
24+
## `ipfs.block.get(cid, [options])`
225

3-
* [block.get](#blockget)
4-
* [block.put](#blockput)
5-
* [block.rm](#blockrm)
6-
* [block.stat](#blockstat)
26+
> Get a raw IPFS block.
727
8-
#### `block.get`
28+
### Parameters
929

10-
> Get a raw IPFS block.
30+
| Name | Type | Description |
31+
| ---- | ---- | ----------- |
32+
| cid | [CID][], `String` or `Buffer` | A CID that corresponds to the desired block |
1133

12-
##### `ipfs.block.get(cid, [options])`
34+
### Options
1335

14-
`cid` is a [cid][cid] which can be passed as:
36+
An optional object which may have the following keys:
1537

16-
- Buffer, the raw Buffer of the cid
17-
- CID, a CID instance
18-
- String, the base58 encoded version of the multihash
38+
| Name | Type | Default | Description |
39+
| ---- | ---- | ------- | ----------- |
40+
| timeout | `Number` | `undefined` | A timeout in ms |
41+
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
1942

20-
**Returns**
43+
### Returns
2144

2245
| Type | Description |
2346
| -------- | -------- |
2447
| `Promise<Block>` | A [Block][block] type object, containing both the data and the hash of the block |
2548

26-
**Example:**
49+
### Example
2750

2851
```JavaScript
2952
const block = await ipfs.block.get(cid)
@@ -32,43 +55,44 @@ console.log(block.data)
3255

3356
A great source of [examples][] can be found in the tests for this API.
3457

35-
#### `block.put`
58+
## `ipfs.block.put(block, [options])`
3659

3760
> Stores input as an IPFS block.
3861
39-
##### `ipfs.block.put(block, [options])`
40-
41-
Where `block` can be:
62+
### Parameters
4263

43-
- `Buffer` - the raw bytes of the Block
44-
- [`Block`][block] instance
64+
| Name | Type | Description |
65+
| ---- | ---- | ----------- |
66+
| block | A `Buffer` or [Block][] instance | The block or data to store |
4567

46-
and `options` is an Object that can contain the following properties:
68+
### Options
4769

48-
- `cid` - a [cid][cid] which can be passed as:
49-
- Buffer, the raw Buffer of the cid
50-
- CID, a CID instance
51-
- String, the base58 encoded version of the multihash
52-
- format
53-
- mhtype
54-
- mhlen
55-
- version
70+
An optional object which may have the following keys:
5671

57-
if no options are passed, it defaults to `{ format: 'dag-pb', mhtype: 'sha2-256', version: 0 }`
72+
| Name | Type | Default | Description |
73+
| ---- | ---- | ------- | ----------- |
74+
| cid | [CID][] | `undefined` | A CID to store the block under |
75+
| format | `String` | `'dag-pb'` | The codec to use to create the CID |
76+
| mhtype | `String` | `sha2-256` | The hashing algorithm to use to create the CID |
77+
| mhlen | `Number` | | |
78+
| version | `Number` | 0 | The version to use to create the CID |
79+
| pin | `boolean` | false | If true, pin added blocks recursively |
80+
| timeout | `Number` | `undefined` | A timeout in ms |
81+
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
5882

5983
**Note:** If you pass a [`Block`][block] instance as the block parameter, you don't need to pass options, as the block instance will carry the CID value as a property.
6084

61-
**Returns**
85+
### Returns
6286

6387
| Type | Description |
6488
| -------- | -------- |
6589
| `Promise<Block>` | A [Block][block] type object, containing both the data and the hash of the block |
6690

67-
**Example:**
91+
### Example
6892

6993
```JavaScript
7094
// Defaults
71-
const buf = new Buffer('a serialized object')
95+
const buf = Buffer.from('a serialized object')
7296

7397
const block = await ipfs.block.put(buf)
7498

@@ -81,7 +105,7 @@ console.log(block.cid.toString())
81105

82106
// With custom format and hashtype through CID
83107
const CID = require('cids')
84-
const buf = new Buffer('another serialized object')
108+
const buf = Buffer.from('another serialized object')
85109
const cid = new CID(1, 'dag-pb', multihash)
86110

87111
const block = await ipfs.block.put(blob, cid)
@@ -96,25 +120,28 @@ console.log(block.cid.toString())
96120

97121
A great source of [examples][] can be found in the tests for this API.
98122

99-
#### `block.rm`
123+
## `ipfs.block.rm(cid, [options])`
100124

101125
> Remove one or more IPFS block(s).
102126
103-
##### `ipfs.block.rm(cid, [options])`
127+
### Parameters
104128

105-
`cid` is a [cid][cid] which can be passed as:
129+
| Name | Type | Description |
130+
| ---- | ---- | ----------- |
131+
| cid | A [CID][] or Array of [CID][]s | Blocks corresponding to the passed CID(s) will be removed |
106132

107-
- Buffer, the raw Buffer of the cid
108-
- CID, a CID instance
109-
- String, the base58 encoded version of the multihash
110-
- Array, list of CIDs in any of the above three formats
133+
### Options
111134

112-
`options` is an Object that can contain the following properties:
135+
An optional object which may have the following keys:
113136

114-
- `force` (boolean): Ignores nonexistent blocks.
115-
- `quiet` (boolean): write minimal output
137+
| Name | Type | Default | Description |
138+
| ---- | ---- | ------- | ----------- |
139+
| force | `boolean` | `false` | Ignores nonexistent blocks |
140+
| quiet | `boolean` | `false | Write minimal output |
141+
| timeout | `Number` | `undefined` | A timeout in ms |
142+
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
116143

117-
**Returns**
144+
### Returns
118145

119146
| Type | Description |
120147
| -------- | -------- |
@@ -131,7 +158,7 @@ Each object yielded is of the form:
131158

132159
Note: If an error string is present for a given object, the block with that hash was not removed and the string will contain the reason why, for example if the block was pinned.
133160

134-
**Example:**
161+
### Example
135162

136163
```JavaScript
137164
for await (const result of ipfs.block.rm(cid)) {
@@ -141,19 +168,26 @@ for await (const result of ipfs.block.rm(cid)) {
141168

142169
A great source of [examples][] can be found in the tests for this API.
143170

144-
#### `block.stat`
171+
## `ipfs.block.stat(cid, [options])`
145172

146173
> Print information of a raw IPFS block.
147174
148-
##### `ipfs.block.stat(cid)`
175+
### Parameters
176+
177+
| Name | Type | Description |
178+
| ---- | ---- | ----------- |
179+
| cid | A [CID][] or Array of [CID][]s | The stats of the passed CID will be returned |
180+
181+
### Options
149182

150-
`cid` is a [cid][cid] which can be passed as:
183+
An optional object which may have the following keys:
151184

152-
- `Buffer`, the raw Buffer of the multihash (or of and encoded version)
153-
- `String`, the toString version of the multihash (or of an encoded version)
154-
- CID, a CID instance
185+
| Name | Type | Default | Description |
186+
| ---- | ---- | ------- | ----------- |
187+
| timeout | `Number` | `undefined` | A timeout in ms |
188+
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
155189

156-
**Returns**
190+
### Returns
157191

158192
| Type | Description |
159193
| -------- | -------- |
@@ -168,7 +202,7 @@ the returned object has the following keys:
168202
}
169203
```
170204

171-
**Example:**
205+
### Example
172206

173207
```JavaScript
174208
const multihashStr = 'QmQULBtTjNcMwMr4VMNknnVv3RpytrLSdgpvMcTnfNhrBJ'
@@ -188,4 +222,5 @@ A great source of [examples][] can be found in the tests for this API.
188222
[block]: https://github.com/ipfs/js-ipfs-block
189223
[multihash]: https://github.com/multiformats/multihash
190224
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/block
225+
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
191226
[cid]: https://www.npmjs.com/package/cids

0 commit comments

Comments
 (0)