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.
- package/README.en.md +428 -0
- package/README.md +424 -2
- package/dist/client/admin.d.ts +229 -0
- package/dist/client/admin.js +2462 -0
- package/dist/client/allocate.d.ts +48 -0
- package/dist/client/allocate.js +365 -0
- package/dist/client/backpressure.d.ts +23 -0
- package/dist/client/backpressure.js +95 -0
- package/dist/client/consumer.d.ts +294 -0
- package/dist/client/consumer.js +3095 -0
- package/dist/client/consumer_result.d.ts +129 -0
- package/dist/client/consumer_result.js +138 -0
- package/dist/client/consumer_stats.d.ts +69 -0
- package/dist/client/consumer_stats.js +258 -0
- package/dist/client/exception.d.ts +39 -0
- package/dist/client/exception.js +36 -0
- package/dist/client/hook.d.ts +145 -0
- package/dist/client/hook.js +171 -0
- package/dist/client/latency.d.ts +61 -0
- package/dist/client/latency.js +239 -0
- package/dist/client/lite_pull_consumer.d.ts +151 -0
- package/dist/client/lite_pull_consumer.js +1096 -0
- package/dist/client/mq_client.d.ts +188 -0
- package/dist/client/mq_client.js +1678 -0
- package/dist/client/mq_client_manager.d.ts +21 -0
- package/dist/client/mq_client_manager.js +129 -0
- package/dist/client/offset_store.d.ts +45 -0
- package/dist/client/offset_store.js +389 -0
- package/dist/client/open_tracing_hook.d.ts +52 -0
- package/dist/client/open_tracing_hook.js +241 -0
- package/dist/client/pop_api.d.ts +28 -0
- package/dist/client/pop_api.js +224 -0
- package/dist/client/pop_consumer.d.ts +23 -0
- package/dist/client/pop_consumer.js +719 -0
- package/dist/client/pop_process_queue.d.ts +15 -0
- package/dist/client/pop_process_queue.js +51 -0
- package/dist/client/process_queue.d.ts +49 -0
- package/dist/client/process_queue.js +261 -0
- package/dist/client/produce_accumulator.d.ts +75 -0
- package/dist/client/produce_accumulator.js +288 -0
- package/dist/client/producer.d.ts +231 -0
- package/dist/client/producer.js +1774 -0
- package/dist/client/pull_api.d.ts +26 -0
- package/dist/client/pull_api.js +264 -0
- package/dist/client/pull_consumer.d.ts +119 -0
- package/dist/client/pull_consumer.js +876 -0
- package/dist/client/pull_schedule_service.d.ts +71 -0
- package/dist/client/pull_schedule_service.js +424 -0
- package/dist/client/request_reply.d.ts +61 -0
- package/dist/client/request_reply.js +232 -0
- package/dist/client/retry_message.d.ts +2 -0
- package/dist/client/retry_message.js +27 -0
- package/dist/client/send_result.d.ts +57 -0
- package/dist/client/send_result.js +70 -0
- package/dist/client/top_addressing.d.ts +20 -0
- package/dist/client/top_addressing.js +75 -0
- package/dist/client/trace_context.d.ts +61 -0
- package/dist/client/trace_context.js +373 -0
- package/dist/client/trace_dispatcher.d.ts +31 -0
- package/dist/client/trace_dispatcher.js +236 -0
- package/dist/client/trace_hook.d.ts +23 -0
- package/dist/client/trace_hook.js +228 -0
- package/dist/client/traceparent.d.ts +9 -0
- package/dist/client/traceparent.js +86 -0
- package/dist/common/boundary_type.d.ts +7 -0
- package/dist/common/boundary_type.js +16 -0
- package/dist/common/compress.d.ts +28 -0
- package/dist/common/compress.js +512 -0
- package/dist/common/message.d.ts +129 -0
- package/dist/common/message.js +214 -0
- package/dist/common/messageClientIdSetter.d.ts +7 -0
- package/dist/common/messageClientIdSetter.js +48 -0
- package/dist/common/messageConst.d.ts +68 -0
- package/dist/common/messageConst.js +68 -0
- package/dist/common/messageDecoder.d.ts +40 -0
- package/dist/common/messageDecoder.js +504 -0
- package/dist/common/messageType.d.ts +11 -0
- package/dist/common/messageType.js +11 -0
- package/dist/common/message_accessor.d.ts +39 -0
- package/dist/common/message_accessor.js +135 -0
- package/dist/common/mixAll.d.ts +106 -0
- package/dist/common/mixAll.js +247 -0
- package/dist/common/recall_message_handle.d.ts +25 -0
- package/dist/common/recall_message_handle.js +98 -0
- package/dist/common/subscriptionData.d.ts +36 -0
- package/dist/common/subscriptionData.js +64 -0
- package/dist/common/sysflag.d.ts +129 -0
- package/dist/common/sysflag.js +96 -0
- package/dist/common/topic_config.d.ts +66 -0
- package/dist/common/topic_config.js +106 -0
- package/dist/common/topic_validator.d.ts +49 -0
- package/dist/common/topic_validator.js +115 -0
- package/dist/common/utilAll.d.ts +39 -0
- package/dist/common/utilAll.js +154 -0
- package/dist/common/validators.d.ts +25 -0
- package/dist/common/validators.js +80 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +36 -0
- package/dist/logging.d.ts +24 -0
- package/dist/logging.js +246 -0
- package/dist/remoting/acl.d.ts +16 -0
- package/dist/remoting/acl.js +39 -0
- package/dist/remoting/admin_body.d.ts +257 -0
- package/dist/remoting/admin_body.js +669 -0
- package/dist/remoting/bodies.d.ts +475 -0
- package/dist/remoting/bodies.js +1131 -0
- package/dist/remoting/client.d.ts +73 -0
- package/dist/remoting/client.js +591 -0
- package/dist/remoting/codes.d.ts +296 -0
- package/dist/remoting/codes.js +301 -0
- package/dist/remoting/exception.d.ts +45 -0
- package/dist/remoting/exception.js +58 -0
- package/dist/remoting/extra_info.d.ts +61 -0
- package/dist/remoting/extra_info.js +220 -0
- package/dist/remoting/headers.d.ts +758 -0
- package/dist/remoting/headers.js +1307 -0
- package/dist/remoting/heartbeat.d.ts +91 -0
- package/dist/remoting/heartbeat.js +227 -0
- package/dist/remoting/namespace.d.ts +14 -0
- package/dist/remoting/namespace.js +85 -0
- package/dist/remoting/pop_bodies.d.ts +58 -0
- package/dist/remoting/pop_bodies.js +214 -0
- package/dist/remoting/remotingCommand.d.ts +39 -0
- package/dist/remoting/remotingCommand.js +225 -0
- package/dist/remoting/route.d.ts +61 -0
- package/dist/remoting/route.js +288 -0
- package/dist/remoting/rpc_hooks.d.ts +31 -0
- package/dist/remoting/rpc_hooks.js +73 -0
- package/dist/remoting/serialize.d.ts +33 -0
- package/dist/remoting/serialize.js +372 -0
- package/dist/remoting/socks_proxy.d.ts +29 -0
- package/dist/remoting/socks_proxy.js +379 -0
- package/dist/remoting/static_topic.d.ts +49 -0
- package/dist/remoting/static_topic.js +244 -0
- package/dist/remoting/subscription.d.ts +65 -0
- package/dist/remoting/subscription.js +172 -0
- package/package.json +38 -4
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// -*- coding: utf-8 -*-
|
|
2
|
+
// POP protocol wire bodies: SetMessageRequestModeRequestBody / BatchAck /
|
|
3
|
+
// BatchAckMessageRequestBody.
|
|
4
|
+
//
|
|
5
|
+
// Note: consumers of the classic remoting protocol never send BATCH_ACK_MESSAGE (200151);
|
|
6
|
+
// their POP path acknowledges messages one by one via ackAsync. We still export batch ack only
|
|
7
|
+
// because that wire capability genuinely exists (the broker implements it, and newer/proxy clients
|
|
8
|
+
// use it). Do NOT route POP consumer acks here — that would invent client behavior out of thin air.
|
|
9
|
+
import { RemotingSerializable } from "./serialize.js";
|
|
10
|
+
export const MESSAGE_REQUEST_MODE_PULL = 'PULL';
|
|
11
|
+
export const MESSAGE_REQUEST_MODE_POP = 'POP';
|
|
12
|
+
// SetMessageRequestModeRequestBody is the body of SET_MESSAGE_REQUEST_MODE
|
|
13
|
+
// (401). The broker has no header for this code — everything rides in the body.
|
|
14
|
+
export class SetMessageRequestModeRequestBody {
|
|
15
|
+
topic = '';
|
|
16
|
+
consumerGroup = '';
|
|
17
|
+
mode = MESSAGE_REQUEST_MODE_PULL; // Field default: a body without an explicit mode requests PULL.
|
|
18
|
+
popShareQueueNum = 0;
|
|
19
|
+
toDict() {
|
|
20
|
+
return {
|
|
21
|
+
topic: this.topic,
|
|
22
|
+
consumerGroup: this.consumerGroup,
|
|
23
|
+
mode: this.mode || MESSAGE_REQUEST_MODE_PULL,
|
|
24
|
+
popShareQueueNum: this.popShareQueueNum,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
encode() { return RemotingSerializable.encode(this.toDict()); }
|
|
28
|
+
}
|
|
29
|
+
// Wire form of java.util.BitSet: toByteArray() -> Base64. Only the bytes required by the highest set
|
|
30
|
+
// bit are sent (toByteArray drops trailing zero bytes).
|
|
31
|
+
export class BitSet {
|
|
32
|
+
bytes = null;
|
|
33
|
+
// set(bitIndex): bit 0 is the least significant bit of byte 0 (the protocol's bit ordering convention).
|
|
34
|
+
set(bitIndex) {
|
|
35
|
+
if (bitIndex < 0)
|
|
36
|
+
throw new Error('bitIndex < 0: ' + bitIndex);
|
|
37
|
+
const byteIndex = bitIndex >> 3;
|
|
38
|
+
const need = byteIndex + 1;
|
|
39
|
+
const cur = this.bytes && this.bytes.length > need ? this.bytes : new Uint8Array(need);
|
|
40
|
+
if (this.bytes)
|
|
41
|
+
cur.set(this.bytes.subarray(0, Math.min(this.bytes.length, cur.length)));
|
|
42
|
+
cur[byteIndex] |= (1 << (bitIndex & 7)) & 0xff;
|
|
43
|
+
this.bytes = cur;
|
|
44
|
+
}
|
|
45
|
+
popCount() {
|
|
46
|
+
if (!this.bytes)
|
|
47
|
+
return 0;
|
|
48
|
+
let n = 0;
|
|
49
|
+
for (const v of this.bytes) {
|
|
50
|
+
let x = v;
|
|
51
|
+
while (x !== 0) {
|
|
52
|
+
n += x & 1;
|
|
53
|
+
x >>= 1;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return n;
|
|
57
|
+
}
|
|
58
|
+
encodeBase64() {
|
|
59
|
+
if (!this.bytes || this.bytes.length === 0)
|
|
60
|
+
return '';
|
|
61
|
+
return Buffer.from(trimZeroTail(this.bytes)).toString('base64');
|
|
62
|
+
}
|
|
63
|
+
decodeBase64(encoded) {
|
|
64
|
+
if (!encoded) {
|
|
65
|
+
this.bytes = null;
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
this.bytes = new Uint8Array(Buffer.from(encoded, 'base64'));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function trimZeroTail(bytes) {
|
|
72
|
+
let end = bytes.length;
|
|
73
|
+
while (end > 0 && bytes[end - 1] === 0)
|
|
74
|
+
end--;
|
|
75
|
+
return bytes.subarray(0, end);
|
|
76
|
+
}
|
|
77
|
+
// BatchAck: one record covers all acknowledged messages sharing the same
|
|
78
|
+
// (retry, queueId, startOffset, popTime) group. The short key names ("c","t",
|
|
79
|
+
// "r","so","q","rq","pt","it","b") are the protocol field names on the wire.
|
|
80
|
+
export class BatchAck {
|
|
81
|
+
consumerGroup = '';
|
|
82
|
+
topic = '';
|
|
83
|
+
retry = '';
|
|
84
|
+
startOffset = 0;
|
|
85
|
+
queueId = 0;
|
|
86
|
+
reviveQueueId = 0;
|
|
87
|
+
popTime = 0;
|
|
88
|
+
invisibleTime = 0;
|
|
89
|
+
bitSet = new BitSet();
|
|
90
|
+
toDict() {
|
|
91
|
+
return {
|
|
92
|
+
c: this.consumerGroup, t: this.topic, r: this.retry, so: this.startOffset,
|
|
93
|
+
q: this.queueId, rq: this.reviveQueueId, pt: this.popTime, it: this.invisibleTime,
|
|
94
|
+
b: this.bitSet.encodeBase64(),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// BatchAckMessageRequestBody is the body of BATCH_ACK_MESSAGE (200151).
|
|
99
|
+
export class BatchAckMessageRequestBody {
|
|
100
|
+
brokerName = '';
|
|
101
|
+
acks = [];
|
|
102
|
+
toDict() {
|
|
103
|
+
return { brokerName: this.brokerName, acks: this.acks.map((a) => a.toDict()) };
|
|
104
|
+
}
|
|
105
|
+
encode() { return RemotingSerializable.encode(this.toDict()); }
|
|
106
|
+
}
|
|
107
|
+
// buildBatchAckMessageRequestBody performs the grouping + bitmap steps required for batch ack:
|
|
108
|
+
// checkpoints sharing (retry, queueId, ckQueueOffset, popTime) collapse into one BatchAck,
|
|
109
|
+
// with the bit index (msgQueueOffset - ckQueueOffset).
|
|
110
|
+
//
|
|
111
|
+
// topic/consumerGroup are supplied by the caller; all other values are read back from the checkpoint, never guessed.
|
|
112
|
+
export function buildBatchAckMessageRequestBody(topic, consumerGroup, extraInfos) {
|
|
113
|
+
const body = new BatchAckMessageRequestBody();
|
|
114
|
+
const index = new Map();
|
|
115
|
+
for (const extraInfo of extraInfos) {
|
|
116
|
+
const parts = splitCk(extraInfo);
|
|
117
|
+
if (parts.length < 8)
|
|
118
|
+
throw new Error('batchAck: checkpoint too short: ' + extraInfo);
|
|
119
|
+
const brokerName = parts[5];
|
|
120
|
+
if (!body.brokerName)
|
|
121
|
+
body.brokerName = brokerName;
|
|
122
|
+
const retry = parts[4];
|
|
123
|
+
const queueId = parseInt(parts[6], 10);
|
|
124
|
+
const msgQueueOffset = parseInt(parts[7], 10);
|
|
125
|
+
const ckQueueOffset = parseInt(parts[0], 10);
|
|
126
|
+
const popTime = parseInt(parts[1], 10);
|
|
127
|
+
const invisibleTime = parseInt(parts[2], 10);
|
|
128
|
+
const reviveQueueId = parseInt(parts[3], 10);
|
|
129
|
+
const key = [retry, queueId, ckQueueOffset, popTime].join('|');
|
|
130
|
+
let ack = index.get(key);
|
|
131
|
+
if (!ack) {
|
|
132
|
+
ack = new BatchAck();
|
|
133
|
+
ack.consumerGroup = consumerGroup;
|
|
134
|
+
ack.topic = topic;
|
|
135
|
+
ack.retry = retry;
|
|
136
|
+
ack.startOffset = ckQueueOffset;
|
|
137
|
+
ack.queueId = queueId;
|
|
138
|
+
ack.reviveQueueId = reviveQueueId;
|
|
139
|
+
ack.popTime = popTime;
|
|
140
|
+
ack.invisibleTime = invisibleTime;
|
|
141
|
+
index.set(key, ack);
|
|
142
|
+
body.acks.push(ack);
|
|
143
|
+
}
|
|
144
|
+
ack.bitSet.set(msgQueueOffset - ckQueueOffset);
|
|
145
|
+
}
|
|
146
|
+
return body;
|
|
147
|
+
}
|
|
148
|
+
// Local split (trailing empties dropped) to avoid importing extra_info here.
|
|
149
|
+
function splitCk(extraInfo) {
|
|
150
|
+
const parts = extraInfo.split(' ');
|
|
151
|
+
while (parts.length && parts[parts.length - 1] === '')
|
|
152
|
+
parts.pop();
|
|
153
|
+
return parts;
|
|
154
|
+
}
|
|
155
|
+
// ---------------------------------------------------------------------------
|
|
156
|
+
// QUERY_ASSIGNMENT(400) body: QueryAssignmentRequestBody /
|
|
157
|
+
// QueryAssignmentResponseBody + MessageQueueAssignment. Pure JSON in both directions.
|
|
158
|
+
// ---------------------------------------------------------------------------
|
|
159
|
+
export class QueryAssignmentRequestBody {
|
|
160
|
+
topic = '';
|
|
161
|
+
consumerGroup = '';
|
|
162
|
+
clientId = '';
|
|
163
|
+
messageModel = 'CLUSTERING';
|
|
164
|
+
strategyName = '';
|
|
165
|
+
// The protocol also carries extraInfo (POP context) — always sent, may be empty.
|
|
166
|
+
extraInfo = '';
|
|
167
|
+
toDict() {
|
|
168
|
+
return {
|
|
169
|
+
topic: this.topic,
|
|
170
|
+
consumerGroup: this.consumerGroup,
|
|
171
|
+
clientId: this.clientId,
|
|
172
|
+
messageModel: this.messageModel,
|
|
173
|
+
strategyName: this.strategyName,
|
|
174
|
+
extraInfo: this.extraInfo,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
encode() { return RemotingSerializable.encode(this.toDict()); }
|
|
178
|
+
}
|
|
179
|
+
export class MessageQueueAssignment {
|
|
180
|
+
mq;
|
|
181
|
+
mode;
|
|
182
|
+
constructor(topic, brokerName, queueId, mode) {
|
|
183
|
+
this.mq = { topic, brokerName, queueId };
|
|
184
|
+
this.mode = mode;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
export class QueryAssignmentResponseBody {
|
|
188
|
+
// assignments === null means "the result is invalid, skip this update". An empty set
|
|
189
|
+
// (the broker explicitly assigned no queues) is a genuine response.
|
|
190
|
+
static decode(body) {
|
|
191
|
+
if (body == null || body.length === 0)
|
|
192
|
+
return null;
|
|
193
|
+
let obj;
|
|
194
|
+
try {
|
|
195
|
+
obj = JSON.parse(body.toString('utf-8'));
|
|
196
|
+
}
|
|
197
|
+
catch (e) {
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
const raw = obj != null && Array.isArray(obj['messageQueueAssignments'])
|
|
201
|
+
? obj['messageQueueAssignments']
|
|
202
|
+
: (Array.isArray(obj) ? obj : null);
|
|
203
|
+
if (raw == null)
|
|
204
|
+
return null;
|
|
205
|
+
const out = [];
|
|
206
|
+
for (const a of raw) {
|
|
207
|
+
const mqa = a && a['messageQueue'] ? a['messageQueue'] : a;
|
|
208
|
+
if (mqa == null || mqa['topic'] == null)
|
|
209
|
+
continue;
|
|
210
|
+
out.push(new MessageQueueAssignment(String(mqa['topic']), String(mqa['brokerName'] ?? ''), Number(mqa['queueId'] ?? 0) | 0, String(a && a['mode'] ? a['mode'] : 'PULL')));
|
|
211
|
+
}
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
export declare const CURRENT_VERSION = 515;
|
|
3
|
+
export declare class RemotingCommand {
|
|
4
|
+
code: number;
|
|
5
|
+
language: number;
|
|
6
|
+
version: number;
|
|
7
|
+
opaque: number;
|
|
8
|
+
flag: number;
|
|
9
|
+
remark: string | null;
|
|
10
|
+
extFields: Record<string, string>;
|
|
11
|
+
customHeader: any;
|
|
12
|
+
body: Buffer | null;
|
|
13
|
+
serializeTypeCurrentRpc: number;
|
|
14
|
+
constructor(code?: number, customHeader?: any, remark?: string | null, opaque?: number | null, flag?: number, body?: Buffer | null);
|
|
15
|
+
static createNewRequestId(): number;
|
|
16
|
+
static createRequestCommand(code: number, customHeader?: any): RemotingCommand;
|
|
17
|
+
static createResponseCommandWithHeader(code: number, customHeader?: any): RemotingCommand;
|
|
18
|
+
static createResponseCommand(code?: number, remark?: string, classHeader?: any): RemotingCommand | null;
|
|
19
|
+
static buildErrorResponse(code: number, remark: string): RemotingCommand | null;
|
|
20
|
+
static getProtocolType(source: number): number;
|
|
21
|
+
static getHeaderLength(length: number): number;
|
|
22
|
+
static markProtocolType(source: number, stype: number): number;
|
|
23
|
+
markResponseType(): void;
|
|
24
|
+
isResponseType(): boolean;
|
|
25
|
+
markOnewayRpc(): void;
|
|
26
|
+
isOnewayRpc(): boolean;
|
|
27
|
+
getType(): string;
|
|
28
|
+
makeCustomHeaderToNet(): void;
|
|
29
|
+
headerEncode(): Buffer;
|
|
30
|
+
encode(): Buffer;
|
|
31
|
+
encodeHeader(bodyLength?: number): Buffer;
|
|
32
|
+
static decode(data: Buffer): RemotingCommand;
|
|
33
|
+
decodeCommandCustomHeader(headerCls: any, _useFastEncode?: boolean): any;
|
|
34
|
+
_toDict(): Record<string, any>;
|
|
35
|
+
addExtField(key: string, value: string): void;
|
|
36
|
+
getExtField(key: string): string | undefined;
|
|
37
|
+
toString(): string;
|
|
38
|
+
}
|
|
39
|
+
export default RemotingCommand;
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { LanguageCode, SerializeType } from "./codes.js";
|
|
3
|
+
import { RemotingSerializable, RocketMQSerializable } from "./serialize.js";
|
|
4
|
+
const SERIALIZE_TYPE_ENV = 'ROCKETMQ_SERIALIZE_TYPE';
|
|
5
|
+
// Protocol version ordinal corresponding to 5.5.1. Must be > 28 (V3_0_7_SNAPSHOT) so the broker
|
|
6
|
+
// invokes the 307/resetOffset callback; sending 0 would trigger a downgrade.
|
|
7
|
+
export const CURRENT_VERSION = 515;
|
|
8
|
+
const RPC_TYPE = 0;
|
|
9
|
+
const RPC_ONEWAY = 1;
|
|
10
|
+
let _serializeTypeConfig = SerializeType.JSON;
|
|
11
|
+
function _loadSerializeTypeConfig() {
|
|
12
|
+
const v = (process.env[SERIALIZE_TYPE_ENV] || '').trim().toUpperCase();
|
|
13
|
+
if (v === 'ROCKETMQ')
|
|
14
|
+
_serializeTypeConfig = SerializeType.ROCKETMQ;
|
|
15
|
+
else
|
|
16
|
+
_serializeTypeConfig = SerializeType.JSON;
|
|
17
|
+
}
|
|
18
|
+
_loadSerializeTypeConfig();
|
|
19
|
+
let _requestId = 0;
|
|
20
|
+
function nextRequestId() { return _requestId++; }
|
|
21
|
+
export class RemotingCommand {
|
|
22
|
+
code;
|
|
23
|
+
language;
|
|
24
|
+
version;
|
|
25
|
+
opaque;
|
|
26
|
+
flag;
|
|
27
|
+
remark;
|
|
28
|
+
extFields;
|
|
29
|
+
customHeader;
|
|
30
|
+
body;
|
|
31
|
+
serializeTypeCurrentRpc;
|
|
32
|
+
constructor(code = 0, customHeader = null, remark = null, opaque = null, flag = 0, body = null) {
|
|
33
|
+
this.code = code;
|
|
34
|
+
this.language = LanguageCode.NODE_JS;
|
|
35
|
+
this.version = 0;
|
|
36
|
+
this.opaque = opaque == null ? nextRequestId() : opaque;
|
|
37
|
+
this.flag = flag;
|
|
38
|
+
this.remark = remark;
|
|
39
|
+
this.extFields = {};
|
|
40
|
+
this.customHeader = customHeader;
|
|
41
|
+
this.body = body;
|
|
42
|
+
this.serializeTypeCurrentRpc = _serializeTypeConfig;
|
|
43
|
+
}
|
|
44
|
+
static createNewRequestId() { return nextRequestId(); }
|
|
45
|
+
static createRequestCommand(code, customHeader = null) {
|
|
46
|
+
const cmd = new RemotingCommand(code, customHeader);
|
|
47
|
+
_setCmdVersion(cmd);
|
|
48
|
+
return cmd;
|
|
49
|
+
}
|
|
50
|
+
static createResponseCommandWithHeader(code, customHeader = null) {
|
|
51
|
+
const cmd = new RemotingCommand(code, customHeader);
|
|
52
|
+
cmd.markResponseType();
|
|
53
|
+
_setCmdVersion(cmd);
|
|
54
|
+
return cmd;
|
|
55
|
+
}
|
|
56
|
+
static createResponseCommand(code = 0, remark = 'not set any response code', classHeader = null) {
|
|
57
|
+
const cmd = new RemotingCommand(code, null, remark);
|
|
58
|
+
cmd.markResponseType();
|
|
59
|
+
_setCmdVersion(cmd);
|
|
60
|
+
if (classHeader != null) {
|
|
61
|
+
try {
|
|
62
|
+
cmd.customHeader = new classHeader();
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return cmd;
|
|
69
|
+
}
|
|
70
|
+
static buildErrorResponse(code, remark) {
|
|
71
|
+
return RemotingCommand.createResponseCommand(code, remark, null);
|
|
72
|
+
}
|
|
73
|
+
static getProtocolType(source) { return (source >> 24) & 0xff; }
|
|
74
|
+
static getHeaderLength(length) { return length & 0x00ffffff; }
|
|
75
|
+
static markProtocolType(source, stype) { return ((stype & 0xff) << 24) | (source & 0x00ffffff); }
|
|
76
|
+
markResponseType() { this.flag |= 1 << RPC_TYPE; }
|
|
77
|
+
isResponseType() { return (this.flag & (1 << RPC_TYPE)) === (1 << RPC_TYPE); }
|
|
78
|
+
markOnewayRpc() { this.flag |= 1 << RPC_ONEWAY; }
|
|
79
|
+
isOnewayRpc() { return (this.flag & (1 << RPC_ONEWAY)) === (1 << RPC_ONEWAY); }
|
|
80
|
+
getType() { return this.isResponseType() ? 'RESPONSE_COMMAND' : 'REQUEST_COMMAND'; }
|
|
81
|
+
makeCustomHeaderToNet() {
|
|
82
|
+
if (this.customHeader != null) {
|
|
83
|
+
const fn = this.customHeader.toExtFields;
|
|
84
|
+
if (typeof fn === 'function') {
|
|
85
|
+
const fields = fn.call(this.customHeader);
|
|
86
|
+
for (const [k, v] of Object.entries(fields)) {
|
|
87
|
+
if (v != null)
|
|
88
|
+
this.extFields[k] = String(v);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
headerEncode() {
|
|
94
|
+
this.makeCustomHeaderToNet();
|
|
95
|
+
if (this.serializeTypeCurrentRpc === SerializeType.ROCKETMQ) {
|
|
96
|
+
return RocketMQSerializable.rocketMqProtocolEncode(this);
|
|
97
|
+
}
|
|
98
|
+
return RemotingSerializable.encode(this._toDict());
|
|
99
|
+
}
|
|
100
|
+
encode() {
|
|
101
|
+
let length = 4;
|
|
102
|
+
const headerData = this.headerEncode();
|
|
103
|
+
length += headerData.length;
|
|
104
|
+
const body = this.body;
|
|
105
|
+
if (body != null)
|
|
106
|
+
length += body.length;
|
|
107
|
+
const out = Buffer.alloc(length + 4);
|
|
108
|
+
let o = 0;
|
|
109
|
+
out.writeInt32BE(length, o);
|
|
110
|
+
o += 4;
|
|
111
|
+
out.writeInt32BE(RemotingCommand.markProtocolType(headerData.length, this.serializeTypeCurrentRpc), o);
|
|
112
|
+
o += 4;
|
|
113
|
+
headerData.copy(out, o);
|
|
114
|
+
o += headerData.length;
|
|
115
|
+
if (body != null)
|
|
116
|
+
body.copy(out, o);
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
encodeHeader(bodyLength = 0) {
|
|
120
|
+
let length = 4;
|
|
121
|
+
const headerData = this.headerEncode();
|
|
122
|
+
length += headerData.length + bodyLength;
|
|
123
|
+
const out = Buffer.alloc(length + 4);
|
|
124
|
+
let o = 0;
|
|
125
|
+
out.writeInt32BE(length, o);
|
|
126
|
+
o += 4;
|
|
127
|
+
out.writeInt32BE(RemotingCommand.markProtocolType(headerData.length, this.serializeTypeCurrentRpc), o);
|
|
128
|
+
o += 4;
|
|
129
|
+
headerData.copy(out, o);
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
static decode(data) {
|
|
133
|
+
let offset = 0;
|
|
134
|
+
const totalLength = data.readInt32BE(offset);
|
|
135
|
+
offset += 4;
|
|
136
|
+
if (totalLength > data.length - 4)
|
|
137
|
+
throw new Error(`decode error, bad total length: ${totalLength}`);
|
|
138
|
+
const oriHeaderLen = data.readInt32BE(offset);
|
|
139
|
+
offset += 4;
|
|
140
|
+
const headerLength = RemotingCommand.getHeaderLength(oriHeaderLen);
|
|
141
|
+
if (headerLength > data.length - offset)
|
|
142
|
+
throw new Error(`decode error, bad header length: ${headerLength}`);
|
|
143
|
+
const protocolType = RemotingCommand.getProtocolType(oriHeaderLen);
|
|
144
|
+
const headerData = data.subarray(offset, offset + headerLength);
|
|
145
|
+
offset += headerLength;
|
|
146
|
+
let cmd;
|
|
147
|
+
if (protocolType === SerializeType.ROCKETMQ) {
|
|
148
|
+
const fields = RocketMQSerializable.rocketMqProtocolDecode(headerData);
|
|
149
|
+
cmd = new RemotingCommand(fields.code, null, fields.remark, fields.opaque, fields.flag);
|
|
150
|
+
cmd.language = fields.language;
|
|
151
|
+
cmd.version = fields.version;
|
|
152
|
+
cmd.extFields = fields.extFields || {};
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
const obj = RemotingSerializable.decode(headerData);
|
|
156
|
+
cmd = new RemotingCommand(obj.code, null, obj.remark, obj.opaque, obj.flag);
|
|
157
|
+
let lang = obj.language;
|
|
158
|
+
if (typeof lang === 'string')
|
|
159
|
+
lang = LanguageCode.nameToCode(lang);
|
|
160
|
+
cmd.language = typeof lang === 'number' ? lang : LanguageCode.NODE_JS;
|
|
161
|
+
cmd.version = obj.version | 0;
|
|
162
|
+
cmd.extFields = obj.extFields || {};
|
|
163
|
+
}
|
|
164
|
+
cmd.serializeTypeCurrentRpc = protocolType;
|
|
165
|
+
const bodyLength = data.length - offset;
|
|
166
|
+
cmd.body = bodyLength > 0 ? Buffer.from(data.subarray(offset)) : null;
|
|
167
|
+
return cmd;
|
|
168
|
+
}
|
|
169
|
+
decodeCommandCustomHeader(headerCls, _useFastEncode = true) {
|
|
170
|
+
const h = new headerCls();
|
|
171
|
+
if (typeof h.fromExtFields === 'function') {
|
|
172
|
+
h.fromExtFields(this.extFields);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
for (const [k, v] of Object.entries(this.extFields)) {
|
|
176
|
+
if (h[k] !== undefined) {
|
|
177
|
+
const cur = h[k];
|
|
178
|
+
if (typeof cur === 'number') {
|
|
179
|
+
h[k] = cur % 1 === 0 ? parseInt(v, 10) : parseFloat(v);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
h[k] = v;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
h[k] = v;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
this.customHeader = h;
|
|
191
|
+
return h;
|
|
192
|
+
}
|
|
193
|
+
_toDict() {
|
|
194
|
+
const d = {
|
|
195
|
+
code: this.code,
|
|
196
|
+
language: this.language,
|
|
197
|
+
version: this.version,
|
|
198
|
+
opaque: this.opaque,
|
|
199
|
+
flag: this.flag,
|
|
200
|
+
};
|
|
201
|
+
if (this.remark != null)
|
|
202
|
+
d['remark'] = this.remark;
|
|
203
|
+
if (Object.keys(this.extFields).length)
|
|
204
|
+
d['extFields'] = this.extFields;
|
|
205
|
+
return d;
|
|
206
|
+
}
|
|
207
|
+
addExtField(key, value) { this.extFields[key] = value; }
|
|
208
|
+
getExtField(key) { return this.extFields[key]; }
|
|
209
|
+
toString() {
|
|
210
|
+
return `RemotingCommand [code=${this.code}, language=${this.language}, version=${this.version}, ` +
|
|
211
|
+
`opaque=${this.opaque}, flag(B)=${this.flag.toString(2)}, remark=${this.remark}, extFields=${JSON.stringify(this.extFields)}, serializeType=${this.serializeTypeCurrentRpc}]`;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function _setCmdVersion(cmd) {
|
|
215
|
+
const v = process.env['rocketmq.remoting.version'];
|
|
216
|
+
if (v) {
|
|
217
|
+
const n = parseInt(v, 10);
|
|
218
|
+
if (!Number.isNaN(n)) {
|
|
219
|
+
cmd.version = n;
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
cmd.version = CURRENT_VERSION;
|
|
224
|
+
}
|
|
225
|
+
export default RemotingCommand;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { MessageQueue } from '../common/message.js';
|
|
2
|
+
export declare class QueueData {
|
|
3
|
+
brokerName: string;
|
|
4
|
+
readQueueNums: number;
|
|
5
|
+
writeQueueNums: number;
|
|
6
|
+
perm: number;
|
|
7
|
+
topicSysFlag: number;
|
|
8
|
+
constructor(brokerName?: string, readQueueNums?: number, writeQueueNums?: number, perm?: number, topicSysFlag?: number);
|
|
9
|
+
clone(): QueueData;
|
|
10
|
+
toDict(): Record<string, any>;
|
|
11
|
+
static fromDict(d: Record<string, any>): QueueData;
|
|
12
|
+
}
|
|
13
|
+
export declare class BrokerData {
|
|
14
|
+
cluster: string;
|
|
15
|
+
brokerName: string;
|
|
16
|
+
brokerAddrs: Record<number, string>;
|
|
17
|
+
zoneName: string;
|
|
18
|
+
enableActingMaster: boolean;
|
|
19
|
+
constructor(cluster?: string, brokerName?: string, brokerAddrs?: Record<number, string> | null, zoneName?: string);
|
|
20
|
+
selectBrokerAddr(): string | null;
|
|
21
|
+
clone(): BrokerData;
|
|
22
|
+
toDict(): Record<string, any>;
|
|
23
|
+
static fromDict(d: Record<string, any>): BrokerData;
|
|
24
|
+
}
|
|
25
|
+
export declare class TopicRouteData {
|
|
26
|
+
orderTopicConf: string | null;
|
|
27
|
+
queueDatas: QueueData[];
|
|
28
|
+
brokerDatas: BrokerData[];
|
|
29
|
+
filterServerTable: Record<string, string[]>;
|
|
30
|
+
topicQueueMappingByBroker: Record<string, any> | null;
|
|
31
|
+
constructor();
|
|
32
|
+
getAllMessageQueue(topic?: string): MessageQueue[];
|
|
33
|
+
getAllSubscribeMessageQueue(topic?: string): MessageQueue[];
|
|
34
|
+
getStaticTopicEndPoints(topic: string): Map<MessageQueue, string> | null;
|
|
35
|
+
cloneTopicRouteData(): TopicRouteData;
|
|
36
|
+
topicRouteDataChanged(old: TopicRouteData | null): boolean;
|
|
37
|
+
toDict(): Record<string, any>;
|
|
38
|
+
static fromDict(d: Record<string, any>): TopicRouteData;
|
|
39
|
+
encode(): Buffer;
|
|
40
|
+
static decode(data: Buffer): TopicRouteData;
|
|
41
|
+
}
|
|
42
|
+
export declare function brokerDataList2Map(bdList: BrokerData[]): Record<string, Record<number, string>>;
|
|
43
|
+
export declare function brokerData2Json(bd: BrokerData): string;
|
|
44
|
+
export declare function topicRouteData2TopicRouteDataJson(trd: TopicRouteData): string;
|
|
45
|
+
export declare function findBrokerAddressInPublish(brokerName: string, trd: TopicRouteData): string | null;
|
|
46
|
+
export declare function findBrokerAddressInSubscribe(brokerName: string, brokerId: number, onlyThisBroker: boolean, trd: TopicRouteData): string | null;
|
|
47
|
+
export declare function findBrokerAddressInNotRepeat(brokerName: string, trd: TopicRouteData): string | null;
|
|
48
|
+
export declare function brokerDeployInContainer(brokerName: string, trd: TopicRouteData): boolean;
|
|
49
|
+
declare const _default: {
|
|
50
|
+
QueueData: typeof QueueData;
|
|
51
|
+
BrokerData: typeof BrokerData;
|
|
52
|
+
TopicRouteData: typeof TopicRouteData;
|
|
53
|
+
brokerDataList2Map: typeof brokerDataList2Map;
|
|
54
|
+
brokerData2Json: typeof brokerData2Json;
|
|
55
|
+
topicRouteData2TopicRouteDataJson: typeof topicRouteData2TopicRouteDataJson;
|
|
56
|
+
findBrokerAddressInPublish: typeof findBrokerAddressInPublish;
|
|
57
|
+
findBrokerAddressInSubscribe: typeof findBrokerAddressInSubscribe;
|
|
58
|
+
findBrokerAddressInNotRepeat: typeof findBrokerAddressInNotRepeat;
|
|
59
|
+
brokerDeployInContainer: typeof brokerDeployInContainer;
|
|
60
|
+
};
|
|
61
|
+
export default _default;
|