From 483cce9deca42eb9252eaa42e2a64a985267a749 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Tue, 12 Sep 2017 19:16:11 +0200 Subject: [PATCH] Use safe-buffer package --- lib/MultipartDataGenerator.js | 2 ++ lib/utils.js | 26 +++----------------------- package.json | 3 ++- 3 files changed, 7 insertions(+), 24 deletions(-) diff --git a/lib/MultipartDataGenerator.js b/lib/MultipartDataGenerator.js index 94797c6492..118f2ddea3 100644 --- a/lib/MultipartDataGenerator.js +++ b/lib/MultipartDataGenerator.js @@ -1,5 +1,7 @@ 'use strict'; +var Buffer = require('safe-buffer').Buffer; + // Method for formatting HTTP body for the multipart/form-data specification // Mostly taken from Fermata.js // https://github.com/natevw/fermata/blob/5d9732a33d776ce925013a265935facd1626cc88/fermata.js#L315-L343 diff --git a/lib/utils.js b/lib/utils.js index 063b0a646c..3836b0f706 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,5 +1,6 @@ 'use strict'; +var Buffer = require('safe-buffer').Buffer; var qs = require('qs'); var crypto = require('crypto'); @@ -131,8 +132,8 @@ var utils = module.exports = { * Secure compare, from https://github.com/freewil/scmp */ secureCompare: function(a, b) { - var a = asBuffer(a); - var b = asBuffer(b); + var a = Buffer.from(a); + var b = Buffer.from(b); // return early here if buffer lengths are not equal since timingSafeEqual // will throw if buffer lengths are not equal @@ -172,24 +173,3 @@ var utils = module.exports = { return obj; }, }; - -function asBuffer(thing) { - if (Buffer.isBuffer(thing)) { - return thing; - } - - if (Buffer.from) { - // Buffer.from fix for node versions prior to 4.5.x - try { - return Buffer.from(thing); - } catch (e) { - if (e instanceof TypeError) { - return new Buffer(thing); - } else { - throw e; - } - } - } else { - return new Buffer(thing); - } -} diff --git a/package.json b/package.json index 4d1203e52d..93ecb368a8 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "dependencies": { "bluebird": "^3.5.0", "lodash.isplainobject": "^4.0.6", - "qs": "~6.5.1" + "qs": "~6.5.1", + "safe-buffer": "^5.1.1" }, "license": "MIT", "scripts": {