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

Commit d729024

Browse files
committed
feat: cancellable api calls
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 0a05d4e commit d729024

File tree

413 files changed

+14835
-6174
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

+14835
-6174
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

+92-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
930

10-
#### `ipfs.bitswap.wantlist([peerId])`
31+
An optional object which may have the following keys:
1132

12-
**Returns**
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 |
37+
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,65 @@ 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+
const list = await ipfs.bitswap.wantlist()
87+
console.log(list)
88+
// [ CID('QmHash') ]
3589

36-
##### `ipfs.bitswap.stat()`
90+
const list2 = await ipfs.bitswap.wantlist(peerId)
91+
console.log(list2)
92+
// [ CID('QmHash') ]
93+
```
94+
95+
A great source of [examples][] can be found in the tests for this API.
96+
97+
## `ipfs.bitswap.stat([options])`
98+
99+
> Show diagnostic information on the bitswap agent.
37100
38101
Note: `bitswap.stat` and `stats.bitswap` can be used interchangeably.
39102

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

42118
| Type | Description |
43119
| -------- | -------- |
@@ -55,7 +131,7 @@ The returned object contains the following keys:
55131
- `dupBlksReceived` is a [BigNumber Int][1]
56132
- `dupDataReceived` is a [BigNumber Int][1]
57133

58-
**Example:**
134+
### Example
59135

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

docs/core-api/BLOCK.md

+90-53
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,46 @@ 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])`
62+
### Parameters
4063

41-
Where `block` can be:
64+
| Name | Type | Description |
65+
| ---- | ---- | ----------- |
66+
| block | A `Buffer` or [Block][] instance | The block or data to store |
4267

43-
- `Buffer` - the raw bytes of the Block
44-
- [`Block`][block] instance
68+
### Options
4569

46-
and `options` is an Object that can contain the following properties:
70+
An optional object which may have the following keys:
4771

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
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 |
5682

57-
if no options are passed, it defaults to `{ format: 'dag-pb', mhtype: 'sha2-256', version: 0 }`
83+
if no options are passed, it defaults to `{ format: 'dag-pb', mhtype: 'sha2-256', version: 0, pin: false }`
5884

5985
**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.
6086

61-
**Returns**
87+
### Returns
6288

6389
| Type | Description |
6490
| -------- | -------- |
6591
| `Promise<Block>` | A [Block][block] type object, containing both the data and the hash of the block |
6692

67-
**Example:**
93+
### Example
6894

6995
```JavaScript
7096
// Defaults
71-
const buf = new Buffer('a serialized object')
97+
const buf = Buffer.from('a serialized object')
7298

7399
const block = await ipfs.block.put(buf)
74100

@@ -81,7 +107,7 @@ console.log(block.cid.toString())
81107

82108
// With custom format and hashtype through CID
83109
const CID = require('cids')
84-
const buf = new Buffer('another serialized object')
110+
const buf = Buffer.from('another serialized object')
85111
const cid = new CID(1, 'dag-pb', multihash)
86112

87113
const block = await ipfs.block.put(blob, cid)
@@ -96,25 +122,28 @@ console.log(block.cid.toString())
96122

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

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

101127
> Remove one or more IPFS block(s).
102128
103-
##### `ipfs.block.rm(cid, [options])`
129+
### Parameters
104130

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

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
135+
### Options
111136

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

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

117-
**Returns**
146+
### Returns
118147

119148
| Type | Description |
120149
| -------- | -------- |
@@ -131,7 +160,7 @@ Each object yielded is of the form:
131160

132161
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.
133162

134-
**Example:**
163+
### Example
135164

136165
```JavaScript
137166
for await (const result of ipfs.block.rm(cid)) {
@@ -141,19 +170,26 @@ for await (const result of ipfs.block.rm(cid)) {
141170

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

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

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

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

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
187+
| Name | Type | Default | Description |
188+
| ---- | ---- | ------- | ----------- |
189+
| timeout | `Number` | `undefined` | A timeout in ms |
190+
| signal | [AbortSignal][] | `undefined` | Can be used to cancel any long running requests started as a result of this call |
155191

156-
**Returns**
192+
### Returns
157193

158194
| Type | Description |
159195
| -------- | -------- |
@@ -168,7 +204,7 @@ the returned object has the following keys:
168204
}
169205
```
170206

171-
**Example:**
207+
### Example
172208

173209
```JavaScript
174210
const multihashStr = 'QmQULBtTjNcMwMr4VMNknnVv3RpytrLSdgpvMcTnfNhrBJ'
@@ -188,4 +224,5 @@ A great source of [examples][] can be found in the tests for this API.
188224
[block]: https://github.com/ipfs/js-ipfs-block
189225
[multihash]: https://github.com/multiformats/multihash
190226
[examples]: https://github.com/ipfs/js-ipfs/blob/master/packages/interface-ipfs-core/src/block
227+
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
191228
[cid]: https://www.npmjs.com/package/cids

0 commit comments

Comments
 (0)