aboutsummaryrefslogtreecommitdiff
path: root/src/bun.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 /src/bun.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 'src/bun.js')
-rw-r--r--src/bun.js/crypto.exports.js172
1 files changed, 130 insertions, 42 deletions
diff --git a/src/bun.js/crypto.exports.js b/src/bun.js/crypto.exports.js
index 4001179e8..eae626fa1 100644
--- a/src/bun.js/crypto.exports.js
+++ b/src/bun.js/crypto.exports.js
@@ -1134,64 +1134,152 @@ var require_browser2 = __commonJS({
"node_modules/create-hash/browser.js"(exports, module) {
"use strict";
const { Transform } = stream_exports;
- class Hash extends Transform {
- #hasher;
- #algorithm;
- #finalized = false;
- constructor(algorithm, options) {
- super(options);
- this.#algorithm = algorithm;
- this.#hasher = new CryptoHasher(algorithm);
- }
-
- #checkFinalized() {
- if (this.#finalized) {
- var err = new Error("Digest already called");
- err.code = "ERR_CRYPTO_HASH_FINALIZED";
- throw err;
- }
- }
-
- update(data, encoding) {
- this.#checkFinalized();
- if (arguments.length > 1) {
- this.#hasher.update(data, encoding);
- } else {
- this.#hasher.update(data);
- }
-
- return this;
+ // does not become a node stream unless you create it into one
+ const LazyHash = function Hash(algorithm, options) {
+ this._options = options;
+ this._hasher = new CryptoHasher(algorithm, options);
+ this._finalized = false;
+ };
+ LazyHash.prototype = Object.create(Transform.prototype);
+ LazyHash.prototype.update = function update(data, encoding) {
+ this._checkFinalized();
+ this._hasher.update(data, encoding);
+ return this;
+ };
+ LazyHash.prototype.digest = function update(data, encoding) {
+ this._checkFinalized();
+ this._finalized = true;
+ return this._hasher.digest(data, encoding);
+ };
+ LazyHash.prototype._checkFinalized = function _checkFinalized() {
+ if (this._finalized) {
+ var err = new Error("Digest already called");
+ err.code = "ERR_CRYPTO_HASH_FINALIZED";
+ throw err;
}
+ };
+ LazyHash.prototype.copy = function copy() {
+ throw new Error("Hash.copy() is not implemented in bun. https://github.com/oven-sh/bun/issues/2651");
+ };
+ const lazyHashFullInitProto = {
+ __proto__: Transform.prototype,
+ ...LazyHash.prototype,
_transform(data, encoding, callback) {
this.update(data, encoding);
callback && callback();
- }
-
+ },
_flush(callback) {
this.push(this.digest());
callback();
- }
-
- digest(encoding) {
- this.#checkFinalized();
- this.#finalized = true;
-
- return this.#hasher.digest(encoding);
- }
+ },
+ };
- copy() {
- return new CryptoHasher(this.#algorithm);
- }
+ const triggerMethods = [
+ "_events",
+ "_eventsCount",
+ "_final",
+ "_maxListeners",
+ "_maxListeners",
+ "_read",
+ "_undestroy",
+ "_writableState",
+ "_write",
+ "_writev",
+ "addListener",
+ "asIndexedPairs",
+ "closed",
+ "compose",
+ "constructor",
+ "cork",
+ "destroy",
+ "destroyed",
+ "drop",
+ "emit",
+ "end",
+ "errored",
+ "eventNames",
+ "every",
+ "filter",
+ "find",
+ "flatMap",
+ "forEach",
+ "getMaxListeners",
+ "hasOwnProperty",
+ "isPaused",
+ "isPrototypeOf",
+ "iterator",
+ "listenerCount",
+ "listeners",
+ "map",
+ "off",
+ "on",
+ "once",
+ "pause",
+ "pipe",
+ "prependListener",
+ "prependOnceListener",
+ "propertyIsEnumerable",
+ "push",
+ "rawListeners",
+ "read",
+ "readable",
+ "readableAborted",
+ "readableBuffer",
+ "readableDidRead",
+ "readableEncoding",
+ "readableEnded",
+ "readableFlowing",
+ "readableHighWaterMark",
+ "readableLength",
+ "readableObjectMode",
+ "reduce",
+ "removeAllListeners",
+ "removeListener",
+ "resume",
+ "setDefaultEncoding",
+ "setEncoding",
+ "setMaxListeners",
+ "some",
+ "take",
+ "toArray",
+ "toLocaleString",
+ "toString",
+ "uncork",
+ "unpipe",
+ "unshift",
+ "valueOf",
+ "wrap",
+ "writable",
+ "writableBuffer",
+ "writableCorked",
+ "writableEnded",
+ "writableFinished",
+ "writableHighWaterMark",
+ "writableLength",
+ "writableNeedDrain",
+ "writableObjectMode",
+ "write",
+ ];
+ for (const method of triggerMethods) {
+ Object.defineProperty(LazyHash.prototype, method, {
+ get() {
+ Object.setPrototypeOf(this, lazyHashFullInitProto);
+ Transform.call(this, this._options);
+ return this[method];
+ },
+ enumerable: false,
+ configurable: true,
+ });
}
module.exports = function createHash(algorithm) {
- return new Hash(algorithm);
+ return new LazyHash(algorithm);
};
module.exports.createHash = module.exports;
- module.exports.Hash = Hash;
+ module.exports.Hash = LazyHash;
},
});