generateDebugId.js 922 B

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const createHash = require("./createHash");
  7. /**
  8. * @param {string | Buffer} content content
  9. * @param {string} file file
  10. * @returns {string} generated debug id
  11. */
  12. module.exports = (content, file) => {
  13. // We need a uuid which is 128 bits so we need 2x 64 bit hashes.
  14. // The first 64 bits is a hash of the source.
  15. const sourceHash = createHash("xxhash64").update(content).digest("hex");
  16. // The next 64 bits is a hash of the filename and sourceHash
  17. const hash128 = `${sourceHash}${createHash("xxhash64")
  18. .update(file)
  19. .update(sourceHash)
  20. .digest("hex")}`;
  21. return [
  22. hash128.slice(0, 8),
  23. hash128.slice(8, 12),
  24. `4${hash128.slice(12, 15)}`,
  25. ((Number.parseInt(hash128.slice(15, 16), 16) & 3) | 8).toString(16) +
  26. hash128.slice(17, 20),
  27. hash128.slice(20, 32)
  28. ].join("-");
  29. };