From b9d8dd86ab446b9467fbe3e4df7d64e90158c58e Mon Sep 17 00:00:00 2001 From: achingbrain Date: Sat, 2 May 2020 15:23:42 +0100 Subject: [PATCH 1/4] fix: pass headers to request Somewhere along the line we stopped passing headers to the underlying request library which means people can't use http auth etc to try to secure their API servers. Fixes #3017 Depends on: - [ ] https://github.com/ipfs/js-ipfs-utils/pull/41 --- packages/interface-ipfs-core/package.json | 2 +- packages/ipfs-core-utils/package.json | 2 +- packages/ipfs-http-client/package.json | 2 +- packages/ipfs-http-client/src/add.js | 2 +- packages/ipfs-http-client/src/bitswap/stat.js | 3 +- .../ipfs-http-client/src/bitswap/unwant.js | 3 +- .../ipfs-http-client/src/bitswap/wantlist.js | 3 +- packages/ipfs-http-client/src/block/get.js | 3 +- packages/ipfs-http-client/src/block/put.js | 2 +- packages/ipfs-http-client/src/block/rm.js | 3 +- packages/ipfs-http-client/src/block/stat.js | 3 +- .../ipfs-http-client/src/bootstrap/add.js | 3 +- .../ipfs-http-client/src/bootstrap/list.js | 3 +- packages/ipfs-http-client/src/bootstrap/rm.js | 3 +- packages/ipfs-http-client/src/cat.js | 3 +- packages/ipfs-http-client/src/commands.js | 3 +- packages/ipfs-http-client/src/config/get.js | 3 +- .../src/config/profiles/apply.js | 3 +- .../src/config/profiles/list.js | 3 +- .../ipfs-http-client/src/config/replace.js | 2 +- packages/ipfs-http-client/src/config/set.js | 3 +- packages/ipfs-http-client/src/dag/put.js | 2 +- packages/ipfs-http-client/src/dag/resolve.js | 3 +- .../ipfs-http-client/src/dht/find-peer.js | 3 +- .../ipfs-http-client/src/dht/find-provs.js | 3 +- packages/ipfs-http-client/src/dht/get.js | 3 +- packages/ipfs-http-client/src/dht/provide.js | 3 +- packages/ipfs-http-client/src/dht/put.js | 3 +- packages/ipfs-http-client/src/dht/query.js | 3 +- packages/ipfs-http-client/src/diag/cmds.js | 3 +- packages/ipfs-http-client/src/diag/net.js | 3 +- packages/ipfs-http-client/src/diag/sys.js | 3 +- packages/ipfs-http-client/src/dns.js | 3 +- packages/ipfs-http-client/src/files/chmod.js | 3 +- packages/ipfs-http-client/src/files/cp.js | 3 +- packages/ipfs-http-client/src/files/flush.js | 3 +- packages/ipfs-http-client/src/files/ls.js | 3 +- packages/ipfs-http-client/src/files/mkdir.js | 3 +- packages/ipfs-http-client/src/files/mv.js | 3 +- packages/ipfs-http-client/src/files/read.js | 3 +- packages/ipfs-http-client/src/files/rm.js | 3 +- packages/ipfs-http-client/src/files/stat.js | 3 +- packages/ipfs-http-client/src/files/touch.js | 3 +- packages/ipfs-http-client/src/files/write.js | 2 +- packages/ipfs-http-client/src/get.js | 3 +- packages/ipfs-http-client/src/id.js | 3 +- packages/ipfs-http-client/src/key/export.js | 3 +- packages/ipfs-http-client/src/key/gen.js | 3 +- packages/ipfs-http-client/src/key/import.js | 3 +- packages/ipfs-http-client/src/key/list.js | 3 +- packages/ipfs-http-client/src/key/rename.js | 3 +- packages/ipfs-http-client/src/key/rm.js | 3 +- .../src/lib/multipart-request.js | 7 +- packages/ipfs-http-client/src/log/level.js | 3 +- packages/ipfs-http-client/src/log/ls.js | 3 +- packages/ipfs-http-client/src/log/tail.js | 3 +- packages/ipfs-http-client/src/ls.js | 3 +- packages/ipfs-http-client/src/mount.js | 3 +- packages/ipfs-http-client/src/name/publish.js | 3 +- .../src/name/pubsub/cancel.js | 3 +- .../ipfs-http-client/src/name/pubsub/state.js | 3 +- .../ipfs-http-client/src/name/pubsub/subs.js | 3 +- packages/ipfs-http-client/src/name/resolve.js | 3 +- packages/ipfs-http-client/src/object/data.js | 3 +- packages/ipfs-http-client/src/object/get.js | 3 +- packages/ipfs-http-client/src/object/links.js | 3 +- packages/ipfs-http-client/src/object/new.js | 3 +- .../src/object/patch/add-link.js | 3 +- .../src/object/patch/append-data.js | 2 +- .../src/object/patch/rm-link.js | 3 +- .../src/object/patch/set-data.js | 2 +- packages/ipfs-http-client/src/object/put.js | 2 +- packages/ipfs-http-client/src/object/stat.js | 3 +- packages/ipfs-http-client/src/pin/add.js | 3 +- packages/ipfs-http-client/src/pin/ls.js | 3 +- packages/ipfs-http-client/src/pin/rm.js | 3 +- packages/ipfs-http-client/src/ping.js | 1 + packages/ipfs-http-client/src/pubsub/ls.js | 3 +- packages/ipfs-http-client/src/pubsub/peers.js | 3 +- .../ipfs-http-client/src/pubsub/publish.js | 3 +- .../ipfs-http-client/src/pubsub/subscribe.js | 3 +- packages/ipfs-http-client/src/refs/index.js | 1 + packages/ipfs-http-client/src/refs/local.js | 3 +- packages/ipfs-http-client/src/repo/gc.js | 1 + packages/ipfs-http-client/src/repo/stat.js | 3 +- packages/ipfs-http-client/src/repo/version.js | 3 +- packages/ipfs-http-client/src/resolve.js | 3 +- packages/ipfs-http-client/src/stats/bw.js | 1 + packages/ipfs-http-client/src/stop.js | 3 +- packages/ipfs-http-client/src/swarm/addrs.js | 3 +- .../ipfs-http-client/src/swarm/connect.js | 3 +- .../ipfs-http-client/src/swarm/disconnect.js | 3 +- .../ipfs-http-client/src/swarm/localAddrs.js | 3 +- packages/ipfs-http-client/src/swarm/peers.js | 3 +- packages/ipfs-http-client/src/update.js | 3 +- packages/ipfs-http-client/src/version.js | 3 +- .../test/custom-headers.spec.js | 64 +++++++++++++++++-- packages/ipfs/package.json | 2 +- 98 files changed, 237 insertions(+), 102 deletions(-) diff --git a/packages/interface-ipfs-core/package.json b/packages/interface-ipfs-core/package.json index 038a8e4013..e674ebc656 100644 --- a/packages/interface-ipfs-core/package.json +++ b/packages/interface-ipfs-core/package.json @@ -39,7 +39,7 @@ "ipfs-block": "^0.8.1", "ipfs-unixfs": "^1.0.1", "ipfs-unixfs-importer": "^2.0.0", - "ipfs-utils": "^2.2.0", + "ipfs-utils": "ipfs/js-ipfs-utils#fix/respect-headers", "ipld-dag-cbor": "^0.15.1", "ipld-dag-pb": "^0.18.3", "is-ipfs": "^1.0.0", diff --git a/packages/ipfs-core-utils/package.json b/packages/ipfs-core-utils/package.json index 1b82aa12f4..8ef79f25d4 100644 --- a/packages/ipfs-core-utils/package.json +++ b/packages/ipfs-core-utils/package.json @@ -30,7 +30,7 @@ "dependencies": { "buffer": "^5.4.2", "err-code": "^2.0.0", - "ipfs-utils": "^2.2.0" + "ipfs-utils": "ipfs/js-ipfs-utils#fix/respect-headers" }, "devDependencies": { "aegir": "21.4.5", diff --git a/packages/ipfs-http-client/package.json b/packages/ipfs-http-client/package.json index 29776c20fb..e2c42f91f5 100644 --- a/packages/ipfs-http-client/package.json +++ b/packages/ipfs-http-client/package.json @@ -47,7 +47,7 @@ "form-data": "^3.0.0", "ipfs-block": "^0.8.1", "ipfs-core-utils": "^0.2.0", - "ipfs-utils": "^2.2.0", + "ipfs-utils": "ipfs/js-ipfs-utils#fix/respect-headers", "ipld-dag-cbor": "^0.15.1", "ipld-dag-pb": "^0.18.3", "ipld-raw": "^4.0.1", diff --git a/packages/ipfs-http-client/src/add.js b/packages/ipfs-http-client/src/add.js index 7c2cd292c7..097bdefeeb 100644 --- a/packages/ipfs-http-client/src/add.js +++ b/packages/ipfs-http-client/src/add.js @@ -19,7 +19,7 @@ module.exports = configure((api) => { timeout: options.timeout, signal: options.signal, ...( - await multipartRequest(input) + await multipartRequest(input, options.headers) ) }) diff --git a/packages/ipfs-http-client/src/bitswap/stat.js b/packages/ipfs-http-client/src/bitswap/stat.js index c4ceb74924..ec27458969 100644 --- a/packages/ipfs-http-client/src/bitswap/stat.js +++ b/packages/ipfs-http-client/src/bitswap/stat.js @@ -10,7 +10,8 @@ module.exports = configure(api => { const res = await api.post('bitswap/stat', { searchParams: toUrlSearchParams(options), timeout: options.timeout, - signal: options.signal + signal: options.signal, + headers: options.headers }) return toCoreInterface(await res.json()) diff --git a/packages/ipfs-http-client/src/bitswap/unwant.js b/packages/ipfs-http-client/src/bitswap/unwant.js index b78e0093d4..b9706cebc4 100644 --- a/packages/ipfs-http-client/src/bitswap/unwant.js +++ b/packages/ipfs-http-client/src/bitswap/unwant.js @@ -12,7 +12,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: typeof cid === 'string' ? cid : new CID(cid).toString(), ...options - }) + }), + headers: options.headers }) return res.json() diff --git a/packages/ipfs-http-client/src/bitswap/wantlist.js b/packages/ipfs-http-client/src/bitswap/wantlist.js index 2faef35b14..d63d03ced4 100644 --- a/packages/ipfs-http-client/src/bitswap/wantlist.js +++ b/packages/ipfs-http-client/src/bitswap/wantlist.js @@ -13,7 +13,8 @@ module.exports = configure(api => { const res = await (await api.post('bitswap/wantlist', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers })).json() return (res.Keys || []).map(k => new CID(k['/'])) diff --git a/packages/ipfs-http-client/src/block/get.js b/packages/ipfs-http-client/src/block/get.js index 34520aeb32..d5a5bfb66f 100644 --- a/packages/ipfs-http-client/src/block/get.js +++ b/packages/ipfs-http-client/src/block/get.js @@ -16,7 +16,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: cid.toString(), ...options - }) + }), + headers: options.headers }) return new Block(Buffer.from(await res.arrayBuffer()), cid) diff --git a/packages/ipfs-http-client/src/block/put.js b/packages/ipfs-http-client/src/block/put.js index ae9f6a5ac0..8fa0ce1fea 100644 --- a/packages/ipfs-http-client/src/block/put.js +++ b/packages/ipfs-http-client/src/block/put.js @@ -39,7 +39,7 @@ module.exports = configure(api => { signal: options.signal, searchParams: toUrlSearchParams(options), ...( - await multipartRequest(data) + await multipartRequest(data, options.headers) ) }) res = await response.json() diff --git a/packages/ipfs-http-client/src/block/rm.js b/packages/ipfs-http-client/src/block/rm.js index 583643ada6..5f25b9fc95 100644 --- a/packages/ipfs-http-client/src/block/rm.js +++ b/packages/ipfs-http-client/src/block/rm.js @@ -17,7 +17,8 @@ module.exports = configure(api => { arg: cid.map(cid => new CID(cid).toString()), 'stream-channels': true, ...options - }) + }), + headers: options.headers }) for await (const removed of res.ndjson()) { diff --git a/packages/ipfs-http-client/src/block/stat.js b/packages/ipfs-http-client/src/block/stat.js index 264ca2a408..12a3c167b9 100644 --- a/packages/ipfs-http-client/src/block/stat.js +++ b/packages/ipfs-http-client/src/block/stat.js @@ -12,7 +12,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: new CID(cid).toString(), ...options - }) + }), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/bootstrap/add.js b/packages/ipfs-http-client/src/bootstrap/add.js index f4893d4a82..40e67c1c03 100644 --- a/packages/ipfs-http-client/src/bootstrap/add.js +++ b/packages/ipfs-http-client/src/bootstrap/add.js @@ -17,7 +17,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: addr, ...options - }) + }), + headers: options.headers }) return res.json() diff --git a/packages/ipfs-http-client/src/bootstrap/list.js b/packages/ipfs-http-client/src/bootstrap/list.js index 529e172c33..0dc2b7cbf0 100644 --- a/packages/ipfs-http-client/src/bootstrap/list.js +++ b/packages/ipfs-http-client/src/bootstrap/list.js @@ -8,7 +8,8 @@ module.exports = configure(api => { const res = await api.post('bootstrap/list', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) return res.json() diff --git a/packages/ipfs-http-client/src/bootstrap/rm.js b/packages/ipfs-http-client/src/bootstrap/rm.js index 146cfbb9e1..85760b8224 100644 --- a/packages/ipfs-http-client/src/bootstrap/rm.js +++ b/packages/ipfs-http-client/src/bootstrap/rm.js @@ -17,7 +17,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: addr, ...options - }) + }), + headers: options.headers }) return res.json() diff --git a/packages/ipfs-http-client/src/cat.js b/packages/ipfs-http-client/src/cat.js index 896f0d882f..b24d46e71d 100644 --- a/packages/ipfs-http-client/src/cat.js +++ b/packages/ipfs-http-client/src/cat.js @@ -12,7 +12,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: typeof path === 'string' ? path : new CID(path).toString(), ...options - }) + }), + headers: options.headers }) yield * res.iterator() diff --git a/packages/ipfs-http-client/src/commands.js b/packages/ipfs-http-client/src/commands.js index 2df0b397d2..81fbd3efc8 100644 --- a/packages/ipfs-http-client/src/commands.js +++ b/packages/ipfs-http-client/src/commands.js @@ -8,7 +8,8 @@ module.exports = configure(api => { const res = await api.post('commands', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) return res.json() diff --git a/packages/ipfs-http-client/src/config/get.js b/packages/ipfs-http-client/src/config/get.js index 68aa43b36a..3ce848b81e 100644 --- a/packages/ipfs-http-client/src/config/get.js +++ b/packages/ipfs-http-client/src/config/get.js @@ -17,7 +17,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: key, ...options - }) + }), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/config/profiles/apply.js b/packages/ipfs-http-client/src/config/profiles/apply.js index 9d96f48be7..6b8d34dfec 100644 --- a/packages/ipfs-http-client/src/config/profiles/apply.js +++ b/packages/ipfs-http-client/src/config/profiles/apply.js @@ -11,7 +11,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: profile, ...options - }) + }), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/config/profiles/list.js b/packages/ipfs-http-client/src/config/profiles/list.js index 5259dce3e6..a5cde9dc56 100644 --- a/packages/ipfs-http-client/src/config/profiles/list.js +++ b/packages/ipfs-http-client/src/config/profiles/list.js @@ -9,7 +9,8 @@ module.exports = configure(api => { const res = await api.post('config/profile/list', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/config/replace.js b/packages/ipfs-http-client/src/config/replace.js index c07b7e486a..fd688e139c 100644 --- a/packages/ipfs-http-client/src/config/replace.js +++ b/packages/ipfs-http-client/src/config/replace.js @@ -12,7 +12,7 @@ module.exports = configure(api => { signal: options.signal, searchParams: toUrlSearchParams(options), ...( - await multipartRequest(Buffer.from(JSON.stringify(config))) + await multipartRequest(Buffer.from(JSON.stringify(config)), options.headers) ) }) diff --git a/packages/ipfs-http-client/src/config/set.js b/packages/ipfs-http-client/src/config/set.js index 0d828a1f00..4388c8c94a 100644 --- a/packages/ipfs-http-client/src/config/set.js +++ b/packages/ipfs-http-client/src/config/set.js @@ -29,7 +29,8 @@ module.exports = configure(api => { const res = await api.post('config', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(params) + searchParams: toUrlSearchParams(params), + headers: options.headers }) return toCamel(await res.json()) diff --git a/packages/ipfs-http-client/src/dag/put.js b/packages/ipfs-http-client/src/dag/put.js index 78e7cdc96b..01ebdc31c9 100644 --- a/packages/ipfs-http-client/src/dag/put.js +++ b/packages/ipfs-http-client/src/dag/put.js @@ -48,7 +48,7 @@ module.exports = configure(api => { signal: options.signal, searchParams: toUrlSearchParams(options), ...( - await multipartRequest(serialized) + await multipartRequest(serialized, options.headers) ) }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/dag/resolve.js b/packages/ipfs-http-client/src/dag/resolve.js index 9863cb2cb6..23b81adeef 100644 --- a/packages/ipfs-http-client/src/dag/resolve.js +++ b/packages/ipfs-http-client/src/dag/resolve.js @@ -17,7 +17,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: path ? [cid, path].join(path.startsWith('/') ? '' : '/') : `${cid}`, ...options - }) + }), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/dht/find-peer.js b/packages/ipfs-http-client/src/dht/find-peer.js index f1ec56f90d..43bd9ff35b 100644 --- a/packages/ipfs-http-client/src/dht/find-peer.js +++ b/packages/ipfs-http-client/src/dht/find-peer.js @@ -15,7 +15,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: `${Buffer.isBuffer(peerId) ? new CID(peerId) : peerId}`, ...options - }) + }), + headers: options.headers }) for await (const data of res.ndjson()) { diff --git a/packages/ipfs-http-client/src/dht/find-provs.js b/packages/ipfs-http-client/src/dht/find-provs.js index 54df1e689e..77876f0a40 100644 --- a/packages/ipfs-http-client/src/dht/find-provs.js +++ b/packages/ipfs-http-client/src/dht/find-provs.js @@ -14,7 +14,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: `${new CID(cid)}`, ...options - }) + }), + headers: options.headers }) for await (const message of res.ndjson()) { diff --git a/packages/ipfs-http-client/src/dht/get.js b/packages/ipfs-http-client/src/dht/get.js index 52922bfb20..4a99f5b82b 100644 --- a/packages/ipfs-http-client/src/dht/get.js +++ b/packages/ipfs-http-client/src/dht/get.js @@ -18,7 +18,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ key: encodeBufferURIComponent(key), ...options - }) + }), + headers: options.headers }) for await (const message of res.ndjson()) { diff --git a/packages/ipfs-http-client/src/dht/provide.js b/packages/ipfs-http-client/src/dht/provide.js index 1dc7ad4d0b..3105a6f0ea 100644 --- a/packages/ipfs-http-client/src/dht/provide.js +++ b/packages/ipfs-http-client/src/dht/provide.js @@ -16,7 +16,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: cids.map(cid => new CID(cid).toString()), ...options - }) + }), + headers: options.headers }) for await (let message of res.ndjson()) { diff --git a/packages/ipfs-http-client/src/dht/put.js b/packages/ipfs-http-client/src/dht/put.js index 4c4211f472..d345a9ee90 100644 --- a/packages/ipfs-http-client/src/dht/put.js +++ b/packages/ipfs-http-client/src/dht/put.js @@ -17,7 +17,8 @@ module.exports = configure(api => { value ], ...options - }) + }), + headers: options.headers }) for await (let message of res.ndjson()) { diff --git a/packages/ipfs-http-client/src/dht/query.js b/packages/ipfs-http-client/src/dht/query.js index cdca5b6f8d..7c77148d9d 100644 --- a/packages/ipfs-http-client/src/dht/query.js +++ b/packages/ipfs-http-client/src/dht/query.js @@ -14,7 +14,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: new CID(peerId), ...options - }) + }), + headers: options.headers }) for await (let message of res.ndjson()) { diff --git a/packages/ipfs-http-client/src/diag/cmds.js b/packages/ipfs-http-client/src/diag/cmds.js index 0810869b0c..dd0854e77e 100644 --- a/packages/ipfs-http-client/src/diag/cmds.js +++ b/packages/ipfs-http-client/src/diag/cmds.js @@ -8,7 +8,8 @@ module.exports = configure(api => { const res = await api.post('diag/cmds', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) return res.json() diff --git a/packages/ipfs-http-client/src/diag/net.js b/packages/ipfs-http-client/src/diag/net.js index 986e49f12e..3fc0fe9a9a 100644 --- a/packages/ipfs-http-client/src/diag/net.js +++ b/packages/ipfs-http-client/src/diag/net.js @@ -8,7 +8,8 @@ module.exports = configure(api => { const res = await api.post('diag/net', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) return res.json() } diff --git a/packages/ipfs-http-client/src/diag/sys.js b/packages/ipfs-http-client/src/diag/sys.js index ef71243eb7..761edeb8b1 100644 --- a/packages/ipfs-http-client/src/diag/sys.js +++ b/packages/ipfs-http-client/src/diag/sys.js @@ -8,7 +8,8 @@ module.exports = configure(api => { const res = await api.post('diag/sys', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) return res.json() diff --git a/packages/ipfs-http-client/src/dns.js b/packages/ipfs-http-client/src/dns.js index 25fc37f1be..d91368a532 100644 --- a/packages/ipfs-http-client/src/dns.js +++ b/packages/ipfs-http-client/src/dns.js @@ -11,7 +11,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: domain, ...options - }) + }), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/files/chmod.js b/packages/ipfs-http-client/src/files/chmod.js index 438e67fe56..a6853e5d41 100644 --- a/packages/ipfs-http-client/src/files/chmod.js +++ b/packages/ipfs-http-client/src/files/chmod.js @@ -12,7 +12,8 @@ module.exports = configure(api => { arg: path, mode, ...options - }) + }), + headers: options.headers }) await res.text() diff --git a/packages/ipfs-http-client/src/files/cp.js b/packages/ipfs-http-client/src/files/cp.js index 322244692a..39449f60fa 100644 --- a/packages/ipfs-http-client/src/files/cp.js +++ b/packages/ipfs-http-client/src/files/cp.js @@ -15,7 +15,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: sources.map(src => CID.isCID(src) ? `/ipfs/${src}` : src), ...options - }) + }), + headers: options.headers }) await res.text() diff --git a/packages/ipfs-http-client/src/files/flush.js b/packages/ipfs-http-client/src/files/flush.js index e7ad9454ae..cb97a6d98c 100644 --- a/packages/ipfs-http-client/src/files/flush.js +++ b/packages/ipfs-http-client/src/files/flush.js @@ -17,7 +17,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: path, ...options - }) + }), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/files/ls.js b/packages/ipfs-http-client/src/files/ls.js index 0a7d768231..c9a3718729 100644 --- a/packages/ipfs-http-client/src/files/ls.js +++ b/packages/ipfs-http-client/src/files/ls.js @@ -25,7 +25,8 @@ module.exports = configure(api => { l: options.long == null ? true : options.long, ...options, stream: true - }) + }), + headers: options.headers }) for await (const result of res.ndjson()) { diff --git a/packages/ipfs-http-client/src/files/mkdir.js b/packages/ipfs-http-client/src/files/mkdir.js index b4d60e35c5..532add9d8c 100644 --- a/packages/ipfs-http-client/src/files/mkdir.js +++ b/packages/ipfs-http-client/src/files/mkdir.js @@ -11,7 +11,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: path, ...options - }) + }), + headers: options.headers }) await res.text() diff --git a/packages/ipfs-http-client/src/files/mv.js b/packages/ipfs-http-client/src/files/mv.js index 6d12d2578e..db9d648c7a 100644 --- a/packages/ipfs-http-client/src/files/mv.js +++ b/packages/ipfs-http-client/src/files/mv.js @@ -15,7 +15,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: sources.map(src => CID.isCID(src) ? `/ipfs/${src}` : src), ...options - }) + }), + headers: options.headers }) await res.text() diff --git a/packages/ipfs-http-client/src/files/read.js b/packages/ipfs-http-client/src/files/read.js index e7119ae09b..a85e33a1d4 100644 --- a/packages/ipfs-http-client/src/files/read.js +++ b/packages/ipfs-http-client/src/files/read.js @@ -14,7 +14,8 @@ module.exports = configure(api => { arg: path, count: options.count || options.length, ...options - }) + }), + headers: options.headers }) for await (const chunk of toIterable(res.body)) { diff --git a/packages/ipfs-http-client/src/files/rm.js b/packages/ipfs-http-client/src/files/rm.js index 07ef5f9d5e..b07e5a3936 100644 --- a/packages/ipfs-http-client/src/files/rm.js +++ b/packages/ipfs-http-client/src/files/rm.js @@ -14,7 +14,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: sources, ...options - }) + }), + headers: options.headers }) await res.text() diff --git a/packages/ipfs-http-client/src/files/stat.js b/packages/ipfs-http-client/src/files/stat.js index 3833c1aa6a..09cec42e07 100644 --- a/packages/ipfs-http-client/src/files/stat.js +++ b/packages/ipfs-http-client/src/files/stat.js @@ -18,7 +18,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: path, ...options - }) + }), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/files/touch.js b/packages/ipfs-http-client/src/files/touch.js index bfefefea4b..47fa870894 100644 --- a/packages/ipfs-http-client/src/files/touch.js +++ b/packages/ipfs-http-client/src/files/touch.js @@ -11,7 +11,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: path, ...options - }) + }), + headers: options.headers }) await res.text() diff --git a/packages/ipfs-http-client/src/files/write.js b/packages/ipfs-http-client/src/files/write.js index 88a7543135..6f30618dd7 100644 --- a/packages/ipfs-http-client/src/files/write.js +++ b/packages/ipfs-http-client/src/files/write.js @@ -23,7 +23,7 @@ module.exports = configure(api => { path: 'arg', mode: modeToString(options.mode), mtime: mtimeToObject(options.mtime) - }) + }, options.headers) ) }) diff --git a/packages/ipfs-http-client/src/get.js b/packages/ipfs-http-client/src/get.js index 77284054a3..43bcd18e01 100644 --- a/packages/ipfs-http-client/src/get.js +++ b/packages/ipfs-http-client/src/get.js @@ -14,7 +14,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: `${Buffer.isBuffer(path) ? new CID(path) : path}`, ...options - }) + }), + headers: options.headers }) const extractor = Tar.extract() diff --git a/packages/ipfs-http-client/src/id.js b/packages/ipfs-http-client/src/id.js index 985df07010..e868460766 100644 --- a/packages/ipfs-http-client/src/id.js +++ b/packages/ipfs-http-client/src/id.js @@ -10,7 +10,8 @@ module.exports = configure(api => { const res = await api.post('id', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/key/export.js b/packages/ipfs-http-client/src/key/export.js index f65d23536d..1692fef16f 100644 --- a/packages/ipfs-http-client/src/key/export.js +++ b/packages/ipfs-http-client/src/key/export.js @@ -17,7 +17,8 @@ module.exports = configure(api => { arg: name, password: password, ...options - }) + }), + headers: options.headers }) return res.text() diff --git a/packages/ipfs-http-client/src/key/gen.js b/packages/ipfs-http-client/src/key/gen.js index debd2df479..fdb21a00d7 100644 --- a/packages/ipfs-http-client/src/key/gen.js +++ b/packages/ipfs-http-client/src/key/gen.js @@ -12,7 +12,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: name, ...options - }) + }), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/key/import.js b/packages/ipfs-http-client/src/key/import.js index c862b65998..9c68120739 100644 --- a/packages/ipfs-http-client/src/key/import.js +++ b/packages/ipfs-http-client/src/key/import.js @@ -19,7 +19,8 @@ module.exports = configure(api => { pem, password, ...options - }) + }), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/key/list.js b/packages/ipfs-http-client/src/key/list.js index 40b98da7db..868c7b81f6 100644 --- a/packages/ipfs-http-client/src/key/list.js +++ b/packages/ipfs-http-client/src/key/list.js @@ -9,7 +9,8 @@ module.exports = configure(api => { const res = await api.post('key/list', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/key/rename.js b/packages/ipfs-http-client/src/key/rename.js index d05714c674..d435b167c9 100644 --- a/packages/ipfs-http-client/src/key/rename.js +++ b/packages/ipfs-http-client/src/key/rename.js @@ -15,7 +15,8 @@ module.exports = configure(api => { newName ], ...options - }) + }), + headers: options.headers }) return toCamel(await res.json()) diff --git a/packages/ipfs-http-client/src/key/rm.js b/packages/ipfs-http-client/src/key/rm.js index 2f78ce791b..47b8124e3a 100644 --- a/packages/ipfs-http-client/src/key/rm.js +++ b/packages/ipfs-http-client/src/key/rm.js @@ -12,7 +12,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: name, ...options - }) + }), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/lib/multipart-request.js b/packages/ipfs-http-client/src/lib/multipart-request.js index f5441fce97..111a873fef 100644 --- a/packages/ipfs-http-client/src/lib/multipart-request.js +++ b/packages/ipfs-http-client/src/lib/multipart-request.js @@ -5,8 +5,9 @@ const toStream = require('./to-stream') const { nanoid } = require('nanoid') const modeToString = require('../lib/mode-to-string') const mtimeToObject = require('../lib/mtime-to-object') +const merge = require('merge-options').bind({ ignoreUndefined: true }) -async function multipartRequest (source, boundary = `-----------------------------${nanoid()}`) { +async function multipartRequest (source, headers = {}, boundary = `-----------------------------${nanoid()}`) { async function * streamFiles (source) { try { let index = 0 @@ -55,9 +56,9 @@ async function multipartRequest (source, boundary = `--------------------------- } return { - headers: { + headers: merge(headers, { 'Content-Type': `multipart/form-data; boundary=${boundary}` - }, + }), body: await toStream(streamFiles(source)) } } diff --git a/packages/ipfs-http-client/src/log/level.js b/packages/ipfs-http-client/src/log/level.js index 89d7fa6709..39f6396dda 100644 --- a/packages/ipfs-http-client/src/log/level.js +++ b/packages/ipfs-http-client/src/log/level.js @@ -15,7 +15,8 @@ module.exports = configure(api => { level ], ...options - }) + }), + headers: options.headers }) return toCamel(await res.json()) diff --git a/packages/ipfs-http-client/src/log/ls.js b/packages/ipfs-http-client/src/log/ls.js index b054c0f4a2..d43c5f0c65 100644 --- a/packages/ipfs-http-client/src/log/ls.js +++ b/packages/ipfs-http-client/src/log/ls.js @@ -8,7 +8,8 @@ module.exports = configure(api => { const res = await api.post('log/ls', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/log/tail.js b/packages/ipfs-http-client/src/log/tail.js index 7d81305a9d..e3238b19f8 100644 --- a/packages/ipfs-http-client/src/log/tail.js +++ b/packages/ipfs-http-client/src/log/tail.js @@ -8,7 +8,8 @@ module.exports = configure(api => { const res = await api.post('log/tail', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) yield * res.ndjson() diff --git a/packages/ipfs-http-client/src/ls.js b/packages/ipfs-http-client/src/ls.js index baa5bffca4..b448a0c2ee 100644 --- a/packages/ipfs-http-client/src/ls.js +++ b/packages/ipfs-http-client/src/ls.js @@ -13,7 +13,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: `${Buffer.isBuffer(path) ? new CID(path) : path}`, ...options - }) + }), + headers: options.headers }) for await (let result of res.ndjson()) { diff --git a/packages/ipfs-http-client/src/mount.js b/packages/ipfs-http-client/src/mount.js index c60fa9964d..56b35e58e4 100644 --- a/packages/ipfs-http-client/src/mount.js +++ b/packages/ipfs-http-client/src/mount.js @@ -9,7 +9,8 @@ module.exports = configure(api => { const res = await api.post('dns', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) return toCamel(await res.json()) diff --git a/packages/ipfs-http-client/src/name/publish.js b/packages/ipfs-http-client/src/name/publish.js index ec4e7e1497..4d98221cc2 100644 --- a/packages/ipfs-http-client/src/name/publish.js +++ b/packages/ipfs-http-client/src/name/publish.js @@ -12,7 +12,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: path, ...options - }) + }), + headers: options.headers }) return toCamel(await res.json()) diff --git a/packages/ipfs-http-client/src/name/pubsub/cancel.js b/packages/ipfs-http-client/src/name/pubsub/cancel.js index 8941d0f425..28c483d466 100644 --- a/packages/ipfs-http-client/src/name/pubsub/cancel.js +++ b/packages/ipfs-http-client/src/name/pubsub/cancel.js @@ -12,7 +12,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: name, ...options - }) + }), + headers: options.headers }) return toCamel(await res.json()) diff --git a/packages/ipfs-http-client/src/name/pubsub/state.js b/packages/ipfs-http-client/src/name/pubsub/state.js index b32c0046c6..5d6a95d28c 100644 --- a/packages/ipfs-http-client/src/name/pubsub/state.js +++ b/packages/ipfs-http-client/src/name/pubsub/state.js @@ -9,7 +9,8 @@ module.exports = configure(api => { const res = await api.post('name/pubsub/state', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) return toCamel(await res.json()) diff --git a/packages/ipfs-http-client/src/name/pubsub/subs.js b/packages/ipfs-http-client/src/name/pubsub/subs.js index 57afe590fc..bc4697868b 100644 --- a/packages/ipfs-http-client/src/name/pubsub/subs.js +++ b/packages/ipfs-http-client/src/name/pubsub/subs.js @@ -8,7 +8,8 @@ module.exports = configure(api => { const res = await api.post('name/pubsub/subs', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/name/resolve.js b/packages/ipfs-http-client/src/name/resolve.js index 2d947c3c23..f168828242 100644 --- a/packages/ipfs-http-client/src/name/resolve.js +++ b/packages/ipfs-http-client/src/name/resolve.js @@ -12,7 +12,8 @@ module.exports = configure(api => { arg: path, ...options, stream: true - }) + }), + headers: options.headers }) for await (const result of res.ndjson()) { diff --git a/packages/ipfs-http-client/src/object/data.js b/packages/ipfs-http-client/src/object/data.js index 58fba5e10a..39446a9934 100644 --- a/packages/ipfs-http-client/src/object/data.js +++ b/packages/ipfs-http-client/src/object/data.js @@ -13,7 +13,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: `${Buffer.isBuffer(cid) ? new CID(cid) : cid}`, ...options - }) + }), + headers: options.headers }) const data = await res.arrayBuffer() diff --git a/packages/ipfs-http-client/src/object/get.js b/packages/ipfs-http-client/src/object/get.js index 51a942eaf9..0ad60f7bff 100644 --- a/packages/ipfs-http-client/src/object/get.js +++ b/packages/ipfs-http-client/src/object/get.js @@ -15,7 +15,8 @@ module.exports = configure(api => { arg: `${Buffer.isBuffer(cid) ? new CID(cid) : cid}`, dataEncoding: 'base64', ...options - }) + }), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/object/links.js b/packages/ipfs-http-client/src/object/links.js index 011d04ae20..abe71de547 100644 --- a/packages/ipfs-http-client/src/object/links.js +++ b/packages/ipfs-http-client/src/object/links.js @@ -14,7 +14,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: `${Buffer.isBuffer(cid) ? new CID(cid) : cid}`, ...options - }) + }), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/object/new.js b/packages/ipfs-http-client/src/object/new.js index 98aa290e18..6b3bb04ebc 100644 --- a/packages/ipfs-http-client/src/object/new.js +++ b/packages/ipfs-http-client/src/object/new.js @@ -17,7 +17,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: template, ...options - }) + }), + headers: options.headers }) const { Hash } = await res.json() diff --git a/packages/ipfs-http-client/src/object/patch/add-link.js b/packages/ipfs-http-client/src/object/patch/add-link.js index 1740023358..a60248ab2b 100644 --- a/packages/ipfs-http-client/src/object/patch/add-link.js +++ b/packages/ipfs-http-client/src/object/patch/add-link.js @@ -17,7 +17,8 @@ module.exports = configure(api => { (dLink.Hash || dLink.cid || '').toString() || null ], ...options - }) + }), + headers: options.headers }) const { Hash } = await res.json() diff --git a/packages/ipfs-http-client/src/object/patch/append-data.js b/packages/ipfs-http-client/src/object/patch/append-data.js index 69349189cc..e41e436d97 100644 --- a/packages/ipfs-http-client/src/object/patch/append-data.js +++ b/packages/ipfs-http-client/src/object/patch/append-data.js @@ -16,7 +16,7 @@ module.exports = configure(api => { ...options }), ...( - await multipartRequest(data) + await multipartRequest(data, options.headers) ) }) diff --git a/packages/ipfs-http-client/src/object/patch/rm-link.js b/packages/ipfs-http-client/src/object/patch/rm-link.js index b7dc25014f..89f02b77b7 100644 --- a/packages/ipfs-http-client/src/object/patch/rm-link.js +++ b/packages/ipfs-http-client/src/object/patch/rm-link.js @@ -16,7 +16,8 @@ module.exports = configure(api => { dLink.Name || dLink.name || null ], ...options - }) + }), + headers: options.headers }) const { Hash } = await res.json() diff --git a/packages/ipfs-http-client/src/object/patch/set-data.js b/packages/ipfs-http-client/src/object/patch/set-data.js index 779cc0e57c..6764de3c6c 100644 --- a/packages/ipfs-http-client/src/object/patch/set-data.js +++ b/packages/ipfs-http-client/src/object/patch/set-data.js @@ -18,7 +18,7 @@ module.exports = configure(api => { ...options }), ...( - await multipartRequest(data) + await multipartRequest(data, options.headers) ) })).json() diff --git a/packages/ipfs-http-client/src/object/put.js b/packages/ipfs-http-client/src/object/put.js index a09761c274..a0c7bc86d2 100644 --- a/packages/ipfs-http-client/src/object/put.js +++ b/packages/ipfs-http-client/src/object/put.js @@ -49,7 +49,7 @@ module.exports = configure(api => { signal: options.signal, searchParams: toUrlSearchParams(options), ...( - await multipartRequest(buf) + await multipartRequest(buf, options.headers) ) }) diff --git a/packages/ipfs-http-client/src/object/stat.js b/packages/ipfs-http-client/src/object/stat.js index af9bae97e7..fbbab191b9 100644 --- a/packages/ipfs-http-client/src/object/stat.js +++ b/packages/ipfs-http-client/src/object/stat.js @@ -15,7 +15,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: `${Buffer.isBuffer(cid) ? new CID(cid) : cid}`, ...options - }) + }), + headers: options.headers })).json() } catch (err) { if (err.name === 'TimeoutError') { diff --git a/packages/ipfs-http-client/src/pin/add.js b/packages/ipfs-http-client/src/pin/add.js index ff6326475c..6180f5d3df 100644 --- a/packages/ipfs-http-client/src/pin/add.js +++ b/packages/ipfs-http-client/src/pin/add.js @@ -14,7 +14,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: paths.map(path => `${path}`), ...options - }) + }), + headers: options.headers })).json() return (res.Pins || []).map(cid => ({ cid: new CID(cid) })) diff --git a/packages/ipfs-http-client/src/pin/ls.js b/packages/ipfs-http-client/src/pin/ls.js index bfac8b5857..1158ec394d 100644 --- a/packages/ipfs-http-client/src/pin/ls.js +++ b/packages/ipfs-http-client/src/pin/ls.js @@ -20,7 +20,8 @@ module.exports = configure(api => { arg: path.map(p => `${p}`), ...options, stream: true - }) + }), + headers: options.headers }) for await (const pin of res.ndjson()) { diff --git a/packages/ipfs-http-client/src/pin/rm.js b/packages/ipfs-http-client/src/pin/rm.js index d97cc6b824..a7d9b5e73b 100644 --- a/packages/ipfs-http-client/src/pin/rm.js +++ b/packages/ipfs-http-client/src/pin/rm.js @@ -12,7 +12,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: `${path}`, ...options - }) + }), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/ping.js b/packages/ipfs-http-client/src/ping.js index d6d9a87722..f4710b7f6b 100644 --- a/packages/ipfs-http-client/src/ping.js +++ b/packages/ipfs-http-client/src/ping.js @@ -13,6 +13,7 @@ module.exports = configure(api => { arg: `${peerId}`, ...options }), + headers: options.headers, transform: toCamel }) diff --git a/packages/ipfs-http-client/src/pubsub/ls.js b/packages/ipfs-http-client/src/pubsub/ls.js index d9693bc658..985f069afd 100644 --- a/packages/ipfs-http-client/src/pubsub/ls.js +++ b/packages/ipfs-http-client/src/pubsub/ls.js @@ -8,7 +8,8 @@ module.exports = configure(api => { const { Strings } = await (await api.post('pubsub/ls', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers })).json() return Strings || [] diff --git a/packages/ipfs-http-client/src/pubsub/peers.js b/packages/ipfs-http-client/src/pubsub/peers.js index 42282e2d66..b8847d0c67 100644 --- a/packages/ipfs-http-client/src/pubsub/peers.js +++ b/packages/ipfs-http-client/src/pubsub/peers.js @@ -16,7 +16,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: topic, ...options - }) + }), + headers: options.headers }) const { Strings } = await res.json() diff --git a/packages/ipfs-http-client/src/pubsub/publish.js b/packages/ipfs-http-client/src/pubsub/publish.js index 743c0dc75a..e7fe921ca7 100644 --- a/packages/ipfs-http-client/src/pubsub/publish.js +++ b/packages/ipfs-http-client/src/pubsub/publish.js @@ -18,7 +18,8 @@ module.exports = configure(api => { const res = await api.post(`pubsub/pub?${searchParams}&arg=${encodeBuffer(data)}`, { timeout: options.timeout, - signal: options.signal + signal: options.signal, + headers: options.headers }) await res.text() diff --git a/packages/ipfs-http-client/src/pubsub/subscribe.js b/packages/ipfs-http-client/src/pubsub/subscribe.js index 5d071a2f11..b09db18404 100644 --- a/packages/ipfs-http-client/src/pubsub/subscribe.js +++ b/packages/ipfs-http-client/src/pubsub/subscribe.js @@ -35,7 +35,8 @@ module.exports = configure((api, options) => { searchParams: toUrlSearchParams({ arg: topic, ...options - }) + }), + headers: options.headers }) } catch (err) { // Initial subscribe fail, ensure we clean up subsTracker.unsubscribe(topic, handler) diff --git a/packages/ipfs-http-client/src/refs/index.js b/packages/ipfs-http-client/src/refs/index.js index b2bf49fff7..c624434c70 100644 --- a/packages/ipfs-http-client/src/refs/index.js +++ b/packages/ipfs-http-client/src/refs/index.js @@ -19,6 +19,7 @@ module.exports = configure((api, options) => { arg: args.map(arg => `${Buffer.isBuffer(arg) ? new CID(arg) : arg}`), ...options }), + headers: options.headers, transform: toCamel }) diff --git a/packages/ipfs-http-client/src/refs/local.js b/packages/ipfs-http-client/src/refs/local.js index 7c0db1ed53..845d320c37 100644 --- a/packages/ipfs-http-client/src/refs/local.js +++ b/packages/ipfs-http-client/src/refs/local.js @@ -10,7 +10,8 @@ module.exports = configure(api => { timeout: options.timeout, signal: options.signal, transform: toCamel, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) yield * res.ndjson() diff --git a/packages/ipfs-http-client/src/repo/gc.js b/packages/ipfs-http-client/src/repo/gc.js index 402cbf71b6..7d69e05957 100644 --- a/packages/ipfs-http-client/src/repo/gc.js +++ b/packages/ipfs-http-client/src/repo/gc.js @@ -10,6 +10,7 @@ module.exports = configure(api => { timeout: options.timeout, signal: options.signal, searchParams: toUrlSearchParams(options), + headers: options.headers, transform: (res) => { return { err: res.Error ? new Error(res.Error) : null, diff --git a/packages/ipfs-http-client/src/repo/stat.js b/packages/ipfs-http-client/src/repo/stat.js index 5ff183e268..200ab9b433 100644 --- a/packages/ipfs-http-client/src/repo/stat.js +++ b/packages/ipfs-http-client/src/repo/stat.js @@ -9,7 +9,8 @@ module.exports = configure(api => { const res = await api.post('repo/stat', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/src/repo/version.js b/packages/ipfs-http-client/src/repo/version.js index fef43858c0..450b3b52a7 100644 --- a/packages/ipfs-http-client/src/repo/version.js +++ b/packages/ipfs-http-client/src/repo/version.js @@ -8,7 +8,8 @@ module.exports = configure(api => { const res = await (await api.post('repo/version', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers })).json() return res.Version diff --git a/packages/ipfs-http-client/src/resolve.js b/packages/ipfs-http-client/src/resolve.js index 0a99e501aa..a7635dad13 100644 --- a/packages/ipfs-http-client/src/resolve.js +++ b/packages/ipfs-http-client/src/resolve.js @@ -11,7 +11,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: path, ...options - }) + }), + headers: options.headers }) const { Path } = await res.json() return Path diff --git a/packages/ipfs-http-client/src/stats/bw.js b/packages/ipfs-http-client/src/stats/bw.js index 791d09c06a..f4997ca0da 100644 --- a/packages/ipfs-http-client/src/stats/bw.js +++ b/packages/ipfs-http-client/src/stats/bw.js @@ -10,6 +10,7 @@ module.exports = configure(api => { timeout: options.timeout, signal: options.signal, searchParams: toUrlSearchParams(options), + headers: options.headers, transform: (stats) => ({ totalIn: new BigNumber(stats.TotalIn), totalOut: new BigNumber(stats.TotalOut), diff --git a/packages/ipfs-http-client/src/stop.js b/packages/ipfs-http-client/src/stop.js index 991092ec91..d5157cdef9 100644 --- a/packages/ipfs-http-client/src/stop.js +++ b/packages/ipfs-http-client/src/stop.js @@ -8,7 +8,8 @@ module.exports = configure(api => { const res = await api.post('shutdown', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) await res.text() diff --git a/packages/ipfs-http-client/src/swarm/addrs.js b/packages/ipfs-http-client/src/swarm/addrs.js index 42bc12d64e..199585b78a 100644 --- a/packages/ipfs-http-client/src/swarm/addrs.js +++ b/packages/ipfs-http-client/src/swarm/addrs.js @@ -9,7 +9,8 @@ module.exports = configure(api => { const res = await api.post('swarm/addrs', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) const { Addrs } = await res.json() diff --git a/packages/ipfs-http-client/src/swarm/connect.js b/packages/ipfs-http-client/src/swarm/connect.js index 363616a4f8..7b939577b4 100644 --- a/packages/ipfs-http-client/src/swarm/connect.js +++ b/packages/ipfs-http-client/src/swarm/connect.js @@ -13,7 +13,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: addrs.map(addr => `${addr}`), ...options - }) + }), + headers: options.headers }) const { Strings } = await res.json() diff --git a/packages/ipfs-http-client/src/swarm/disconnect.js b/packages/ipfs-http-client/src/swarm/disconnect.js index b585c710c6..956c384431 100644 --- a/packages/ipfs-http-client/src/swarm/disconnect.js +++ b/packages/ipfs-http-client/src/swarm/disconnect.js @@ -13,7 +13,8 @@ module.exports = configure(api => { searchParams: toUrlSearchParams({ arg: addrs.map(addr => `${addr}`), ...options - }) + }), + headers: options.headers }) const { Strings } = await res.json() diff --git a/packages/ipfs-http-client/src/swarm/localAddrs.js b/packages/ipfs-http-client/src/swarm/localAddrs.js index 9e59a2f28f..23d2874f2f 100644 --- a/packages/ipfs-http-client/src/swarm/localAddrs.js +++ b/packages/ipfs-http-client/src/swarm/localAddrs.js @@ -9,7 +9,8 @@ module.exports = configure(api => { const res = await api.post('swarm/addrs/local', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) const { Strings } = await res.json() diff --git a/packages/ipfs-http-client/src/swarm/peers.js b/packages/ipfs-http-client/src/swarm/peers.js index ef96813a4d..ed1b0f7d85 100644 --- a/packages/ipfs-http-client/src/swarm/peers.js +++ b/packages/ipfs-http-client/src/swarm/peers.js @@ -9,7 +9,8 @@ module.exports = configure(api => { const res = await (await api.post('swarm/peers', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers })).json() return (res.Peers || []).map(peer => { diff --git a/packages/ipfs-http-client/src/update.js b/packages/ipfs-http-client/src/update.js index 618080ceda..026eff5b84 100644 --- a/packages/ipfs-http-client/src/update.js +++ b/packages/ipfs-http-client/src/update.js @@ -8,7 +8,8 @@ module.exports = configure(api => { const res = await api.post('update', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) await res.text() diff --git a/packages/ipfs-http-client/src/version.js b/packages/ipfs-http-client/src/version.js index 872ded6631..28c49101bd 100644 --- a/packages/ipfs-http-client/src/version.js +++ b/packages/ipfs-http-client/src/version.js @@ -9,7 +9,8 @@ module.exports = configure(api => { const res = await api.post('version', { timeout: options.timeout, signal: options.signal, - searchParams: toUrlSearchParams(options) + searchParams: toUrlSearchParams(options), + headers: options.headers }) const data = await res.json() diff --git a/packages/ipfs-http-client/test/custom-headers.spec.js b/packages/ipfs-http-client/test/custom-headers.spec.js index 5e88b72a6a..a728e33d74 100644 --- a/packages/ipfs-http-client/test/custom-headers.spec.js +++ b/packages/ipfs-http-client/test/custom-headers.spec.js @@ -16,12 +16,12 @@ describe('custom headers', function () { port: 6001, protocol: 'http', headers: { - authorization: 'Bearer ' + 'YOLO' + authorization: 'Bearer YOLO' } }) }) - it('are supported', (done) => { + it('are supported in the client constructor', (done) => { // spin up a test http server to inspect the requests made by the library const server = require('http').createServer((req, res) => { req.on('data', () => {}) @@ -30,19 +30,69 @@ describe('custom headers', function () { res.write(JSON.stringify({})) res.end() // ensure custom headers are present - expect(req.headers.authorization).to.equal('Bearer ' + 'YOLO') + expect(req.headers.authorization).to.equal('Bearer YOLO') server.close() done() }) }) server.listen(6001, () => { - ipfs.id((err, res) => { - if (err) { - throw err + // this call is used to test that headers are being sent. + ipfs.id() + .then(() => {}, done) + }) + }) + + it('are supported in API calls', (done) => { + // spin up a test http server to inspect the requests made by the library + const server = require('http').createServer((req, res) => { + req.on('data', () => {}) + req.on('end', () => { + res.writeHead(200) + res.write(JSON.stringify({})) + res.end() + // ensure custom headers are present + expect(req.headers.authorization).to.equal('Bearer OLOY') + server.close() + done() + }) + }) + + server.listen(6001, () => { + // this call is used to test that headers are being sent. + ipfs.id({ + headers: { + authorization: 'Bearer OLOY' + } + }) + .then(() => {}, done) + }) + }) + + it('are supported in multipart API calls', (done) => { + // spin up a test http server to inspect the requests made by the library + const server = require('http').createServer((req, res) => { + req.on('data', () => {}) + req.on('end', () => { + res.writeHead(200) + res.write(JSON.stringify({})) + res.end() + // ensure custom headers are present + expect(req.headers.authorization).to.equal('Bearer OYLO') + server.close() + done() + }) + }) + + server.listen(6001, () => { + // this call is used to test that headers are being sent. + ipfs.files.write('/foo/bar', Buffer.from('derp'), { + create: true, + headers: { + authorization: 'Bearer OYLO' } - // this call is used to test that headers are being sent. }) + .then(() => {}, done) }) }) }) diff --git a/packages/ipfs/package.json b/packages/ipfs/package.json index 8860e9c5b4..8705260e0f 100644 --- a/packages/ipfs/package.json +++ b/packages/ipfs/package.json @@ -106,7 +106,7 @@ "ipfs-unixfs": "^1.0.1", "ipfs-unixfs-exporter": "^2.0.0", "ipfs-unixfs-importer": "^2.0.0", - "ipfs-utils": "^2.2.0", + "ipfs-utils": "ipfs/js-ipfs-utils#fix/respect-headers", "ipld": "^0.25.0", "ipld-bitcoin": "^0.3.0", "ipld-dag-cbor": "^0.15.1", From 96bca07cc5393d1912ecd64f266363dfba6ed4ae Mon Sep 17 00:00:00 2001 From: achingbrain Date: Sun, 3 May 2020 08:39:27 +0100 Subject: [PATCH 2/4] test: add tests for custom headers in other call types --- .../ipfs-http-client/src/lib/configure.js | 4 +- .../test/custom-headers.spec.js | 135 +++++++++--------- 2 files changed, 72 insertions(+), 67 deletions(-) diff --git a/packages/ipfs-http-client/src/lib/configure.js b/packages/ipfs-http-client/src/lib/configure.js index 49b2d2f5a3..c30a6add6a 100644 --- a/packages/ipfs-http-client/src/lib/configure.js +++ b/packages/ipfs-http-client/src/lib/configure.js @@ -14,7 +14,9 @@ const Client = require('./core') */ const configure = (fn) => { return (options) => { - return fn(new Client(options), options) + return fn({ + post: (...args) => new Client(options).post(...args) + }, options) } } module.exports = configure diff --git a/packages/ipfs-http-client/test/custom-headers.spec.js b/packages/ipfs-http-client/test/custom-headers.spec.js index a728e33d74..f989455197 100644 --- a/packages/ipfs-http-client/test/custom-headers.spec.js +++ b/packages/ipfs-http-client/test/custom-headers.spec.js @@ -5,94 +5,97 @@ const { isNode } = require('ipfs-utils/src/env') const { expect } = require('interface-ipfs-core/src/utils/mocha') const ipfsClient = require('../src') +function startServer (fn) { + let headersResolve + const headers = new Promise((resolve) => { + headersResolve = resolve + }) + + // spin up a test http server to inspect the requests made by the library + const server = require('http').createServer((req, res) => { + req.on('data', () => {}) + req.on('end', () => { + res.writeHead(200) + res.write(JSON.stringify({})) + res.end() + server.close() + + headersResolve(req.headers) + }) + }) + + server.listen(6001, () => { + fn().then(() => {}, () => {}) + }) + + return headers +} + describe('custom headers', function () { // do not test in browser - if (!isNode) { return } + if (!isNode) { + return + } + let ipfs - // initialize ipfs with custom headers - before(() => { - ipfs = ipfsClient({ - host: 'localhost', - port: 6001, - protocol: 'http', - headers: { - authorization: 'Bearer YOLO' - } - }) - }) - it('are supported in the client constructor', (done) => { - // spin up a test http server to inspect the requests made by the library - const server = require('http').createServer((req, res) => { - req.on('data', () => {}) - req.on('end', () => { - res.writeHead(200) - res.write(JSON.stringify({})) - res.end() - // ensure custom headers are present - expect(req.headers.authorization).to.equal('Bearer YOLO') - server.close() - done() + describe('supported in the constructor', () => { + // initialize ipfs with custom headers + before(() => { + ipfs = ipfsClient({ + host: 'localhost', + port: 6001, + protocol: 'http', + headers: { + authorization: 'Bearer YOLO' + } }) }) - server.listen(6001, () => { - // this call is used to test that headers are being sent. - ipfs.id() - .then(() => {}, done) + it('regular API calls', async () => { + const headers = await startServer(() => ipfs.id()) + + expect(headers.authorization).to.equal('Bearer YOLO') + }) + + it('multipart API calls', async () => { + const headers = await startServer(() => ipfs.files.write('/foo/bar', Buffer.from('derp'), { + create: true + })) + + expect(headers.authorization).to.equal('Bearer YOLO') }) }) - it('are supported in API calls', (done) => { - // spin up a test http server to inspect the requests made by the library - const server = require('http').createServer((req, res) => { - req.on('data', () => {}) - req.on('end', () => { - res.writeHead(200) - res.write(JSON.stringify({})) - res.end() - // ensure custom headers are present - expect(req.headers.authorization).to.equal('Bearer OLOY') - server.close() - done() + describe('supported as API call arguemnts', () => { + // initialize ipfs with custom headers + before(() => { + ipfs = ipfsClient({ + host: 'localhost', + port: 6001, + protocol: 'http' }) }) - server.listen(6001, () => { - // this call is used to test that headers are being sent. - ipfs.id({ + it('regular API calls', async () => { + const headers = await startServer(() => ipfs.id({ headers: { authorization: 'Bearer OLOY' } - }) - .then(() => {}, done) - }) - }) + })) - it('are supported in multipart API calls', (done) => { - // spin up a test http server to inspect the requests made by the library - const server = require('http').createServer((req, res) => { - req.on('data', () => {}) - req.on('end', () => { - res.writeHead(200) - res.write(JSON.stringify({})) - res.end() - // ensure custom headers are present - expect(req.headers.authorization).to.equal('Bearer OYLO') - server.close() - done() - }) + expect(headers.authorization).to.equal('Bearer OLOY') }) - server.listen(6001, () => { - // this call is used to test that headers are being sent. - ipfs.files.write('/foo/bar', Buffer.from('derp'), { + it('multipart API calls', async () => { + const headers = await startServer(() => ipfs.files.write('/foo/bar', Buffer.from('derp'), { create: true, headers: { - authorization: 'Bearer OYLO' + authorization: 'Bearer OLOY' } - }) - .then(() => {}, done) + })) + + expect(headers.authorization).to.equal('Bearer OLOY') }) }) }) From 29dda2bcd45afbee89322a7869fa2bc90e3fb9be Mon Sep 17 00:00:00 2001 From: achingbrain Date: Sun, 3 May 2020 08:53:38 +0100 Subject: [PATCH 3/4] chore: bug has been fixed in http utils --- packages/ipfs-http-client/src/lib/configure.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/ipfs-http-client/src/lib/configure.js b/packages/ipfs-http-client/src/lib/configure.js index c30a6add6a..49b2d2f5a3 100644 --- a/packages/ipfs-http-client/src/lib/configure.js +++ b/packages/ipfs-http-client/src/lib/configure.js @@ -14,9 +14,7 @@ const Client = require('./core') */ const configure = (fn) => { return (options) => { - return fn({ - post: (...args) => new Client(options).post(...args) - }, options) + return fn(new Client(options), options) } } module.exports = configure From 3ec089d4ad193bf915eaa1d4bdb8716f5f3ca7c2 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 5 May 2020 15:26:02 +0100 Subject: [PATCH 4/4] chore: dep update --- examples/circuit-relaying/package.json | 2 +- packages/interface-ipfs-core/package.json | 4 ++-- packages/ipfs-core-utils/package.json | 4 ++-- packages/ipfs-http-client/package.json | 4 ++-- packages/ipfs/package.json | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/circuit-relaying/package.json b/examples/circuit-relaying/package.json index 5c3bea6a54..6029995376 100644 --- a/examples/circuit-relaying/package.json +++ b/examples/circuit-relaying/package.json @@ -18,7 +18,7 @@ "ipfs-pubsub-room": "^2.0.1" }, "devDependencies": { - "aegir": "21.4.5", + "aegir": "^21.10.0", "execa": "^4.0.0", "ipfs-css": "^0.13.1", "ipfs-http-client": "^43.0.1", diff --git a/packages/interface-ipfs-core/package.json b/packages/interface-ipfs-core/package.json index e674ebc656..5930b3fcf5 100644 --- a/packages/interface-ipfs-core/package.json +++ b/packages/interface-ipfs-core/package.json @@ -39,7 +39,7 @@ "ipfs-block": "^0.8.1", "ipfs-unixfs": "^1.0.1", "ipfs-unixfs-importer": "^2.0.0", - "ipfs-utils": "ipfs/js-ipfs-utils#fix/respect-headers", + "ipfs-utils": "^2.2.2", "ipld-dag-cbor": "^0.15.1", "ipld-dag-pb": "^0.18.3", "is-ipfs": "^1.0.0", @@ -58,7 +58,7 @@ "temp-write": "^4.0.0" }, "devDependencies": { - "aegir": "21.4.5", + "aegir": "^21.10.0", "ipfsd-ctl": "^3.0.0" }, "contributors": [ diff --git a/packages/ipfs-core-utils/package.json b/packages/ipfs-core-utils/package.json index 8ef79f25d4..b9cbab6515 100644 --- a/packages/ipfs-core-utils/package.json +++ b/packages/ipfs-core-utils/package.json @@ -30,10 +30,10 @@ "dependencies": { "buffer": "^5.4.2", "err-code": "^2.0.0", - "ipfs-utils": "ipfs/js-ipfs-utils#fix/respect-headers" + "ipfs-utils": "^2.2.2" }, "devDependencies": { - "aegir": "21.4.5", + "aegir": "^21.10.0", "chai": "^4.2.0", "chai-as-promised": "^7.1.1", "delay": "^4.3.0", diff --git a/packages/ipfs-http-client/package.json b/packages/ipfs-http-client/package.json index e2c42f91f5..e6086dcbdc 100644 --- a/packages/ipfs-http-client/package.json +++ b/packages/ipfs-http-client/package.json @@ -47,7 +47,7 @@ "form-data": "^3.0.0", "ipfs-block": "^0.8.1", "ipfs-core-utils": "^0.2.0", - "ipfs-utils": "ipfs/js-ipfs-utils#fix/respect-headers", + "ipfs-utils": "^2.2.2", "ipld-dag-cbor": "^0.15.1", "ipld-dag-pb": "^0.18.3", "ipld-raw": "^4.0.1", @@ -67,7 +67,7 @@ "stream-to-it": "^0.2.0" }, "devDependencies": { - "aegir": "21.4.5", + "aegir": "^21.10.0", "browser-process-platform": "^0.1.1", "cross-env": "^7.0.0", "go-ipfs-dep": "0.4.23-3", diff --git a/packages/ipfs/package.json b/packages/ipfs/package.json index 8705260e0f..989c972eed 100644 --- a/packages/ipfs/package.json +++ b/packages/ipfs/package.json @@ -106,7 +106,7 @@ "ipfs-unixfs": "^1.0.1", "ipfs-unixfs-exporter": "^2.0.0", "ipfs-unixfs-importer": "^2.0.0", - "ipfs-utils": "ipfs/js-ipfs-utils#fix/respect-headers", + "ipfs-utils": "^2.2.2", "ipld": "^0.25.0", "ipld-bitcoin": "^0.3.0", "ipld-dag-cbor": "^0.15.1", @@ -176,7 +176,7 @@ "yargs-promise": "^1.1.0" }, "devDependencies": { - "aegir": "21.4.5", + "aegir": "^21.10.0", "base64url": "^3.0.1", "clear-module": "^4.0.0", "cross-env": "^7.0.0",