deukpack 1.0.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.
Files changed (122) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +6 -0
  3. package/README.ko.md +138 -0
  4. package/README.md +182 -0
  5. package/RELEASING.md +71 -0
  6. package/bin/deukpack.js +9 -0
  7. package/dist/ast/DeukPackASTBuilder.d.ts +153 -0
  8. package/dist/ast/DeukPackASTBuilder.d.ts.map +1 -0
  9. package/dist/ast/DeukPackASTBuilder.js +931 -0
  10. package/dist/ast/DeukPackASTBuilder.js.map +1 -0
  11. package/dist/codegen/CSharpGenerator.d.ts +136 -0
  12. package/dist/codegen/CSharpGenerator.d.ts.map +1 -0
  13. package/dist/codegen/CSharpGenerator.js +2303 -0
  14. package/dist/codegen/CSharpGenerator.js.map +1 -0
  15. package/dist/codegen/CodeGenerator.d.ts +11 -0
  16. package/dist/codegen/CodeGenerator.d.ts.map +1 -0
  17. package/dist/codegen/CodeGenerator.js +11 -0
  18. package/dist/codegen/CodeGenerator.js.map +1 -0
  19. package/dist/codegen/CppGenerator.d.ts +23 -0
  20. package/dist/codegen/CppGenerator.d.ts.map +1 -0
  21. package/dist/codegen/CppGenerator.js +215 -0
  22. package/dist/codegen/CppGenerator.js.map +1 -0
  23. package/dist/codegen/HighPerformanceCSharpGenerator.d.ts +29 -0
  24. package/dist/codegen/HighPerformanceCSharpGenerator.d.ts.map +1 -0
  25. package/dist/codegen/HighPerformanceCSharpGenerator.js +486 -0
  26. package/dist/codegen/HighPerformanceCSharpGenerator.js.map +1 -0
  27. package/dist/core/DeukPackEngine.d.ts +69 -0
  28. package/dist/core/DeukPackEngine.d.ts.map +1 -0
  29. package/dist/core/DeukPackEngine.js +379 -0
  30. package/dist/core/DeukPackEngine.js.map +1 -0
  31. package/dist/core/DeukPackGenerator.d.ts +9 -0
  32. package/dist/core/DeukPackGenerator.d.ts.map +1 -0
  33. package/dist/core/DeukPackGenerator.js +15 -0
  34. package/dist/core/DeukPackGenerator.js.map +1 -0
  35. package/dist/core/DeukParser.d.ts +12 -0
  36. package/dist/core/DeukParser.d.ts.map +1 -0
  37. package/dist/core/DeukParser.js +27 -0
  38. package/dist/core/DeukParser.js.map +1 -0
  39. package/dist/core/IdlParser.d.ts +27 -0
  40. package/dist/core/IdlParser.d.ts.map +1 -0
  41. package/dist/core/IdlParser.js +157 -0
  42. package/dist/core/IdlParser.js.map +1 -0
  43. package/dist/core/ProtoParser.d.ts +12 -0
  44. package/dist/core/ProtoParser.d.ts.map +1 -0
  45. package/dist/core/ProtoParser.js +27 -0
  46. package/dist/core/ProtoParser.js.map +1 -0
  47. package/dist/csharp/DpExcelProtocol.cs +3005 -0
  48. package/dist/csharp/DpProtocolLibrary.cs +13 -0
  49. package/dist/index.d.ts +22 -0
  50. package/dist/index.d.ts.map +1 -0
  51. package/dist/index.js +43 -0
  52. package/dist/index.js.map +1 -0
  53. package/dist/lexer/DeukLexer.d.ts +31 -0
  54. package/dist/lexer/DeukLexer.d.ts.map +1 -0
  55. package/dist/lexer/DeukLexer.js +292 -0
  56. package/dist/lexer/DeukLexer.js.map +1 -0
  57. package/dist/lexer/IdlLexer.d.ts +33 -0
  58. package/dist/lexer/IdlLexer.d.ts.map +1 -0
  59. package/dist/lexer/IdlLexer.js +286 -0
  60. package/dist/lexer/IdlLexer.js.map +1 -0
  61. package/dist/native/NativeDeukPackEngine.d.ts +30 -0
  62. package/dist/native/NativeDeukPackEngine.d.ts.map +1 -0
  63. package/dist/native/NativeDeukPackEngine.js +99 -0
  64. package/dist/native/NativeDeukPackEngine.js.map +1 -0
  65. package/dist/proto/ProtoASTBuilder.d.ts +29 -0
  66. package/dist/proto/ProtoASTBuilder.d.ts.map +1 -0
  67. package/dist/proto/ProtoASTBuilder.js +239 -0
  68. package/dist/proto/ProtoASTBuilder.js.map +1 -0
  69. package/dist/proto/ProtoLexer.d.ts +29 -0
  70. package/dist/proto/ProtoLexer.d.ts.map +1 -0
  71. package/dist/proto/ProtoLexer.js +264 -0
  72. package/dist/proto/ProtoLexer.js.map +1 -0
  73. package/dist/proto/ProtoTypes.d.ts +40 -0
  74. package/dist/proto/ProtoTypes.d.ts.map +1 -0
  75. package/dist/proto/ProtoTypes.js +37 -0
  76. package/dist/proto/ProtoTypes.js.map +1 -0
  77. package/dist/protocols/BinaryProtocol.d.ts +7 -0
  78. package/dist/protocols/BinaryProtocol.d.ts.map +1 -0
  79. package/dist/protocols/BinaryProtocol.js +11 -0
  80. package/dist/protocols/BinaryProtocol.js.map +1 -0
  81. package/dist/protocols/BinaryWriter.d.ts +22 -0
  82. package/dist/protocols/BinaryWriter.d.ts.map +1 -0
  83. package/dist/protocols/BinaryWriter.js +104 -0
  84. package/dist/protocols/BinaryWriter.js.map +1 -0
  85. package/dist/protocols/CompactProtocol.d.ts +7 -0
  86. package/dist/protocols/CompactProtocol.d.ts.map +1 -0
  87. package/dist/protocols/CompactProtocol.js +11 -0
  88. package/dist/protocols/CompactProtocol.js.map +1 -0
  89. package/dist/protocols/ExcelProtocol.d.ts +98 -0
  90. package/dist/protocols/ExcelProtocol.d.ts.map +1 -0
  91. package/dist/protocols/ExcelProtocol.js +639 -0
  92. package/dist/protocols/ExcelProtocol.js.map +1 -0
  93. package/dist/protocols/JsonProtocol.d.ts +68 -0
  94. package/dist/protocols/JsonProtocol.d.ts.map +1 -0
  95. package/dist/protocols/JsonProtocol.js +422 -0
  96. package/dist/protocols/JsonProtocol.js.map +1 -0
  97. package/dist/protocols/WireProtocol.d.ts +348 -0
  98. package/dist/protocols/WireProtocol.d.ts.map +1 -0
  99. package/dist/protocols/WireProtocol.js +912 -0
  100. package/dist/protocols/WireProtocol.js.map +1 -0
  101. package/dist/serialization/WireDeserializer.d.ts +8 -0
  102. package/dist/serialization/WireDeserializer.d.ts.map +1 -0
  103. package/dist/serialization/WireDeserializer.js +13 -0
  104. package/dist/serialization/WireDeserializer.js.map +1 -0
  105. package/dist/serialization/WireSerializer.d.ts +20 -0
  106. package/dist/serialization/WireSerializer.d.ts.map +1 -0
  107. package/dist/serialization/WireSerializer.js +100 -0
  108. package/dist/serialization/WireSerializer.js.map +1 -0
  109. package/dist/types/DeukPackTypes.d.ts +291 -0
  110. package/dist/types/DeukPackTypes.d.ts.map +1 -0
  111. package/dist/types/DeukPackTypes.js +76 -0
  112. package/dist/types/DeukPackTypes.js.map +1 -0
  113. package/dist/utils/EndianUtils.d.ts +11 -0
  114. package/dist/utils/EndianUtils.d.ts.map +1 -0
  115. package/dist/utils/EndianUtils.js +32 -0
  116. package/dist/utils/EndianUtils.js.map +1 -0
  117. package/dist/utils/PerformanceMonitor.d.ts +26 -0
  118. package/dist/utils/PerformanceMonitor.d.ts.map +1 -0
  119. package/dist/utils/PerformanceMonitor.js +57 -0
  120. package/dist/utils/PerformanceMonitor.js.map +1 -0
  121. package/package.json +77 -0
  122. package/scripts/build_deukpack.js +669 -0
