aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-19 20:28:34 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-19 20:28:34 -0800
commit4c0b0e2e8ee3a2710ea3d5b3711efc905f6f2be5 (patch)
tree8a559a3eba5b1f6d1f0ece48f1619f1d752a58dc
parentbab7e63d7cf928460c34c8bf438fc167fe5e7b31 (diff)
downloadbun-4c0b0e2e8ee3a2710ea3d5b3711efc905f6f2be5.tar.gz
bun-4c0b0e2e8ee3a2710ea3d5b3711efc905f6f2be5.tar.zst
bun-4c0b0e2e8ee3a2710ea3d5b3711efc905f6f2be5.zip
Fix buffer encoding bug
Diffstat (limited to '')
-rw-r--r--src/bun.js/node/types.zig6
-rw-r--r--test/bun.js/node-crypto.test.js15
2 files changed, 17 insertions, 4 deletions
diff --git a/src/bun.js/node/types.zig b/src/bun.js/node/types.zig
index 79e2c0618..e7f76c018 100644
--- a/src/bun.js/node/types.zig
+++ b/src/bun.js/node/types.zig
@@ -221,6 +221,10 @@ pub const StringOrBuffer = union(Tag) {
}
pub fn fromJS(global: *JSC.JSGlobalObject, allocator: std.mem.Allocator, value: JSC.JSValue, exception: JSC.C.ExceptionRef) ?StringOrBuffer {
+ if (!value.isCell()) {
+ var zig_str = value.toSlice(global, allocator);
+ return StringOrBuffer{ .string = zig_str.slice() };
+ }
return switch (value.jsType()) {
JSC.JSValue.JSType.String, JSC.JSValue.JSType.StringObject, JSC.JSValue.JSType.DerivedStringObject, JSC.JSValue.JSType.Object => {
var zig_str = value.toSlice(global, allocator);
@@ -418,7 +422,7 @@ pub const SliceOrBuffer = union(Tag) {
}
const out = brk: {
- if (zig_str.is16Bit()) {
+ if (!zig_str.is16Bit()) {
const buf = zig_str.slice();
break :brk switch (encoding) {
Encoding.utf8 => JSC.WebCore.Encoder.constructFromU8(buf.ptr, buf.len, .utf8),
diff --git a/test/bun.js/node-crypto.test.js b/test/bun.js/node-crypto.test.js
index edd6f6e3d..227368760 100644
--- a/test/bun.js/node-crypto.test.js
+++ b/test/bun.js/node-crypto.test.js
@@ -1,8 +1,17 @@
import { it, expect } from "bun:test";
-const nodeCrypto = require("node:crypto");
+import crypto from "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);
+ expect(crypto.randomBytes(1) instanceof Buffer).toBe(true);
+ expect(Buffer.isBuffer(crypto.randomBytes(1))).toBe(true);
+});
+
+// https://github.com/oven-sh/bun/issues/1839
+it("crypto.createHash ", () => {
+ function fn() {
+ crypto.createHash("sha1").update(Math.random(), "ascii").digest("base64");
+ }
+
+ for (let i = 0; i < 10; i++) fn();
});