@naeemo/capnp 0.1.0 → 0.2.0
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 -2
- package/README.zh.md +9 -2
- package/dist/codegen/cli-v3.js +1676 -0
- package/dist/codegen/cli-v3.js.map +1 -0
- package/dist/index.cjs +107 -27
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +107 -27
- package/dist/index.js.map +1 -1
- package/package.json +8 -3
|
@@ -0,0 +1,1676 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { execSync } from "node:child_process";
|
|
3
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { dirname, join } from "node:path";
|
|
6
|
+
import { parseArgs } from "node:util";
|
|
7
|
+
|
|
8
|
+
//#region src/core/pointer.ts
|
|
9
|
+
/**
|
|
10
|
+
* Cap'n Proto 指针编解码
|
|
11
|
+
* 纯 TypeScript 实现
|
|
12
|
+
*/
|
|
13
|
+
let PointerTag = /* @__PURE__ */ function(PointerTag) {
|
|
14
|
+
PointerTag[PointerTag["STRUCT"] = 0] = "STRUCT";
|
|
15
|
+
PointerTag[PointerTag["LIST"] = 1] = "LIST";
|
|
16
|
+
PointerTag[PointerTag["FAR"] = 2] = "FAR";
|
|
17
|
+
PointerTag[PointerTag["OTHER"] = 3] = "OTHER";
|
|
18
|
+
return PointerTag;
|
|
19
|
+
}({});
|
|
20
|
+
let ElementSize = /* @__PURE__ */ function(ElementSize) {
|
|
21
|
+
ElementSize[ElementSize["VOID"] = 0] = "VOID";
|
|
22
|
+
ElementSize[ElementSize["BIT"] = 1] = "BIT";
|
|
23
|
+
ElementSize[ElementSize["BYTE"] = 2] = "BYTE";
|
|
24
|
+
ElementSize[ElementSize["TWO_BYTES"] = 3] = "TWO_BYTES";
|
|
25
|
+
ElementSize[ElementSize["FOUR_BYTES"] = 4] = "FOUR_BYTES";
|
|
26
|
+
ElementSize[ElementSize["EIGHT_BYTES"] = 5] = "EIGHT_BYTES";
|
|
27
|
+
ElementSize[ElementSize["POINTER"] = 6] = "POINTER";
|
|
28
|
+
ElementSize[ElementSize["COMPOSITE"] = 7] = "COMPOSITE";
|
|
29
|
+
ElementSize[ElementSize["INLINE_COMPOSITE"] = 7] = "INLINE_COMPOSITE";
|
|
30
|
+
return ElementSize;
|
|
31
|
+
}({});
|
|
32
|
+
/**
|
|
33
|
+
* 解码指针(64位)
|
|
34
|
+
*/
|
|
35
|
+
function decodePointer(ptr) {
|
|
36
|
+
const tag = Number(ptr & BigInt(3));
|
|
37
|
+
switch (tag) {
|
|
38
|
+
case PointerTag.STRUCT: {
|
|
39
|
+
const offset = Number(ptr >> BigInt(2)) & 1073741823;
|
|
40
|
+
return {
|
|
41
|
+
tag,
|
|
42
|
+
offset: offset >= 536870912 ? offset - 1073741824 : offset,
|
|
43
|
+
dataWords: Number(ptr >> BigInt(32) & BigInt(65535)),
|
|
44
|
+
pointerCount: Number(ptr >> BigInt(48) & BigInt(65535))
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
case PointerTag.LIST: {
|
|
48
|
+
const offset = Number(ptr >> BigInt(2)) & 1073741823;
|
|
49
|
+
return {
|
|
50
|
+
tag,
|
|
51
|
+
offset: offset >= 536870912 ? offset - 1073741824 : offset,
|
|
52
|
+
elementSize: Number(ptr >> BigInt(32) & BigInt(7)),
|
|
53
|
+
elementCount: Number(ptr >> BigInt(35) & BigInt(536870911))
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
case PointerTag.FAR: {
|
|
57
|
+
const doubleFar = Boolean(ptr >> BigInt(2) & BigInt(1));
|
|
58
|
+
const targetOffset = Number(ptr >> BigInt(3) & BigInt(536870911));
|
|
59
|
+
return {
|
|
60
|
+
tag,
|
|
61
|
+
doubleFar,
|
|
62
|
+
targetSegment: Number(ptr >> BigInt(32) & BigInt(4294967295)),
|
|
63
|
+
targetOffset
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
default: return { tag: PointerTag.OTHER };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/core/segment.ts
|
|
72
|
+
/**
|
|
73
|
+
* Cap'n Proto Segment 管理
|
|
74
|
+
* 纯 TypeScript 实现
|
|
75
|
+
*/
|
|
76
|
+
const WORD_SIZE = 8;
|
|
77
|
+
var Segment = class Segment {
|
|
78
|
+
buffer;
|
|
79
|
+
view;
|
|
80
|
+
_size;
|
|
81
|
+
constructor(initialCapacity = 1024) {
|
|
82
|
+
this.buffer = new ArrayBuffer(initialCapacity);
|
|
83
|
+
this.view = new DataView(this.buffer);
|
|
84
|
+
this._size = 0;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* 从现有 buffer 创建(用于读取)
|
|
88
|
+
*/
|
|
89
|
+
static fromBuffer(buffer) {
|
|
90
|
+
const seg = new Segment(0);
|
|
91
|
+
seg.buffer = buffer;
|
|
92
|
+
seg.view = new DataView(buffer);
|
|
93
|
+
seg._size = buffer.byteLength;
|
|
94
|
+
return seg;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 确保容量足够
|
|
98
|
+
*/
|
|
99
|
+
ensureCapacity(minBytes) {
|
|
100
|
+
if (this.buffer.byteLength >= minBytes) return;
|
|
101
|
+
let newCapacity = this.buffer.byteLength * 2;
|
|
102
|
+
while (newCapacity < minBytes) newCapacity *= 2;
|
|
103
|
+
const newBuffer = new ArrayBuffer(newCapacity);
|
|
104
|
+
new Uint8Array(newBuffer).set(new Uint8Array(this.buffer, 0, this._size));
|
|
105
|
+
this.buffer = newBuffer;
|
|
106
|
+
this.view = new DataView(newBuffer);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 分配空间,返回字偏移
|
|
110
|
+
*/
|
|
111
|
+
allocate(words) {
|
|
112
|
+
const bytes = words * WORD_SIZE;
|
|
113
|
+
const offset = this._size;
|
|
114
|
+
this.ensureCapacity(offset + bytes);
|
|
115
|
+
this._size = offset + bytes;
|
|
116
|
+
return offset / WORD_SIZE;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* 获取字(64位)
|
|
120
|
+
*/
|
|
121
|
+
getWord(wordOffset) {
|
|
122
|
+
const byteOffset = wordOffset * WORD_SIZE;
|
|
123
|
+
const low = BigInt(this.view.getUint32(byteOffset, true));
|
|
124
|
+
return BigInt(this.view.getUint32(byteOffset + 4, true)) << BigInt(32) | low;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* 设置字(64位)
|
|
128
|
+
*/
|
|
129
|
+
setWord(wordOffset, value) {
|
|
130
|
+
const byteOffset = wordOffset * WORD_SIZE;
|
|
131
|
+
this.view.setUint32(byteOffset, Number(value & BigInt(4294967295)), true);
|
|
132
|
+
this.view.setUint32(byteOffset + 4, Number(value >> BigInt(32)), true);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 获取原始 buffer(只读到 _size)
|
|
136
|
+
*/
|
|
137
|
+
asUint8Array() {
|
|
138
|
+
return new Uint8Array(this.buffer, 0, this._size);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* 获取字数量
|
|
142
|
+
*/
|
|
143
|
+
get wordCount() {
|
|
144
|
+
return this._size / WORD_SIZE;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* 获取字节数量
|
|
148
|
+
*/
|
|
149
|
+
get byteLength() {
|
|
150
|
+
return this._size;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* 获取 DataView
|
|
154
|
+
*/
|
|
155
|
+
get dataView() {
|
|
156
|
+
return this.view;
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/core/list.ts
|
|
162
|
+
/**
|
|
163
|
+
* ListReader - 读取列表
|
|
164
|
+
*/
|
|
165
|
+
var ListReader = class {
|
|
166
|
+
segment;
|
|
167
|
+
startOffset;
|
|
168
|
+
segmentIndex;
|
|
169
|
+
constructor(message, segmentIndex, elementSize, elementCount, structSize, wordOffset) {
|
|
170
|
+
this.message = message;
|
|
171
|
+
this.elementSize = elementSize;
|
|
172
|
+
this.elementCount = elementCount;
|
|
173
|
+
this.structSize = structSize;
|
|
174
|
+
this.segmentIndex = segmentIndex;
|
|
175
|
+
this.segment = message.getSegment(segmentIndex);
|
|
176
|
+
this.startOffset = wordOffset;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* 列表长度
|
|
180
|
+
*/
|
|
181
|
+
get length() {
|
|
182
|
+
return this.elementCount;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 获取元素(基础类型)
|
|
186
|
+
*/
|
|
187
|
+
getPrimitive(index) {
|
|
188
|
+
if (index < 0 || index >= this.elementCount) throw new RangeError("Index out of bounds");
|
|
189
|
+
switch (this.elementSize) {
|
|
190
|
+
case ElementSize.BIT: {
|
|
191
|
+
const byteOffset = Math.floor(index / 8);
|
|
192
|
+
const bitInByte = index % 8;
|
|
193
|
+
return (this.segment.dataView.getUint8(this.startOffset * WORD_SIZE + byteOffset) & 1 << bitInByte) !== 0 ? 1 : 0;
|
|
194
|
+
}
|
|
195
|
+
case ElementSize.BYTE: return this.segment.dataView.getUint8(this.startOffset * WORD_SIZE + index);
|
|
196
|
+
case ElementSize.TWO_BYTES: return this.segment.dataView.getUint16(this.startOffset * WORD_SIZE + index * 2, true);
|
|
197
|
+
case ElementSize.FOUR_BYTES: return this.segment.dataView.getUint32(this.startOffset * WORD_SIZE + index * 4, true);
|
|
198
|
+
case ElementSize.EIGHT_BYTES: {
|
|
199
|
+
const offset = this.startOffset * WORD_SIZE + index * 8;
|
|
200
|
+
const low = BigInt(this.segment.dataView.getUint32(offset, true));
|
|
201
|
+
return BigInt(this.segment.dataView.getUint32(offset + 4, true)) << BigInt(32) | low;
|
|
202
|
+
}
|
|
203
|
+
default: throw new Error(`Unsupported element size: ${this.elementSize}`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* 获取结构元素
|
|
208
|
+
*/
|
|
209
|
+
getStruct(index) {
|
|
210
|
+
if (!this.structSize) throw new Error("Not a struct list");
|
|
211
|
+
const { dataWords, pointerCount } = this.structSize;
|
|
212
|
+
const size = dataWords + pointerCount;
|
|
213
|
+
const offset = this.startOffset + index * size;
|
|
214
|
+
return new StructReader(this.message, this.segmentIndex, offset, dataWords, pointerCount);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* 迭代器
|
|
218
|
+
*/
|
|
219
|
+
*[Symbol.iterator]() {
|
|
220
|
+
for (let i = 0; i < this.elementCount; i++) yield this.getPrimitive(i);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
//#endregion
|
|
225
|
+
//#region src/core/message-reader.ts
|
|
226
|
+
/**
|
|
227
|
+
* Cap'n Proto MessageReader
|
|
228
|
+
* 纯 TypeScript 实现
|
|
229
|
+
*/
|
|
230
|
+
var MessageReader = class {
|
|
231
|
+
segments;
|
|
232
|
+
constructor(buffer) {
|
|
233
|
+
const uint8Array = buffer instanceof ArrayBuffer ? new Uint8Array(buffer) : buffer;
|
|
234
|
+
this.segments = [];
|
|
235
|
+
if (uint8Array.byteLength < 8) return;
|
|
236
|
+
const view = new DataView(uint8Array.buffer, uint8Array.byteOffset, uint8Array.byteLength);
|
|
237
|
+
const firstWordLow = view.getUint32(0, true);
|
|
238
|
+
const firstWordHigh = view.getUint32(4, true);
|
|
239
|
+
const segmentCount = (firstWordLow & 4294967295) + 1;
|
|
240
|
+
const firstSegmentSize = firstWordHigh;
|
|
241
|
+
let offset = 8;
|
|
242
|
+
const segmentSizes = [firstSegmentSize];
|
|
243
|
+
for (let i = 1; i < segmentCount; i++) {
|
|
244
|
+
if (offset + 4 > uint8Array.byteLength) {
|
|
245
|
+
this.segments = [];
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
segmentSizes.push(view.getUint32(offset, true));
|
|
249
|
+
offset += 4;
|
|
250
|
+
}
|
|
251
|
+
offset = offset + 7 & -8;
|
|
252
|
+
if (offset > uint8Array.byteLength) {
|
|
253
|
+
this.segments = [];
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
this.segments = [];
|
|
257
|
+
for (const size of segmentSizes) {
|
|
258
|
+
if (offset + size * WORD_SIZE > uint8Array.byteLength) break;
|
|
259
|
+
const segmentBuffer = uint8Array.slice(offset, offset + size * WORD_SIZE);
|
|
260
|
+
this.segments.push(Segment.fromBuffer(segmentBuffer.buffer));
|
|
261
|
+
offset += size * WORD_SIZE;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* 获取根结构
|
|
266
|
+
*/
|
|
267
|
+
getRoot(_dataWords, _pointerCount) {
|
|
268
|
+
const segment = this.segments[0];
|
|
269
|
+
const ptr = decodePointer(segment.getWord(0));
|
|
270
|
+
if (ptr.tag === PointerTag.STRUCT) {
|
|
271
|
+
const structPtr = ptr;
|
|
272
|
+
const dataOffset = 1 + structPtr.offset;
|
|
273
|
+
return new StructReader(this, 0, dataOffset, structPtr.dataWords, structPtr.pointerCount);
|
|
274
|
+
}
|
|
275
|
+
if (ptr.tag === PointerTag.FAR) {
|
|
276
|
+
const farPtr = ptr;
|
|
277
|
+
const targetSegment = this.getSegment(farPtr.targetSegment);
|
|
278
|
+
if (!targetSegment) throw new Error(`Far pointer references non-existent segment ${farPtr.targetSegment}`);
|
|
279
|
+
if (farPtr.doubleFar) {
|
|
280
|
+
const landingPadPtr = decodePointer(targetSegment.getWord(farPtr.targetOffset));
|
|
281
|
+
if (landingPadPtr.tag !== PointerTag.FAR) throw new Error("Double-far landing pad is not a far pointer");
|
|
282
|
+
const innerFarPtr = landingPadPtr;
|
|
283
|
+
const finalSegment = this.getSegment(innerFarPtr.targetSegment);
|
|
284
|
+
if (!finalSegment) throw new Error(`Double-far references non-existent segment ${innerFarPtr.targetSegment}`);
|
|
285
|
+
const structPtr = decodePointer(finalSegment.getWord(innerFarPtr.targetOffset));
|
|
286
|
+
const dataOffset = innerFarPtr.targetOffset + 1 + structPtr.offset;
|
|
287
|
+
return new StructReader(this, innerFarPtr.targetSegment, dataOffset, structPtr.dataWords, structPtr.pointerCount);
|
|
288
|
+
}
|
|
289
|
+
const structPtr = decodePointer(targetSegment.getWord(farPtr.targetOffset));
|
|
290
|
+
const dataOffset = farPtr.targetOffset + 1 + structPtr.offset;
|
|
291
|
+
return new StructReader(this, farPtr.targetSegment, dataOffset, structPtr.dataWords, structPtr.pointerCount);
|
|
292
|
+
}
|
|
293
|
+
throw new Error(`Root pointer is not a struct or far pointer: ${ptr.tag}`);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* 获取段
|
|
297
|
+
*/
|
|
298
|
+
getSegment(index) {
|
|
299
|
+
return this.segments[index];
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* 解析指针,处理 far pointer 间接寻址
|
|
303
|
+
* 返回 { segmentIndex, wordOffset, pointer },其中 pointer 是实际的 struct/list 指针
|
|
304
|
+
*/
|
|
305
|
+
resolvePointer(segmentIndex, wordOffset) {
|
|
306
|
+
const segment = this.getSegment(segmentIndex);
|
|
307
|
+
if (!segment) return null;
|
|
308
|
+
const ptrValue = segment.getWord(wordOffset);
|
|
309
|
+
if (ptrValue === 0n) return null;
|
|
310
|
+
const ptr = decodePointer(ptrValue);
|
|
311
|
+
if (ptr.tag === PointerTag.STRUCT || ptr.tag === PointerTag.LIST) return {
|
|
312
|
+
segmentIndex,
|
|
313
|
+
wordOffset: wordOffset + 1 + ptr.offset,
|
|
314
|
+
pointer: ptr
|
|
315
|
+
};
|
|
316
|
+
if (ptr.tag === PointerTag.FAR) {
|
|
317
|
+
const farPtr = ptr;
|
|
318
|
+
const targetSegment = this.getSegment(farPtr.targetSegment);
|
|
319
|
+
if (!targetSegment) return null;
|
|
320
|
+
if (farPtr.doubleFar) {
|
|
321
|
+
const landingPadPtr = decodePointer(targetSegment.getWord(farPtr.targetOffset));
|
|
322
|
+
if (landingPadPtr.tag !== PointerTag.FAR) return null;
|
|
323
|
+
const innerFarPtr = landingPadPtr;
|
|
324
|
+
const finalSegment = this.getSegment(innerFarPtr.targetSegment);
|
|
325
|
+
if (!finalSegment) return null;
|
|
326
|
+
const finalPtr = decodePointer(finalSegment.getWord(innerFarPtr.targetOffset));
|
|
327
|
+
if (finalPtr.tag !== PointerTag.STRUCT && finalPtr.tag !== PointerTag.LIST) return null;
|
|
328
|
+
const targetOffset = innerFarPtr.targetOffset + 1 + finalPtr.offset;
|
|
329
|
+
return {
|
|
330
|
+
segmentIndex: innerFarPtr.targetSegment,
|
|
331
|
+
wordOffset: targetOffset,
|
|
332
|
+
pointer: finalPtr
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
const landingPadPtr = decodePointer(targetSegment.getWord(farPtr.targetOffset));
|
|
336
|
+
if (landingPadPtr.tag !== PointerTag.STRUCT && landingPadPtr.tag !== PointerTag.LIST) return null;
|
|
337
|
+
const targetOffset = farPtr.targetOffset + 1 + landingPadPtr.offset;
|
|
338
|
+
return {
|
|
339
|
+
segmentIndex: farPtr.targetSegment,
|
|
340
|
+
wordOffset: targetOffset,
|
|
341
|
+
pointer: landingPadPtr
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* 段数量
|
|
348
|
+
*/
|
|
349
|
+
get segmentCount() {
|
|
350
|
+
return this.segments.length;
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
/**
|
|
354
|
+
* 结构读取器
|
|
355
|
+
*/
|
|
356
|
+
var StructReader = class StructReader {
|
|
357
|
+
constructor(message, segmentIndex, wordOffset, dataWords, pointerCount) {
|
|
358
|
+
this.message = message;
|
|
359
|
+
this.segmentIndex = segmentIndex;
|
|
360
|
+
this.wordOffset = wordOffset;
|
|
361
|
+
this.dataWords = dataWords;
|
|
362
|
+
this.pointerCount = pointerCount;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* 获取 bool 字段
|
|
366
|
+
*/
|
|
367
|
+
getBool(bitOffset) {
|
|
368
|
+
const byteOffset = Math.floor(bitOffset / 8);
|
|
369
|
+
const bitInByte = bitOffset % 8;
|
|
370
|
+
return (this.message.getSegment(this.segmentIndex).dataView.getUint8(this.wordOffset * WORD_SIZE + byteOffset) & 1 << bitInByte) !== 0;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* 获取 int8 字段
|
|
374
|
+
*/
|
|
375
|
+
getInt8(byteOffset) {
|
|
376
|
+
return this.message.getSegment(this.segmentIndex).dataView.getInt8(this.wordOffset * WORD_SIZE + byteOffset);
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* 获取 int16 字段
|
|
380
|
+
*/
|
|
381
|
+
getInt16(byteOffset) {
|
|
382
|
+
return this.message.getSegment(this.segmentIndex).dataView.getInt16(this.wordOffset * WORD_SIZE + byteOffset, true);
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* 获取 int32 字段
|
|
386
|
+
*/
|
|
387
|
+
getInt32(byteOffset) {
|
|
388
|
+
return this.message.getSegment(this.segmentIndex).dataView.getInt32(this.wordOffset * WORD_SIZE + byteOffset, true);
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* 获取 int64 字段
|
|
392
|
+
*/
|
|
393
|
+
getInt64(byteOffset) {
|
|
394
|
+
const segment = this.message.getSegment(this.segmentIndex);
|
|
395
|
+
const offset = this.wordOffset * WORD_SIZE + byteOffset;
|
|
396
|
+
const low = BigInt(segment.dataView.getUint32(offset, true));
|
|
397
|
+
return BigInt(segment.dataView.getInt32(offset + 4, true)) << BigInt(32) | low;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* 获取 uint8 字段
|
|
401
|
+
*/
|
|
402
|
+
getUint8(byteOffset) {
|
|
403
|
+
return this.message.getSegment(this.segmentIndex).dataView.getUint8(this.wordOffset * WORD_SIZE + byteOffset);
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* 获取 uint16 字段
|
|
407
|
+
*/
|
|
408
|
+
getUint16(byteOffset) {
|
|
409
|
+
return this.message.getSegment(this.segmentIndex).dataView.getUint16(this.wordOffset * WORD_SIZE + byteOffset, true);
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* 获取 uint32 字段
|
|
413
|
+
*/
|
|
414
|
+
getUint32(byteOffset) {
|
|
415
|
+
return this.message.getSegment(this.segmentIndex).dataView.getUint32(this.wordOffset * WORD_SIZE + byteOffset, true);
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* 获取 uint64 字段
|
|
419
|
+
*/
|
|
420
|
+
getUint64(byteOffset) {
|
|
421
|
+
const segment = this.message.getSegment(this.segmentIndex);
|
|
422
|
+
const offset = this.wordOffset * WORD_SIZE + byteOffset;
|
|
423
|
+
const low = BigInt(segment.dataView.getUint32(offset, true));
|
|
424
|
+
return BigInt(segment.dataView.getUint32(offset + 4, true)) << BigInt(32) | low;
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* 获取 float32 字段
|
|
428
|
+
*/
|
|
429
|
+
getFloat32(byteOffset) {
|
|
430
|
+
return this.message.getSegment(this.segmentIndex).dataView.getFloat32(this.wordOffset * WORD_SIZE + byteOffset, true);
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* 获取 float64 字段
|
|
434
|
+
*/
|
|
435
|
+
getFloat64(byteOffset) {
|
|
436
|
+
return this.message.getSegment(this.segmentIndex).dataView.getFloat64(this.wordOffset * WORD_SIZE + byteOffset, true);
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* 获取文本字段
|
|
440
|
+
*/
|
|
441
|
+
getText(pointerIndex) {
|
|
442
|
+
const ptrOffset = this.wordOffset + this.dataWords + pointerIndex;
|
|
443
|
+
const resolved = this.message.resolvePointer(this.segmentIndex, ptrOffset);
|
|
444
|
+
if (!resolved) return "";
|
|
445
|
+
const { segmentIndex, wordOffset, pointer } = resolved;
|
|
446
|
+
if (pointer.tag !== PointerTag.LIST) return "";
|
|
447
|
+
const listPtr = pointer;
|
|
448
|
+
const segment = this.message.getSegment(segmentIndex);
|
|
449
|
+
const byteLength = listPtr.elementCount > 0 ? listPtr.elementCount - 1 : 0;
|
|
450
|
+
if (byteLength === 0) return "";
|
|
451
|
+
const bytes = new Uint8Array(segment.dataView.buffer, wordOffset * WORD_SIZE, byteLength);
|
|
452
|
+
return new TextDecoder().decode(bytes);
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* 获取嵌套结构
|
|
456
|
+
*/
|
|
457
|
+
getStruct(pointerIndex, _dataWords, _pointerCount) {
|
|
458
|
+
const ptrOffset = this.wordOffset + this.dataWords + pointerIndex;
|
|
459
|
+
const resolved = this.message.resolvePointer(this.segmentIndex, ptrOffset);
|
|
460
|
+
if (!resolved) return void 0;
|
|
461
|
+
const { segmentIndex, wordOffset, pointer } = resolved;
|
|
462
|
+
if (pointer.tag !== PointerTag.STRUCT) return void 0;
|
|
463
|
+
const structPtr = pointer;
|
|
464
|
+
return new StructReader(this.message, segmentIndex, wordOffset, structPtr.dataWords, structPtr.pointerCount);
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* 获取列表
|
|
468
|
+
*/
|
|
469
|
+
getList(pointerIndex, _elementSize, structSize) {
|
|
470
|
+
const ptrOffset = this.wordOffset + this.dataWords + pointerIndex;
|
|
471
|
+
const resolved = this.message.resolvePointer(this.segmentIndex, ptrOffset);
|
|
472
|
+
if (!resolved) return void 0;
|
|
473
|
+
const { segmentIndex, wordOffset, pointer } = resolved;
|
|
474
|
+
if (pointer.tag !== PointerTag.LIST) return void 0;
|
|
475
|
+
const listPtr = pointer;
|
|
476
|
+
let targetOffset = wordOffset;
|
|
477
|
+
let elementCount = listPtr.elementCount;
|
|
478
|
+
let actualStructSize = structSize;
|
|
479
|
+
const segment = this.message.getSegment(segmentIndex);
|
|
480
|
+
if (listPtr.elementSize === ElementSize.COMPOSITE) {
|
|
481
|
+
const tagWord = segment.getWord(targetOffset);
|
|
482
|
+
elementCount = Number(tagWord & BigInt(4294967295));
|
|
483
|
+
actualStructSize = {
|
|
484
|
+
dataWords: Number(tagWord >> BigInt(32) & BigInt(65535)),
|
|
485
|
+
pointerCount: Number(tagWord >> BigInt(48) & BigInt(65535))
|
|
486
|
+
};
|
|
487
|
+
targetOffset += 1;
|
|
488
|
+
}
|
|
489
|
+
return new ListReader(this.message, segmentIndex, listPtr.elementSize, elementCount, actualStructSize, targetOffset);
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
//#endregion
|
|
494
|
+
//#region src/schema/schema-reader.ts
|
|
495
|
+
/**
|
|
496
|
+
* Cap'n Proto 编译后 Schema 的 TypeScript Reader
|
|
497
|
+
* 基于官方 schema.capnp 手动编写
|
|
498
|
+
*
|
|
499
|
+
* 这是自举的基础:我们需要先能读取 schema 才能生成 schema 的代码
|
|
500
|
+
*
|
|
501
|
+
* 结构信息来自: capnp compile -ocapnp schema.capnp
|
|
502
|
+
*/
|
|
503
|
+
var NodeReader = class {
|
|
504
|
+
constructor(reader) {
|
|
505
|
+
this.reader = reader;
|
|
506
|
+
}
|
|
507
|
+
get id() {
|
|
508
|
+
return this.reader.getUint64(0);
|
|
509
|
+
}
|
|
510
|
+
get displayName() {
|
|
511
|
+
return this.reader.getText(0);
|
|
512
|
+
}
|
|
513
|
+
get displayNamePrefixLength() {
|
|
514
|
+
return this.reader.getUint32(8);
|
|
515
|
+
}
|
|
516
|
+
get scopeId() {
|
|
517
|
+
return this.reader.getUint64(16);
|
|
518
|
+
}
|
|
519
|
+
/** union tag 在 bits [96, 112) = bytes [12, 14) */
|
|
520
|
+
get unionTag() {
|
|
521
|
+
return this.reader.getUint16(12);
|
|
522
|
+
}
|
|
523
|
+
get isFile() {
|
|
524
|
+
return this.unionTag === 0;
|
|
525
|
+
}
|
|
526
|
+
get isStruct() {
|
|
527
|
+
return this.unionTag === 1;
|
|
528
|
+
}
|
|
529
|
+
get isEnum() {
|
|
530
|
+
return this.unionTag === 2;
|
|
531
|
+
}
|
|
532
|
+
get isInterface() {
|
|
533
|
+
return this.unionTag === 3;
|
|
534
|
+
}
|
|
535
|
+
get isConst() {
|
|
536
|
+
return this.unionTag === 4;
|
|
537
|
+
}
|
|
538
|
+
get isAnnotation() {
|
|
539
|
+
return this.unionTag === 5;
|
|
540
|
+
}
|
|
541
|
+
get structDataWordCount() {
|
|
542
|
+
return this.reader.getUint16(14);
|
|
543
|
+
}
|
|
544
|
+
get structPointerCount() {
|
|
545
|
+
return this.reader.getUint16(24);
|
|
546
|
+
}
|
|
547
|
+
get structPreferredListEncoding() {
|
|
548
|
+
return this.reader.getUint16(26);
|
|
549
|
+
}
|
|
550
|
+
get structIsGroup() {
|
|
551
|
+
return this.reader.getBool(224);
|
|
552
|
+
}
|
|
553
|
+
get structDiscriminantCount() {
|
|
554
|
+
return this.reader.getUint16(30);
|
|
555
|
+
}
|
|
556
|
+
get structDiscriminantOffset() {
|
|
557
|
+
return this.reader.getUint32(32);
|
|
558
|
+
}
|
|
559
|
+
get structFields() {
|
|
560
|
+
const listReader = this.reader.getList(3, ElementSize.INLINE_COMPOSITE, {
|
|
561
|
+
dataWords: 3,
|
|
562
|
+
pointerCount: 4
|
|
563
|
+
});
|
|
564
|
+
if (!listReader) return [];
|
|
565
|
+
const fields = [];
|
|
566
|
+
for (let i = 0; i < listReader.length; i++) {
|
|
567
|
+
const field = new FieldReader(listReader.getStruct(i));
|
|
568
|
+
if (!field.name && i > 0) break;
|
|
569
|
+
fields.push(field);
|
|
570
|
+
}
|
|
571
|
+
return fields;
|
|
572
|
+
}
|
|
573
|
+
get enumEnumerants() {
|
|
574
|
+
const listReader = this.reader.getList(3, ElementSize.INLINE_COMPOSITE, {
|
|
575
|
+
dataWords: 1,
|
|
576
|
+
pointerCount: 2
|
|
577
|
+
});
|
|
578
|
+
if (!listReader) return [];
|
|
579
|
+
const enumerants = [];
|
|
580
|
+
for (let i = 0; i < Math.min(listReader.length, 10); i++) try {
|
|
581
|
+
const enumerant = new EnumerantReader(listReader.getStruct(i));
|
|
582
|
+
if (!enumerant.name) break;
|
|
583
|
+
enumerants.push(enumerant);
|
|
584
|
+
} catch (_e) {
|
|
585
|
+
break;
|
|
586
|
+
}
|
|
587
|
+
return enumerants;
|
|
588
|
+
}
|
|
589
|
+
get nestedNodes() {
|
|
590
|
+
const listReader = this.reader.getList(1, ElementSize.INLINE_COMPOSITE, {
|
|
591
|
+
dataWords: 1,
|
|
592
|
+
pointerCount: 1
|
|
593
|
+
});
|
|
594
|
+
if (!listReader) return [];
|
|
595
|
+
const nodes = [];
|
|
596
|
+
for (let i = 0; i < listReader.length; i++) {
|
|
597
|
+
const itemReader = listReader.getStruct(i);
|
|
598
|
+
nodes.push(new NestedNodeReader(itemReader));
|
|
599
|
+
}
|
|
600
|
+
return nodes;
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
var FieldReader = class {
|
|
604
|
+
constructor(reader) {
|
|
605
|
+
this.reader = reader;
|
|
606
|
+
}
|
|
607
|
+
get name() {
|
|
608
|
+
return this.reader.getText(0);
|
|
609
|
+
}
|
|
610
|
+
get codeOrder() {
|
|
611
|
+
return this.reader.getUint16(0);
|
|
612
|
+
}
|
|
613
|
+
get discriminantValue() {
|
|
614
|
+
return this.reader.getUint16(2);
|
|
615
|
+
}
|
|
616
|
+
/** union tag 在 bits [64, 80) = bytes [8, 10) */
|
|
617
|
+
get unionTag() {
|
|
618
|
+
return this.reader.getUint16(8);
|
|
619
|
+
}
|
|
620
|
+
get isSlot() {
|
|
621
|
+
return this.unionTag === 0;
|
|
622
|
+
}
|
|
623
|
+
get isGroup() {
|
|
624
|
+
return this.unionTag === 1;
|
|
625
|
+
}
|
|
626
|
+
get slotOffset() {
|
|
627
|
+
return this.reader.getUint32(4);
|
|
628
|
+
}
|
|
629
|
+
get slotType() {
|
|
630
|
+
const typeReader = this.reader.getStruct(2, 3, 1);
|
|
631
|
+
return typeReader ? new TypeReader(typeReader) : null;
|
|
632
|
+
}
|
|
633
|
+
get groupTypeId() {
|
|
634
|
+
return this.reader.getUint64(16);
|
|
635
|
+
}
|
|
636
|
+
get defaultValue() {
|
|
637
|
+
const valueReader = this.reader.getStruct(3, 2, 1);
|
|
638
|
+
return valueReader ? new ValueReader(valueReader) : null;
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
var TypeReader = class TypeReader {
|
|
642
|
+
constructor(reader) {
|
|
643
|
+
this.reader = reader;
|
|
644
|
+
}
|
|
645
|
+
get kind() {
|
|
646
|
+
return TYPE_KIND_MAP[this.reader.getUint16(0)] ?? "unknown";
|
|
647
|
+
}
|
|
648
|
+
get isPrimitive() {
|
|
649
|
+
const k = this.kind;
|
|
650
|
+
return k !== "list" && k !== "enum" && k !== "struct" && k !== "interface" && k !== "anyPointer";
|
|
651
|
+
}
|
|
652
|
+
get listElementType() {
|
|
653
|
+
if (this.kind !== "list") return null;
|
|
654
|
+
const elementReader = this.reader.getStruct(0, 3, 1);
|
|
655
|
+
return elementReader ? new TypeReader(elementReader) : null;
|
|
656
|
+
}
|
|
657
|
+
get typeId() {
|
|
658
|
+
const k = this.kind;
|
|
659
|
+
if (k === "enum" || k === "struct" || k === "interface") return this.reader.getUint64(8);
|
|
660
|
+
return null;
|
|
661
|
+
}
|
|
662
|
+
};
|
|
663
|
+
const TYPE_KIND_MAP = {
|
|
664
|
+
0: "void",
|
|
665
|
+
1: "bool",
|
|
666
|
+
2: "int8",
|
|
667
|
+
3: "int16",
|
|
668
|
+
4: "int32",
|
|
669
|
+
5: "int64",
|
|
670
|
+
6: "uint8",
|
|
671
|
+
7: "uint16",
|
|
672
|
+
8: "uint32",
|
|
673
|
+
9: "uint64",
|
|
674
|
+
10: "float32",
|
|
675
|
+
11: "float64",
|
|
676
|
+
12: "text",
|
|
677
|
+
13: "data",
|
|
678
|
+
14: "list",
|
|
679
|
+
15: "enum",
|
|
680
|
+
16: "struct",
|
|
681
|
+
17: "interface",
|
|
682
|
+
18: "anyPointer"
|
|
683
|
+
};
|
|
684
|
+
var ValueReader = class {
|
|
685
|
+
constructor(reader) {
|
|
686
|
+
this.reader = reader;
|
|
687
|
+
}
|
|
688
|
+
/** union tag 在 bits [0, 16) = bytes [0, 2) */
|
|
689
|
+
get unionTag() {
|
|
690
|
+
return this.reader.getUint16(0);
|
|
691
|
+
}
|
|
692
|
+
get isVoid() {
|
|
693
|
+
return this.unionTag === 0;
|
|
694
|
+
}
|
|
695
|
+
get isBool() {
|
|
696
|
+
return this.unionTag === 1;
|
|
697
|
+
}
|
|
698
|
+
get isInt8() {
|
|
699
|
+
return this.unionTag === 2;
|
|
700
|
+
}
|
|
701
|
+
get isInt16() {
|
|
702
|
+
return this.unionTag === 3;
|
|
703
|
+
}
|
|
704
|
+
get isInt32() {
|
|
705
|
+
return this.unionTag === 4;
|
|
706
|
+
}
|
|
707
|
+
get isInt64() {
|
|
708
|
+
return this.unionTag === 5;
|
|
709
|
+
}
|
|
710
|
+
get isUint8() {
|
|
711
|
+
return this.unionTag === 6;
|
|
712
|
+
}
|
|
713
|
+
get isUint16() {
|
|
714
|
+
return this.unionTag === 7;
|
|
715
|
+
}
|
|
716
|
+
get isUint32() {
|
|
717
|
+
return this.unionTag === 8;
|
|
718
|
+
}
|
|
719
|
+
get isUint64() {
|
|
720
|
+
return this.unionTag === 9;
|
|
721
|
+
}
|
|
722
|
+
get isFloat32() {
|
|
723
|
+
return this.unionTag === 10;
|
|
724
|
+
}
|
|
725
|
+
get isFloat64() {
|
|
726
|
+
return this.unionTag === 11;
|
|
727
|
+
}
|
|
728
|
+
get isText() {
|
|
729
|
+
return this.unionTag === 12;
|
|
730
|
+
}
|
|
731
|
+
get isData() {
|
|
732
|
+
return this.unionTag === 13;
|
|
733
|
+
}
|
|
734
|
+
get isList() {
|
|
735
|
+
return this.unionTag === 14;
|
|
736
|
+
}
|
|
737
|
+
get isEnum() {
|
|
738
|
+
return this.unionTag === 15;
|
|
739
|
+
}
|
|
740
|
+
get isStruct() {
|
|
741
|
+
return this.unionTag === 16;
|
|
742
|
+
}
|
|
743
|
+
get isInterface() {
|
|
744
|
+
return this.unionTag === 17;
|
|
745
|
+
}
|
|
746
|
+
get isAnyPointer() {
|
|
747
|
+
return this.unionTag === 18;
|
|
748
|
+
}
|
|
749
|
+
get boolValue() {
|
|
750
|
+
return this.reader.getBool(16);
|
|
751
|
+
}
|
|
752
|
+
get int8Value() {
|
|
753
|
+
return this.reader.getInt8(2);
|
|
754
|
+
}
|
|
755
|
+
get int16Value() {
|
|
756
|
+
return this.reader.getInt16(2);
|
|
757
|
+
}
|
|
758
|
+
get int32Value() {
|
|
759
|
+
return this.reader.getInt32(4);
|
|
760
|
+
}
|
|
761
|
+
get int64Value() {
|
|
762
|
+
return this.reader.getInt64(8);
|
|
763
|
+
}
|
|
764
|
+
get uint8Value() {
|
|
765
|
+
return this.reader.getUint8(2);
|
|
766
|
+
}
|
|
767
|
+
get uint16Value() {
|
|
768
|
+
return this.reader.getUint16(2);
|
|
769
|
+
}
|
|
770
|
+
get uint32Value() {
|
|
771
|
+
return this.reader.getUint32(4);
|
|
772
|
+
}
|
|
773
|
+
get uint64Value() {
|
|
774
|
+
return this.reader.getUint64(8);
|
|
775
|
+
}
|
|
776
|
+
get float32Value() {
|
|
777
|
+
return this.reader.getFloat32(4);
|
|
778
|
+
}
|
|
779
|
+
get float64Value() {
|
|
780
|
+
return this.reader.getFloat64(8);
|
|
781
|
+
}
|
|
782
|
+
get enumValue() {
|
|
783
|
+
return this.reader.getUint16(2);
|
|
784
|
+
}
|
|
785
|
+
getAsNumber() {
|
|
786
|
+
if (this.isInt8) return this.int8Value;
|
|
787
|
+
if (this.isInt16) return this.int16Value;
|
|
788
|
+
if (this.isInt32) return this.int32Value;
|
|
789
|
+
if (this.isUint8) return this.uint8Value;
|
|
790
|
+
if (this.isUint16) return this.uint16Value;
|
|
791
|
+
if (this.isUint32) return this.uint32Value;
|
|
792
|
+
if (this.isFloat32) return this.float32Value;
|
|
793
|
+
if (this.isFloat64) return this.float64Value;
|
|
794
|
+
if (this.isEnum) return this.enumValue;
|
|
795
|
+
}
|
|
796
|
+
getAsBigint() {
|
|
797
|
+
if (this.isInt64) return this.int64Value;
|
|
798
|
+
if (this.isUint64) return this.uint64Value;
|
|
799
|
+
}
|
|
800
|
+
getAsBoolean() {
|
|
801
|
+
if (this.isBool) return this.boolValue;
|
|
802
|
+
}
|
|
803
|
+
};
|
|
804
|
+
var EnumerantReader = class {
|
|
805
|
+
constructor(reader) {
|
|
806
|
+
this.reader = reader;
|
|
807
|
+
}
|
|
808
|
+
get name() {
|
|
809
|
+
return this.reader.getText(0);
|
|
810
|
+
}
|
|
811
|
+
get codeOrder() {
|
|
812
|
+
return this.reader.getUint16(0);
|
|
813
|
+
}
|
|
814
|
+
};
|
|
815
|
+
var NestedNodeReader = class {
|
|
816
|
+
constructor(reader) {
|
|
817
|
+
this.reader = reader;
|
|
818
|
+
}
|
|
819
|
+
get name() {
|
|
820
|
+
return this.reader.getText(0);
|
|
821
|
+
}
|
|
822
|
+
get id() {
|
|
823
|
+
return this.reader.getUint64(0);
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
var CodeGeneratorRequestReader = class CodeGeneratorRequestReader {
|
|
827
|
+
constructor(message) {
|
|
828
|
+
this.message = message;
|
|
829
|
+
}
|
|
830
|
+
static fromBuffer(buffer) {
|
|
831
|
+
return new CodeGeneratorRequestReader(new MessageReader(buffer));
|
|
832
|
+
}
|
|
833
|
+
get nodes() {
|
|
834
|
+
const listReader = this.message.getRoot(0, 4).getList(0, ElementSize.INLINE_COMPOSITE, {
|
|
835
|
+
dataWords: 6,
|
|
836
|
+
pointerCount: 6
|
|
837
|
+
});
|
|
838
|
+
if (!listReader) return [];
|
|
839
|
+
const nodes = [];
|
|
840
|
+
for (let i = 0; i < listReader.length; i++) {
|
|
841
|
+
const nodeReader = listReader.getStruct(i);
|
|
842
|
+
nodes.push(new NodeReader(nodeReader));
|
|
843
|
+
}
|
|
844
|
+
return nodes;
|
|
845
|
+
}
|
|
846
|
+
get requestedFiles() {
|
|
847
|
+
const listReader = this.message.getRoot(0, 4).getList(1, ElementSize.INLINE_COMPOSITE, {
|
|
848
|
+
dataWords: 1,
|
|
849
|
+
pointerCount: 2
|
|
850
|
+
});
|
|
851
|
+
if (!listReader) return [];
|
|
852
|
+
const files = [];
|
|
853
|
+
for (let i = 0; i < listReader.length; i++) try {
|
|
854
|
+
const file = new RequestedFileReader(listReader.getStruct(i));
|
|
855
|
+
if (!file.filename && files.length > 0) break;
|
|
856
|
+
files.push(file);
|
|
857
|
+
} catch (_e) {
|
|
858
|
+
break;
|
|
859
|
+
}
|
|
860
|
+
return files;
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
var RequestedFileReader = class {
|
|
864
|
+
constructor(reader) {
|
|
865
|
+
this.reader = reader;
|
|
866
|
+
}
|
|
867
|
+
get id() {
|
|
868
|
+
return this.reader.getUint64(0);
|
|
869
|
+
}
|
|
870
|
+
get filename() {
|
|
871
|
+
return this.reader.getText(0);
|
|
872
|
+
}
|
|
873
|
+
};
|
|
874
|
+
|
|
875
|
+
//#endregion
|
|
876
|
+
//#region src/codegen/generator-v3.ts
|
|
877
|
+
const DEFAULT_OPTIONS = { runtimeImportPath: "@naeemo/capnp" };
|
|
878
|
+
/**
|
|
879
|
+
* 从 CodeGeneratorRequest 生成 TypeScript 代码
|
|
880
|
+
*/
|
|
881
|
+
function generateFromRequest(request, options) {
|
|
882
|
+
const opts = {
|
|
883
|
+
...DEFAULT_OPTIONS,
|
|
884
|
+
...options
|
|
885
|
+
};
|
|
886
|
+
const files = /* @__PURE__ */ new Map();
|
|
887
|
+
const nodes = request.nodes;
|
|
888
|
+
const requestedFiles = request.requestedFiles;
|
|
889
|
+
for (const file of requestedFiles) {
|
|
890
|
+
const filename = file.filename.replace(/\.capnp$/, ".ts");
|
|
891
|
+
const code = generateFile(file.id, nodes, opts);
|
|
892
|
+
files.set(filename, code);
|
|
893
|
+
}
|
|
894
|
+
return files;
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* 生成单个文件的 TypeScript 代码
|
|
898
|
+
*/
|
|
899
|
+
function generateFile(fileId, allNodes, options) {
|
|
900
|
+
const lines = [];
|
|
901
|
+
lines.push("// Generated by @naeemo/capnp");
|
|
902
|
+
lines.push("// DO NOT EDIT MANUALLY");
|
|
903
|
+
lines.push("");
|
|
904
|
+
lines.push(`import { MessageReader, MessageBuilder, StructReader, StructBuilder, createUnionReader, createUnionBuilder } from "${options.runtimeImportPath}";`);
|
|
905
|
+
lines.push("");
|
|
906
|
+
lines.push("// XOR helpers for default value encoding");
|
|
907
|
+
lines.push("function xorFloat32(a: number, b: number): number {");
|
|
908
|
+
lines.push(" const view = new DataView(new ArrayBuffer(4));");
|
|
909
|
+
lines.push(" view.setFloat32(0, a, true);");
|
|
910
|
+
lines.push(" const aBits = view.getUint32(0, true);");
|
|
911
|
+
lines.push(" view.setFloat32(0, b, true);");
|
|
912
|
+
lines.push(" const bBits = view.getUint32(0, true);");
|
|
913
|
+
lines.push(" view.setUint32(0, aBits ^ bBits, true);");
|
|
914
|
+
lines.push(" return view.getFloat32(0, true);");
|
|
915
|
+
lines.push("}");
|
|
916
|
+
lines.push("");
|
|
917
|
+
lines.push("function xorFloat64(a: number, b: number): number {");
|
|
918
|
+
lines.push(" const view = new DataView(new ArrayBuffer(8));");
|
|
919
|
+
lines.push(" view.setFloat64(0, a, true);");
|
|
920
|
+
lines.push(" const aBits = view.getBigUint64(0, true);");
|
|
921
|
+
lines.push(" view.setFloat64(0, b, true);");
|
|
922
|
+
lines.push(" const bBits = view.getBigUint64(0, true);");
|
|
923
|
+
lines.push(" view.setBigUint64(0, aBits ^ bBits, true);");
|
|
924
|
+
lines.push(" return view.getFloat64(0, true);");
|
|
925
|
+
lines.push("}");
|
|
926
|
+
lines.push("");
|
|
927
|
+
const fileNodes = allNodes.filter((n) => n.scopeId === fileId);
|
|
928
|
+
for (const node of fileNodes) {
|
|
929
|
+
if (node.isStruct) lines.push(generateStruct(node, allNodes));
|
|
930
|
+
else if (node.isEnum) lines.push(generateEnum(node));
|
|
931
|
+
lines.push("");
|
|
932
|
+
}
|
|
933
|
+
return lines.join("\n");
|
|
934
|
+
}
|
|
935
|
+
/**
|
|
936
|
+
* 生成 Struct 的 TypeScript 代码
|
|
937
|
+
*/
|
|
938
|
+
function generateStruct(node, allNodes) {
|
|
939
|
+
const lines = [];
|
|
940
|
+
const structName = getShortName(node.displayName);
|
|
941
|
+
const { unionGroups, regularFields, groupFields } = analyzeFields(node, allNodes);
|
|
942
|
+
lines.push(`export interface ${structName} {`);
|
|
943
|
+
for (const field of regularFields) {
|
|
944
|
+
if (!field.isSlot) continue;
|
|
945
|
+
const tsType = getTypeScriptType(field.slotType, allNodes);
|
|
946
|
+
lines.push(` ${field.name}: ${tsType};`);
|
|
947
|
+
}
|
|
948
|
+
for (const { field: groupField, groupNode } of groupFields) {
|
|
949
|
+
const groupName = groupField.name;
|
|
950
|
+
for (const field of groupNode.structFields) {
|
|
951
|
+
if (!field.isSlot) continue;
|
|
952
|
+
const tsType = getTypeScriptType(field.slotType, allNodes);
|
|
953
|
+
lines.push(` ${groupName}${capitalize(field.name)}: ${tsType};`);
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
for (const [groupIndex, group] of unionGroups.entries()) {
|
|
957
|
+
const unionName = group.fields.length > 0 ? `${capitalize(group.fields[0].name)}Union` : `Union${groupIndex}`;
|
|
958
|
+
lines.push(` ${unionName}: ${generateUnionVariantType(group, allNodes)};`);
|
|
959
|
+
}
|
|
960
|
+
lines.push("}");
|
|
961
|
+
lines.push("");
|
|
962
|
+
lines.push(`export class ${structName}Reader {`);
|
|
963
|
+
lines.push(" constructor(private reader: StructReader) {}");
|
|
964
|
+
lines.push("");
|
|
965
|
+
for (const field of regularFields) {
|
|
966
|
+
if (!field.isSlot) continue;
|
|
967
|
+
const getter = generateFieldGetter(field);
|
|
968
|
+
lines.push(` ${getter}`);
|
|
969
|
+
lines.push("");
|
|
970
|
+
}
|
|
971
|
+
for (const { field: groupField, groupNode } of groupFields) {
|
|
972
|
+
const groupName = groupField.name;
|
|
973
|
+
for (const field of groupNode.structFields) {
|
|
974
|
+
if (!field.isSlot) continue;
|
|
975
|
+
const getter = generateGroupFieldGetter(field, groupName);
|
|
976
|
+
lines.push(` ${getter}`);
|
|
977
|
+
lines.push("");
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
for (const [groupIndex, group] of unionGroups.entries()) {
|
|
981
|
+
const unionName = group.fields.length > 0 ? `${capitalize(group.fields[0].name)}Union` : `Union${groupIndex}`;
|
|
982
|
+
const variants = {};
|
|
983
|
+
for (const field of group.fields) variants[field.discriminantValue] = field.name;
|
|
984
|
+
const variantsStr = JSON.stringify(variants).replace(/"/g, "'");
|
|
985
|
+
lines.push(` get${unionName}Tag(): number {`);
|
|
986
|
+
lines.push(` return this.reader.getUint16(${group.discriminantOffset * 2});`);
|
|
987
|
+
lines.push(" }");
|
|
988
|
+
lines.push("");
|
|
989
|
+
lines.push(` get${unionName}Variant(): string | undefined {`);
|
|
990
|
+
lines.push(` const tag = this.get${unionName}Tag();`);
|
|
991
|
+
lines.push(` const variants = ${variantsStr};`);
|
|
992
|
+
lines.push(" return variants[tag];");
|
|
993
|
+
lines.push(" }");
|
|
994
|
+
lines.push("");
|
|
995
|
+
for (const field of group.fields) {
|
|
996
|
+
const getter = generateUnionFieldGetter(field, unionName, field.discriminantValue);
|
|
997
|
+
lines.push(` ${getter}`);
|
|
998
|
+
lines.push("");
|
|
999
|
+
}
|
|
1000
|
+
for (const field of group.fields) {
|
|
1001
|
+
const setter = generateUnionFieldSetter(field, unionName, field.discriminantValue, group.discriminantOffset * 2);
|
|
1002
|
+
lines.push(` ${setter}`);
|
|
1003
|
+
lines.push("");
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
lines.push("}");
|
|
1007
|
+
lines.push("");
|
|
1008
|
+
lines.push(`export class ${structName}Builder {`);
|
|
1009
|
+
lines.push(" constructor(private builder: StructBuilder) {}");
|
|
1010
|
+
lines.push("");
|
|
1011
|
+
for (const field of regularFields) {
|
|
1012
|
+
if (!field.isSlot) continue;
|
|
1013
|
+
const setter = generateFieldSetter(field);
|
|
1014
|
+
lines.push(` ${setter}`);
|
|
1015
|
+
lines.push("");
|
|
1016
|
+
}
|
|
1017
|
+
for (const { field: groupField, groupNode } of groupFields) {
|
|
1018
|
+
const groupName = groupField.name;
|
|
1019
|
+
for (const field of groupNode.structFields) {
|
|
1020
|
+
if (!field.isSlot) continue;
|
|
1021
|
+
const setter = generateGroupFieldSetter(field, groupName);
|
|
1022
|
+
lines.push(` ${setter}`);
|
|
1023
|
+
lines.push("");
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
for (const [groupIndex, group] of unionGroups.entries()) {
|
|
1027
|
+
const unionName = group.fields.length > 0 ? `${capitalize(group.fields[0].name)}Union` : `Union${groupIndex}`;
|
|
1028
|
+
for (const field of group.fields) {
|
|
1029
|
+
const setter = generateUnionFieldSetter(field, unionName, field.discriminantValue, group.discriminantOffset * 2);
|
|
1030
|
+
lines.push(` ${setter}`);
|
|
1031
|
+
lines.push("");
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
lines.push("}");
|
|
1035
|
+
return lines.join("\n");
|
|
1036
|
+
}
|
|
1037
|
+
/**
|
|
1038
|
+
* 分析字段,分离 Union 字段和普通字段,展开 Group 字段
|
|
1039
|
+
*/
|
|
1040
|
+
function analyzeFields(node, allNodes) {
|
|
1041
|
+
const fields = node.structFields;
|
|
1042
|
+
const unionGroups = /* @__PURE__ */ new Map();
|
|
1043
|
+
const regularFields = [];
|
|
1044
|
+
const groupFields = [];
|
|
1045
|
+
for (const field of fields) {
|
|
1046
|
+
if (field.isGroup) {
|
|
1047
|
+
const groupNode = allNodes.find((n) => n.id === field.groupTypeId);
|
|
1048
|
+
if (groupNode?.isStruct) groupFields.push({
|
|
1049
|
+
field,
|
|
1050
|
+
groupNode
|
|
1051
|
+
});
|
|
1052
|
+
continue;
|
|
1053
|
+
}
|
|
1054
|
+
if (node.structDiscriminantCount > 0 && field.discriminantValue !== 65535 && field.discriminantValue !== 0) {
|
|
1055
|
+
const discriminantOffset = node.structDiscriminantOffset;
|
|
1056
|
+
if (!unionGroups.has(discriminantOffset)) unionGroups.set(discriminantOffset, {
|
|
1057
|
+
discriminantOffset,
|
|
1058
|
+
fields: []
|
|
1059
|
+
});
|
|
1060
|
+
unionGroups.get(discriminantOffset).fields.push(field);
|
|
1061
|
+
} else regularFields.push(field);
|
|
1062
|
+
}
|
|
1063
|
+
return {
|
|
1064
|
+
unionGroups: Array.from(unionGroups.values()),
|
|
1065
|
+
regularFields,
|
|
1066
|
+
groupFields
|
|
1067
|
+
};
|
|
1068
|
+
}
|
|
1069
|
+
/**
|
|
1070
|
+
* 生成 Union variant 类型
|
|
1071
|
+
*/
|
|
1072
|
+
function generateUnionVariantType(group, allNodes) {
|
|
1073
|
+
return group.fields.map((f) => {
|
|
1074
|
+
const tsType = f.isSlot ? getTypeScriptType(f.slotType, allNodes) : "unknown";
|
|
1075
|
+
return `{ tag: ${f.discriminantValue}; variant: '${f.name}'; value: ${tsType} }`;
|
|
1076
|
+
}).join(" | ");
|
|
1077
|
+
}
|
|
1078
|
+
/**
|
|
1079
|
+
* 生成 Union 字段的 getter
|
|
1080
|
+
*/
|
|
1081
|
+
function generateUnionFieldGetter(field, unionName, discriminantValue) {
|
|
1082
|
+
const name = field.name;
|
|
1083
|
+
const type = field.slotType;
|
|
1084
|
+
if (!type || !field.isSlot) return `get${capitalize(name)}(): unknown | undefined { return this.get${unionName}Tag() === ${discriminantValue} ? undefined : undefined; }`;
|
|
1085
|
+
const returnType = getTypeScriptTypeForSetter(type);
|
|
1086
|
+
let body;
|
|
1087
|
+
switch (type.kind) {
|
|
1088
|
+
case "void":
|
|
1089
|
+
body = "return undefined;";
|
|
1090
|
+
break;
|
|
1091
|
+
case "bool":
|
|
1092
|
+
body = `return this.reader.getBool(${field.slotOffset * 8});`;
|
|
1093
|
+
break;
|
|
1094
|
+
case "int8":
|
|
1095
|
+
body = `return this.reader.getInt8(${field.slotOffset});`;
|
|
1096
|
+
break;
|
|
1097
|
+
case "int16":
|
|
1098
|
+
body = `return this.reader.getInt16(${field.slotOffset * 2});`;
|
|
1099
|
+
break;
|
|
1100
|
+
case "int32":
|
|
1101
|
+
body = `return this.reader.getInt32(${field.slotOffset * 4});`;
|
|
1102
|
+
break;
|
|
1103
|
+
case "int64":
|
|
1104
|
+
body = `return this.reader.getInt64(${field.slotOffset * 8});`;
|
|
1105
|
+
break;
|
|
1106
|
+
case "uint8":
|
|
1107
|
+
body = `return this.reader.getUint8(${field.slotOffset});`;
|
|
1108
|
+
break;
|
|
1109
|
+
case "uint16":
|
|
1110
|
+
body = `return this.reader.getUint16(${field.slotOffset * 2});`;
|
|
1111
|
+
break;
|
|
1112
|
+
case "uint32":
|
|
1113
|
+
body = `return this.reader.getUint32(${field.slotOffset * 4});`;
|
|
1114
|
+
break;
|
|
1115
|
+
case "uint64":
|
|
1116
|
+
body = `return this.reader.getUint64(${field.slotOffset * 8});`;
|
|
1117
|
+
break;
|
|
1118
|
+
case "float32":
|
|
1119
|
+
body = `return this.reader.getFloat32(${field.slotOffset * 4});`;
|
|
1120
|
+
break;
|
|
1121
|
+
case "float64":
|
|
1122
|
+
body = `return this.reader.getFloat64(${field.slotOffset * 8});`;
|
|
1123
|
+
break;
|
|
1124
|
+
case "text":
|
|
1125
|
+
body = `return this.reader.getText(${field.slotOffset});`;
|
|
1126
|
+
break;
|
|
1127
|
+
case "data":
|
|
1128
|
+
body = `return this.reader.getData(${field.slotOffset});`;
|
|
1129
|
+
break;
|
|
1130
|
+
default: body = "return undefined;";
|
|
1131
|
+
}
|
|
1132
|
+
return `get${capitalize(name)}(): ${returnType} | undefined {
|
|
1133
|
+
if (this.get${unionName}Tag() !== ${discriminantValue}) return undefined;
|
|
1134
|
+
${body}
|
|
1135
|
+
}`;
|
|
1136
|
+
}
|
|
1137
|
+
/**
|
|
1138
|
+
* 生成 Group 字段的 getter
|
|
1139
|
+
* Group 字段在父 struct 的 data/pointer section 中,但命名空间是 Group 的名字
|
|
1140
|
+
*/
|
|
1141
|
+
function generateGroupFieldGetter(field, groupName) {
|
|
1142
|
+
const name = field.name;
|
|
1143
|
+
const type = field.slotType;
|
|
1144
|
+
if (!type) return `get${capitalize(groupName)}${capitalize(name)}(): unknown { return undefined; }`;
|
|
1145
|
+
switch (type.kind) {
|
|
1146
|
+
case "void": return `get${capitalize(groupName)}${capitalize(name)}(): void { return undefined; }`;
|
|
1147
|
+
case "bool": return `get${capitalize(groupName)}${capitalize(name)}(): boolean { return this.reader.getBool(${field.slotOffset * 8}); }`;
|
|
1148
|
+
case "int8": return `get${capitalize(groupName)}${capitalize(name)}(): number { return this.reader.getInt8(${field.slotOffset}); }`;
|
|
1149
|
+
case "int16": return `get${capitalize(groupName)}${capitalize(name)}(): number { return this.reader.getInt16(${field.slotOffset * 2}); }`;
|
|
1150
|
+
case "int32": return `get${capitalize(groupName)}${capitalize(name)}(): number { return this.reader.getInt32(${field.slotOffset * 4}); }`;
|
|
1151
|
+
case "int64": return `get${capitalize(groupName)}${capitalize(name)}(): bigint { return this.reader.getInt64(${field.slotOffset * 8}); }`;
|
|
1152
|
+
case "uint8": return `get${capitalize(groupName)}${capitalize(name)}(): number { return this.reader.getUint8(${field.slotOffset}); }`;
|
|
1153
|
+
case "uint16": return `get${capitalize(groupName)}${capitalize(name)}(): number { return this.reader.getUint16(${field.slotOffset * 2}); }`;
|
|
1154
|
+
case "uint32": return `get${capitalize(groupName)}${capitalize(name)}(): number { return this.reader.getUint32(${field.slotOffset * 4}); }`;
|
|
1155
|
+
case "uint64": return `get${capitalize(groupName)}${capitalize(name)}(): bigint { return this.reader.getUint64(${field.slotOffset * 8}); }`;
|
|
1156
|
+
case "float32": return `get${capitalize(groupName)}${capitalize(name)}(): number { return this.reader.getFloat32(${field.slotOffset * 4}); }`;
|
|
1157
|
+
case "float64": return `get${capitalize(groupName)}${capitalize(name)}(): number { return this.reader.getFloat64(${field.slotOffset * 8}); }`;
|
|
1158
|
+
case "text": return `get${capitalize(groupName)}${capitalize(name)}(): string { return this.reader.getText(${field.slotOffset}); }`;
|
|
1159
|
+
case "data": return `get${capitalize(groupName)}${capitalize(name)}(): Uint8Array { return this.reader.getData(${field.slotOffset}); }`;
|
|
1160
|
+
default: return `get${capitalize(groupName)}${capitalize(name)}(): unknown { return undefined; }`;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
/**
|
|
1164
|
+
* 生成 Group 字段的 setter
|
|
1165
|
+
*/
|
|
1166
|
+
function generateGroupFieldSetter(field, groupName) {
|
|
1167
|
+
const name = field.name;
|
|
1168
|
+
const type = field.slotType;
|
|
1169
|
+
const paramType = getTypeScriptTypeForSetter(type);
|
|
1170
|
+
if (!type || !field.isSlot) return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { /* TODO */ }`;
|
|
1171
|
+
switch (type.kind) {
|
|
1172
|
+
case "void": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { }`;
|
|
1173
|
+
case "bool": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { this.builder.setBool(${field.slotOffset * 8}, value); }`;
|
|
1174
|
+
case "int8": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { this.builder.setInt8(${field.slotOffset}, value); }`;
|
|
1175
|
+
case "int16": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { this.builder.setInt16(${field.slotOffset * 2}, value); }`;
|
|
1176
|
+
case "int32": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { this.builder.setInt32(${field.slotOffset * 4}, value); }`;
|
|
1177
|
+
case "int64": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { this.builder.setInt64(${field.slotOffset * 8}, value); }`;
|
|
1178
|
+
case "uint8": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { this.builder.setUint8(${field.slotOffset}, value); }`;
|
|
1179
|
+
case "uint16": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { this.builder.setUint16(${field.slotOffset * 2}, value); }`;
|
|
1180
|
+
case "uint32": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { this.builder.setUint32(${field.slotOffset * 4}, value); }`;
|
|
1181
|
+
case "uint64": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { this.builder.setUint64(${field.slotOffset * 8}, value); }`;
|
|
1182
|
+
case "float32": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { this.builder.setFloat32(${field.slotOffset * 4}, value); }`;
|
|
1183
|
+
case "float64": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { this.builder.setFloat64(${field.slotOffset * 8}, value); }`;
|
|
1184
|
+
case "text": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { this.builder.setText(${field.slotOffset}, value); }`;
|
|
1185
|
+
case "data": return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { this.builder.setData(${field.slotOffset}, value); }`;
|
|
1186
|
+
default: return `set${capitalize(groupName)}${capitalize(name)}(value: ${paramType}): void { /* TODO */ }`;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
/**
|
|
1190
|
+
* 生成 Union 字段的 setter
|
|
1191
|
+
*/
|
|
1192
|
+
function generateUnionFieldSetter(field, _unionName, discriminantValue, discriminantOffset) {
|
|
1193
|
+
const name = field.name;
|
|
1194
|
+
const type = field.slotType;
|
|
1195
|
+
const paramType = getTypeScriptTypeForSetter(type);
|
|
1196
|
+
if (!type || !field.isSlot) return `set${capitalize(name)}(value: ${paramType}): void {
|
|
1197
|
+
this.builder.setUint16(${discriminantOffset}, ${discriminantValue});
|
|
1198
|
+
}`;
|
|
1199
|
+
let body;
|
|
1200
|
+
switch (type.kind) {
|
|
1201
|
+
case "void":
|
|
1202
|
+
body = "";
|
|
1203
|
+
break;
|
|
1204
|
+
case "bool":
|
|
1205
|
+
body = `this.builder.setBool(${field.slotOffset * 8}, value);`;
|
|
1206
|
+
break;
|
|
1207
|
+
case "int8":
|
|
1208
|
+
body = `this.builder.setInt8(${field.slotOffset}, value);`;
|
|
1209
|
+
break;
|
|
1210
|
+
case "int16":
|
|
1211
|
+
body = `this.builder.setInt16(${field.slotOffset * 2}, value);`;
|
|
1212
|
+
break;
|
|
1213
|
+
case "int32":
|
|
1214
|
+
body = `this.builder.setInt32(${field.slotOffset * 4}, value);`;
|
|
1215
|
+
break;
|
|
1216
|
+
case "int64":
|
|
1217
|
+
body = `this.builder.setInt64(${field.slotOffset * 8}, value);`;
|
|
1218
|
+
break;
|
|
1219
|
+
case "uint8":
|
|
1220
|
+
body = `this.builder.setUint8(${field.slotOffset}, value);`;
|
|
1221
|
+
break;
|
|
1222
|
+
case "uint16":
|
|
1223
|
+
body = `this.builder.setUint16(${field.slotOffset * 2}, value);`;
|
|
1224
|
+
break;
|
|
1225
|
+
case "uint32":
|
|
1226
|
+
body = `this.builder.setUint32(${field.slotOffset * 4}, value);`;
|
|
1227
|
+
break;
|
|
1228
|
+
case "uint64":
|
|
1229
|
+
body = `this.builder.setUint64(${field.slotOffset * 8}, value);`;
|
|
1230
|
+
break;
|
|
1231
|
+
case "float32":
|
|
1232
|
+
body = `this.builder.setFloat32(${field.slotOffset * 4}, value);`;
|
|
1233
|
+
break;
|
|
1234
|
+
case "float64":
|
|
1235
|
+
body = `this.builder.setFloat64(${field.slotOffset * 8}, value);`;
|
|
1236
|
+
break;
|
|
1237
|
+
case "text":
|
|
1238
|
+
body = `this.builder.setText(${field.slotOffset}, value);`;
|
|
1239
|
+
break;
|
|
1240
|
+
case "data":
|
|
1241
|
+
body = `this.builder.setData(${field.slotOffset}, value);`;
|
|
1242
|
+
break;
|
|
1243
|
+
default: body = "";
|
|
1244
|
+
}
|
|
1245
|
+
return `set${capitalize(name)}(value: ${paramType}): void {
|
|
1246
|
+
this.builder.setUint16(${discriminantOffset}, ${discriminantValue});
|
|
1247
|
+
${body}
|
|
1248
|
+
}`;
|
|
1249
|
+
}
|
|
1250
|
+
/**
|
|
1251
|
+
* 从 ValueReader 提取默认值,用于代码生成
|
|
1252
|
+
*/
|
|
1253
|
+
function extractDefaultValue(field) {
|
|
1254
|
+
const defaultValue = field.defaultValue;
|
|
1255
|
+
if (!defaultValue) return void 0;
|
|
1256
|
+
const type = field.slotType;
|
|
1257
|
+
if (!type) return void 0;
|
|
1258
|
+
switch (type.kind) {
|
|
1259
|
+
case "bool":
|
|
1260
|
+
if (defaultValue.isBool) return defaultValue.boolValue ? "true" : "false";
|
|
1261
|
+
return;
|
|
1262
|
+
case "int8":
|
|
1263
|
+
if (defaultValue.isInt8) return `${defaultValue.int8Value}`;
|
|
1264
|
+
return;
|
|
1265
|
+
case "int16":
|
|
1266
|
+
if (defaultValue.isInt16) return `${defaultValue.int16Value}`;
|
|
1267
|
+
return;
|
|
1268
|
+
case "int32":
|
|
1269
|
+
if (defaultValue.isInt32) return `${defaultValue.int32Value}`;
|
|
1270
|
+
return;
|
|
1271
|
+
case "int64":
|
|
1272
|
+
if (defaultValue.isInt64) return `${defaultValue.int64Value}n`;
|
|
1273
|
+
return;
|
|
1274
|
+
case "uint8":
|
|
1275
|
+
if (defaultValue.isUint8) return `${defaultValue.uint8Value}`;
|
|
1276
|
+
return;
|
|
1277
|
+
case "uint16":
|
|
1278
|
+
if (defaultValue.isUint16) return `${defaultValue.uint16Value}`;
|
|
1279
|
+
return;
|
|
1280
|
+
case "uint32":
|
|
1281
|
+
if (defaultValue.isUint32) return `${defaultValue.uint32Value}`;
|
|
1282
|
+
return;
|
|
1283
|
+
case "uint64":
|
|
1284
|
+
if (defaultValue.isUint64) return `${defaultValue.uint64Value}n`;
|
|
1285
|
+
return;
|
|
1286
|
+
case "float32":
|
|
1287
|
+
if (defaultValue.isFloat32) {
|
|
1288
|
+
const val = defaultValue.float32Value;
|
|
1289
|
+
if (Number.isNaN(val)) return "NaN";
|
|
1290
|
+
if (val === Number.POSITIVE_INFINITY) return "Infinity";
|
|
1291
|
+
if (val === Number.NEGATIVE_INFINITY) return "-Infinity";
|
|
1292
|
+
return `${val}`;
|
|
1293
|
+
}
|
|
1294
|
+
return;
|
|
1295
|
+
case "float64":
|
|
1296
|
+
if (defaultValue.isFloat64) {
|
|
1297
|
+
const val = defaultValue.float64Value;
|
|
1298
|
+
if (Number.isNaN(val)) return "NaN";
|
|
1299
|
+
if (val === Number.POSITIVE_INFINITY) return "Infinity";
|
|
1300
|
+
if (val === Number.NEGATIVE_INFINITY) return "-Infinity";
|
|
1301
|
+
return `${val}`;
|
|
1302
|
+
}
|
|
1303
|
+
return;
|
|
1304
|
+
case "enum":
|
|
1305
|
+
if (defaultValue.isEnum) return `${defaultValue.enumValue}`;
|
|
1306
|
+
return;
|
|
1307
|
+
default: return;
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
/**
|
|
1311
|
+
* 生成 XOR 解码表达式(用于 getter)
|
|
1312
|
+
* stored ^ default = actual
|
|
1313
|
+
*/
|
|
1314
|
+
function generateXorDecode(expr, defaultValue, typeKind) {
|
|
1315
|
+
switch (typeKind) {
|
|
1316
|
+
case "bool": return `${expr} !== ${defaultValue}`;
|
|
1317
|
+
case "int8":
|
|
1318
|
+
case "int16":
|
|
1319
|
+
case "int32": return `(${expr} ^ ${defaultValue}) | 0`;
|
|
1320
|
+
case "uint8":
|
|
1321
|
+
case "uint16":
|
|
1322
|
+
case "uint32": return `${expr} ^ ${defaultValue}`;
|
|
1323
|
+
case "int64":
|
|
1324
|
+
case "uint64": return `${expr} ^ ${defaultValue}`;
|
|
1325
|
+
case "float32": return `xorFloat32(${expr}, ${defaultValue})`;
|
|
1326
|
+
case "float64": return `xorFloat64(${expr}, ${defaultValue})`;
|
|
1327
|
+
case "enum": return `${expr} ^ ${defaultValue}`;
|
|
1328
|
+
default: return expr;
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
/**
|
|
1332
|
+
* 生成 XOR 编码表达式(用于 setter)
|
|
1333
|
+
* actual ^ default = stored
|
|
1334
|
+
*/
|
|
1335
|
+
function generateXorEncode(valueExpr, defaultValue, typeKind) {
|
|
1336
|
+
switch (typeKind) {
|
|
1337
|
+
case "bool": return `${valueExpr} !== ${defaultValue}`;
|
|
1338
|
+
case "int8":
|
|
1339
|
+
case "int16":
|
|
1340
|
+
case "int32": return `(${valueExpr} >>> 0) ^ ${defaultValue}`;
|
|
1341
|
+
case "uint8":
|
|
1342
|
+
case "uint16":
|
|
1343
|
+
case "uint32": return `${valueExpr} ^ ${defaultValue}`;
|
|
1344
|
+
case "int64":
|
|
1345
|
+
case "uint64": return `${valueExpr} ^ ${defaultValue}`;
|
|
1346
|
+
case "float32": return `xorFloat32(${valueExpr}, ${defaultValue})`;
|
|
1347
|
+
case "float64": return `xorFloat64(${valueExpr}, ${defaultValue})`;
|
|
1348
|
+
case "enum": return `${valueExpr} ^ ${defaultValue}`;
|
|
1349
|
+
default: return valueExpr;
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
/**
|
|
1353
|
+
* 生成字段 getter
|
|
1354
|
+
*/
|
|
1355
|
+
function generateFieldGetter(field) {
|
|
1356
|
+
const name = field.name;
|
|
1357
|
+
const type = field.slotType;
|
|
1358
|
+
const defaultValue = extractDefaultValue(field);
|
|
1359
|
+
if (!type) return `get ${name}(): unknown { return undefined; }`;
|
|
1360
|
+
if (defaultValue !== void 0 && type.kind !== "text" && type.kind !== "data") {
|
|
1361
|
+
const decodedExpr = generateXorDecode(`this.reader.get${capitalize(type.kind)}(${getByteOffset(field, type.kind)})`, defaultValue, type.kind);
|
|
1362
|
+
return `get ${name}(): ${getTypeScriptTypeForSetter(type)} { return ${decodedExpr}; }`;
|
|
1363
|
+
}
|
|
1364
|
+
switch (type.kind) {
|
|
1365
|
+
case "void": return `get ${name}(): void { return undefined; }`;
|
|
1366
|
+
case "bool": return `get ${name}(): boolean { return this.reader.getBool(${field.slotOffset * 8}); }`;
|
|
1367
|
+
case "int8": return `get ${name}(): number { return this.reader.getInt8(${field.slotOffset}); }`;
|
|
1368
|
+
case "int16": return `get ${name}(): number { return this.reader.getInt16(${field.slotOffset * 2}); }`;
|
|
1369
|
+
case "int32": return `get ${name}(): number { return this.reader.getInt32(${field.slotOffset * 4}); }`;
|
|
1370
|
+
case "int64": return `get ${name}(): bigint { return this.reader.getInt64(${field.slotOffset * 8}); }`;
|
|
1371
|
+
case "uint8": return `get ${name}(): number { return this.reader.getUint8(${field.slotOffset}); }`;
|
|
1372
|
+
case "uint16": return `get ${name}(): number { return this.reader.getUint16(${field.slotOffset * 2}); }`;
|
|
1373
|
+
case "uint32": return `get ${name}(): number { return this.reader.getUint32(${field.slotOffset * 4}); }`;
|
|
1374
|
+
case "uint64": return `get ${name}(): bigint { return this.reader.getUint64(${field.slotOffset * 8}); }`;
|
|
1375
|
+
case "float32": return `get ${name}(): number { return this.reader.getFloat32(${field.slotOffset * 4}); }`;
|
|
1376
|
+
case "float64": return `get ${name}(): number { return this.reader.getFloat64(${field.slotOffset * 8}); }`;
|
|
1377
|
+
case "text": return `get ${name}(): string { return this.reader.getText(${field.slotOffset}); }`;
|
|
1378
|
+
case "data": return `get ${name}(): Uint8Array { return this.reader.getData(${field.slotOffset}); }`;
|
|
1379
|
+
default: return `get ${name}(): unknown { return undefined; }`;
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
/**
|
|
1383
|
+
* 获取字节偏移量
|
|
1384
|
+
*/
|
|
1385
|
+
function getByteOffset(field, typeKind) {
|
|
1386
|
+
switch (typeKind) {
|
|
1387
|
+
case "bool": return field.slotOffset * 8;
|
|
1388
|
+
case "int8":
|
|
1389
|
+
case "uint8": return field.slotOffset;
|
|
1390
|
+
case "int16":
|
|
1391
|
+
case "uint16":
|
|
1392
|
+
case "enum": return field.slotOffset * 2;
|
|
1393
|
+
case "int32":
|
|
1394
|
+
case "uint32":
|
|
1395
|
+
case "float32": return field.slotOffset * 4;
|
|
1396
|
+
case "int64":
|
|
1397
|
+
case "uint64":
|
|
1398
|
+
case "float64": return field.slotOffset * 8;
|
|
1399
|
+
default: return field.slotOffset;
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
/**
|
|
1403
|
+
* 生成字段 setter
|
|
1404
|
+
*/
|
|
1405
|
+
function generateFieldSetter(field) {
|
|
1406
|
+
const name = field.name;
|
|
1407
|
+
const type = field.slotType;
|
|
1408
|
+
const paramType = getTypeScriptTypeForSetter(type);
|
|
1409
|
+
const defaultValue = extractDefaultValue(field);
|
|
1410
|
+
if (!type) return `set${capitalize(name)}(value: unknown): void { /* TODO */ }`;
|
|
1411
|
+
if (defaultValue !== void 0 && type.kind !== "text" && type.kind !== "data") {
|
|
1412
|
+
const encodedExpr = generateXorEncode("value", defaultValue, type.kind);
|
|
1413
|
+
const setterMethod = `set${capitalize(type.kind)}`;
|
|
1414
|
+
return `set${capitalize(name)}(value: ${paramType}): void { this.builder.${setterMethod}(${getByteOffset(field, type.kind)}, ${encodedExpr}); }`;
|
|
1415
|
+
}
|
|
1416
|
+
switch (type.kind) {
|
|
1417
|
+
case "void": return `set${capitalize(name)}(value: void): void { /* void */ }`;
|
|
1418
|
+
case "bool": return `set${capitalize(name)}(value: ${paramType}): void { this.builder.setBool(${field.slotOffset * 8}, value); }`;
|
|
1419
|
+
case "int8": return `set${capitalize(name)}(value: ${paramType}): void { this.builder.setInt8(${field.slotOffset}, value); }`;
|
|
1420
|
+
case "int16": return `set${capitalize(name)}(value: ${paramType}): void { this.builder.setInt16(${field.slotOffset * 2}, value); }`;
|
|
1421
|
+
case "int32": return `set${capitalize(name)}(value: ${paramType}): void { this.builder.setInt32(${field.slotOffset * 4}, value); }`;
|
|
1422
|
+
case "int64": return `set${capitalize(name)}(value: ${paramType}): void { this.builder.setInt64(${field.slotOffset * 8}, value); }`;
|
|
1423
|
+
case "uint8": return `set${capitalize(name)}(value: ${paramType}): void { this.builder.setUint8(${field.slotOffset}, value); }`;
|
|
1424
|
+
case "uint16": return `set${capitalize(name)}(value: ${paramType}): void { this.builder.setUint16(${field.slotOffset * 2}, value); }`;
|
|
1425
|
+
case "uint32": return `set${capitalize(name)}(value: ${paramType}): void { this.builder.setUint32(${field.slotOffset * 4}, value); }`;
|
|
1426
|
+
case "uint64": return `set${capitalize(name)}(value: ${paramType}): void { this.builder.setUint64(${field.slotOffset * 8}, value); }`;
|
|
1427
|
+
case "float32": return `set${capitalize(name)}(value: ${paramType}): void { this.builder.setFloat32(${field.slotOffset * 4}, value); }`;
|
|
1428
|
+
case "float64": return `set${capitalize(name)}(value: ${paramType}): void { this.builder.setFloat64(${field.slotOffset * 8}, value); }`;
|
|
1429
|
+
case "text": return `set${capitalize(name)}(value: ${paramType}): void { this.builder.setText(${field.slotOffset}, value); }`;
|
|
1430
|
+
case "data": return `set${capitalize(name)}(value: ${paramType}): void { this.builder.setData(${field.slotOffset}, value); }`;
|
|
1431
|
+
default: return `set${capitalize(name)}(value: ${paramType}): void { /* TODO */ }`;
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* 获取 TypeScript setter 参数类型
|
|
1436
|
+
*/
|
|
1437
|
+
function getTypeScriptTypeForSetter(type) {
|
|
1438
|
+
if (!type) return "unknown";
|
|
1439
|
+
switch (type.kind) {
|
|
1440
|
+
case "void": return "void";
|
|
1441
|
+
case "bool": return "boolean";
|
|
1442
|
+
case "int8":
|
|
1443
|
+
case "int16":
|
|
1444
|
+
case "int32":
|
|
1445
|
+
case "uint8":
|
|
1446
|
+
case "uint16":
|
|
1447
|
+
case "uint32":
|
|
1448
|
+
case "float32":
|
|
1449
|
+
case "float64": return "number";
|
|
1450
|
+
case "int64":
|
|
1451
|
+
case "uint64": return "bigint";
|
|
1452
|
+
case "text": return "string";
|
|
1453
|
+
case "data": return "Uint8Array";
|
|
1454
|
+
default: return "unknown";
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
/**
|
|
1458
|
+
* 首字母大写
|
|
1459
|
+
*/
|
|
1460
|
+
function capitalize(str) {
|
|
1461
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
1462
|
+
}
|
|
1463
|
+
/**
|
|
1464
|
+
* 生成 Enum 的 TypeScript 代码
|
|
1465
|
+
*/
|
|
1466
|
+
function generateEnum(node) {
|
|
1467
|
+
const lines = [];
|
|
1468
|
+
const enumName = getShortName(node.displayName);
|
|
1469
|
+
lines.push(`export enum ${enumName} {`);
|
|
1470
|
+
for (const enumerant of node.enumEnumerants) lines.push(` ${enumerant.name} = ${enumerant.codeOrder},`);
|
|
1471
|
+
lines.push("}");
|
|
1472
|
+
return lines.join("\n");
|
|
1473
|
+
}
|
|
1474
|
+
/**
|
|
1475
|
+
* 获取 TypeScript 类型字符串
|
|
1476
|
+
*/
|
|
1477
|
+
function getTypeScriptType(type, allNodes) {
|
|
1478
|
+
if (!type) return "unknown";
|
|
1479
|
+
switch (type.kind) {
|
|
1480
|
+
case "void": return "void";
|
|
1481
|
+
case "bool": return "boolean";
|
|
1482
|
+
case "int8":
|
|
1483
|
+
case "int16":
|
|
1484
|
+
case "int32":
|
|
1485
|
+
case "uint8":
|
|
1486
|
+
case "uint16":
|
|
1487
|
+
case "uint32":
|
|
1488
|
+
case "float32":
|
|
1489
|
+
case "float64": return "number";
|
|
1490
|
+
case "int64":
|
|
1491
|
+
case "uint64": return "bigint";
|
|
1492
|
+
case "text": return "string";
|
|
1493
|
+
case "data": return "Uint8Array";
|
|
1494
|
+
case "list": {
|
|
1495
|
+
const elementType = type.listElementType;
|
|
1496
|
+
if (!elementType) return "unknown[]";
|
|
1497
|
+
return `${getTypeScriptType(elementType, allNodes)}[]`;
|
|
1498
|
+
}
|
|
1499
|
+
case "struct": {
|
|
1500
|
+
const structNode = allNodes.find((n) => n.id === type.typeId);
|
|
1501
|
+
if (structNode) return getShortName(structNode.displayName);
|
|
1502
|
+
return "unknown";
|
|
1503
|
+
}
|
|
1504
|
+
case "enum": {
|
|
1505
|
+
const enumNode = allNodes.find((n) => n.id === type.typeId);
|
|
1506
|
+
if (enumNode) return getShortName(enumNode.displayName);
|
|
1507
|
+
return "unknown";
|
|
1508
|
+
}
|
|
1509
|
+
default: return "unknown";
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
/**
|
|
1513
|
+
* 从 displayName 获取短名称
|
|
1514
|
+
* 例如 "test-schema.capnp:Person" -> "Person"
|
|
1515
|
+
*/
|
|
1516
|
+
function getShortName(displayName) {
|
|
1517
|
+
const colonIndex = displayName.lastIndexOf(":");
|
|
1518
|
+
if (colonIndex >= 0) return displayName.substring(colonIndex + 1);
|
|
1519
|
+
return displayName.replace(/\.capnp$/, "").replace(/[^a-zA-Z0-9_]/g, "_");
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
//#endregion
|
|
1523
|
+
//#region src/codegen/cli-v3.ts
|
|
1524
|
+
/**
|
|
1525
|
+
* Cap'n Proto TypeScript Code Generator CLI v3
|
|
1526
|
+
*
|
|
1527
|
+
* 使用官方 capnp 编译器生成的 binary schema 来生成 TypeScript 代码
|
|
1528
|
+
* 这是推荐的生产环境方案
|
|
1529
|
+
*/
|
|
1530
|
+
const { values, positionals } = parseArgs({
|
|
1531
|
+
args: process.argv.slice(2),
|
|
1532
|
+
options: {
|
|
1533
|
+
output: {
|
|
1534
|
+
type: "string",
|
|
1535
|
+
short: "o"
|
|
1536
|
+
},
|
|
1537
|
+
outDir: {
|
|
1538
|
+
type: "string",
|
|
1539
|
+
short: "d"
|
|
1540
|
+
},
|
|
1541
|
+
runtimePath: {
|
|
1542
|
+
type: "string",
|
|
1543
|
+
short: "r"
|
|
1544
|
+
},
|
|
1545
|
+
help: {
|
|
1546
|
+
type: "boolean",
|
|
1547
|
+
short: "h"
|
|
1548
|
+
},
|
|
1549
|
+
version: {
|
|
1550
|
+
type: "boolean",
|
|
1551
|
+
short: "v"
|
|
1552
|
+
}
|
|
1553
|
+
},
|
|
1554
|
+
allowPositionals: true
|
|
1555
|
+
});
|
|
1556
|
+
const VERSION = "3.0.0";
|
|
1557
|
+
if (values.version) {
|
|
1558
|
+
console.log(VERSION);
|
|
1559
|
+
process.exit(0);
|
|
1560
|
+
}
|
|
1561
|
+
if (values.help || positionals.length === 0) {
|
|
1562
|
+
console.log(`
|
|
1563
|
+
Cap'n Proto TypeScript Code Generator v3
|
|
1564
|
+
|
|
1565
|
+
Usage: capnp-ts-codegen <schema.capnp> [options]
|
|
1566
|
+
|
|
1567
|
+
Options:
|
|
1568
|
+
-o, --output Output file (default: stdout for single file)
|
|
1569
|
+
-d, --outDir Output directory for multiple files
|
|
1570
|
+
-r, --runtimePath Runtime library import path (default: @naeemo/capnp)
|
|
1571
|
+
-h, --help Show this help
|
|
1572
|
+
-v, --version Show version
|
|
1573
|
+
|
|
1574
|
+
Examples:
|
|
1575
|
+
capnp-ts-codegen schema.capnp -o schema.ts
|
|
1576
|
+
capnp-ts-codegen schema.capnp -d ./generated
|
|
1577
|
+
capnp-ts-codegen schema.capnp -o schema.ts -r ../runtime
|
|
1578
|
+
|
|
1579
|
+
Features:
|
|
1580
|
+
- Struct with all primitive types
|
|
1581
|
+
- Enum
|
|
1582
|
+
- List<T>
|
|
1583
|
+
- Text, Data
|
|
1584
|
+
- Nested struct references
|
|
1585
|
+
- Union (discriminant handling)
|
|
1586
|
+
- Group
|
|
1587
|
+
- Default values (XOR encoding)
|
|
1588
|
+
- Multi-segment messages
|
|
1589
|
+
|
|
1590
|
+
Not yet supported:
|
|
1591
|
+
- Interface
|
|
1592
|
+
- Const
|
|
1593
|
+
- RPC
|
|
1594
|
+
`);
|
|
1595
|
+
process.exit(0);
|
|
1596
|
+
}
|
|
1597
|
+
const inputFile = positionals[0];
|
|
1598
|
+
if (!existsSync(inputFile)) {
|
|
1599
|
+
console.error(`Error: File not found: ${inputFile}`);
|
|
1600
|
+
process.exit(1);
|
|
1601
|
+
}
|
|
1602
|
+
function checkCapnpTool() {
|
|
1603
|
+
try {
|
|
1604
|
+
execSync("capnp --version", { stdio: "ignore" });
|
|
1605
|
+
return true;
|
|
1606
|
+
} catch {
|
|
1607
|
+
return false;
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
async function main() {
|
|
1611
|
+
if (!checkCapnpTool()) {
|
|
1612
|
+
console.error("Error: capnp tool not found. Please install Cap'n Proto.");
|
|
1613
|
+
console.error(" macOS: brew install capnp");
|
|
1614
|
+
console.error(" Ubuntu/Debian: apt-get install capnproto");
|
|
1615
|
+
console.error(" Other: https://capnproto.org/install.html");
|
|
1616
|
+
process.exit(1);
|
|
1617
|
+
}
|
|
1618
|
+
const tmpDir = mkdtempSync(join(tmpdir(), "capnp-ts-"));
|
|
1619
|
+
const binFile = join(tmpDir, "schema.bin");
|
|
1620
|
+
try {
|
|
1621
|
+
console.error(`Compiling ${inputFile}...`);
|
|
1622
|
+
const inputDir = dirname(inputFile);
|
|
1623
|
+
execSync(`capnp compile -o- "${inputFile}" > "${binFile}"`, {
|
|
1624
|
+
cwd: inputDir,
|
|
1625
|
+
stdio: [
|
|
1626
|
+
"ignore",
|
|
1627
|
+
"ignore",
|
|
1628
|
+
"pipe"
|
|
1629
|
+
]
|
|
1630
|
+
});
|
|
1631
|
+
console.error("Reading binary schema...");
|
|
1632
|
+
const buffer = readFileSync(binFile);
|
|
1633
|
+
const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
|
1634
|
+
console.error("Generating TypeScript code...");
|
|
1635
|
+
const files = generateFromRequest(CodeGeneratorRequestReader.fromBuffer(arrayBuffer), { runtimeImportPath: values.runtimePath || "@naeemo/capnp" });
|
|
1636
|
+
if (values.outDir) {
|
|
1637
|
+
mkdirSync(values.outDir, { recursive: true });
|
|
1638
|
+
for (const [filename, code] of files) {
|
|
1639
|
+
const outPath = join(values.outDir, filename);
|
|
1640
|
+
mkdirSync(dirname(outPath), { recursive: true });
|
|
1641
|
+
writeFileSync(outPath, code);
|
|
1642
|
+
console.error(`Generated: ${outPath}`);
|
|
1643
|
+
}
|
|
1644
|
+
} else if (values.output) {
|
|
1645
|
+
const firstFile = files.values().next().value;
|
|
1646
|
+
if (firstFile) {
|
|
1647
|
+
writeFileSync(values.output, firstFile);
|
|
1648
|
+
console.error(`Output written to ${values.output}`);
|
|
1649
|
+
} else {
|
|
1650
|
+
console.error("Error: No files generated");
|
|
1651
|
+
process.exit(1);
|
|
1652
|
+
}
|
|
1653
|
+
} else {
|
|
1654
|
+
const firstFile = files.values().next().value;
|
|
1655
|
+
if (firstFile) console.log(firstFile);
|
|
1656
|
+
else {
|
|
1657
|
+
console.error("Error: No files generated");
|
|
1658
|
+
process.exit(1);
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
} catch (err) {
|
|
1662
|
+
console.error("Error:", err.message);
|
|
1663
|
+
if (err.stderr) console.error(err.stderr.toString());
|
|
1664
|
+
process.exit(1);
|
|
1665
|
+
} finally {
|
|
1666
|
+
rmSync(tmpDir, {
|
|
1667
|
+
recursive: true,
|
|
1668
|
+
force: true
|
|
1669
|
+
});
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
main();
|
|
1673
|
+
|
|
1674
|
+
//#endregion
|
|
1675
|
+
export { };
|
|
1676
|
+
//# sourceMappingURL=cli-v3.js.map
|