From 3b8665cec9e35c28e4b6ac6ad99ba467bf286c0f Mon Sep 17 00:00:00 2001 From: Tommy Troy Lin Date: Sun, 6 Nov 2016 17:48:27 +0800 Subject: [PATCH] fix TypeError: binary is not a function (#243) --- index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index fe6b02f59..953ff296b 100644 --- a/index.js +++ b/index.js @@ -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 || {}; @@ -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);