@tdengine/websocket 3.0.0 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/example/basicSchemaless.d.ts +2 -0
- package/lib/example/basicSchemaless.d.ts.map +1 -0
- package/lib/example/basicSchemaless.js +47 -0
- package/lib/example/basicSql.d.ts +2 -0
- package/lib/example/basicSql.d.ts.map +1 -0
- package/lib/example/basicSql.js +52 -0
- package/lib/example/basicStmt.d.ts +2 -0
- package/lib/example/basicStmt.d.ts.map +1 -0
- package/lib/example/basicStmt.js +58 -0
- package/lib/example/basicTmq.d.ts +2 -0
- package/lib/example/basicTmq.d.ts.map +1 -0
- package/lib/example/basicTmq.js +71 -0
- package/lib/index.d.ts +26 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +41 -0
- package/lib/src/client/wsClient.d.ts +22 -0
- package/lib/src/client/wsClient.d.ts.map +1 -0
- package/lib/src/client/wsClient.js +253 -0
- package/lib/src/client/wsConnector.d.ts +17 -0
- package/lib/src/client/wsConnector.d.ts.map +1 -0
- package/lib/src/client/wsConnector.js +140 -0
- package/lib/src/client/wsConnectorPool.d.ts +13 -0
- package/lib/src/client/wsConnectorPool.d.ts.map +1 -0
- package/lib/src/client/wsConnectorPool.js +107 -0
- package/lib/src/client/wsEventCallback.d.ts +22 -0
- package/lib/src/client/wsEventCallback.d.ts.map +1 -0
- package/lib/src/client/wsEventCallback.js +97 -0
- package/lib/src/client/wsResponse.d.ts +66 -0
- package/lib/src/client/wsResponse.d.ts.map +1 -0
- package/lib/src/client/wsResponse.js +76 -0
- package/lib/src/common/config.d.ts +22 -0
- package/lib/src/common/config.d.ts.map +1 -0
- package/lib/src/common/config.js +45 -0
- package/lib/src/common/constant.d.ts +13 -0
- package/lib/src/common/constant.d.ts.map +1 -0
- package/{src/constant.ts → lib/src/common/constant.js} +25 -24
- package/lib/src/common/log.d.ts +5 -0
- package/lib/src/common/log.d.ts.map +1 -0
- package/lib/src/common/log.js +40 -0
- package/lib/src/common/reqid.d.ts +8 -0
- package/lib/src/common/reqid.d.ts.map +1 -0
- package/lib/src/common/reqid.js +59 -0
- package/lib/src/common/taosResult.d.ts +51 -0
- package/lib/src/common/taosResult.d.ts.map +1 -0
- package/lib/src/common/taosResult.js +419 -0
- package/lib/src/common/ut8Helper.d.ts +2 -0
- package/lib/src/common/ut8Helper.d.ts.map +1 -0
- package/{src/ut8Helper.ts → lib/src/common/ut8Helper.js} +22 -21
- package/lib/src/common/utils.d.ts +5 -0
- package/lib/src/common/utils.d.ts.map +1 -0
- package/lib/src/common/utils.js +33 -0
- package/lib/src/common/wsError.d.ts +29 -0
- package/lib/src/common/wsError.d.ts.map +1 -0
- package/lib/src/common/wsError.js +51 -0
- package/{src/wsOptions.ts → lib/src/common/wsOptions.d.ts} +4 -2
- package/lib/src/common/wsOptions.d.ts.map +1 -0
- package/lib/src/common/wsOptions.js +2 -0
- package/lib/src/index.d.ts +9 -0
- package/lib/src/index.d.ts.map +1 -0
- package/lib/src/index.js +58 -0
- package/lib/src/sql/wsProto.d.ts +26 -0
- package/lib/src/sql/wsProto.d.ts.map +1 -0
- package/lib/src/sql/wsProto.js +19 -0
- package/lib/src/sql/wsRows.d.ts +16 -0
- package/lib/src/sql/wsRows.d.ts.map +1 -0
- package/lib/src/sql/wsRows.js +76 -0
- package/lib/src/sql/wsSql.d.ts +24 -0
- package/lib/src/sql/wsSql.d.ts.map +1 -0
- package/lib/src/sql/wsSql.js +164 -0
- package/lib/src/stmt/wsParams.d.ts +41 -0
- package/lib/src/stmt/wsParams.d.ts.map +1 -0
- package/lib/src/stmt/wsParams.js +428 -0
- package/lib/src/stmt/wsProto.d.ts +27 -0
- package/lib/src/stmt/wsProto.d.ts.map +1 -0
- package/lib/src/stmt/wsProto.js +67 -0
- package/lib/src/stmt/wsStmt.d.ts +30 -0
- package/lib/src/stmt/wsStmt.d.ts.map +1 -0
- package/lib/src/stmt/wsStmt.js +207 -0
- package/lib/src/tmq/config.d.ts +14 -0
- package/lib/src/tmq/config.d.ts.map +1 -0
- package/lib/src/tmq/config.js +42 -0
- package/lib/src/tmq/constant.d.ts +85 -0
- package/lib/src/tmq/constant.d.ts.map +1 -0
- package/lib/src/tmq/constant.js +89 -0
- package/lib/src/tmq/tmqResponse.d.ts +80 -0
- package/lib/src/tmq/tmqResponse.d.ts.map +1 -0
- package/lib/src/tmq/tmqResponse.js +204 -0
- package/lib/src/tmq/wsTmq.d.ts +32 -0
- package/lib/src/tmq/wsTmq.d.ts.map +1 -0
- package/lib/src/tmq/wsTmq.js +316 -0
- package/lib/test/bulkPulling/queryTables.test.d.ts +2 -0
- package/lib/test/bulkPulling/queryTables.test.d.ts.map +1 -0
- package/lib/test/bulkPulling/queryTables.test.js +297 -0
- package/lib/test/bulkPulling/schemaless.test.d.ts +2 -0
- package/lib/test/bulkPulling/schemaless.test.d.ts.map +1 -0
- package/lib/test/bulkPulling/schemaless.test.js +94 -0
- package/lib/test/bulkPulling/sql.test.d.ts +2 -0
- package/lib/test/bulkPulling/sql.test.d.ts.map +1 -0
- package/lib/test/bulkPulling/sql.test.js +124 -0
- package/lib/test/bulkPulling/stmt.func.test.d.ts +2 -0
- package/lib/test/bulkPulling/stmt.func.test.d.ts.map +1 -0
- package/lib/test/bulkPulling/stmt.func.test.js +381 -0
- package/lib/test/bulkPulling/stmt.type.test.d.ts +2 -0
- package/lib/test/bulkPulling/stmt.type.test.d.ts.map +1 -0
- package/lib/test/bulkPulling/stmt.type.test.js +273 -0
- package/lib/test/bulkPulling/tmq.test.d.ts +2 -0
- package/lib/test/bulkPulling/tmq.test.d.ts.map +1 -0
- package/lib/test/bulkPulling/tmq.test.js +177 -0
- package/lib/test/bulkPulling/wsConnectPool.test.d.ts +2 -0
- package/lib/test/bulkPulling/wsConnectPool.test.d.ts.map +1 -0
- package/lib/test/bulkPulling/wsConnectPool.test.js +129 -0
- package/lib/test/utils.d.ts +18 -0
- package/lib/test/utils.d.ts.map +1 -0
- package/lib/test/utils.js +318 -0
- package/package.json +27 -14
- package/readme.md +302 -0
- package/README.md +0 -59
- package/dist/browser/index.js +0 -778
- package/dist/main/index.js +0 -711
- package/dist/main/index.js.map +0 -1
- package/dist/module/index.mjs +0 -704
- package/dist/module/index.mjs.map +0 -1
- package/dist/types.d.ts +0 -103
- package/dist/types.d.ts.map +0 -1
- package/example/basicUsageAsync.ts +0 -48
- package/example/basicUsagePrimse.ts +0 -43
- package/example/cloudUsage.ts +0 -55
- package/example/continousConnectAndVersion.ts +0 -16
- package/example/test.mjs +0 -51
- package/index.ts +0 -7
- package/jest.config.js +0 -8
- package/src/taosResult.ts +0 -269
- package/src/tdengineWebsocket.ts +0 -39
- package/src/wsClient.ts +0 -196
- package/src/wsError.ts +0 -5
- package/src/wsQuery.ts +0 -30
- package/src/wsQueryInterface.ts +0 -212
- package/src/wsQueryResponse.ts +0 -112
- package/tdengine-websocket-3.0.0.tgz +0 -0
- package/test/bulkPulling/connect.test.ts +0 -27
- package/test/bulkPulling/queryTables.test.ts +0 -274
- package/test/bulkPulling/version.test.ts +0 -19
- package/test/utils.ts +0 -235
- package/tsconfig.json +0 -101
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseTmqBlock = exports.TopicPartition = exports.CommittedResp = exports.PartitionsResp = exports.SubscriptionResp = exports.AssignmentResp = exports.WSTmqFetchBlockResponse = exports.TaosTmqResult = exports.WsTmqQueryResponse = exports.WsPollResponse = void 0;
|
|
4
|
+
const wsResponse_1 = require("../client/wsResponse");
|
|
5
|
+
const constant_1 = require("../common/constant");
|
|
6
|
+
const taosResult_1 = require("../common/taosResult");
|
|
7
|
+
const wsError_1 = require("../common/wsError");
|
|
8
|
+
class WsPollResponse {
|
|
9
|
+
constructor(resp) {
|
|
10
|
+
this.totalTime = resp.totalTime;
|
|
11
|
+
this.code = resp.msg.code;
|
|
12
|
+
this.message = resp.msg.message;
|
|
13
|
+
this.action = resp.msg.action;
|
|
14
|
+
this.req_id = resp.msg.req_id;
|
|
15
|
+
this.have_message = resp.msg.have_message;
|
|
16
|
+
this.topic = resp.msg.topic;
|
|
17
|
+
this.database = resp.msg.database;
|
|
18
|
+
this.vgroup_id = resp.msg.vgroup_id;
|
|
19
|
+
this.message_id = resp.msg.message_id;
|
|
20
|
+
this.message_type = resp.msg.message_type;
|
|
21
|
+
if (resp.msg.id) {
|
|
22
|
+
this.id = BigInt(resp.msg.id);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
this.id = BigInt(0);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.WsPollResponse = WsPollResponse;
|
|
30
|
+
// resp: {"code":0,"message":"","action":"fetch","req_id":4,"message_id":1,"completed":false,"table_name":"ct2","rows":1,"fields_count":4,"fields_names":["ts","c1","c2","c3"],"fields_types":[9,4,6,8],"fields_lengths":[8,4,4,10],"precision":0}
|
|
31
|
+
class WsTmqQueryResponse extends wsResponse_1.WSQueryResponse {
|
|
32
|
+
constructor(resp) {
|
|
33
|
+
super(resp);
|
|
34
|
+
this.completed = resp.msg.completed;
|
|
35
|
+
this.table_name = resp.msg.table_name;
|
|
36
|
+
this.rows = resp.msg.rows;
|
|
37
|
+
this.message_id = resp.msg.message_id;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.WsTmqQueryResponse = WsTmqQueryResponse;
|
|
41
|
+
class TaosTmqResult extends taosResult_1.TaosResult {
|
|
42
|
+
constructor(resp, pollResp) {
|
|
43
|
+
super(resp);
|
|
44
|
+
this.table_name = resp.table_name;
|
|
45
|
+
// this._affectRows = resp.rows;
|
|
46
|
+
this.topic = pollResp.topic;
|
|
47
|
+
this.database = pollResp.database;
|
|
48
|
+
this.vgroup_id = pollResp.vgroup_id;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.TaosTmqResult = TaosTmqResult;
|
|
52
|
+
class WSTmqFetchBlockResponse {
|
|
53
|
+
constructor(resp) {
|
|
54
|
+
this.totalTime = resp.totalTime;
|
|
55
|
+
this.blockData = resp.msg;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.WSTmqFetchBlockResponse = WSTmqFetchBlockResponse;
|
|
59
|
+
class AssignmentResp {
|
|
60
|
+
constructor(resp, topic) {
|
|
61
|
+
this.timing = BigInt(resp.msg.timing);
|
|
62
|
+
this.code = resp.msg.code;
|
|
63
|
+
this.message = resp.msg.message;
|
|
64
|
+
this.req_id = resp.msg.req_id;
|
|
65
|
+
this.action = resp.msg.action;
|
|
66
|
+
this.totalTime = resp.totalTime;
|
|
67
|
+
this.topicPartition = resp.msg.assignment;
|
|
68
|
+
for (let i in this.topicPartition) {
|
|
69
|
+
this.topicPartition[i].topic = topic;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.AssignmentResp = AssignmentResp;
|
|
74
|
+
class SubscriptionResp {
|
|
75
|
+
constructor(resp) {
|
|
76
|
+
this.timing = BigInt(resp.msg.timing);
|
|
77
|
+
this.code = resp.msg.code;
|
|
78
|
+
this.message = resp.msg.message;
|
|
79
|
+
this.req_id = resp.msg.req_id;
|
|
80
|
+
this.action = resp.msg.action;
|
|
81
|
+
this.totalTime = resp.totalTime;
|
|
82
|
+
this.topics = resp.msg.topics;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.SubscriptionResp = SubscriptionResp;
|
|
86
|
+
class PartitionsResp {
|
|
87
|
+
constructor(resp) {
|
|
88
|
+
this.timing = BigInt(resp.msg.timing);
|
|
89
|
+
this.code = resp.msg.code;
|
|
90
|
+
this.message = resp.msg.message;
|
|
91
|
+
this.req_id = resp.msg.req_id;
|
|
92
|
+
this.action = resp.msg.action;
|
|
93
|
+
this.totalTime = resp.totalTime;
|
|
94
|
+
this.positions = resp.msg.position;
|
|
95
|
+
}
|
|
96
|
+
setTopicPartitions(topicPartitions) {
|
|
97
|
+
if (topicPartitions.length != this.positions.length) {
|
|
98
|
+
throw new wsError_1.WebSocketInterfaceError(wsError_1.ErrorCode.ERR_PARTITIONS_TOPIC_VGROUP_LENGTH_NOT_EQUAL, 'TopicPartitions and positions are not equal in length');
|
|
99
|
+
}
|
|
100
|
+
for (let i in this.positions) {
|
|
101
|
+
topicPartitions[i].offset = this.positions[i];
|
|
102
|
+
}
|
|
103
|
+
return topicPartitions;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.PartitionsResp = PartitionsResp;
|
|
107
|
+
class CommittedResp extends PartitionsResp {
|
|
108
|
+
constructor(resp) {
|
|
109
|
+
super(resp);
|
|
110
|
+
this.positions = resp.msg.committed;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.CommittedResp = CommittedResp;
|
|
114
|
+
class TopicPartition {
|
|
115
|
+
constructor(msg) {
|
|
116
|
+
this.vgroup_id = msg.vgroup_id;
|
|
117
|
+
this.offset = msg.offset;
|
|
118
|
+
this.begin = msg.begin;
|
|
119
|
+
this.end = msg.end;
|
|
120
|
+
this.topic = '';
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
exports.TopicPartition = TopicPartition;
|
|
124
|
+
function parseTmqBlock(rows, resp, taosResult) {
|
|
125
|
+
let dataList = new Array(rows);
|
|
126
|
+
if (!resp || !taosResult) {
|
|
127
|
+
return taosResult;
|
|
128
|
+
}
|
|
129
|
+
let metaList = taosResult.getTaosMeta();
|
|
130
|
+
let taosdata = taosResult.getData();
|
|
131
|
+
if (metaList && rows && taosdata) {
|
|
132
|
+
//get bitmap length
|
|
133
|
+
let bitMapOffset = getBitmapLen(rows);
|
|
134
|
+
//skip data head
|
|
135
|
+
let bufferOffset = 24 + 28 + 5 * metaList.length;
|
|
136
|
+
let dataBuffer = resp.blockData.slice(bufferOffset);
|
|
137
|
+
let metaLens = [];
|
|
138
|
+
for (let i = 0; i < metaList.length; i++) {
|
|
139
|
+
//get data len
|
|
140
|
+
metaLens.push(new DataView(dataBuffer, i * 4, 4).getInt32(0, true));
|
|
141
|
+
}
|
|
142
|
+
bufferOffset = metaList.length * 4;
|
|
143
|
+
for (let i = 0; i < metaList.length; i++) {
|
|
144
|
+
let data = [];
|
|
145
|
+
//get type code
|
|
146
|
+
let isVarType = (0, taosResult_1._isVarType)(metaList[i]);
|
|
147
|
+
//fixed length type
|
|
148
|
+
if (isVarType == constant_1.ColumnsBlockType.SOLID) {
|
|
149
|
+
let bitMapArr = dataBuffer.slice(bufferOffset, bufferOffset + bitMapOffset);
|
|
150
|
+
bufferOffset += bitMapOffset;
|
|
151
|
+
//decode column data, data is array
|
|
152
|
+
data = (0, taosResult_1.readSolidDataToArray)(dataBuffer, bufferOffset, rows, metaList[i], bitMapArr);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
//Variable length type
|
|
156
|
+
let offset = bufferOffset;
|
|
157
|
+
let offsets = [];
|
|
158
|
+
for (let i = 0; i < rows; i++, offset += constant_1.TDengineTypeLength['INT']) {
|
|
159
|
+
//get data length, -1 is null
|
|
160
|
+
offsets.push(new DataView(dataBuffer, offset, 4).getInt32(0, true));
|
|
161
|
+
}
|
|
162
|
+
let start = offset;
|
|
163
|
+
for (let i = 0; i < rows; i++) {
|
|
164
|
+
let value = '';
|
|
165
|
+
if (-1 == offsets[i]) {
|
|
166
|
+
value = null;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
let header = start + offsets[i];
|
|
170
|
+
let dataLength = new DataView(dataBuffer, header, 2).getInt16(0, true) & 0xFFFF;
|
|
171
|
+
if (isVarType == constant_1.ColumnsBlockType.VARCHAR) {
|
|
172
|
+
//decode var char
|
|
173
|
+
value = (0, taosResult_1.readVarchar)(dataBuffer, header + 2, dataLength);
|
|
174
|
+
}
|
|
175
|
+
else if (isVarType == constant_1.ColumnsBlockType.GEOMETRY || isVarType == constant_1.ColumnsBlockType.VARBINARY) {
|
|
176
|
+
//decode binary
|
|
177
|
+
value = (0, taosResult_1.readBinary)(dataBuffer, header + 2, dataLength);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
//decode nchar
|
|
181
|
+
value = (0, taosResult_1.readNchar)(dataBuffer, header + 2, dataLength);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
data.push(value);
|
|
185
|
+
}
|
|
186
|
+
bufferOffset += rows * 4;
|
|
187
|
+
}
|
|
188
|
+
bufferOffset += metaLens[i];
|
|
189
|
+
//column data to row data
|
|
190
|
+
for (let row = 0; row < data.length; row++) {
|
|
191
|
+
if (dataList[row] == null) {
|
|
192
|
+
dataList[row] = [];
|
|
193
|
+
}
|
|
194
|
+
dataList[row].push(data[row]);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
taosdata.push(...dataList);
|
|
198
|
+
}
|
|
199
|
+
return taosResult;
|
|
200
|
+
}
|
|
201
|
+
exports.parseTmqBlock = parseTmqBlock;
|
|
202
|
+
function getBitmapLen(n) {
|
|
203
|
+
return (n + 0x7) >> 3;
|
|
204
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { TaosResult } from '../common/taosResult';
|
|
2
|
+
import { TopicPartition } from './tmqResponse';
|
|
3
|
+
export declare class WsConsumer {
|
|
4
|
+
private _wsClient;
|
|
5
|
+
private _wsConfig;
|
|
6
|
+
private _topics?;
|
|
7
|
+
private _commitTime?;
|
|
8
|
+
private constructor();
|
|
9
|
+
private init;
|
|
10
|
+
static newConsumer(wsConfig: Map<string, any>): Promise<WsConsumer>;
|
|
11
|
+
subscribe(topics: Array<string>, reqId?: number): Promise<void>;
|
|
12
|
+
unsubscribe(reqId?: number): Promise<void>;
|
|
13
|
+
poll(timeoutMs: number, reqId?: number): Promise<Map<string, TaosResult>>;
|
|
14
|
+
subscription(reqId?: number): Promise<Array<string>>;
|
|
15
|
+
commit(reqId?: number): Promise<Array<TopicPartition>>;
|
|
16
|
+
private doCommit;
|
|
17
|
+
committed(partitions: Array<TopicPartition>, reqId?: number): Promise<Array<TopicPartition>>;
|
|
18
|
+
commitOffsets(partitions: Array<TopicPartition>): Promise<Array<TopicPartition>>;
|
|
19
|
+
commitOffset(partition: TopicPartition, reqId?: number): Promise<void>;
|
|
20
|
+
positions(partitions: Array<TopicPartition>, reqId?: number): Promise<Array<TopicPartition>>;
|
|
21
|
+
seek(partition: TopicPartition, reqId?: number): Promise<void>;
|
|
22
|
+
seekToBeginning(partitions: Array<TopicPartition>): Promise<void>;
|
|
23
|
+
seekToEnd(partitions: Array<TopicPartition>): Promise<void>;
|
|
24
|
+
close(): Promise<void>;
|
|
25
|
+
private fetch;
|
|
26
|
+
private fetchBlockData;
|
|
27
|
+
private pollData;
|
|
28
|
+
private sendAssignmentReq;
|
|
29
|
+
assignment(topics?: string[]): Promise<Array<TopicPartition>>;
|
|
30
|
+
private seekToBeginOrEnd;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=wsTmq.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wsTmq.d.ts","sourceRoot":"","sources":["../../../src/tmq/wsTmq.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAElD,OAAO,EAAkF,cAAc,EAA6E,MAAM,eAAe,CAAC;AAI1M,qBAAa,UAAU;IACnB,OAAO,CAAC,SAAS,CAAW;IAC5B,OAAO,CAAC,SAAS,CAAW;IAC5B,OAAO,CAAC,OAAO,CAAC,CAAU;IAC1B,OAAO,CAAC,WAAW,CAAC,CAAQ;IAC5B,OAAO;YAMO,IAAI;WAML,WAAW,CAAC,QAAQ,EAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,GAAE,OAAO,CAAC,UAAU,CAAC;IAUjE,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC,EAAC,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAwB9D,WAAW,CAAC,KAAK,CAAC,EAAC,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAUzC,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,CAAC,EAAC,MAAM,GAAE,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IAkBvE,YAAY,CAAC,KAAK,CAAC,EAAC,MAAM,GAAE,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAYlD,MAAM,CAAC,KAAK,CAAC,EAAC,MAAM,GAAE,OAAO,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;YAK5C,QAAQ;IAYhB,SAAS,CAAC,UAAU,EAAC,KAAK,CAAC,cAAc,CAAC,EAAE,KAAK,CAAC,EAAC,MAAM,GAAE,OAAO,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;IA0BzF,aAAa,CAAC,UAAU,EAAC,KAAK,CAAC,cAAc,CAAC,GAAE,OAAO,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;IAc9E,YAAY,CAAC,SAAS,EAAC,cAAc,EAAE,KAAK,CAAC,EAAC,MAAM,GAAE,OAAO,CAAC,IAAI,CAAC;IAkBnE,SAAS,CAAC,UAAU,EAAC,KAAK,CAAC,cAAc,CAAC,EAAE,KAAK,CAAC,EAAC,MAAM,GAAE,OAAO,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;IA0BzF,IAAI,CAAC,SAAS,EAAC,cAAc,EAAE,KAAK,CAAC,EAAC,MAAM,GAAE,OAAO,CAAC,IAAI,CAAC;IAkB3D,eAAe,CAAC,UAAU,EAAC,KAAK,CAAC,cAAc,CAAC,GAAE,OAAO,CAAC,IAAI,CAAC;IAQ/D,SAAS,CAAC,UAAU,EAAC,KAAK,CAAC,cAAc,CAAC,GAAE,OAAO,CAAC,IAAI,CAAC;IASzD,KAAK,IAAG,OAAO,CAAC,IAAI,CAAC;YAIb,KAAK;YAcL,cAAc;YAed,QAAQ;YAkCR,iBAAiB;IAczB,UAAU,CAAC,MAAM,CAAC,EAAC,MAAM,EAAE,GAAE,OAAO,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;YAoBnD,gBAAgB;CA6BjC"}
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.WsConsumer = void 0;
|
|
7
|
+
const config_1 = require("./config");
|
|
8
|
+
const constant_1 = require("./constant");
|
|
9
|
+
const wsClient_1 = require("../client/wsClient");
|
|
10
|
+
const wsError_1 = require("../common/wsError");
|
|
11
|
+
const tmqResponse_1 = require("./tmqResponse");
|
|
12
|
+
const reqid_1 = require("../common/reqid");
|
|
13
|
+
const log_1 = __importDefault(require("../common/log"));
|
|
14
|
+
class WsConsumer {
|
|
15
|
+
constructor(wsConfig) {
|
|
16
|
+
this._wsConfig = new config_1.TmqConfig(wsConfig);
|
|
17
|
+
log_1.default.debug(this._wsConfig);
|
|
18
|
+
this._wsClient = new wsClient_1.WsClient(this._wsConfig.url, this._wsConfig.timeout);
|
|
19
|
+
}
|
|
20
|
+
async init() {
|
|
21
|
+
await this._wsClient.ready();
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
static async newConsumer(wsConfig) {
|
|
25
|
+
if (wsConfig.size == 0 || !wsConfig.get(constant_1.TMQConstants.WS_URL)) {
|
|
26
|
+
throw new wsError_1.WebSocketInterfaceError(wsError_1.ErrorCode.ERR_INVALID_URL, 'invalid url, password or username needed.');
|
|
27
|
+
}
|
|
28
|
+
let wsConsumer = new WsConsumer(wsConfig);
|
|
29
|
+
return await wsConsumer.init();
|
|
30
|
+
}
|
|
31
|
+
async subscribe(topics, reqId) {
|
|
32
|
+
if (!topics || topics.length == 0) {
|
|
33
|
+
throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq Subscribe params is error!');
|
|
34
|
+
}
|
|
35
|
+
let queryMsg = {
|
|
36
|
+
action: constant_1.TMQMessageType.Subscribe,
|
|
37
|
+
args: {
|
|
38
|
+
req_id: reqid_1.ReqId.getReqID(reqId),
|
|
39
|
+
user: this._wsConfig.user,
|
|
40
|
+
password: this._wsConfig.password,
|
|
41
|
+
group_id: this._wsConfig.group_id,
|
|
42
|
+
client_id: this._wsConfig.client_id,
|
|
43
|
+
topics: topics,
|
|
44
|
+
offset_rest: this._wsConfig.offset_rest,
|
|
45
|
+
auto_commit: this._wsConfig.auto_commit,
|
|
46
|
+
auto_commit_interval_ms: this._wsConfig.auto_commit_interval_ms
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
this._topics = topics;
|
|
50
|
+
return await this._wsClient.exec(JSON.stringify(queryMsg));
|
|
51
|
+
}
|
|
52
|
+
async unsubscribe(reqId) {
|
|
53
|
+
let queryMsg = {
|
|
54
|
+
action: constant_1.TMQMessageType.Unsubscribe,
|
|
55
|
+
args: {
|
|
56
|
+
req_id: reqid_1.ReqId.getReqID(reqId),
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
return await this._wsClient.exec(JSON.stringify(queryMsg));
|
|
60
|
+
}
|
|
61
|
+
async poll(timeoutMs, reqId) {
|
|
62
|
+
if (this._wsConfig.auto_commit) {
|
|
63
|
+
if (this._commitTime) {
|
|
64
|
+
let currTime = new Date().getTime();
|
|
65
|
+
let diff = Math.abs(currTime - this._commitTime);
|
|
66
|
+
if (diff >= this._wsConfig.auto_commit_interval_ms) {
|
|
67
|
+
await this.doCommit();
|
|
68
|
+
this._commitTime = new Date().getTime();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
this._commitTime = new Date().getTime();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return await this.pollData(timeoutMs, reqId);
|
|
76
|
+
}
|
|
77
|
+
async subscription(reqId) {
|
|
78
|
+
let queryMsg = {
|
|
79
|
+
action: constant_1.TMQMessageType.ListTopics,
|
|
80
|
+
args: {
|
|
81
|
+
req_id: reqid_1.ReqId.getReqID(reqId),
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
let resp = await this._wsClient.exec(JSON.stringify(queryMsg), false);
|
|
85
|
+
return new tmqResponse_1.SubscriptionResp(resp).topics;
|
|
86
|
+
}
|
|
87
|
+
async commit(reqId) {
|
|
88
|
+
await this.doCommit(reqId);
|
|
89
|
+
return await this.assignment();
|
|
90
|
+
}
|
|
91
|
+
async doCommit(reqId) {
|
|
92
|
+
let queryMsg = {
|
|
93
|
+
action: constant_1.TMQMessageType.Commit,
|
|
94
|
+
args: {
|
|
95
|
+
req_id: reqid_1.ReqId.getReqID(reqId),
|
|
96
|
+
message_id: 0
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
await this._wsClient.exec(JSON.stringify(queryMsg));
|
|
100
|
+
}
|
|
101
|
+
async committed(partitions, reqId) {
|
|
102
|
+
if (!partitions || partitions.length == 0) {
|
|
103
|
+
throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq Positions params is error!');
|
|
104
|
+
}
|
|
105
|
+
let offsets = new Array(partitions.length);
|
|
106
|
+
for (let i = 0; i < partitions.length; i++) {
|
|
107
|
+
offsets[i] = {
|
|
108
|
+
topic: partitions[i].topic,
|
|
109
|
+
vgroup_id: partitions[i].vgroup_id
|
|
110
|
+
};
|
|
111
|
+
offsets[i].vgroup_id = partitions[i].vgroup_id;
|
|
112
|
+
}
|
|
113
|
+
let queryMsg = {
|
|
114
|
+
action: constant_1.TMQMessageType.Committed,
|
|
115
|
+
args: {
|
|
116
|
+
req_id: reqid_1.ReqId.getReqID(reqId),
|
|
117
|
+
topic_vgroup_ids: offsets
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
let resp = await this._wsClient.exec(JSON.stringify(queryMsg), false);
|
|
121
|
+
return new tmqResponse_1.CommittedResp(resp).setTopicPartitions(offsets);
|
|
122
|
+
}
|
|
123
|
+
async commitOffsets(partitions) {
|
|
124
|
+
if (!partitions || partitions.length == 0) {
|
|
125
|
+
throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq CommitOffsets params is error!');
|
|
126
|
+
}
|
|
127
|
+
const allp = [];
|
|
128
|
+
partitions.forEach(e => {
|
|
129
|
+
allp.push(this.commitOffset(e));
|
|
130
|
+
});
|
|
131
|
+
await Promise.all(allp);
|
|
132
|
+
return await this.committed(partitions);
|
|
133
|
+
}
|
|
134
|
+
async commitOffset(partition, reqId) {
|
|
135
|
+
if (!partition) {
|
|
136
|
+
throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq CommitOffsets params is error!');
|
|
137
|
+
}
|
|
138
|
+
let queryMsg = {
|
|
139
|
+
action: constant_1.TMQMessageType.CommitOffset,
|
|
140
|
+
args: {
|
|
141
|
+
req_id: reqid_1.ReqId.getReqID(reqId),
|
|
142
|
+
vgroup_id: partition.vgroup_id,
|
|
143
|
+
topic: partition.topic,
|
|
144
|
+
offset: partition.offset,
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
return await this._wsClient.exec(JSON.stringify(queryMsg));
|
|
148
|
+
}
|
|
149
|
+
async positions(partitions, reqId) {
|
|
150
|
+
if (!partitions || partitions.length == 0) {
|
|
151
|
+
throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq Positions params is error!');
|
|
152
|
+
}
|
|
153
|
+
let offsets = new Array(partitions.length);
|
|
154
|
+
for (let i = 0; i < partitions.length; i++) {
|
|
155
|
+
offsets[i] = {
|
|
156
|
+
topic: partitions[i].topic,
|
|
157
|
+
vgroup_id: partitions[i].vgroup_id
|
|
158
|
+
};
|
|
159
|
+
offsets[i].vgroup_id = partitions[i].vgroup_id;
|
|
160
|
+
}
|
|
161
|
+
let queryMsg = {
|
|
162
|
+
action: constant_1.TMQMessageType.Position,
|
|
163
|
+
args: {
|
|
164
|
+
req_id: reqid_1.ReqId.getReqID(reqId),
|
|
165
|
+
topic_vgroup_ids: offsets
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
let resp = await this._wsClient.exec(JSON.stringify(queryMsg), false);
|
|
169
|
+
return new tmqResponse_1.PartitionsResp(resp).setTopicPartitions(offsets);
|
|
170
|
+
}
|
|
171
|
+
async seek(partition, reqId) {
|
|
172
|
+
if (!partition) {
|
|
173
|
+
throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq Seek params is error!');
|
|
174
|
+
}
|
|
175
|
+
let queryMsg = {
|
|
176
|
+
action: constant_1.TMQMessageType.Seek,
|
|
177
|
+
args: {
|
|
178
|
+
req_id: reqid_1.ReqId.getReqID(reqId),
|
|
179
|
+
vgroup_id: partition.vgroup_id,
|
|
180
|
+
topic: partition.topic,
|
|
181
|
+
offset: partition.offset,
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
return await this._wsClient.exec(JSON.stringify(queryMsg));
|
|
185
|
+
}
|
|
186
|
+
async seekToBeginning(partitions) {
|
|
187
|
+
if (!partitions || partitions.length == 0) {
|
|
188
|
+
throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq SeekToBeginning params is error!');
|
|
189
|
+
}
|
|
190
|
+
return await this.seekToBeginOrEnd(partitions);
|
|
191
|
+
}
|
|
192
|
+
async seekToEnd(partitions) {
|
|
193
|
+
if (!partitions || partitions.length == 0) {
|
|
194
|
+
throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq SeekToEnd params is error!');
|
|
195
|
+
}
|
|
196
|
+
return await this.seekToBeginOrEnd(partitions, false);
|
|
197
|
+
}
|
|
198
|
+
async close() {
|
|
199
|
+
await this._wsClient.close();
|
|
200
|
+
}
|
|
201
|
+
async fetch(pollResp) {
|
|
202
|
+
let fetchMsg = {
|
|
203
|
+
action: 'fetch',
|
|
204
|
+
args: {
|
|
205
|
+
req_id: reqid_1.ReqId.getReqID(),
|
|
206
|
+
message_id: pollResp.message_id,
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
let jsonStr = JSON.stringify(fetchMsg);
|
|
210
|
+
log_1.default.debug('[wsQueryInterface.fetch.fetchMsg]===>' + jsonStr);
|
|
211
|
+
let result = await this._wsClient.exec(jsonStr, false);
|
|
212
|
+
return new tmqResponse_1.WsTmqQueryResponse(result);
|
|
213
|
+
}
|
|
214
|
+
async fetchBlockData(fetchResponse, taosResult) {
|
|
215
|
+
let fetchMsg = {
|
|
216
|
+
action: 'fetch_block',
|
|
217
|
+
args: {
|
|
218
|
+
req_id: fetchResponse.req_id,
|
|
219
|
+
message_id: fetchResponse.message_id,
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
let jsonStr = JSON.stringify(fetchMsg);
|
|
223
|
+
log_1.default.debug('[wsQueryInterface.fetch.fetchMsg]===>' + jsonStr);
|
|
224
|
+
let result = await this._wsClient.sendMsg(jsonStr);
|
|
225
|
+
(0, tmqResponse_1.parseTmqBlock)(fetchResponse.rows, new tmqResponse_1.WSTmqFetchBlockResponse(result), taosResult);
|
|
226
|
+
return taosResult;
|
|
227
|
+
}
|
|
228
|
+
async pollData(timeoutMs, reqId) {
|
|
229
|
+
let queryMsg = {
|
|
230
|
+
action: constant_1.TMQMessageType.Poll,
|
|
231
|
+
args: {
|
|
232
|
+
req_id: reqid_1.ReqId.getReqID(reqId),
|
|
233
|
+
blocking_time: timeoutMs
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
var taosResults = new Map();
|
|
237
|
+
let resp = await this._wsClient.exec(JSON.stringify(queryMsg), false);
|
|
238
|
+
let pollResp = new tmqResponse_1.WsPollResponse(resp);
|
|
239
|
+
if (!pollResp.have_message || pollResp.message_type != constant_1.TMQMessageType.ResDataType) {
|
|
240
|
+
return taosResults;
|
|
241
|
+
}
|
|
242
|
+
while (true) {
|
|
243
|
+
let fetchResp = await this.fetch(pollResp);
|
|
244
|
+
if (fetchResp.completed || fetchResp.rows == 0) {
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
let taosResult = taosResults.get(pollResp.topic + pollResp.vgroup_id);
|
|
248
|
+
if (taosResult == null) {
|
|
249
|
+
taosResult = new tmqResponse_1.TaosTmqResult(fetchResp, pollResp);
|
|
250
|
+
taosResults.set(pollResp.topic + pollResp.vgroup_id, taosResult);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
taosResult.setRowsAndTime(fetchResp.rows);
|
|
254
|
+
}
|
|
255
|
+
await this.fetchBlockData(fetchResp, taosResult);
|
|
256
|
+
}
|
|
257
|
+
return taosResults;
|
|
258
|
+
}
|
|
259
|
+
async sendAssignmentReq(topic) {
|
|
260
|
+
let queryMsg = {
|
|
261
|
+
action: constant_1.TMQMessageType.GetTopicAssignment,
|
|
262
|
+
args: {
|
|
263
|
+
req_id: reqid_1.ReqId.getReqID(),
|
|
264
|
+
topic: topic
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
let resp = await this._wsClient.exec(JSON.stringify(queryMsg), false);
|
|
268
|
+
let assignmentInfo = new tmqResponse_1.AssignmentResp(resp, queryMsg.args.topic);
|
|
269
|
+
return assignmentInfo.topicPartition;
|
|
270
|
+
}
|
|
271
|
+
async assignment(topics) {
|
|
272
|
+
if (!topics || topics.length == 0) {
|
|
273
|
+
topics = this._topics;
|
|
274
|
+
}
|
|
275
|
+
let topicPartitions = [];
|
|
276
|
+
if (topics && topics.length > 0) {
|
|
277
|
+
const allp = [];
|
|
278
|
+
for (let i in topics) {
|
|
279
|
+
allp.push(this.sendAssignmentReq(topics[i]));
|
|
280
|
+
}
|
|
281
|
+
let result = await Promise.all(allp);
|
|
282
|
+
result.forEach(e => {
|
|
283
|
+
topicPartitions.push(...e);
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
return topicPartitions;
|
|
287
|
+
}
|
|
288
|
+
async seekToBeginOrEnd(partitions, bBegin = true) {
|
|
289
|
+
let topics = [];
|
|
290
|
+
partitions.forEach(e => {
|
|
291
|
+
topics.push(e.topic);
|
|
292
|
+
});
|
|
293
|
+
let topicPartitions = await this.assignment(topics);
|
|
294
|
+
let itemMap = topicPartitions.reduce((map, obj) => {
|
|
295
|
+
map.set(obj.topic + '_' + obj.vgroup_id, obj);
|
|
296
|
+
return map;
|
|
297
|
+
}, new Map());
|
|
298
|
+
const allp = [];
|
|
299
|
+
for (let i in partitions) {
|
|
300
|
+
if (itemMap.has(partitions[i].topic + '_' + partitions[i].vgroup_id)) {
|
|
301
|
+
let topicPartition = itemMap.get(partitions[i].topic + '_' + partitions[i].vgroup_id);
|
|
302
|
+
if (topicPartition) {
|
|
303
|
+
if (bBegin) {
|
|
304
|
+
topicPartition.offset = topicPartition.begin;
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
topicPartition.offset = topicPartition.end;
|
|
308
|
+
}
|
|
309
|
+
allp.push(this.seek(topicPartition));
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
await Promise.all(allp);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
exports.WsConsumer = WsConsumer;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queryTables.test.d.ts","sourceRoot":"","sources":["../../../test/bulkPulling/queryTables.test.ts"],"names":[],"mappings":""}
|