@chainstream-io/sdk 0.1.20 → 0.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/{index-Rg3zipzv.d.cts → index-CuPWnFsI.d.cts} +44 -3
- package/dist/{index-8REU_oTW.d.ts → index-DWL6morC.d.ts} +44 -3
- package/dist/index.cjs +103 -61
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +103 -60
- package/dist/index.mjs.map +1 -1
- package/dist/stream/index.cjs +98 -55
- package/dist/stream/index.cjs.map +1 -1
- package/dist/stream/index.d.cts +1 -1
- package/dist/stream/index.d.ts +1 -1
- package/dist/stream/index.mjs +98 -54
- package/dist/stream/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/stream/index.cjs
CHANGED
|
@@ -37,7 +37,6 @@ module.exports = __toCommonJS(stream_exports);
|
|
|
37
37
|
|
|
38
38
|
// src/stream/stream.ts
|
|
39
39
|
var import_centrifuge = require("@chainstream-io/centrifuge");
|
|
40
|
-
var import_module = require("module");
|
|
41
40
|
|
|
42
41
|
// src/stream/stream.fields.ts
|
|
43
42
|
var CEL_FIELD_MAPPINGS = {
|
|
@@ -298,67 +297,104 @@ function getAvailableFields(methodName) {
|
|
|
298
297
|
}
|
|
299
298
|
|
|
300
299
|
// src/stream/stream.ts
|
|
301
|
-
var import_meta = {};
|
|
302
300
|
var StreamApi = class {
|
|
303
301
|
constructor(context) {
|
|
304
302
|
__publicField(this, "realtimeClient");
|
|
305
303
|
__publicField(this, "listenersMap");
|
|
306
|
-
__publicField(this, "
|
|
307
|
-
|
|
308
|
-
this.
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
}
|
|
319
|
-
if (!wsImpl && typeof WebSocket !== "undefined") {
|
|
320
|
-
wsImpl = WebSocket;
|
|
304
|
+
__publicField(this, "context");
|
|
305
|
+
__publicField(this, "initPromise", null);
|
|
306
|
+
this.context = context;
|
|
307
|
+
this.listenersMap = /* @__PURE__ */ new Map();
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Initialize Centrifuge client with current token
|
|
311
|
+
* This is called lazily when connect() is called
|
|
312
|
+
*/
|
|
313
|
+
async initClient() {
|
|
314
|
+
if (this.realtimeClient) {
|
|
315
|
+
return;
|
|
321
316
|
}
|
|
317
|
+
const token = await this.getTokenValue();
|
|
318
|
+
const realtimeEndpoint = this.buildWsUrl(this.context.streamUrl, token);
|
|
322
319
|
this.realtimeClient = new import_centrifuge.Centrifuge(realtimeEndpoint, {
|
|
323
|
-
...wsImpl && { websocket: wsImpl },
|
|
324
320
|
getToken: async (_ctx) => {
|
|
325
|
-
|
|
326
|
-
this.realtimeClient.setHttpHeaders({
|
|
327
|
-
Authorization: `Bearer ${token}`,
|
|
328
|
-
"User-Agent": `chainstream-io/sdk/javascript`
|
|
329
|
-
});
|
|
330
|
-
return token;
|
|
321
|
+
return this.getTokenValue();
|
|
331
322
|
}
|
|
332
323
|
});
|
|
333
324
|
this.realtimeClient.on("connected", () => {
|
|
334
325
|
console.log("[streaming] connected");
|
|
335
|
-
}).on("disconnected", (
|
|
336
|
-
console.warn("[streaming] disconnected",
|
|
326
|
+
}).on("disconnected", (ctx) => {
|
|
327
|
+
console.warn("[streaming] disconnected", ctx);
|
|
337
328
|
}).on("error", (err) => {
|
|
338
329
|
console.error("[streaming] error: ", err);
|
|
339
330
|
});
|
|
340
|
-
this.listenersMap = /* @__PURE__ */ new Map();
|
|
341
331
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
332
|
+
/**
|
|
333
|
+
* Wait for initialization to complete
|
|
334
|
+
* If not initialized, auto-connect first
|
|
335
|
+
*/
|
|
336
|
+
async ensureInitialized() {
|
|
337
|
+
if (!this.initPromise) {
|
|
338
|
+
this.connect();
|
|
339
|
+
}
|
|
340
|
+
await this.initPromise;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Get token value from string or TokenProvider
|
|
344
|
+
*/
|
|
345
|
+
async getTokenValue() {
|
|
346
|
+
return typeof this.context.accessToken === "string" ? this.context.accessToken : await this.context.accessToken.getToken();
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Build WebSocket URL with token as query parameter
|
|
350
|
+
*/
|
|
351
|
+
buildWsUrl(endpoint, token) {
|
|
352
|
+
const url = new URL(endpoint);
|
|
353
|
+
url.searchParams.set("token", token);
|
|
354
|
+
return url.toString();
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Connect to the WebSocket server.
|
|
358
|
+
* This is automatically called when you use subscribe methods if not already connected.
|
|
359
|
+
* You can also call this method manually for explicit control.
|
|
360
|
+
*/
|
|
361
|
+
connect() {
|
|
362
|
+
this.initPromise = this.initClient().then(() => {
|
|
363
|
+
this.realtimeClient.connect();
|
|
346
364
|
});
|
|
347
|
-
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Disconnect from the WebSocket server.
|
|
368
|
+
* All subscriptions will be automatically removed.
|
|
369
|
+
*/
|
|
370
|
+
disconnect() {
|
|
371
|
+
if (this.realtimeClient) {
|
|
372
|
+
this.realtimeClient.disconnect();
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Check if the WebSocket is currently connected.
|
|
377
|
+
*/
|
|
378
|
+
isConnected() {
|
|
379
|
+
return this.realtimeClient?.state === "connected";
|
|
348
380
|
}
|
|
349
381
|
/**
|
|
350
382
|
* Start batching commands for efficient bulk operations
|
|
351
383
|
* All subscription commands after this call will be batched until stopBatching is called
|
|
352
384
|
*/
|
|
353
385
|
startBatching() {
|
|
354
|
-
this.realtimeClient
|
|
386
|
+
if (this.realtimeClient) {
|
|
387
|
+
this.realtimeClient.startBatching();
|
|
388
|
+
}
|
|
355
389
|
}
|
|
356
390
|
/**
|
|
357
391
|
* Stop batching and flush all collected commands to the server
|
|
358
392
|
* This will send all batched subscription commands in a single network request
|
|
359
393
|
*/
|
|
360
394
|
stopBatching() {
|
|
361
|
-
this.realtimeClient
|
|
395
|
+
if (this.realtimeClient) {
|
|
396
|
+
this.realtimeClient.stopBatching();
|
|
397
|
+
}
|
|
362
398
|
}
|
|
363
399
|
/**
|
|
364
400
|
* Batch subscribe method that accepts a function containing subscription calls
|
|
@@ -388,26 +424,31 @@ var StreamApi = class {
|
|
|
388
424
|
});
|
|
389
425
|
}
|
|
390
426
|
subscribe(channel, fn, filter, methodName) {
|
|
391
|
-
let sub = this.realtimeClient.getSubscription(channel);
|
|
392
427
|
let listeners = this.listenersMap.get(channel);
|
|
393
|
-
if (!
|
|
428
|
+
if (!listeners) {
|
|
394
429
|
listeners = /* @__PURE__ */ new Set();
|
|
395
430
|
this.listenersMap.set(channel, listeners);
|
|
396
|
-
console.log("[xrealtime] create new sub: ", channel);
|
|
397
|
-
const processedFilter = filter && methodName ? replaceFilterFields(filter, methodName) : filter;
|
|
398
|
-
sub = this.realtimeClient.newSubscription(channel, {
|
|
399
|
-
delta: "fossil",
|
|
400
|
-
...processedFilter && { filter: processedFilter }
|
|
401
|
-
});
|
|
402
|
-
sub.on("subscribed", () => {
|
|
403
|
-
console.log("[xrealtime] subscribed", channel);
|
|
404
|
-
}).on("unsubscribed", () => {
|
|
405
|
-
console.log("[xrealtime] unsubscribed", channel);
|
|
406
|
-
}).on("publication", (ctx) => {
|
|
407
|
-
listeners?.forEach((it) => it(ctx.data));
|
|
408
|
-
}).subscribe();
|
|
409
431
|
}
|
|
410
|
-
listeners
|
|
432
|
+
listeners.add(fn);
|
|
433
|
+
this.ensureInitialized().then(() => {
|
|
434
|
+
let sub = this.realtimeClient.getSubscription(channel);
|
|
435
|
+
if (!sub) {
|
|
436
|
+
console.log("[xrealtime] create new sub: ", channel);
|
|
437
|
+
const processedFilter = filter && methodName ? replaceFilterFields(filter, methodName) : filter;
|
|
438
|
+
sub = this.realtimeClient.newSubscription(channel, {
|
|
439
|
+
delta: "fossil",
|
|
440
|
+
...processedFilter && { filter: processedFilter }
|
|
441
|
+
});
|
|
442
|
+
sub.on("subscribed", () => {
|
|
443
|
+
console.log("[xrealtime] subscribed", channel);
|
|
444
|
+
}).on("unsubscribed", () => {
|
|
445
|
+
console.log("[xrealtime] unsubscribed", channel);
|
|
446
|
+
}).on("publication", (ctx) => {
|
|
447
|
+
const currentListeners = this.listenersMap.get(channel);
|
|
448
|
+
currentListeners?.forEach((it) => it(ctx.data));
|
|
449
|
+
}).subscribe();
|
|
450
|
+
}
|
|
451
|
+
});
|
|
411
452
|
return new StreamUnsubscrible(this, channel, fn);
|
|
412
453
|
}
|
|
413
454
|
unsubscribe(channel, fn) {
|
|
@@ -419,12 +460,14 @@ var StreamApi = class {
|
|
|
419
460
|
console.log("unsubscribe, remain listeners: ", listeners.size);
|
|
420
461
|
if (listeners.size === 0) {
|
|
421
462
|
console.log("unsubscribe channel: ", channel);
|
|
422
|
-
const sub = this.realtimeClient.getSubscription(channel);
|
|
423
|
-
if (sub) {
|
|
424
|
-
sub.unsubscribe();
|
|
425
|
-
this.realtimeClient.removeSubscription(sub);
|
|
426
|
-
}
|
|
427
463
|
this.listenersMap.delete(channel);
|
|
464
|
+
this.ensureInitialized().then(() => {
|
|
465
|
+
const sub = this.realtimeClient.getSubscription(channel);
|
|
466
|
+
if (sub) {
|
|
467
|
+
sub.unsubscribe();
|
|
468
|
+
this.realtimeClient.removeSubscription(sub);
|
|
469
|
+
}
|
|
470
|
+
});
|
|
428
471
|
}
|
|
429
472
|
}
|
|
430
473
|
formatScientificNotation(value) {
|