Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add digestType 'base64-safe' #259

Merged
merged 7 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- `[<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`
- other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`, `base64-safe`
- 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`
- other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`, `base64-safe`
- 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.

Examples

```javascript
Expand Down Expand Up @@ -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`, `base64-safe`
- `maxLength` the maximum length in chars

## License
Expand Down
15 changes: 14 additions & 1 deletion lib/getHashDigest.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ 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 @@ -124,7 +130,14 @@ function getHashDigest(buffer, algorithm, digestType, maxLength) {
) {
return encodeBufferToBase(hash.digest(), digestType.substr(4), maxLength);
} else {
return hash.digest(digestType || "hex").substr(0, maxLength);
const hashResult = hash.digest(digestType || "hex").substr(0, maxLength);
if (makeSafe) {
return hashResult
.replace(/\//g, "_")
.replace(/\+/g, "-")
.replace(/=/g, "");
}
return hashResult;
}
}

Expand Down
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*))?(?::(\d+))?\]/gi,
/\[(?:([^[:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*(?:-safe)?))?(?::(\d+))?\]/gi,
(all, hashType, digestType, maxLength) =>
getHashDigest(content, hashType, digestType, parseInt(maxLength, 10))
);
Expand Down
1 change: 1 addition & 0 deletions test/getHashDigest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", "base64-safe", undefined, "Rrlif-z0m4Dq8BwB2Grp_Q"],
["abc\\0💩", "md4", "base52", undefined, "dtXZENFEkYHXGxOkJbevPoD"],
["abc\\0♥", "md4", "base52", undefined, "fYFFcfXRGsVweukHKlPayHs"],

Expand Down
Loading