v35.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. exports.URL = exports.DNS = void 0;
  7. var _stringify = _interopRequireDefault(require("./stringify.js"));
  8. var _parse = _interopRequireDefault(require("./parse.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function stringToBytes(str) {
  11. str = unescape(encodeURIComponent(str)); // UTF8 escape
  12. const bytes = [];
  13. for (let i = 0; i < str.length; ++i) {
  14. bytes.push(str.charCodeAt(i));
  15. }
  16. return bytes;
  17. }
  18. const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
  19. exports.DNS = DNS;
  20. const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
  21. exports.URL = URL;
  22. function _default(name, version, hashfunc) {
  23. function generateUUID(value, namespace, buf, offset) {
  24. if (typeof value === 'string') {
  25. value = stringToBytes(value);
  26. }
  27. if (typeof namespace === 'string') {
  28. namespace = (0, _parse.default)(namespace);
  29. }
  30. if (namespace.length !== 16) {
  31. throw TypeError('Namespace must be array-like (16 iterable integer values, 0-255)');
  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. let 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 (let i = 0; i < 16; ++i) {
  44. buf[offset + i] = bytes[i];
  45. }
  46. return buf;
  47. }
  48. return (0, _stringify.default)(bytes);
  49. } // Function#name is not settable on some platforms (#270)
  50. try {
  51. generateUUID.name = name; // eslint-disable-next-line no-empty
  52. } catch (err) {} // For CommonJS default export support
  53. generateUUID.DNS = DNS;
  54. generateUUID.URL = URL;
  55. return generateUUID;
  56. }