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