generateDebugId.js 953 B

12345678910111213141516171819202122232425262728293031323334
  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. * Returns generated debug id.
  9. * @param {string | Buffer} content content
  10. * @param {string} file file
  11. * @returns {string} generated debug id
  12. */
  13. module.exports = (content, file) => {
  14. // We need a uuid which is 128 bits so we need 2x 64 bit hashes.
  15. // The first 64 bits is a hash of the source.
  16. const sourceHash = createHash("xxhash64").update(content).digest("hex");
  17. // The next 64 bits is a hash of the filename and sourceHash
  18. const hash128 = `${sourceHash}${createHash("xxhash64")
  19. .update(file)
  20. .update(sourceHash)
  21. .digest("hex")}`;
  22. return [
  23. hash128.slice(0, 8),
  24. hash128.slice(8, 12),
  25. `4${hash128.slice(12, 15)}`,
  26. ((Number.parseInt(hash128.slice(15, 16), 16) & 3) | 8).toString(16) +
  27. hash128.slice(17, 20),
  28. hash128.slice(20, 32)
  29. ].join("-");
  30. };