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