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 | if (typeof value === 'string') {
|
42 | value = stringToBytes(value);
|
43 | }
|
44 |
|
45 | if (typeof namespace === 'string') {
|
46 | namespace = uuidToBytes(namespace);
|
47 | }
|
48 |
|
49 | if (!Array.isArray(value)) {
|
50 | throw TypeError('value must be an array of bytes');
|
51 | }
|
52 |
|
53 | if (!Array.isArray(namespace) || namespace.length !== 16) {
|
54 | throw TypeError('namespace must be uuid string or an Array of 16 byte values');
|
55 | }
|
56 |
|
57 |
|
58 | const bytes = hashfunc(namespace.concat(value));
|
59 | bytes[6] = bytes[6] & 0x0f | version;
|
60 | bytes[8] = bytes[8] & 0x3f | 0x80;
|
61 |
|
62 | if (buf) {
|
63 | offset = offset || 0;
|
64 |
|
65 | for (let i = 0; i < 16; ++i) {
|
66 | buf[offset + i] = bytes[i];
|
67 | }
|
68 |
|
69 | return buf;
|
70 | }
|
71 |
|
72 | return (0, _bytesToUuid.default)(bytes);
|
73 | }
|
74 |
|
75 |
|
76 | try {
|
77 | generateUUID.name = name;
|
78 | } catch (err) {}
|
79 |
|
80 |
|
81 | generateUUID.DNS = DNS;
|
82 | generateUUID.URL = URL;
|
83 | return generateUUID;
|
84 | } |
\ | No newline at end of file |