From 959ea95bf200d64939ed76897d3b06bb684f3a0d Mon Sep 17 00:00:00 2001 From: "David Humphrey (:humph) david.humphrey@senecacollege.ca" Date: Fri, 6 Jun 2014 21:02:01 -0400 Subject: [PATCH] Add support for {encoding:null} option and binary Buffer for body. --- index.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 1a77c6f..2bf7213 100644 --- a/index.js +++ b/index.js @@ -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) @@ -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)