forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR enabled pubsub in the browser and paves the way for a switch to using `fetch` by default and allowing for cancelable requests via the use of `AbortController`. It's mostly the work done in ipfs-shipyard/js-ipfs-http-client-lite#1 but adapted a bit for use here. If approved, we can start work moving the other commands to use `fetch`. The work in https://github.com/ipfs-shipyard/js-ipfs-http-client-lite has proven the hard parts (uploading files) are all possible using the `fetch` API. Since `fetch` is promise based, when moving the other commands it makes sense to just switch to async/await as per ipfs#1670 (and callbackify instead of promisify). Depends on: * [x] ipfs-inactive/interface-js-ipfs-core#505 resolves ipfs-inactive/js-ipfs-http-client#518 refs ipfs#2093 resolves ipfs-inactive/js-ipfs-http-client#932 License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
- Loading branch information
Alan Shaw
authored
Aug 28, 2019
1 parent
07e6841
commit 3764d06
Showing
22 changed files
with
618 additions
and
467 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict' | ||
/* eslint-env browser */ | ||
|
||
const ky = require('ky-universal').default | ||
const { isBrowser, isWebWorker } = require('ipfs-utils/src/env') | ||
const { toUri } = require('./multiaddr') | ||
const errorHandler = require('./error-handler') | ||
|
||
// Set default configuration and call create function with them | ||
module.exports = create => config => { | ||
config = config || {} | ||
|
||
if (typeof config === 'string') { | ||
config = { apiAddr: config } | ||
} else if (config.constructor && config.constructor.isMultiaddr) { | ||
config = { apiAddr: config } | ||
} else { | ||
config = { ...config } | ||
} | ||
|
||
config.apiAddr = (config.apiAddr || getDefaultApiAddr(config)).toString() | ||
config.apiAddr = config.apiAddr.startsWith('/') ? toUri(config.apiAddr) : config.apiAddr | ||
config.apiPath = config.apiPath || config['api-path'] || '/api/v0' | ||
|
||
return create({ | ||
// TODO configure ky to use config.fetch when this is released: | ||
// https://github.com/sindresorhus/ky/pull/153 | ||
ky: ky.extend({ | ||
prefixUrl: config.apiAddr + config.apiPath, | ||
timeout: config.timeout || 60 * 1000, | ||
headers: config.headers, | ||
hooks: { | ||
afterResponse: [errorHandler] | ||
} | ||
}), | ||
...config | ||
}) | ||
} | ||
|
||
function getDefaultApiAddr ({ protocol, host, port }) { | ||
if (isBrowser || isWebWorker) { | ||
if (!protocol && !host && !port) { // Use current origin | ||
return '' | ||
} | ||
|
||
if (!protocol) { | ||
protocol = location.protocol.startsWith('http') | ||
? location.protocol.split(':')[0] | ||
: 'http' | ||
} | ||
|
||
host = host || location.hostname | ||
port = port || location.port | ||
|
||
return `${protocol}://${host}${port ? ':' + port : ''}` | ||
} | ||
|
||
return `${protocol || 'http'}://${host || 'localhost'}:${port || 5001}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict' | ||
|
||
const { HTTPError } = require('ky-universal') | ||
const log = require('debug')('ipfs-http-client:lib:error-handler') | ||
|
||
function isJsonResponse (res) { | ||
return (res.headers.get('Content-Type') || '').startsWith('application/json') | ||
} | ||
|
||
module.exports = async function errorHandler (response) { | ||
if (response.ok) return | ||
|
||
let msg | ||
|
||
try { | ||
if (isJsonResponse(response)) { | ||
const data = await response.json() | ||
log(data) | ||
msg = data.Message || data.message | ||
} else { | ||
msg = await response.text() | ||
} | ||
} catch (err) { | ||
log('Failed to parse error response', err) | ||
// Failed to extract/parse error message from response | ||
throw new HTTPError(response) | ||
} | ||
|
||
if (!msg) throw new HTTPError(response) | ||
throw Object.assign(new Error(msg), { status: response.status }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict' | ||
|
||
// Convert a multiaddr to a URI | ||
// Assumes multiaddr is in a format that can be converted to a HTTP(s) URI | ||
exports.toUri = ma => { | ||
const parts = `${ma}`.split('/') | ||
const port = getPort(parts) | ||
return `${getProtocol(parts)}://${parts[2]}${port == null ? '' : ':' + port}` | ||
} | ||
|
||
function getProtocol (maParts) { | ||
return maParts.indexOf('https') === -1 ? 'http' : 'https' | ||
} | ||
|
||
function getPort (maParts) { | ||
const tcpIndex = maParts.indexOf('tcp') | ||
return tcpIndex === -1 ? null : maParts[tcpIndex + 1] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict' | ||
|
||
module.exports = function toIterable (body) { | ||
// Node.js stream | ||
if (body[Symbol.asyncIterator]) return body | ||
|
||
// Browser ReadableStream | ||
if (body.getReader) { | ||
return (async function * () { | ||
const reader = body.getReader() | ||
|
||
try { | ||
while (true) { | ||
const { done, value } = await reader.read() | ||
if (done) return | ||
yield value | ||
} | ||
} finally { | ||
reader.releaseLock() | ||
} | ||
})() | ||
} | ||
|
||
throw new Error('unknown stream') | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.