From 1cbcff24343426923a4329a1d1ecb797050fee9c Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Sat, 4 Mar 2023 12:14:21 -0800 Subject: [PATCH] use native base64url encoding, remove external dependency --- lib/mystery-box.js | 6 ++-- package-lock.json | 72 +++++++++++++++++++++++++++-------------- package.json | 5 +-- test/lib/mystery-box.js | 2 +- 4 files changed, 52 insertions(+), 33 deletions(-) diff --git a/lib/mystery-box.js b/lib/mystery-box.js index 05fe28f..55967ee 100644 --- a/lib/mystery-box.js +++ b/lib/mystery-box.js @@ -3,7 +3,6 @@ const crypto = require('crypto'); const zlib = require('zlib'); const { promisify } = require('util'); -const { base64ToBase64URL, base64URLToBase64 } = require('@squeep/base64url'); const common = require('./common'); const allVersions = require('./version-parameters'); const { performance } = require('perf_hooks'); @@ -221,8 +220,7 @@ class MysteryBox { const final = cipher.final(); const tag = cipher.getAuthTag(); - const merged = Buffer.concat([versionBuffer, flagsBuffer, iv, salt, tag, encrypted, final]).toString('base64'); - const result = base64ToBase64URL(merged); + const result = Buffer.concat([versionBuffer, flagsBuffer, iv, salt, tag, encrypted, final]).toString('base64url'); timingsMs.end = timingsMs.postCrypt = performance.now(); this.logger.debug(_scope, 'statistics', { version, flags: this._prettyFlags(flags), serialized: contents.length, compressed: payload.length, encoded: result.length, ...MysteryBox._timingsLog(timingsMs) }); @@ -251,7 +249,7 @@ class MysteryBox { throw new RangeError('nothing to unpack'); } - const raw = Buffer.from(base64URLToBase64(box), 'base64'); + const raw = Buffer.from(box, 'base64url'); let offset = 0; const version = raw.subarray(offset, 1).readUInt8(0); diff --git a/package-lock.json b/package-lock.json index d95918b..0e7127c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,9 +8,6 @@ "name": "@squeep/mystery-box", "version": "1.2.1", "license": "ISC", - "dependencies": { - "@squeep/base64url": "^1.0.5" - }, "devDependencies": { "eslint": "^8.35.0", "eslint-plugin-node": "^11.1.0", @@ -22,7 +19,7 @@ "sinon": "^15.0.1" }, "engines": { - "node": ">=14" + "node": "^14 >=14.18.0 || >=15.7.0" } }, "node_modules/@ampproject/remapping": { @@ -712,14 +709,6 @@ "integrity": "sha512-sXXKG+uL9IrKqViTtao2Ws6dy0znu9sOaP1di/jKGW1M6VssO8vlpXCQcpZ+jisQ1tTFAC5Jo/EOzFbggBagFQ==", "dev": true }, - "node_modules/@squeep/base64url": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/@squeep/base64url/-/base64url-1.0.5.tgz", - "integrity": "sha512-J1UBXYQ4tBuHGnFfg0MdmxCP3oEti7jJWK/qBsg520d8tZd10sL6TXiMDGdBiH9GD3OtlfSYf6wWIN4QfdW21A==", - "engines": { - "node": ">=14" - } - }, "node_modules/acorn": { "version": "8.8.2", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.2.tgz", @@ -2991,6 +2980,12 @@ "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==", "dev": true }, + "node_modules/readable-stream/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "dev": true + }, "node_modules/readdirp": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", @@ -3126,10 +3121,24 @@ } }, "node_modules/safe-buffer": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", - "dev": true + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] }, "node_modules/safe-regex": { "version": "2.1.1", @@ -3261,6 +3270,12 @@ "safe-buffer": "~5.1.0" } }, + "node_modules/string_decoder/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "dev": true + }, "node_modules/string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", @@ -4171,11 +4186,6 @@ "integrity": "sha512-sXXKG+uL9IrKqViTtao2Ws6dy0znu9sOaP1di/jKGW1M6VssO8vlpXCQcpZ+jisQ1tTFAC5Jo/EOzFbggBagFQ==", "dev": true }, - "@squeep/base64url": { - "version": "1.0.5", - "resolved": "https://registry.npmjs.org/@squeep/base64url/-/base64url-1.0.5.tgz", - "integrity": "sha512-J1UBXYQ4tBuHGnFfg0MdmxCP3oEti7jJWK/qBsg520d8tZd10sL6TXiMDGdBiH9GD3OtlfSYf6wWIN4QfdW21A==" - }, "acorn": { "version": "8.8.2", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.2.tgz", @@ -5867,6 +5877,12 @@ "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==", "dev": true + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "dev": true } } }, @@ -5954,9 +5970,9 @@ } }, "safe-buffer": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", "dev": true }, "safe-regex": { @@ -6067,6 +6083,14 @@ "dev": true, "requires": { "safe-buffer": "~5.1.0" + }, + "dependencies": { + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", + "dev": true + } } }, "string-width": { diff --git a/package.json b/package.json index 03f2f96..6bb4f56 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "test": "mocha --recursive" }, "engines": { - "node": ">=14" + "node": "^14 >=14.18.0 || >=15.7.0" }, "pre-commit": [ "eslint", @@ -34,8 +34,5 @@ "nyc": "^15.1.0", "pre-commit": "^1.2.2", "sinon": "^15.0.1" - }, - "dependencies": { - "@squeep/base64url": "^1.0.5" } } diff --git a/test/lib/mystery-box.js b/test/lib/mystery-box.js index 2f2b734..c71f6ff 100644 --- a/test/lib/mystery-box.js +++ b/test/lib/mystery-box.js @@ -73,7 +73,7 @@ describe('MysteryBox', function () { it('covers unpacking unsupported version', async function () { const badBuffer = Buffer.alloc(128); badBuffer.writeUInt8(0, 0); // No such thing as version 0 - const badPayload = badBuffer.toString('base64'); + const badPayload = badBuffer.toString('base64url'); assert.rejects(() => mb.unpack(badPayload), RangeError); }); -- 2.45.2