| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | /** @typedef {import("webpack-sources").Source} Source */
|
|---|
| 9 | /** @typedef {import("./Hash")} Hash */
|
|---|
| 10 |
|
|---|
| 11 | /** @type {WeakMap<Source, WeakMap<Source, boolean>>} */
|
|---|
| 12 | const equalityCache = new WeakMap();
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Checks whether source equal true, when both sources are equal.
|
|---|
| 16 | * @param {Source} a a source
|
|---|
| 17 | * @param {Source} b another source
|
|---|
| 18 | * @returns {boolean} true, when both sources are equal
|
|---|
| 19 | */
|
|---|
| 20 | const _isSourceEqual = (a, b) => {
|
|---|
| 21 | // prefer .buffer(), it's called anyway during emit
|
|---|
| 22 | /** @type {Buffer | string} */
|
|---|
| 23 | let aSource = typeof a.buffer === "function" ? a.buffer() : a.source();
|
|---|
| 24 | /** @type {Buffer | string} */
|
|---|
| 25 | let bSource = typeof b.buffer === "function" ? b.buffer() : b.source();
|
|---|
| 26 | if (aSource === bSource) return true;
|
|---|
| 27 | if (typeof aSource === "string" && typeof bSource === "string") return false;
|
|---|
| 28 | if (!Buffer.isBuffer(aSource)) aSource = Buffer.from(aSource, "utf8");
|
|---|
| 29 | if (!Buffer.isBuffer(bSource)) bSource = Buffer.from(bSource, "utf8");
|
|---|
| 30 | return aSource.equals(bSource);
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * Checks whether this object is source equal.
|
|---|
| 35 | * @param {Source} a a source
|
|---|
| 36 | * @param {Source} b another source
|
|---|
| 37 | * @returns {boolean} true, when both sources are equal
|
|---|
| 38 | */
|
|---|
| 39 | const isSourceEqual = (a, b) => {
|
|---|
| 40 | if (a === b) return true;
|
|---|
| 41 | const cache1 = equalityCache.get(a);
|
|---|
| 42 | if (cache1 !== undefined) {
|
|---|
| 43 | const result = cache1.get(b);
|
|---|
| 44 | if (result !== undefined) return result;
|
|---|
| 45 | }
|
|---|
| 46 | const result = _isSourceEqual(a, b);
|
|---|
| 47 | if (cache1 !== undefined) {
|
|---|
| 48 | cache1.set(b, result);
|
|---|
| 49 | } else {
|
|---|
| 50 | const map = new WeakMap();
|
|---|
| 51 | map.set(b, result);
|
|---|
| 52 | equalityCache.set(a, map);
|
|---|
| 53 | }
|
|---|
| 54 | const cache2 = equalityCache.get(b);
|
|---|
| 55 | if (cache2 !== undefined) {
|
|---|
| 56 | cache2.set(a, result);
|
|---|
| 57 | } else {
|
|---|
| 58 | const map = new WeakMap();
|
|---|
| 59 | map.set(a, result);
|
|---|
| 60 | equalityCache.set(b, map);
|
|---|
| 61 | }
|
|---|
| 62 | return result;
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 | // TODO remove in webpack 6, this is protection against authors who directly use `webpack-sources` outdated version
|
|---|
| 66 | /**
|
|---|
| 67 | * Feeds the Source's content into a Hash without forcing a single
|
|---|
| 68 | * concatenated Buffer. Uses webpack-sources >= 3.4.0 `buffers()` when
|
|---|
| 69 | * available so `ConcatSource` can stream its children directly.
|
|---|
| 70 | * @param {Hash} hash hash to update
|
|---|
| 71 | * @param {Source} source source whose bytes are appended
|
|---|
| 72 | * @returns {void}
|
|---|
| 73 | */
|
|---|
| 74 | const updateHashFromSource = (hash, source) => {
|
|---|
| 75 | // TODO webpack 6: drop the `buffers` check, require webpack-sources >= 3.4
|
|---|
| 76 | // and call `source.buffers()` unconditionally.
|
|---|
| 77 | if (typeof source.buffers === "function") {
|
|---|
| 78 | for (const buf of source.buffers()) hash.update(buf);
|
|---|
| 79 | } else {
|
|---|
| 80 | hash.update(source.buffer());
|
|---|
| 81 | }
|
|---|
| 82 | };
|
|---|
| 83 |
|
|---|
| 84 | module.exports.isSourceEqual = isSourceEqual;
|
|---|
| 85 | module.exports.updateHashFromSource = updateHashFromSource;
|
|---|