@woosh/meep-engine 2.123.0 → 2.123.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/core/binary/base58/base58_encode.d.ts +10 -0
- package/src/core/binary/base58/base58_encode.d.ts.map +1 -0
- package/src/core/binary/base58/base58_encode.js +87 -0
- package/src/engine/ecs/guid/UUID.d.ts +19 -7
- package/src/engine/ecs/guid/UUID.d.ts.map +1 -1
- package/src/engine/ecs/guid/UUID.js +26 -7
package/package.json
CHANGED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Using bitcoin-inspired base58 encoding
|
|
3
|
+
* Primarily intended for UUID compression
|
|
4
|
+
* @param {Uint8Array} source
|
|
5
|
+
* @param {number} offset
|
|
6
|
+
* @param {number} byte_count
|
|
7
|
+
* @returns {string}
|
|
8
|
+
*/
|
|
9
|
+
export function base58_encode(source: Uint8Array, offset: number, byte_count: number): string;
|
|
10
|
+
//# sourceMappingURL=base58_encode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base58_encode.d.ts","sourceRoot":"","sources":["../../../../../src/core/binary/base58/base58_encode.js"],"names":[],"mappings":"AAMA;;;;;;;GAOG;AACH,sCALW,UAAU,UACV,MAAM,cACN,MAAM,GACJ,MAAM,CA0ElB"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { assert } from "../../assert.js";
|
|
2
|
+
|
|
3
|
+
const base58_alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
4
|
+
|
|
5
|
+
const scratch = new Uint8Array(256);
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Using bitcoin-inspired base58 encoding
|
|
9
|
+
* Primarily intended for UUID compression
|
|
10
|
+
* @param {Uint8Array} source
|
|
11
|
+
* @param {number} offset
|
|
12
|
+
* @param {number} byte_count
|
|
13
|
+
* @returns {string}
|
|
14
|
+
*/
|
|
15
|
+
export function base58_encode(
|
|
16
|
+
source,
|
|
17
|
+
offset,
|
|
18
|
+
byte_count
|
|
19
|
+
) {
|
|
20
|
+
|
|
21
|
+
// ported from the bitcoin's C codebase: https://github.com/bitcoin/bitcoin/blob/08a7316c144f9f2516db8fa62400893f4358c5ae/src/base58.cpp#L71
|
|
22
|
+
|
|
23
|
+
if (byte_count === 0) {
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Skip & count leading zeroes.
|
|
28
|
+
let zeroes = 0
|
|
29
|
+
let length = 0
|
|
30
|
+
let pbegin = offset
|
|
31
|
+
const pend = offset + byte_count
|
|
32
|
+
|
|
33
|
+
while (pbegin !== pend && source[pbegin] === 0) {
|
|
34
|
+
pbegin++
|
|
35
|
+
zeroes++
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Allocate enough space in big-endian base58 representation.
|
|
39
|
+
const size = ((pend - pbegin) * 138 / 100 + 1) >>> 0; /// log(256) / log(58), rounded up.
|
|
40
|
+
|
|
41
|
+
// use scratch if possible to avoid allocation
|
|
42
|
+
const b58 = (size <= scratch.length) ? scratch : new Uint8Array(size);
|
|
43
|
+
|
|
44
|
+
// Process the bytes.
|
|
45
|
+
while (pbegin !== pend) {
|
|
46
|
+
let carry = source[pbegin]
|
|
47
|
+
|
|
48
|
+
// Apply "b58 = b58 * 256 + ch".
|
|
49
|
+
let i = 0
|
|
50
|
+
|
|
51
|
+
for (
|
|
52
|
+
let it1 = size - 1;
|
|
53
|
+
(carry !== 0 || i < length) && (it1 !== -1);
|
|
54
|
+
it1--, i++
|
|
55
|
+
) {
|
|
56
|
+
|
|
57
|
+
carry += (256 * b58[it1]) >>> 0;
|
|
58
|
+
b58[it1] = (carry % 58) >>> 0;
|
|
59
|
+
carry = (carry / 58) >>> 0;
|
|
60
|
+
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
assert.equal(carry, 0, 'carry must be zero');
|
|
64
|
+
|
|
65
|
+
length = i;
|
|
66
|
+
pbegin++;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Skip leading zeroes in base58 result.
|
|
70
|
+
let it2 = size - length
|
|
71
|
+
while (it2 !== size && b58[it2] === 0) {
|
|
72
|
+
it2++
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Translate the result into a string.
|
|
76
|
+
let str = base58_alphabet.charAt(0).repeat(zeroes)
|
|
77
|
+
for (; it2 < size; ++it2) {
|
|
78
|
+
str += base58_alphabet.charAt(b58[it2])
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (b58 === scratch) {
|
|
82
|
+
// clear out scratch
|
|
83
|
+
scratch.fill(0);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return str;
|
|
87
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Universally Unique Identifier
|
|
3
|
+
* The default is Nil UUID, where all bits are set to 0
|
|
3
4
|
* @see IETF RFC 4122
|
|
4
|
-
*
|
|
5
5
|
*/
|
|
6
6
|
export class UUID {
|
|
7
7
|
/**
|
|
@@ -10,16 +10,22 @@ export class UUID {
|
|
|
10
10
|
*/
|
|
11
11
|
static v1(): UUID;
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* Variant 4 UUID generator (fully random)
|
|
14
14
|
* @returns {UUID}
|
|
15
15
|
*/
|
|
16
16
|
static v4(): UUID;
|
|
17
17
|
/**
|
|
18
18
|
* Shortcut to generate string-form ID
|
|
19
|
-
* Uses v4 UUID
|
|
19
|
+
* Uses v4(random) UUID
|
|
20
20
|
* @return {string}
|
|
21
21
|
*/
|
|
22
22
|
static string(): string;
|
|
23
|
+
/**
|
|
24
|
+
* Shortcut to generate 22-character base58 string
|
|
25
|
+
* Uses v4(random) UUID
|
|
26
|
+
* @return {string}
|
|
27
|
+
*/
|
|
28
|
+
static base58(): string;
|
|
23
29
|
/**
|
|
24
30
|
*
|
|
25
31
|
* @param {string} string
|
|
@@ -46,13 +52,19 @@ export class UUID {
|
|
|
46
52
|
*/
|
|
47
53
|
v4(): void;
|
|
48
54
|
/***
|
|
49
|
-
*
|
|
50
|
-
* @
|
|
51
|
-
* @param {string} string
|
|
55
|
+
* Parses standard UUID string of form AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE
|
|
56
|
+
* @param {string} string String in UUID format
|
|
52
57
|
*/
|
|
53
58
|
parse(string: string): void;
|
|
54
59
|
/**
|
|
55
|
-
*
|
|
60
|
+
* Use base58 encoding to represent UUID as a 22 character string
|
|
61
|
+
*/
|
|
62
|
+
get base58(): string;
|
|
63
|
+
/**
|
|
64
|
+
* Standard UUID string in from: AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE
|
|
65
|
+
* Result is compatible with the {@link UUID.parse} method.
|
|
66
|
+
* @example
|
|
67
|
+
* a88bb73a-c89f-11ed-afa1-0242ac120002
|
|
56
68
|
* @returns {string}
|
|
57
69
|
*/
|
|
58
70
|
toString(): string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"UUID.d.ts","sourceRoot":"","sources":["../../../../../src/engine/ecs/guid/UUID.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"UUID.d.ts","sourceRoot":"","sources":["../../../../../src/engine/ecs/guid/UUID.js"],"names":[],"mappings":"AAyBA;;;;GAIG;AACH;IAiHI;;;OAGG;IACH,aAFY,IAAI,CAQf;IAED;;;OAGG;IACH,aAFa,IAAI,CAQhB;IAED;;;;OAIG;IACH,iBAFY,MAAM,CAIjB;IAED;;;;OAIG;IACH,iBAFY,MAAM,CAIjB;IAyCD;;;;OAIG;IACH,qBAHW,MAAM,GACL,IAAI,CAQf;IA1MD;;;OAGG;IACH,gBAFW,MAAM,EAAE,GAAC,UAAU,GAAC,SAAS,CAAC,MAAM,CAAC,EAO/C;IAED;;;OAGG;IACH,YAFa,UAAU,CAItB;IAED;;;OAGG;IACH,WAsEC;IAED;;OAEG;IACH,WAUC;IA4CD;;;OAGG;IACH,cAFW,MAAM,QAmChB;IAeD;;OAEG;IACH,qBAEC;IAED;;;;;;OAMG;IACH,YAFa,MAAM,CA2BlB;IAED;;;OAGG;IACH,YAFW,IAAI,QAId;IAED;;;;OAIG;IACH,cAHW,IAAI,GACF,OAAO,CAanB;IAED;;OAEG;IACH,QAFa,MAAM,CAWlB;IAED,iBAEC;IAED,uBAEC;IASL;;;OAGG;IACH,iBAFU,OAAO,CAEI;;CAZpB;;kBAIS,MAAM"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { assert } from "../../../core/assert.js";
|
|
2
|
+
import { base58_encode } from "../../../core/binary/base58/base58_encode.js";
|
|
2
3
|
import { dec2hex } from "../../../core/binary/dec2hex.js";
|
|
3
4
|
import { array_copy } from "../../../core/collection/array/array_copy.js";
|
|
4
5
|
import { randomBytes } from "../../../core/math/random/randomBytes.js";
|
|
@@ -24,8 +25,8 @@ let _clockseq = ((randomUint8(random) << 8) | randomUint8(random)) & 0x3fff;
|
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
27
|
* Universally Unique Identifier
|
|
28
|
+
* The default is Nil UUID, where all bits are set to 0
|
|
27
29
|
* @see IETF RFC 4122
|
|
28
|
-
*
|
|
29
30
|
*/
|
|
30
31
|
export class UUID {
|
|
31
32
|
#data = new Uint8Array(16);
|
|
@@ -153,7 +154,7 @@ export class UUID {
|
|
|
153
154
|
}
|
|
154
155
|
|
|
155
156
|
/**
|
|
156
|
-
*
|
|
157
|
+
* Variant 4 UUID generator (fully random)
|
|
157
158
|
* @returns {UUID}
|
|
158
159
|
*/
|
|
159
160
|
static v4() {
|
|
@@ -166,17 +167,25 @@ export class UUID {
|
|
|
166
167
|
|
|
167
168
|
/**
|
|
168
169
|
* Shortcut to generate string-form ID
|
|
169
|
-
* Uses v4 UUID
|
|
170
|
+
* Uses v4(random) UUID
|
|
170
171
|
* @return {string}
|
|
171
172
|
*/
|
|
172
173
|
static string() {
|
|
173
174
|
return UUID.v4().toString();
|
|
174
175
|
}
|
|
175
176
|
|
|
177
|
+
/**
|
|
178
|
+
* Shortcut to generate 22-character base58 string
|
|
179
|
+
* Uses v4(random) UUID
|
|
180
|
+
* @return {string}
|
|
181
|
+
*/
|
|
182
|
+
static base58() {
|
|
183
|
+
return UUID.v4().base58;
|
|
184
|
+
}
|
|
185
|
+
|
|
176
186
|
/***
|
|
177
|
-
*
|
|
178
|
-
* @
|
|
179
|
-
* @param {string} string
|
|
187
|
+
* Parses standard UUID string of form AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE
|
|
188
|
+
* @param {string} string String in UUID format
|
|
180
189
|
*/
|
|
181
190
|
parse(string) {
|
|
182
191
|
assert.isString(string, 'string');
|
|
@@ -227,7 +236,17 @@ export class UUID {
|
|
|
227
236
|
}
|
|
228
237
|
|
|
229
238
|
/**
|
|
230
|
-
*
|
|
239
|
+
* Use base58 encoding to represent UUID as a 22 character string
|
|
240
|
+
*/
|
|
241
|
+
get base58() {
|
|
242
|
+
return base58_encode(this.#data, 0, 16);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Standard UUID string in from: AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE
|
|
247
|
+
* Result is compatible with the {@link UUID.parse} method.
|
|
248
|
+
* @example
|
|
249
|
+
* a88bb73a-c89f-11ed-afa1-0242ac120002
|
|
231
250
|
* @returns {string}
|
|
232
251
|
*/
|
|
233
252
|
toString() {
|