typia 4.1.6-dev.20230213 → 5.0.0-dev.20230711
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 +9 -1
- package/lib/factories/MetadataTagFactory.js +11 -4
- package/lib/factories/MetadataTagFactory.js.map +1 -1
- package/lib/factories/ProtocolFactory.d.ts +8 -0
- package/lib/factories/ProtocolFactory.js +113 -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 +173 -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 +3 -3
- package/lib/metadata/Metadata.js.map +1 -1
- package/lib/module.d.ts +2 -0
- package/lib/module.js +6 -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 +8 -5
- package/lib/programmers/internal/application_number.js.map +1 -1
- package/lib/transformers/CallExpressionTransformer.js +5 -3
- 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 +20 -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 +1 -1
- package/src/factories/MetadataTagFactory.ts +10 -3
- package/src/factories/ProtocolFactory.ts +80 -0
- package/src/factories/internal/protocols/ProtocolMetadataUtil.ts +180 -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 +11 -3
- package/src/metadata/Metadata.ts +3 -0
- package/src/module.ts +11 -0
- package/src/programmers/MessageProgrammer.ts +126 -0
- package/src/programmers/internal/application_number.ts +10 -7
- package/src/transformers/CallExpressionTransformer.ts +17 -10
- package/src/transformers/features/miscellaneous/ApplicationTransformer.ts +1 -3
- package/src/transformers/features/protocols/MessageTransformer.ts +31 -0
- package/src/utils/Escaper.ts +46 -46
- package/src/utils/NameEncoder.ts +32 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { IMetadataTag } from "../../../metadata/IMetadataTag";
|
|
2
|
+
import { Metadata } from "../../../metadata/Metadata";
|
|
3
|
+
import { MetadataObject } from "../../../metadata/MetadataObject";
|
|
4
|
+
|
|
5
|
+
import { IProtocolMap } from "../../../messages/IProtocolMap";
|
|
6
|
+
import { IProtocolMessage } from "../../../messages/IProtocolMessage";
|
|
7
|
+
import { ProtocolMetadataUtil } from "./ProtocolMetadataUtil";
|
|
8
|
+
import { emplace_protocol_object } from "./emplace_protocol_object";
|
|
9
|
+
import { iterate_protocol_metadata } from "./iterate_protocol_metadata";
|
|
10
|
+
|
|
11
|
+
export const iterate_protocol_repeated =
|
|
12
|
+
(container: "Array" | "Set") =>
|
|
13
|
+
(sole: boolean, bucket: boolean) =>
|
|
14
|
+
(dict: Map<string, IProtocolMessage>) =>
|
|
15
|
+
(meta: Metadata) =>
|
|
16
|
+
(tags: IMetadataTag[]): string | IProtocolMap => {
|
|
17
|
+
sole &&= ProtocolMetadataUtil.standalone(meta);
|
|
18
|
+
if (sole === true)
|
|
19
|
+
return iterate_protocol_metadata(dict)(meta)(tags).oneOf[0]!.type;
|
|
20
|
+
|
|
21
|
+
const obj: MetadataObject = ProtocolMetadataUtil.object(
|
|
22
|
+
`${container}.${bucket ? "Element" : "Wrapper"}<${meta.getName()}>`,
|
|
23
|
+
dict.size,
|
|
24
|
+
);
|
|
25
|
+
obj.properties.push(
|
|
26
|
+
ProtocolMetadataUtil.property(
|
|
27
|
+
"value",
|
|
28
|
+
bucket ? meta : ProtocolMetadataUtil.array(meta),
|
|
29
|
+
tags,
|
|
30
|
+
),
|
|
31
|
+
);
|
|
32
|
+
emplace_protocol_object(dict)(obj);
|
|
33
|
+
return obj.name;
|
|
34
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Metadata } from "../../../metadata/Metadata";
|
|
2
|
+
import { MetadataObject } from "../../../metadata/MetadataObject";
|
|
3
|
+
|
|
4
|
+
import { IProtocolMessage } from "../../../messages/IProtocolMessage";
|
|
5
|
+
import { ProtocolMetadataUtil } from "./ProtocolMetadataUtil";
|
|
6
|
+
import { emplace_protocol_object } from "./emplace_protocol_object";
|
|
7
|
+
|
|
8
|
+
export const iterate_protocol_tuple =
|
|
9
|
+
(dict: Map<string, IProtocolMessage>) =>
|
|
10
|
+
(tuple: Metadata[]): string => {
|
|
11
|
+
const name: string = `[${tuple.map((m) => m.getName()).join(", ")}]`;
|
|
12
|
+
if (dict.has(name)) return name;
|
|
13
|
+
|
|
14
|
+
const obj: MetadataObject = ProtocolMetadataUtil.object(
|
|
15
|
+
name,
|
|
16
|
+
dict.size,
|
|
17
|
+
);
|
|
18
|
+
obj.properties.push(
|
|
19
|
+
...tuple.map((value, i) =>
|
|
20
|
+
ProtocolMetadataUtil.property(
|
|
21
|
+
`v${i}`,
|
|
22
|
+
value.rest ? ProtocolMetadataUtil.array(value.rest) : value,
|
|
23
|
+
[],
|
|
24
|
+
),
|
|
25
|
+
),
|
|
26
|
+
);
|
|
27
|
+
emplace_protocol_object(dict)(obj);
|
|
28
|
+
return obj.name;
|
|
29
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { $varint_decode_i32, $varint_encode } from "./$varint";
|
|
2
|
+
|
|
3
|
+
export function $proto_bytes_encode(
|
|
4
|
+
dst: Uint8Array,
|
|
5
|
+
offset: number,
|
|
6
|
+
value: Uint8Array,
|
|
7
|
+
): number {
|
|
8
|
+
offset = $varint_encode(dst, offset, value.length);
|
|
9
|
+
dst.set(value, offset);
|
|
10
|
+
offset += value.length;
|
|
11
|
+
return offset;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function $proto_bytes_decode(
|
|
15
|
+
src: Uint8Array,
|
|
16
|
+
offset: number,
|
|
17
|
+
): [value: Uint8Array, offset: number] {
|
|
18
|
+
let length = 0;
|
|
19
|
+
|
|
20
|
+
[length, offset] = $varint_decode_i32(src, offset); // Max Length: ~2GB
|
|
21
|
+
const value = src.subarray(offset, offset + length);
|
|
22
|
+
offset += length;
|
|
23
|
+
|
|
24
|
+
return [value, offset];
|
|
25
|
+
}
|
|
@@ -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.buffer).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.buffer).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.buffer).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.buffer).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
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type IMetadataTag =
|
|
2
2
|
// NUMBER
|
|
3
|
-
| IMetadataTag.
|
|
3
|
+
| IMetadataTag.INumberType
|
|
4
4
|
| IMetadataTag.IMinimum
|
|
5
5
|
| IMetadataTag.IMaximum
|
|
6
6
|
| IMetadataTag.IExclusiveMinimum
|
|
@@ -22,9 +22,17 @@ export namespace IMetadataTag {
|
|
|
22
22
|
/* -----------------------------------------------------------
|
|
23
23
|
NUMBER
|
|
24
24
|
----------------------------------------------------------- */
|
|
25
|
-
export interface
|
|
25
|
+
export interface INumberType {
|
|
26
26
|
kind: "type";
|
|
27
|
-
value:
|
|
27
|
+
value:
|
|
28
|
+
| "int"
|
|
29
|
+
| "uint"
|
|
30
|
+
| "int32"
|
|
31
|
+
| "uint32"
|
|
32
|
+
| "int64"
|
|
33
|
+
| "uint64"
|
|
34
|
+
| "float"
|
|
35
|
+
| "double";
|
|
28
36
|
}
|
|
29
37
|
|
|
30
38
|
export interface IMinimum {
|
package/src/metadata/Metadata.ts
CHANGED
|
@@ -266,6 +266,7 @@ export class Metadata {
|
|
|
266
266
|
public empty(): boolean {
|
|
267
267
|
return this.bucket() === 0 || this.size() === 0;
|
|
268
268
|
}
|
|
269
|
+
|
|
269
270
|
public size(): number {
|
|
270
271
|
return (
|
|
271
272
|
(this.resolved ? 1 : 0) +
|
|
@@ -285,6 +286,7 @@ export class Metadata {
|
|
|
285
286
|
this.aliases.length
|
|
286
287
|
);
|
|
287
288
|
}
|
|
289
|
+
|
|
288
290
|
public bucket(): number {
|
|
289
291
|
return (
|
|
290
292
|
(this.resolved ? 1 : 0) +
|
|
@@ -302,6 +304,7 @@ export class Metadata {
|
|
|
302
304
|
(this.aliases.length ? 1 : 0)
|
|
303
305
|
);
|
|
304
306
|
}
|
|
307
|
+
|
|
305
308
|
public isConstant(): boolean {
|
|
306
309
|
return this.bucket() === (this.constants.length ? 1 : 0);
|
|
307
310
|
}
|
package/src/module.ts
CHANGED
|
@@ -26,6 +26,7 @@ export * from "./TypeGuardError";
|
|
|
26
26
|
SINGLE FUNCTIONS
|
|
27
27
|
- BASIC VALIDATORS
|
|
28
28
|
- STRICT VALIDATORS
|
|
29
|
+
- PROTOCOL BUFFER FUNCTIONS
|
|
29
30
|
- JSON FUNCTIONS
|
|
30
31
|
- MISCELLANEOUS
|
|
31
32
|
==============================================================
|
|
@@ -466,6 +467,16 @@ export const customValidators: CustomValidatorMap = {
|
|
|
466
467
|
erase: (name) => (type) => $dictionary.get(name)?.delete(type) ?? false,
|
|
467
468
|
};
|
|
468
469
|
|
|
470
|
+
/* -----------------------------------------------------------
|
|
471
|
+
PROTOCOL BUFFER FUNCTIONS
|
|
472
|
+
----------------------------------------------------------- */
|
|
473
|
+
export function message(): never;
|
|
474
|
+
export function message<T>(): string;
|
|
475
|
+
|
|
476
|
+
export function message(): string {
|
|
477
|
+
halt("message");
|
|
478
|
+
}
|
|
479
|
+
|
|
469
480
|
/* -----------------------------------------------------------
|
|
470
481
|
JSON FUNCTIONS
|
|
471
482
|
----------------------------------------------------------- */
|