rocketmq-client-remoting-nodejs 0.0.0-stage → 0.0.1

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 (137) hide show
  1. package/README.en.md +428 -0
  2. package/README.md +424 -2
  3. package/dist/client/admin.d.ts +229 -0
  4. package/dist/client/admin.js +2462 -0
  5. package/dist/client/allocate.d.ts +48 -0
  6. package/dist/client/allocate.js +365 -0
  7. package/dist/client/backpressure.d.ts +23 -0
  8. package/dist/client/backpressure.js +95 -0
  9. package/dist/client/consumer.d.ts +294 -0
  10. package/dist/client/consumer.js +3095 -0
  11. package/dist/client/consumer_result.d.ts +129 -0
  12. package/dist/client/consumer_result.js +138 -0
  13. package/dist/client/consumer_stats.d.ts +69 -0
  14. package/dist/client/consumer_stats.js +258 -0
  15. package/dist/client/exception.d.ts +39 -0
  16. package/dist/client/exception.js +36 -0
  17. package/dist/client/hook.d.ts +145 -0
  18. package/dist/client/hook.js +171 -0
  19. package/dist/client/latency.d.ts +61 -0
  20. package/dist/client/latency.js +239 -0
  21. package/dist/client/lite_pull_consumer.d.ts +151 -0
  22. package/dist/client/lite_pull_consumer.js +1096 -0
  23. package/dist/client/mq_client.d.ts +188 -0
  24. package/dist/client/mq_client.js +1678 -0
  25. package/dist/client/mq_client_manager.d.ts +21 -0
  26. package/dist/client/mq_client_manager.js +129 -0
  27. package/dist/client/offset_store.d.ts +45 -0
  28. package/dist/client/offset_store.js +389 -0
  29. package/dist/client/open_tracing_hook.d.ts +52 -0
  30. package/dist/client/open_tracing_hook.js +241 -0
  31. package/dist/client/pop_api.d.ts +28 -0
  32. package/dist/client/pop_api.js +224 -0
  33. package/dist/client/pop_consumer.d.ts +23 -0
  34. package/dist/client/pop_consumer.js +719 -0
  35. package/dist/client/pop_process_queue.d.ts +15 -0
  36. package/dist/client/pop_process_queue.js +51 -0
  37. package/dist/client/process_queue.d.ts +49 -0
  38. package/dist/client/process_queue.js +261 -0
  39. package/dist/client/produce_accumulator.d.ts +75 -0
  40. package/dist/client/produce_accumulator.js +288 -0
  41. package/dist/client/producer.d.ts +231 -0
  42. package/dist/client/producer.js +1774 -0
  43. package/dist/client/pull_api.d.ts +26 -0
  44. package/dist/client/pull_api.js +264 -0
  45. package/dist/client/pull_consumer.d.ts +119 -0
  46. package/dist/client/pull_consumer.js +876 -0
  47. package/dist/client/pull_schedule_service.d.ts +71 -0
  48. package/dist/client/pull_schedule_service.js +424 -0
  49. package/dist/client/request_reply.d.ts +61 -0
  50. package/dist/client/request_reply.js +232 -0
  51. package/dist/client/retry_message.d.ts +2 -0
  52. package/dist/client/retry_message.js +27 -0
  53. package/dist/client/send_result.d.ts +57 -0
  54. package/dist/client/send_result.js +70 -0
  55. package/dist/client/top_addressing.d.ts +20 -0
  56. package/dist/client/top_addressing.js +75 -0
  57. package/dist/client/trace_context.d.ts +61 -0
  58. package/dist/client/trace_context.js +373 -0
  59. package/dist/client/trace_dispatcher.d.ts +31 -0
  60. package/dist/client/trace_dispatcher.js +236 -0
  61. package/dist/client/trace_hook.d.ts +23 -0
  62. package/dist/client/trace_hook.js +228 -0
  63. package/dist/client/traceparent.d.ts +9 -0
  64. package/dist/client/traceparent.js +86 -0
  65. package/dist/common/boundary_type.d.ts +7 -0
  66. package/dist/common/boundary_type.js +16 -0
  67. package/dist/common/compress.d.ts +28 -0
  68. package/dist/common/compress.js +512 -0
  69. package/dist/common/message.d.ts +129 -0
  70. package/dist/common/message.js +214 -0
  71. package/dist/common/messageClientIdSetter.d.ts +7 -0
  72. package/dist/common/messageClientIdSetter.js +48 -0
  73. package/dist/common/messageConst.d.ts +68 -0
  74. package/dist/common/messageConst.js +68 -0
  75. package/dist/common/messageDecoder.d.ts +40 -0
  76. package/dist/common/messageDecoder.js +504 -0
  77. package/dist/common/messageType.d.ts +11 -0
  78. package/dist/common/messageType.js +11 -0
  79. package/dist/common/message_accessor.d.ts +39 -0
  80. package/dist/common/message_accessor.js +135 -0
  81. package/dist/common/mixAll.d.ts +106 -0
  82. package/dist/common/mixAll.js +247 -0
  83. package/dist/common/recall_message_handle.d.ts +25 -0
  84. package/dist/common/recall_message_handle.js +98 -0
  85. package/dist/common/subscriptionData.d.ts +36 -0
  86. package/dist/common/subscriptionData.js +64 -0
  87. package/dist/common/sysflag.d.ts +129 -0
  88. package/dist/common/sysflag.js +96 -0
  89. package/dist/common/topic_config.d.ts +66 -0
  90. package/dist/common/topic_config.js +106 -0
  91. package/dist/common/topic_validator.d.ts +49 -0
  92. package/dist/common/topic_validator.js +115 -0
  93. package/dist/common/utilAll.d.ts +39 -0
  94. package/dist/common/utilAll.js +154 -0
  95. package/dist/common/validators.d.ts +25 -0
  96. package/dist/common/validators.js +80 -0
  97. package/dist/index.d.ts +24 -0
  98. package/dist/index.js +36 -0
  99. package/dist/logging.d.ts +24 -0
  100. package/dist/logging.js +246 -0
  101. package/dist/remoting/acl.d.ts +16 -0
  102. package/dist/remoting/acl.js +39 -0
  103. package/dist/remoting/admin_body.d.ts +257 -0
  104. package/dist/remoting/admin_body.js +669 -0
  105. package/dist/remoting/bodies.d.ts +475 -0
  106. package/dist/remoting/bodies.js +1131 -0
  107. package/dist/remoting/client.d.ts +73 -0
  108. package/dist/remoting/client.js +591 -0
  109. package/dist/remoting/codes.d.ts +296 -0
  110. package/dist/remoting/codes.js +301 -0
  111. package/dist/remoting/exception.d.ts +45 -0
  112. package/dist/remoting/exception.js +58 -0
  113. package/dist/remoting/extra_info.d.ts +61 -0
  114. package/dist/remoting/extra_info.js +220 -0
  115. package/dist/remoting/headers.d.ts +758 -0
  116. package/dist/remoting/headers.js +1307 -0
  117. package/dist/remoting/heartbeat.d.ts +91 -0
  118. package/dist/remoting/heartbeat.js +227 -0
  119. package/dist/remoting/namespace.d.ts +14 -0
  120. package/dist/remoting/namespace.js +85 -0
  121. package/dist/remoting/pop_bodies.d.ts +58 -0
  122. package/dist/remoting/pop_bodies.js +214 -0
  123. package/dist/remoting/remotingCommand.d.ts +39 -0
  124. package/dist/remoting/remotingCommand.js +225 -0
  125. package/dist/remoting/route.d.ts +61 -0
  126. package/dist/remoting/route.js +288 -0
  127. package/dist/remoting/rpc_hooks.d.ts +31 -0
  128. package/dist/remoting/rpc_hooks.js +73 -0
  129. package/dist/remoting/serialize.d.ts +33 -0
  130. package/dist/remoting/serialize.js +372 -0
  131. package/dist/remoting/socks_proxy.d.ts +29 -0
  132. package/dist/remoting/socks_proxy.js +379 -0
  133. package/dist/remoting/static_topic.d.ts +49 -0
  134. package/dist/remoting/static_topic.js +244 -0
  135. package/dist/remoting/subscription.d.ts +65 -0
  136. package/dist/remoting/subscription.js +172 -0
  137. package/package.json +38 -4
