@cubejs-client/ws-transport 1.3.13 → 1.3.14

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/dist/index.d.ts CHANGED
@@ -1,12 +1,4 @@
1
1
  import type { ITransport, ITransportResponse } from '@cubejs-client/core';
2
- /**
3
- * @title @cubejs-client/ws-transport
4
- * @permalink /@cubejs-client-ws-transport
5
- * @menuCategory Cube.js Frontend
6
- * @subcategory Reference
7
- * @menuOrder 4
8
- * @description WebSocket transport for Cube.js client
9
- */
10
2
  declare class WebSocketTransportResult {
11
3
  protected readonly status: unknown;
12
4
  protected readonly result: unknown;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAE1E;;;;;;;GAOG;AAEH,cAAM,wBAAwB;IAC5B,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IAEnC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;gBAEhB,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE;QAAE,MAAM,EAAE,OAAO,CAAC;QAAC,OAAO,EAAE,OAAO,CAAA;KAAE;IAKhE,IAAI;IAIV,KAAK;IAKC,IAAI;CAGlB;AAED,KAAK,yBAAyB,GAAG;IAC/B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IAEf,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,KAAK,OAAO,GAAG;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,GAAG,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC,CAAC;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AAW,aAAa,CAAC,KAAK,oBAAA,EAM7B;IAEY,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IASnC,IAAW,aAAa,uBAEvB;IAED,SAAS,CAAC,UAAU;IAqFpB,SAAS,CAAC,WAAW,CAAC,OAAO,EAAE,GAAG;IAa3B,OAAO,CACZ,MAAM,EAAE,MAAM,EACd,EAAE,aAAa,EAAE,GAAG,MAAM,EAAE,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GACpD,kBAAkB,CAAC,wBAAwB,CAAC;CA+ChD;AAED,eAAe,kBAAkB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cubejs-client/ws-transport",
3
- "version": "1.3.13",
3
+ "version": "1.3.14",
4
4
  "engines": {},
5
5
  "repository": {
6
6
  "type": "git",
@@ -20,13 +20,12 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@babel/runtime": "^7.1.2",
23
- "@cubejs-client/core": "1.3.13",
23
+ "@cubejs-client/core": "1.3.14",
24
24
  "core-js": "^3.6.5",
25
25
  "isomorphic-ws": "^4.0.1",
26
26
  "ws": "^7.3.1"
27
27
  },
28
28
  "files": [
29
- "src",
30
29
  "dist"
31
30
  ],
32
31
  "license": "MIT",
@@ -34,7 +33,7 @@
34
33
  "@babel/core": "^7.3.3",
35
34
  "@babel/preset-env": "^7.3.1",
36
35
  "@babel/preset-typescript": "^7.12.1",
37
- "@cubejs-backend/linter": "1.3.13",
36
+ "@cubejs-backend/linter": "1.3.14",
38
37
  "@types/ws": "^7.2.9",
39
38
  "typescript": "~5.2.2"
40
39
  },
@@ -44,5 +43,5 @@
44
43
  "eslintConfig": {
45
44
  "extends": "../cubejs-linter"
46
45
  },
47
- "gitHead": "801fbe5c470d7d3d9ee03a937073048f5523be42"
46
+ "gitHead": "91c5e878fceff1984b8bcfa2dde95e7e086f26c6"
48
47
  }
