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 _bytesToUuid = _interopRequireDefault(require("./bytesToUuid.js"));
|
10 |
|
11 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
12 |
|
13 | function uuidToBytes(uuid) {
|
14 |
|
15 | const bytes = [];
|
16 | uuid.replace(/[a-fA-F0-9]{2}/g, function (hex) {
|
17 | bytes.push(parseInt(hex, 16));
|
18 | });
|
19 | return bytes;
|
20 | }
|
21 |
|
22 | function stringToBytes(str) {
|
23 | str = unescape(encodeURIComponent(str));
|
24 |
|
25 | const bytes = [];
|
26 |
|
27 | for (let i = 0; i < str.length; ++i) {
|
28 | bytes.push(str.charCodeAt(i));
|
29 | }
|
30 |
|
31 | return bytes;
|
32 | }
|
33 |
|
34 | const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
|
35 | exports.DNS = DNS;
|
36 | const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
|
37 | exports.URL = URL;
|
38 |
|
39 | function _default(name, version, hashfunc) {
|
40 | function generateUUID(value, namespace, buf, offset) {
|
41 | const off = buf && offset || 0;
|
42 | if (typeof value === 'string') value = stringToBytes(value);
|
43 | if (typeof namespace === 'string') namespace = uuidToBytes(namespace);
|
44 |
|
45 | if (!Array.isArray(value)) {
|
46 | throw TypeError('value must be an array of bytes');
|
47 | }
|
48 |
|
49 | if (!Array.isArray(namespace) || namespace.length !== 16) {
|
50 | throw TypeError('namespace must be uuid string or an Array of 16 byte values');
|
51 | }
|
52 |
|
53 |
|
54 | const bytes = hashfunc(namespace.concat(value));
|
55 | bytes[6] = bytes[6] & 0x0f | version;
|
56 | bytes[8] = bytes[8] & 0x3f | 0x80;
|
57 |
|
58 | if (buf) {
|
59 | for (let idx = 0; idx < 16; ++idx) {
|
60 | buf[off + idx] = bytes[idx];
|
61 | }
|
62 | }
|
63 |
|
64 | return buf || (0, _bytesToUuid.default)(bytes);
|
65 | }
|
66 |
|
67 |
|
68 | try {
|
69 | generateUUID.name = name;
|
70 | } catch (err) {}
|
71 |
|
72 |
|
73 | generateUUID.DNS = DNS;
|
74 | generateUUID.URL = URL;
|
75 | return generateUUID;
|
76 | } |
\ | No newline at end of file |