@tdengine/websocket 3.0.0 → 3.1.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 (157) hide show
  1. package/lib/example/all_type_query.d.ts +2 -0
  2. package/lib/example/all_type_query.d.ts.map +1 -0
  3. package/lib/example/all_type_query.js +89 -0
  4. package/lib/example/all_type_stmt.d.ts +2 -0
  5. package/lib/example/all_type_stmt.d.ts.map +1 -0
  6. package/lib/example/all_type_stmt.js +130 -0
  7. package/lib/example/basicBatchTmq.d.ts +2 -0
  8. package/lib/example/basicBatchTmq.d.ts.map +1 -0
  9. package/lib/example/basicBatchTmq.js +129 -0
  10. package/lib/example/basicSchemaless.d.ts +2 -0
  11. package/lib/example/basicSchemaless.d.ts.map +1 -0
  12. package/lib/example/basicSchemaless.js +47 -0
  13. package/lib/example/basicSql.d.ts +2 -0
  14. package/lib/example/basicSql.d.ts.map +1 -0
  15. package/lib/example/basicSql.js +54 -0
  16. package/lib/example/basicStmt.d.ts +2 -0
  17. package/lib/example/basicStmt.d.ts.map +1 -0
  18. package/lib/example/basicStmt.js +72 -0
  19. package/lib/example/basicTmq.d.ts +2 -0
  20. package/lib/example/basicTmq.d.ts.map +1 -0
  21. package/lib/example/basicTmq.js +73 -0
  22. package/lib/index.d.ts +26 -0
  23. package/lib/index.d.ts.map +1 -0
  24. package/lib/index.js +41 -0
  25. package/lib/src/client/wsClient.d.ts +22 -0
  26. package/lib/src/client/wsClient.d.ts.map +1 -0
  27. package/lib/src/client/wsClient.js +256 -0
  28. package/lib/src/client/wsConnector.d.ts +17 -0
  29. package/lib/src/client/wsConnector.d.ts.map +1 -0
  30. package/lib/src/client/wsConnector.js +140 -0
  31. package/lib/src/client/wsConnectorPool.d.ts +13 -0
  32. package/lib/src/client/wsConnectorPool.d.ts.map +1 -0
  33. package/lib/src/client/wsConnectorPool.js +107 -0
  34. package/lib/src/client/wsEventCallback.d.ts +22 -0
  35. package/lib/src/client/wsEventCallback.d.ts.map +1 -0
  36. package/lib/src/client/wsEventCallback.js +97 -0
  37. package/lib/src/client/wsResponse.d.ts +74 -0
  38. package/lib/src/client/wsResponse.d.ts.map +1 -0
  39. package/lib/src/client/wsResponse.js +103 -0
  40. package/lib/src/common/config.d.ts +22 -0
  41. package/lib/src/common/config.d.ts.map +1 -0
  42. package/lib/src/common/config.js +45 -0
  43. package/lib/src/common/constant.d.ts +35 -0
  44. package/lib/src/common/constant.d.ts.map +1 -0
  45. package/lib/src/common/constant.js +78 -0
  46. package/lib/src/common/log.d.ts +5 -0
  47. package/lib/src/common/log.d.ts.map +1 -0
  48. package/lib/src/common/log.js +40 -0
  49. package/lib/src/common/reqid.d.ts +8 -0
  50. package/lib/src/common/reqid.d.ts.map +1 -0
  51. package/lib/src/common/reqid.js +59 -0
  52. package/lib/src/common/taosResult.d.ts +57 -0
  53. package/lib/src/common/taosResult.d.ts.map +1 -0
  54. package/lib/src/common/taosResult.js +449 -0
  55. package/lib/src/common/ut8Helper.d.ts +2 -0
  56. package/lib/src/common/ut8Helper.d.ts.map +1 -0
  57. package/{src/ut8Helper.ts → lib/src/common/ut8Helper.js} +22 -21
  58. package/lib/src/common/utils.d.ts +7 -0
  59. package/lib/src/common/utils.d.ts.map +1 -0
  60. package/lib/src/common/utils.js +66 -0
  61. package/lib/src/common/wsError.d.ts +29 -0
  62. package/lib/src/common/wsError.d.ts.map +1 -0
  63. package/lib/src/common/wsError.js +51 -0
  64. package/{src/wsOptions.ts → lib/src/common/wsOptions.d.ts} +4 -2
  65. package/lib/src/common/wsOptions.d.ts.map +1 -0
  66. package/lib/src/common/wsOptions.js +2 -0
  67. package/lib/src/index.d.ts +9 -0
  68. package/lib/src/index.d.ts.map +1 -0
  69. package/lib/src/index.js +58 -0
  70. package/lib/src/sql/wsProto.d.ts +26 -0
  71. package/lib/src/sql/wsProto.d.ts.map +1 -0
  72. package/lib/src/sql/wsProto.js +19 -0
  73. package/lib/src/sql/wsRows.d.ts +16 -0
  74. package/lib/src/sql/wsRows.d.ts.map +1 -0
  75. package/lib/src/sql/wsRows.js +87 -0
  76. package/lib/src/sql/wsSql.d.ts +24 -0
  77. package/lib/src/sql/wsSql.d.ts.map +1 -0
  78. package/lib/src/sql/wsSql.js +174 -0
  79. package/lib/src/stmt/wsParams.d.ts +41 -0
  80. package/lib/src/stmt/wsParams.d.ts.map +1 -0
  81. package/lib/src/stmt/wsParams.js +428 -0
  82. package/lib/src/stmt/wsProto.d.ts +27 -0
  83. package/lib/src/stmt/wsProto.d.ts.map +1 -0
  84. package/lib/src/stmt/wsProto.js +67 -0
  85. package/lib/src/stmt/wsStmt.d.ts +30 -0
  86. package/lib/src/stmt/wsStmt.d.ts.map +1 -0
  87. package/lib/src/stmt/wsStmt.js +207 -0
  88. package/lib/src/tmq/config.d.ts +14 -0
  89. package/lib/src/tmq/config.d.ts.map +1 -0
  90. package/lib/src/tmq/config.js +42 -0
  91. package/lib/src/tmq/constant.d.ts +100 -0
  92. package/lib/src/tmq/constant.d.ts.map +1 -0
  93. package/lib/src/tmq/constant.js +105 -0
  94. package/lib/src/tmq/tmqResponse.d.ts +92 -0
  95. package/lib/src/tmq/tmqResponse.d.ts.map +1 -0
  96. package/lib/src/tmq/tmqResponse.js +329 -0
  97. package/lib/src/tmq/wsTmq.d.ts +32 -0
  98. package/lib/src/tmq/wsTmq.d.ts.map +1 -0
  99. package/lib/src/tmq/wsTmq.js +319 -0
  100. package/lib/test/bulkPulling/log.test.d.ts +2 -0
  101. package/lib/test/bulkPulling/log.test.d.ts.map +1 -0
  102. package/lib/test/bulkPulling/log.test.js +44 -0
  103. package/lib/test/bulkPulling/queryTables.test.d.ts +2 -0
  104. package/lib/test/bulkPulling/queryTables.test.d.ts.map +1 -0
  105. package/lib/test/bulkPulling/queryTables.test.js +297 -0
  106. package/lib/test/bulkPulling/schemaless.test.d.ts +2 -0
  107. package/lib/test/bulkPulling/schemaless.test.d.ts.map +1 -0
  108. package/lib/test/bulkPulling/schemaless.test.js +95 -0
  109. package/lib/test/bulkPulling/sql.test.d.ts +2 -0
  110. package/lib/test/bulkPulling/sql.test.d.ts.map +1 -0
  111. package/lib/test/bulkPulling/sql.test.js +144 -0
  112. package/lib/test/bulkPulling/stmt.func.test.d.ts +2 -0
  113. package/lib/test/bulkPulling/stmt.func.test.d.ts.map +1 -0
  114. package/lib/test/bulkPulling/stmt.func.test.js +397 -0
  115. package/lib/test/bulkPulling/stmt.type.test.d.ts +2 -0
  116. package/lib/test/bulkPulling/stmt.type.test.d.ts.map +1 -0
  117. package/lib/test/bulkPulling/stmt.type.test.js +269 -0
  118. package/lib/test/bulkPulling/tmq.test.d.ts +2 -0
  119. package/lib/test/bulkPulling/tmq.test.d.ts.map +1 -0
  120. package/lib/test/bulkPulling/tmq.test.js +178 -0
  121. package/lib/test/bulkPulling/wsConnectPool.test.d.ts +2 -0
  122. package/lib/test/bulkPulling/wsConnectPool.test.d.ts.map +1 -0
  123. package/lib/test/bulkPulling/wsConnectPool.test.js +136 -0
  124. package/lib/test/utils.d.ts +18 -0
  125. package/lib/test/utils.d.ts.map +1 -0
  126. package/lib/test/utils.js +318 -0
  127. package/package.json +27 -14
  128. package/readme.md +302 -0
  129. package/README.md +0 -59
  130. package/dist/browser/index.js +0 -778
  131. package/dist/main/index.js +0 -711
  132. package/dist/main/index.js.map +0 -1
  133. package/dist/module/index.mjs +0 -704
  134. package/dist/module/index.mjs.map +0 -1
  135. package/dist/types.d.ts +0 -103
  136. package/dist/types.d.ts.map +0 -1
  137. package/example/basicUsageAsync.ts +0 -48
  138. package/example/basicUsagePrimse.ts +0 -43
  139. package/example/cloudUsage.ts +0 -55
  140. package/example/continousConnectAndVersion.ts +0 -16
  141. package/example/test.mjs +0 -51
  142. package/index.ts +0 -7
  143. package/jest.config.js +0 -8
  144. package/src/constant.ts +0 -74
  145. package/src/taosResult.ts +0 -269
  146. package/src/tdengineWebsocket.ts +0 -39
  147. package/src/wsClient.ts +0 -196
  148. package/src/wsError.ts +0 -5
  149. package/src/wsQuery.ts +0 -30
  150. package/src/wsQueryInterface.ts +0 -212
  151. package/src/wsQueryResponse.ts +0 -112
  152. package/tdengine-websocket-3.0.0.tgz +0 -0
  153. package/test/bulkPulling/connect.test.ts +0 -27
  154. package/test/bulkPulling/queryTables.test.ts +0 -274
  155. package/test/bulkPulling/version.test.ts +0 -19
  156. package/test/utils.ts +0 -235
  157. package/tsconfig.json +0 -101
