|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | 3 | const RawSource = require("../").RawSource; |
| 4 | +const crypto = require("crypto"); |
| 5 | +const BatchedHash = require("webpack/lib/util/hash/BatchedHash"); |
| 6 | +const createMd4 = require("webpack/lib/util/hash/md4"); |
| 7 | +const createXXHash64 = require("webpack/lib/util/hash/xxhash64"); |
4 | 8 | const { |
5 | 9 | enableDualStringBufferCaching, |
6 | 10 | enterStringInterningRange, |
@@ -29,6 +33,50 @@ describe("RawSource", () => { |
29 | 33 | expect.assertions(3); |
30 | 34 | }); |
31 | 35 |
|
| 36 | + for (const hash of [ |
| 37 | + ["md5", [crypto.createHash("md5"), crypto.createHash("md5")]], |
| 38 | + ["md4", [new BatchedHash(createMd4()), new BatchedHash(createMd4())]], |
| 39 | + [ |
| 40 | + "xxhash64", |
| 41 | + [new BatchedHash(createXXHash64()), new BatchedHash(createXXHash64())] |
| 42 | + ] |
| 43 | + ]) { |
| 44 | + it(`should have the same hash (${hash[0]}) for string and Buffer`, () => { |
| 45 | + const sourceString = new RawSource("Text"); |
| 46 | + const sourceBuffer = new RawSource(Buffer.from("Text")); |
| 47 | + |
| 48 | + expect(sourceString.source()).toBe("Text"); |
| 49 | + expect(sourceString.buffer()).toEqual(sourceBuffer.buffer()); |
| 50 | + |
| 51 | + sourceString.updateHash(hash[1][0]); |
| 52 | + sourceBuffer.updateHash(hash[1][1]); |
| 53 | + |
| 54 | + expect(hash[1][0].digest("hex")).toBe(hash[1][1].digest("hex")); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + for (const hash of [ |
| 59 | + ["md5", [crypto.createHash("md5"), crypto.createHash("md5")]], |
| 60 | + ["md4", [new BatchedHash(createMd4()), new BatchedHash(createMd4())]], |
| 61 | + [ |
| 62 | + "xxhash64", |
| 63 | + [new BatchedHash(createXXHash64()), new BatchedHash(createXXHash64())] |
| 64 | + ] |
| 65 | + ]) { |
| 66 | + it(`should have the same hash (${hash[0]}) for string and Buffer (convert to string)`, () => { |
| 67 | + const sourceString = new RawSource("Text", true); |
| 68 | + const sourceBuffer = new RawSource(Buffer.from("Text"), true); |
| 69 | + |
| 70 | + expect(sourceString.source()).toBe("Text"); |
| 71 | + expect(sourceString.buffer()).toEqual(sourceBuffer.buffer()); |
| 72 | + |
| 73 | + sourceString.updateHash(hash[1][0]); |
| 74 | + sourceBuffer.updateHash(hash[1][1]); |
| 75 | + |
| 76 | + expect(hash[1][0].digest("hex")).toBe(hash[1][1].digest("hex")); |
| 77 | + }); |
| 78 | + } |
| 79 | + |
32 | 80 | describe("memory optimizations are enabled", () => { |
33 | 81 | beforeEach(() => { |
34 | 82 | disableDualStringBufferCaching(); |
|
0 commit comments