typia 9.7.2 → 10.0.0-dev.20251107

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.
Files changed (116) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +153 -153
  3. package/lib/factories/ProtobufFactory.js +1 -1
  4. package/lib/factories/ProtobufFactory.mjs +1 -1
  5. package/lib/programmers/internal/json_schema_station.d.mts +2 -2
  6. package/lib/programmers/internal/json_schema_station.d.ts +2 -2
  7. package/lib/programmers/llm/LlmApplicationProgrammer.js +5 -1
  8. package/lib/programmers/llm/LlmApplicationProgrammer.js.map +1 -1
  9. package/lib/programmers/llm/LlmApplicationProgrammer.mjs +5 -1
  10. package/lib/programmers/llm/LlmSchemaProgrammer.js +1 -4
  11. package/lib/programmers/llm/LlmSchemaProgrammer.js.map +1 -1
  12. package/lib/programmers/llm/LlmSchemaProgrammer.mjs +1 -35
  13. package/package.json +121 -121
  14. package/src/AssertionGuard.ts +41 -41
  15. package/src/CamelCase.ts +75 -75
  16. package/src/IRandomGenerator.ts +337 -337
  17. package/src/IReadableURLSearchParams.ts +9 -9
  18. package/src/PascalCase.ts +71 -71
  19. package/src/Primitive.ts +90 -90
  20. package/src/Resolved.ts +72 -72
  21. package/src/SnakeCase.ts +127 -127
  22. package/src/TypeGuardError.ts +216 -216
  23. package/src/factories/MetadataCollection.ts +270 -270
  24. package/src/factories/MetadataCommentTagFactory.ts +632 -632
  25. package/src/factories/MetadataFactory.ts +402 -402
  26. package/src/factories/ProtobufFactory.ts +873 -873
  27. package/src/functional.ts +705 -705
  28. package/src/http.ts +972 -972
  29. package/src/internal/_ProtobufReader.ts +188 -188
  30. package/src/internal/_ProtobufSizer.ts +137 -137
  31. package/src/internal/_ProtobufWriter.ts +135 -135
  32. package/src/internal/_jsonStringifyString.ts +42 -42
  33. package/src/json.ts +643 -643
  34. package/src/llm.ts +615 -615
  35. package/src/misc.ts +594 -594
  36. package/src/module.ts +889 -889
  37. package/src/notations.ts +751 -751
  38. package/src/programmers/FeatureProgrammer.ts +605 -605
  39. package/src/programmers/ImportProgrammer.ts +179 -179
  40. package/src/programmers/RandomProgrammer.ts +1195 -1195
  41. package/src/programmers/helpers/ProtobufWire.ts +34 -34
  42. package/src/programmers/internal/check_array_length.ts +43 -43
  43. package/src/programmers/internal/check_bigint.ts +46 -46
  44. package/src/programmers/internal/check_dynamic_key.ts +197 -197
  45. package/src/programmers/internal/check_dynamic_properties.ts +231 -231
  46. package/src/programmers/internal/check_everything.ts +21 -21
  47. package/src/programmers/internal/check_native.ts +23 -23
  48. package/src/programmers/internal/check_number.ts +108 -108
  49. package/src/programmers/internal/check_object.ts +72 -72
  50. package/src/programmers/internal/check_string.ts +46 -46
  51. package/src/programmers/internal/check_template.ts +46 -46
  52. package/src/programmers/internal/check_union_array_like.ts +331 -331
  53. package/src/programmers/internal/decode_union_object.ts +110 -110
  54. package/src/programmers/internal/feature_object_entries.ts +59 -59
  55. package/src/programmers/internal/json_schema_escaped.ts +78 -78
  56. package/src/programmers/internal/json_schema_object.ts +150 -150
  57. package/src/programmers/internal/json_schema_station.ts +2 -2
  58. package/src/programmers/internal/metadata_to_pattern.ts +40 -40
  59. package/src/programmers/internal/postfix_of_tuple.ts +3 -3
  60. package/src/programmers/internal/prune_object_properties.ts +69 -69
  61. package/src/programmers/internal/stringify_dynamic_properties.ts +158 -158
  62. package/src/programmers/internal/stringify_native.ts +5 -5
  63. package/src/programmers/internal/stringify_regular_properties.ts +77 -77
  64. package/src/programmers/internal/template_to_pattern.ts +21 -21
  65. package/src/programmers/internal/wrap_metadata_rest_tuple.ts +21 -21
  66. package/src/programmers/json/JsonStringifyProgrammer.ts +1124 -1124
  67. package/src/programmers/llm/LlmApplicationProgrammer.ts +10 -1
  68. package/src/programmers/llm/LlmSchemaProgrammer.ts +2 -7
  69. package/src/protobuf.ts +820 -820
  70. package/src/reflect.ts +46 -46
  71. package/src/schemas/json/IJsonApplication.ts +77 -77
  72. package/src/schemas/json/IJsonSchemaCollection.ts +212 -212
  73. package/src/schemas/json/IJsonSchemaUnit.ts +263 -263
  74. package/src/schemas/metadata/IMetadataTypeTag.ts +14 -14
  75. package/src/schemas/metadata/Metadata.ts +669 -669
  76. package/src/schemas/metadata/MetadataAliasType.ts +57 -57
  77. package/src/schemas/metadata/MetadataApplication.ts +40 -40
  78. package/src/schemas/metadata/MetadataArray.ts +47 -47
  79. package/src/schemas/metadata/MetadataArrayType.ts +51 -51
  80. package/src/schemas/metadata/MetadataAtomic.ts +85 -85
  81. package/src/schemas/metadata/MetadataEscaped.ts +45 -45
  82. package/src/schemas/metadata/MetadataFunction.ts +45 -45
  83. package/src/schemas/metadata/MetadataObject.ts +46 -46
  84. package/src/schemas/metadata/MetadataObjectType.ts +137 -137
  85. package/src/schemas/metadata/MetadataParameter.ts +52 -52
  86. package/src/schemas/metadata/MetadataProperty.ts +53 -53
  87. package/src/schemas/metadata/MetadataTemplate.ts +78 -78
  88. package/src/schemas/metadata/MetadataTuple.ts +28 -28
  89. package/src/schemas/metadata/MetadataTupleType.ts +61 -61
  90. package/src/tags/Constant.ts +47 -47
  91. package/src/tags/ContentMediaType.ts +27 -27
  92. package/src/tags/Default.ts +52 -52
  93. package/src/tags/Example.ts +56 -56
  94. package/src/tags/Examples.ts +56 -56
  95. package/src/tags/ExclusiveMaximum.ts +44 -44
  96. package/src/tags/ExclusiveMinimum.ts +44 -44
  97. package/src/tags/Format.ts +78 -78
  98. package/src/tags/JsonSchemaPlugin.ts +36 -36
  99. package/src/tags/MaxItems.ts +31 -31
  100. package/src/tags/MaxLength.ts +25 -25
  101. package/src/tags/Maximum.ts +39 -39
  102. package/src/tags/MinItems.ts +31 -31
  103. package/src/tags/MinLength.ts +25 -25
  104. package/src/tags/Minimum.ts +39 -39
  105. package/src/tags/MultipleOf.ts +42 -42
  106. package/src/tags/Pattern.ts +49 -49
  107. package/src/tags/Sequence.ts +37 -37
  108. package/src/tags/TagBase.ts +102 -102
  109. package/src/tags/Type.ts +64 -64
  110. package/src/tags/UniqueItems.ts +34 -34
  111. package/src/tags/internal/FormatCheatSheet.ts +71 -71
  112. package/src/transformers/ITransformOptions.ts +70 -70
  113. package/src/transformers/ImportTransformer.ts +253 -253
  114. package/src/transformers/NoTransformConfigurationError.ts +16 -16
  115. package/src/transformers/features/llm/LlmApplicationTransformer.ts +224 -224
  116. package/src/typings/Equal.ts +18 -18
