Skip to content

Commit

Permalink
fix TypeError: binary is not a function (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommytroylin authored and floatdrop committed Nov 6, 2016
1 parent adfc6d1 commit 3b8665c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const nodeStatusCodes = require('node-status-codes');
const isRetryAllowed = require('is-retry-allowed');
const pkg = require('./package.json');

const isModernBuffer = (
typeof Buffer.alloc === 'function' &&
typeof Buffer.allocUnsafe === 'function' &&
typeof Buffer.from === 'function'
);

function requestAsEventEmitter(opts) {
opts = opts || {};

Expand All @@ -41,7 +47,11 @@ function requestAsEventEmitter(opts) {
return;
}

redirectUrl = urlLib.resolve(urlLib.format(opts), Buffer.from(res.headers.location, 'binary').toString());
const bufferString = isModernBuffer ?
Buffer.from(res.headers.location, 'binary').toString() :
new Buffer(res.headers.location, 'binary').toString();

redirectUrl = urlLib.resolve(urlLib.format(opts), bufferString);
const redirectOpts = Object.assign({}, opts, urlLib.parse(redirectUrl));

ee.emit('redirect', res, redirectOpts);
Expand Down

0 comments on commit 3b8665c

Please sign in to comment.