@@ -0,0 +1,329 @@
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.TopicPartition = exports.CommittedResp = exports.PartitionsResp = exports.SubscriptionResp = exports.AssignmentResp = exports.WSTmqFetchBlockInfo = exports.TaosTmqResult = exports.WsTmqQueryResponse = exports.WsPollResponse = void 0;
7
+ const wsResponse_1 = require("../client/wsResponse");
8
+ const constant_1 = require("../common/constant");
9
+ const taosResult_1 = require("../common/taosResult");
10
+ const wsError_1 = require("../common/wsError");
11
+ const constant_2 = require("./constant");
12
+ const utils_1 = require("../common/utils");
13
+ const log_1 = __importDefault(require("../common/log"));
14
+ class WsPollResponse {
15
+ constructor(resp) {
16
+ this.totalTime = resp.totalTime;
17
+ this.code = resp.msg.code;
18
+ this.message = resp.msg.message;
19
+ this.action = resp.msg.action;
20
+ this.req_id = resp.msg.req_id;
21
+ this.have_message = resp.msg.have_message;
22
+ this.topic = resp.msg.topic;
23
+ this.database = resp.msg.database;
24
+ this.vgroup_id = resp.msg.vgroup_id;
25
+ this.message_id = resp.msg.message_id;
26
+ this.message_type = resp.msg.message_type;
27
+ if (resp.msg.id) {
28
+ this.id = BigInt(resp.msg.id);
29
+ }
30
+ else {
31
+ this.id = BigInt(0);
32
+ }
33
+ }
34
+ }
35
+ exports.WsPollResponse = WsPollResponse;
36
+ // 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}
37
+ class WsTmqQueryResponse extends wsResponse_1.WSQueryResponse {
38
+ constructor(resp) {
39
+ super(resp);
40
+ this.completed = resp.msg.completed;
41
+ this.table_name = resp.msg.table_name;
42
+ this.rows = resp.msg.rows;
43
+ this.message_id = resp.msg.message_id;
44
+ }
45
+ }
46
+ exports.WsTmqQueryResponse = WsTmqQueryResponse;
47
+ class TaosTmqResult extends taosResult_1.TaosResult {
48
+ constructor(pollResp) {
49
+ super();
50
+ this.setTopic(pollResp.topic);
51
+ this.database = pollResp.database;
52
+ this.vgroup_id = pollResp.vgroup_id;
53
+ }
54
+ }
55
+ exports.TaosTmqResult = TaosTmqResult;
56
+ class WSTmqFetchBlockInfo {
57
+ constructor(dataView, taosResult) {
58
+ // this.totalTime = resp.totalTime
59
+ // this.blockData = resp.msg
60
+ this.textDecoder = new TextDecoder();
61
+ this.taosResult = taosResult;
62
+ this.schema = [];
63
+ this.schemaLen = 0;
64
+ let blockDataView = this.skipHead(dataView);
65
+ this.rows = this.parseBlockInfos(blockDataView);
66
+ }
67
+ getRows() {
68
+ return this.rows;
69
+ }
70
+ skipHead(dataView) {
71
+ let v = dataView.getUint8(0);
72
+ if (v >= 100) {
73
+ let skip = dataView.getUint32(1, true);
74
+ return new DataView(dataView.buffer, dataView.byteOffset + skip + 5);
75
+ }
76
+ let skip1 = this.getTypeSkip(v);
77
+ v = dataView.getUint8(1 + skip1);
78
+ let skip2 = this.getTypeSkip(v);
79
+ return new DataView(dataView.buffer, dataView.byteOffset + skip1 + 2 + skip2);
80
+ }
81
+ getTypeSkip(v) {
82
+ switch (v) {
83
+ case 1:
84
+ return 8;
85
+ case 2:
86
+ case 3:
87
+ return 16;
88
+ default:
89
+ throw (new wsError_1.TDWebSocketClientError(wsError_1.ErrorCode.ERR_INVALID_FETCH_MESSAGE_DATA, `FetchBlockRawResp getTypeSkip error, type: ${v}`));
90
+ }
91
+ }
92
+ parseBlockInfos(dataView) {
93
+ let blockNum = dataView.getUint32(0, true);
94
+ if (blockNum == 0) {
95
+ return 0;
96
+ }
97
+ this.withTableName = dataView.getUint8(4) == 1 ? true : false;
98
+ this.withSchema = dataView.getUint8(5) == 1 ? true : false;
99
+ // let dataBuffer = dataView.buffer.slice(6)
100
+ let dataBuffer = new DataView(dataView.buffer, dataView.byteOffset + 6);
101
+ let rows = 0;
102
+ // const parseStartTime = new Date().getTime();
103
+ // console.log("parseBlockInfos blockNum="+ blockNum)
104
+ for (let i = 0; i < blockNum; i++) {
105
+ let variableInfo = this.parseVariableByteInteger(dataBuffer);
106
+ this.taosResult.setPrecision(variableInfo[1].getUint8(17));
107
+ dataView = new DataView(variableInfo[1].buffer, variableInfo[1].byteOffset + 17);
108
+ let offset = variableInfo[0] - 17;
109
+ dataBuffer = this.parseSchemaInfo(dataView, offset);
110
+ rows += this.parseTmqBlock(dataView, 1);
111
+ }
112
+ // const parseEndTime = new Date().getTime();
113
+ // console.log("------------->", parseEndTime- parseStartTime, rows);
114
+ log_1.default.info("parseBlockInfos blockNum=" + blockNum + ", withTableName=" + this.withTableName + ", withSchema=" + this.withSchema + ", rows=" + rows);
115
+ return rows;
116
+ }
117
+ parseSchemaInfo(dataBuffer, offset) {
118
+ if (this.withSchema) {
119
+ let isSkip = this.schema.length > 0;
120
+ if (!isSkip) {
121
+ dataBuffer = new DataView(dataBuffer.buffer, dataBuffer.byteOffset + offset);
122
+ let variableInfo = this.parseVariableByteInteger(dataBuffer);
123
+ this.schemaLen = variableInfo[2];
124
+ let cols = (0, utils_1.zigzagDecode)(variableInfo[0]);
125
+ variableInfo = this.parseVariableByteInteger(variableInfo[1]);
126
+ this.schemaLen += variableInfo[2];
127
+ let dataView = variableInfo[1];
128
+ for (let index = 0; index < cols; index++) {
129
+ let schema = new constant_2.TMQRawDataSchema();
130
+ schema.colType = dataView.getInt8(0);
131
+ schema.flag = dataView.getInt8(1);
132
+ variableInfo = this.parseVariableByteInteger(dataView, 2);
133
+ this.schemaLen += 2 + variableInfo[2];
134
+ schema.bytes = BigInt((0, utils_1.zigzagDecode)(variableInfo[0]));
135
+ variableInfo = this.parseVariableByteInteger(variableInfo[1]);
136
+ this.schemaLen += variableInfo[2];
137
+ schema.colID = (0, utils_1.zigzagDecode)(variableInfo[0]);
138
+ variableInfo = this.parseVariableByteInteger(variableInfo[1]);
139
+ this.schemaLen += variableInfo[2];
140
+ schema.name = (0, taosResult_1.getString)(variableInfo[1], 0, variableInfo[0], this.textDecoder);
141
+ if (!isSkip) {
142
+ this.taosResult.setMeta({
143
+ name: schema.name,
144
+ type: schema.colType,
145
+ length: Number(schema.bytes)
146
+ });
147
+ this.schema.push(schema);
148
+ }
149
+ dataView = new DataView(variableInfo[1].buffer, variableInfo[1].byteOffset + variableInfo[0]);
150
+ this.schemaLen += variableInfo[0];
151
+ }
152
+ if (this.withTableName) {
153
+ variableInfo = this.parseVariableByteInteger(dataView);
154
+ this.schemaLen += variableInfo[2];
155
+ this.tableName = (0, taosResult_1.readVarchar)(variableInfo[1].buffer, variableInfo[1].byteOffset, variableInfo[0], this.textDecoder);
156
+ dataView = new DataView(variableInfo[1].buffer, variableInfo[1].byteOffset + variableInfo[0]);
157
+ this.schemaLen += variableInfo[0];
158
+ }
159
+ return dataView;
160
+ }
161
+ else {
162
+ return new DataView(dataBuffer.buffer, dataBuffer.byteOffset + this.schemaLen + offset);
163
+ }
164
+ }
165
+ return dataBuffer;
166
+ }
167
+ parseVariableByteInteger(dataView, offset = 0) {
168
+ let value = 0;
169
+ let multiplier = 1;
170
+ let count = 0;
171
+ while (true) {
172
+ let encodedByte = dataView.getUint8(count + offset);
173
+ value += (encodedByte & 127) * multiplier;
174
+ if ((encodedByte & 128) == 0) {
175
+ break;
176
+ }
177
+ multiplier *= 128;
178
+ count++;
179
+ }
180
+ return [value, new DataView(dataView.buffer, dataView.byteOffset + count + 1 + offset), count + 1];
181
+ }
182
+ parseTmqBlock(dataView, startOffset) {
183
+ // let dataView = new DataView(dataBuffer)
184
+ let rows = dataView.getInt32(8 + startOffset, true);
185
+ if (rows == 0) {
186
+ return rows;
187
+ }
188
+ let taosData = this.taosResult.getData();
189
+ let metaData = this.taosResult.getMeta();
190
+ if (metaData && rows && taosData) {
191
+ let dataList = new Array(rows);
192
+ //get bitmap length
193
+ let bitMapOffset = getBitmapLen(rows);
194
+ //skip data head
195
+ let bufferOffset = 28 + 5 * this.schema.length + startOffset;
196
+ let metaLens = [];
197
+ for (let i = 0; i < this.schema.length; i++) {
198
+ //get data len
199
+ metaLens.push(dataView.getInt32(bufferOffset + i * 4, true));
200
+ }
201
+ bufferOffset += this.schema.length * 4;
202
+ for (let i = 0; i < this.schema.length; i++) {
203
+ let data = [];
204
+ //get type code
205
+ let isVarType = (0, taosResult_1._isVarType)(this.schema[i].colType);
206
+ //fixed length type
207
+ if (isVarType == constant_1.ColumnsBlockType.SOLID) {
208
+ // let bitMapArr = dataBuffer.slice(bufferOffset, bufferOffset + bitMapOffset);
209
+ let bitMapArr = new Uint8Array(dataView.buffer, dataView.byteOffset + bufferOffset, bitMapOffset);
210
+ bufferOffset += bitMapOffset;
211
+ //decode column data, data is array
212
+ data = (0, taosResult_1.readSolidDataToArray)(dataView, bufferOffset, rows, this.schema[i].colType, bitMapArr);
213
+ }
214
+ else {
215
+ //Variable length type
216
+ let start = bufferOffset;
217
+ let offsets = [];
218
+ for (let i = 0; i < rows; i++, start += constant_1.TDengineTypeLength['INT']) {
219
+ //get data length, -1 is null
220
+ offsets.push(dataView.getInt32(start, true));
221
+ }
222
+ for (let i = 0; i < rows; i++) {
223
+ let value = '';
224
+ if (-1 == offsets[i]) {
225
+ value = null;
226
+ }
227
+ else {
228
+ let header = start + offsets[i];
229
+ let dataLength = dataView.getInt16(header, true) & 0xFFFF;
230
+ if (isVarType == constant_1.ColumnsBlockType.VARCHAR) {
231
+ //decode var char
232
+ value = (0, taosResult_1.readVarchar)(dataView.buffer, dataView.byteOffset + header + 2, dataLength, this.textDecoder);
233
+ }
234
+ else if (isVarType == constant_1.ColumnsBlockType.GEOMETRY || isVarType == constant_1.ColumnsBlockType.VARBINARY) {
235
+ //decode binary
236
+ value = (0, taosResult_1.readBinary)(dataView.buffer, dataView.byteOffset + header + 2, dataLength);
237
+ }
238
+ else {
239
+ //decode nchar
240
+ value = (0, taosResult_1.readNchar)(dataView.buffer, dataView.byteOffset + header + 2, dataLength);
241
+ }
242
+ }
243
+ data.push(value);
244
+ }
245
+ bufferOffset += rows * 4;
246
+ }
247
+ bufferOffset += metaLens[i];
248
+ //column data to row data
249
+ for (let row = 0; row < data.length; row++) {
250
+ if (dataList[row] == null) {
251
+ dataList[row] = [];
252
+ }
253
+ dataList[row].push(data[row]);
254
+ }
255
+ }
256
+ taosData.push(...dataList);
257
+ }
258
+ return rows;
259
+ }
260
+ }
261
+ exports.WSTmqFetchBlockInfo = WSTmqFetchBlockInfo;
262
+ class AssignmentResp {
263
+ constructor(resp, topic) {
264
+ this.timing = BigInt(resp.msg.timing);
265
+ this.code = resp.msg.code;
266
+ this.message = resp.msg.message;
267
+ this.req_id = resp.msg.req_id;
268
+ this.action = resp.msg.action;
269
+ this.totalTime = resp.totalTime;
270
+ this.topicPartition = resp.msg.assignment;
271
+ for (let i in this.topicPartition) {
272
+ this.topicPartition[i].topic = topic;
273
+ }
274
+ }
275
+ }
276
+ exports.AssignmentResp = AssignmentResp;
277
+ class SubscriptionResp {
278
+ constructor(resp) {
279
+ this.timing = BigInt(resp.msg.timing);
280
+ this.code = resp.msg.code;
281
+ this.message = resp.msg.message;
282
+ this.req_id = resp.msg.req_id;
283
+ this.action = resp.msg.action;
284
+ this.totalTime = resp.totalTime;
285
+ this.topics = resp.msg.topics;
286
+ }
287
+ }
288
+ exports.SubscriptionResp = SubscriptionResp;
289
+ class PartitionsResp {
290
+ constructor(resp) {
291
+ this.timing = BigInt(resp.msg.timing);
292
+ this.code = resp.msg.code;
293
+ this.message = resp.msg.message;
294
+ this.req_id = resp.msg.req_id;
295
+ this.action = resp.msg.action;
296
+ this.totalTime = resp.totalTime;
297
+ this.positions = resp.msg.position;
298
+ }
299
+ setTopicPartitions(topicPartitions) {
300
+ if (topicPartitions.length != this.positions.length) {
301
+ throw new wsError_1.WebSocketInterfaceError(wsError_1.ErrorCode.ERR_PARTITIONS_TOPIC_VGROUP_LENGTH_NOT_EQUAL, 'TopicPartitions and positions are not equal in length');
302
+ }
303
+ for (let i in this.positions) {
304
+ topicPartitions[i].offset = this.positions[i];
305
+ }
306
+ return topicPartitions;
307
+ }
308
+ }
309
+ exports.PartitionsResp = PartitionsResp;
310
+ class CommittedResp extends PartitionsResp {
311
+ constructor(resp) {
312
+ super(resp);
313
+ this.positions = resp.msg.committed;
314
+ }
315
+ }
316
+ exports.CommittedResp = CommittedResp;
317
+ class TopicPartition {
318
+ constructor(msg) {
319
+ this.vgroup_id = msg.vgroup_id;
320
+ this.offset = msg.offset;
321
+ this.begin = msg.begin;
322
+ this.end = msg.end;
323
+ this.topic = '';
324
+ }
325
+ }
326
+ exports.TopicPartition = TopicPartition;
327
+ function getBitmapLen(n) {
328
+ return (n + 0x7) >> 3;
329
+ }
@@ -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,EAA0D,MAAM,eAAe,CAAC;AAKvL,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;IAgBvE,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;YA4Bd,QAAQ;YA4BR,iBAAiB;IAczB,UAAU,CAAC,MAAM,CAAC,EAAC,MAAM,EAAE,GAAE,OAAO,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;YAoBnD,gBAAgB;CA6BjC"}
@@ -0,0 +1,319 @@
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
+ const wsResponse_1 = require("../client/wsResponse");
15
+ class WsConsumer {
16
+ constructor(wsConfig) {
17
+ this._wsConfig = new config_1.TmqConfig(wsConfig);
18
+ log_1.default.debug(this._wsConfig);
19
+ this._wsClient = new wsClient_1.WsClient(this._wsConfig.url, this._wsConfig.timeout);
20
+ }
21
+ async init() {
22
+ await this._wsClient.ready();
23
+ return this;
24
+ }
25
+ static async newConsumer(wsConfig) {
26
+ if (wsConfig.size == 0 || !wsConfig.get(constant_1.TMQConstants.WS_URL)) {
27
+ throw new wsError_1.WebSocketInterfaceError(wsError_1.ErrorCode.ERR_INVALID_URL, 'invalid url, password or username needed.');
28
+ }
29
+ let wsConsumer = new WsConsumer(wsConfig);
30
+ return await wsConsumer.init();
31
+ }
32
+ async subscribe(topics, reqId) {
33
+ if (!topics || topics.length == 0) {
34
+ throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq Subscribe params is error!');
35
+ }
36
+ let queryMsg = {
37
+ action: constant_1.TMQMessageType.Subscribe,
38
+ args: {
39
+ req_id: reqid_1.ReqId.getReqID(reqId),
40
+ user: this._wsConfig.user,
41
+ password: this._wsConfig.password,
42
+ group_id: this._wsConfig.group_id,
43
+ client_id: this._wsConfig.client_id,
44
+ topics: topics,
45
+ offset_rest: this._wsConfig.offset_rest,
46
+ auto_commit: this._wsConfig.auto_commit,
47
+ auto_commit_interval_ms: this._wsConfig.auto_commit_interval_ms
48
+ },
49
+ };
50
+ this._topics = topics;
51
+ return await this._wsClient.exec(JSON.stringify(queryMsg));
52
+ }
53
+ async unsubscribe(reqId) {
54
+ let queryMsg = {
55
+ action: constant_1.TMQMessageType.Unsubscribe,
56
+ args: {
57
+ req_id: reqid_1.ReqId.getReqID(reqId),
58
+ },
59
+ };
60
+ return await this._wsClient.exec(JSON.stringify(queryMsg));
61
+ }
62
+ async poll(timeoutMs, reqId) {
63
+ if (this._wsConfig.auto_commit) {
64
+ if (this._commitTime) {
65
+ let currTime = new Date().getTime();
66
+ let diff = Math.abs(currTime - this._commitTime);
67
+ if (diff >= this._wsConfig.auto_commit_interval_ms) {
68
+ await this.doCommit();
69
+ this._commitTime = new Date().getTime();
70
+ }
71
+ }
72
+ else {
73
+ this._commitTime = new Date().getTime();
74
+ }
75
+ }
76
+ return await this.pollData(timeoutMs, reqId);
77
+ }
78
+ async subscription(reqId) {
79
+ let queryMsg = {
80
+ action: constant_1.TMQMessageType.ListTopics,
81
+ args: {
82
+ req_id: reqid_1.ReqId.getReqID(reqId),
83
+ },
84
+ };
85
+ let resp = await this._wsClient.exec(JSON.stringify(queryMsg), false);
86
+ return new tmqResponse_1.SubscriptionResp(resp).topics;
87
+ }
88
+ async commit(reqId) {
89
+ await this.doCommit(reqId);
90
+ return await this.assignment();
91
+ }
92
+ async doCommit(reqId) {
93
+ let queryMsg = {
94
+ action: constant_1.TMQMessageType.Commit,
95
+ args: {
96
+ req_id: reqid_1.ReqId.getReqID(reqId),
97
+ message_id: 0
98
+ },
99
+ };
100
+ await this._wsClient.exec(JSON.stringify(queryMsg));
101
+ }
102
+ async committed(partitions, reqId) {
103
+ if (!partitions || partitions.length == 0) {
104
+ throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq Positions params is error!');
105
+ }
106
+ let offsets = new Array(partitions.length);
107
+ for (let i = 0; i < partitions.length; i++) {
108
+ offsets[i] = {
109
+ topic: partitions[i].topic,
110
+ vgroup_id: partitions[i].vgroup_id
111
+ };
112
+ offsets[i].vgroup_id = partitions[i].vgroup_id;
113
+ }
114
+ let queryMsg = {
115
+ action: constant_1.TMQMessageType.Committed,
116
+ args: {
117
+ req_id: reqid_1.ReqId.getReqID(reqId),
118
+ topic_vgroup_ids: offsets
119
+ },
120
+ };
121
+ let resp = await this._wsClient.exec(JSON.stringify(queryMsg), false);
122
+ return new tmqResponse_1.CommittedResp(resp).setTopicPartitions(offsets);
123
+ }
124
+ async commitOffsets(partitions) {
125
+ if (!partitions || partitions.length == 0) {
126
+ throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq CommitOffsets params is error!');
127
+ }
128
+ const allp = [];
129
+ partitions.forEach(e => {
130
+ allp.push(this.commitOffset(e));
131
+ });
132
+ await Promise.all(allp);
133
+ return await this.committed(partitions);
134
+ }
135
+ async commitOffset(partition, reqId) {
136
+ if (!partition) {
137
+ throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq CommitOffsets params is error!');
138
+ }
139
+ let queryMsg = {
140
+ action: constant_1.TMQMessageType.CommitOffset,
141
+ args: {
142
+ req_id: reqid_1.ReqId.getReqID(reqId),
143
+ vgroup_id: partition.vgroup_id,
144
+ topic: partition.topic,
145
+ offset: partition.offset,
146
+ },
147
+ };
148
+ return await this._wsClient.exec(JSON.stringify(queryMsg));
149
+ }
150
+ async positions(partitions, reqId) {
151
+ if (!partitions || partitions.length == 0) {
152
+ throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq Positions params is error!');
153
+ }
154
+ let offsets = new Array(partitions.length);
155
+ for (let i = 0; i < partitions.length; i++) {
156
+ offsets[i] = {
157
+ topic: partitions[i].topic,
158
+ vgroup_id: partitions[i].vgroup_id
159
+ };
160
+ offsets[i].vgroup_id = partitions[i].vgroup_id;
161
+ }
162
+ let queryMsg = {
163
+ action: constant_1.TMQMessageType.Position,
164
+ args: {
165
+ req_id: reqid_1.ReqId.getReqID(reqId),
166
+ topic_vgroup_ids: offsets
167
+ },
168
+ };
169
+ let resp = await this._wsClient.exec(JSON.stringify(queryMsg), false);
170
+ return new tmqResponse_1.PartitionsResp(resp).setTopicPartitions(offsets);
171
+ }
172
+ async seek(partition, reqId) {
173
+ if (!partition) {
174
+ throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq Seek params is error!');
175
+ }
176
+ let queryMsg = {
177
+ action: constant_1.TMQMessageType.Seek,
178
+ args: {
179
+ req_id: reqid_1.ReqId.getReqID(reqId),
180
+ vgroup_id: partition.vgroup_id,
181
+ topic: partition.topic,
182
+ offset: partition.offset,
183
+ },
184
+ };
185
+ return await this._wsClient.exec(JSON.stringify(queryMsg));
186
+ }
187
+ async seekToBeginning(partitions) {
188
+ if (!partitions || partitions.length == 0) {
189
+ throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq SeekToBeginning params is error!');
190
+ }
191
+ return await this.seekToBeginOrEnd(partitions);
192
+ }
193
+ async seekToEnd(partitions) {
194
+ if (!partitions || partitions.length == 0) {
195
+ throw new wsError_1.TaosResultError(wsError_1.ErrorCode.ERR_INVALID_PARAMS, 'WsTmq SeekToEnd params is error!');
196
+ }
197
+ return await this.seekToBeginOrEnd(partitions, false);
198
+ }
199
+ async close() {
200
+ await this._wsClient.close();
201
+ }
202
+ async fetch(pollResp) {
203
+ let fetchMsg = {
204
+ action: 'fetch',
205
+ args: {
206
+ req_id: reqid_1.ReqId.getReqID(),
207
+ message_id: pollResp.message_id,
208
+ },
209
+ };
210
+ let jsonStr = JSON.stringify(fetchMsg);
211
+ log_1.default.debug('[wsQueryInterface.fetch.fetchMsg]===>' + jsonStr);
212
+ let result = await this._wsClient.exec(jsonStr, false);
213
+ return new tmqResponse_1.WsTmqQueryResponse(result);
214
+ }
215
+ async fetchBlockData(pollResp, taosResult) {
216
+ let fetchMsg = {
217
+ action: 'fetch_raw_data',
218
+ args: {
219
+ req_id: reqid_1.ReqId.getReqID(),
220
+ message_id: pollResp.message_id,
221
+ },
222
+ };
223
+ let jsonStr = JSON.stringify(fetchMsg);
224
+ log_1.default.debug('[wsQueryInterface.fetch.fetchMsg]===>' + jsonStr);
225
+ // const startTime = new Date().getTime();
226
+ let result = await this._wsClient.sendMsg(jsonStr);
227
+ let wsResponse = new wsResponse_1.WSFetchBlockResponse(result.msg);
228
+ if (wsResponse && wsResponse.data && wsResponse.blockLen > 0) {
229
+ // const parseStartTime = new Date().getTime();
230
+ let wsTmqResponse = new tmqResponse_1.WSTmqFetchBlockInfo(wsResponse.data, taosResult);
231
+ log_1.default.debug('[WSTmqFetchBlockInfo.fetchBlockData]===>' + wsTmqResponse.taosResult);
232
+ if (wsTmqResponse.rows > 0) {
233
+ // const endTime = new Date().getTime();
234
+ // console.log(endTime - parseStartTime, endTime - startTime);
235
+ return true;
236
+ }
237
+ }
238
+ return false;
239
+ }
240
+ async pollData(timeoutMs, reqId) {
241
+ let queryMsg = {
242
+ action: constant_1.TMQMessageType.Poll,
243
+ args: {
244
+ req_id: reqid_1.ReqId.getReqID(reqId),
245
+ blocking_time: timeoutMs
246
+ },
247
+ };
248
+ let resp = await this._wsClient.exec(JSON.stringify(queryMsg), false);
249
+ let pollResp = new tmqResponse_1.WsPollResponse(resp);
250
+ let taosResult = new tmqResponse_1.TaosTmqResult(pollResp);
251
+ var taosResults = new Map();
252
+ taosResults.set(pollResp.topic, taosResult);
253
+ if (!pollResp.have_message || pollResp.message_type != constant_1.TMQMessageType.ResDataType) {
254
+ return taosResults;
255
+ }
256
+ let finish = false;
257
+ while (!finish) {
258
+ finish = await this.fetchBlockData(pollResp, taosResult);
259
+ }
260
+ return taosResults;
261
+ }
262
+ async sendAssignmentReq(topic) {
263
+ let queryMsg = {
264
+ action: constant_1.TMQMessageType.GetTopicAssignment,
265
+ args: {
266
+ req_id: reqid_1.ReqId.getReqID(),
267
+ topic: topic
268
+ }
269
+ };
270
+ let resp = await this._wsClient.exec(JSON.stringify(queryMsg), false);
271
+ let assignmentInfo = new tmqResponse_1.AssignmentResp(resp, queryMsg.args.topic);
272
+ return assignmentInfo.topicPartition;
273
+ }
274
+ async assignment(topics) {
275
+ if (!topics || topics.length == 0) {
276
+ topics = this._topics;
277
+ }
278
+ let topicPartitions = [];
279
+ if (topics && topics.length > 0) {
280
+ const allp = [];
281
+ for (let i in topics) {
282
+ allp.push(this.sendAssignmentReq(topics[i]));
283
+ }
284
+ let result = await Promise.all(allp);
285
+ result.forEach(e => {
286
+ topicPartitions.push(...e);
287
+ });
288
+ }
289
+ return topicPartitions;
290
+ }
291
+ async seekToBeginOrEnd(partitions, bBegin = true) {
292
+ let topics = [];
293
+ partitions.forEach(e => {
294
+ topics.push(e.topic);
295
+ });
296
+ let topicPartitions = await this.assignment(topics);
297
+ let itemMap = topicPartitions.reduce((map, obj) => {
298
+ map.set(obj.topic + '_' + obj.vgroup_id, obj);
299
+ return map;
300
+ }, new Map());
301
+ const allp = [];
302
+ for (let i in partitions) {
303
+ if (itemMap.has(partitions[i].topic + '_' + partitions[i].vgroup_id)) {
304
+ let topicPartition = itemMap.get(partitions[i].topic + '_' + partitions[i].vgroup_id);
305
+ if (topicPartition) {
306
+ if (bBegin) {
307
+ topicPartition.offset = topicPartition.begin;
308
+ }
309
+ else {
310
+ topicPartition.offset = topicPartition.end;
311
+ }
312
+ allp.push(this.seek(topicPartition));
313
+ }
314
+ }
315
+ }
316
+ await Promise.all(allp);
317
+ }
318
+ }
319
+ exports.WsConsumer = WsConsumer;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=log.test.d.ts.map