123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = _default;
- exports.URL = exports.DNS = void 0;
- var _bytesToUuid = _interopRequireDefault(require("./bytesToUuid.js"));
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- function uuidToBytes(uuid) {
- // Note: We assume we're being passed a valid uuid string
- const bytes = [];
- uuid.replace(/[a-fA-F0-9]{2}/g, function (hex) {
- bytes.push(parseInt(hex, 16));
- });
- return bytes;
- }
- function stringToBytes(str) {
- str = unescape(encodeURIComponent(str)); // UTF8 escape
- const bytes = [];
- for (let i = 0; i < str.length; ++i) {
- bytes.push(str.charCodeAt(i));
- }
- return bytes;
- }
- const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
- exports.DNS = DNS;
- const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
- exports.URL = URL;
- function _default(name, version, hashfunc) {
- function generateUUID(value, namespace, buf, offset) {
- if (typeof value === 'string') {
- value = stringToBytes(value);
- }
- if (typeof namespace === 'string') {
- namespace = uuidToBytes(namespace);
- }
- if (!Array.isArray(value)) {
- throw TypeError('value must be an array of bytes');
- }
- if (!Array.isArray(namespace) || namespace.length !== 16) {
- throw TypeError('namespace must be uuid string or an Array of 16 byte values');
- } // Per 4.3
- const bytes = hashfunc(namespace.concat(value));
- bytes[6] = bytes[6] & 0x0f | version;
- bytes[8] = bytes[8] & 0x3f | 0x80;
- if (buf) {
- offset = offset || 0;
- for (let i = 0; i < 16; ++i) {
- buf[offset + i] = bytes[i];
- }
- return buf;
- }
- return (0, _bytesToUuid.default)(bytes);
- } // Function#name is not settable on some platforms (#270)
- try {
- generateUUID.name = name; // eslint-disable-next-line no-empty
- } catch (err) {} // For CommonJS default export support
- generateUUID.DNS = DNS;
- generateUUID.URL = URL;
- return generateUUID;
- }
|