package/src/index.ts DELETED
@@ -1,255 +0,0 @@
1
- import WebSocket from 'isomorphic-ws';
2
- import type { ITransport, ITransportResponse } from '@cubejs-client/core';
3
-
4
- /**
5
- * @title @cubejs-client/ws-transport
6
- * @permalink /@cubejs-client-ws-transport
7
- * @menuCategory Cube.js Frontend
8
- * @subcategory Reference
9
- * @menuOrder 4
10
- * @description WebSocket transport for Cube.js client
11
- */
12
-
13
- class WebSocketTransportResult {
14
- protected readonly status: unknown;
15
-
16
- protected readonly result: unknown;
17
-
18
- public constructor({ status, message }: { status: unknown, message: unknown }) {
19
- this.status = status;
20
- this.result = message;
21
- }
22
-
23
- public async json() {
24
- return this.result;
25
- }
26
-
27
- public clone() {
28
- // no need to actually clone it
29
- return this;
30
- }
31
-
32
- public async text() {
33
- return typeof this.result === 'string' ? this.result : JSON.stringify(this.result);
34
- }
35
- }
36
-
37
- type WebSocketTransportOptions = {
38
- authorization?: string,
39
- apiUrl: string,
40
- // @deprecated
41
- hearBeatInterval?: number,
42
- heartBeatInterval?: number,
43
- };
44
-
45
- type Message = {
46
- messageId: number,
47
- requestId: any,
48
- method: string,
49
- params: Record<string, unknown>,
50
- };
51
-
52
- type Subscription = {
53
- message: Message,
54
- callback: (result: WebSocketTransportResult) => void,
55
- };
56
-
57
- class WebSocketTransport implements ITransport<WebSocketTransportResult> {
58
- protected readonly apiUrl: string;
59
-
60
- protected readonly heartBeatInterval: number = 60;
61
-
62
- protected token: string | undefined;
63
-
64
- protected ws: any = null;
65
-
66
- protected messageCounter: number = 1;
67
-
68
- protected messageIdToSubscription: Record<number, Subscription> = {};
69
-
70
- protected messageQueue: Message[] = [];
71
-
72
- public constructor({ authorization, apiUrl, heartBeatInterval, hearBeatInterval }: WebSocketTransportOptions) {
73
- this.token = authorization;
74
- this.apiUrl = apiUrl;
75
-
76
- if (heartBeatInterval) {
77
- this.heartBeatInterval = heartBeatInterval;
78
- } else if (hearBeatInterval) {
79
- console.warn('Option hearBeatInterval is deprecated. It was replaced by heartBeatInterval.');
80
- this.heartBeatInterval = hearBeatInterval;
81
- }
82
- }
83
-
84
- public set authorization(token) {
85
- this.token = token;
86
-
87
- if (this.ws) {
88
- this.ws.close();
89
- }
90
- }
91
-
92
- public async close(): Promise<void> {
93
- if (this.ws) {
94
- // Flush send queue before sending close frame
95
- this.ws.sendQueue();
96
-
97
- this.ws.close();
98
- }
99
- }
100
-
101
- public get authorization() {
102
- return this.token;
103
- }
104
-
105
- protected initSocket() {
106
- if (this.ws) {
107
- return this.ws.initPromise;
108
- }
109
-
110
- const ws: any = new WebSocket(this.apiUrl);
111
-
112
- ws.messageIdSent = {};
113
-
114
- ws.sendMessage = (message: any) => {
115
- if (!message.messageId || message.messageId && !ws.messageIdSent[message.messageId]) {
116
- ws.send(JSON.stringify(message));
117
- ws.messageIdSent[message.messageId] = true;
118
- }
119
- };
120
-
121
- ws.sendQueue = () => {
122
- this.messageQueue.forEach(message => ws.sendMessage(message));
123
- this.messageQueue = [];
124
- };
125
-
126
- ws.reconcile = () => {
127
- if (new Date().getTime() - ws.lastMessageTimestamp.getTime() > 4 * this.heartBeatInterval * 1000) {
128
- ws.close();
129
- } else {
130
- Object.keys(this.messageIdToSubscription).forEach(messageId => {
131
- // @ts-ignore
132
- ws.sendMessage(this.messageIdToSubscription[messageId].message);
133
- });
134
- }
135
- };
136
-
137
- ws.lastMessageTimestamp = new Date();
138
-
139
- ws.initPromise = new Promise<void>(resolve => {
140
- ws.onopen = () => {
141
- ws.sendMessage({ authorization: this.authorization });
142
- };
143
-
144
- ws.onmessage = (event: any) => {
145
- ws.lastMessageTimestamp = new Date();
146
-
147
- const message: any = JSON.parse(event.data);
148
- if (message.handshake) {
149
- ws.reconcile();
150
- ws.reconcileTimer = setInterval(() => {
151
- ws.messageIdSent = {};
152
- ws.reconcile();
153
- }, this.heartBeatInterval * 1000);
154
- resolve();
155
- }
156
-
157
- if (this.messageIdToSubscription[message.messageId]) {
158
- this.messageIdToSubscription[message.messageId].callback(
159
- new WebSocketTransportResult(message)
160
- );
161
- }
162
-
163
- ws.sendQueue();
164
- };
165
-
166
- ws.onclose = () => {
167
- if (ws && ws.readyState !== WebSocket.CLOSED && ws.readyState !== WebSocket.CLOSING) {
168
- ws.close();
169
- }
170
- if (ws.reconcileTimer) {
171
- clearInterval(ws.reconcileTimer);
172
- ws.reconcileTimer = null;
173
- }
174
- if (this.ws === ws) {
175
- this.ws = null;
176
- if (Object.keys(this.messageIdToSubscription).length) {
177
- this.initSocket();
178
- }
179
- }
180
- };
181
-
182
- ws.onerror = ws.onclose;
183
- });
184
-
185
- this.ws = ws;
186
-
187
- return this.ws.initPromise;
188
- }
189
-
190
- protected sendMessage(message: any) {
191
- if (message.unsubscribe && this.messageQueue.find(m => m.messageId === message.unsubscribe)) {
192
- this.messageQueue = this.messageQueue.filter(m => m.messageId !== message.unsubscribe);
193
- } else {
194
- this.messageQueue.push(message);
195
- }
196
-
197
- setTimeout(async () => {
198
- await this.initSocket();
199
- this.ws.sendQueue();
200
- }, 100);
201
- }
202
-
203
- public request(
204
- method: string,
205
- { baseRequestId, ...params }: Record<string, unknown>
206
- ): ITransportResponse<WebSocketTransportResult> {
207
- const message: Message = {
208
- messageId: this.messageCounter++,
209
- requestId: baseRequestId,
210
- method,
211
- params
212
- };
213
-
214
- const pendingResults: WebSocketTransportResult[] = [];
215
- let nextMessage: ((value: any) => void) | null = null;
216
-
217
- const runNextMessage = () => {
218
- if (nextMessage) {
219
- nextMessage(pendingResults.pop());
220
- nextMessage = null;
221
- }
222
- };
223
-
224
- this.messageIdToSubscription[message.messageId] = {
225
- message,
226
- callback: (result) => {
227
- pendingResults.push(result);
228
- runNextMessage();
229
- }
230
- };
231
-
232
- const transport = this;
233
-
234
- return {
235
- async subscribe(callback) {
236
- transport.sendMessage(message);
237
-
238
- const result = await new Promise<WebSocketTransportResult>((resolve) => {
239
- nextMessage = resolve;
240
- if (pendingResults.length) {
241
- runNextMessage();
242
- }
243
- });
244
-
245
- return callback(result, () => this.subscribe(callback));
246
- },
247
- async unsubscribe() {
248
- transport.sendMessage({ unsubscribe: message.messageId });
249
- delete transport.messageIdToSubscription[message.messageId];
250
- }
251
- };
252
- }
253
- }
254
-
255
- export default WebSocketTransport;