split b64u functions into separate package
authorJustin Wind <justin.wind+git@gmail.com>
Wed, 6 Apr 2022 17:00:02 +0000 (10:00 -0700)
committerJustin Wind <justin.wind+git@gmail.com>
Wed, 6 Apr 2022 17:00:02 +0000 (10:00 -0700)
lib/common.js
lib/mystery-box.js
package-lock.json
package.json
test/lib/common.js

index a47226f6e76fe355416380f3a77db24450481783..4c7e6392fe1f7e6f28a9321680d56fbf933404cb 100644 (file)
@@ -19,56 +19,7 @@ const fileScope = (filename) => {
 }
 
 
-/**
- * Convert Base64 to Base64URL.
- * @param {String} input
- * @returns {String}
- */
-const base64ToBase64URL = (input) => {
-  if (!input) {
-    return input;
-  }
-  return input
-    .replace(/=/g, '')
-    .replace(/\+/g, '-')
-    .replace(/\//g, '_');
-};
-
-
-/**
- * Convert Base64URL to normal Base64.
- * @param {String} input
- * @returns {String}
- */
-const base64URLToBase64 = (input) => {
-  if (!input) {
-    return input;
-  }
-  return base64RePad(input)
-    .replace(/-/g, '+')
-    .replace(/_/, '/');
-};
-
-
-/**
- * Add any missing trailing padding which may have been removed from Base64URL encoding.
- * @param {String} input
- */
-const base64RePad = (input) => {
-  const blockSize = 4;
-  const lastBlockSize = input.length % blockSize;
-  if (lastBlockSize) {
-    const missing = blockSize - lastBlockSize;
-    return input + '='.repeat(missing);
-  }
-  return input;
-};
-
-
 module.exports = {
-  base64ToBase64URL,
-  base64URLToBase64,
-  base64RePad,
   fileScope,
   randomBytesAsync,
 };
\ No newline at end of file
index 6198cc816484b406804cf4a4cdd3c84685bce60e..2343c5e164f79f9d04629a97c366a1fbd0f58595 100644 (file)
@@ -3,6 +3,7 @@
 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');
@@ -183,7 +184,7 @@ class MysteryBox {
     const tag = cipher.getAuthTag();
 
     const merged = Buffer.concat([versionBuffer, flagsBuffer, iv, salt, tag, encrypted, final]).toString('base64');
-    const result = common.base64ToBase64URL(merged);
+    const result = base64ToBase64URL(merged);
     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) });
@@ -212,7 +213,7 @@ class MysteryBox {
       throw new RangeError('nothing to unpack');
     }
 
-    const raw = Buffer.from(common.base64URLToBase64(box), 'base64');
+    const raw = Buffer.from(base64URLToBase64(box), 'base64');
     let offset = 0;
 
     const version = raw.slice(offset, 1).readUInt8(0);
index ad26d98f8b01c6c0e7d09ce3f29746e475ca603e..f2716f846e3ad57a56ca341c27eaf6e5182ad055 100644 (file)
       "integrity": "sha512-+iTbntw2IZPb/anVDbypzfQa+ay64MW0Zo8aJ8gZPWMMK6/OubMVb6lUPMagqjOPnmtauXnFCACVl3O7ogjeqQ==",
       "dev": true
     },
+    "@squeep/base64url": {
+      "version": "git+https://git.squeep.com/squeep-base64url#5788dd9a225eb1898d624291e50a86f9800866a7",
+      "from": "git+https://git.squeep.com/squeep-base64url#v1.0.0"
+    },
     "@ungap/promise-all-settled": {
       "version": "1.1.2",
       "resolved": "https://registry.npmjs.org/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz",
       "dev": true
     },
     "caniuse-lite": {
-      "version": "1.0.30001265",
-      "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001265.tgz",
-      "integrity": "sha512-YzBnspggWV5hep1m9Z6sZVLOt7vrju8xWooFAgN6BA5qvy98qPAPb7vNUzypFaoh2pb3vlfzbDO8tB57UPGbtw==",
+      "version": "1.0.30001325",
+      "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001325.tgz",
+      "integrity": "sha512-sB1bZHjseSjDtijV1Hb7PB2Zd58Kyx+n/9EotvZ4Qcz2K3d0lWB8dB4nb8wN/TsOGFq3UuAm0zQZNQ4SoR7TrQ==",
       "dev": true
     },
     "chalk": {
       }
     },
     "eslint-plugin-sonarjs": {
-      "version": "0.12.0",
-      "resolved": "https://registry.npmjs.org/eslint-plugin-sonarjs/-/eslint-plugin-sonarjs-0.12.0.tgz",
-      "integrity": "sha512-soxjK67hoYxO8hesKqXWN50GSM+oG2r35N5WnAMehetahO6zoVpv3HZbdziP0jYWNopEe6te/BFUZOYAZI+qhg==",
+      "version": "0.13.0",
+      "resolved": "https://registry.npmjs.org/eslint-plugin-sonarjs/-/eslint-plugin-sonarjs-0.13.0.tgz",
+      "integrity": "sha512-t3m7ta0EspzDxSOZh3cEOJIJVZgN/TlJYaBGnQlK6W/PZNbWep8q4RQskkJkA7/zwNpX0BaoEOSUUrqaADVoqA==",
       "dev": true
     },
     "eslint-scope": {
index 34a918807249ed696624c61f9896f7865acddd43..0bc9798e421f29d83c6cced1ccda5ee542735a61 100644 (file)
     "eslint": "^8.12.0",
     "eslint-plugin-node": "^11.1.0",
     "eslint-plugin-security": "^1.4.0",
-    "eslint-plugin-sonarjs": "^0.12.0",
+    "eslint-plugin-sonarjs": "^0.13.0",
     "mocha": "^9.2.2",
     "nyc": "^15.1.0",
     "pre-commit": "^1.2.2",
     "sinon": "^13.0.1"
+  },
+  "dependencies": {
+    "@squeep/base64url": "git+https://git.squeep.com/squeep-base64url#v1.0.0"
   }
 }
index f5df10c779e1054b0881d701e382f4164f53ab1d..344e162366da391c15422e7588484b127d1ed9b1 100644 (file)
@@ -19,45 +19,4 @@ describe('Common', function () {
     });
   }); // fileScope
 
-  describe('base64ToBase64URL', function () {
-    it('covers', function () {
-      const b64 = '/+==';
-      const expected = '_-';
-      const result = common.base64ToBase64URL(b64);
-      assert.strictEqual(result, expected);
-    });
-    it('covers empty', function () {
-      const result = common.base64ToBase64URL(undefined);
-      assert.strictEqual(result, undefined);
-    });
-  }); // base64ToBase64URL
-
-  describe('base64URLToBase64', function () {
-    it('covers', function () {
-      const b64url = '_-';
-      const expected = '/+==';
-      const result = common.base64URLToBase64(b64url);
-      assert.strictEqual(result, expected);
-    });
-    it('covers empty', function () {
-      const result = common.base64URLToBase64(undefined);
-      assert.strictEqual(result, undefined);
-    });
-  }); // base64URLToBase64
-
-  describe('base64RePad', function () {
-    it('covers', function () {
-      const b64short = 'af';
-      const expected = 'af==';
-      const result = common.base64RePad(b64short);
-      assert.strictEqual(result, expected);
-    });
-    it('covers padded', function () {
-      const b64 = 'afd4';
-      const expected = b64;
-      const result = common.base64RePad(b64);
-      assert.strictEqual(result, expected);
-    });
-  }); // base64RePad
-
 }); // Common