Skip to content

Commit

Permalink
refactor: improve
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jun 4, 2024
1 parent fa8de8f commit bb961ec
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 21 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ 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)
- `[<hashType>:contenthash:<digestType>:<length>]` 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`, `base64-safe`
- 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)
- `[<hashType>:hash:<digestType>:<length>]` 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`, `base64-safe`
- 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 `base64-safe` don't contain `/`, `+` and `=` symbols.
`digestType` with `base64safe` don't contain `/`, `+` and `=` symbols.

Examples

Expand Down
20 changes: 4 additions & 16 deletions lib/getHashDigest.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ function getHashDigest(buffer, algorithm, digestType, maxLength) {
algorithm = algorithm || "xxhash64";
maxLength = maxLength || 9999;

const makeSafe = digestType && digestType.endsWith("-safe");
if (makeSafe) {
// remove '-safe' from 'digestType'
digestType = digestType.slice(0, -5);
}

let hash;

if (algorithm === "xxhash64") {
Expand Down Expand Up @@ -126,19 +120,13 @@ 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 {
const hashResult = hash.digest(digestType || "hex").substr(0, maxLength);
if (makeSafe) {
return hashResult
.replace(/\//g, "_")
.replace(/\+/g, "-")
.replace(/=/g, "");
}
return hashResult;
}

return hash.digest(digestType || "hex").substr(0, maxLength);
}

module.exports = getHashDigest;
2 changes: 1 addition & 1 deletion lib/interpolateName.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function interpolateName(loaderContext, name, options = {}) {
// `hash` and `contenthash` are same in `loader-utils` context
// let's keep `hash` for backward compatibility
.replace(
/\[(?:([^[:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*(?:-safe)?))?(?::(\d+))?\]/gi,
/\[(?:([^[:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*))?(?::(\d+))?\]/gi,
(all, hashType, digestType, maxLength) =>
getHashDigest(content, hashType, digestType, parseInt(maxLength, 10))
);
Expand Down
2 changes: 1 addition & 1 deletion test/getHashDigest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +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", "base64-safe", undefined, "Rrlif-z0m4Dq8BwB2Grp_Q"],
["abc\\0♥", "md4", "base64safe", undefined, "Rrlif-z0m4Dq8BwB2Grp_Q"],
["abc\\0💩", "md4", "base52", undefined, "dtXZENFEkYHXGxOkJbevPoD"],
["abc\\0♥", "md4", "base52", undefined, "fYFFcfXRGsVweukHKlPayHs"],

Expand Down

0 comments on commit bb961ec

Please sign in to comment.