@@ -0,0 +1,504 @@
1
+ import zlib from 'node:zlib';
2
+ import { Buffer } from 'node:buffer';
3
+ import { Message, MessageExt, isWaitStoreMsgOK } from "./message.js";
4
+ import { MessageSysFlag } from "./sysflag.js";
5
+ import { compressFor, decompressFor } from "./compress.js";
6
+ import { crc32Match } from "./utilAll.js";
7
+ import { getLogger } from "../logging.js";
8
+ const logger = getLogger('common.message_decoder');
9
+ const CHARSET = 'utf8';
10
+ const NAME_VALUE_SEPARATOR = 1;
11
+ const PROPERTY_SEPARATOR = 2;
12
+ const MESSAGE_MAGIC_CODE = -626843481;
13
+ const MESSAGE_MAGIC_CODE_V2 = -626843477;
14
+ const BLANK_MAGIC_CODE = -875286124;
15
+ const HEX = '0123456789ABCDEF';
16
+ function bytes2string(bs) {
17
+ let s = '';
18
+ for (let i = 0; i < bs.length; i++)
19
+ s += HEX[(bs[i] >> 4) & 0x0f] + HEX[bs[i] & 0x0f];
20
+ return s;
21
+ }
22
+ function writeLongBE(value) {
23
+ const b = Buffer.alloc(8);
24
+ b.writeBigInt64BE(BigInt(value), 0);
25
+ return b;
26
+ }
27
+ function ipToBytes(ip, v6) {
28
+ if (!v6) {
29
+ const parts = ip.split('.').map(Number);
30
+ const b = Buffer.alloc(4);
31
+ b.writeUInt8(parts[0] | 0, 0);
32
+ b.writeUInt8(parts[1] | 0, 1);
33
+ b.writeUInt8(parts[2] | 0, 2);
34
+ b.writeUInt8(parts[3] | 0, 3);
35
+ return b;
36
+ }
37
+ // ipv6
38
+ const segs = ip.split(':');
39
+ const groups = new Array(8).fill(0);
40
+ let zi = segs.indexOf('');
41
+ if (zi >= 0) {
42
+ const before = zi;
43
+ const after = segs.length - 1 - zi;
44
+ const missing = 8 - (before + after);
45
+ segs.splice(zi, 1, ...new Array(missing).fill('0'));
46
+ }
47
+ segs.forEach((s, i) => { groups[i] = parseInt(s || '0', 16); });
48
+ const b = Buffer.alloc(16);
49
+ for (let i = 0; i < 8; i++)
50
+ b.writeUInt16BE(groups[i], i * 2);
51
+ return b;
52
+ }
53
+ function ipAndPortToBytes(ip, port, v6) {
54
+ const addr = ipToBytes(ip, v6);
55
+ const out = Buffer.alloc(addr.length + 4);
56
+ addr.copy(out, 0);
57
+ out.writeUInt32BE(port >>> 0, addr.length);
58
+ return out;
59
+ }
60
+ function bytesToIpAndPort(raw) {
61
+ if (raw.length === 8 || raw.length === 20) {
62
+ const ip = `${raw[0]}.${raw[1]}.${raw[2]}.${raw[3]}`;
63
+ const port = raw.readUInt32BE(4);
64
+ return [ip, port];
65
+ }
66
+ // ipv6: 16 ip + 4 port
67
+ const parts = [];
68
+ for (let i = 0; i < 16; i += 2)
69
+ parts.push(raw.readUInt16BE(i).toString(16));
70
+ const ip = parts.join(':');
71
+ const port = raw.readUInt32BE(16);
72
+ return [ip, port];
73
+ }
74
+ export function createMessageId(addrBytes, offset) {
75
+ return bytes2string(Buffer.concat([addrBytes, writeLongBE(offset)]));
76
+ }
77
+ export function decodeMessageId(msgId) {
78
+ const raw = Buffer.from(msgId, 'hex');
79
+ const ipLen = raw.length === 16 ? 4 : 16;
80
+ let ip, port, offset;
81
+ if (ipLen === 4) {
82
+ ip = `${raw[0]}.${raw[1]}.${raw[2]}.${raw[3]}`;
83
+ port = raw.readUInt32BE(4);
84
+ offset = Number(raw.readBigInt64BE(8));
85
+ }
86
+ else {
87
+ const parts = [];
88
+ for (let i = 0; i < 16; i += 2)
89
+ parts.push(raw.readUInt16BE(i).toString(16));
90
+ ip = parts.join(':');
91
+ port = raw.readUInt32BE(16);
92
+ offset = Number(raw.readBigInt64BE(20));
93
+ }
94
+ return { ip, port, offset };
95
+ }
96
+ export function messageProperties2String(properties) {
97
+ if (properties == null)
98
+ return '';
99
+ let out = '';
100
+ for (const [name, value] of Object.entries(properties)) {
101
+ if (value == null)
102
+ continue;
103
+ out += `${name}${String.fromCharCode(NAME_VALUE_SEPARATOR)}${value}${String.fromCharCode(PROPERTY_SEPARATOR)}`;
104
+ }
105
+ return out;
106
+ }
107
+ export function string2MessageProperties(propertiesStr) {
108
+ const result = {};
109
+ if (!propertiesStr)
110
+ return result;
111
+ const sep = String.fromCharCode(PROPERTY_SEPARATOR);
112
+ const kvSep = String.fromCharCode(NAME_VALUE_SEPARATOR);
113
+ let index = 0;
114
+ const length = propertiesStr.length;
115
+ while (index < length) {
116
+ let newIndex = propertiesStr.indexOf(sep, index);
117
+ if (newIndex < 0)
118
+ newIndex = length;
119
+ if (newIndex - index >= 3) {
120
+ const kv = propertiesStr.indexOf(kvSep, index);
121
+ if (kv > index && kv < newIndex - 1) {
122
+ result[propertiesStr.slice(index, kv)] = propertiesStr.slice(kv + 1, newIndex);
123
+ }
124
+ }
125
+ index = newIndex + 1;
126
+ }
127
+ return result;
128
+ }
129
+ // ---- compression (ZLIB native; LZ4/ZSTD via common/compress.ts) ----
130
+ function normalizeCompressionType(ct) {
131
+ if (ct === 0)
132
+ return MessageSysFlag.ZLIB_TYPE;
133
+ return ct;
134
+ }
135
+ class UnsupportedCompressionError extends Error {
136
+ }
137
+ function _compress(data, compressionType) {
138
+ const ct = normalizeCompressionType(compressionType);
139
+ if (ct === MessageSysFlag.ZLIB_TYPE)
140
+ return zlib.deflateSync(data);
141
+ if (ct === MessageSysFlag.LZ4_TYPE || ct === MessageSysFlag.ZSTD_TYPE) {
142
+ return compressFor(data, ct);
143
+ }
144
+ throw new UnsupportedCompressionError(`unsupported compression type: ${compressionType}`);
145
+ }
146
+ function _decompress(data, compressionType) {
147
+ const ct = normalizeCompressionType(compressionType);
148
+ if (ct === MessageSysFlag.ZLIB_TYPE)
149
+ return zlib.inflateSync(data);
150
+ if (ct === MessageSysFlag.LZ4_TYPE || ct === MessageSysFlag.ZSTD_TYPE) {
151
+ return decompressFor(data, ct);
152
+ }
153
+ throw new UnsupportedCompressionError(`unsupported compression type: ${compressionType}`);
154
+ }
155
+ export function decompressBody(data, compressionType) {
156
+ return _decompress(data, compressionType);
157
+ }
158
+ function topicLengthSize(magicCode) {
159
+ return magicCode === MESSAGE_MAGIC_CODE_V2 ? 2 : 1;
160
+ }
161
+ export function encodeMessageExt(messageExt, needCompress = false) {
162
+ let body = messageExt.getBody() || Buffer.alloc(0);
163
+ if (needCompress && (messageExt.getSysFlag() & MessageSysFlag.COMPRESSED_FLAG)) {
164
+ const ct = MessageSysFlag.getCompressionType(messageExt.getSysFlag());
165
+ body = _compress(body, ct);
166
+ }
167
+ const bodyLength = body.length;
168
+ const topicBytes = Buffer.from(messageExt.getTopic(), CHARSET);
169
+ const topicLen = topicBytes.length;
170
+ const propsBytes = Buffer.from(messageProperties2String(messageExt.getProperties()), CHARSET);
171
+ const propsLength = propsBytes.length;
172
+ const sysFlag = messageExt.getSysFlag();
173
+ const bornhostLen = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) ? 20 : 8;
174
+ const storehostLen = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) ? 20 : 8;
175
+ const computedSize = (4 + 4 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + 8 + bornhostLen + storehostLen + 4 + 8
176
+ + 4 + bodyLength + 1 + topicLen + 2 + propsLength);
177
+ let storeSize = messageExt.getStoreSize();
178
+ if (!(storeSize > 0))
179
+ storeSize = computedSize;
180
+ storeSize = Math.max(storeSize, computedSize);
181
+ const bornHost = messageExt.getBornHost() || '127.0.0.1';
182
+ const bornPort = messageExt.bornHostPort || 0;
183
+ const storeHost = messageExt.getStoreHost() || '127.0.0.1';
184
+ const storePort = messageExt.storeHostPort || 0;
185
+ const buf = Buffer.alloc(storeSize);
186
+ let o = 0;
187
+ buf.writeInt32BE(storeSize, o);
188
+ o += 4;
189
+ buf.writeInt32BE(MESSAGE_MAGIC_CODE, o);
190
+ o += 4;
191
+ buf.writeUInt32BE(messageExt.getBodyCrc() >>> 0, o);
192
+ o += 4;
193
+ buf.writeInt32BE(messageExt.getQueueId(), o);
194
+ o += 4;
195
+ buf.writeInt32BE(messageExt.getFlag(), o);
196
+ o += 4;
197
+ buf.writeBigInt64BE(BigInt(messageExt.getQueueOffset()), o);
198
+ o += 8;
199
+ buf.writeBigInt64BE(BigInt(messageExt.getCommitLogOffset()), o);
200
+ o += 8;
201
+ buf.writeInt32BE(sysFlag, o);
202
+ o += 4;
203
+ buf.writeBigInt64BE(BigInt(messageExt.getBornTimestamp()), o);
204
+ o += 8;
205
+ const bh = ipAndPortToBytes(bornHost, bornPort, !!(sysFlag & MessageSysFlag.BORNHOST_V6_FLAG));
206
+ bh.copy(buf, o);
207
+ o += bh.length;
208
+ buf.writeBigInt64BE(BigInt(messageExt.getStoreTimestamp()), o);
209
+ o += 8;
210
+ const sh = ipAndPortToBytes(storeHost, storePort, !!(sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG));
211
+ sh.copy(buf, o);
212
+ o += sh.length;
213
+ buf.writeInt32BE(messageExt.getReconsumeTimes(), o);
214
+ o += 4;
215
+ buf.writeBigInt64BE(BigInt(messageExt.getPreparedTransactionOffset()), o);
216
+ o += 8;
217
+ buf.writeInt32BE(bodyLength, o);
218
+ o += 4;
219
+ body.copy(buf, o);
220
+ o += bodyLength;
221
+ buf.writeUInt8(topicLen, o);
222
+ o += 1;
223
+ topicBytes.copy(buf, o);
224
+ o += topicLen;
225
+ buf.writeUInt16BE(propsLength, o);
226
+ o += 2;
227
+ propsBytes.copy(buf, o);
228
+ o += propsLength;
229
+ return buf;
230
+ }
231
+ export function decodeMessage(raw, readBody = true, decompressBody = true, isClient = true, checkCrc = false) {
232
+ try {
233
+ const msgExt = new MessageExt();
234
+ let offset = 0;
235
+ const storeSize = raw.readInt32BE(offset);
236
+ offset += 4;
237
+ const magicCode = raw.readInt32BE(offset);
238
+ offset += 4;
239
+ if (magicCode !== MESSAGE_MAGIC_CODE && magicCode !== MESSAGE_MAGIC_CODE_V2)
240
+ return null;
241
+ const useV2 = magicCode === MESSAGE_MAGIC_CODE_V2;
242
+ const bodyCrc = raw.readUInt32BE(offset);
243
+ offset += 4;
244
+ const queueId = raw.readInt32BE(offset);
245
+ offset += 4;
246
+ const flag = raw.readInt32BE(offset);
247
+ offset += 4;
248
+ const queueOffset = Number(raw.readBigInt64BE(offset));
249
+ offset += 8;
250
+ const physicOffset = Number(raw.readBigInt64BE(offset));
251
+ offset += 8;
252
+ const sysFlag = raw.readInt32BE(offset);
253
+ offset += 4;
254
+ const bornTimestamp = Number(raw.readBigInt64BE(offset));
255
+ offset += 8;
256
+ const bornhostLen = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) ? 20 : 8;
257
+ const [bornHost, bornPort] = bytesToIpAndPort(raw.subarray(offset, offset + bornhostLen));
258
+ offset += bornhostLen;
259
+ const storeTimestamp = Number(raw.readBigInt64BE(offset));
260
+ offset += 8;
261
+ const storehostLen = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) ? 20 : 8;
262
+ const [storeHost, storePort] = bytesToIpAndPort(raw.subarray(offset, offset + storehostLen));
263
+ offset += storehostLen;
264
+ const reconsumeTimes = raw.readInt32BE(offset);
265
+ offset += 4;
266
+ const preparedTransactionOffset = Number(raw.readBigInt64BE(offset));
267
+ offset += 8;
268
+ msgExt.setStoreSize(storeSize);
269
+ msgExt.setBodyCrc(bodyCrc);
270
+ msgExt.setQueueId(queueId);
271
+ msgExt.setFlag(flag);
272
+ msgExt.setQueueOffset(queueOffset);
273
+ msgExt.setCommitLogOffset(physicOffset);
274
+ msgExt.setSysFlag(sysFlag);
275
+ msgExt.setBornTimestamp(bornTimestamp);
276
+ msgExt.setBornHost(bornHost);
277
+ msgExt.bornHostPort = bornPort;
278
+ msgExt.setStoreTimestamp(storeTimestamp);
279
+ msgExt.setStoreHost(storeHost);
280
+ msgExt.storeHostPort = storePort;
281
+ msgExt.setReconsumeTimes(reconsumeTimes);
282
+ msgExt.setPreparedTransactionOffset(preparedTransactionOffset);
283
+ const bodyLen = raw.readInt32BE(offset);
284
+ offset += 4;
285
+ if (bodyLen > 0) {
286
+ if (readBody) {
287
+ let body = raw.subarray(offset, offset + bodyLen);
288
+ // The BODYCRC field carries the CRC with the sign bit dropped, so the
289
+ // comparison must be made against that masked form. Checked on the stored
290
+ // bytes, before any decompression.
291
+ if (checkCrc && crc32Match(body) !== bodyCrc)
292
+ throw new Error('Msg crc is error');
293
+ if (decompressBody && (sysFlag & MessageSysFlag.COMPRESSED_FLAG)) {
294
+ const ct = MessageSysFlag.getCompressionType(sysFlag);
295
+ body = _decompress(body, ct);
296
+ msgExt.setSysFlag(MessageSysFlag.clearCompressedFlag(sysFlag));
297
+ }
298
+ msgExt.setBody(Buffer.from(body));
299
+ }
300
+ else {
301
+ // Header-only decode: assigning the field directly is required because
302
+ // setBody(null) would throw inside Buffer.from and null the whole frame.
303
+ msgExt.body = null;
304
+ }
305
+ offset += bodyLen;
306
+ }
307
+ else {
308
+ msgExt.body = null;
309
+ }
310
+ if (useV2) {
311
+ const topicLen = raw.readUInt16BE(offset);
312
+ offset += 2;
313
+ msgExt.setTopic(raw.subarray(offset, offset + topicLen).toString(CHARSET));
314
+ offset += topicLen;
315
+ }
316
+ else {
317
+ const topicLen = raw.readUInt8(offset);
318
+ offset += 1;
319
+ msgExt.setTopic(raw.subarray(offset, offset + topicLen).toString(CHARSET));
320
+ offset += topicLen;
321
+ }
322
+ const propsLength = raw.readUInt16BE(offset);
323
+ offset += 2;
324
+ if (propsLength > 0) {
325
+ msgExt.setProperties(string2MessageProperties(raw.subarray(offset, offset + propsLength).toString(CHARSET)));
326
+ }
327
+ const storeAddrRaw = ipAndPortToBytes(storeHost, storePort, (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) !== 0);
328
+ msgExt.setMsgId(createMessageId(storeAddrRaw, physicOffset));
329
+ if (isClient)
330
+ msgExt.setOffsetMsgId(msgExt.getMsgId());
331
+ return msgExt;
332
+ }
333
+ catch (e) {
334
+ return null;
335
+ }
336
+ }
337
+ export function decodeMessages(raw, readBody = true) {
338
+ const result = [];
339
+ let pos = 0;
340
+ const total = raw.length;
341
+ while (pos < total) {
342
+ if (total - pos < 4)
343
+ break;
344
+ const storeSize = raw.readInt32BE(pos);
345
+ if (storeSize <= 0 || storeSize > total - pos)
346
+ break;
347
+ const msg = decodeMessage(raw.subarray(pos, pos + storeSize), readBody);
348
+ if (msg == null)
349
+ break;
350
+ result.push(msg);
351
+ pos += storeSize;
352
+ }
353
+ return result;
354
+ }
355
+ // ---- 6-segment lightweight format for batch message body ----
356
+ export function encodeMessage(message) {
357
+ const body = message.getBody() || Buffer.alloc(0);
358
+ const propsBytes = Buffer.from(messageProperties2String(message.getProperties()), CHARSET);
359
+ const propsLength = propsBytes.length;
360
+ const storeSize = 4 + 4 + 4 + 4 + 4 + body.length + 2 + propsLength;
361
+ const buf = Buffer.alloc(storeSize);
362
+ let o = 0;
363
+ buf.writeInt32BE(storeSize, o);
364
+ o += 4;
365
+ buf.writeInt32BE(0, o);
366
+ o += 4;
367
+ buf.writeInt32BE(0, o);
368
+ o += 4;
369
+ buf.writeInt32BE(message.getFlag(), o);
370
+ o += 4;
371
+ buf.writeInt32BE(body.length, o);
372
+ o += 4;
373
+ body.copy(buf, o);
374
+ o += body.length;
375
+ buf.writeUInt16BE(propsLength, o);
376
+ o += 2;
377
+ propsBytes.copy(buf, o);
378
+ o += propsLength;
379
+ return buf;
380
+ }
381
+ export function encodeMessages(messages) {
382
+ const parts = messages.map(encodeMessage);
383
+ return Buffer.concat(parts);
384
+ }
385
+ function decodeBatchMessage(raw) {
386
+ let offset = 0;
387
+ offset += 4; // TOTALSIZE
388
+ offset += 4; // MAGICCODE
389
+ offset += 4; // BODYCRC
390
+ const flag = raw.readInt32BE(offset);
391
+ offset += 4;
392
+ const bodyLen = raw.readInt32BE(offset);
393
+ offset += 4;
394
+ const body = raw.subarray(offset, offset + bodyLen);
395
+ offset += bodyLen;
396
+ const propsLen = raw.readUInt16BE(offset);
397
+ offset += 2;
398
+ const props = string2MessageProperties(raw.subarray(offset, offset + propsLen).toString(CHARSET));
399
+ const msg = new Message();
400
+ msg.setFlag(flag);
401
+ msg.setBody(Buffer.from(body));
402
+ msg.setProperties(props);
403
+ return msg;
404
+ }
405
+ export function decodeBatchMessages(raw) {
406
+ const result = [];
407
+ let pos = 0;
408
+ const total = raw.length;
409
+ while (pos < total) {
410
+ if (total - pos < 4)
411
+ break;
412
+ const storeSize = raw.readInt32BE(pos);
413
+ if (storeSize <= 0 || storeSize > total - pos)
414
+ break;
415
+ result.push(decodeBatchMessage(raw.subarray(pos, pos + storeSize)));
416
+ pos += storeSize;
417
+ }
418
+ return result;
419
+ }
420
+ function isInnerBatchToUnwrap(msg) {
421
+ const flag = msg.getSysFlag();
422
+ return MessageSysFlag.check(flag, MessageSysFlag.INNER_BATCH_FLAG)
423
+ && MessageSysFlag.check(flag, MessageSysFlag.NEED_UNWRAP_FLAG);
424
+ }
425
+ // An envelope carrying both INNER_BATCH_FLAG (body is a batch) and
426
+ // NEED_UNWRAP_FLAG (the client asked to see the members) is replaced by its
427
+ // members. Each member copies the envelope's routing/metadata fields and takes
428
+ // the envelope's queue offset incremented by its position, because the broker
429
+ // stored the whole batch under a single queue offset. A member that cannot be
430
+ // decoded keeps the envelope and is logged.
431
+ export function decodeInnerBatchMessages(msgs) {
432
+ if (msgs.length === 0)
433
+ return msgs;
434
+ let hasEnvelope = false;
435
+ for (const msg of msgs) {
436
+ if (isInnerBatchToUnwrap(msg)) {
437
+ hasEnvelope = true;
438
+ break;
439
+ }
440
+ }
441
+ if (!hasEnvelope)
442
+ return msgs;
443
+ const out = [];
444
+ for (const env of msgs) {
445
+ if (!isInnerBatchToUnwrap(env)) {
446
+ out.push(env);
447
+ continue;
448
+ }
449
+ try {
450
+ const members = decodeBatchMessages(env.getBody() || Buffer.alloc(0));
451
+ if (members.length === 0) {
452
+ out.push(env);
453
+ continue;
454
+ }
455
+ for (let i = 0; i < members.length; i++) {
456
+ const member = members[i];
457
+ const inner = new MessageExt(env.getTopic());
458
+ inner.setBody(member.getBody());
459
+ inner.setFlag(member.getFlag());
460
+ inner.setProperties({ ...(env.getProperties() || {}), ...(member.getProperties() || {}) });
461
+ inner.setQueueOffset(env.getQueueOffset() + i);
462
+ inner.setQueueId(env.getQueueId());
463
+ inner.setStoreHost(env.getStoreHost());
464
+ inner.storeHostPort = env.storeHostPort;
465
+ inner.setBornHost(env.getBornHost());
466
+ inner.bornHostPort = env.bornHostPort;
467
+ inner.setBornTimestamp(env.getBornTimestamp());
468
+ inner.setStoreTimestamp(env.getStoreTimestamp());
469
+ inner.setSysFlag(env.getSysFlag());
470
+ inner.setCommitLogOffset(env.getCommitLogOffset());
471
+ inner.setWaitStoreMsgOK(isWaitStoreMsgOK(env));
472
+ inner.setBrokerName(env.getBrokerName());
473
+ inner.setReconsumeTimes(env.getReconsumeTimes());
474
+ inner.setPreparedTransactionOffset(env.getPreparedTransactionOffset());
475
+ out.push(inner);
476
+ }
477
+ }
478
+ catch (e) {
479
+ logger.error('unwrap inner batch %s failed, keep the envelope: %s', env.getMsgId(), e.message);
480
+ out.push(env);
481
+ }
482
+ }
483
+ return out;
484
+ }
485
+ export function countInnerMsgNum(raw) {
486
+ let count = 0;
487
+ let pos = 0;
488
+ const total = raw.length;
489
+ while (pos < total) {
490
+ count++;
491
+ const size = raw.readInt32BE(pos);
492
+ if (size <= 0 || size > total - pos)
493
+ break;
494
+ pos += size;
495
+ }
496
+ return count;
497
+ }
498
+ export default {
499
+ MESSAGE_MAGIC_CODE, MESSAGE_MAGIC_CODE_V2, BLANK_MAGIC_CODE,
500
+ createMessageId, decodeMessageId, messageProperties2String, string2MessageProperties,
501
+ encodeMessageExt, decodeMessage, decodeMessages,
502
+ encodeMessage, encodeMessages, decodeBatchMessage, decodeBatchMessages,
503
+ decodeInnerBatchMessages, countInnerMsgNum, decompressBody,
504
+ };
@@ -0,0 +1,11 @@
1
+ export declare const MessageType: {
2
+ NORMAL: string;
3
+ ORDERLY: string;
4
+ TRANSACTION: string;
5
+ DELAY: string;
6
+ BATCH: string;
7
+ REQUEST_REPLY: string;
8
+ REPLY: string;
9
+ TIMER: string;
10
+ };
11
+ export default MessageType;
@@ -0,0 +1,11 @@
1
+ export const MessageType = {
2
+ NORMAL: 'Normal_Msg',
3
+ ORDERLY: 'Order_Msg',
4
+ TRANSACTION: 'Trans_Msg_Half',
5
+ DELAY: 'Delay_Msg',
6
+ BATCH: 'Batch_Msg',
7
+ REQUEST_REPLY: 'Request_Reply_Msg',
8
+ REPLY: 'reply', // MixAll.REPLY_MESSAGE_FLAG — what a REPLY send carries
9
+ TIMER: 'Timer_Msg',
10
+ };
11
+ export default MessageType;
@@ -0,0 +1,39 @@
1
+ export declare const MessageAccessor: {
2
+ putProperty(msg: any, name: string, value: string): void;
3
+ getProperty(msg: any, name: string): string | null;
4
+ clearProperty(msg: any, name: string): void;
5
+ setProperties(msg: any, properties: Record<string, string>): void;
6
+ getProperties(msg: any): Record<string, string>;
7
+ setRegionId(msg: any, regionId: string): void;
8
+ getOrInitProducerGroup(msg: any, producerGroup: string): string;
9
+ setMessageType(msg: any, messageType: string): void;
10
+ getMessageType(msg: any): string | null;
11
+ setCorrectionBeforePublish(msg: any, producerGroup: string): void;
12
+ getReconsumeTime(msg: any): string | null;
13
+ setReconsumeTime(msg: any, v: any): void;
14
+ getMaxReconsumeTimes(msg: any): string | null;
15
+ setMaxReconsumeTimes(msg: any, v: any): void;
16
+ getDeliveryTimestamp(msg: any): string | null;
17
+ setDeliveryTimestamp(msg: any, ts: any): void;
18
+ getDelayTimeLevel(msg: any): string | null;
19
+ setDelayTimeLevel(msg: any, level: any): void;
20
+ getTimerDelaySec(msg: any): string | null;
21
+ setTimerDelaySec(msg: any, s: any): void;
22
+ getTimerDeliverMs(msg: any): string | null;
23
+ setTimerDeliverMs(msg: any, ms: any): void;
24
+ setKeys(msg: any, keys: string): void;
25
+ getKeys(msg: any): string | null;
26
+ setTags(msg: any, tags: string): void;
27
+ getTags(msg: any): string | null;
28
+ setWaitStoreMsgOK(msg: any, ok: boolean): void;
29
+ setTransactionId(msg: any, transactionId: string): void;
30
+ getTransactionId(msg: any): string | null;
31
+ setOriginMessageId(msg: any, originMessageId: string): void;
32
+ getOriginMessageId(msg: any): string | null;
33
+ setConsumeStartTimestamp(msg: any, ts: any): void;
34
+ getConsumeStartTimestamp(msg: any): string | null;
35
+ setTransactionPrepared(msg: any): void;
36
+ isTransactionPrepared(msg: any): boolean;
37
+ setTransactionPreparedQueueOffset(msg: any, offset: any): void;
38
+ };
39
+ export default MessageAccessor;