1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.nil = exports.UUID = void 0;
|
7 |
|
8 | function _crypto() {
|
9 | const data = require("crypto");
|
10 |
|
11 | _crypto = function () {
|
12 | return data;
|
13 | };
|
14 |
|
15 | return data;
|
16 | }
|
17 |
|
18 | function _index() {
|
19 | const data = require("./index");
|
20 |
|
21 | _index = function () {
|
22 | return data;
|
23 | };
|
24 |
|
25 | return data;
|
26 | }
|
27 |
|
28 | const invalidName = "options.name must be either a string or a Buffer";
|
29 |
|
30 | const randomHost = (0, _crypto().randomBytes)(16);
|
31 | randomHost[0] = randomHost[0] | 0x01;
|
32 |
|
33 | const hex2byte = {};
|
34 |
|
35 | const byte2hex = [];
|
36 |
|
37 | for (let i = 0; i < 256; i++) {
|
38 | const hex = (i + 0x100).toString(16).substr(1);
|
39 | hex2byte[hex] = i;
|
40 | byte2hex[i] = hex;
|
41 | }
|
42 |
|
43 |
|
44 | class UUID {
|
45 | constructor(uuid) {
|
46 | this.ascii = null;
|
47 | this.binary = null;
|
48 | const check = UUID.check(uuid);
|
49 |
|
50 | if (!check) {
|
51 | throw new Error("not a UUID");
|
52 | }
|
53 |
|
54 | this.version = check.version;
|
55 |
|
56 | if (check.format === "ascii") {
|
57 | this.ascii = uuid;
|
58 | } else {
|
59 | this.binary = uuid;
|
60 | }
|
61 | }
|
62 |
|
63 | static v5(name, namespace) {
|
64 | return uuidNamed(name, "sha1", 0x50, namespace);
|
65 | }
|
66 |
|
67 | toString() {
|
68 | if (this.ascii == null) {
|
69 | this.ascii = stringify(this.binary);
|
70 | }
|
71 |
|
72 | return this.ascii;
|
73 | }
|
74 |
|
75 | inspect() {
|
76 | return `UUID v${this.version} ${this.toString()}`;
|
77 | }
|
78 |
|
79 | static check(uuid, offset = 0) {
|
80 | if (typeof uuid === "string") {
|
81 | uuid = uuid.toLowerCase();
|
82 |
|
83 | if (!/^[a-f0-9]{8}(-[a-f0-9]{4}){3}-([a-f0-9]{12})$/.test(uuid)) {
|
84 | return false;
|
85 | }
|
86 |
|
87 | if (uuid === "00000000-0000-0000-0000-000000000000") {
|
88 | return {
|
89 | version: undefined,
|
90 | variant: "nil",
|
91 | format: "ascii"
|
92 | };
|
93 | }
|
94 |
|
95 | return {
|
96 | version: (hex2byte[uuid[14] + uuid[15]] & 0xf0) >> 4,
|
97 | variant: getVariant((hex2byte[uuid[19] + uuid[20]] & 0xe0) >> 5),
|
98 | format: "ascii"
|
99 | };
|
100 | }
|
101 |
|
102 | if (Buffer.isBuffer(uuid)) {
|
103 | if (uuid.length < offset + 16) {
|
104 | return false;
|
105 | }
|
106 |
|
107 | let i = 0;
|
108 |
|
109 | for (; i < 16; i++) {
|
110 | if (uuid[offset + i] !== 0) {
|
111 | break;
|
112 | }
|
113 | }
|
114 |
|
115 | if (i === 16) {
|
116 | return {
|
117 | version: undefined,
|
118 | variant: "nil",
|
119 | format: "binary"
|
120 | };
|
121 | }
|
122 |
|
123 | return {
|
124 | version: (uuid[offset + 6] & 0xf0) >> 4,
|
125 | variant: getVariant((uuid[offset + 8] & 0xe0) >> 5),
|
126 | format: "binary"
|
127 | };
|
128 | }
|
129 |
|
130 | throw (0, _index().newError)("Unknown type of uuid", "ERR_UNKNOWN_UUID_TYPE");
|
131 | }
|
132 |
|
133 |
|
134 | static parse(input) {
|
135 | const buffer = Buffer.allocUnsafe(16);
|
136 | let j = 0;
|
137 |
|
138 | for (let i = 0; i < 16; i++) {
|
139 | buffer[i] = hex2byte[input[j++] + input[j++]];
|
140 |
|
141 | if (i === 3 || i === 5 || i === 7 || i === 9) {
|
142 | j += 1;
|
143 | }
|
144 | }
|
145 |
|
146 | return buffer;
|
147 | }
|
148 |
|
149 | }
|
150 |
|
151 |
|
152 | exports.UUID = UUID;
|
153 | UUID.OID = UUID.parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8");
|
154 |
|
155 | function getVariant(bits) {
|
156 | switch (bits) {
|
157 | case 0:
|
158 | case 1:
|
159 | case 3:
|
160 | return "ncs";
|
161 |
|
162 | case 4:
|
163 | case 5:
|
164 | return "rfc4122";
|
165 |
|
166 | case 6:
|
167 | return "microsoft";
|
168 |
|
169 | default:
|
170 | return "future";
|
171 | }
|
172 | }
|
173 |
|
174 | var UuidEncoding;
|
175 |
|
176 | (function (UuidEncoding) {
|
177 | UuidEncoding[UuidEncoding["ASCII"] = 0] = "ASCII";
|
178 | UuidEncoding[UuidEncoding["BINARY"] = 1] = "BINARY";
|
179 | UuidEncoding[UuidEncoding["OBJECT"] = 2] = "OBJECT";
|
180 | })(UuidEncoding || (UuidEncoding = {}));
|
181 |
|
182 |
|
183 | function uuidNamed(name, hashMethod, version, namespace, encoding = UuidEncoding.ASCII) {
|
184 | const hash = (0, _crypto().createHash)(hashMethod);
|
185 | const nameIsNotAString = typeof name !== "string";
|
186 |
|
187 | if (nameIsNotAString && !Buffer.isBuffer(name)) {
|
188 | throw (0, _index().newError)(invalidName, "ERR_INVALID_UUID_NAME");
|
189 | }
|
190 |
|
191 | hash.update(namespace);
|
192 | hash.update(name);
|
193 | const buffer = hash.digest();
|
194 | let result;
|
195 |
|
196 | switch (encoding) {
|
197 | case UuidEncoding.BINARY:
|
198 | buffer[6] = buffer[6] & 0x0f | version;
|
199 | buffer[8] = buffer[8] & 0x3f | 0x80;
|
200 | result = buffer;
|
201 | break;
|
202 |
|
203 | case UuidEncoding.OBJECT:
|
204 | buffer[6] = buffer[6] & 0x0f | version;
|
205 | buffer[8] = buffer[8] & 0x3f | 0x80;
|
206 | result = new UUID(buffer);
|
207 | break;
|
208 |
|
209 | default:
|
210 | result = byte2hex[buffer[0]] + byte2hex[buffer[1]] + byte2hex[buffer[2]] + byte2hex[buffer[3]] + "-" + byte2hex[buffer[4]] + byte2hex[buffer[5]] + "-" + byte2hex[buffer[6] & 0x0f | version] + byte2hex[buffer[7]] + "-" + byte2hex[buffer[8] & 0x3f | 0x80] + byte2hex[buffer[9]] + "-" + byte2hex[buffer[10]] + byte2hex[buffer[11]] + byte2hex[buffer[12]] + byte2hex[buffer[13]] + byte2hex[buffer[14]] + byte2hex[buffer[15]];
|
211 | break;
|
212 | }
|
213 |
|
214 | return result;
|
215 | }
|
216 |
|
217 | function stringify(buffer) {
|
218 | return byte2hex[buffer[0]] + byte2hex[buffer[1]] + byte2hex[buffer[2]] + byte2hex[buffer[3]] + "-" + byte2hex[buffer[4]] + byte2hex[buffer[5]] + "-" + byte2hex[buffer[6]] + byte2hex[buffer[7]] + "-" + byte2hex[buffer[8]] + byte2hex[buffer[9]] + "-" + byte2hex[buffer[10]] + byte2hex[buffer[11]] + byte2hex[buffer[12]] + byte2hex[buffer[13]] + byte2hex[buffer[14]] + byte2hex[buffer[15]];
|
219 | }
|
220 |
|
221 |
|
222 | const nil = new UUID("00000000-0000-0000-0000-000000000000");
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | exports.nil = nil;
|
228 |
|
229 |
|
\ | No newline at end of file |