Skip to content

Commit

Permalink
use future testing for Buffer.from
Browse files Browse the repository at this point in the history
`alloc` and `allocUnsafe` were backported with `from` and not used in Buffer class
https://github.com/LinusU/buffer-from/blob/master/index.js#L3
  • Loading branch information
floatdrop committed Nov 6, 2016
1 parent c582d97 commit 5f21337
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions 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 @@ -40,8 +46,11 @@ function requestAsEventEmitter(opts) {
ee.emit('error', new got.MaxRedirectsError(statusCode, opts), null, res);
return;
}
const isNode4 = process.version ? process.version.indexOf('v4.') === 0 : true;
const bufferString = isNode4 ? new Buffer(res.headers.location, 'binary').toString() : 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));

Expand Down

0 comments on commit 5f21337

Please sign in to comment.