v35.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.URL = exports.DNS = void 0;
  6. exports.default = v35;
  7. var _stringify = require("./stringify.js");
  8. var _parse = _interopRequireDefault(require("./parse.js"));
  9. function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
  10. function stringToBytes(str) {
  11. str = unescape(encodeURIComponent(str)); // UTF8 escape
  12. var bytes = [];
  13. for (var i = 0; i < str.length; ++i) {
  14. bytes.push(str.charCodeAt(i));
  15. }
  16. return bytes;
  17. }
  18. var DNS = exports.DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
  19. var URL = exports.URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
  20. function v35(name, version, hashfunc) {
  21. function generateUUID(value, namespace, buf, offset) {
  22. var _namespace;
  23. if (typeof value === 'string') {
  24. value = stringToBytes(value);
  25. }
  26. if (typeof namespace === 'string') {
  27. namespace = (0, _parse.default)(namespace);
  28. }
  29. if (((_namespace = namespace) === null || _namespace === void 0 ? void 0 : _namespace.length) !== 16) {
  30. throw TypeError('Namespace must be array-like (16 iterable integer values, 0-255)');
  31. }
  32. // Compute hash of namespace and value, Per 4.3
  33. // Future: Use spread syntax when supported on all platforms, e.g. `bytes =
  34. // hashfunc([...namespace, ... value])`
  35. var bytes = new Uint8Array(16 + value.length);
  36. bytes.set(namespace);
  37. bytes.set(value, namespace.length);
  38. bytes = hashfunc(bytes);
  39. bytes[6] = bytes[6] & 0x0f | version;
  40. bytes[8] = bytes[8] & 0x3f | 0x80;
  41. if (buf) {
  42. offset = offset || 0;
  43. for (var i = 0; i < 16; ++i) {
  44. buf[offset + i] = bytes[i];
  45. }
  46. return buf;
  47. }
  48. return (0, _stringify.unsafeStringify)(bytes);
  49. }
  50. // Function#name is not settable on some platforms (#270)
  51. try {
  52. generateUUID.name = name;
  53. } catch (err) {}
  54. // For CommonJS default export support
  55. generateUUID.DNS = DNS;
  56. generateUUID.URL = URL;
  57. return generateUUID;
  58. }