typia 3.6.6 → 3.7.0-dev.20230310
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/README.md +44 -15
- package/lib/factories/MetadataCollection.d.ts +1 -9
- package/lib/factories/MetadataCollection.js +2 -75
- package/lib/factories/MetadataCollection.js.map +1 -1
- package/lib/factories/ProtocolFactory.d.ts +8 -0
- package/lib/factories/ProtocolFactory.js +112 -0
- package/lib/factories/ProtocolFactory.js.map +1 -0
- package/lib/factories/internal/protocols/ProtocolMetadataUtil.d.ts +16 -0
- package/lib/factories/internal/protocols/ProtocolMetadataUtil.js +156 -0
- package/lib/factories/internal/protocols/ProtocolMetadataUtil.js.map +1 -0
- package/lib/factories/internal/protocols/emplace_protocol_object.d.ts +3 -0
- package/lib/factories/internal/protocols/emplace_protocol_object.js +47 -0
- package/lib/factories/internal/protocols/emplace_protocol_object.js.map +1 -0
- package/lib/factories/internal/protocols/emplace_protocol_property.d.ts +4 -0
- package/lib/factories/internal/protocols/emplace_protocol_property.js +20 -0
- package/lib/factories/internal/protocols/emplace_protocol_property.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_atomic.d.ts +3 -0
- package/lib/factories/internal/protocols/iterate_protocol_atomic.js +69 -0
- package/lib/factories/internal/protocols/iterate_protocol_atomic.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_constant.d.ts +2 -0
- package/lib/factories/internal/protocols/iterate_protocol_constant.js +30 -0
- package/lib/factories/internal/protocols/iterate_protocol_constant.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_main.d.ts +3 -0
- package/lib/factories/internal/protocols/iterate_protocol_main.js +17 -0
- package/lib/factories/internal/protocols/iterate_protocol_main.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_map.d.ts +4 -0
- package/lib/factories/internal/protocols/iterate_protocol_map.js +75 -0
- package/lib/factories/internal/protocols/iterate_protocol_map.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_metadata.d.ts +5 -0
- package/lib/factories/internal/protocols/iterate_protocol_metadata.js +190 -0
- package/lib/factories/internal/protocols/iterate_protocol_metadata.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_native.d.ts +2 -0
- package/lib/factories/internal/protocols/iterate_protocol_native.js +33 -0
- package/lib/factories/internal/protocols/iterate_protocol_native.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_never.d.ts +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_never.js +6 -0
- package/lib/factories/internal/protocols/iterate_protocol_never.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_object.d.ts +4 -0
- package/lib/factories/internal/protocols/iterate_protocol_object.js +157 -0
- package/lib/factories/internal/protocols/iterate_protocol_object.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_repeated.d.ts +5 -0
- package/lib/factories/internal/protocols/iterate_protocol_repeated.js +25 -0
- package/lib/factories/internal/protocols/iterate_protocol_repeated.js.map +1 -0
- package/lib/factories/internal/protocols/iterate_protocol_tuple.d.ts +3 -0
- package/lib/factories/internal/protocols/iterate_protocol_tuple.js +46 -0
- package/lib/factories/internal/protocols/iterate_protocol_tuple.js.map +1 -0
- package/lib/functional/$proto_bytes.d.ts +2 -0
- package/lib/functional/$proto_bytes.js +37 -0
- package/lib/functional/$proto_bytes.js.map +1 -0
- package/lib/functional/$proto_field.d.ts +10 -0
- package/lib/functional/$proto_field.js +42 -0
- package/lib/functional/$proto_field.js.map +1 -0
- package/lib/functional/$proto_float.d.ts +4 -0
- package/lib/functional/$proto_float.js +28 -0
- package/lib/functional/$proto_float.js.map +1 -0
- package/lib/functional/$proto_i32.d.ts +2 -0
- package/lib/functional/$proto_i32.js +23 -0
- package/lib/functional/$proto_i32.js.map +1 -0
- package/lib/functional/$proto_i64.d.ts +2 -0
- package/lib/functional/$proto_i64.js +31 -0
- package/lib/functional/$proto_i64.js.map +1 -0
- package/lib/functional/$proto_size.d.ts +6 -0
- package/lib/functional/$proto_size.js +76 -0
- package/lib/functional/$proto_size.js.map +1 -0
- package/lib/functional/$proto_string.d.ts +2 -0
- package/lib/functional/$proto_string.js +34 -0
- package/lib/functional/$proto_string.js.map +1 -0
- package/lib/functional/$varint.d.ts +6 -0
- package/lib/functional/$varint.js +99 -0
- package/lib/functional/$varint.js.map +1 -0
- package/lib/functional/$zigzag.d.ts +4 -0
- package/lib/functional/$zigzag.js +34 -0
- package/lib/functional/$zigzag.js.map +1 -0
- package/lib/messages/IProtocolMap.d.ts +5 -0
- package/lib/messages/IProtocolMap.js +3 -0
- package/lib/messages/IProtocolMap.js.map +1 -0
- package/lib/messages/IProtocolMessage.d.ts +5 -0
- package/lib/messages/IProtocolMessage.js +3 -0
- package/lib/messages/IProtocolMessage.js.map +1 -0
- package/lib/messages/IProtocolProperty.d.ts +12 -0
- package/lib/messages/IProtocolProperty.js +3 -0
- package/lib/messages/IProtocolProperty.js.map +1 -0
- package/lib/metadata/IMetadataTag.d.ts +7 -3
- package/lib/metadata/Metadata.js +2 -2
- package/lib/metadata/Metadata.js.map +1 -1
- package/lib/module.d.ts +2 -0
- package/lib/module.js +5 -1
- package/lib/module.js.map +1 -1
- package/lib/programmers/MessageProgrammer.d.ts +5 -0
- package/lib/programmers/MessageProgrammer.js +141 -0
- package/lib/programmers/MessageProgrammer.js.map +1 -0
- package/lib/programmers/internal/application_number.js +20 -14
- package/lib/programmers/internal/application_number.js.map +1 -1
- package/lib/programmers/internal/application_object.js +11 -11
- package/lib/programmers/internal/application_object.js.map +1 -1
- package/lib/programmers/internal/application_schema.js +5 -3
- package/lib/programmers/internal/application_schema.js.map +1 -1
- package/lib/schemas/IJsonSchema.d.ts +4 -10
- package/lib/transformers/CallExpressionTransformer.js +3 -1
- package/lib/transformers/CallExpressionTransformer.js.map +1 -1
- package/lib/transformers/features/miscellaneous/ApplicationTransformer.js +1 -3
- package/lib/transformers/features/miscellaneous/ApplicationTransformer.js.map +1 -1
- package/lib/transformers/features/protocols/MessageTransformer.d.ts +5 -0
- package/lib/transformers/features/protocols/MessageTransformer.js +17 -0
- package/lib/transformers/features/protocols/MessageTransformer.js.map +1 -0
- package/lib/utils/NameEncoder.d.ts +4 -0
- package/lib/utils/NameEncoder.js +89 -0
- package/lib/utils/NameEncoder.js.map +1 -0
- package/package.json +3 -2
- package/src/factories/MetadataCollection.ts +2 -41
- package/src/factories/ProtocolFactory.ts +79 -0
- package/src/factories/internal/protocols/ProtocolMetadataUtil.ts +163 -0
- package/src/factories/internal/protocols/emplace_protocol_object.ts +25 -0
- package/src/factories/internal/protocols/emplace_protocol_property.ts +12 -0
- package/src/factories/internal/protocols/iterate_protocol_atomic.ts +34 -0
- package/src/factories/internal/protocols/iterate_protocol_constant.ts +27 -0
- package/src/factories/internal/protocols/iterate_protocol_main.ts +19 -0
- package/src/factories/internal/protocols/iterate_protocol_map.ts +58 -0
- package/src/factories/internal/protocols/iterate_protocol_metadata.ts +96 -0
- package/src/factories/internal/protocols/iterate_protocol_native.ts +34 -0
- package/src/factories/internal/protocols/iterate_protocol_never.ts +1 -0
- package/src/factories/internal/protocols/iterate_protocol_object.ts +110 -0
- package/src/factories/internal/protocols/iterate_protocol_repeated.ts +34 -0
- package/src/factories/internal/protocols/iterate_protocol_tuple.ts +29 -0
- package/src/functional/$proto_bytes.ts +25 -0
- package/src/functional/$proto_field.ts +30 -0
- package/src/functional/$proto_float.ts +37 -0
- package/src/functional/$proto_i32.ts +29 -0
- package/src/functional/$proto_i64.ts +37 -0
- package/src/functional/$proto_size.ts +82 -0
- package/src/functional/$proto_string.ts +24 -0
- package/src/functional/$varint.ts +130 -0
- package/src/functional/$zigzag.ts +39 -0
- package/src/messages/IProtocolMap.ts +5 -0
- package/src/messages/IProtocolMessage.ts +6 -0
- package/src/messages/IProtocolProperty.ts +13 -0
- package/src/metadata/IMetadataTag.ts +20 -4
- package/src/metadata/Metadata.ts +6 -3
- package/src/module.ts +11 -0
- package/src/programmers/MessageProgrammer.ts +126 -0
- package/src/programmers/internal/application_number.ts +19 -16
- package/src/programmers/internal/application_object.ts +5 -5
- package/src/programmers/internal/application_schema.ts +7 -3
- package/src/schemas/IJsonSchema.ts +4 -10
- package/src/transformers/CallExpressionTransformer.ts +16 -9
- package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +1 -3
- package/src/transformers/features/protocols/MessageTransformer.ts +32 -0
- package/src/utils/NameEncoder.ts +32 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { $varint_decode_i32, $varint_encode } from "./$varint";
|
|
2
|
+
|
|
3
|
+
export enum $proto_field_wiretype {
|
|
4
|
+
VARINT = 0,
|
|
5
|
+
I64 = 1,
|
|
6
|
+
LEN = 2,
|
|
7
|
+
SGROUP = 3, // deprecated
|
|
8
|
+
EGROUP = 4, // deprecated
|
|
9
|
+
I32 = 5,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function $proto_field_encode(
|
|
13
|
+
dst: Uint8Array,
|
|
14
|
+
offset: number,
|
|
15
|
+
fieldNumber: number,
|
|
16
|
+
wireType: $proto_field_wiretype,
|
|
17
|
+
): number {
|
|
18
|
+
const tag = (fieldNumber << 3) | Number(wireType);
|
|
19
|
+
return $varint_encode(dst, offset, tag);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function $proto_field_decode(
|
|
23
|
+
buf: Uint8Array,
|
|
24
|
+
offset: number,
|
|
25
|
+
): [fieldNumber: number, wireType: $proto_field_wiretype, offset: number] {
|
|
26
|
+
const [tag, o] = $varint_decode_i32(buf, offset);
|
|
27
|
+
const fieldNumber = tag >>> 3;
|
|
28
|
+
const wireType = tag & 0b111;
|
|
29
|
+
return [fieldNumber, wireType, o];
|
|
30
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export function $proto_float32_encode(
|
|
2
|
+
dst: Uint8Array,
|
|
3
|
+
offset: number,
|
|
4
|
+
value: number,
|
|
5
|
+
): number {
|
|
6
|
+
new DataView(dst).setFloat32(offset, value, true);
|
|
7
|
+
offset += 4;
|
|
8
|
+
return offset;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function $proto_float32_decode(
|
|
12
|
+
src: Uint8Array,
|
|
13
|
+
offset: number,
|
|
14
|
+
): [value: number, offset: number] {
|
|
15
|
+
const value = new DataView(src).getFloat32(offset, true);
|
|
16
|
+
offset += 4;
|
|
17
|
+
return [value, offset];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function $proto_float64_encode(
|
|
21
|
+
dst: Uint8Array,
|
|
22
|
+
offset: number,
|
|
23
|
+
value: number,
|
|
24
|
+
): number {
|
|
25
|
+
new DataView(dst).setFloat64(offset, value, true);
|
|
26
|
+
offset += 8;
|
|
27
|
+
return offset;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function $proto_float64_decode(
|
|
31
|
+
src: Uint8Array,
|
|
32
|
+
offset: number,
|
|
33
|
+
): [value: number, offset: number] {
|
|
34
|
+
const value = new DataView(src).getFloat64(offset, true);
|
|
35
|
+
offset += 8;
|
|
36
|
+
return [value, offset];
|
|
37
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function $proto_i32_encode(
|
|
2
|
+
dst: Uint8Array,
|
|
3
|
+
offset: number,
|
|
4
|
+
value: number,
|
|
5
|
+
): number {
|
|
6
|
+
value = (value | 0) >>> 0;
|
|
7
|
+
|
|
8
|
+
dst[offset] = value & 0xff;
|
|
9
|
+
dst[offset + 1] = (value >>> 8) & 0xff;
|
|
10
|
+
dst[offset + 2] = (value >>> 16) & 0xff;
|
|
11
|
+
dst[offset + 3] = (value >>> 24) & 0xff;
|
|
12
|
+
offset += 4;
|
|
13
|
+
|
|
14
|
+
return offset;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function $proto_i32_decode(
|
|
18
|
+
buf: Uint8Array,
|
|
19
|
+
offset: number,
|
|
20
|
+
): [value: number, offset: number] {
|
|
21
|
+
const value =
|
|
22
|
+
(buf[offset]! << 0) |
|
|
23
|
+
(buf[offset + 1]! << 8) |
|
|
24
|
+
(buf[offset + 2]! << 16) |
|
|
25
|
+
(buf[offset + 3]! << 24);
|
|
26
|
+
offset += 4;
|
|
27
|
+
|
|
28
|
+
return [value, offset];
|
|
29
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export function $proto_i64_encode(
|
|
2
|
+
dst: Uint8Array,
|
|
3
|
+
offset: number,
|
|
4
|
+
value: bigint,
|
|
5
|
+
): number {
|
|
6
|
+
value = BigInt.asUintN(64, value);
|
|
7
|
+
|
|
8
|
+
dst[offset] = Number(value & BigInt(0xff));
|
|
9
|
+
dst[offset + 1] = Number((value >> BigInt(8)) & BigInt(0xff));
|
|
10
|
+
dst[offset + 2] = Number((value >> BigInt(16)) & BigInt(0xff));
|
|
11
|
+
dst[offset + 3] = Number((value >> BigInt(24)) & BigInt(0xff));
|
|
12
|
+
dst[offset + 4] = Number((value >> BigInt(32)) & BigInt(0xff));
|
|
13
|
+
dst[offset + 5] = Number((value >> BigInt(40)) & BigInt(0xff));
|
|
14
|
+
dst[offset + 6] = Number((value >> BigInt(48)) & BigInt(0xff));
|
|
15
|
+
dst[offset + 7] = Number((value >> BigInt(56)) & BigInt(0xff));
|
|
16
|
+
offset += 8;
|
|
17
|
+
|
|
18
|
+
return offset;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function $proto_i64_decode(
|
|
22
|
+
buf: Uint8Array,
|
|
23
|
+
offset: number,
|
|
24
|
+
): [value: bigint, offset: number] {
|
|
25
|
+
const value =
|
|
26
|
+
(BigInt(buf[offset]!) << BigInt(0)) |
|
|
27
|
+
(BigInt(buf[offset + 1]!) << BigInt(8)) |
|
|
28
|
+
(BigInt(buf[offset + 2]!) << BigInt(16)) |
|
|
29
|
+
(BigInt(buf[offset + 3]!) << BigInt(24)) |
|
|
30
|
+
(BigInt(buf[offset + 4]!) << BigInt(32)) |
|
|
31
|
+
(BigInt(buf[offset + 5]!) << BigInt(40)) |
|
|
32
|
+
(BigInt(buf[offset + 6]!) << BigInt(48)) |
|
|
33
|
+
(BigInt(buf[offset + 7]!) << BigInt(56));
|
|
34
|
+
offset += 8;
|
|
35
|
+
|
|
36
|
+
return [BigInt.asIntN(64, value), offset];
|
|
37
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { $proto_field_wiretype } from "./$proto_field";
|
|
2
|
+
|
|
3
|
+
export function $proto_size_varint(n: number): number;
|
|
4
|
+
export function $proto_size_varint(n: bigint): number;
|
|
5
|
+
|
|
6
|
+
export function $proto_size_varint(n: number | bigint): number {
|
|
7
|
+
if (typeof n === "number") {
|
|
8
|
+
if (n < 0) {
|
|
9
|
+
return SizeVarInt64(BigInt(n));
|
|
10
|
+
}
|
|
11
|
+
return SizeVarInt32(n);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return SizeVarInt64(n);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function $proto_size_field(
|
|
18
|
+
fieldNumber: number,
|
|
19
|
+
wireType: $proto_field_wiretype,
|
|
20
|
+
): number {
|
|
21
|
+
const tag = (fieldNumber << 3) | Number(wireType);
|
|
22
|
+
return $proto_size_varint(tag);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function $proto_size_bytes(len: number): number {
|
|
26
|
+
return $proto_size_varint(len) + len;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const utf8_encoder = new TextEncoder();
|
|
30
|
+
|
|
31
|
+
export function $proto_size_string(value: string): number {
|
|
32
|
+
// TODO: optimize (DON'T ENCODE TEXT TWICE)
|
|
33
|
+
const bytes = utf8_encoder.encode(value);
|
|
34
|
+
return $proto_size_bytes(bytes.length);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function SizeVarInt32(value: number): number {
|
|
38
|
+
value = (value | 0) >>> 0; // 32-bit integer
|
|
39
|
+
// TODO: optimize ( branchless solution exists )
|
|
40
|
+
// TODO: try lookup table based solution
|
|
41
|
+
|
|
42
|
+
if (value <= 127) {
|
|
43
|
+
return 1;
|
|
44
|
+
} else if (value <= 16383) {
|
|
45
|
+
return 2;
|
|
46
|
+
} else if (value <= 2097151) {
|
|
47
|
+
return 3;
|
|
48
|
+
} else if (value <= 268435455) {
|
|
49
|
+
return 4;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// < 34359738367
|
|
53
|
+
return 5;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function SizeVarInt64(value: bigint): number {
|
|
57
|
+
value = BigInt.asUintN(64, value);
|
|
58
|
+
// TODO: optimize ( branchless solution exists )
|
|
59
|
+
// TODO: try lookup table based solution
|
|
60
|
+
|
|
61
|
+
if (value <= BigInt("127")) {
|
|
62
|
+
return 1;
|
|
63
|
+
} else if (value <= BigInt("16383")) {
|
|
64
|
+
return 2;
|
|
65
|
+
} else if (value <= BigInt("2097151")) {
|
|
66
|
+
return 3;
|
|
67
|
+
} else if (value <= BigInt("268435455")) {
|
|
68
|
+
return 4;
|
|
69
|
+
} else if (value <= BigInt("34359738367")) {
|
|
70
|
+
return 5;
|
|
71
|
+
} else if (value <= BigInt("4398046511103")) {
|
|
72
|
+
return 6;
|
|
73
|
+
} else if (value <= BigInt("562949953421311")) {
|
|
74
|
+
return 7;
|
|
75
|
+
} else if (value <= BigInt("72057594037927935")) {
|
|
76
|
+
return 8;
|
|
77
|
+
} else if (value <= BigInt("9223372036854775807")) {
|
|
78
|
+
return 9;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return 10;
|
|
82
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { $proto_bytes_decode, $proto_bytes_encode } from "./$proto_bytes";
|
|
2
|
+
|
|
3
|
+
const utf8_encoder = new TextEncoder();
|
|
4
|
+
|
|
5
|
+
export function $proto_string_encode(
|
|
6
|
+
dst: Uint8Array,
|
|
7
|
+
offset: number,
|
|
8
|
+
value: string,
|
|
9
|
+
): number {
|
|
10
|
+
// TODO: optimize (DON'T ENCODE TEXT TWICE)
|
|
11
|
+
|
|
12
|
+
const bytes = utf8_encoder.encode(value);
|
|
13
|
+
return $proto_bytes_encode(dst, offset, bytes);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function $proto_string_decode(
|
|
17
|
+
src: Uint8Array,
|
|
18
|
+
offset: number,
|
|
19
|
+
): [value: string, offset: number] {
|
|
20
|
+
const utf8_decoder = new TextDecoder("utf-8");
|
|
21
|
+
const [bytes, o] = $proto_bytes_decode(src, offset);
|
|
22
|
+
const value = utf8_decoder.decode(bytes);
|
|
23
|
+
return [value, o];
|
|
24
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
function EncodeVarNumber(
|
|
2
|
+
dst: Uint8Array,
|
|
3
|
+
offset: number,
|
|
4
|
+
value: number,
|
|
5
|
+
): number {
|
|
6
|
+
value = (value | 0) >>> 0; // 32-bit integer
|
|
7
|
+
|
|
8
|
+
while (value > 127) {
|
|
9
|
+
dst[offset++] = (value & 0b01111111) | 0b10000000;
|
|
10
|
+
value >>>= 7;
|
|
11
|
+
}
|
|
12
|
+
dst[offset++] = value;
|
|
13
|
+
|
|
14
|
+
return offset;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function DecodeVarNumber(
|
|
18
|
+
buf: Uint8Array,
|
|
19
|
+
offset: number,
|
|
20
|
+
): [value: number, offset: number] {
|
|
21
|
+
let value = 0;
|
|
22
|
+
let shift = 0;
|
|
23
|
+
|
|
24
|
+
while (true) {
|
|
25
|
+
const byte = buf[offset++]!;
|
|
26
|
+
value |= (byte & 0b01111111) << shift;
|
|
27
|
+
if (byte < 128) {
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
shift += 7;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return [value | 0, offset];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function DecodeVarBigInt(
|
|
37
|
+
buf: Uint8Array,
|
|
38
|
+
offset: number,
|
|
39
|
+
): [value: bigint, offset: number] {
|
|
40
|
+
let value = BigInt(0);
|
|
41
|
+
let shift = BigInt(0);
|
|
42
|
+
|
|
43
|
+
while (true) {
|
|
44
|
+
const byte = buf[offset++]!;
|
|
45
|
+
value |= BigInt(byte & 0b01111111) << shift;
|
|
46
|
+
if (byte < 128) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
shift += BigInt(7);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return [BigInt.asIntN(64, value), offset];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function $varint_decode_i32(
|
|
56
|
+
buf: Uint8Array,
|
|
57
|
+
offset: number,
|
|
58
|
+
): [value: number, offset: number] {
|
|
59
|
+
const [v, o] = DecodeVarNumber(buf, offset);
|
|
60
|
+
return [v, o];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function $varint_decode_u32(
|
|
64
|
+
buf: Uint8Array,
|
|
65
|
+
offset: number,
|
|
66
|
+
): [value: number, offset: number] {
|
|
67
|
+
const [v, o] = DecodeVarNumber(buf, offset);
|
|
68
|
+
return [v >>> 0, o];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function $varint_decode_i64(
|
|
72
|
+
buf: Uint8Array,
|
|
73
|
+
offset: number,
|
|
74
|
+
): [value: bigint, offset: number] {
|
|
75
|
+
const [v, o] = DecodeVarBigInt(buf, offset);
|
|
76
|
+
return [v, o];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function $varint_decode_u64(
|
|
80
|
+
buf: Uint8Array,
|
|
81
|
+
offset: number,
|
|
82
|
+
): [value: bigint, offset: number] {
|
|
83
|
+
const [v, o] = DecodeVarBigInt(buf, offset);
|
|
84
|
+
return [BigInt.asUintN(64, v), o];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function EncodeVarBigInt(
|
|
88
|
+
dst: Uint8Array,
|
|
89
|
+
offset: number,
|
|
90
|
+
value: bigint,
|
|
91
|
+
): number {
|
|
92
|
+
value = BigInt.asUintN(64, value);
|
|
93
|
+
|
|
94
|
+
while (value > BigInt(127)) {
|
|
95
|
+
dst[offset++] = Number(value & BigInt(0b01111111)) | 0b10000000;
|
|
96
|
+
value >>= BigInt(7);
|
|
97
|
+
}
|
|
98
|
+
dst[offset++] = Number(value);
|
|
99
|
+
|
|
100
|
+
return offset;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function $varint_encode(
|
|
104
|
+
dst: Uint8Array,
|
|
105
|
+
offset: number,
|
|
106
|
+
value: number,
|
|
107
|
+
): number;
|
|
108
|
+
export function $varint_encode(
|
|
109
|
+
dst: Uint8Array,
|
|
110
|
+
offset: number,
|
|
111
|
+
value: bigint,
|
|
112
|
+
): number;
|
|
113
|
+
|
|
114
|
+
export function $varint_encode(
|
|
115
|
+
dst: Uint8Array,
|
|
116
|
+
offset: number,
|
|
117
|
+
value: number | bigint,
|
|
118
|
+
): number {
|
|
119
|
+
if (typeof value === "bigint") {
|
|
120
|
+
offset = EncodeVarBigInt(dst, offset, value);
|
|
121
|
+
} else {
|
|
122
|
+
if (value < 0) {
|
|
123
|
+
// NOTE: Protocol Buffers signed varint encoding uses two's complement of 64-bit unsigned integers.
|
|
124
|
+
offset = EncodeVarBigInt(dst, offset, BigInt(value));
|
|
125
|
+
} else {
|
|
126
|
+
offset = EncodeVarNumber(dst, offset, value);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return offset;
|
|
130
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export function $zigzag_encode(value: number): number;
|
|
2
|
+
export function $zigzag_encode(value: bigint): bigint;
|
|
3
|
+
export function $zigzag_encode(value: number | bigint): any {
|
|
4
|
+
// TODO: optimize (branchless solution exists)
|
|
5
|
+
|
|
6
|
+
if (typeof value === "bigint") {
|
|
7
|
+
if (value < BigInt(0)) {
|
|
8
|
+
value = -value;
|
|
9
|
+
return value * BigInt(2) - BigInt(1);
|
|
10
|
+
}
|
|
11
|
+
return value * BigInt(2);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (value < 0) {
|
|
15
|
+
value = -value;
|
|
16
|
+
return value * 2 - 1;
|
|
17
|
+
}
|
|
18
|
+
return value * 2;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function $zigzag_decode(value: number): number;
|
|
22
|
+
export function $zigzag_decode(value: bigint): bigint;
|
|
23
|
+
export function $zigzag_decode(value: number | bigint): any {
|
|
24
|
+
// TODO: optimize (branchless solution exists)
|
|
25
|
+
|
|
26
|
+
if (typeof value === "bigint") {
|
|
27
|
+
value = BigInt.asUintN(64, value);
|
|
28
|
+
if (value & BigInt(1)) {
|
|
29
|
+
return -(value + BigInt(1)) / BigInt(2);
|
|
30
|
+
}
|
|
31
|
+
return value / BigInt(2);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
value = value >>> 0;
|
|
35
|
+
if (value & 1) {
|
|
36
|
+
return -(value + 1) / 2;
|
|
37
|
+
}
|
|
38
|
+
return value / 2;
|
|
39
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IProtocolMap } from "./IProtocolMap";
|
|
2
|
+
|
|
3
|
+
export interface IProtocolProperty {
|
|
4
|
+
key: string;
|
|
5
|
+
oneOf: IProtocolProperty.IOneOf[];
|
|
6
|
+
required: boolean;
|
|
7
|
+
repeated: boolean;
|
|
8
|
+
}
|
|
9
|
+
export namespace IProtocolProperty {
|
|
10
|
+
export interface IOneOf {
|
|
11
|
+
type: string | IProtocolMap;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -10,13 +10,16 @@ export type IMetadataTag =
|
|
|
10
10
|
| IMetadataTag.IMinLength
|
|
11
11
|
| IMetadataTag.IMaxLength
|
|
12
12
|
// NUMBER
|
|
13
|
-
| IMetadataTag.
|
|
13
|
+
| IMetadataTag.INumberType
|
|
14
|
+
| IMetadataTag.IBigintType
|
|
14
15
|
| IMetadataTag.IMinimum
|
|
15
16
|
| IMetadataTag.IMaximum
|
|
16
17
|
| IMetadataTag.IExclusiveMinimum
|
|
17
18
|
| IMetadataTag.IExclusiveMaximum
|
|
18
19
|
| IMetadataTag.IMultipleOf
|
|
19
|
-
| IMetadataTag.IStep
|
|
20
|
+
| IMetadataTag.IStep
|
|
21
|
+
// BIGINT
|
|
22
|
+
| IMetadataTag.IBigintType;
|
|
20
23
|
|
|
21
24
|
export namespace IMetadataTag {
|
|
22
25
|
/* -----------------------------------------------------------
|
|
@@ -68,9 +71,17 @@ export namespace IMetadataTag {
|
|
|
68
71
|
/* -----------------------------------------------------------
|
|
69
72
|
NUMBER
|
|
70
73
|
----------------------------------------------------------- */
|
|
71
|
-
export interface
|
|
74
|
+
export interface INumberType {
|
|
72
75
|
kind: "type";
|
|
73
|
-
value:
|
|
76
|
+
value:
|
|
77
|
+
| "int"
|
|
78
|
+
| "uint"
|
|
79
|
+
| "int32"
|
|
80
|
+
| "uint32"
|
|
81
|
+
| "int64"
|
|
82
|
+
| "uint64"
|
|
83
|
+
| "float"
|
|
84
|
+
| "double";
|
|
74
85
|
}
|
|
75
86
|
|
|
76
87
|
export interface IMinimum {
|
|
@@ -106,6 +117,11 @@ export namespace IMetadataTag {
|
|
|
106
117
|
/* -----------------------------------------------------------
|
|
107
118
|
MISCELLANEOUS
|
|
108
119
|
----------------------------------------------------------- */
|
|
120
|
+
export interface IBigintType {
|
|
121
|
+
kind: "type";
|
|
122
|
+
value: "int64" | "uint64";
|
|
123
|
+
}
|
|
124
|
+
|
|
109
125
|
export interface ISign {
|
|
110
126
|
include: boolean;
|
|
111
127
|
value: number;
|
package/src/metadata/Metadata.ts
CHANGED
|
@@ -205,6 +205,7 @@ export class Metadata {
|
|
|
205
205
|
public empty(): boolean {
|
|
206
206
|
return this.bucket() === 0 || this.size() === 0;
|
|
207
207
|
}
|
|
208
|
+
|
|
208
209
|
public size(): number {
|
|
209
210
|
return (
|
|
210
211
|
(this.resolved ? 1 : 0) +
|
|
@@ -223,6 +224,7 @@ export class Metadata {
|
|
|
223
224
|
this.maps.length
|
|
224
225
|
);
|
|
225
226
|
}
|
|
227
|
+
|
|
226
228
|
public bucket(): number {
|
|
227
229
|
return (
|
|
228
230
|
(this.resolved ? 1 : 0) +
|
|
@@ -239,6 +241,7 @@ export class Metadata {
|
|
|
239
241
|
(this.maps.length ? 1 : 0)
|
|
240
242
|
);
|
|
241
243
|
}
|
|
244
|
+
|
|
242
245
|
public isConstant(): boolean {
|
|
243
246
|
return this.bucket() === (this.constants.length ? 1 : 0);
|
|
244
247
|
}
|
|
@@ -508,15 +511,15 @@ function getName(metadata: Metadata): string {
|
|
|
508
511
|
elements.push(`Map<${map.key.getName()}, ${map.value.getName()}>`);
|
|
509
512
|
|
|
510
513
|
// ARRAY
|
|
511
|
-
if (metadata.rest !== null)
|
|
514
|
+
if (metadata.rest !== null)
|
|
515
|
+
elements.push(`Rest<${metadata.rest.getName()}>`);
|
|
512
516
|
for (const tuple of metadata.tuples)
|
|
513
517
|
elements.push(`[${tuple.map((elem) => elem.getName()).join(", ")}]`);
|
|
514
518
|
for (const array of metadata.arrays)
|
|
515
519
|
elements.push(`Array<${array.getName()}>`);
|
|
516
520
|
|
|
517
521
|
// OBJECT
|
|
518
|
-
for (const object of metadata.objects)
|
|
519
|
-
elements.push(`Resolve<${object.name}>`);
|
|
522
|
+
for (const object of metadata.objects) elements.push(object.name);
|
|
520
523
|
if (metadata.resolved !== null) elements.push(metadata.resolved.getName());
|
|
521
524
|
|
|
522
525
|
// RETURNS
|
package/src/module.ts
CHANGED
|
@@ -20,6 +20,7 @@ export * from "./TypeGuardError";
|
|
|
20
20
|
SINGLE FUNCTIONS
|
|
21
21
|
- BASIC VALIDATORS
|
|
22
22
|
- STRICT VALIDATORS
|
|
23
|
+
- PROTOCOL BUFFER FUNCTIONS
|
|
23
24
|
- JSON FUNCTIONS
|
|
24
25
|
- MISCELLANEOUS
|
|
25
26
|
==============================================================
|
|
@@ -410,6 +411,16 @@ export function validateEquals(): never {
|
|
|
410
411
|
}
|
|
411
412
|
Object.assign(validateEquals, Namespace.validate());
|
|
412
413
|
|
|
414
|
+
/* -----------------------------------------------------------
|
|
415
|
+
PROTOCOL BUFFER FUNCTIONS
|
|
416
|
+
----------------------------------------------------------- */
|
|
417
|
+
export function message(): never;
|
|
418
|
+
export function message<T>(): string;
|
|
419
|
+
|
|
420
|
+
export function message(): string {
|
|
421
|
+
halt("message");
|
|
422
|
+
}
|
|
423
|
+
|
|
413
424
|
/* -----------------------------------------------------------
|
|
414
425
|
JSON FUNCTIONS
|
|
415
426
|
----------------------------------------------------------- */
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { MetadataCollection } from "../factories/MetadataCollection";
|
|
4
|
+
import { ProtocolFactory } from "../factories/ProtocolFactory";
|
|
5
|
+
|
|
6
|
+
import { Metadata } from "../metadata/Metadata";
|
|
7
|
+
|
|
8
|
+
import { IProject } from "../transformers/IProject";
|
|
9
|
+
|
|
10
|
+
import { Escaper } from "../utils/Escaper";
|
|
11
|
+
import { MapUtil } from "../utils/MapUtil";
|
|
12
|
+
import { NameEncoder } from "../utils/NameEncoder";
|
|
13
|
+
|
|
14
|
+
import { IProtocolMap } from "../messages/IProtocolMap";
|
|
15
|
+
import { IProtocolMessage } from "../messages/IProtocolMessage";
|
|
16
|
+
|
|
17
|
+
export namespace MessageProgrammer {
|
|
18
|
+
export const generate =
|
|
19
|
+
({ checker }: IProject) =>
|
|
20
|
+
(type: ts.Type) => {
|
|
21
|
+
// PARSE TARGET TYPE
|
|
22
|
+
const collection: MetadataCollection = new MetadataCollection();
|
|
23
|
+
const metadata: Metadata =
|
|
24
|
+
ProtocolFactory.metadata(checker)(collection)(type);
|
|
25
|
+
|
|
26
|
+
// CONVERT TO PROTOCOL MESSAGES
|
|
27
|
+
const dict: Map<string, IProtocolMessage> = new Map();
|
|
28
|
+
ProtocolFactory.generate(collection)(dict)(metadata);
|
|
29
|
+
|
|
30
|
+
// STRINGIFY
|
|
31
|
+
const hierarchies: Map<string, Hierarchy> = new Map();
|
|
32
|
+
for (const msg of dict.values()) emplace(hierarchies)(msg);
|
|
33
|
+
|
|
34
|
+
const content: string =
|
|
35
|
+
`syntax = "proto3";\n\n` +
|
|
36
|
+
[...hierarchies.values()]
|
|
37
|
+
.map((hier) => stringify(hier))
|
|
38
|
+
.join("\n\n");
|
|
39
|
+
|
|
40
|
+
// RETURNS
|
|
41
|
+
return ts.factory.createStringLiteral(content);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const stringify = (hierarchy: Hierarchy): string => {
|
|
45
|
+
const { key, message, children } = hierarchy;
|
|
46
|
+
let index: number = 1;
|
|
47
|
+
|
|
48
|
+
const elements: string[] = [`message ${NameEncoder.encode(key)} {`];
|
|
49
|
+
if (message !== null)
|
|
50
|
+
elements.push(
|
|
51
|
+
...message.properties.map((property) => {
|
|
52
|
+
const key: string =
|
|
53
|
+
Escaper.variable(property.key) &&
|
|
54
|
+
property.key.indexOf("$") === -1
|
|
55
|
+
? property.key
|
|
56
|
+
: `v${index + 1}`;
|
|
57
|
+
if (property.oneOf.length === 1)
|
|
58
|
+
return [
|
|
59
|
+
TAB,
|
|
60
|
+
property.required ? "" : "optional ",
|
|
61
|
+
property.repeated ? "repeated " : "",
|
|
62
|
+
getTypeName(property.oneOf[0]!.type) + " ",
|
|
63
|
+
key,
|
|
64
|
+
" = ",
|
|
65
|
+
`${index++};`,
|
|
66
|
+
].join("");
|
|
67
|
+
return (
|
|
68
|
+
`${TAB}oneof ${key} {\n` +
|
|
69
|
+
property.oneOf
|
|
70
|
+
.map(
|
|
71
|
+
(o, i) =>
|
|
72
|
+
`${TAB}${TAB}${getTypeName(
|
|
73
|
+
o.type,
|
|
74
|
+
)} ${key}_o${i} = ${index++};`,
|
|
75
|
+
)
|
|
76
|
+
.join("\n") +
|
|
77
|
+
`\n${TAB}}`
|
|
78
|
+
);
|
|
79
|
+
}),
|
|
80
|
+
);
|
|
81
|
+
if (message !== null && children.size) elements.push("\n");
|
|
82
|
+
if (children.size)
|
|
83
|
+
elements.push(
|
|
84
|
+
[...children.values()]
|
|
85
|
+
.map((child) => stringify(child))
|
|
86
|
+
.map((body) =>
|
|
87
|
+
body
|
|
88
|
+
.split("\n")
|
|
89
|
+
.map((line) => `${TAB}${line}`)
|
|
90
|
+
.join("\n"),
|
|
91
|
+
)
|
|
92
|
+
.join("\n\n"),
|
|
93
|
+
);
|
|
94
|
+
elements.push("}");
|
|
95
|
+
|
|
96
|
+
return elements.join("\n");
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const emplace =
|
|
100
|
+
(dict: Map<string, Hierarchy>) => (message: IProtocolMessage) => {
|
|
101
|
+
const accessors: string[] = message.name.split(".");
|
|
102
|
+
accessors.forEach((access, i) => {
|
|
103
|
+
const hierarchy: Hierarchy = MapUtil.take(dict, access, () => ({
|
|
104
|
+
key: access,
|
|
105
|
+
message: null!,
|
|
106
|
+
children: new Map(),
|
|
107
|
+
}));
|
|
108
|
+
dict = hierarchy.children;
|
|
109
|
+
if (i === accessors.length - 1) hierarchy.message = message;
|
|
110
|
+
});
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
function getTypeName(type: string | IProtocolMap): string {
|
|
114
|
+
return typeof type === "string"
|
|
115
|
+
? NameEncoder.encode(type)
|
|
116
|
+
: `map<${type.key}, ${getTypeName(type.value)}>`;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
interface Hierarchy {
|
|
121
|
+
key: string;
|
|
122
|
+
message: IProtocolMessage | null;
|
|
123
|
+
children: Map<string, Hierarchy>;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const TAB = " ".repeat(4);
|