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

Add support for {encoding:null} option and binary Buffer for body. #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ function run_xhr(options) {

xhr.onreadystatechange = on_state_change
xhr.open(options.method, options.uri, true) // asynchronous
// Deal with requests for raw buffer response
if(options.encoding === null) {
xhr.responseType = 'arraybuffer';
}
if(is_cors)
xhr.withCredentials = !! options.withCredentials
xhr.send(options.body)
Expand Down Expand Up @@ -268,10 +272,14 @@ function run_xhr(options) {
did.end = true
request.log.debug('Request done', {'id':xhr.id})

xhr.body = xhr.responseText
if(options.json) {
try { xhr.body = JSON.parse(xhr.responseText) }
catch (er) { return options.callback(er, xhr) }
if(options.encoding === null) {
xhr.body = new Buffer(new Uint8Array(xhr.response));
} else {
xhr.body = xhr.responseText
if(options.json) {
try { xhr.body = JSON.parse(xhr.responseText) }
catch (er) { return options.callback(er, xhr) }
}
}

options.callback(null, xhr, xhr.body)
Expand Down