relayx-webjs 1.0.3 → 1.0.5
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/CHANGELOG.md +3 -0
- package/package.json +1 -1
- package/realtime/realtime.js +13 -19
- package/realtime/dns_change.js +0 -20
- package/tests/test.js +0 -658
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/realtime/realtime.js
CHANGED
|
@@ -2,7 +2,6 @@ import { connect, JSONCodec, Events, DebugEvents, AckPolicy, ReplayPolicy, creds
|
|
|
2
2
|
import { DeliverPolicy, jetstream } from "@nats-io/jetstream";
|
|
3
3
|
import { encode, decode } from "@msgpack/msgpack";
|
|
4
4
|
import { v4 as uuidv4 } from 'uuid';
|
|
5
|
-
import { initDNSSpoof } from "./dns_change.js";
|
|
6
5
|
|
|
7
6
|
export class Realtime {
|
|
8
7
|
|
|
@@ -116,28 +115,23 @@ export class Realtime {
|
|
|
116
115
|
this.staging = staging;
|
|
117
116
|
this.opts = opts;
|
|
118
117
|
|
|
119
|
-
if(
|
|
120
|
-
this.#baseUrl = [
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
"nats://0.0.0.0:4422",
|
|
127
|
-
"nats://0.0.0.0:4423"
|
|
128
|
-
] :
|
|
129
|
-
[
|
|
130
|
-
`wss://api.relay-x.io:4421`,
|
|
131
|
-
`wss://api.relay-x.io:4422`,
|
|
132
|
-
`wss://api.relay-x.io:4423`
|
|
133
|
-
];
|
|
134
|
-
}else{
|
|
135
|
-
this.#baseUrl = [
|
|
118
|
+
if (staging !== undefined || staging !== null){
|
|
119
|
+
this.#baseUrl = staging ? [
|
|
120
|
+
"nats://0.0.0.0:4421",
|
|
121
|
+
"nats://0.0.0.0:4422",
|
|
122
|
+
"nats://0.0.0.0:4423"
|
|
123
|
+
] :
|
|
124
|
+
[
|
|
136
125
|
`wss://api.relay-x.io:4421`,
|
|
137
126
|
`wss://api.relay-x.io:4422`,
|
|
138
127
|
`wss://api.relay-x.io:4423`
|
|
139
128
|
];
|
|
140
|
-
|
|
129
|
+
}else{
|
|
130
|
+
this.#baseUrl = [
|
|
131
|
+
`wss://api.relay-x.io:4421`,
|
|
132
|
+
`wss://api.relay-x.io:4422`,
|
|
133
|
+
`wss://api.relay-x.io:4423`
|
|
134
|
+
];
|
|
141
135
|
}
|
|
142
136
|
|
|
143
137
|
this.#log(this.#baseUrl);
|
package/realtime/dns_change.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import dns from 'node:dns';
|
|
2
|
-
|
|
3
|
-
export function initDNSSpoof(){
|
|
4
|
-
const originalLookup = dns.lookup;
|
|
5
|
-
|
|
6
|
-
// Override for the whole process
|
|
7
|
-
dns.lookup = function patchedLookup(hostname, options, callback) {
|
|
8
|
-
|
|
9
|
-
// ── Our one special case ──────────────────────────────────
|
|
10
|
-
if (hostname === 'api2.relay-x.io') {
|
|
11
|
-
// Map to loop‑back; family 4 avoids ::1
|
|
12
|
-
return process.nextTick(() =>
|
|
13
|
-
callback(null, [{address: '127.0.0.1', family: 4}])
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Anything else → real DNS
|
|
18
|
-
return originalLookup.call(dns, hostname, options, callback);
|
|
19
|
-
};
|
|
20
|
-
}
|
package/tests/test.js
DELETED
|
@@ -1,658 +0,0 @@
|
|
|
1
|
-
import { Realtime, CONNECTED, RECONNECT, DISCONNECTED, MESSAGE_RESEND } from "../realtime/realtime.js";
|
|
2
|
-
import { test, before, after } from 'node:test';
|
|
3
|
-
import assert from 'node:assert';
|
|
4
|
-
|
|
5
|
-
let realTimeEnabled;
|
|
6
|
-
|
|
7
|
-
before(async () => {
|
|
8
|
-
// Start server for testing. Run local server!!
|
|
9
|
-
realTimeEnabled = new Realtime({
|
|
10
|
-
api_key: process.env.AUTH_JWT,
|
|
11
|
-
secret: process.env.AUTH_SECRET
|
|
12
|
-
});
|
|
13
|
-
await realTimeEnabled.init(false, {
|
|
14
|
-
debug: true
|
|
15
|
-
});
|
|
16
|
-
await realTimeEnabled.connect();
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
after(() => {
|
|
20
|
-
realTimeEnabled.close();
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test("No creds in constructor", async () => {
|
|
24
|
-
assert.throws(() => {
|
|
25
|
-
new Realtime({});
|
|
26
|
-
},
|
|
27
|
-
new Error("api_key value null"),
|
|
28
|
-
"Expected error was not thrown");
|
|
29
|
-
|
|
30
|
-
//---------------------------------------------------------------
|
|
31
|
-
|
|
32
|
-
assert.throws(() => {
|
|
33
|
-
new Realtime({api_key: "<KEY>"});
|
|
34
|
-
},
|
|
35
|
-
new Error("secret value null"),
|
|
36
|
-
"Expected error was not thrown");
|
|
37
|
-
|
|
38
|
-
//---------------------------------------------------------------
|
|
39
|
-
|
|
40
|
-
assert.throws(() => {
|
|
41
|
-
var realtime = new Realtime(null);
|
|
42
|
-
},
|
|
43
|
-
new Error("{api_key: <value>, secret: <value>} not passed in constructor"),
|
|
44
|
-
"Expected error was not thrown")
|
|
45
|
-
|
|
46
|
-
//---------------------------------------------------------------
|
|
47
|
-
|
|
48
|
-
assert.throws(() => {
|
|
49
|
-
new Realtime("KEY");
|
|
50
|
-
},
|
|
51
|
-
new Error("Realtime($config). $config not object => {}"),
|
|
52
|
-
"Expected error was not thrown")
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test('init() function test', async () => {
|
|
56
|
-
var realtime = new Realtime({
|
|
57
|
-
api_key: process.env.AUTH_JWT,
|
|
58
|
-
secret: process.env.AUTH_SECRET
|
|
59
|
-
});
|
|
60
|
-
await realtime.init(true);
|
|
61
|
-
|
|
62
|
-
assert.strictEqual(realtime.staging, true);
|
|
63
|
-
assert.deepStrictEqual(realtime.opts, {});
|
|
64
|
-
|
|
65
|
-
//---------------------------------------------------------------
|
|
66
|
-
|
|
67
|
-
await realtime.init({
|
|
68
|
-
debug: true,
|
|
69
|
-
max_retries: 2
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
assert.strictEqual(realtime.staging, false);
|
|
73
|
-
assert.deepStrictEqual(realtime.opts, {
|
|
74
|
-
debug: true,
|
|
75
|
-
max_retries: 2
|
|
76
|
-
})
|
|
77
|
-
assert.strictEqual(realtime.opts.debug, true);
|
|
78
|
-
assert.strictEqual(realtime.opts.max_retries, 2);
|
|
79
|
-
|
|
80
|
-
//---------------------------------------------------------------
|
|
81
|
-
|
|
82
|
-
await realtime.init(true, {
|
|
83
|
-
debug: false,
|
|
84
|
-
max_retries: 2
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
assert.strictEqual(realtime.staging, true);
|
|
88
|
-
assert.deepStrictEqual(realtime.opts, {
|
|
89
|
-
debug: false,
|
|
90
|
-
max_retries: 2
|
|
91
|
-
})
|
|
92
|
-
assert.strictEqual(realtime.opts.debug, false);
|
|
93
|
-
assert.strictEqual(realtime.opts.max_retries, 2);
|
|
94
|
-
|
|
95
|
-
//---------------------------------------------------------------
|
|
96
|
-
|
|
97
|
-
await realtime.init(false);
|
|
98
|
-
|
|
99
|
-
assert.strictEqual(realtime.staging, false);
|
|
100
|
-
assert.deepStrictEqual(realtime.opts, {})
|
|
101
|
-
|
|
102
|
-
assert.strictEqual(realtime.opts.debug, undefined);
|
|
103
|
-
assert.strictEqual(realtime.opts.max_retries, undefined);
|
|
104
|
-
|
|
105
|
-
//---------------------------------------------------------------
|
|
106
|
-
|
|
107
|
-
await realtime.init();
|
|
108
|
-
|
|
109
|
-
assert.strictEqual(realtime.staging, false);
|
|
110
|
-
assert.deepStrictEqual(realtime.opts, {})
|
|
111
|
-
|
|
112
|
-
assert.strictEqual(realtime.opts.debug, undefined);
|
|
113
|
-
assert.strictEqual(realtime.opts.max_retries, undefined);
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
test("Namespace check test", async () => {
|
|
117
|
-
assert.strictEqual(realTimeEnabled.namespace.length > 0, true)
|
|
118
|
-
assert.strictEqual(realTimeEnabled.topicHash.length > 0, true)
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
test("Retry method test", async () => {
|
|
122
|
-
var retryMethod = realTimeEnabled.testRetryTillSuccess();
|
|
123
|
-
|
|
124
|
-
assert.notStrictEqual(retryMethod, null, "Obj != null")
|
|
125
|
-
|
|
126
|
-
function testMethod1(arg){
|
|
127
|
-
return {
|
|
128
|
-
success: true,
|
|
129
|
-
output: arg
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
var output = await retryMethod(testMethod1, 5, 1, "test_output")
|
|
134
|
-
|
|
135
|
-
assert.strictEqual(output, "test_output");
|
|
136
|
-
|
|
137
|
-
function testMethod2(){
|
|
138
|
-
return {
|
|
139
|
-
success: false,
|
|
140
|
-
output: null
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
output = await retryMethod(testMethod2, 5, 1);
|
|
145
|
-
assert.strictEqual(output, null);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
test("get publish retry count test based in init()", async () => {
|
|
149
|
-
var realtime = new Realtime({
|
|
150
|
-
api_key: process.env.AUTH_JWT,
|
|
151
|
-
secret: process.env.AUTH_SECRET
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
await realtime.init({
|
|
155
|
-
max_retries: 2
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
var publishRetryMethod = realtime.testGetPublishRetry();
|
|
159
|
-
assert.notStrictEqual(publishRetryMethod, null);
|
|
160
|
-
|
|
161
|
-
var attempts = publishRetryMethod();
|
|
162
|
-
assert.strictEqual(attempts, 2);
|
|
163
|
-
|
|
164
|
-
//-----------------------------------------------------------------
|
|
165
|
-
|
|
166
|
-
await realtime.init({
|
|
167
|
-
max_retries: 0
|
|
168
|
-
})
|
|
169
|
-
|
|
170
|
-
attempts = publishRetryMethod();
|
|
171
|
-
assert.notStrictEqual(attempts, 0);
|
|
172
|
-
assert.strictEqual(attempts, 5);
|
|
173
|
-
|
|
174
|
-
//-----------------------------------------------------------------
|
|
175
|
-
|
|
176
|
-
await realtime.init({
|
|
177
|
-
max_retries: -4
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
attempts = publishRetryMethod();
|
|
181
|
-
assert.notStrictEqual(attempts, -4);
|
|
182
|
-
assert.strictEqual(attempts, 5);
|
|
183
|
-
|
|
184
|
-
//-----------------------------------------------------------------
|
|
185
|
-
|
|
186
|
-
await realtime.init({
|
|
187
|
-
max_retries: 9
|
|
188
|
-
})
|
|
189
|
-
|
|
190
|
-
attempts = await publishRetryMethod();
|
|
191
|
-
assert.strictEqual(attempts, 9);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
test("Testing publish(topic, data) method", async () => {
|
|
195
|
-
// Successful publish
|
|
196
|
-
var response = await realTimeEnabled.publish("hello", {
|
|
197
|
-
message: "Hello World!"
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
assert.strictEqual(response, true);
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
test("Testing publish(topic, data) with invalid inputs", async () => {
|
|
204
|
-
var data = {
|
|
205
|
-
message: "Hello World!"
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
await assert.rejects(async () => {
|
|
209
|
-
await realTimeEnabled.publish(null, data);
|
|
210
|
-
},
|
|
211
|
-
new Error("$topic is null or undefined"),
|
|
212
|
-
"Expected error was not thrown");
|
|
213
|
-
|
|
214
|
-
//---------------------------------------------------------------
|
|
215
|
-
|
|
216
|
-
await assert.rejects(async () => {
|
|
217
|
-
await realTimeEnabled.publish(undefined, data);
|
|
218
|
-
},
|
|
219
|
-
new Error("$topic is null or undefined"),
|
|
220
|
-
"Expected error was not thrown");
|
|
221
|
-
|
|
222
|
-
//---------------------------------------------------------------
|
|
223
|
-
|
|
224
|
-
await assert.rejects(async () => {
|
|
225
|
-
await realTimeEnabled.publish("", data);
|
|
226
|
-
},
|
|
227
|
-
new Error("$topic cannot be an empty string"),
|
|
228
|
-
"Expected error was not thrown");
|
|
229
|
-
|
|
230
|
-
//---------------------------------------------------------------
|
|
231
|
-
|
|
232
|
-
await assert.rejects(async () => {
|
|
233
|
-
await realTimeEnabled.publish(123, data);
|
|
234
|
-
},
|
|
235
|
-
new Error("Expected $topic type -> string. Instead receieved -> number"),
|
|
236
|
-
"Expected error was not thrown");
|
|
237
|
-
|
|
238
|
-
//---------------------------------------------------------------
|
|
239
|
-
|
|
240
|
-
await assert.rejects(async () => {
|
|
241
|
-
await realTimeEnabled.publish(CONNECTED, {});
|
|
242
|
-
},
|
|
243
|
-
new Error("Invalid topic, use isTopicValid($topic) to validate topic"),
|
|
244
|
-
"Expected error was not thrown");
|
|
245
|
-
|
|
246
|
-
//---------------------------------------------------------------
|
|
247
|
-
|
|
248
|
-
await assert.rejects(async () => {
|
|
249
|
-
await realTimeEnabled.publish(RECONNECT, {});
|
|
250
|
-
},
|
|
251
|
-
new Error("Invalid topic, use isTopicValid($topic) to validate topic"),
|
|
252
|
-
"Expected error was not thrown");
|
|
253
|
-
|
|
254
|
-
//---------------------------------------------------------------
|
|
255
|
-
|
|
256
|
-
await assert.rejects(async () => {
|
|
257
|
-
await realTimeEnabled.publish(DISCONNECTED, {});
|
|
258
|
-
},
|
|
259
|
-
new Error("Invalid topic, use isTopicValid($topic) to validate topic"),
|
|
260
|
-
"Expected error was not thrown");
|
|
261
|
-
|
|
262
|
-
//---------------------------------------------------------------
|
|
263
|
-
|
|
264
|
-
await assert.rejects(async () => {
|
|
265
|
-
await realTimeEnabled.publish(MESSAGE_RESEND, {});
|
|
266
|
-
},
|
|
267
|
-
new Error("Invalid topic, use isTopicValid($topic) to validate topic"),
|
|
268
|
-
"Expected error was not thrown");
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
test("on() test", async () => {
|
|
272
|
-
var realtime = new Realtime({
|
|
273
|
-
api_key: process.env.AUTH_JWT,
|
|
274
|
-
secret: process.env.AUTH_SECRET
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
await assert.rejects(async () => {
|
|
278
|
-
await realtime.on(null, null);
|
|
279
|
-
},
|
|
280
|
-
new Error("$topic is null / undefined"),
|
|
281
|
-
"Expected error was not thrown");
|
|
282
|
-
|
|
283
|
-
//---------------------------------------------------------------
|
|
284
|
-
|
|
285
|
-
await assert.rejects(async () => {
|
|
286
|
-
await realtime.on(undefined, null);
|
|
287
|
-
},
|
|
288
|
-
new Error("$topic is null / undefined"),
|
|
289
|
-
"Expected error was not thrown");
|
|
290
|
-
|
|
291
|
-
//---------------------------------------------------------------
|
|
292
|
-
|
|
293
|
-
await assert.rejects(async () => {
|
|
294
|
-
await realtime.on("undefined", null);
|
|
295
|
-
},
|
|
296
|
-
new Error("$func is null / undefined"),
|
|
297
|
-
"Expected error was not thrown");
|
|
298
|
-
|
|
299
|
-
//---------------------------------------------------------------
|
|
300
|
-
|
|
301
|
-
await assert.rejects(async () => {
|
|
302
|
-
await realtime.on("undefined", undefined);
|
|
303
|
-
},
|
|
304
|
-
new Error("$func is null / undefined"),
|
|
305
|
-
"Expected error was not thrown");
|
|
306
|
-
|
|
307
|
-
//---------------------------------------------------------------
|
|
308
|
-
|
|
309
|
-
await assert.rejects(async () => {
|
|
310
|
-
await realtime.on(123, () => {});
|
|
311
|
-
},
|
|
312
|
-
new Error("Expected $topic type -> string. Instead receieved -> number"),
|
|
313
|
-
"Expected error was not thrown");
|
|
314
|
-
|
|
315
|
-
//---------------------------------------------------------------
|
|
316
|
-
|
|
317
|
-
await assert.rejects(async () => {
|
|
318
|
-
await realtime.on("hello_world", "() => {}");
|
|
319
|
-
},
|
|
320
|
-
new Error("Expected $listener type -> function. Instead receieved -> string"),
|
|
321
|
-
"Expected error was not thrown");
|
|
322
|
-
|
|
323
|
-
//---------------------------------------------------------------
|
|
324
|
-
|
|
325
|
-
var res = await realtime.on("hello_world", () => {});
|
|
326
|
-
assert.strictEqual(res, true)
|
|
327
|
-
|
|
328
|
-
var eventFunc = realtime.testGetEventMap();
|
|
329
|
-
var topicMap = realtime.testGetTopicMap();
|
|
330
|
-
|
|
331
|
-
assert.strictEqual(topicMap.includes("hello_world"), true)
|
|
332
|
-
assert.strictEqual(topicMap.length > 0, true)
|
|
333
|
-
|
|
334
|
-
assert.notStrictEqual(eventFunc["hello_world"], null)
|
|
335
|
-
assert.notStrictEqual(eventFunc["hello_world"], undefined)
|
|
336
|
-
assert.strictEqual(typeof eventFunc["hello_world"], "function")
|
|
337
|
-
|
|
338
|
-
// Realtime already has a reference of this topic, so the return val will be false
|
|
339
|
-
res = await realtime.on("hello_world", () => {});
|
|
340
|
-
assert.strictEqual(res, false)
|
|
341
|
-
|
|
342
|
-
res = await realtime.on("hello_world", () => {});
|
|
343
|
-
assert.strictEqual(res, false)
|
|
344
|
-
|
|
345
|
-
res = await realtime.on("hello_world", () => {});
|
|
346
|
-
assert.strictEqual(res, false)
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
test("off() test", async () => {
|
|
350
|
-
var realtime = new Realtime({
|
|
351
|
-
api_key: process.env.AUTH_JWT,
|
|
352
|
-
secret: process.env.AUTH_SECRET
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
await assert.rejects(async () => {
|
|
356
|
-
await realtime.off(null);
|
|
357
|
-
},
|
|
358
|
-
new Error("$topic is null / undefined"),
|
|
359
|
-
"Expected error was not thrown");
|
|
360
|
-
|
|
361
|
-
await assert.rejects(async () => {
|
|
362
|
-
await realtime.off(undefined);
|
|
363
|
-
},
|
|
364
|
-
new Error("$topic is null / undefined"),
|
|
365
|
-
"Expected error was not thrown");
|
|
366
|
-
|
|
367
|
-
await assert.rejects(async () => {
|
|
368
|
-
await realtime.off(123);
|
|
369
|
-
},
|
|
370
|
-
new Error("Expected $topic type -> string. Instead receieved -> number"),
|
|
371
|
-
"Expected error was not thrown");
|
|
372
|
-
|
|
373
|
-
// Turning off topic multiple times to check for crashes.
|
|
374
|
-
// Since it is off already, output will be false
|
|
375
|
-
var status = await realtime.off("hello");
|
|
376
|
-
assert.strictEqual(status, false)
|
|
377
|
-
|
|
378
|
-
var eventFunc = realtime.testGetEventMap();
|
|
379
|
-
var topicMap = realtime.testGetTopicMap();
|
|
380
|
-
var consumerMap = realtime.testGetConsumerMap();
|
|
381
|
-
|
|
382
|
-
assert.strictEqual(!topicMap.includes("hello"), true)
|
|
383
|
-
assert.strictEqual(eventFunc["hello"], undefined)
|
|
384
|
-
assert.strictEqual(consumerMap["hello"], undefined)
|
|
385
|
-
|
|
386
|
-
var status = await realtime.off("hello");
|
|
387
|
-
assert.strictEqual(status, false)
|
|
388
|
-
|
|
389
|
-
var status = await realtime.off("hello");
|
|
390
|
-
assert.strictEqual(status, false)
|
|
391
|
-
|
|
392
|
-
var status = await realtime.off("hello");
|
|
393
|
-
assert.strictEqual(status, false)
|
|
394
|
-
|
|
395
|
-
});
|
|
396
|
-
|
|
397
|
-
test("Get stream name test", () => {
|
|
398
|
-
var realtime = new Realtime({
|
|
399
|
-
api_key: process.env.AUTH_JWT,
|
|
400
|
-
secret: process.env.AUTH_SECRET
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
realtime.namespace = "spacex-dragon-program"
|
|
404
|
-
realtime.topicHash = "topic_hash";
|
|
405
|
-
|
|
406
|
-
var getStreamName = realtime.testGetStreamName();
|
|
407
|
-
var getStreamTopic = realtime.testGetStreamTopic();
|
|
408
|
-
|
|
409
|
-
var name = getStreamName();
|
|
410
|
-
assert.strictEqual(name, `${realtime.namespace}_stream`);
|
|
411
|
-
|
|
412
|
-
var topic = getStreamTopic("hello_world")
|
|
413
|
-
assert.strictEqual(topic, `${realtime.topicHash}.hello_world`)
|
|
414
|
-
|
|
415
|
-
realtime.namespace = null;
|
|
416
|
-
realtime.topicHash = null;
|
|
417
|
-
|
|
418
|
-
assert.throws(() => {
|
|
419
|
-
getStreamName();
|
|
420
|
-
},
|
|
421
|
-
new Error("$namespace is null. Cannot initialize program with null $namespace"),
|
|
422
|
-
"Expected error was not thrown")
|
|
423
|
-
|
|
424
|
-
assert.throws(() => {
|
|
425
|
-
getStreamTopic("hello_world");
|
|
426
|
-
},
|
|
427
|
-
new Error("$topicHash is null. Cannot initialize program with null $topicHash"),
|
|
428
|
-
"Expected error was not thrown")
|
|
429
|
-
});
|
|
430
|
-
|
|
431
|
-
test("Test isTopicValidMethod()", () => {
|
|
432
|
-
var reservedTopics = ["CONNECTED", "DISCONNECTED",
|
|
433
|
-
"RECONNECT", "RECONNECTED", "RECONNECTING", "RECONN_FAIL", "MESSAGE_RESEND"
|
|
434
|
-
];
|
|
435
|
-
|
|
436
|
-
reservedTopics.forEach(topic => {
|
|
437
|
-
var valid = realTimeEnabled.isTopicValid(topic);
|
|
438
|
-
assert.strictEqual(valid, false);
|
|
439
|
-
});
|
|
440
|
-
|
|
441
|
-
var unreservedInvalidTopics = [null, undefined, 1234,
|
|
442
|
-
() => {console.log("hello")},
|
|
443
|
-
12.2, false, true, [], [1,2,3],
|
|
444
|
-
{test: 1}, {}];
|
|
445
|
-
|
|
446
|
-
unreservedInvalidTopics.forEach(topic => {
|
|
447
|
-
var valid = realTimeEnabled.isTopicValid(topic);
|
|
448
|
-
assert.strictEqual(valid, false);
|
|
449
|
-
});
|
|
450
|
-
|
|
451
|
-
unreservedInvalidTopics = [
|
|
452
|
-
"$foo",
|
|
453
|
-
"foo$",
|
|
454
|
-
"foo.$.bar",
|
|
455
|
-
"foo..bar",
|
|
456
|
-
".foo",
|
|
457
|
-
"foo.",
|
|
458
|
-
"foo.>.bar",
|
|
459
|
-
">foo",
|
|
460
|
-
"foo>bar",
|
|
461
|
-
"foo.>bar",
|
|
462
|
-
"foo.bar.>.",
|
|
463
|
-
"foo bar",
|
|
464
|
-
"foo/bar",
|
|
465
|
-
"foo#bar",
|
|
466
|
-
"",
|
|
467
|
-
" ",
|
|
468
|
-
"..",
|
|
469
|
-
".>",
|
|
470
|
-
"foo..",
|
|
471
|
-
".",
|
|
472
|
-
">.",
|
|
473
|
-
"foo,baz",
|
|
474
|
-
"αbeta",
|
|
475
|
-
"foo|bar",
|
|
476
|
-
"foo;bar",
|
|
477
|
-
"foo:bar",
|
|
478
|
-
"foo%bar",
|
|
479
|
-
"foo.*.>.bar",
|
|
480
|
-
"foo.*.>.",
|
|
481
|
-
"foo.*..bar",
|
|
482
|
-
"foo.>.bar",
|
|
483
|
-
"foo>"
|
|
484
|
-
];
|
|
485
|
-
|
|
486
|
-
unreservedInvalidTopics.forEach(topic => {
|
|
487
|
-
var valid = realTimeEnabled.isTopicValid(topic);
|
|
488
|
-
assert.strictEqual(valid, false);
|
|
489
|
-
});
|
|
490
|
-
|
|
491
|
-
var unreservedValidTopics = [
|
|
492
|
-
"foo",
|
|
493
|
-
"foo.bar",
|
|
494
|
-
"foo.bar.baz",
|
|
495
|
-
"*",
|
|
496
|
-
"foo.*",
|
|
497
|
-
"*.bar",
|
|
498
|
-
"foo.*.baz",
|
|
499
|
-
">",
|
|
500
|
-
"foo.>",
|
|
501
|
-
"foo.bar.>",
|
|
502
|
-
"*.*.>",
|
|
503
|
-
"alpha_beta",
|
|
504
|
-
"alpha-beta",
|
|
505
|
-
"alpha~beta",
|
|
506
|
-
"abc123",
|
|
507
|
-
"123abc",
|
|
508
|
-
"~",
|
|
509
|
-
"alpha.*.>",
|
|
510
|
-
"alpha.*",
|
|
511
|
-
"alpha.*.*",
|
|
512
|
-
"-foo",
|
|
513
|
-
"foo_bar-baz~qux",
|
|
514
|
-
"A.B.C",
|
|
515
|
-
"sensor.temperature",
|
|
516
|
-
"metric.cpu.load",
|
|
517
|
-
"foo.*.*",
|
|
518
|
-
"foo.*.>",
|
|
519
|
-
"foo_bar.*",
|
|
520
|
-
"*.*",
|
|
521
|
-
"metrics.>"
|
|
522
|
-
];
|
|
523
|
-
|
|
524
|
-
unreservedValidTopics.forEach(topic => {
|
|
525
|
-
var valid = realTimeEnabled.isTopicValid(topic);
|
|
526
|
-
console.log(topic)
|
|
527
|
-
assert.strictEqual(valid, true);
|
|
528
|
-
});
|
|
529
|
-
});
|
|
530
|
-
|
|
531
|
-
test("History test", async () => {
|
|
532
|
-
await assert.rejects(async () => {
|
|
533
|
-
await realTimeEnabled.history(null);
|
|
534
|
-
},
|
|
535
|
-
new Error("$topic is null or undefined"),
|
|
536
|
-
"Expected error was not thrown");
|
|
537
|
-
|
|
538
|
-
await assert.rejects(async () => {
|
|
539
|
-
await realTimeEnabled.history(undefined);
|
|
540
|
-
},
|
|
541
|
-
new Error("$topic is null or undefined"),
|
|
542
|
-
"Expected error was not thrown");
|
|
543
|
-
|
|
544
|
-
await assert.rejects(async () => {
|
|
545
|
-
await realTimeEnabled.history("");
|
|
546
|
-
},
|
|
547
|
-
new Error("$topic cannot be an empty string"),
|
|
548
|
-
"Expected error was not thrown");
|
|
549
|
-
|
|
550
|
-
await assert.rejects(async () => {
|
|
551
|
-
await realTimeEnabled.history(1234);
|
|
552
|
-
},
|
|
553
|
-
new Error("Expected $topic type -> string. Instead receieved -> number"),
|
|
554
|
-
"Expected error was not thrown");
|
|
555
|
-
|
|
556
|
-
await assert.rejects(async () => {
|
|
557
|
-
await realTimeEnabled.history("hello", null);
|
|
558
|
-
},
|
|
559
|
-
new Error("$start must be provided. $start is => null"),
|
|
560
|
-
"Expected error was not thrown");
|
|
561
|
-
|
|
562
|
-
await assert.rejects(async () => {
|
|
563
|
-
await realTimeEnabled.history("hello", undefined);
|
|
564
|
-
},
|
|
565
|
-
new Error("$start must be provided. $start is => undefined"),
|
|
566
|
-
"Expected error was not thrown");
|
|
567
|
-
|
|
568
|
-
await assert.rejects(async () => {
|
|
569
|
-
await realTimeEnabled.history("hello", "undefined");
|
|
570
|
-
},
|
|
571
|
-
new Error("$start must be a Date object"),
|
|
572
|
-
"Expected error was not thrown");
|
|
573
|
-
|
|
574
|
-
await assert.rejects(async () => {
|
|
575
|
-
await realTimeEnabled.history("hello", 1234);
|
|
576
|
-
},
|
|
577
|
-
new Error("$start must be a Date object"),
|
|
578
|
-
"Expected error was not thrown");
|
|
579
|
-
|
|
580
|
-
await assert.rejects(async () => {
|
|
581
|
-
await realTimeEnabled.history("hello", {});
|
|
582
|
-
},
|
|
583
|
-
new Error("$start must be a Date object"),
|
|
584
|
-
"Expected error was not thrown");
|
|
585
|
-
})
|
|
586
|
-
|
|
587
|
-
test("Pattern matcher test", async () => {
|
|
588
|
-
var cases = [
|
|
589
|
-
["foo", "foo", true], // 1
|
|
590
|
-
["foo", "bar", false], // 2
|
|
591
|
-
["foo.*", "foo.bar", true], // 3
|
|
592
|
-
["foo.bar", "foo.*", true], // 4
|
|
593
|
-
["*", "token", true], // 5
|
|
594
|
-
["*", "*", true], // 6
|
|
595
|
-
["foo.*", "foo.bar.baz", false], // 7
|
|
596
|
-
["foo.>", "foo.bar.baz", true], // 8
|
|
597
|
-
["foo.>", "foo", false], // 9 (zero‑token '>' now invalid)
|
|
598
|
-
["foo.bar.baz", "foo.>", true], // 10
|
|
599
|
-
["foo.bar.>", "foo.bar", false], // 11
|
|
600
|
-
["foo", "foo.>", false], // 12
|
|
601
|
-
["foo.*.>", "foo.bar.baz.qux", true], // 13
|
|
602
|
-
["foo.*.baz", "foo.bar.>", true], // 14
|
|
603
|
-
["alpha.*", "beta.gamma", false], // 15
|
|
604
|
-
["alpha.beta", "alpha.*.*", false], // 16
|
|
605
|
-
["foo.>.bar", "foo.any.bar", false], // 17 ('>' mid‑pattern)
|
|
606
|
-
[">", "foo.bar", true], // 18
|
|
607
|
-
[">", ">", true], // 19
|
|
608
|
-
["*", ">", true], // 20
|
|
609
|
-
["*.>", "foo.bar", true], // 21
|
|
610
|
-
["*.*.*", "a.b.c", true], // 22
|
|
611
|
-
["*.*.*", "a.b", false], // 23
|
|
612
|
-
["a.b.c.d.e", "a.b.c.d.e", true], // 24
|
|
613
|
-
["a.b.c.d.e", "a.b.c.d.f", false], // 25
|
|
614
|
-
["a.b.*.d", "a.b.c.d", true], // 26
|
|
615
|
-
["a.b.*.d", "a.b.c.e", false], // 27
|
|
616
|
-
["a.b.>", "a.b", false], // 28
|
|
617
|
-
["a.b", "a.b.c.d.>", false], // 29
|
|
618
|
-
["a.b.>.c", "a.b.x.c", false], // 30
|
|
619
|
-
["a.*.*", "a.b", false], // 31
|
|
620
|
-
["a.*", "a.b.c", false], // 32
|
|
621
|
-
["metrics.cpu.load", "metrics.*.load", true], // 33
|
|
622
|
-
["metrics.cpu.load", "metrics.cpu.*", true], // 34
|
|
623
|
-
["metrics.cpu.load", "metrics.>.load", false], // 35
|
|
624
|
-
["metrics.>", "metrics", false], // 36
|
|
625
|
-
["metrics.>", "othermetrics.cpu", false], // 37
|
|
626
|
-
["*.*.>", "a.b", false], // 38
|
|
627
|
-
["*.*.>", "a.b.c.d", true], // 39
|
|
628
|
-
["a.b.c", "*.*.*", true], // 40
|
|
629
|
-
["a.b.c", "*.*", false], // 41
|
|
630
|
-
["alpha.*.>", "alpha", false], // 42
|
|
631
|
-
["alpha.*.>", "alpha.beta", false], // 43
|
|
632
|
-
["alpha.*.>", "alpha.beta.gamma", true], // 44
|
|
633
|
-
["alpha.*.>", "beta.alpha.gamma", false], // 45
|
|
634
|
-
["foo-bar_baz", "foo-bar_baz", true], // 46
|
|
635
|
-
["foo-bar_*", "foo-bar_123", false], // 47 ( '*' here is literal )
|
|
636
|
-
["foo-bar_*", "foo-bar_*", true], // 48
|
|
637
|
-
["order-*", "order-123", false], // 49
|
|
638
|
-
["hello.hey.*", "hello.hey.>", true] // 50
|
|
639
|
-
];
|
|
640
|
-
|
|
641
|
-
var realtime = new Realtime({
|
|
642
|
-
api_key: process.env.AUTH_JWT,
|
|
643
|
-
secret: process.env.AUTH_SECRET
|
|
644
|
-
});
|
|
645
|
-
|
|
646
|
-
var patternMatcher = realtime.testPatternMatcher();
|
|
647
|
-
|
|
648
|
-
cases.forEach(testCase => {
|
|
649
|
-
var tokenA = testCase[0];
|
|
650
|
-
var tokenB = testCase[1];
|
|
651
|
-
var expectedResult = testCase[2];
|
|
652
|
-
|
|
653
|
-
console.log(`${tokenA} ⇆ ${tokenB} → ${expectedResult}`)
|
|
654
|
-
|
|
655
|
-
var result = patternMatcher(tokenA, tokenB)
|
|
656
|
-
assert.strictEqual(expectedResult, result)
|
|
657
|
-
});
|
|
658
|
-
})
|