Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for response to end #463

Merged
merged 2 commits into from
Nov 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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