v35.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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(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 v35(name, version, hashfunc) {
  23. function generateUUID(value, namespace, buf, offset) {
  24. var _namespace;
  25. if (typeof value === 'string') {
  26. value = stringToBytes(value);
  27. }
  28. if (typeof namespace === 'string') {
  29. namespace = (0, _parse.default)(namespace);
  30. }
  31. if (((_namespace = namespace) === null || _namespace === void 0 ? void 0 : _namespace.length) !== 16) {
  32. throw TypeError('Namespace must be array-like (16 iterable integer values, 0-255)');
  33. } // Compute hash of namespace and value, Per 4.3
  34. // Future: Use spread syntax when supported on all platforms, e.g. `bytes =
  35. // hashfunc([...namespace, ... value])`
  36. let bytes = new Uint8Array(16 + value.length);
  37. bytes.set(namespace);
  38. bytes.set(value, namespace.length);
  39. bytes = hashfunc(bytes);
  40. bytes[6] = bytes[6] & 0x0f | version;
  41. bytes[8] = bytes[8] & 0x3f | 0x80;
  42. if (buf) {
  43. offset = offset || 0;
  44. for (let i = 0; i < 16; ++i) {
  45. buf[offset + i] = bytes[i];
  46. }
  47. return buf;
  48. }
  49. return (0, _stringify.unsafeStringify)(bytes);
  50. } // Function#name is not settable on some platforms (#270)
  51. try {
  52. generateUUID.name = name; // eslint-disable-next-line no-empty
  53. } catch (err) {} // For CommonJS default export support
  54. generateUUID.DNS = DNS;
  55. generateUUID.URL = URL;
  56. return generateUUID;
  57. }