Skip to content
This repository has been archived by the owner on Dec 14, 2020. It is now read-only.

Commit

Permalink
Fix HTTP requests with binary responses in the browser.
Browse files Browse the repository at this point in the history
Set the response type for XHR requests to 'arraybuffer' to ensure
correct delivery of binary data when making HTTP requests in
the browser.

This required working around an issue with XMLHttpRequest.responseType
described in browserify/http-browserify#65
  • Loading branch information
robertknight committed Sep 6, 2014
1 parent 620f210 commit e963885
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/base/streamutil.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
/// <reference path="../../typings/DefinitelyTyped/node/node.d.ts" />
/// <reference path="../../typings/DefinitelyTyped/q/Q.d.ts" />

import collectionutil = require('./collectionutil');
import Q = require('q');

export function readAll(readable: NodeJS.ReadableStream) : Q.Promise<string> {
var result = Q.defer<string>();
var body = '';
readable.on('data', (chunk: Buffer) => {
body += chunk.toString('binary');
readable.on('data', (chunk: any) => {
// in Node.js if the stream is from http.ClientResponse
// then chunk will be a Buffer. Under node-browserify it
// will be a Uint8Array if xhr.responseType was set to 'arraybuffer'
if (chunk instanceof Uint8Array) {
body += collectionutil.stringFromBuffer(chunk);
} else {
body += chunk.toString('binary');
}
});
readable.on('end', () => {
result.resolve(body);
Expand Down
23 changes: 21 additions & 2 deletions lib/http_client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path="../typings/sprintf.d.ts" />
/// <reference path="../typings/DefinitelyTyped/node/node.d.ts" />
/// <reference path="../typings/DefinitelyTyped/q/Q.d.ts" />
/// <reference path="../typings/sprintf.d.ts" />

import http = require('http');
import https = require('https');
Expand All @@ -8,6 +9,7 @@ import sprintf = require('sprintf');
import urlLib = require('url');

import asyncutil = require('./base/asyncutil');
import env = require('./base/env');
import err_util = require('./base/err_util');
import streamutil = require('./base/streamutil');

Expand Down Expand Up @@ -110,7 +112,14 @@ export function request<T>(method: string, url: string, data?: T) : Q.Promise<Re
host: urlParts.hostname,
scheme: urlParts.protocol,
port: urlParts.port,
withCredentials: false
withCredentials: false,

// in the browser, where http.request is implemented by http-browserify,
// specify that the response type should be an ArrayBuffer.
//
// See comment about https://github.com/substack/http-browserify/issues/65
// below
responseType: 'arraybuffer'
};

// strip trailing colon from protocol.
Expand Down Expand Up @@ -141,6 +150,16 @@ export function request<T>(method: string, url: string, data?: T) : Q.Promise<Re
response.reject(err);
}).done();
});

if (env.isBrowser()) {
// work around http-browserify not setting failing to set
// xhr.responseType successfully due to Firefox not allowing
// xhr.responseType to be set until xhr.open() has been called.
// See https://github.com/substack/http-browserify/issues/65
var browserifyRequest: any = request;
browserifyRequest.xhr.responseType = 'arraybuffer';
}

if (data) {
switch (typeof data) {
case 'string':
Expand Down

0 comments on commit e963885

Please sign in to comment.