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 5 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`, `base64safe`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to base64safe (without -), because regexp in interpolateName can be used in many place and I don't want to change it

- 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`, `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
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
7 changes: 4 additions & 3 deletions lib/getHashDigest.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +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 {
return hash.digest(digestType || "hex").substr(0, maxLength);
}

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

module.exports = getHashDigest;
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", "base64safe", undefined, "Rrlif-z0m4Dq8BwB2Grp_Q"],
["abc\\0💩", "md4", "base52", undefined, "dtXZENFEkYHXGxOkJbevPoD"],
["abc\\0♥", "md4", "base52", undefined, "fYFFcfXRGsVweukHKlPayHs"],

Expand Down
Loading