@@ -0,0 +1,912 @@
1
+ "use strict";
2
+ /**
3
+ * DeukPack Wire Protocol
4
+ * DpProtocol interface and implementations.
5
+ * - DpBinaryProtocol, DpCompactProtocol: Apache Thrift wire-compatible (binary/compact).
6
+ * - DpPackProtocol: DeukPack-native (magic 0x44 0x50 0x01); detectable via detectWireProtocol().
7
+ * - 엔벨로프(writeEnvelope/readEnvelope): 팩 시 프로토콜 타입 저장 → 언팩 시 타입으로 디코더 선택. 기본(성능·메모리 권장): pack.
8
+ * - Cross-platform serialization with endian support.
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.DpPackProtocol = exports.DP_WIRE_PROTOCOL_ZEROCOPY = exports.DP_WIRE_PROTOCOL_PACK = exports.DP_WIRE_PROTOCOL_COMPACT = exports.DP_WIRE_PROTOCOL_BINARY = exports.DP_WIRE_ENVELOPE_MAGIC1 = exports.DP_WIRE_ENVELOPE_MAGIC0 = exports.DP_WIRE_ENVELOPE_SIZE = exports.DP_PACK_MAGIC = exports.DpCompactProtocol = exports.DpZeroCopyProtocol = exports.DpBinaryProtocol = exports.DpMessageType = exports.DpWireType = void 0;
12
+ exports.writeEnvelope = writeEnvelope;
13
+ exports.readEnvelope = readEnvelope;
14
+ exports.detectWireProtocol = detectWireProtocol;
15
+ exports.createProtocolForUnpack = createProtocolForUnpack;
16
+ var DpWireType;
17
+ (function (DpWireType) {
18
+ DpWireType[DpWireType["Stop"] = 0] = "Stop";
19
+ DpWireType[DpWireType["Void"] = 1] = "Void";
20
+ DpWireType[DpWireType["Bool"] = 2] = "Bool";
21
+ DpWireType[DpWireType["Byte"] = 3] = "Byte";
22
+ DpWireType[DpWireType["Double"] = 4] = "Double";
23
+ DpWireType[DpWireType["I16"] = 6] = "I16";
24
+ DpWireType[DpWireType["I32"] = 8] = "I32";
25
+ DpWireType[DpWireType["I64"] = 10] = "I64";
26
+ DpWireType[DpWireType["String"] = 11] = "String";
27
+ DpWireType[DpWireType["Struct"] = 12] = "Struct";
28
+ DpWireType[DpWireType["Map"] = 13] = "Map";
29
+ DpWireType[DpWireType["Set"] = 14] = "Set";
30
+ DpWireType[DpWireType["List"] = 15] = "List";
31
+ })(DpWireType || (exports.DpWireType = DpWireType = {}));
32
+ var DpMessageType;
33
+ (function (DpMessageType) {
34
+ DpMessageType[DpMessageType["Call"] = 1] = "Call";
35
+ DpMessageType[DpMessageType["Reply"] = 2] = "Reply";
36
+ DpMessageType[DpMessageType["Exception"] = 3] = "Exception";
37
+ DpMessageType[DpMessageType["Oneway"] = 4] = "Oneway";
38
+ })(DpMessageType || (exports.DpMessageType = DpMessageType = {}));
39
+ /**
40
+ * Thrift-compatible binary protocol (fixed-width fields, type+id per field).
41
+ * Endianness configurable (default little-endian); Apache Thrift binary is big-endian.
42
+ */
43
+ class DpBinaryProtocol {
44
+ constructor(buffer, littleEndian = true) {
45
+ this.buffer = buffer;
46
+ this.view = new DataView(buffer);
47
+ this.offset = 0;
48
+ this.littleEndian = littleEndian;
49
+ }
50
+ // Write methods
51
+ writeStructBegin(_struct) {
52
+ // Binary protocol doesn't write struct names
53
+ }
54
+ writeStructEnd() {
55
+ // Binary protocol doesn't write struct end markers
56
+ }
57
+ writeFieldBegin(field) {
58
+ this.writeByte(field.type);
59
+ this.writeI16(field.id);
60
+ }
61
+ writeFieldEnd() {
62
+ // Binary protocol doesn't write field end markers
63
+ }
64
+ writeFieldStop() {
65
+ this.writeByte(DpWireType.Stop);
66
+ }
67
+ writeBool(value) {
68
+ this.writeByte(value ? 1 : 0);
69
+ }
70
+ writeByte(value) {
71
+ this.view.setUint8(this.offset, value);
72
+ this.offset += 1;
73
+ }
74
+ writeI16(value) {
75
+ this.view.setInt16(this.offset, value, this.littleEndian);
76
+ this.offset += 2;
77
+ }
78
+ writeI32(value) {
79
+ this.view.setInt32(this.offset, value, this.littleEndian);
80
+ this.offset += 4;
81
+ }
82
+ writeI64(value) {
83
+ this.view.setBigInt64(this.offset, value, this.littleEndian);
84
+ this.offset += 8;
85
+ }
86
+ writeDouble(value) {
87
+ this.view.setFloat64(this.offset, value, this.littleEndian);
88
+ this.offset += 8;
89
+ }
90
+ writeString(value) {
91
+ const encoder = new TextEncoder();
92
+ const bytes = encoder.encode(value);
93
+ this.writeI32(bytes.length);
94
+ this.writeBinary(new Uint8Array(bytes));
95
+ }
96
+ writeBinary(value) {
97
+ this.writeI32(value.length);
98
+ new Uint8Array(this.buffer, this.offset, value.length).set(value);
99
+ this.offset += value.length;
100
+ }
101
+ writeListBegin(list) {
102
+ this.writeByte(list.elementType);
103
+ this.writeI32(list.count);
104
+ }
105
+ writeListEnd() {
106
+ // Binary protocol doesn't write list end markers
107
+ }
108
+ writeSetBegin(set) {
109
+ this.writeByte(set.elementType);
110
+ this.writeI32(set.count);
111
+ }
112
+ writeSetEnd() {
113
+ // Binary protocol doesn't write set end markers
114
+ }
115
+ writeMapBegin(map) {
116
+ this.writeByte(map.keyType);
117
+ this.writeByte(map.valueType);
118
+ this.writeI32(map.count);
119
+ }
120
+ writeMapEnd() {
121
+ // Binary protocol doesn't write map end markers
122
+ }
123
+ // Read methods
124
+ readStructBegin() {
125
+ return { name: '' };
126
+ }
127
+ readStructEnd() {
128
+ // Binary protocol doesn't read struct end markers
129
+ }
130
+ readFieldBegin() {
131
+ const type = this.readByte();
132
+ if (type === DpWireType.Stop) {
133
+ return { name: '', type: DpWireType.Stop, id: 0 };
134
+ }
135
+ const id = this.readI16();
136
+ return { name: '', type: type, id };
137
+ }
138
+ readFieldEnd() {
139
+ // Binary protocol doesn't read field end markers
140
+ }
141
+ readBool() {
142
+ return this.readByte() !== 0;
143
+ }
144
+ readByte() {
145
+ const value = this.view.getUint8(this.offset);
146
+ this.offset += 1;
147
+ return value;
148
+ }
149
+ readI16() {
150
+ const value = this.view.getInt16(this.offset, this.littleEndian);
151
+ this.offset += 2;
152
+ return value;
153
+ }
154
+ readI32() {
155
+ const value = this.view.getInt32(this.offset, this.littleEndian);
156
+ this.offset += 4;
157
+ return value;
158
+ }
159
+ readI64() {
160
+ const value = this.view.getBigInt64(this.offset, this.littleEndian);
161
+ this.offset += 8;
162
+ return value;
163
+ }
164
+ readDouble() {
165
+ const value = this.view.getFloat64(this.offset, this.littleEndian);
166
+ this.offset += 8;
167
+ return value;
168
+ }
169
+ readString() {
170
+ const length = this.readI32();
171
+ const bytes = new Uint8Array(this.buffer, this.offset, length);
172
+ this.offset += length;
173
+ const decoder = new TextDecoder();
174
+ return decoder.decode(bytes);
175
+ }
176
+ readBinary() {
177
+ const length = this.readI32();
178
+ const bytes = new Uint8Array(this.buffer, this.offset, length);
179
+ this.offset += length;
180
+ return bytes;
181
+ }
182
+ readListBegin() {
183
+ const elementType = this.readByte();
184
+ const count = this.readI32();
185
+ return { elementType: elementType, count };
186
+ }
187
+ readListEnd() {
188
+ // Binary protocol doesn't read list end markers
189
+ }
190
+ readSetBegin() {
191
+ const elementType = this.readByte();
192
+ const count = this.readI32();
193
+ return { elementType: elementType, count };
194
+ }
195
+ readSetEnd() {
196
+ // Binary protocol doesn't read set end markers
197
+ }
198
+ readMapBegin() {
199
+ const keyType = this.readByte();
200
+ const valueType = this.readByte();
201
+ const count = this.readI32();
202
+ return { keyType: keyType, valueType: valueType, count };
203
+ }
204
+ readMapEnd() {
205
+ // Binary protocol doesn't read map end markers
206
+ }
207
+ // Utility methods
208
+ getBuffer() {
209
+ return this.buffer.slice(0, this.offset);
210
+ }
211
+ getOffset() {
212
+ return this.offset;
213
+ }
214
+ setOffset(offset) {
215
+ this.offset = offset;
216
+ }
217
+ }
218
+ exports.DpBinaryProtocol = DpBinaryProtocol;
219
+ /**
220
+ * 제로카피 프로토콜 (Zero-Copy Protocol). 기본 선택사항 아님 — 사용자가 writeEnvelope(..., 'zerocopy') 또는 엔벨로프 id 3으로 명시할 때만 사용.
221
+ * Wire 포맷은 Thrift Binary와 동일. read 시 버퍼 복사 없이 뷰(Uint8Array) 반환.
222
+ * 주의: 반환된 뷰 사용 중에는 원본 버퍼를 수정하지 말 것. 버퍼 수명이 뷰보다 길어야 함.
223
+ */
224
+ class DpZeroCopyProtocol {
225
+ constructor(buffer, littleEndian = true) {
226
+ this.buffer = buffer;
227
+ this.view = new DataView(buffer);
228
+ this.offset = 0;
229
+ this.littleEndian = littleEndian;
230
+ }
231
+ writeStructBegin(_struct) { }
232
+ writeStructEnd() { }
233
+ writeFieldBegin(field) {
234
+ this.writeByte(field.type);
235
+ this.writeI16(field.id);
236
+ }
237
+ writeFieldEnd() { }
238
+ writeFieldStop() {
239
+ this.writeByte(DpWireType.Stop);
240
+ }
241
+ writeBool(value) {
242
+ this.writeByte(value ? 1 : 0);
243
+ }
244
+ writeByte(value) {
245
+ this.view.setUint8(this.offset, value);
246
+ this.offset += 1;
247
+ }
248
+ writeI16(value) {
249
+ this.view.setInt16(this.offset, value, this.littleEndian);
250
+ this.offset += 2;
251
+ }
252
+ writeI32(value) {
253
+ this.view.setInt32(this.offset, value, this.littleEndian);
254
+ this.offset += 4;
255
+ }
256
+ writeI64(value) {
257
+ this.view.setBigInt64(this.offset, value, this.littleEndian);
258
+ this.offset += 8;
259
+ }
260
+ writeDouble(value) {
261
+ this.view.setFloat64(this.offset, value, this.littleEndian);
262
+ this.offset += 8;
263
+ }
264
+ writeString(value) {
265
+ const encoder = new TextEncoder();
266
+ const bytes = encoder.encode(value);
267
+ this.writeI32(bytes.length);
268
+ this.writeBinary(new Uint8Array(bytes));
269
+ }
270
+ writeBinary(value) {
271
+ this.writeI32(value.length);
272
+ new Uint8Array(this.buffer, this.offset, value.length).set(value);
273
+ this.offset += value.length;
274
+ }
275
+ writeListBegin(list) {
276
+ this.writeByte(list.elementType);
277
+ this.writeI32(list.count);
278
+ }
279
+ writeListEnd() { }
280
+ writeSetBegin(set) {
281
+ this.writeByte(set.elementType);
282
+ this.writeI32(set.count);
283
+ }
284
+ writeSetEnd() { }
285
+ writeMapBegin(map) {
286
+ this.writeByte(map.keyType);
287
+ this.writeByte(map.valueType);
288
+ this.writeI32(map.count);
289
+ }
290
+ writeMapEnd() { }
291
+ readStructBegin() {
292
+ return { name: '' };
293
+ }
294
+ readStructEnd() { }
295
+ readFieldBegin() {
296
+ const type = this.readByte();
297
+ if (type === DpWireType.Stop) {
298
+ return { name: '', type: DpWireType.Stop, id: 0 };
299
+ }
300
+ const id = this.readI16();
301
+ return { name: '', type: type, id };
302
+ }
303
+ readFieldEnd() { }
304
+ readBool() {
305
+ return this.readByte() !== 0;
306
+ }
307
+ readByte() {
308
+ const value = this.view.getUint8(this.offset);
309
+ this.offset += 1;
310
+ return value;
311
+ }
312
+ readI16() {
313
+ const value = this.view.getInt16(this.offset, this.littleEndian);
314
+ this.offset += 2;
315
+ return value;
316
+ }
317
+ readI32() {
318
+ const value = this.view.getInt32(this.offset, this.littleEndian);
319
+ this.offset += 4;
320
+ return value;
321
+ }
322
+ readI64() {
323
+ const value = this.view.getBigInt64(this.offset, this.littleEndian);
324
+ this.offset += 8;
325
+ return value;
326
+ }
327
+ readDouble() {
328
+ const value = this.view.getFloat64(this.offset, this.littleEndian);
329
+ this.offset += 8;
330
+ return value;
331
+ }
332
+ readString() {
333
+ const length = this.readI32();
334
+ const bytes = new Uint8Array(this.buffer, this.offset, length);
335
+ this.offset += length;
336
+ return new TextDecoder().decode(bytes);
337
+ }
338
+ /** 제로카피: 원본 버퍼에 대한 뷰 반환. 버퍼 수명·불변성 책임은 호출자. */
339
+ readBinary() {
340
+ const length = this.readI32();
341
+ const view = new Uint8Array(this.buffer, this.offset, length);
342
+ this.offset += length;
343
+ return view;
344
+ }
345
+ readListBegin() {
346
+ const elementType = this.readByte();
347
+ const count = this.readI32();
348
+ return { elementType: elementType, count };
349
+ }
350
+ readListEnd() { }
351
+ readSetBegin() {
352
+ const elementType = this.readByte();
353
+ const count = this.readI32();
354
+ return { elementType: elementType, count };
355
+ }
356
+ readSetEnd() { }
357
+ readMapBegin() {
358
+ const keyType = this.readByte();
359
+ const valueType = this.readByte();
360
+ const count = this.readI32();
361
+ return { keyType: keyType, valueType: valueType, count };
362
+ }
363
+ readMapEnd() { }
364
+ getBuffer() {
365
+ return this.buffer.slice(0, this.offset);
366
+ }
367
+ getOffset() {
368
+ return this.offset;
369
+ }
370
+ setOffset(offset) {
371
+ this.offset = offset;
372
+ }
373
+ }
374
+ exports.DpZeroCopyProtocol = DpZeroCopyProtocol;
375
+ /**
376
+ * Thrift-compatible compact protocol (varint encoding, smaller payload).
377
+ * Same wire format family as Apache Thrift compact.
378
+ */
379
+ class DpCompactProtocol {
380
+ constructor(buffer, littleEndian = true) {
381
+ this.buffer = buffer;
382
+ this.view = new DataView(buffer);
383
+ this.offset = 0;
384
+ this.littleEndian = littleEndian;
385
+ }
386
+ // Compact protocol uses variable-length encoding
387
+ writeStructBegin(_struct) {
388
+ // Compact protocol doesn't write struct names
389
+ }
390
+ writeStructEnd() {
391
+ // Compact protocol doesn't write struct end markers
392
+ }
393
+ writeFieldBegin(field) {
394
+ if (field.type === DpWireType.Stop) {
395
+ this.writeByte(DpWireType.Stop);
396
+ }
397
+ else {
398
+ this.writeByte((field.id << 4) | field.type);
399
+ }
400
+ }
401
+ writeFieldEnd() {
402
+ // Compact protocol doesn't write field end markers
403
+ }
404
+ writeFieldStop() {
405
+ this.writeByte(DpWireType.Stop);
406
+ }
407
+ writeBool(value) {
408
+ this.writeByte(value ? 1 : 0);
409
+ }
410
+ writeByte(value) {
411
+ this.view.setUint8(this.offset, value);
412
+ this.offset += 1;
413
+ }
414
+ writeI16(value) {
415
+ this.writeVarint(value);
416
+ }
417
+ writeI32(value) {
418
+ this.writeVarint(value);
419
+ }
420
+ writeI64(value) {
421
+ this.writeVarint64(value);
422
+ }
423
+ writeDouble(value) {
424
+ this.view.setFloat64(this.offset, value, this.littleEndian);
425
+ this.offset += 8;
426
+ }
427
+ writeString(value) {
428
+ const encoder = new TextEncoder();
429
+ const bytes = encoder.encode(value);
430
+ this.writeVarint(bytes.length);
431
+ this.writeBinary(new Uint8Array(bytes));
432
+ }
433
+ writeBinary(value) {
434
+ this.writeVarint(value.length);
435
+ new Uint8Array(this.buffer, this.offset, value.length).set(value);
436
+ this.offset += value.length;
437
+ }
438
+ writeListBegin(list) {
439
+ this.writeByte((list.elementType << 4) | (list.count < 15 ? list.count : 15));
440
+ if (list.count >= 15) {
441
+ this.writeVarint(list.count - 15);
442
+ }
443
+ }
444
+ writeListEnd() {
445
+ // Compact protocol doesn't write list end markers
446
+ }
447
+ writeSetBegin(set) {
448
+ this.writeListBegin({ elementType: set.elementType, count: set.count });
449
+ }
450
+ writeSetEnd() {
451
+ // Compact protocol doesn't write set end markers
452
+ }
453
+ writeMapBegin(map) {
454
+ this.writeVarint(map.count);
455
+ if (map.count > 0) {
456
+ this.writeByte((map.keyType << 4) | map.valueType);
457
+ }
458
+ }
459
+ writeMapEnd() {
460
+ // Compact protocol doesn't write map end markers
461
+ }
462
+ // Read methods (similar to binary protocol but with varint decoding)
463
+ readStructBegin() {
464
+ return { name: '' };
465
+ }
466
+ readStructEnd() {
467
+ // Compact protocol doesn't read struct end markers
468
+ }
469
+ readFieldBegin() {
470
+ const byte = this.readByte();
471
+ if (byte === DpWireType.Stop) {
472
+ return { name: '', type: DpWireType.Stop, id: 0 };
473
+ }
474
+ const type = byte & 0x0F;
475
+ const id = byte >> 4;
476
+ return { name: '', type: type, id };
477
+ }
478
+ readFieldEnd() {
479
+ // Compact protocol doesn't read field end markers
480
+ }
481
+ readBool() {
482
+ return this.readByte() !== 0;
483
+ }
484
+ readByte() {
485
+ const value = this.view.getUint8(this.offset);
486
+ this.offset += 1;
487
+ return value;
488
+ }
489
+ readI16() {
490
+ return this.readVarint();
491
+ }
492
+ readI32() {
493
+ return this.readVarint();
494
+ }
495
+ readI64() {
496
+ return this.readVarint64();
497
+ }
498
+ readDouble() {
499
+ const value = this.view.getFloat64(this.offset, this.littleEndian);
500
+ this.offset += 8;
501
+ return value;
502
+ }
503
+ readString() {
504
+ const length = this.readVarint();
505
+ const bytes = new Uint8Array(this.buffer, this.offset, length);
506
+ this.offset += length;
507
+ const decoder = new TextDecoder();
508
+ return decoder.decode(bytes);
509
+ }
510
+ readBinary() {
511
+ const length = this.readVarint();
512
+ const bytes = new Uint8Array(this.buffer, this.offset, length);
513
+ this.offset += length;
514
+ return bytes;
515
+ }
516
+ readListBegin() {
517
+ const byte = this.readByte();
518
+ const elementType = byte & 0x0F;
519
+ let count = byte >> 4;
520
+ if (count === 15) {
521
+ count = this.readVarint() + 15;
522
+ }
523
+ return { elementType: elementType, count };
524
+ }
525
+ readListEnd() {
526
+ // Compact protocol doesn't read list end markers
527
+ }
528
+ readSetBegin() {
529
+ const list = this.readListBegin();
530
+ return { elementType: list.elementType, count: list.count };
531
+ }
532
+ readSetEnd() {
533
+ // Compact protocol doesn't read set end markers
534
+ }
535
+ readMapBegin() {
536
+ const count = this.readVarint();
537
+ if (count === 0) {
538
+ return { keyType: DpWireType.Stop, valueType: DpWireType.Stop, count: 0 };
539
+ }
540
+ const byte = this.readByte();
541
+ const keyType = byte >> 4;
542
+ const valueType = byte & 0x0F;
543
+ return { keyType: keyType, valueType: valueType, count };
544
+ }
545
+ readMapEnd() {
546
+ // Compact protocol doesn't read map end markers
547
+ }
548
+ // Varint encoding/decoding
549
+ writeVarint(value) {
550
+ while (value > 0x7F) {
551
+ this.writeByte((value & 0x7F) | 0x80);
552
+ value >>>= 7;
553
+ }
554
+ this.writeByte(value & 0x7F);
555
+ }
556
+ readVarint() {
557
+ let result = 0;
558
+ let shift = 0;
559
+ let byte;
560
+ do {
561
+ byte = this.readByte();
562
+ result |= (byte & 0x7F) << shift;
563
+ shift += 7;
564
+ } while (byte & 0x80);
565
+ return result;
566
+ }
567
+ writeVarint64(value) {
568
+ while (value > 0x7fn) {
569
+ this.writeByte(Number((value & 0x7fn) | 0x80n));
570
+ value = value >> 7n;
571
+ }
572
+ this.writeByte(Number(value & 0x7fn));
573
+ }
574
+ readVarint64() {
575
+ let result = 0n;
576
+ let shift = 0n;
577
+ let byte;
578
+ do {
579
+ byte = this.readByte();
580
+ result |= BigInt(byte & 0x7F) << shift;
581
+ shift += 7n;
582
+ } while (byte & 0x80);
583
+ return result;
584
+ }
585
+ // Utility methods
586
+ getBuffer() {
587
+ return this.buffer.slice(0, this.offset);
588
+ }
589
+ getOffset() {
590
+ return this.offset;
591
+ }
592
+ setOffset(offset) {
593
+ this.offset = offset;
594
+ }
595
+ }
596
+ exports.DpCompactProtocol = DpCompactProtocol;
597
+ /** Magic header for DpPack wire (3 bytes): 'DP' + version 1. Enables protocol detection from stored bytes. */
598
+ exports.DP_PACK_MAGIC = new Uint8Array([0x44, 0x50, 0x01]);
599
+ const DP_PACK_MAGIC_LEN = 3;
600
+ /** Envelope: [0x44, 0x50, version, protocol_id]. Pack/Unpack 시 저장된 프로토콜 타입으로 올바른 디코더 선택. */
601
+ exports.DP_WIRE_ENVELOPE_SIZE = 4;
602
+ exports.DP_WIRE_ENVELOPE_MAGIC0 = 0x44;
603
+ exports.DP_WIRE_ENVELOPE_MAGIC1 = 0x50;
604
+ /** Protocol id in envelope: 성능·메모리 기본은 pack(2). 제로카피(3)는 사용자 선택 전용(기본 아님), FlatBuffers 유사·버퍼 뷰 반환. */
605
+ exports.DP_WIRE_PROTOCOL_BINARY = 0;
606
+ exports.DP_WIRE_PROTOCOL_COMPACT = 1;
607
+ exports.DP_WIRE_PROTOCOL_PACK = 2;
608
+ exports.DP_WIRE_PROTOCOL_ZEROCOPY = 3;
609
+ const ENVELOPE_PROTOCOL_TO_ID = {
610
+ binary: exports.DP_WIRE_PROTOCOL_BINARY,
611
+ compact: exports.DP_WIRE_PROTOCOL_COMPACT,
612
+ pack: exports.DP_WIRE_PROTOCOL_PACK,
613
+ zerocopy: exports.DP_WIRE_PROTOCOL_ZEROCOPY
614
+ };
615
+ const ENVELOPE_ID_TO_PROTOCOL = {
616
+ [exports.DP_WIRE_PROTOCOL_BINARY]: 'binary',
617
+ [exports.DP_WIRE_PROTOCOL_COMPACT]: 'compact',
618
+ [exports.DP_WIRE_PROTOCOL_PACK]: 'pack',
619
+ [exports.DP_WIRE_PROTOCOL_ZEROCOPY]: 'zerocopy'
620
+ };
621
+ /**
622
+ * Pack 시 버퍼 앞에 프로토콜 타입을 씀. 호출 후 동일 버퍼로 프로토콜 생성하고 setOffset(DP_WIRE_ENVELOPE_SIZE) 후 Write.
623
+ * @param buffer 최소 4바이트 이상 확보된 버퍼
624
+ * @param protocol 저장할 프로토콜 (기본·권장: 'pack' — 성능·메모리 양호)
625
+ * @param version 엔벨로프 버전 (기본 1)
626
+ */
627
+ function writeEnvelope(buffer, protocol, version = 1) {
628
+ const u8 = new Uint8Array(buffer, 0, exports.DP_WIRE_ENVELOPE_SIZE);
629
+ u8[0] = exports.DP_WIRE_ENVELOPE_MAGIC0;
630
+ u8[1] = exports.DP_WIRE_ENVELOPE_MAGIC1;
631
+ u8[2] = version & 0xff;
632
+ u8[3] = ENVELOPE_PROTOCOL_TO_ID[protocol];
633
+ }
634
+ /**
635
+ * Unpack 시 저장된 프로토콜 타입 반환. 엔벨로프가 있으면 version·protocol 반환; 없으면 null.
636
+ */
637
+ function readEnvelope(buffer) {
638
+ const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
639
+ if (bytes.length < exports.DP_WIRE_ENVELOPE_SIZE ||
640
+ bytes[0] !== exports.DP_WIRE_ENVELOPE_MAGIC0 ||
641
+ bytes[1] !== exports.DP_WIRE_ENVELOPE_MAGIC1) {
642
+ return null;
643
+ }
644
+ const version = (bytes[2] ?? 0) & 0xff;
645
+ const id = (bytes[3] ?? 0) & 0xff;
646
+ const protocol = ENVELOPE_ID_TO_PROTOCOL[id];
647
+ if (protocol === undefined)
648
+ return null;
649
+ return { version, protocol };
650
+ }
651
+ /**
652
+ * DeukPack-native wire protocol (pack).
653
+ * Same encoding as Thrift compact (varint tag + varint values) but with a 3-byte magic header
654
+ * so stored payloads can be identified as DpPack. Not wire-compatible with Apache Thrift.
655
+ */
656
+ class DpPackProtocol {
657
+ constructor(buffer, littleEndian = true) {
658
+ this.buffer = buffer;
659
+ this.view = new DataView(buffer);
660
+ this.offset = 0;
661
+ this.littleEndian = littleEndian;
662
+ }
663
+ ensureMagicWritten() {
664
+ if (this.offset === 0) {
665
+ new Uint8Array(this.buffer).set(exports.DP_PACK_MAGIC, 0);
666
+ this.offset = DP_PACK_MAGIC_LEN;
667
+ }
668
+ }
669
+ ensureMagicSkipped() {
670
+ if (this.offset === 0 && this.buffer.byteLength >= DP_PACK_MAGIC_LEN) {
671
+ const u8 = new Uint8Array(this.buffer);
672
+ if (u8[0] === exports.DP_PACK_MAGIC[0] && u8[1] === exports.DP_PACK_MAGIC[1] && u8[2] === exports.DP_PACK_MAGIC[2])
673
+ this.offset = DP_PACK_MAGIC_LEN;
674
+ }
675
+ }
676
+ writeStructBegin(_struct) {
677
+ this.ensureMagicWritten();
678
+ }
679
+ writeStructEnd() { }
680
+ writeFieldBegin(field) {
681
+ this.ensureMagicWritten();
682
+ if (field.type === DpWireType.Stop) {
683
+ this.writeByte(DpWireType.Stop);
684
+ }
685
+ else {
686
+ this.writeByte((field.id << 4) | field.type);
687
+ }
688
+ }
689
+ writeFieldEnd() { }
690
+ writeFieldStop() {
691
+ this.ensureMagicWritten();
692
+ this.writeByte(DpWireType.Stop);
693
+ }
694
+ writeBool(value) {
695
+ this.writeByte(value ? 1 : 0);
696
+ }
697
+ writeByte(value) {
698
+ this.view.setUint8(this.offset, value);
699
+ this.offset += 1;
700
+ }
701
+ writeI16(value) {
702
+ this.writeVarint(value);
703
+ }
704
+ writeI32(value) {
705
+ this.writeVarint(value);
706
+ }
707
+ writeI64(value) {
708
+ this.writeVarint64(value);
709
+ }
710
+ writeDouble(value) {
711
+ this.view.setFloat64(this.offset, value, this.littleEndian);
712
+ this.offset += 8;
713
+ }
714
+ writeString(value) {
715
+ const encoder = new TextEncoder();
716
+ const bytes = encoder.encode(value);
717
+ this.writeVarint(bytes.length);
718
+ new Uint8Array(this.buffer, this.offset, bytes.length).set(bytes);
719
+ this.offset += bytes.length;
720
+ }
721
+ writeBinary(value) {
722
+ this.writeVarint(value.length);
723
+ new Uint8Array(this.buffer, this.offset, value.length).set(value);
724
+ this.offset += value.length;
725
+ }
726
+ writeListBegin(list) {
727
+ this.writeByte((list.elementType << 4) | (list.count < 15 ? list.count : 15));
728
+ if (list.count >= 15) {
729
+ this.writeVarint(list.count - 15);
730
+ }
731
+ }
732
+ writeListEnd() { }
733
+ writeSetBegin(set) {
734
+ this.writeListBegin({ elementType: set.elementType, count: set.count });
735
+ }
736
+ writeSetEnd() { }
737
+ writeMapBegin(map) {
738
+ this.writeVarint(map.count);
739
+ if (map.count > 0) {
740
+ this.writeByte((map.keyType << 4) | map.valueType);
741
+ }
742
+ }
743
+ writeMapEnd() { }
744
+ readStructBegin() {
745
+ this.ensureMagicSkipped();
746
+ return { name: '' };
747
+ }
748
+ readStructEnd() { }
749
+ readFieldBegin() {
750
+ this.ensureMagicSkipped();
751
+ const byte = this.readByte();
752
+ if (byte === DpWireType.Stop) {
753
+ return { name: '', type: DpWireType.Stop, id: 0 };
754
+ }
755
+ const type = byte & 0x0F;
756
+ const id = byte >> 4;
757
+ return { name: '', type: type, id };
758
+ }
759
+ readFieldEnd() { }
760
+ readBool() {
761
+ return this.readByte() !== 0;
762
+ }
763
+ readByte() {
764
+ const value = this.view.getUint8(this.offset);
765
+ this.offset += 1;
766
+ return value;
767
+ }
768
+ readI16() {
769
+ return this.readVarint();
770
+ }
771
+ readI32() {
772
+ return this.readVarint();
773
+ }
774
+ readI64() {
775
+ return this.readVarint64();
776
+ }
777
+ readDouble() {
778
+ const value = this.view.getFloat64(this.offset, this.littleEndian);
779
+ this.offset += 8;
780
+ return value;
781
+ }
782
+ readString() {
783
+ const length = this.readVarint();
784
+ const bytes = new Uint8Array(this.buffer, this.offset, length);
785
+ this.offset += length;
786
+ return new TextDecoder().decode(bytes);
787
+ }
788
+ readBinary() {
789
+ const length = this.readVarint();
790
+ const bytes = new Uint8Array(this.buffer, this.offset, length);
791
+ this.offset += length;
792
+ return new Uint8Array(bytes);
793
+ }
794
+ readListBegin() {
795
+ const byte = this.readByte();
796
+ const elementType = byte & 0x0F;
797
+ let count = byte >> 4;
798
+ if (count === 15) {
799
+ count = this.readVarint() + 15;
800
+ }
801
+ return { elementType: elementType, count };
802
+ }
803
+ readListEnd() { }
804
+ readSetBegin() {
805
+ const list = this.readListBegin();
806
+ return { elementType: list.elementType, count: list.count };
807
+ }
808
+ readSetEnd() { }
809
+ readMapBegin() {
810
+ const count = this.readVarint();
811
+ if (count === 0) {
812
+ return { keyType: DpWireType.Stop, valueType: DpWireType.Stop, count: 0 };
813
+ }
814
+ const byte = this.readByte();
815
+ const keyType = byte >> 4;
816
+ const valueType = byte & 0x0F;
817
+ return { keyType: keyType, valueType: valueType, count };
818
+ }
819
+ readMapEnd() { }
820
+ writeVarint(value) {
821
+ while (value > 0x7F) {
822
+ this.writeByte((value & 0x7F) | 0x80);
823
+ value >>>= 7;
824
+ }
825
+ this.writeByte(value & 0x7F);
826
+ }
827
+ readVarint() {
828
+ let result = 0;
829
+ let shift = 0;
830
+ let byte;
831
+ do {
832
+ byte = this.readByte();
833
+ result |= (byte & 0x7F) << shift;
834
+ shift += 7;
835
+ } while (byte & 0x80);
836
+ return result;
837
+ }
838
+ writeVarint64(value) {
839
+ while (value > 0x7fn) {
840
+ this.writeByte(Number((value & 0x7fn) | 0x80n));
841
+ value = value >> 7n;
842
+ }
843
+ this.writeByte(Number(value & 0x7fn));
844
+ }
845
+ readVarint64() {
846
+ let result = 0n;
847
+ let shift = 0n;
848
+ let byte;
849
+ do {
850
+ byte = this.readByte();
851
+ result |= BigInt(byte & 0x7F) << shift;
852
+ shift += 7n;
853
+ } while (byte & 0x80);
854
+ return result;
855
+ }
856
+ getBuffer() {
857
+ return this.buffer.slice(0, this.offset);
858
+ }
859
+ getOffset() {
860
+ return this.offset;
861
+ }
862
+ setOffset(offset) {
863
+ this.offset = offset;
864
+ }
865
+ }
866
+ exports.DpPackProtocol = DpPackProtocol;
867
+ /**
868
+ * Unpack 시 저장된 바이트에서 프로토콜 판별.
869
+ * - 엔벨로프(4바이트)가 있으면 그 안의 프로토콜 타입 반환.
870
+ * - pack 매직(3바이트)만 있으면 'pack'.
871
+ * - 매직이 없으면 Thrift 호환 바이너리로 간주하여 'binary' 반환.
872
+ */
873
+ function detectWireProtocol(buffer) {
874
+ const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
875
+ const env = readEnvelope(bytes);
876
+ if (env)
877
+ return env.protocol;
878
+ if (bytes.length >= DP_PACK_MAGIC_LEN &&
879
+ bytes[0] === exports.DP_PACK_MAGIC[0] &&
880
+ bytes[1] === exports.DP_PACK_MAGIC[1] &&
881
+ bytes[2] === exports.DP_PACK_MAGIC[2]) {
882
+ return 'pack';
883
+ }
884
+ return 'binary';
885
+ }
886
+ /**
887
+ * Unpack 시 버퍼에서 프로토콜 타입을 읽고, 해당 프로토콜 인스턴스와 바디 시작 오프셋 반환.
888
+ * 엔벨로프 있음 → bodyOffset=4. pack 매직만 있음 → bodyOffset=3. 매직 없음 → Thrift 호환 바이너리, bodyOffset=0.
889
+ */
890
+ function createProtocolForUnpack(buffer, littleEndian = true) {
891
+ const env = readEnvelope(buffer);
892
+ if (env) {
893
+ const proto = env.protocol === 'binary' ? new DpBinaryProtocol(buffer, littleEndian)
894
+ : env.protocol === 'compact' ? new DpCompactProtocol(buffer, littleEndian)
895
+ : env.protocol === 'zerocopy' ? new DpZeroCopyProtocol(buffer, littleEndian)
896
+ : new DpPackProtocol(buffer, littleEndian);
897
+ proto.setOffset(exports.DP_WIRE_ENVELOPE_SIZE);
898
+ return { protocol: proto, bodyOffset: exports.DP_WIRE_ENVELOPE_SIZE };
899
+ }
900
+ const bytes = new Uint8Array(buffer);
901
+ if (bytes.length >= DP_PACK_MAGIC_LEN &&
902
+ bytes[0] === exports.DP_PACK_MAGIC[0] &&
903
+ bytes[1] === exports.DP_PACK_MAGIC[1] &&
904
+ bytes[2] === exports.DP_PACK_MAGIC[2]) {
905
+ const proto = new DpPackProtocol(buffer, littleEndian);
906
+ proto.setOffset(DP_PACK_MAGIC_LEN);
907
+ return { protocol: proto, bodyOffset: DP_PACK_MAGIC_LEN };
908
+ }
909
+ const proto = new DpBinaryProtocol(buffer, littleEndian);
910
+ return { protocol: proto, bodyOffset: 0 };
911
+ }
912
+ //# sourceMappingURL=WireProtocol.js.map