aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-01 12:04:49 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-01 12:04:49 -0800
commit96438a4d58569abedccdfe6acff0ad6ed60bb5bc (patch)
treebe97cd6fa6bdf0d135be0467f8919a60821ddb90
parent5b3cf8bec56e7c13d2cce557bab8e203be729006 (diff)
downloadbun-96438a4d58569abedccdfe6acff0ad6ed60bb5bc.tar.gz
bun-96438a4d58569abedccdfe6acff0ad6ed60bb5bc.tar.zst
bun-96438a4d58569abedccdfe6acff0ad6ed60bb5bc.zip
test crypto.randomBytes returns a Buffer
-rw-r--r--src/bun.js/crypto.exports.js34
-rw-r--r--test/bun.js/node-crypto.test.js8
2 files changed, 11 insertions, 31 deletions
diff --git a/src/bun.js/crypto.exports.js b/src/bun.js/crypto.exports.js
index cd06879bb..6362a6ea1 100644
--- a/src/bun.js/crypto.exports.js
+++ b/src/bun.js/crypto.exports.js
@@ -8456,38 +8456,10 @@ var require_brorand = __commonJS({
return this._rand(len);
};
Rand.prototype._rand = function (n) {
- if (this.rand.getBytes) return this.rand.getBytes(n);
- for (var res = new Uint8Array(n), i = 0; i < res.length; i++)
- res[i] = this.rand.getByte();
- return res;
+ var out = new Buffer(n);
+ crypto.getRandomValues(out);
+ return out;
};
- if (typeof self == "object")
- self.crypto && self.crypto.getRandomValues
- ? (Rand.prototype._rand = function (n) {
- var arr = new Uint8Array(n);
- return self.crypto.getRandomValues(arr), arr;
- })
- : self.msCrypto && self.msCrypto.getRandomValues
- ? (Rand.prototype._rand = function (n) {
- var arr = new Uint8Array(n);
- return self.msCrypto.getRandomValues(arr), arr;
- })
- : typeof window == "object" &&
- (Rand.prototype._rand = function () {
- throw new Error("Not implemented yet");
- });
- else
- try {
- if (
- ((crypto2 = require_crypto_browserify()),
- typeof crypto2.randomBytes != "function")
- )
- throw new Error("Not supported");
- Rand.prototype._rand = function (n) {
- return crypto2.randomBytes(n);
- };
- } catch {}
- var crypto2;
},
});
diff --git a/test/bun.js/node-crypto.test.js b/test/bun.js/node-crypto.test.js
new file mode 100644
index 000000000..edd6f6e3d
--- /dev/null
+++ b/test/bun.js/node-crypto.test.js
@@ -0,0 +1,8 @@
+import { it, expect } from "bun:test";
+
+const nodeCrypto = require("node:crypto");
+
+it("crypto.randomBytes should return a Buffer", () => {
+ expect(nodeCrypto.randomBytes(1) instanceof Buffer).toBe(true);
+ expect(Buffer.isBuffer(nodeCrypto.randomBytes(1))).toBe(true);
+});