protobuf-fastdsl 0.1.3 → 0.1.6

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.
@@ -0,0 +1,947 @@
1
+ // src/ast/dependency-graph.ts
2
+ function collectDependencyClosure(registry, roots) {
3
+ const selected = /* @__PURE__ */ new Set();
4
+ function visit(name) {
5
+ if (selected.has(name)) return;
6
+ const msg = registry.get(name);
7
+ if (!msg) return;
8
+ selected.add(name);
9
+ for (const field of msg.fields) {
10
+ if (registry.has(field.typeName)) visit(field.typeName);
11
+ }
12
+ }
13
+ for (const root of roots) visit(root);
14
+ return selected;
15
+ }
16
+ function pickRegistrySubset(registry, selectedNames) {
17
+ const subset = /* @__PURE__ */ new Map();
18
+ for (const [name, msg] of registry) {
19
+ if (selectedNames.has(name)) subset.set(name, msg);
20
+ }
21
+ return subset;
22
+ }
23
+ function buildDependencyRegistry(registry, roots) {
24
+ const selected = collectDependencyClosure(registry, roots);
25
+ return pickRegistrySubset(registry, selected);
26
+ }
27
+
28
+ // src/ast/types.ts
29
+ var PRIMITIVE_TYPE_MAP = {
30
+ uint_32: { wireType: 0 /* Varint */, defaultValue: "0" },
31
+ int_32: { wireType: 0 /* Varint */, defaultValue: "0" },
32
+ uint_64: { wireType: 0 /* Varint */, defaultValue: "0" },
33
+ int_64: { wireType: 0 /* Varint */, defaultValue: "0" },
34
+ sint_32: { wireType: 0 /* Varint */, defaultValue: "0" },
35
+ sint_64: { wireType: 0 /* Varint */, defaultValue: "0" },
36
+ bool: { wireType: 0 /* Varint */, defaultValue: "false" },
37
+ string: { wireType: 2 /* LengthDelim */, defaultValue: '""' },
38
+ bytes: { wireType: 2 /* LengthDelim */, defaultValue: "new Uint8Array(0)" },
39
+ float: { wireType: 5 /* Bit32 */, defaultValue: "0" },
40
+ double: { wireType: 1 /* Bit64 */, defaultValue: "0" },
41
+ fixed_32: { wireType: 5 /* Bit32 */, defaultValue: "0" },
42
+ fixed_64: { wireType: 1 /* Bit64 */, defaultValue: "0" },
43
+ sfixed_32: { wireType: 5 /* Bit32 */, defaultValue: "0" },
44
+ sfixed_64: { wireType: 1 /* Bit64 */, defaultValue: "0" }
45
+ };
46
+ var PB_MARKER = "pb";
47
+ var PB_REPEATED_MARKER = "pb_repeated";
48
+
49
+ // src/codegen/encoder.ts
50
+ function computeTagBytes(fieldNumber, wireType) {
51
+ let value = (fieldNumber << 3 | wireType) >>> 0;
52
+ const bytes = [];
53
+ while (value > 127) {
54
+ bytes.push(value & 127 | 128);
55
+ value >>>= 7;
56
+ }
57
+ bytes.push(value & 127);
58
+ return bytes;
59
+ }
60
+ function writeTag(fieldNumber, wireType, ind) {
61
+ return computeTagBytes(fieldNumber, wireType).map((byte) => `${ind}buf[offset++] = ${byte};`).join("\n");
62
+ }
63
+ function varintSize(varName, ind) {
64
+ return `${ind}size += ${varName} < 0x80 ? 1 : ${varName} < 0x4000 ? 2 : ${varName} < 0x200000 ? 3 : ${varName} < 0x10000000 ? 4 : 5;`;
65
+ }
66
+ function writeVarint(expr, ind) {
67
+ return [
68
+ `${ind}let _v = ${expr};`,
69
+ `${ind}if (_v < 0x80) {`,
70
+ `${ind} buf[offset++] = _v;`,
71
+ `${ind}} else if (_v < 0x4000) {`,
72
+ `${ind} buf[offset++] = (_v & 0x7f) | 0x80;`,
73
+ `${ind} buf[offset++] = _v >>> 7;`,
74
+ `${ind}} else if (_v < 0x200000) {`,
75
+ `${ind} buf[offset++] = (_v & 0x7f) | 0x80;`,
76
+ `${ind} buf[offset++] = ((_v >>> 7) & 0x7f) | 0x80;`,
77
+ `${ind} buf[offset++] = _v >>> 14;`,
78
+ `${ind}} else if (_v < 0x10000000) {`,
79
+ `${ind} buf[offset++] = (_v & 0x7f) | 0x80;`,
80
+ `${ind} buf[offset++] = ((_v >>> 7) & 0x7f) | 0x80;`,
81
+ `${ind} buf[offset++] = ((_v >>> 14) & 0x7f) | 0x80;`,
82
+ `${ind} buf[offset++] = _v >>> 21;`,
83
+ `${ind}} else {`,
84
+ `${ind} buf[offset++] = (_v & 0x7f) | 0x80;`,
85
+ `${ind} buf[offset++] = ((_v >>> 7) & 0x7f) | 0x80;`,
86
+ `${ind} buf[offset++] = ((_v >>> 14) & 0x7f) | 0x80;`,
87
+ `${ind} buf[offset++] = ((_v >>> 21) & 0x7f) | 0x80;`,
88
+ `${ind} buf[offset++] = _v >>> 28;`,
89
+ `${ind}}`
90
+ ].join("\n");
91
+ }
92
+ function isVarint64(typeName) {
93
+ return typeName === "uint_64" || typeName === "int_64" || typeName === "sint_64";
94
+ }
95
+ function isFixed64BigInt(typeName) {
96
+ return typeName === "fixed_64" || typeName === "sfixed_64";
97
+ }
98
+ function bigintVarintExpr(typeName, expr) {
99
+ if (typeName === "uint_64") return `BigInt.asUintN(64, ${expr})`;
100
+ if (typeName === "int_64") return `BigInt.asUintN(64, ${expr})`;
101
+ return `__zigZagEncode64(${expr})`;
102
+ }
103
+ function generateEncoder(msg, _registry) {
104
+ const blocks = msg.fields.map((field, index) => field.isRepeated ? buildRepeatedBlock(field, index) : buildSingularBlock(field, index));
105
+ const L = [
106
+ `function protobuf_encode_${msg.name}(obj) {`,
107
+ ` let size = 0;`,
108
+ ...blocks.flatMap((block) => block.declare),
109
+ ...blocks.flatMap((block) => block.size),
110
+ ` const buf = new Uint8Array(size);`,
111
+ ` let offset = 0;`,
112
+ ...blocks.flatMap((block) => block.write),
113
+ ` return buf;`,
114
+ `}`
115
+ ];
116
+ return L.join("\n");
117
+ }
118
+ function buildSingularBlock(field, index) {
119
+ const { name, fieldNumber, typeName, wireType, isMessage } = field;
120
+ const valueVar = `_f${index}`;
121
+ const cacheVar = `_c${index}`;
122
+ const tagLength = computeTagBytes(fieldNumber, isMessage || typeName === "string" || typeName === "bytes" ? 2 : wireType).length;
123
+ if (isMessage) {
124
+ return {
125
+ declare: [` const ${valueVar} = obj.${name};`, ` let ${cacheVar};`],
126
+ size: [
127
+ ` if (${valueVar} != null) {`,
128
+ ` ${cacheVar} = protobuf_encode_${typeName}(${valueVar});`,
129
+ ` const _len = ${cacheVar}.length;`,
130
+ ` size += ${tagLength};`,
131
+ varintSize("_len", " "),
132
+ ` size += _len;`,
133
+ ` }`
134
+ ],
135
+ write: [
136
+ ` if (${valueVar} != null) {`,
137
+ ` const _data = ${cacheVar};`,
138
+ writeTag(fieldNumber, 2, " "),
139
+ ` const _len = _data.length;`,
140
+ writeVarint("_len", " "),
141
+ ` buf.set(_data, offset);`,
142
+ ` offset += _len;`,
143
+ ` }`
144
+ ]
145
+ };
146
+ }
147
+ if (typeName === "string") {
148
+ return {
149
+ declare: [` const ${valueVar} = obj.${name};`, ` let ${cacheVar};`],
150
+ size: [
151
+ ` if (${valueVar} != null && ${valueVar} !== "") {`,
152
+ ` ${cacheVar} = __utf8Len(${valueVar});`,
153
+ ` const _len = ${cacheVar};`,
154
+ ` size += ${tagLength};`,
155
+ varintSize("_len", " "),
156
+ ` size += _len;`,
157
+ ` }`
158
+ ],
159
+ write: [
160
+ ` if (${valueVar} != null && ${valueVar} !== "") {`,
161
+ writeTag(fieldNumber, 2, " "),
162
+ ` const _len = ${cacheVar};`,
163
+ writeVarint("_len", " "),
164
+ ` offset = __utf8Write(buf, offset, ${valueVar});`,
165
+ ` }`
166
+ ]
167
+ };
168
+ }
169
+ if (typeName === "bytes") {
170
+ return {
171
+ declare: [` const ${valueVar} = obj.${name};`],
172
+ size: [
173
+ ` if (${valueVar} != null && ${valueVar}.length > 0) {`,
174
+ ` const _len = ${valueVar}.length;`,
175
+ ` size += ${tagLength};`,
176
+ varintSize("_len", " "),
177
+ ` size += _len;`,
178
+ ` }`
179
+ ],
180
+ write: [
181
+ ` if (${valueVar} != null && ${valueVar}.length > 0) {`,
182
+ writeTag(fieldNumber, 2, " "),
183
+ ` const _len = ${valueVar}.length;`,
184
+ writeVarint("_len", " "),
185
+ ` buf.set(${valueVar}, offset);`,
186
+ ` offset += _len;`,
187
+ ` }`
188
+ ]
189
+ };
190
+ }
191
+ if (typeName === "bool") {
192
+ return {
193
+ declare: [` const ${valueVar} = obj.${name};`],
194
+ size: [` if (${valueVar} === true) size += ${tagLength + 1};`],
195
+ write: [
196
+ ` if (${valueVar} === true) {`,
197
+ writeTag(fieldNumber, 0, " "),
198
+ ` buf[offset++] = 1;`,
199
+ ` }`
200
+ ]
201
+ };
202
+ }
203
+ if (isVarint64(typeName)) {
204
+ const bigintExpr = bigintVarintExpr(typeName, valueVar);
205
+ return {
206
+ declare: [` const ${valueVar} = obj.${name};`],
207
+ size: [
208
+ ` if (${valueVar} != null && ${valueVar} !== 0n) {`,
209
+ ` const _val = ${bigintExpr};`,
210
+ ` size += ${tagLength};`,
211
+ ` size += __varint64Size(_val);`,
212
+ ` }`
213
+ ],
214
+ write: [
215
+ ` if (${valueVar} != null && ${valueVar} !== 0n) {`,
216
+ ` const _val = ${bigintExpr};`,
217
+ writeTag(fieldNumber, 0, " "),
218
+ ` offset = __writeVarint64(buf, offset, _val);`,
219
+ ` }`
220
+ ]
221
+ };
222
+ }
223
+ if (typeName === "sint_32") {
224
+ return {
225
+ declare: [` const ${valueVar} = obj.${name};`],
226
+ size: [
227
+ ` if (${valueVar} != null && ${valueVar} !== 0) {`,
228
+ ` const _val = ((${valueVar} << 1) ^ (${valueVar} >> 31)) >>> 0;`,
229
+ ` size += ${tagLength};`,
230
+ varintSize("_val", " "),
231
+ ` }`
232
+ ],
233
+ write: [
234
+ ` if (${valueVar} != null && ${valueVar} !== 0) {`,
235
+ writeTag(fieldNumber, 0, " "),
236
+ writeVarint(`((${valueVar} << 1) ^ (${valueVar} >> 31)) >>> 0`, " "),
237
+ ` }`
238
+ ]
239
+ };
240
+ }
241
+ if (wireType === 0 /* Varint */) {
242
+ return {
243
+ declare: [` const ${valueVar} = obj.${name};`],
244
+ size: [
245
+ ` if (${valueVar} != null && ${valueVar} !== 0) {`,
246
+ ` const _val = ${valueVar} >>> 0;`,
247
+ ` size += ${tagLength};`,
248
+ varintSize("_val", " "),
249
+ ` }`
250
+ ],
251
+ write: [
252
+ ` if (${valueVar} != null && ${valueVar} !== 0) {`,
253
+ writeTag(fieldNumber, 0, " "),
254
+ writeVarint(`${valueVar} >>> 0`, " "),
255
+ ` }`
256
+ ]
257
+ };
258
+ }
259
+ if (typeName === "float") {
260
+ return {
261
+ declare: [` const ${valueVar} = obj.${name};`],
262
+ size: [` if (${valueVar} != null && ${valueVar} !== 0) size += ${tagLength + 4};`],
263
+ write: [
264
+ ` if (${valueVar} != null && ${valueVar} !== 0) {`,
265
+ writeTag(fieldNumber, 5, " "),
266
+ ` offset = __writeFloat32(buf, offset, ${valueVar});`,
267
+ ` }`
268
+ ]
269
+ };
270
+ }
271
+ if (wireType === 5 /* Bit32 */) {
272
+ return {
273
+ declare: [` const ${valueVar} = obj.${name};`],
274
+ size: [` if (${valueVar} != null && ${valueVar} !== 0) size += ${tagLength + 4};`],
275
+ write: [
276
+ ` if (${valueVar} != null && ${valueVar} !== 0) {`,
277
+ writeTag(fieldNumber, 5, " "),
278
+ ` const _val = ${valueVar};`,
279
+ ` buf[offset++] = _val & 0xff;`,
280
+ ` buf[offset++] = (_val >> 8) & 0xff;`,
281
+ ` buf[offset++] = (_val >> 16) & 0xff;`,
282
+ ` buf[offset++] = (_val >> 24) & 0xff;`,
283
+ ` }`
284
+ ]
285
+ };
286
+ }
287
+ if (typeName === "double") {
288
+ return {
289
+ declare: [` const ${valueVar} = obj.${name};`],
290
+ size: [` if (${valueVar} != null && ${valueVar} !== 0) size += ${tagLength + 8};`],
291
+ write: [
292
+ ` if (${valueVar} != null && ${valueVar} !== 0) {`,
293
+ writeTag(fieldNumber, 1, " "),
294
+ ` offset = __writeFloat64(buf, offset, ${valueVar});`,
295
+ ` }`
296
+ ]
297
+ };
298
+ }
299
+ if (isFixed64BigInt(typeName)) {
300
+ return {
301
+ declare: [` const ${valueVar} = obj.${name};`],
302
+ size: [` if (${valueVar} != null && ${valueVar} !== 0n) size += ${tagLength + 8};`],
303
+ write: [
304
+ ` if (${valueVar} != null && ${valueVar} !== 0n) {`,
305
+ writeTag(fieldNumber, 1, " "),
306
+ ` offset = __writeFixed64(buf, offset, ${valueVar});`,
307
+ ` }`
308
+ ]
309
+ };
310
+ }
311
+ if (wireType === 1 /* Bit64 */) {
312
+ return {
313
+ declare: [` const ${valueVar} = obj.${name};`],
314
+ size: [` if (${valueVar} != null && ${valueVar} !== 0) size += ${tagLength + 8};`],
315
+ write: [
316
+ ` if (${valueVar} != null && ${valueVar} !== 0) {`,
317
+ writeTag(fieldNumber, 1, " "),
318
+ ` const _val = ${valueVar};`,
319
+ ` buf[offset++] = _val & 0xff;`,
320
+ ` buf[offset++] = (_val >> 8) & 0xff;`,
321
+ ` buf[offset++] = (_val >> 16) & 0xff;`,
322
+ ` buf[offset++] = (_val >> 24) & 0xff;`,
323
+ ` buf[offset++] = 0;`,
324
+ ` buf[offset++] = 0;`,
325
+ ` buf[offset++] = 0;`,
326
+ ` buf[offset++] = 0;`,
327
+ ` }`
328
+ ]
329
+ };
330
+ }
331
+ return { declare: [], size: [], write: [] };
332
+ }
333
+ function buildRepeatedBlock(field, index) {
334
+ const { name, fieldNumber, typeName, wireType, isMessage } = field;
335
+ const arrayVar = `_f${index}`;
336
+ const cacheVar = `_c${index}`;
337
+ const tagWireType = isMessage || typeName === "string" || typeName === "bytes" ? 2 : wireType;
338
+ const tagLength = computeTagBytes(fieldNumber, tagWireType).length;
339
+ if (isMessage) {
340
+ return {
341
+ declare: [` const ${arrayVar} = obj.${name};`, ` let ${cacheVar};`],
342
+ size: [
343
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
344
+ ` ${cacheVar} = new Array(${arrayVar}.length);`,
345
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
346
+ ` const _data = protobuf_encode_${typeName}(${arrayVar}[_i]);`,
347
+ ` ${cacheVar}[_i] = _data;`,
348
+ ` const _len = _data.length;`,
349
+ ` size += ${tagLength};`,
350
+ varintSize("_len", " "),
351
+ ` size += _len;`,
352
+ ` }`,
353
+ ` }`
354
+ ],
355
+ write: [
356
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
357
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
358
+ ` const _data = ${cacheVar}[_i];`,
359
+ writeTag(fieldNumber, 2, " "),
360
+ ` const _len = _data.length;`,
361
+ writeVarint("_len", " "),
362
+ ` buf.set(_data, offset);`,
363
+ ` offset += _len;`,
364
+ ` }`,
365
+ ` }`
366
+ ]
367
+ };
368
+ }
369
+ if (typeName === "string") {
370
+ return {
371
+ declare: [` const ${arrayVar} = obj.${name};`, ` let ${cacheVar};`],
372
+ size: [
373
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
374
+ ` ${cacheVar} = new Array(${arrayVar}.length);`,
375
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
376
+ ` const _len = __utf8Len(${arrayVar}[_i]);`,
377
+ ` ${cacheVar}[_i] = _len;`,
378
+ ` size += ${tagLength};`,
379
+ varintSize("_len", " "),
380
+ ` size += _len;`,
381
+ ` }`,
382
+ ` }`
383
+ ],
384
+ write: [
385
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
386
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
387
+ ` const _len = ${cacheVar}[_i];`,
388
+ writeTag(fieldNumber, 2, " "),
389
+ writeVarint("_len", " "),
390
+ ` offset = __utf8Write(buf, offset, ${arrayVar}[_i]);`,
391
+ ` }`,
392
+ ` }`
393
+ ]
394
+ };
395
+ }
396
+ if (typeName === "bytes") {
397
+ return {
398
+ declare: [` const ${arrayVar} = obj.${name};`],
399
+ size: [
400
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
401
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
402
+ ` const _data = ${arrayVar}[_i];`,
403
+ ` const _len = _data.length;`,
404
+ ` size += ${tagLength};`,
405
+ varintSize("_len", " "),
406
+ ` size += _len;`,
407
+ ` }`,
408
+ ` }`
409
+ ],
410
+ write: [
411
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
412
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
413
+ ` const _data = ${arrayVar}[_i];`,
414
+ writeTag(fieldNumber, 2, " "),
415
+ ` const _len = _data.length;`,
416
+ writeVarint("_len", " "),
417
+ ` buf.set(_data, offset);`,
418
+ ` offset += _len;`,
419
+ ` }`,
420
+ ` }`
421
+ ]
422
+ };
423
+ }
424
+ if (typeName === "bool") {
425
+ return {
426
+ declare: [` const ${arrayVar} = obj.${name};`],
427
+ size: [` if (${arrayVar} != null && ${arrayVar}.length > 0) size += ${arrayVar}.length * ${tagLength + 1};`],
428
+ write: [
429
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
430
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
431
+ writeTag(fieldNumber, 0, " "),
432
+ ` buf[offset++] = ${arrayVar}[_i] ? 1 : 0;`,
433
+ ` }`,
434
+ ` }`
435
+ ]
436
+ };
437
+ }
438
+ if (isVarint64(typeName)) {
439
+ const bigintExpr = bigintVarintExpr(typeName, `${arrayVar}[_i]`);
440
+ return {
441
+ declare: [` const ${arrayVar} = obj.${name};`],
442
+ size: [
443
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
444
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
445
+ ` const _val = ${bigintExpr};`,
446
+ ` size += ${tagLength};`,
447
+ ` size += __varint64Size(_val);`,
448
+ ` }`,
449
+ ` }`
450
+ ],
451
+ write: [
452
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
453
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
454
+ ` const _val = ${bigintExpr};`,
455
+ writeTag(fieldNumber, 0, " "),
456
+ ` offset = __writeVarint64(buf, offset, _val);`,
457
+ ` }`,
458
+ ` }`
459
+ ]
460
+ };
461
+ }
462
+ if (typeName === "sint_32") {
463
+ return {
464
+ declare: [` const ${arrayVar} = obj.${name};`],
465
+ size: [
466
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
467
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
468
+ ` const _val = ((${arrayVar}[_i] << 1) ^ (${arrayVar}[_i] >> 31)) >>> 0;`,
469
+ ` size += ${tagLength};`,
470
+ varintSize("_val", " "),
471
+ ` }`,
472
+ ` }`
473
+ ],
474
+ write: [
475
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
476
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
477
+ writeTag(fieldNumber, 0, " "),
478
+ writeVarint(`((${arrayVar}[_i] << 1) ^ (${arrayVar}[_i] >> 31)) >>> 0`, " "),
479
+ ` }`,
480
+ ` }`
481
+ ]
482
+ };
483
+ }
484
+ if (wireType === 0 /* Varint */) {
485
+ return {
486
+ declare: [` const ${arrayVar} = obj.${name};`],
487
+ size: [
488
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
489
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
490
+ ` const _val = ${arrayVar}[_i] >>> 0;`,
491
+ ` size += ${tagLength};`,
492
+ varintSize("_val", " "),
493
+ ` }`,
494
+ ` }`
495
+ ],
496
+ write: [
497
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
498
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
499
+ writeTag(fieldNumber, 0, " "),
500
+ writeVarint(`${arrayVar}[_i] >>> 0`, " "),
501
+ ` }`,
502
+ ` }`
503
+ ]
504
+ };
505
+ }
506
+ if (typeName === "float") {
507
+ return {
508
+ declare: [` const ${arrayVar} = obj.${name};`],
509
+ size: [` if (${arrayVar} != null && ${arrayVar}.length > 0) size += ${arrayVar}.length * ${tagLength + 4};`],
510
+ write: [
511
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
512
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
513
+ writeTag(fieldNumber, 5, " "),
514
+ ` offset = __writeFloat32(buf, offset, ${arrayVar}[_i]);`,
515
+ ` }`,
516
+ ` }`
517
+ ]
518
+ };
519
+ }
520
+ if (wireType === 5 /* Bit32 */) {
521
+ return {
522
+ declare: [` const ${arrayVar} = obj.${name};`],
523
+ size: [` if (${arrayVar} != null && ${arrayVar}.length > 0) size += ${arrayVar}.length * ${tagLength + 4};`],
524
+ write: [
525
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
526
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
527
+ writeTag(fieldNumber, 5, " "),
528
+ ` const _val = ${arrayVar}[_i];`,
529
+ ` buf[offset++] = _val & 0xff;`,
530
+ ` buf[offset++] = (_val >> 8) & 0xff;`,
531
+ ` buf[offset++] = (_val >> 16) & 0xff;`,
532
+ ` buf[offset++] = (_val >> 24) & 0xff;`,
533
+ ` }`,
534
+ ` }`
535
+ ]
536
+ };
537
+ }
538
+ if (typeName === "double") {
539
+ return {
540
+ declare: [` const ${arrayVar} = obj.${name};`],
541
+ size: [` if (${arrayVar} != null && ${arrayVar}.length > 0) size += ${arrayVar}.length * ${tagLength + 8};`],
542
+ write: [
543
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
544
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
545
+ writeTag(fieldNumber, 1, " "),
546
+ ` offset = __writeFloat64(buf, offset, ${arrayVar}[_i]);`,
547
+ ` }`,
548
+ ` }`
549
+ ]
550
+ };
551
+ }
552
+ if (isFixed64BigInt(typeName)) {
553
+ return {
554
+ declare: [` const ${arrayVar} = obj.${name};`],
555
+ size: [` if (${arrayVar} != null && ${arrayVar}.length > 0) size += ${arrayVar}.length * ${tagLength + 8};`],
556
+ write: [
557
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
558
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
559
+ writeTag(fieldNumber, 1, " "),
560
+ ` offset = __writeFixed64(buf, offset, ${arrayVar}[_i]);`,
561
+ ` }`,
562
+ ` }`
563
+ ]
564
+ };
565
+ }
566
+ if (wireType === 1 /* Bit64 */) {
567
+ return {
568
+ declare: [` const ${arrayVar} = obj.${name};`],
569
+ size: [` if (${arrayVar} != null && ${arrayVar}.length > 0) size += ${arrayVar}.length * ${tagLength + 8};`],
570
+ write: [
571
+ ` if (${arrayVar} != null && ${arrayVar}.length > 0) {`,
572
+ ` for (let _i = 0; _i < ${arrayVar}.length; _i++) {`,
573
+ writeTag(fieldNumber, 1, " "),
574
+ ` const _val = ${arrayVar}[_i];`,
575
+ ` buf[offset++] = _val & 0xff;`,
576
+ ` buf[offset++] = (_val >> 8) & 0xff;`,
577
+ ` buf[offset++] = (_val >> 16) & 0xff;`,
578
+ ` buf[offset++] = (_val >> 24) & 0xff;`,
579
+ ` buf[offset++] = 0;`,
580
+ ` buf[offset++] = 0;`,
581
+ ` buf[offset++] = 0;`,
582
+ ` buf[offset++] = 0;`,
583
+ ` }`,
584
+ ` }`
585
+ ]
586
+ };
587
+ }
588
+ return { declare: [], size: [], write: [] };
589
+ }
590
+
591
+ // src/codegen/decoder.ts
592
+ function tagValue(field) {
593
+ const wireType = field.isMessage || field.typeName === "string" || field.typeName === "bytes" ? 2 /* LengthDelim */ : field.wireType;
594
+ return (field.fieldNumber << 3 | wireType) >>> 0;
595
+ }
596
+ function varintDec(varName, ind) {
597
+ return [
598
+ `${ind}let ${varName} = data[offset++];`,
599
+ `${ind}if (${varName} & 0x80) {`,
600
+ `${ind} let _s = 7, _b;`,
601
+ `${ind} ${varName} &= 0x7f;`,
602
+ `${ind} do { _b = data[offset++]; ${varName} |= (_b & 0x7f) << _s; _s += 7; } while (_b & 0x80);`,
603
+ `${ind}}`
604
+ ].join("\n");
605
+ }
606
+ function varintDec64(varName, ind) {
607
+ return [
608
+ `${ind}let ${varName} = 0n, _s = 0n, _b;`,
609
+ `${ind}do { _b = data[offset++]; ${varName} |= BigInt(_b & 0x7f) << _s; _s += 7n; } while (_b & 0x80);`
610
+ ].join("\n");
611
+ }
612
+ function isVarint642(typeName) {
613
+ return typeName === "uint_64" || typeName === "int_64" || typeName === "sint_64";
614
+ }
615
+ function isFixed64BigInt2(typeName) {
616
+ return typeName === "fixed_64" || typeName === "sfixed_64";
617
+ }
618
+ var INLINE_SKIP = [
619
+ ` const wireType = _tag & 0x7;`,
620
+ ` if (wireType === 0) { while (data[offset] & 0x80) offset++; offset++; }`,
621
+ ` else if (wireType === 1) offset += 8;`,
622
+ ` else if (wireType === 2) { let _l = data[offset++]; if (_l & 0x80) { let _s = 7, _b; _l &= 0x7f; do { _b = data[offset++]; _l |= (_b & 0x7f) << _s; _s += 7; } while (_b & 0x80); } offset += _l; }`,
623
+ ` else if (wireType === 5) offset += 4;`
624
+ ].join("\n");
625
+ function generateDecoder(msg, _registry) {
626
+ const locals = msg.fields.map((field, index) => {
627
+ const keyword = field.isRepeated ? "const" : "let";
628
+ return ` ${keyword} _f${index} = ${getDefault(field)};`;
629
+ });
630
+ const result = msg.fields.map((field, index) => `${field.name}: _f${index}`).join(", ");
631
+ const L = [
632
+ `function protobuf_decode_${msg.name}(data, offset = 0, end = data.length) {`,
633
+ ...locals,
634
+ ` while (offset < end) {`,
635
+ ` let _tag = data[offset++];`,
636
+ ` if (_tag & 0x80) {`,
637
+ ` let _ts = 7, _tb;`,
638
+ ` _tag &= 0x7f;`,
639
+ ` do { _tb = data[offset++]; _tag |= (_tb & 0x7f) << _ts; _ts += 7; } while (_tb & 0x80);`,
640
+ ` }`,
641
+ ` switch (_tag) {`
642
+ ];
643
+ msg.fields.forEach((field, index) => {
644
+ L.push(decodeField(field, index));
645
+ });
646
+ L.push(
647
+ ` default: {`,
648
+ INLINE_SKIP,
649
+ ` break;`,
650
+ ` }`,
651
+ ` }`,
652
+ ` }`,
653
+ ` return { ${result} };`,
654
+ `}`
655
+ );
656
+ return L.join("\n");
657
+ }
658
+ function decodeField(field, index) {
659
+ const { typeName, wireType, isMessage, isRepeated } = field;
660
+ const I = " ";
661
+ const local = `_f${index}`;
662
+ const assign = (expr) => isRepeated ? `${I}${local}.push(${expr});` : `${I}${local} = ${expr};`;
663
+ const L = [` case ${tagValue(field)}: {`];
664
+ if (isMessage) {
665
+ L.push(varintDec("_len", I));
666
+ L.push(assign(`protobuf_decode_${typeName}(data, offset, offset + _len)`));
667
+ L.push(`${I}offset += _len;`);
668
+ } else if (typeName === "string") {
669
+ L.push(varintDec("_len", I));
670
+ L.push(`${I}const _end = offset + _len;`);
671
+ L.push(assign(`__td.decode(data.subarray(offset, _end))`));
672
+ L.push(`${I}offset = _end;`);
673
+ } else if (typeName === "bytes") {
674
+ L.push(varintDec("_len", I));
675
+ L.push(`${I}const _end = offset + _len;`);
676
+ L.push(assign(`data.slice(offset, _end)`));
677
+ L.push(`${I}offset = _end;`);
678
+ } else if (typeName === "bool") {
679
+ L.push(varintDec("_val", I));
680
+ L.push(assign(`_val !== 0`));
681
+ } else if (isVarint642(typeName)) {
682
+ L.push(varintDec64("_val", I));
683
+ if (typeName === "uint_64") {
684
+ L.push(assign(`_val`));
685
+ } else if (typeName === "int_64") {
686
+ L.push(assign(`BigInt.asIntN(64, _val)`));
687
+ } else {
688
+ L.push(assign(`__zigZagDecode64(_val)`));
689
+ }
690
+ } else if (typeName === "sint_32") {
691
+ L.push(varintDec("_val", I));
692
+ L.push(assign(`(_val >>> 1) ^ -(_val & 1)`));
693
+ } else if (wireType === 0 /* Varint */) {
694
+ L.push(varintDec("_val", I));
695
+ L.push(assign(`_val >>> 0`));
696
+ } else if (typeName === "float") {
697
+ L.push(assign(`__readFloat32(data, offset)`));
698
+ L.push(`${I}offset += 4;`);
699
+ } else if (wireType === 5 /* Bit32 */) {
700
+ L.push(assign(`data[offset] | (data[offset + 1] << 8) | (data[offset + 2] << 16) | (data[offset + 3] << 24)`));
701
+ L.push(`${I}offset += 4;`);
702
+ } else if (typeName === "double") {
703
+ L.push(assign(`__readFloat64(data, offset)`));
704
+ L.push(`${I}offset += 8;`);
705
+ } else if (isFixed64BigInt2(typeName)) {
706
+ if (typeName === "fixed_64") {
707
+ L.push(assign(`__readFixed64(data, offset)`));
708
+ } else {
709
+ L.push(assign(`BigInt.asIntN(64, __readFixed64(data, offset))`));
710
+ }
711
+ L.push(`${I}offset += 8;`);
712
+ } else if (wireType === 1 /* Bit64 */) {
713
+ L.push(assign(`data[offset] | (data[offset + 1] << 8) | (data[offset + 2] << 16) | (data[offset + 3] << 24)`));
714
+ L.push(`${I}offset += 8;`);
715
+ }
716
+ L.push(`${I}break;`, ` }`);
717
+ return L.join("\n");
718
+ }
719
+ function getDefault(field) {
720
+ if (field.isRepeated) return "[]";
721
+ if (field.isOptional || field.isMessage) return "null";
722
+ const primitive = PRIMITIVE_TYPE_MAP[field.typeName];
723
+ return primitive ? primitive.defaultValue : "null";
724
+ }
725
+
726
+ // src/codegen/generator.ts
727
+ var CODEGEN_PREAMBLE = `const __td = new TextDecoder();
728
+ const __scratch = new DataView(new ArrayBuffer(8));
729
+ function __utf8Len(value) {
730
+ let length = 0;
731
+ for (let i = 0; i < value.length; i++) {
732
+ const code = value.charCodeAt(i);
733
+ if (code < 0x80) {
734
+ length++;
735
+ continue;
736
+ }
737
+ if (code < 0x800) {
738
+ length += 2;
739
+ continue;
740
+ }
741
+ if ((code & 0xfc00) === 0xd800) {
742
+ if (i + 1 < value.length) {
743
+ const next = value.charCodeAt(i + 1);
744
+ if ((next & 0xfc00) === 0xdc00) {
745
+ length += 4;
746
+ i++;
747
+ continue;
748
+ }
749
+ }
750
+ length += 3;
751
+ continue;
752
+ }
753
+ if ((code & 0xfc00) === 0xdc00) {
754
+ length += 3;
755
+ continue;
756
+ }
757
+ length += 3;
758
+ }
759
+ return length;
760
+ }
761
+ function __utf8Write(buf, offset, value) {
762
+ for (let i = 0; i < value.length; i++) {
763
+ let code = value.charCodeAt(i);
764
+ if (code < 0x80) {
765
+ buf[offset++] = code;
766
+ continue;
767
+ }
768
+ if (code < 0x800) {
769
+ buf[offset++] = 0xc0 | (code >> 6);
770
+ buf[offset++] = 0x80 | (code & 0x3f);
771
+ continue;
772
+ }
773
+ if ((code & 0xfc00) === 0xd800) {
774
+ if (i + 1 < value.length) {
775
+ const next = value.charCodeAt(i + 1);
776
+ if ((next & 0xfc00) === 0xdc00) {
777
+ const point = ((code - 0xd800) << 10) + (next - 0xdc00) + 0x10000;
778
+ buf[offset++] = 0xf0 | (point >> 18);
779
+ buf[offset++] = 0x80 | ((point >> 12) & 0x3f);
780
+ buf[offset++] = 0x80 | ((point >> 6) & 0x3f);
781
+ buf[offset++] = 0x80 | (point & 0x3f);
782
+ i++;
783
+ continue;
784
+ }
785
+ }
786
+ code = 0xfffd;
787
+ } else if ((code & 0xfc00) === 0xdc00) {
788
+ code = 0xfffd;
789
+ }
790
+ buf[offset++] = 0xe0 | (code >> 12);
791
+ buf[offset++] = 0x80 | ((code >> 6) & 0x3f);
792
+ buf[offset++] = 0x80 | (code & 0x3f);
793
+ }
794
+ return offset;
795
+ }
796
+ function __varint64Size(value) {
797
+ let size = 1;
798
+ while (value > 0x7fn) {
799
+ value >>= 7n;
800
+ size++;
801
+ }
802
+ return size;
803
+ }
804
+ function __writeVarint64(buf, offset, value) {
805
+ while (value > 0x7fn) {
806
+ buf[offset++] = Number((value & 0x7fn) | 0x80n);
807
+ value >>= 7n;
808
+ }
809
+ buf[offset++] = Number(value);
810
+ return offset;
811
+ }
812
+ function __zigZagEncode64(value) {
813
+ value = BigInt.asIntN(64, value);
814
+ return BigInt.asUintN(64, (value << 1n) ^ (value >> 63n));
815
+ }
816
+ function __zigZagDecode64(value) {
817
+ return BigInt.asIntN(64, (value >> 1n) ^ -(value & 1n));
818
+ }
819
+ function __writeFloat32(buf, offset, value) {
820
+ __scratch.setFloat32(0, value, true);
821
+ buf[offset++] = __scratch.getUint8(0);
822
+ buf[offset++] = __scratch.getUint8(1);
823
+ buf[offset++] = __scratch.getUint8(2);
824
+ buf[offset++] = __scratch.getUint8(3);
825
+ return offset;
826
+ }
827
+ function __readFloat32(data, offset) {
828
+ __scratch.setUint8(0, data[offset]);
829
+ __scratch.setUint8(1, data[offset + 1]);
830
+ __scratch.setUint8(2, data[offset + 2]);
831
+ __scratch.setUint8(3, data[offset + 3]);
832
+ return __scratch.getFloat32(0, true);
833
+ }
834
+ function __writeFloat64(buf, offset, value) {
835
+ __scratch.setFloat64(0, value, true);
836
+ buf[offset++] = __scratch.getUint8(0);
837
+ buf[offset++] = __scratch.getUint8(1);
838
+ buf[offset++] = __scratch.getUint8(2);
839
+ buf[offset++] = __scratch.getUint8(3);
840
+ buf[offset++] = __scratch.getUint8(4);
841
+ buf[offset++] = __scratch.getUint8(5);
842
+ buf[offset++] = __scratch.getUint8(6);
843
+ buf[offset++] = __scratch.getUint8(7);
844
+ return offset;
845
+ }
846
+ function __readFloat64(data, offset) {
847
+ __scratch.setUint8(0, data[offset]);
848
+ __scratch.setUint8(1, data[offset + 1]);
849
+ __scratch.setUint8(2, data[offset + 2]);
850
+ __scratch.setUint8(3, data[offset + 3]);
851
+ __scratch.setUint8(4, data[offset + 4]);
852
+ __scratch.setUint8(5, data[offset + 5]);
853
+ __scratch.setUint8(6, data[offset + 6]);
854
+ __scratch.setUint8(7, data[offset + 7]);
855
+ return __scratch.getFloat64(0, true);
856
+ }
857
+ function __writeFixed64(buf, offset, value) {
858
+ value = BigInt.asUintN(64, value);
859
+ buf[offset++] = Number(value & 0xffn);
860
+ buf[offset++] = Number((value >> 8n) & 0xffn);
861
+ buf[offset++] = Number((value >> 16n) & 0xffn);
862
+ buf[offset++] = Number((value >> 24n) & 0xffn);
863
+ buf[offset++] = Number((value >> 32n) & 0xffn);
864
+ buf[offset++] = Number((value >> 40n) & 0xffn);
865
+ buf[offset++] = Number((value >> 48n) & 0xffn);
866
+ buf[offset++] = Number((value >> 56n) & 0xffn);
867
+ return offset;
868
+ }
869
+ function __readFixed64(data, offset) {
870
+ return BigInt(data[offset])
871
+ | (BigInt(data[offset + 1]) << 8n)
872
+ | (BigInt(data[offset + 2]) << 16n)
873
+ | (BigInt(data[offset + 3]) << 24n)
874
+ | (BigInt(data[offset + 4]) << 32n)
875
+ | (BigInt(data[offset + 5]) << 40n)
876
+ | (BigInt(data[offset + 6]) << 48n)
877
+ | (BigInt(data[offset + 7]) << 56n);
878
+ }`;
879
+ function generateCode(registry) {
880
+ if (registry.size === 0) return "";
881
+ const parts = [CODEGEN_PREAMBLE];
882
+ for (const msg of registry.values()) {
883
+ parts.push(generateEncoder(msg, registry));
884
+ parts.push(generateDecoder(msg, registry));
885
+ }
886
+ return parts.join("\n");
887
+ }
888
+
889
+ // src/runtime-map.ts
890
+ function cloneField(field) {
891
+ return {
892
+ name: field.name,
893
+ fieldNumber: field.fieldNumber,
894
+ typeName: field.typeName,
895
+ wireType: field.wireType,
896
+ isMessage: field.isMessage,
897
+ isOptional: field.isOptional,
898
+ isRepeated: field.isRepeated
899
+ };
900
+ }
901
+ function cloneMessage(msg) {
902
+ return {
903
+ name: msg.name,
904
+ fields: msg.fields.map(cloneField)
905
+ };
906
+ }
907
+ function registryToRuntimeMapMessages(registry) {
908
+ return [...registry.values()].map(cloneMessage);
909
+ }
910
+ function runtimeMapToRegistry(map) {
911
+ const registry = /* @__PURE__ */ new Map();
912
+ for (const msg of map.messages) {
913
+ registry.set(msg.name, cloneMessage(msg));
914
+ }
915
+ return registry;
916
+ }
917
+ function normalizeRuntimeMapPath(filePath) {
918
+ const noQuery = filePath.replace(/[?#].*$/, "");
919
+ return noQuery.replace(/\\/g, "/").replace(/^file:\/\//, "");
920
+ }
921
+ function createRuntimeMap(map) {
922
+ const dedupedCallSites = /* @__PURE__ */ new Map();
923
+ for (const cs of map.callSites) {
924
+ const normalized = {
925
+ ...cs,
926
+ file: normalizeRuntimeMapPath(cs.file)
927
+ };
928
+ const key = `${normalized.file}:${normalized.line}:${normalized.column}:${normalized.fnName}:${normalized.typeName}`;
929
+ dedupedCallSites.set(key, normalized);
930
+ }
931
+ return {
932
+ version: 1,
933
+ messages: registryToRuntimeMapMessages(map.messages),
934
+ callSites: [...dedupedCallSites.values()]
935
+ };
936
+ }
937
+
938
+ export {
939
+ PRIMITIVE_TYPE_MAP,
940
+ PB_MARKER,
941
+ PB_REPEATED_MARKER,
942
+ buildDependencyRegistry,
943
+ generateCode,
944
+ runtimeMapToRegistry,
945
+ normalizeRuntimeMapPath,
946
+ createRuntimeMap
947
+ };