Skip to content

Commit

Permalink
Merge pull request #463 from dadi/patch/await-remote-response-end
Browse files Browse the repository at this point in the history
Wait for response to end
  • Loading branch information
jimlambie committed Nov 21, 2018
2 parents bc3c823 + fc511f6 commit da1444a
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions dadi/lib/storage/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,23 @@ HTTPStorage.prototype.get = function ({
'User-Agent': 'DADI CDN'
}
}, res => {
let statusCode = res.statusCode
let buffers = []

if (statusCode === 200) {
let buffers = []
res.on('data', chunk => {
buffers.push(chunk)
})

res.on('data', chunk => {
buffers.push(chunk)
})
res.on('end', () => {
let statusCode = res.statusCode

res.on('end', () => {
// Successful response, return the data
if (statusCode === 200) {
return resolve(streamifier.createReadStream(Buffer.concat(buffers)))
})
} else {
}

// Determine what to do with the response when not successful. If it's
// a redirect status code, we continue trying to get it until we reach the
// configured redirect limit.
if (
[301, 302, 307].includes(statusCode) &&
typeof res.headers.location === 'string'
Expand All @@ -91,23 +95,20 @@ HTTPStorage.prototype.get = function ({
statusCode = 404
}

// It's not a redirect, determine what to return
let httpError

switch (statusCode) {
case 404:
httpError = new Error(`Not Found: ${this.getFullUrl()}`)

break

case 403:
httpError = new Error(`Forbidden: ${this.getFullUrl()}`)

break

default:
httpError = new Error(`Remote server responded with error code ${statusCode} for URL: ${this.getFullUrl()}`)

break
}

httpError.statusCode = statusCode
Expand All @@ -126,7 +127,7 @@ HTTPStorage.prototype.get = function ({
} else {
reject(httpError)
}
}
})
}).on('error', (err) => {
let httpError

Expand Down

0 comments on commit da1444a

Please sign in to comment.