include simple encoded secret generator
authorJustin Wind <justin.wind+git@gmail.com>
Sat, 26 Jul 2025 20:06:32 +0000 (13:06 -0700)
committerJustin Wind <justin.wind+git@gmail.com>
Sat, 26 Jul 2025 20:08:51 +0000 (13:08 -0700)
bin/create-secret.js [new file with mode: 0755]
package.json

diff --git a/bin/create-secret.js b/bin/create-secret.js
new file mode 100755 (executable)
index 0000000..f1fac50
--- /dev/null
@@ -0,0 +1,38 @@
+#!/usr/bin/env node
+'use strict';
+
+/**
+ * Simple script to create a secret key for mysterybox configuration.
+ */
+
+const { randomBytes } = require('node:crypto');
+
+/**
+ * Get the value following a command line option.
+ * Removes the option and its value from the command line arguments.
+ * @param {string} option e.g. '--bytes'
+ * @returns {string} value following the option
+ */
+function cliGetOption(option) {
+  let value;
+  while (process.argv.includes(option)) {
+    const optionIndex = process.argv.indexOf(option);
+    value = process.argv.splice(optionIndex, 2)[1];
+    if (value === undefined) {
+      throw new Error(`Missing value for option: ${option}`);
+    }
+  }
+  return value;
+}
+
+const bytes = parseInt(cliGetOption('--bytes'), 10) || 64;
+const encoding = cliGetOption('--encoding') || 'base64url';
+
+randomBytes(bytes, (err, buf) => {
+  if (err) {
+    console.error(err);
+    throw err;
+  }
+  const secret = buf.toString(encoding);
+  console.log(`Buffer.from('${secret}', '${encoding}')`);
+});
index 7f8a367d3c31d3a1e72d945b7d14bbde4603a9d0..f565399a062e047a4d4b29ff8758db6ded38186e 100644 (file)
     "prepare": "husky"
   },
   "files": [
-    "lib/**"
+    "lib/**",
+    "bin/**"
   ],
+  "bin": {
+    "mystery-box-create-secret": "bin/create-secret.js"
+  },
   "engines": {
     "node": "^14 >=14.18.0 || >=15.7.0"
   },