From af157934abb1ee172cffd015acbabb065f0e1dbf Mon Sep 17 00:00:00 2001 From: denisx Date: Tue, 4 Jun 2024 20:44:18 +0300 Subject: [PATCH] feat: add digestType 'base64safe' (#259) --- README.md | 8 +++++--- lib/getHashDigest.js | 13 +++++++++---- test/getHashDigest.test.js | 1 + 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a9aee06..f142c6e 100644 --- a/README.md +++ b/README.md @@ -76,17 +76,19 @@ The following tokens are replaced in the `name` parameter: - `[contenthash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the `xxhash64` hash) - `[:contenthash::]` optionally one can configure - other `hashType`s, i. e. `xxhash64`, `sha1`, `md4` (wasm version), `native-md4` (`crypto` module version), `md5`, `sha256`, `sha512` - - other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64` + - other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`, `base64safe` - and `length` the length in chars - `[hash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the `xxhash64` hash) - `[:hash::]` optionally one can configure - other `hashType`s, i. e. `xxhash64`, `sha1`, `md4` (wasm version), `native-md4` (`crypto` module version), `md5`, `sha256`, `sha512` - - other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64` + - other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`, `base64safe` - and `length` the length in chars - `[N]` the N-th match obtained from matching the current file name against `options.regExp` In loader context `[hash]` and `[contenthash]` are the same, but we recommend using `[contenthash]` for avoid misleading. +`digestType` with `base64safe` don't contain `/`, `+` and `=` symbols. + Examples ```javascript @@ -157,7 +159,7 @@ const digestString = loaderUtils.getHashDigest( - `buffer` the content that should be hashed - `hashType` one of `xxhash64`, `sha1`, `md4`, `md5`, `sha256`, `sha512` or any other node.js supported hash type -- `digestType` one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64` +- `digestType` one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`, `base64safe` - `maxLength` the maximum length in chars ## License diff --git a/lib/getHashDigest.js b/lib/getHashDigest.js index 600b86e..5cbeb6a 100644 --- a/lib/getHashDigest.js +++ b/lib/getHashDigest.js @@ -120,12 +120,17 @@ function getHashDigest(buffer, algorithm, digestType, maxLength) { digestType === "base49" || digestType === "base52" || digestType === "base58" || - digestType === "base62" + digestType === "base62" || + digestType === "base64safe" ) { - return encodeBufferToBase(hash.digest(), digestType.substr(4), maxLength); - } else { - return hash.digest(digestType || "hex").substr(0, maxLength); + return encodeBufferToBase( + hash.digest(), + digestType === "base64safe" ? 64 : digestType.substr(4), + maxLength + ); } + + return hash.digest(digestType || "hex").substr(0, maxLength); } module.exports = getHashDigest; diff --git a/test/getHashDigest.test.js b/test/getHashDigest.test.js index e993f11..d647a1c 100644 --- a/test/getHashDigest.test.js +++ b/test/getHashDigest.test.js @@ -21,6 +21,7 @@ describe("getHashDigest()", () => { ["abc\\0💩", "md4", "hex", undefined, "45aa5b332f8e562aaf0106ad6fc1d78f"], ["abc\\0💩", "md4", "base64", undefined, "RapbMy+OViqvAQatb8HXjw=="], ["abc\\0♥", "md4", "base64", undefined, "Rrlif+z0m4Dq8BwB2Grp/Q=="], + ["abc\\0♥", "md4", "base64safe", undefined, "3ZWmHo0hPMWE2rZeN_oHB6"], ["abc\\0💩", "md4", "base52", undefined, "dtXZENFEkYHXGxOkJbevPoD"], ["abc\\0♥", "md4", "base52", undefined, "fYFFcfXRGsVweukHKlPayHs"],