@@ -1,188 +1,188 @@
1
- import { ProtobufWire } from "../programmers/helpers/ProtobufWire";
2
-
3
- import { Singleton } from "../utils/Singleton";
4
-
5
- /// @reference https://github.com/piotr-oles/as-proto/blob/main/packages/as-proto/assembly/internal/FixedReader.ts
6
- export class _ProtobufReader {
7
- /** Read buffer */
8
- private buf: Uint8Array;
9
-
10
- /** Read buffer pointer. */
11
- private ptr: number;
12
-
13
- /** DataView for buffer. */
14
- private view: DataView;
15
-
16
- public constructor(buf: Uint8Array) {
17
- this.buf = buf;
18
- this.ptr = 0;
19
- this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
20
- }
21
-
22
- public index(): number {
23
- return this.ptr;
24
- }
25
-
26
- public size(): number {
27
- return this.buf.length;
28
- }
29
-
30
- public uint32(): number {
31
- return this.varint32();
32
- }
33
-
34
- public int32(): number {
35
- return this.varint32();
36
- }
37
-
38
- public sint32(): number {
39
- const value: number = this.varint32();
40
- return (value >>> 1) ^ -(value & 1);
41
- }
42
-
43
- public uint64(): bigint {
44
- return this.varint64();
45
- }
46
-
47
- public int64(): bigint {
48
- return this.varint64();
49
- }
50
-
51
- public sint64(): bigint {
52
- const value = this.varint64();
53
- return (value >> BigInt(0x01)) ^ -(value & BigInt(0x01));
54
- }
55
-
56
- public bool(): boolean {
57
- return this.varint32() !== 0;
58
- }
59
-
60
- public float(): number {
61
- const value: number = this.view.getFloat32(this.ptr, true);
62
- this.ptr += 4;
63
- return value;
64
- }
65
-
66
- public double(): number {
67
- const value: number = this.view.getFloat64(this.ptr, true);
68
- this.ptr += 8;
69
- return value;
70
- }
71
-
72
- public bytes(): Uint8Array {
73
- const length: number = this.uint32();
74
- const from: number = this.ptr;
75
- this.ptr += length;
76
- return this.buf.subarray(from, from + length);
77
- }
78
-
79
- public string(): string {
80
- return utf8.get().decode(this.bytes());
81
- }
82
-
83
- public skip(length: number): void {
84
- if (length === 0) while (this.u8() & 0x80);
85
- else {
86
- if (this.index() + length > this.size())
87
- throw new Error("Error on typia.protobuf.decode(): buffer overflow.");
88
- this.ptr += length;
89
- }
90
- }
91
-
92
- public skipType(wireType: ProtobufWire): void {
93
- switch (wireType) {
94
- case ProtobufWire.VARIANT:
95
- this.skip(0);
96
- break;
97
- case ProtobufWire.I64:
98
- this.skip(8);
99
- break;
100
- case ProtobufWire.LEN:
101
- this.skip(this.uint32());
102
- break;
103
- case ProtobufWire.START_GROUP:
104
- while ((wireType = this.uint32() & 0x07) !== ProtobufWire.END_GROUP)
105
- this.skipType(wireType);
106
- break;
107
- case ProtobufWire.I32:
108
- this.skip(4);
109
- break;
110
- default:
111
- throw new Error(`Invalid wire type ${wireType} at offset ${this.ptr}.`);
112
- }
113
- }
114
-
115
- private varint32(): number {
116
- let loaded: number;
117
- let value: number;
118
-
119
- value = (loaded = this.u8()) & 0x7f;
120
- if (loaded < 0x80) return value;
121
-
122
- value |= ((loaded = this.u8()) & 0x7f) << 7;
123
- if (loaded < 0x80) return value;
124
-
125
- value |= ((loaded = this.u8()) & 0x7f) << 14;
126
- if (loaded < 0x80) return value;
127
-
128
- value |= ((loaded = this.u8()) & 0x7f) << 21;
129
- if (loaded < 0x80) return value;
130
-
131
- value |= ((loaded = this.u8()) & 0xf) << 28;
132
- if (loaded < 0x80) return value;
133
-
134
- // increment position until there is no continuation bit or until we read 10 bytes
135
- if (this.u8() < 0x80) return value;
136
- if (this.u8() < 0x80) return value;
137
- if (this.u8() < 0x80) return value;
138
- if (this.u8() < 0x80) return value;
139
- if (this.u8() < 0x80) return value;
140
-
141
- return value;
142
- }
143
-
144
- private varint64(): bigint {
145
- let loaded: bigint;
146
- let value: bigint;
147
-
148
- value = (loaded = this.u8n()) & BigInt(0x7f);
149
- if (loaded < BigInt(0x80)) return value;
150
-
151
- value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(7);
152
- if (loaded < BigInt(0x80)) return value;
153
-
154
- value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(14);
155
- if (loaded < BigInt(0x80)) return value;
156
-
157
- value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(21);
158
- if (loaded < BigInt(0x80)) return value;
159
-
160
- value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(28);
161
- if (loaded < BigInt(0x80)) return value;
162
-
163
- value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(35);
164
- if (loaded < BigInt(0x80)) return value;
165
-
166
- value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(42);
167
- if (loaded < BigInt(0x80)) return value;
168
-
169
- value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(49);
170
- if (loaded < BigInt(0x80)) return value;
171
-
172
- value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(56);
173
- if (loaded < BigInt(0x80)) return value;
174
-
175
- value |= (this.u8n() & BigInt(0x01)) << BigInt(63);
176
- return BigInt.asIntN(64, value);
177
- }
178
-
179
- private u8(): number {
180
- return this.view.getUint8(this.ptr++);
181
- }
182
-
183
- private u8n(): bigint {
184
- return BigInt(this.u8());
185
- }
186
- }
187
-
188
- const utf8 = /** @__PURE__ */ new Singleton(() => new TextDecoder("utf-8"));
1
+ import { ProtobufWire } from "../programmers/helpers/ProtobufWire";
2
+
3
+ import { Singleton } from "../utils/Singleton";
4
+
5
+ /// @reference https://github.com/piotr-oles/as-proto/blob/main/packages/as-proto/assembly/internal/FixedReader.ts
6
+ export class _ProtobufReader {
7
+ /** Read buffer */
8
+ private buf: Uint8Array;
9
+
10
+ /** Read buffer pointer. */
11
+ private ptr: number;
12
+
13
+ /** DataView for buffer. */
14
+ private view: DataView;
15
+
16
+ public constructor(buf: Uint8Array) {
17
+ this.buf = buf;
18
+ this.ptr = 0;
19
+ this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
20
+ }
21
+
22
+ public index(): number {
23
+ return this.ptr;
24
+ }
25
+
26
+ public size(): number {
27
+ return this.buf.length;
28
+ }
29
+
30
+ public uint32(): number {
31
+ return this.varint32();
32
+ }
33
+
34
+ public int32(): number {
35
+ return this.varint32();
36
+ }
37
+
38
+ public sint32(): number {
39
+ const value: number = this.varint32();
40
+ return (value >>> 1) ^ -(value & 1);
41
+ }
42
+
43
+ public uint64(): bigint {
44
+ return this.varint64();
45
+ }
46
+
47
+ public int64(): bigint {
48
+ return this.varint64();
49
+ }
50
+
51
+ public sint64(): bigint {
52
+ const value = this.varint64();
53
+ return (value >> BigInt(0x01)) ^ -(value & BigInt(0x01));
54
+ }
55
+
56
+ public bool(): boolean {
57
+ return this.varint32() !== 0;
58
+ }
59
+
60
+ public float(): number {
61
+ const value: number = this.view.getFloat32(this.ptr, true);
62
+ this.ptr += 4;
63
+ return value;
64
+ }
65
+
66
+ public double(): number {
67
+ const value: number = this.view.getFloat64(this.ptr, true);
68
+ this.ptr += 8;
69
+ return value;
70
+ }
71
+
72
+ public bytes(): Uint8Array {
73
+ const length: number = this.uint32();
74
+ const from: number = this.ptr;
75
+ this.ptr += length;
76
+ return this.buf.subarray(from, from + length);
77
+ }
78
+
79
+ public string(): string {
80
+ return utf8.get().decode(this.bytes());
81
+ }
82
+
83
+ public skip(length: number): void {
84
+ if (length === 0) while (this.u8() & 0x80);
85
+ else {
86
+ if (this.index() + length > this.size())
87
+ throw new Error("Error on typia.protobuf.decode(): buffer overflow.");
88
+ this.ptr += length;
89
+ }
90
+ }
91
+
92
+ public skipType(wireType: ProtobufWire): void {
93
+ switch (wireType) {
94
+ case ProtobufWire.VARIANT:
95
+ this.skip(0);
96
+ break;
97
+ case ProtobufWire.I64:
98
+ this.skip(8);
99
+ break;
100
+ case ProtobufWire.LEN:
101
+ this.skip(this.uint32());
102
+ break;
103
+ case ProtobufWire.START_GROUP:
104
+ while ((wireType = this.uint32() & 0x07) !== ProtobufWire.END_GROUP)
105
+ this.skipType(wireType);
106
+ break;
107
+ case ProtobufWire.I32:
108
+ this.skip(4);
109
+ break;
110
+ default:
111
+ throw new Error(`Invalid wire type ${wireType} at offset ${this.ptr}.`);
112
+ }
113
+ }
114
+
115
+ private varint32(): number {
116
+ let loaded: number;
117
+ let value: number;
118
+
119
+ value = (loaded = this.u8()) & 0x7f;
120
+ if (loaded < 0x80) return value;
121
+
122
+ value |= ((loaded = this.u8()) & 0x7f) << 7;
123
+ if (loaded < 0x80) return value;
124
+
125
+ value |= ((loaded = this.u8()) & 0x7f) << 14;
126
+ if (loaded < 0x80) return value;
127
+
128
+ value |= ((loaded = this.u8()) & 0x7f) << 21;
129
+ if (loaded < 0x80) return value;
130
+
131
+ value |= ((loaded = this.u8()) & 0xf) << 28;
132
+ if (loaded < 0x80) return value;
133
+
134
+ // increment position until there is no continuation bit or until we read 10 bytes
135
+ if (this.u8() < 0x80) return value;
136
+ if (this.u8() < 0x80) return value;
137
+ if (this.u8() < 0x80) return value;
138
+ if (this.u8() < 0x80) return value;
139
+ if (this.u8() < 0x80) return value;
140
+
141
+ return value;
142
+ }
143
+
144
+ private varint64(): bigint {
145
+ let loaded: bigint;
146
+ let value: bigint;
147
+
148
+ value = (loaded = this.u8n()) & BigInt(0x7f);
149
+ if (loaded < BigInt(0x80)) return value;
150
+
151
+ value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(7);
152
+ if (loaded < BigInt(0x80)) return value;
153
+
154
+ value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(14);
155
+ if (loaded < BigInt(0x80)) return value;
156
+
157
+ value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(21);
158
+ if (loaded < BigInt(0x80)) return value;
159
+
160
+ value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(28);
161
+ if (loaded < BigInt(0x80)) return value;
162
+
163
+ value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(35);
164
+ if (loaded < BigInt(0x80)) return value;
165
+
166
+ value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(42);
167
+ if (loaded < BigInt(0x80)) return value;
168
+
169
+ value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(49);
170
+ if (loaded < BigInt(0x80)) return value;
171
+
172
+ value |= ((loaded = this.u8n()) & BigInt(0x7f)) << BigInt(56);
173
+ if (loaded < BigInt(0x80)) return value;
174
+
175
+ value |= (this.u8n() & BigInt(0x01)) << BigInt(63);
176
+ return BigInt.asIntN(64, value);
177
+ }
178
+
179
+ private u8(): number {
180
+ return this.view.getUint8(this.ptr++);
181
+ }
182
+
183
+ private u8n(): bigint {
184
+ return BigInt(this.u8());
185
+ }
186
+ }
187
+
188
+ const utf8 = /** @__PURE__ */ new Singleton(() => new TextDecoder("utf-8"));
@@ -1,137 +1,137 @@
1
- import { _IProtobufWriter } from "./_IProtobufWriter";
2
-
3
- /// @reference https://github.com/piotr-oles/as-proto/blob/main/packages/as-proto/assembly/internal/FixedSizer.ts
4
- export class _ProtobufSizer implements _IProtobufWriter {
5
- /** Total length. */
6
- public len: number;
7
-
8
- /** Position stack. */
9
- public readonly pos: Array<number>;
10
-
11
- /** Variable length list. */
12
- public readonly varlen: Array<number>;
13
-
14
- /** Variable length index stack. */
15
- public readonly varlenidx: Array<number>;
16
-
17
- public constructor(length: number = 0) {
18
- this.len = length;
19
- this.pos = [];
20
- this.varlen = [];
21
- this.varlenidx = [];
22
- }
23
-
24
- public bool(): void {
25
- this.len += 1;
26
- }
27
- public int32(value: number): void {
28
- if (value < 0) {
29
- // 10 bytes to encode negative number
30
- this.len += 10;
31
- } else {
32
- this.varint32(value);
33
- }
34
- }
35
- public sint32(value: number): void {
36
- this.varint32((value << 1) ^ (value >> 31));
37
- }
38
- public uint32(value: number): void {
39
- this.varint32(value);
40
- }
41
-
42
- public int64(value: bigint | number): void {
43
- this.varint64(typeof value === "number" ? BigInt(value) : value);
44
- }
45
- public sint64(value: bigint | number): void {
46
- if (typeof value === "number") value = BigInt(value);
47
- this.varint64((value << BigInt(1)) ^ (value >> BigInt(63)));
48
- }
49
- public uint64(value: bigint | number): void {
50
- this.varint64(typeof value === "number" ? BigInt(value) : value);
51
- }
52
-
53
- // public fixed32(_value: number): void {
54
- // this.len += 4;
55
- // }
56
- // public sfixed32(_value: number): void {
57
- // this.len += 4;
58
- // }
59
- // public fixed64(_value: number | bigint): void {
60
- // this.len += 8;
61
- // }
62
- // public sfixed64(_value: number | bigint): void {
63
- // this.len += 8;
64
- // }
65
- public float(_value: number): void {
66
- this.len += 4;
67
- }
68
- public double(_value: number): void {
69
- this.len += 8;
70
- }
71
-
72
- public bytes(value: Uint8Array): void {
73
- this.uint32(value.byteLength);
74
- this.len += value.byteLength;
75
- }
76
- public string(value: string): void {
77
- const len: number = strlen(value);
78
- this.varlen.push(len);
79
- this.uint32(len);
80
- this.len += len;
81
- }
82
-
83
- public fork(): void {
84
- this.pos.push(this.len); // save current position
85
- this.varlenidx.push(this.varlen.length); // save current index in varlen array
86
- this.varlen.push(0); // add 0 length to varlen array (to be updated in ldelim())
87
- }
88
-
89
- public ldelim(): void {
90
- if (!(this.pos.length && this.varlenidx.length))
91
- throw new Error(
92
- "Error on typia.protobuf.encode(): missing fork() before ldelim() call.",
93
- );
94
-
95
- const endPos = this.len; // current position is end position
96
- const startPos = this.pos.pop()!; // get start position from stack
97
- const idx = this.varlenidx.pop()!; // get varlen index from stack
98
- const len = endPos - startPos; // calculate length
99
-
100
- this.varlen[idx] = len; // update variable length
101
- this.uint32(len); // add uint32 that should be called in fork()
102
- }
103
-
104
- public reset(): void {
105
- this.len = 0;
106
- // re-use arrays
107
- this.pos.length = 0;
108
- this.varlen.length = 0;
109
- this.varlenidx.length = 0;
110
- }
111
-
112
- private varint32(value: number): void {
113
- this.len +=
114
- value < 0
115
- ? 10 // 10 bits with leading 1's
116
- : value < 0x80
117
- ? 1
118
- : value < 0x4000
119
- ? 2
120
- : value < 0x200000
121
- ? 3
122
- : value < 0x10000000
123
- ? 4
124
- : 5;
125
- }
126
-
127
- private varint64(val: bigint): void {
128
- val = BigInt.asUintN(64, val);
129
- while (val > BigInt(0x7f)) {
130
- ++this.len;
131
- val = val >> BigInt(0x07);
132
- }
133
- ++this.len;
134
- }
135
- }
136
-
137
- const strlen = (str: string): number => new Blob([str]).size;
1
+ import { _IProtobufWriter } from "./_IProtobufWriter";
2
+
3
+ /// @reference https://github.com/piotr-oles/as-proto/blob/main/packages/as-proto/assembly/internal/FixedSizer.ts
4
+ export class _ProtobufSizer implements _IProtobufWriter {
5
+ /** Total length. */
6
+ public len: number;
7
+
8
+ /** Position stack. */
9
+ public readonly pos: Array<number>;
10
+
11
+ /** Variable length list. */
12
+ public readonly varlen: Array<number>;
13
+
14
+ /** Variable length index stack. */
15
+ public readonly varlenidx: Array<number>;
16
+
17
+ public constructor(length: number = 0) {
18
+ this.len = length;
19
+ this.pos = [];
20
+ this.varlen = [];
21
+ this.varlenidx = [];
22
+ }
23
+
24
+ public bool(): void {
25
+ this.len += 1;
26
+ }
27
+ public int32(value: number): void {
28
+ if (value < 0) {
29
+ // 10 bytes to encode negative number
30
+ this.len += 10;
31
+ } else {
32
+ this.varint32(value);
33
+ }
34
+ }
35
+ public sint32(value: number): void {
36
+ this.varint32((value << 1) ^ (value >> 31));
37
+ }
38
+ public uint32(value: number): void {
39
+ this.varint32(value);
40
+ }
41
+
42
+ public int64(value: bigint | number): void {
43
+ this.varint64(typeof value === "number" ? BigInt(value) : value);
44
+ }
45
+ public sint64(value: bigint | number): void {
46
+ if (typeof value === "number") value = BigInt(value);
47
+ this.varint64((value << BigInt(1)) ^ (value >> BigInt(63)));
48
+ }
49
+ public uint64(value: bigint | number): void {
50
+ this.varint64(typeof value === "number" ? BigInt(value) : value);
51
+ }
52
+
53
+ // public fixed32(_value: number): void {
54
+ // this.len += 4;
55
+ // }
56
+ // public sfixed32(_value: number): void {
57
+ // this.len += 4;
58
+ // }
59
+ // public fixed64(_value: number | bigint): void {
60
+ // this.len += 8;
61
+ // }
62
+ // public sfixed64(_value: number | bigint): void {
63
+ // this.len += 8;
64
+ // }
65
+ public float(_value: number): void {
66
+ this.len += 4;
67
+ }
68
+ public double(_value: number): void {
69
+ this.len += 8;
70
+ }
71
+
72
+ public bytes(value: Uint8Array): void {
73
+ this.uint32(value.byteLength);
74
+ this.len += value.byteLength;
75
+ }
76
+ public string(value: string): void {
77
+ const len: number = strlen(value);
78
+ this.varlen.push(len);
79
+ this.uint32(len);
80
+ this.len += len;
81
+ }
82
+
83
+ public fork(): void {
84
+ this.pos.push(this.len); // save current position
85
+ this.varlenidx.push(this.varlen.length); // save current index in varlen array
86
+ this.varlen.push(0); // add 0 length to varlen array (to be updated in ldelim())
87
+ }
88
+
89
+ public ldelim(): void {
90
+ if (!(this.pos.length && this.varlenidx.length))
91
+ throw new Error(
92
+ "Error on typia.protobuf.encode(): missing fork() before ldelim() call.",
93
+ );
94
+
95
+ const endPos = this.len; // current position is end position
96
+ const startPos = this.pos.pop()!; // get start position from stack
97
+ const idx = this.varlenidx.pop()!; // get varlen index from stack
98
+ const len = endPos - startPos; // calculate length
99
+
100
+ this.varlen[idx] = len; // update variable length
101
+ this.uint32(len); // add uint32 that should be called in fork()
102
+ }
103
+
104
+ public reset(): void {
105
+ this.len = 0;
106
+ // re-use arrays
107
+ this.pos.length = 0;
108
+ this.varlen.length = 0;
109
+ this.varlenidx.length = 0;
110
+ }
111
+
112
+ private varint32(value: number): void {
113
+ this.len +=
114
+ value < 0
115
+ ? 10 // 10 bits with leading 1's
116
+ : value < 0x80
117
+ ? 1
118
+ : value < 0x4000
119
+ ? 2
120
+ : value < 0x200000
121
+ ? 3
122
+ : value < 0x10000000
123
+ ? 4
124
+ : 5;
125
+ }
126
+
127
+ private varint64(val: bigint): void {
128
+ val = BigInt.asUintN(64, val);
129
+ while (val > BigInt(0x7f)) {
130
+ ++this.len;
131
+ val = val >> BigInt(0x07);
132
+ }
133
+ ++this.len;
134
+ }
135
+ }
136
+
137
+ const strlen = (str: string): number => new Blob([str]).size;