Skip to content

Commit a304998

Browse files
committed
debt - modernize uuid and remove unused stuff
1 parent 076c919 commit a304998

3 files changed

Lines changed: 53 additions & 103 deletions

File tree

src/vs/base/common/uuid.ts

Lines changed: 45 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,106 +3,59 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
/**
7-
* Represents a UUID as defined by rfc4122.
8-
*/
9-
export interface UUID {
10-
11-
/**
12-
* @returns the canonical representation in sets of hexadecimal numbers separated by dashes.
13-
*/
14-
asHex(): string;
15-
}
16-
17-
class ValueUUID implements UUID {
18-
19-
constructor(public _value: string) {
20-
// empty
21-
}
22-
23-
public asHex(): string {
24-
return this._value;
25-
}
26-
}
27-
28-
class V4UUID extends ValueUUID {
29-
30-
private static readonly _chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
31-
32-
private static readonly _timeHighBits = ['8', '9', 'a', 'b'];
33-
34-
private static _oneOf(array: string[]): string {
35-
return array[Math.floor(array.length * Math.random())];
36-
}
37-
38-
private static _randomHex(): string {
39-
return V4UUID._oneOf(V4UUID._chars);
40-
}
41-
42-
constructor() {
43-
super([
44-
V4UUID._randomHex(),
45-
V4UUID._randomHex(),
46-
V4UUID._randomHex(),
47-
V4UUID._randomHex(),
48-
V4UUID._randomHex(),
49-
V4UUID._randomHex(),
50-
V4UUID._randomHex(),
51-
V4UUID._randomHex(),
52-
'-',
53-
V4UUID._randomHex(),
54-
V4UUID._randomHex(),
55-
V4UUID._randomHex(),
56-
V4UUID._randomHex(),
57-
'-',
58-
'4',
59-
V4UUID._randomHex(),
60-
V4UUID._randomHex(),
61-
V4UUID._randomHex(),
62-
'-',
63-
V4UUID._oneOf(V4UUID._timeHighBits),
64-
V4UUID._randomHex(),
65-
V4UUID._randomHex(),
66-
V4UUID._randomHex(),
67-
'-',
68-
V4UUID._randomHex(),
69-
V4UUID._randomHex(),
70-
V4UUID._randomHex(),
71-
V4UUID._randomHex(),
72-
V4UUID._randomHex(),
73-
V4UUID._randomHex(),
74-
V4UUID._randomHex(),
75-
V4UUID._randomHex(),
76-
V4UUID._randomHex(),
77-
V4UUID._randomHex(),
78-
V4UUID._randomHex(),
79-
V4UUID._randomHex(),
80-
].join(''));
81-
}
82-
}
83-
84-
export function v4(): UUID {
85-
return new V4UUID();
86-
}
876

887
const _UUIDPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
898

909
export function isUUID(value: string): boolean {
9110
return _UUIDPattern.test(value);
9211
}
9312

94-
/**
95-
* Parses a UUID that is of the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
96-
* @param value A uuid string.
97-
*/
98-
export function parse(value: string): UUID {
99-
if (!isUUID(value)) {
100-
throw new Error('invalid uuid');
101-
}
102-
103-
return new ValueUUID(value);
13+
// prep-work
14+
const _data = new Uint8Array(16);
15+
const _hex: string[] = [];
16+
for (let i = 0; i < 256; i++) {
17+
_hex.push(i.toString(16).padStart(2, '0'));
10418
}
10519

20+
const _fillRandomValues = typeof crypto === 'object'
21+
? crypto.getRandomValues.bind(crypto)
22+
: function (bucket: Uint8Array): Uint8Array { // todo@jrieken node nodejs use `crypto#randomBytes`, see: https://nodejs.org/docs/latest/api/crypto.html#crypto_crypto_randombytes_size_callback
23+
for (let i = 0; i < bucket.length; i++) {
24+
bucket[i] = Math.floor(Math.random() * 256);
25+
}
26+
return bucket;
27+
};
28+
10629
export function generateUuid(): string {
107-
return v4().asHex();
30+
// get data
31+
_fillRandomValues(_data);
32+
33+
// set version bits
34+
_data[6] = (_data[6] & 0x0f) | 0x40;
35+
_data[8] = (_data[8] & 0x3f) | 0x80;
36+
37+
// print as string
38+
let i = 0;
39+
let result = '';
40+
result += _hex[_data[i++]];
41+
result += _hex[_data[i++]];
42+
result += _hex[_data[i++]];
43+
result += _hex[_data[i++]];
44+
result += '-';
45+
result += _hex[_data[i++]];
46+
result += _hex[_data[i++]];
47+
result += '-';
48+
result += _hex[_data[i++]];
49+
result += _hex[_data[i++]];
50+
result += '-';
51+
result += _hex[_data[i++]];
52+
result += _hex[_data[i++]];
53+
result += '-';
54+
result += _hex[_data[i++]];
55+
result += _hex[_data[i++]];
56+
result += _hex[_data[i++]];
57+
result += _hex[_data[i++]];
58+
result += _hex[_data[i++]];
59+
result += _hex[_data[i++]];
60+
return result;
10861
}

src/vs/base/test/common/uuid.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import * as uuid from 'vs/base/common/uuid';
77

88
suite('UUID', () => {
99
test('generation', () => {
10-
const asHex = uuid.v4().asHex();
10+
const asHex = uuid.generateUuid();
1111
assert.equal(asHex.length, 36);
1212
assert.equal(asHex[14], '4');
1313
assert.ok(asHex[19] === '8' || asHex[19] === '9' || asHex[19] === 'a' || asHex[19] === 'b');
1414
});
1515

16-
test('parse', () => {
17-
const id = uuid.v4();
18-
const asHext = id.asHex();
19-
const id2 = uuid.parse(asHext);
20-
assert.equal(id.asHex(), id2.asHex());
16+
test('self-check', function () {
17+
const t1 = Date.now();
18+
while (Date.now() - t1 < 50) {
19+
const value = uuid.generateUuid();
20+
assert.ok(uuid.isUUID(value));
21+
}
2122
});
2223
});

src/vs/workbench/contrib/tasks/test/common/configuration.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,11 +514,7 @@ function assertProblemMatcher(actual: string | ProblemMatcher, expected: string
514514
}
515515
if (typeof actual !== 'string' && typeof expected !== 'string') {
516516
if (expected.owner === ProblemMatcherBuilder.DEFAULT_UUID) {
517-
try {
518-
UUID.parse(actual.owner);
519-
} catch (err) {
520-
assert.fail(actual.owner, 'Owner must be a UUID');
521-
}
517+
assert.ok(UUID.isUUID(actual.owner), 'Owner must be a UUID');
522518
} else {
523519
assert.strictEqual(actual.owner, expected.owner);
524520
}

0 commit comments

Comments
 (0)