This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
refactor: replace wreck with raw request #414
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -4,15 +4,13 @@ | |
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "browserify -t brfs index.js > bundle.js && http-server -a 127.0.0.1 -p 8888" | ||
"start": "browserify index.js > bundle.js && http-server -a 127.0.0.1 -p 8888" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <3 |
||
}, | ||
"keywords": [], | ||
"author": "Friedel Ziegelmayer", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"brfs": "^1.4.3", | ||
"browserify": "^13.0.1", | ||
"http-server": "^0.9.0", | ||
"ipfs-api": "^6.0.3" | ||
"http-server": "^0.9.0" | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,32 +1,54 @@ | ||
'use strict' | ||
|
||
const Wreck = require('wreck') | ||
const Qs = require('qs') | ||
const ndjson = require('ndjson') | ||
const getFilesStream = require('./get-files-stream') | ||
|
||
const isNode = require('detect-node') | ||
const once = require('once') | ||
const concat = require('concat-stream') | ||
|
||
const getFilesStream = require('./get-files-stream') | ||
const request = require('./request') | ||
|
||
// -- Internal | ||
|
||
function parseChunkedJson (res, cb) { | ||
const parsed = [] | ||
res | ||
.pipe(ndjson.parse()) | ||
.on('data', (obj) => { | ||
parsed.push(obj) | ||
}) | ||
.on('end', () => { | ||
cb(null, parsed) | ||
}) | ||
.once('error', cb) | ||
.pipe(concat((data) => cb(null, data))) | ||
} | ||
|
||
function onRes (buffer, cb, uri) { | ||
return (err, res) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
function parseRaw (res, cb) { | ||
res | ||
.once('error', cb) | ||
.pipe(concat((data) => cb(null, data))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because concat does the right thing for streams, arrays and buffers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you :) Will remind me of that the next time I need to do this |
||
} | ||
|
||
function parseJson (res, cb) { | ||
res | ||
.once('error', cb) | ||
.pipe(concat((data) => { | ||
if (!data || data.length === 0) { | ||
return cb() | ||
} | ||
|
||
if (Buffer.isBuffer(data)) { | ||
data = data.toString() | ||
} | ||
|
||
let res | ||
try { | ||
res = JSON.parse(data) | ||
} catch (err) { | ||
return cb(err) | ||
} | ||
|
||
cb(null, res) | ||
})) | ||
} | ||
|
||
function onRes (buffer, cb) { | ||
return (res) => { | ||
const stream = Boolean(res.headers['x-stream-output']) | ||
const chunkedObjects = Boolean(res.headers['x-chunked-output']) | ||
const isJson = res.headers['content-type'] && | ||
|
@@ -35,7 +57,7 @@ function onRes (buffer, cb, uri) { | |
if (res.statusCode >= 400 || !res.statusCode) { | ||
const error = new Error(`Server responded with ${res.statusCode}`) | ||
|
||
return Wreck.read(res, {json: true}, (err, payload) => { | ||
parseJson(res, (err, payload) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
@@ -51,20 +73,21 @@ function onRes (buffer, cb, uri) { | |
return cb(null, res) | ||
} | ||
|
||
if (chunkedObjects) { | ||
if (isJson) { | ||
return parseChunkedJson(res, cb) | ||
} | ||
if (chunkedObjects && isJson) { | ||
return parseChunkedJson(res, cb) | ||
} | ||
|
||
return Wreck.read(res, null, cb) | ||
if (isJson) { | ||
return parseJson(res, cb) | ||
} | ||
|
||
Wreck.read(res, {json: isJson}, cb) | ||
parseRaw(res, cb) | ||
} | ||
} | ||
|
||
function requestAPI (config, options, callback) { | ||
options.qs = options.qs || {} | ||
callback = once(callback) | ||
|
||
if (Array.isArray(options.files)) { | ||
options.qs.recursive = true | ||
|
@@ -99,29 +122,38 @@ function requestAPI (config, options, callback) { | |
// this option is only used internally, not passed to daemon | ||
delete options.qs.followSymlinks | ||
|
||
const port = config.port ? `:${config.port}` : '' | ||
|
||
const opts = { | ||
method: 'POST', | ||
uri: `${config.protocol}://${config.host}${port}${config['api-path']}${options.path}?${Qs.stringify(options.qs, {arrayFormat: 'repeat'})}`, | ||
headers: {} | ||
} | ||
const method = 'POST' | ||
const headers = {} | ||
|
||
if (isNode) { | ||
// Browsers do not allow you to modify the user agent | ||
opts.headers['User-Agent'] = config['user-agent'] | ||
headers['User-Agent'] = config['user-agent'] | ||
} | ||
|
||
if (options.files) { | ||
if (!stream.boundary) { | ||
return callback(new Error('No boundary in multipart stream')) | ||
} | ||
|
||
opts.headers['Content-Type'] = `multipart/form-data; boundary=${stream.boundary}` | ||
opts.payload = stream | ||
headers['Content-Type'] = `multipart/form-data; boundary=${stream.boundary}` | ||
} | ||
|
||
const qs = Qs.stringify(options.qs, {arrayFormat: 'repeat'}) | ||
const req = request(config.protocol)({ | ||
hostname: config.host, | ||
path: `${config['api-path']}${options.path}?${qs}`, | ||
port: config.port, | ||
method: method, | ||
headers: headers | ||
}, onRes(options.buffer, callback)) | ||
|
||
if (options.files) { | ||
stream.pipe(req) | ||
} else { | ||
req.end() | ||
} | ||
|
||
return Wreck.request(opts.method, opts.uri, opts, onRes(options.buffer, callback, opts.uri)) | ||
return req | ||
} | ||
|
||
// | ||
|
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,12 @@ | ||
'use strict' | ||
|
||
const httpRequest = require('http').request | ||
const httpsRequest = require('https').request | ||
|
||
module.exports = (protocol) => { | ||
if (protocol.indexOf('https') === 0) { | ||
return httpsRequest | ||
} | ||
|
||
return httpRequest | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It needs to be buffered?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think this
buffer: true
is one of the most misleading features. Can you add a comment that says:"Buffer the whole response instead of streaming it"