Skip to content

Commit 319ba34

Browse files
authored
feat: support custom config for ipfs-http-client (#24)
1 parent 3e275b3 commit 319ba34

File tree

7 files changed

+7384
-8222
lines changed

7 files changed

+7384
-8222
lines changed

README.md

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
- [Usage](#usage)
1515
- [Examples](#examples)
1616
- [Providers](#providers)
17-
- [`httpClient`](#httpclient)
18-
- [`jsIpfs`](#jsipfs)
19-
- [`windowIpfs`](#windowipfs)
20-
- [`webExt`](#webext)
17+
- [`httpClient`](#httpclient) (remote/local HTTP API)
18+
- [`jsIpfs`](#jsipfs) (embedded js-ipfs node)
2119
<!-- TODO: improve this API - [create our own!](#creating-a-provider) -->
2220
- [Test](#test)
2321

@@ -58,7 +56,7 @@ Details [here](https://github.com/ipfs/js-ipfs/tree/master/packages/ipfs-http-cl
5856

5957
```js
6058
const { getIpfs, providers } = require('ipfs-provider')
61-
const { httpClient, jsIpfs, windowIpfs } = providers
59+
const { httpClient, jsIpfs } = providers
6260

6361
const { ipfs, provider, apiAddress } = await getIpfs({
6462
// when httpClient provider is used multiple times
@@ -68,24 +66,29 @@ const { ipfs, provider, apiAddress } = await getIpfs({
6866
// note this is an array, providers are tried in order:
6967
providers: [
7068

71-
// (1) try window.ipfs (experimental, but some browsers expose it),
72-
windowIpfs({
73-
// request specific permissions upfront (optional)
74-
permissions: { commands: ['files.add', 'files.cat'] }
75-
}),
76-
77-
// (2) try various HTTP endpoints (best-effort),
69+
// try various HTTP endpoints (best-effort),
7870
httpClient({
79-
// (2.1) try multiaddr of a local node
71+
// (1) try multiaddr of a local node
8072
apiAddress: '/ip4/127.0.0.1/tcp/5001'
8173
}),
82-
httpClient(), // (2.2) try "/api/v0/" on the same Origin as the page
74+
httpClient(), // (2) try "/api/v0/" on the same Origin as the page
8375
httpClient({
84-
// (2.3) try arbitrary API URL
76+
// (3) try arbitrary API from URL string
8577
apiAddress: 'https://some.example.com:8080'
8678
}),
87-
88-
// (3) final fallback to spawning embedded js-ipfs running in-page
79+
httpClient({
80+
// (4) try API defined by a custom http client config
81+
apiAddress: {
82+
host: 'apis.example.com',
83+
port: '443',
84+
protocol: 'https',
85+
apiPath: 'custom/path/to/api/v0',
86+
headers: {
87+
authorization: 'Basic dXNlcjpwYXNz'
88+
}
89+
}
90+
}),
91+
// (5) final fallback to spawning embedded js-ipfs running in-page
8992
jsIpfs({
9093
// js-ipfs package is used only once, as a last resort
9194
loadJsIpfsModule: () => require('ipfs'),
@@ -118,16 +121,16 @@ See [`examples/`](./examples) for sample code and demonstration of advanced fall
118121
You can customize the order of the providers by passing a different array order to the `providers` array.
119122

120123

121-
For example, if you want to try `httpClient` and then `windowIpfs`, you should run it like this:
124+
For example, if you want to try `httpClient` and then `jsIpfs`, you should run it like this:
122125

123126
```js
124127
const { getIpfs, providers } = require('ipfs-provider')
125-
const { httpClient, windowIpfs } = providers
128+
const { httpClient, jsIpfs } = providers
126129

127130
const { ipfs, provider } = await getIpfs({
128131
providers: [
129132
httpClient(),
130-
windowIpfs()
133+
jsIpfs()
131134
]
132135
})
133136
```
@@ -199,8 +202,8 @@ const { ipfs, provider } = await getIpfs({
199202

200203
### `windowIpfs`
201204

202-
[`window.ipfs`](https://github.com/ipfs-shipyard/ipfs-companion/blob/master/docs/window.ipfs.md) is created by [ipfs-companion](https://github.com/ipfs/ipfs-companion) browser extension.
203-
It supports passing an optional list of permissions to [display a single ACL prompt](https://github.com/ipfs-shipyard/ipfs-companion/blob/master/docs/window.ipfs.md#do-i-need-to-confirm-every-api-call) the first time it is used:
205+
[`window.ipfs`](https://github.com/ipfs-shipyard/ipfs-companion/blob/master/docs/window.ipfs.md) was an experiment created by [ipfs-companion](https://github.com/ipfs/ipfs-companion) browser extension.
206+
It supported passing an optional list of permissions to [display a single ACL prompt](https://github.com/ipfs-shipyard/ipfs-companion/blob/master/docs/window.ipfs.md#do-i-need-to-confirm-every-api-call) the first time it is used:
204207

205208
```js
206209
const { ipfs, provider } = await getIpfs({

examples/browser-browserify/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
"keywords": [],
1313
"license": "MIT",
1414
"devDependencies": {
15-
"browserify": "^16.5.1",
15+
"browserify": "^16.5.2",
1616
"concat-stream": "^2.0.0",
17-
"execa": "^4.0.0",
18-
"http-server": "~0.12.1",
17+
"execa": "^4.0.3",
18+
"http-server": "~0.12.3",
1919
"ipfs-provider": "file:../../"
2020
},
2121
"dependencies": {
2222
"buffer": "5.6.0",
23-
"ipfs": "0.43.0",
24-
"ipfs-http-client": "44.0.0"
23+
"ipfs": "0.50.1",
24+
"ipfs-http-client": "46.1.1"
2525
},
2626
"browser": {
27-
"ipfs": "ipfs/dist"
27+
"ipfs": "ipfs/dist/index.min.js"
2828
}
2929
}

examples/browser-browserify/src/index.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
3+
const Buffer = require('buffer/').Buffer
44
const { getIpfs, providers } = require('ipfs-provider')
55
const { httpClient, jsIpfs, windowIpfs } = providers
66

@@ -40,13 +40,12 @@ document.addEventListener('DOMContentLoaded', async () => {
4040

4141
async function store () {
4242
const toStore = document.getElementById('source').value
43-
for await (const file of ipfs.add(toStore)) {
44-
if (file && file.cid) {
45-
console.log('successfully stored', file)
46-
await display(file.cid.toString())
47-
} else {
48-
console.error('unable to add', file)
49-
}
43+
const result = await ipfs.add(toStore)
44+
if (result && result.cid) {
45+
console.log('successfully stored', result)
46+
await display(result.cid.toString())
47+
} else {
48+
console.error('unable to ipfs.add', result)
5049
}
5150
}
5251

0 commit comments

Comments
 (0)