diff --git a/.travis.yml b/.travis.yml index bc4c951..54ec7b3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,5 +6,3 @@ before_install: node_js: - "6" - "4" - - "0.12" - - "0.10" diff --git a/src/base64url.ts b/src/base64url.ts index 8a077df..1279459 100644 --- a/src/base64url.ts +++ b/src/base64url.ts @@ -6,11 +6,11 @@ function encode(input: string | Buffer, encoding: string = "utf8"): string { if (Buffer.isBuffer(input)) { return fromBase64(input.toString("base64")); } - return fromBase64(new Buffer(input as string, encoding).toString("base64")); + return fromBase64(Buffer.from(input as string, encoding).toString("base64")); }; function decode(base64url: string, encoding: string = "utf8"): string { - return new Buffer(toBase64(base64url), "base64").toString(encoding); + return Buffer.from(toBase64(base64url), "base64").toString(encoding); } function toBase64(base64url: string | Buffer): string { @@ -30,7 +30,7 @@ function fromBase64(base64: string): string { } function toBuffer(base64url: string): Buffer { - return new Buffer(toBase64(base64url), "base64"); + return Buffer.from(toBase64(base64url), "base64"); } export interface Base64Url { diff --git a/src/pad-string.ts b/src/pad-string.ts index 7f52d7f..fd6b35b 100644 --- a/src/pad-string.ts +++ b/src/pad-string.ts @@ -10,7 +10,7 @@ export default function padString(input: string): string { let position = stringLength; let padLength = segmentLength - diff; let paddedStringLength = stringLength + padLength; - let buffer = new Buffer(paddedStringLength); + let buffer = Buffer.alloc(paddedStringLength); buffer.write(input); diff --git a/test/base64url.test.js b/test/base64url.test.js index 61ef6f4..dce5384 100644 --- a/test/base64url.test.js +++ b/test/base64url.test.js @@ -54,3 +54,19 @@ test('from base64url to buffer', function (t) { t.same(result, testBuffer, 'should be able to convert to buffer'); t.end(); }); + +test('encode validates input', function (t) { + const b64url = base64url(testBuffer, 'binary'); + + var result = undefined; + + try { + base64url.encode(1000); + } catch (err) { + result = err; + } + + t.not(result, undefined, 'should validate encode input is string or Buffer'); + t.end(); +}); +