Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix: Do not parse non json responses
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Nov 21, 2015
1 parent c45a191 commit 527228d
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/request-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ const isNode = !global.window

// -- Internal

function parseChunkedJson (res, cb) {
const parsed = []
res.on('data', chunk => {
try {
parsed.push(JSON.parse(chunk))
} catch (err) {
// Browser quirks emit more than needed sometimes
}
})
res.on('end', () => cb(null, parsed))
}

function onRes (buffer, cb) {
return (err, res) => {
if (err) {
Expand All @@ -33,16 +45,9 @@ function onRes (buffer, cb) {
if (stream && !buffer) return cb(null, res)

if (chunkedObjects) {
const parsed = []
res.on('data', chunk => {
try {
parsed.push(JSON.parse(chunk))
} catch (err) {
// Browser quirks emit more than needed sometimes
}
})
res.on('end', () => cb(null, parsed))
return
if (isJson) return parseChunkedJson(res, cb)

return Wreck.read(res, null, cb)
}

Wreck.read(res, {json: isJson}, cb)
Expand Down

0 comments on commit 527228d

Please sign in to comment.