aboutsummaryrefslogtreecommitdiff
path: root/test/js
diff options
context:
space:
mode:
authorGravatar dave caruso <me@paperdave.net> 2023-04-14 00:55:01 -0400
committerGravatar GitHub <noreply@github.com> 2023-04-13 21:55:01 -0700
commit3a2fd65f20d3b4e99c89f789acec5e5e40615008 (patch)
treed0491f57d2f612aaec638f52bdb36155e5b3bdde /test/js
parent267a38f6fc226156a50292945a697308e7201e69 (diff)
downloadbun-3a2fd65f20d3b4e99c89f789acec5e5e40615008.tar.gz
bun-3a2fd65f20d3b4e99c89f789acec5e5e40615008.tar.zst
bun-3a2fd65f20d3b4e99c89f789acec5e5e40615008.zip
use a lazyily initialized stream for `node:crypto` `createHash` (#2652)
* lazy hash * finish up crypto stuff * remove lockfiles * ok * add pipe test * update this lockfile * remove unrelated crypto benchmark from this file
Diffstat (limited to 'test/js')
-rw-r--r--test/js/node/crypto/crypto-lazyhash.test.ts13
-rw-r--r--test/js/node/crypto/crypto.test.ts1
-rw-r--r--test/js/node/crypto/node-crypto.test.js73
3 files changed, 80 insertions, 7 deletions
diff --git a/test/js/node/crypto/crypto-lazyhash.test.ts b/test/js/node/crypto/crypto-lazyhash.test.ts
new file mode 100644
index 000000000..78ac4daf7
--- /dev/null
+++ b/test/js/node/crypto/crypto-lazyhash.test.ts
@@ -0,0 +1,13 @@
+import { describe, expect, test } from "bun:test";
+import { Hash, createHash } from "crypto";
+import { Transform } from "stream";
+
+describe("LazyHash quirks", () => {
+ test("hash instanceof Transform", () => {
+ const hash = createHash("sha256");
+ expect(hash instanceof Transform).toBe(true);
+ });
+ test("Hash.prototype instanceof Transform", () => {
+ expect(Hash.prototype instanceof Transform).toBe(true);
+ });
+});
diff --git a/test/js/node/crypto/crypto.test.ts b/test/js/node/crypto/crypto.test.ts
index b5b8e9286..afec25813 100644
--- a/test/js/node/crypto/crypto.test.ts
+++ b/test/js/node/crypto/crypto.test.ts
@@ -1,6 +1,5 @@
import { sha, MD5, MD4, SHA1, SHA224, SHA256, SHA384, SHA512, SHA512_256, gc, CryptoHasher } from "bun";
import { it, expect, describe } from "bun:test";
-import { readFileSync } from "fs";
const HashClasses = [MD5, MD4, SHA1, SHA224, SHA256, SHA384, SHA512, SHA512_256];
diff --git a/test/js/node/crypto/node-crypto.test.js b/test/js/node/crypto/node-crypto.test.js
index 324a2790f..bf5414196 100644
--- a/test/js/node/crypto/node-crypto.test.js
+++ b/test/js/node/crypto/node-crypto.test.js
@@ -1,6 +1,7 @@
-import { it, expect } from "bun:test";
+import { it, expect, describe } from "bun:test";
import crypto from "node:crypto";
+import { PassThrough, Readable } from "node:stream";
it("crypto.randomBytes should return a Buffer", () => {
expect(crypto.randomBytes(1) instanceof Buffer).toBe(true);
@@ -8,12 +9,72 @@ it("crypto.randomBytes should return a Buffer", () => {
});
// https://github.com/oven-sh/bun/issues/1839
-it("crypto.createHash ", () => {
- function fn() {
- crypto.createHash("sha1").update(Math.random(), "ascii").digest("base64");
- }
+describe("createHash", () => {
+ it("update & digest", () => {
+ const hash = crypto.createHash("sha256");
+ hash.update("some data to hash");
+ expect(hash.digest("hex")).toBe("6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50");
+ });
- for (let i = 0; i < 10; i++) fn();
+ it("stream (sync)", () => {
+ const hash = crypto.createHash("sha256");
+ hash.write("some data to hash");
+ hash.end();
+ expect(hash.read().toString("hex")).toBe("6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50");
+ });
+
+ it("stream (async)", done => {
+ const hash = crypto.createHash("sha256");
+ hash.on("readable", () => {
+ const data = hash.read();
+ if (data) {
+ expect(data.toString("hex")).toBe("6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50");
+ done();
+ }
+ });
+ hash.write("some data to hash");
+ hash.end();
+ });
+
+ it("stream multiple chunks", done => {
+ const hash = crypto.createHash("sha256");
+ hash.write("some data to hash");
+ hash.on("readable", () => {
+ const data = hash.read();
+ if (data) {
+ expect(data.toString("hex")).toBe("43cc4cdc6bd7799b13da2d7c94bba96f3768bf7c4eba7038e0c393e4474fc9e5");
+ done();
+ }
+ });
+ hash.write("some data to hash");
+ hash.write("some data to hash");
+ hash.end();
+ });
+
+ it("stream with pipe", done => {
+ const hash = crypto.createHash("sha256");
+ const s = new PassThrough();
+
+ hash.on("readable", () => {
+ const data = hash.read();
+ if (data) {
+ expect(data.toString("hex")).toBe("0e1076315962f2e639ba2eea46223a813dafea530425613948c4b21635abd8fc");
+ done();
+ }
+ });
+ s.write("Hello world");
+ s.pipe(hash);
+ s.write("Bun!");
+ s.end();
+ });
+
+ it("repeated calls doesnt segfault", () => {
+ function fn() {
+ crypto.createHash("sha1").update(Math.random(), "ascii").digest("base64");
+ }
+
+ for (let i = 0; i < 10; i++) fn();
+ });
});
it("crypto.createHmac", () => {