Skip to content

Commit af15793

Browse files
authored
feat: add digestType 'base64safe' (#259)
1 parent 977b28d commit af15793

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,19 @@ The following tokens are replaced in the `name` parameter:
7676
- `[contenthash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the `xxhash64` hash)
7777
- `[<hashType>:contenthash:<digestType>:<length>]` optionally one can configure
7878
- other `hashType`s, i. e. `xxhash64`, `sha1`, `md4` (wasm version), `native-md4` (`crypto` module version), `md5`, `sha256`, `sha512`
79-
- other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
79+
- other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`, `base64safe`
8080
- and `length` the length in chars
8181
- `[hash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the `xxhash64` hash)
8282
- `[<hashType>:hash:<digestType>:<length>]` optionally one can configure
8383
- other `hashType`s, i. e. `xxhash64`, `sha1`, `md4` (wasm version), `native-md4` (`crypto` module version), `md5`, `sha256`, `sha512`
84-
- other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
84+
- other `digestType`s, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`, `base64safe`
8585
- and `length` the length in chars
8686
- `[N]` the N-th match obtained from matching the current file name against `options.regExp`
8787

8888
In loader context `[hash]` and `[contenthash]` are the same, but we recommend using `[contenthash]` for avoid misleading.
8989

90+
`digestType` with `base64safe` don't contain `/`, `+` and `=` symbols.
91+
9092
Examples
9193

9294
```javascript
@@ -157,7 +159,7 @@ const digestString = loaderUtils.getHashDigest(
157159

158160
- `buffer` the content that should be hashed
159161
- `hashType` one of `xxhash64`, `sha1`, `md4`, `md5`, `sha256`, `sha512` or any other node.js supported hash type
160-
- `digestType` one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
162+
- `digestType` one of `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`, `base64safe`
161163
- `maxLength` the maximum length in chars
162164

163165
## License

lib/getHashDigest.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,17 @@ function getHashDigest(buffer, algorithm, digestType, maxLength) {
120120
digestType === "base49" ||
121121
digestType === "base52" ||
122122
digestType === "base58" ||
123-
digestType === "base62"
123+
digestType === "base62" ||
124+
digestType === "base64safe"
124125
) {
125-
return encodeBufferToBase(hash.digest(), digestType.substr(4), maxLength);
126-
} else {
127-
return hash.digest(digestType || "hex").substr(0, maxLength);
126+
return encodeBufferToBase(
127+
hash.digest(),
128+
digestType === "base64safe" ? 64 : digestType.substr(4),
129+
maxLength
130+
);
128131
}
132+
133+
return hash.digest(digestType || "hex").substr(0, maxLength);
129134
}
130135

131136
module.exports = getHashDigest;

test/getHashDigest.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe("getHashDigest()", () => {
2121
["abc\\0💩", "md4", "hex", undefined, "45aa5b332f8e562aaf0106ad6fc1d78f"],
2222
["abc\\0💩", "md4", "base64", undefined, "RapbMy+OViqvAQatb8HXjw=="],
2323
["abc\\0♥", "md4", "base64", undefined, "Rrlif+z0m4Dq8BwB2Grp/Q=="],
24+
["abc\\0♥", "md4", "base64safe", undefined, "3ZWmHo0hPMWE2rZeN_oHB6"],
2425
["abc\\0💩", "md4", "base52", undefined, "dtXZENFEkYHXGxOkJbevPoD"],
2526
["abc\\0♥", "md4", "base52", undefined, "fYFFcfXRGsVweukHKlPayHs"],
2627

0 commit comments

Comments
 (0)