This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 294
[discuss] Switch to superagent instead of raw http.request #92
Merged
Merged
Changes from all commits
Commits
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 hidden or 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,3 +1,4 @@ | ||
{ | ||
"parser": "babel-eslint", | ||
"extends": "standard" | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,108 +1,123 @@ | ||
var http = require('http') | ||
var qs = require('querystring') | ||
var getFilesStream = require('./get-files-stream') | ||
'use strict' | ||
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. I've read that this is required for 'let' to work properly, is it the reason why you added? 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. yes |
||
|
||
exports = module.exports = function getRequestAPI (config) { | ||
return requestAPI | ||
const request = require('request') | ||
const getFilesStream = require('./get-files-stream') | ||
|
||
function requestAPI (path, args, opts, files, buffer, cb) { | ||
var query, stream, contentType | ||
contentType = 'application/json' | ||
const isNode = !global.window | ||
|
||
if (Array.isArray(path)) path = path.join('/') | ||
// -- Internal | ||
|
||
opts = opts || {} | ||
function onEnd (buffer, result, cb) { | ||
return (err, res, body) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
if (args && !Array.isArray(args)) args = [args] | ||
if (args) opts.arg = args | ||
if (res.statusCode >= 400 || !res.statusCode) { | ||
return cb(new Error(`Server responded with ${res.statusCode}: ${body}`)) | ||
} | ||
|
||
if (files) { | ||
stream = getFilesStream(files, opts) | ||
if (!stream.boundary) { | ||
throw new Error('no boundary in multipart stream') | ||
} | ||
contentType = 'multipart/form-data; boundary=' + stream.boundary | ||
if ((result.stream && !buffer) || | ||
(result.chunkedObjects && buffer)) { | ||
return cb(null, body) | ||
} | ||
|
||
if (typeof buffer === 'function') { | ||
cb = buffer | ||
buffer = false | ||
if (result.chunkedObjects) return cb(null, result.objects) | ||
|
||
try { | ||
const parsedBody = JSON.parse(body) | ||
cb(null, parsedBody) | ||
} catch (e) { | ||
cb(null, body) | ||
} | ||
} | ||
} | ||
|
||
// this option is only used internally, not passed to daemon | ||
delete opts.followSymlinks | ||
|
||
opts['stream-channels'] = true | ||
query = qs.stringify(opts) | ||
|
||
var reqo = { | ||
method: files ? 'POST' : 'GET', | ||
host: config.host, | ||
port: config.port, | ||
path: config['api-path'] + path + '?' + query, | ||
headers: { | ||
'User-Agent': config['user-agent'], | ||
'Content-Type': contentType | ||
}, | ||
withCredentials: false | ||
function onData (result) { | ||
return chunk => { | ||
if (!result.chunkedObjects) return | ||
|
||
try { | ||
const obj = JSON.parse(chunk.toString()) | ||
result.objects.push(obj) | ||
} catch (e) { | ||
result.chunkedObjects = false | ||
} | ||
} | ||
} | ||
|
||
function onResponse (result) { | ||
return res => { | ||
result.stream = !!res.headers['x-stream-output'] | ||
result.chunkedObjects = !!res.headers['x-chunked-output'] | ||
} | ||
} | ||
|
||
function makeRequest (opts, buffer, cb) { | ||
// this option is only used internally, not passed to daemon | ||
delete opts.qs.followSymlinks | ||
|
||
const result = { | ||
stream: false, | ||
chunkedObjects: false, | ||
objects: [] | ||
} | ||
|
||
return request(opts, onEnd(buffer, result, cb)) | ||
.on('data', onData(result)) | ||
.on('response', onResponse(result)) | ||
} | ||
|
||
function requestAPI (config, path, args, qs, files, buffer, cb) { | ||
qs = qs || {} | ||
if (Array.isArray(path)) path = path.join('/') | ||
if (args && !Array.isArray(args)) args = [args] | ||
if (args) qs.arg = args | ||
if (files && !Array.isArray(files)) files = [files] | ||
|
||
if (typeof buffer === 'function') { | ||
cb = buffer | ||
buffer = false | ||
} | ||
|
||
if (qs.r) qs.recursive = qs.r | ||
|
||
if (!isNode && qs.recursive && path === 'add') { | ||
return cb(new Error('Recursive uploads are not supported in the browser')) | ||
} | ||
|
||
qs['stream-channels'] = true | ||
|
||
var req = http.request(reqo, function (res) { | ||
var data = '' | ||
var objects = [] | ||
var stream = !!res.headers && !!res.headers['x-stream-output'] | ||
var chunkedObjects = !!res.headers && !!res.headers['x-chunked-output'] | ||
|
||
if (stream && !buffer) return cb(null, res) | ||
if (chunkedObjects && buffer) return cb(null, res) | ||
|
||
res.on('data', function (chunk) { | ||
if (!chunkedObjects) { | ||
data += chunk | ||
return data | ||
} | ||
|
||
try { | ||
var obj = JSON.parse(chunk.toString()) | ||
objects.push(obj) | ||
} catch (e) { | ||
chunkedObjects = false | ||
data += chunk | ||
} | ||
}) | ||
res.on('end', function () { | ||
var parsed | ||
|
||
if (!chunkedObjects) { | ||
try { | ||
parsed = JSON.parse(data) | ||
data = parsed | ||
} catch (e) {} | ||
} else { | ||
data = objects | ||
} | ||
|
||
if (res.statusCode >= 400 || !res.statusCode) { | ||
if (!data) data = new Error() | ||
return cb(data, null) | ||
} | ||
return cb(null, data) | ||
}) | ||
res.on('error', function (err) { | ||
return cb(err, null) | ||
}) | ||
}) | ||
|
||
req.on('error', function (err) { | ||
return cb(err, null) | ||
}) | ||
|
||
if (stream) { | ||
stream.pipe(req) | ||
} else { | ||
req.end() | ||
const opts = { | ||
method: files ? 'POST' : 'GET', | ||
uri: `http://${config.host}:${config.port}${config['api-path']}${path}`, | ||
qs: qs, | ||
useQuerystring: true, | ||
headers: {}, | ||
withCredentials: false, | ||
gzip: true | ||
} | ||
|
||
if (isNode) { | ||
// Browsers do not allow you to modify the user agent | ||
opts.headers['User-Agent'] = config['user-agent'] | ||
} | ||
|
||
if (files) { | ||
const stream = getFilesStream(files, qs) | ||
if (!stream.boundary) { | ||
return cb(new Error('No boundary in multipart stream')) | ||
} | ||
|
||
return req | ||
opts.headers['Content-Type'] = `multipart/form-data; boundary=${stream.boundary}` | ||
stream.pipe(makeRequest(opts, buffer, cb)) | ||
} else { | ||
makeRequest(opts, buffer, cb) | ||
} | ||
} | ||
|
||
// -- Interface | ||
|
||
exports = module.exports = function getRequestAPI (config) { | ||
return requestAPI.bind(null, config) | ||
} |
This file contains hidden or 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
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.
why no more
r
?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'm merging the
r
option with therecursive
option in the configuration setup, so later down the line it's only one option to check.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.
got it, nice :)