@bymax-one/nest-realtime 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 +48 -1
- package/README.md +1 -1
- package/dist/internal/index.cjs +35 -17
- package/dist/internal/index.d.cts +56 -7
- package/dist/internal/index.d.ts +56 -7
- package/dist/internal/index.mjs +35 -17
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,51 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [1.0.5] - 2026-08-04
|
|
11
|
+
|
|
12
|
+
### Security
|
|
13
|
+
|
|
14
|
+
- The Redis credentials are no longer disclosed when the adapters that hold a client are
|
|
15
|
+
serialized. `RedisOfflineQueue` and `RedisRealtimePubSub` kept their ioredis instances
|
|
16
|
+
in TypeScript `private` properties, which are erased at runtime and leave enumerable
|
|
17
|
+
own properties, and an ioredis instance carries `options.password` as a plain field.
|
|
18
|
+
`JSON.stringify`, object spread and `util.inspect` therefore reached the password —
|
|
19
|
+
and, because both adapters are referenced from the module options, so did anything that
|
|
20
|
+
serialized those. Both clients and the lazily created subscriber connection move to
|
|
21
|
+
ECMAScript private fields.
|
|
22
|
+
|
|
23
|
+
Reading on purpose is unchanged and no public type or export moved.
|
|
24
|
+
|
|
25
|
+
## [1.0.4] - 2026-08-02
|
|
26
|
+
|
|
27
|
+
### Fixed
|
|
28
|
+
|
|
29
|
+
- **The published declarations required `@types/express`, which the package does
|
|
30
|
+
not depend on.** `dist/internal/index.d.ts` imported `Request` and `Response`
|
|
31
|
+
from `express`, so a consumer compiling with `skipLibCheck: false` failed with
|
|
32
|
+
`TS2307: Cannot find module 'express'` on the root and `./websocket` subpaths.
|
|
33
|
+
`express` is not a dependency, nor a peer, nor an optional peer, so there was no
|
|
34
|
+
supported way to satisfy it.
|
|
35
|
+
|
|
36
|
+
The SSE endpoint reads three fields from the request — `headers`, `ip`, `query` —
|
|
37
|
+
and does two things to the response: sets the anti-buffering headers, and writes
|
|
38
|
+
the keepalive comment. Those are now described by `SseRequest` and
|
|
39
|
+
`SseResponse`, structural contracts an `express.Request` satisfies without being
|
|
40
|
+
named by it. The same shape is what a Fastify request
|
|
41
|
+
provides, so the endpoint no longer assumes a platform it never required.
|
|
42
|
+
|
|
43
|
+
Runtime is unaffected: no bundle ever imported `express`, and the only change to
|
|
44
|
+
the built JavaScript is two JSDoc lines.
|
|
45
|
+
|
|
46
|
+
### Changed
|
|
47
|
+
|
|
48
|
+
- **`pnpm check:surface` now also checks what the published declarations import.**
|
|
49
|
+
Every module a `.d.ts` names has to be a declared peer, `./internal` included —
|
|
50
|
+
the root's declarations re-export from it, so an undeclared import there reaches
|
|
51
|
+
the consumer's compiler just the same. `tsc` could not see this (it only checks
|
|
52
|
+
what this repository references) and neither could `attw` (it checks that
|
|
53
|
+
declarations resolve, not what they contain).
|
|
54
|
+
|
|
10
55
|
## [1.0.3] - 2026-08-02
|
|
11
56
|
|
|
12
57
|
### Fixed
|
|
@@ -167,7 +212,9 @@ First published release.
|
|
|
167
212
|
rather than by the manual sweep that raised the NestJS floors — that sweep
|
|
168
213
|
asked about NestJS and never put the same question to the other ten peers.
|
|
169
214
|
|
|
170
|
-
[Unreleased]: https://github.com/bymaxone/nest-realtime/compare/v1.0.
|
|
215
|
+
[Unreleased]: https://github.com/bymaxone/nest-realtime/compare/v1.0.5...HEAD
|
|
216
|
+
[1.0.5]: https://github.com/bymaxone/nest-realtime/compare/v1.0.4...v1.0.5
|
|
217
|
+
[1.0.4]: https://github.com/bymaxone/nest-realtime/compare/v1.0.3...v1.0.4
|
|
171
218
|
[1.0.3]: https://github.com/bymaxone/nest-realtime/compare/v1.0.2...v1.0.3
|
|
172
219
|
[1.0.2]: https://github.com/bymaxone/nest-realtime/compare/v1.0.1...v1.0.2
|
|
173
220
|
[1.0.1]: https://github.com/bymaxone/nest-realtime/compare/v1.0.0...v1.0.1
|
package/README.md
CHANGED
|
@@ -644,7 +644,7 @@ pnpm typecheck && pnpm lint && pnpm test && pnpm build && pnpm size
|
|
|
644
644
|
|
|
645
645
|
## 🔒 Security Policy
|
|
646
646
|
|
|
647
|
-
If you discover a security vulnerability, please **do not** open a public issue. Instead, email us at **
|
|
647
|
+
If you discover a security vulnerability, please **do not** open a public issue. Instead, email us at **support@bymax.one** with details. We take security seriously and will respond promptly. Full policy and scope: [SECURITY.md](./SECURITY.md).
|
|
648
648
|
|
|
649
649
|
---
|
|
650
650
|
|
package/dist/internal/index.cjs
CHANGED
|
@@ -789,8 +789,8 @@ exports.SseSubscriptionHandler = class SseSubscriptionHandler {
|
|
|
789
789
|
* wires the downstream listener BEFORE calling `registerConnection` — so a plain
|
|
790
790
|
* `Subject` suffices and the replay buffer cannot grow unboundedly.
|
|
791
791
|
*
|
|
792
|
-
* @param req - The
|
|
793
|
-
* @param res - The
|
|
792
|
+
* @param req - The incoming request (SSE GET).
|
|
793
|
+
* @param res - The response (passthrough for header mutations and heartbeat writes).
|
|
794
794
|
* @returns An Observable of `MessageEvent` values for NestJS `@Sse` to stream.
|
|
795
795
|
* @throws UnauthorizedException when authentication returns null.
|
|
796
796
|
*/
|
|
@@ -1453,17 +1453,26 @@ exports.BymaxRealtimeModule = __decorateClass([
|
|
|
1453
1453
|
|
|
1454
1454
|
// src/server/pubsub/redis-realtime-pubsub.ts
|
|
1455
1455
|
var RedisRealtimePubSub = class {
|
|
1456
|
-
|
|
1456
|
+
/**
|
|
1457
|
+
* The publishing Redis client.
|
|
1458
|
+
*
|
|
1459
|
+
* An ECMAScript private field for the same reason as the subscriber below: an
|
|
1460
|
+
* ioredis instance carries `options.password` as a plain field, and a
|
|
1461
|
+
* TypeScript `private` one is erased at runtime, leaving it enumerable to
|
|
1462
|
+
* anything that serializes this pubsub adapter.
|
|
1463
|
+
*/
|
|
1464
|
+
#pub;
|
|
1457
1465
|
channel;
|
|
1458
|
-
|
|
1466
|
+
/** The lazily created subscriber connection, which carries the same credentials. */
|
|
1467
|
+
#subInit = null;
|
|
1459
1468
|
handlers = /* @__PURE__ */ new Set();
|
|
1460
1469
|
constructor(options) {
|
|
1461
|
-
this
|
|
1470
|
+
this.#pub = options.client;
|
|
1462
1471
|
this.channel = options.channel ?? "bymax:realtime";
|
|
1463
1472
|
}
|
|
1464
1473
|
/** JSON-encodes the message and PUBLISHes it to the configured channel. */
|
|
1465
1474
|
async publish(message) {
|
|
1466
|
-
await this
|
|
1475
|
+
await this.#pub.publish(this.channel, JSON.stringify(message));
|
|
1467
1476
|
}
|
|
1468
1477
|
/**
|
|
1469
1478
|
* Lazily creates the single shared subscribe client on first call; returns an async
|
|
@@ -1482,13 +1491,13 @@ var RedisRealtimePubSub = class {
|
|
|
1482
1491
|
sub = await this.ensureSubscriber();
|
|
1483
1492
|
} catch (err) {
|
|
1484
1493
|
this.handlers.delete(handler);
|
|
1485
|
-
this
|
|
1494
|
+
this.#subInit = null;
|
|
1486
1495
|
throw err;
|
|
1487
1496
|
}
|
|
1488
1497
|
return async () => {
|
|
1489
1498
|
this.handlers.delete(handler);
|
|
1490
1499
|
if (this.handlers.size === 0) {
|
|
1491
|
-
this
|
|
1500
|
+
this.#subInit = null;
|
|
1492
1501
|
try {
|
|
1493
1502
|
await sub.quit();
|
|
1494
1503
|
} catch {
|
|
@@ -1498,11 +1507,11 @@ var RedisRealtimePubSub = class {
|
|
|
1498
1507
|
}
|
|
1499
1508
|
/** Lazily create the single shared subscribe client (idempotent under concurrent calls). */
|
|
1500
1509
|
ensureSubscriber() {
|
|
1501
|
-
if (!this
|
|
1502
|
-
return this
|
|
1510
|
+
if (!this.#subInit) this.#subInit = this.createSubscriber();
|
|
1511
|
+
return this.#subInit;
|
|
1503
1512
|
}
|
|
1504
1513
|
async createSubscriber() {
|
|
1505
|
-
const sub = this
|
|
1514
|
+
const sub = this.#pub.duplicate();
|
|
1506
1515
|
await sub.subscribe(this.channel);
|
|
1507
1516
|
sub.on("message", (_ch, payload) => this.dispatch(payload));
|
|
1508
1517
|
return sub;
|
|
@@ -1525,7 +1534,16 @@ var RedisRealtimePubSub = class {
|
|
|
1525
1534
|
|
|
1526
1535
|
// src/server/offline-queue/redis-offline-queue.ts
|
|
1527
1536
|
var RedisOfflineQueue = class _RedisOfflineQueue {
|
|
1528
|
-
|
|
1537
|
+
/**
|
|
1538
|
+
* The Redis client.
|
|
1539
|
+
*
|
|
1540
|
+
* An ECMAScript private field rather than a TypeScript `private` one, which
|
|
1541
|
+
* is erased at runtime: an ioredis instance carries `options.password` as a
|
|
1542
|
+
* plain field, so leaving this enumerable would let anything that serializes
|
|
1543
|
+
* this queue — or the module options that reference it — reach the Redis
|
|
1544
|
+
* credentials.
|
|
1545
|
+
*/
|
|
1546
|
+
#client;
|
|
1529
1547
|
maxPerUser;
|
|
1530
1548
|
ttlSeconds;
|
|
1531
1549
|
/** Zero-pad width for the epoch-millisecond component of the sort key. */
|
|
@@ -1533,7 +1551,7 @@ var RedisOfflineQueue = class _RedisOfflineQueue {
|
|
|
1533
1551
|
/** Zero-pad width for the intra-millisecond counter component of the sort key. */
|
|
1534
1552
|
static COUNTER_PAD = 6;
|
|
1535
1553
|
constructor(options) {
|
|
1536
|
-
this
|
|
1554
|
+
this.#client = options.client;
|
|
1537
1555
|
this.maxPerUser = options.maxPerUser ?? 500;
|
|
1538
1556
|
this.ttlSeconds = options.ttlSeconds ?? 3600;
|
|
1539
1557
|
}
|
|
@@ -1572,7 +1590,7 @@ var RedisOfflineQueue = class _RedisOfflineQueue {
|
|
|
1572
1590
|
}
|
|
1573
1591
|
async append(userId, event) {
|
|
1574
1592
|
const key = this.key(userId);
|
|
1575
|
-
const pipeline = this
|
|
1593
|
+
const pipeline = this.#client.pipeline();
|
|
1576
1594
|
pipeline.zadd(key, 0, this.encodeMember(event));
|
|
1577
1595
|
pipeline.zremrangebyrank(key, 0, -(this.maxPerUser + 1));
|
|
1578
1596
|
pipeline.expire(key, this.ttlSeconds);
|
|
@@ -1583,7 +1601,7 @@ var RedisOfflineQueue = class _RedisOfflineQueue {
|
|
|
1583
1601
|
}
|
|
1584
1602
|
async retrieveSince(userId, sinceId, limit) {
|
|
1585
1603
|
const lowerBound = `[${this.lexKeyNext(sinceId)}`;
|
|
1586
|
-
const raw = await this
|
|
1604
|
+
const raw = await this.#client.zrangebylex(this.key(userId), lowerBound, "+", "LIMIT", 0, limit);
|
|
1587
1605
|
return raw.map(
|
|
1588
1606
|
(entry) => JSON.parse(
|
|
1589
1607
|
entry.slice(entry.indexOf("|") + 1),
|
|
@@ -1593,9 +1611,9 @@ var RedisOfflineQueue = class _RedisOfflineQueue {
|
|
|
1593
1611
|
}
|
|
1594
1612
|
async acknowledge(userId, upToId) {
|
|
1595
1613
|
const upperBound = `(${this.lexKeyNext(upToId)}`;
|
|
1596
|
-
const toRemove = await this
|
|
1614
|
+
const toRemove = await this.#client.zrangebylex(this.key(userId), "-", upperBound);
|
|
1597
1615
|
if (toRemove.length > 0) {
|
|
1598
|
-
await this
|
|
1616
|
+
await this.#client.zrem(this.key(userId), ...toRemove);
|
|
1599
1617
|
}
|
|
1600
1618
|
}
|
|
1601
1619
|
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ModuleMetadata, Type, Provider, Logger, DynamicModule, MessageEvent, OnModuleInit, OnApplicationShutdown } from '@nestjs/common';
|
|
2
2
|
import { Subject, Observable } from 'rxjs';
|
|
3
|
-
import { Request, Response } from 'express';
|
|
4
3
|
import { Redis } from 'ioredis';
|
|
5
4
|
|
|
6
5
|
/**
|
|
@@ -854,6 +853,57 @@ declare class HeartbeatService {
|
|
|
854
853
|
stopAll(): void;
|
|
855
854
|
}
|
|
856
855
|
|
|
856
|
+
/**
|
|
857
|
+
* @fileoverview The HTTP shape the SSE endpoint depends on.
|
|
858
|
+
* @layer contracts
|
|
859
|
+
*
|
|
860
|
+
* Structural on purpose, rather than `express.Request` and `express.Response`.
|
|
861
|
+
*
|
|
862
|
+
* Naming Express would make the published declarations import its types, and
|
|
863
|
+
* `@types/express` is not a dependency of this package — nor an optional peer, so
|
|
864
|
+
* a consumer compiling with `skipLibCheck: false` would have no supported way to
|
|
865
|
+
* satisfy it. It would also be the wrong type for an application on Fastify,
|
|
866
|
+
* which NestJS supports equally.
|
|
867
|
+
*
|
|
868
|
+
* The endpoint reads three fields from the request — `headers`, `ip`, `query` —
|
|
869
|
+
* and does two things to the response: sets the anti-buffering headers, and
|
|
870
|
+
* writes the keepalive comment. An `express.Request` satisfies these interfaces
|
|
871
|
+
* without being named by them, and so does a Fastify request, because
|
|
872
|
+
* assignability is structural.
|
|
873
|
+
*/
|
|
874
|
+
/** A header value as an HTTP server exposes it, before normalization. */
|
|
875
|
+
type SseHeaderValue = string | string[] | undefined;
|
|
876
|
+
/** The request fields the SSE endpoint reads. */
|
|
877
|
+
interface SseRequest {
|
|
878
|
+
/**
|
|
879
|
+
* Request headers, keyed as the HTTP framework delivers them. Node lowercases
|
|
880
|
+
* incoming header names, and the endpoint looks them up lowercase — `cookie`,
|
|
881
|
+
* `user-agent`, `x-forwarded-for` and `last-event-id` — so an adapter that
|
|
882
|
+
* preserves the client's casing has to lowercase the keys before it gets here,
|
|
883
|
+
* or those lookups miss.
|
|
884
|
+
*/
|
|
885
|
+
readonly headers: Readonly<Record<string, SseHeaderValue>>;
|
|
886
|
+
/** Remote address as the HTTP framework resolved it, when it resolves one. */
|
|
887
|
+
readonly ip?: string | undefined;
|
|
888
|
+
/** Parsed query string. Values are narrowed to strings by the caller. */
|
|
889
|
+
readonly query: Readonly<Record<string, unknown>>;
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* The response surface the SSE endpoint uses: anti-buffering headers, and a
|
|
893
|
+
* writable stream for the keepalive.
|
|
894
|
+
*
|
|
895
|
+
* `write` is structurally the same contract as `HeartbeatWritable`, which the
|
|
896
|
+
* heartbeat declares for itself so it can be driven by any writable in a test.
|
|
897
|
+
* It is repeated here rather than imported so the contracts layer does not depend
|
|
898
|
+
* on a service module.
|
|
899
|
+
*/
|
|
900
|
+
interface SseResponse {
|
|
901
|
+
/** Set a response header before the stream begins. */
|
|
902
|
+
setHeader(name: string, value: string): unknown;
|
|
903
|
+
/** Write raw bytes to the open stream — the `: keepalive` comment. */
|
|
904
|
+
write(chunk: string): unknown;
|
|
905
|
+
}
|
|
906
|
+
|
|
857
907
|
/** Parameters required to register a freshly authenticated SSE connection. */
|
|
858
908
|
interface RegisterSseConnectionParams {
|
|
859
909
|
connectionId: string;
|
|
@@ -1009,12 +1059,12 @@ declare class SseSubscriptionHandler {
|
|
|
1009
1059
|
* wires the downstream listener BEFORE calling `registerConnection` — so a plain
|
|
1010
1060
|
* `Subject` suffices and the replay buffer cannot grow unboundedly.
|
|
1011
1061
|
*
|
|
1012
|
-
* @param req - The
|
|
1013
|
-
* @param res - The
|
|
1062
|
+
* @param req - The incoming request (SSE GET).
|
|
1063
|
+
* @param res - The response (passthrough for header mutations and heartbeat writes).
|
|
1014
1064
|
* @returns An Observable of `MessageEvent` values for NestJS `@Sse` to stream.
|
|
1015
1065
|
* @throws UnauthorizedException when authentication returns null.
|
|
1016
1066
|
*/
|
|
1017
|
-
handle(req:
|
|
1067
|
+
handle(req: SseRequest, res: SseResponse): Promise<Observable<MessageEvent>>;
|
|
1018
1068
|
/**
|
|
1019
1069
|
* Build the live Observable using a subscribe-before-register pattern.
|
|
1020
1070
|
*
|
|
@@ -1153,9 +1203,8 @@ interface RedisRealtimePubSubOptions {
|
|
|
1153
1203
|
* ```
|
|
1154
1204
|
*/
|
|
1155
1205
|
declare class RedisRealtimePubSub implements IRealtimePubSub {
|
|
1156
|
-
private
|
|
1206
|
+
#private;
|
|
1157
1207
|
private readonly channel;
|
|
1158
|
-
private subInit;
|
|
1159
1208
|
private readonly handlers;
|
|
1160
1209
|
constructor(options: RedisRealtimePubSubOptions);
|
|
1161
1210
|
/** JSON-encodes the message and PUBLISHes it to the configured channel. */
|
|
@@ -1212,7 +1261,7 @@ interface RedisOfflineQueueOptions {
|
|
|
1212
1261
|
* ```
|
|
1213
1262
|
*/
|
|
1214
1263
|
declare class RedisOfflineQueue implements IOfflineQueueStorage {
|
|
1215
|
-
private
|
|
1264
|
+
#private;
|
|
1216
1265
|
private readonly maxPerUser;
|
|
1217
1266
|
private readonly ttlSeconds;
|
|
1218
1267
|
/** Zero-pad width for the epoch-millisecond component of the sort key. */
|
package/dist/internal/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { ModuleMetadata, Type, Provider, Logger, DynamicModule, MessageEvent, OnModuleInit, OnApplicationShutdown } from '@nestjs/common';
|
|
2
2
|
import { Subject, Observable } from 'rxjs';
|
|
3
|
-
import { Request, Response } from 'express';
|
|
4
3
|
import { Redis } from 'ioredis';
|
|
5
4
|
|
|
6
5
|
/**
|
|
@@ -854,6 +853,57 @@ declare class HeartbeatService {
|
|
|
854
853
|
stopAll(): void;
|
|
855
854
|
}
|
|
856
855
|
|
|
856
|
+
/**
|
|
857
|
+
* @fileoverview The HTTP shape the SSE endpoint depends on.
|
|
858
|
+
* @layer contracts
|
|
859
|
+
*
|
|
860
|
+
* Structural on purpose, rather than `express.Request` and `express.Response`.
|
|
861
|
+
*
|
|
862
|
+
* Naming Express would make the published declarations import its types, and
|
|
863
|
+
* `@types/express` is not a dependency of this package — nor an optional peer, so
|
|
864
|
+
* a consumer compiling with `skipLibCheck: false` would have no supported way to
|
|
865
|
+
* satisfy it. It would also be the wrong type for an application on Fastify,
|
|
866
|
+
* which NestJS supports equally.
|
|
867
|
+
*
|
|
868
|
+
* The endpoint reads three fields from the request — `headers`, `ip`, `query` —
|
|
869
|
+
* and does two things to the response: sets the anti-buffering headers, and
|
|
870
|
+
* writes the keepalive comment. An `express.Request` satisfies these interfaces
|
|
871
|
+
* without being named by them, and so does a Fastify request, because
|
|
872
|
+
* assignability is structural.
|
|
873
|
+
*/
|
|
874
|
+
/** A header value as an HTTP server exposes it, before normalization. */
|
|
875
|
+
type SseHeaderValue = string | string[] | undefined;
|
|
876
|
+
/** The request fields the SSE endpoint reads. */
|
|
877
|
+
interface SseRequest {
|
|
878
|
+
/**
|
|
879
|
+
* Request headers, keyed as the HTTP framework delivers them. Node lowercases
|
|
880
|
+
* incoming header names, and the endpoint looks them up lowercase — `cookie`,
|
|
881
|
+
* `user-agent`, `x-forwarded-for` and `last-event-id` — so an adapter that
|
|
882
|
+
* preserves the client's casing has to lowercase the keys before it gets here,
|
|
883
|
+
* or those lookups miss.
|
|
884
|
+
*/
|
|
885
|
+
readonly headers: Readonly<Record<string, SseHeaderValue>>;
|
|
886
|
+
/** Remote address as the HTTP framework resolved it, when it resolves one. */
|
|
887
|
+
readonly ip?: string | undefined;
|
|
888
|
+
/** Parsed query string. Values are narrowed to strings by the caller. */
|
|
889
|
+
readonly query: Readonly<Record<string, unknown>>;
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* The response surface the SSE endpoint uses: anti-buffering headers, and a
|
|
893
|
+
* writable stream for the keepalive.
|
|
894
|
+
*
|
|
895
|
+
* `write` is structurally the same contract as `HeartbeatWritable`, which the
|
|
896
|
+
* heartbeat declares for itself so it can be driven by any writable in a test.
|
|
897
|
+
* It is repeated here rather than imported so the contracts layer does not depend
|
|
898
|
+
* on a service module.
|
|
899
|
+
*/
|
|
900
|
+
interface SseResponse {
|
|
901
|
+
/** Set a response header before the stream begins. */
|
|
902
|
+
setHeader(name: string, value: string): unknown;
|
|
903
|
+
/** Write raw bytes to the open stream — the `: keepalive` comment. */
|
|
904
|
+
write(chunk: string): unknown;
|
|
905
|
+
}
|
|
906
|
+
|
|
857
907
|
/** Parameters required to register a freshly authenticated SSE connection. */
|
|
858
908
|
interface RegisterSseConnectionParams {
|
|
859
909
|
connectionId: string;
|
|
@@ -1009,12 +1059,12 @@ declare class SseSubscriptionHandler {
|
|
|
1009
1059
|
* wires the downstream listener BEFORE calling `registerConnection` — so a plain
|
|
1010
1060
|
* `Subject` suffices and the replay buffer cannot grow unboundedly.
|
|
1011
1061
|
*
|
|
1012
|
-
* @param req - The
|
|
1013
|
-
* @param res - The
|
|
1062
|
+
* @param req - The incoming request (SSE GET).
|
|
1063
|
+
* @param res - The response (passthrough for header mutations and heartbeat writes).
|
|
1014
1064
|
* @returns An Observable of `MessageEvent` values for NestJS `@Sse` to stream.
|
|
1015
1065
|
* @throws UnauthorizedException when authentication returns null.
|
|
1016
1066
|
*/
|
|
1017
|
-
handle(req:
|
|
1067
|
+
handle(req: SseRequest, res: SseResponse): Promise<Observable<MessageEvent>>;
|
|
1018
1068
|
/**
|
|
1019
1069
|
* Build the live Observable using a subscribe-before-register pattern.
|
|
1020
1070
|
*
|
|
@@ -1153,9 +1203,8 @@ interface RedisRealtimePubSubOptions {
|
|
|
1153
1203
|
* ```
|
|
1154
1204
|
*/
|
|
1155
1205
|
declare class RedisRealtimePubSub implements IRealtimePubSub {
|
|
1156
|
-
private
|
|
1206
|
+
#private;
|
|
1157
1207
|
private readonly channel;
|
|
1158
|
-
private subInit;
|
|
1159
1208
|
private readonly handlers;
|
|
1160
1209
|
constructor(options: RedisRealtimePubSubOptions);
|
|
1161
1210
|
/** JSON-encodes the message and PUBLISHes it to the configured channel. */
|
|
@@ -1212,7 +1261,7 @@ interface RedisOfflineQueueOptions {
|
|
|
1212
1261
|
* ```
|
|
1213
1262
|
*/
|
|
1214
1263
|
declare class RedisOfflineQueue implements IOfflineQueueStorage {
|
|
1215
|
-
private
|
|
1264
|
+
#private;
|
|
1216
1265
|
private readonly maxPerUser;
|
|
1217
1266
|
private readonly ttlSeconds;
|
|
1218
1267
|
/** Zero-pad width for the epoch-millisecond component of the sort key. */
|
package/dist/internal/index.mjs
CHANGED
|
@@ -787,8 +787,8 @@ var SseSubscriptionHandler = class {
|
|
|
787
787
|
* wires the downstream listener BEFORE calling `registerConnection` — so a plain
|
|
788
788
|
* `Subject` suffices and the replay buffer cannot grow unboundedly.
|
|
789
789
|
*
|
|
790
|
-
* @param req - The
|
|
791
|
-
* @param res - The
|
|
790
|
+
* @param req - The incoming request (SSE GET).
|
|
791
|
+
* @param res - The response (passthrough for header mutations and heartbeat writes).
|
|
792
792
|
* @returns An Observable of `MessageEvent` values for NestJS `@Sse` to stream.
|
|
793
793
|
* @throws UnauthorizedException when authentication returns null.
|
|
794
794
|
*/
|
|
@@ -1451,17 +1451,26 @@ BymaxRealtimeModule = __decorateClass([
|
|
|
1451
1451
|
|
|
1452
1452
|
// src/server/pubsub/redis-realtime-pubsub.ts
|
|
1453
1453
|
var RedisRealtimePubSub = class {
|
|
1454
|
-
|
|
1454
|
+
/**
|
|
1455
|
+
* The publishing Redis client.
|
|
1456
|
+
*
|
|
1457
|
+
* An ECMAScript private field for the same reason as the subscriber below: an
|
|
1458
|
+
* ioredis instance carries `options.password` as a plain field, and a
|
|
1459
|
+
* TypeScript `private` one is erased at runtime, leaving it enumerable to
|
|
1460
|
+
* anything that serializes this pubsub adapter.
|
|
1461
|
+
*/
|
|
1462
|
+
#pub;
|
|
1455
1463
|
channel;
|
|
1456
|
-
|
|
1464
|
+
/** The lazily created subscriber connection, which carries the same credentials. */
|
|
1465
|
+
#subInit = null;
|
|
1457
1466
|
handlers = /* @__PURE__ */ new Set();
|
|
1458
1467
|
constructor(options) {
|
|
1459
|
-
this
|
|
1468
|
+
this.#pub = options.client;
|
|
1460
1469
|
this.channel = options.channel ?? "bymax:realtime";
|
|
1461
1470
|
}
|
|
1462
1471
|
/** JSON-encodes the message and PUBLISHes it to the configured channel. */
|
|
1463
1472
|
async publish(message) {
|
|
1464
|
-
await this
|
|
1473
|
+
await this.#pub.publish(this.channel, JSON.stringify(message));
|
|
1465
1474
|
}
|
|
1466
1475
|
/**
|
|
1467
1476
|
* Lazily creates the single shared subscribe client on first call; returns an async
|
|
@@ -1480,13 +1489,13 @@ var RedisRealtimePubSub = class {
|
|
|
1480
1489
|
sub = await this.ensureSubscriber();
|
|
1481
1490
|
} catch (err) {
|
|
1482
1491
|
this.handlers.delete(handler);
|
|
1483
|
-
this
|
|
1492
|
+
this.#subInit = null;
|
|
1484
1493
|
throw err;
|
|
1485
1494
|
}
|
|
1486
1495
|
return async () => {
|
|
1487
1496
|
this.handlers.delete(handler);
|
|
1488
1497
|
if (this.handlers.size === 0) {
|
|
1489
|
-
this
|
|
1498
|
+
this.#subInit = null;
|
|
1490
1499
|
try {
|
|
1491
1500
|
await sub.quit();
|
|
1492
1501
|
} catch {
|
|
@@ -1496,11 +1505,11 @@ var RedisRealtimePubSub = class {
|
|
|
1496
1505
|
}
|
|
1497
1506
|
/** Lazily create the single shared subscribe client (idempotent under concurrent calls). */
|
|
1498
1507
|
ensureSubscriber() {
|
|
1499
|
-
if (!this
|
|
1500
|
-
return this
|
|
1508
|
+
if (!this.#subInit) this.#subInit = this.createSubscriber();
|
|
1509
|
+
return this.#subInit;
|
|
1501
1510
|
}
|
|
1502
1511
|
async createSubscriber() {
|
|
1503
|
-
const sub = this
|
|
1512
|
+
const sub = this.#pub.duplicate();
|
|
1504
1513
|
await sub.subscribe(this.channel);
|
|
1505
1514
|
sub.on("message", (_ch, payload) => this.dispatch(payload));
|
|
1506
1515
|
return sub;
|
|
@@ -1523,7 +1532,16 @@ var RedisRealtimePubSub = class {
|
|
|
1523
1532
|
|
|
1524
1533
|
// src/server/offline-queue/redis-offline-queue.ts
|
|
1525
1534
|
var RedisOfflineQueue = class _RedisOfflineQueue {
|
|
1526
|
-
|
|
1535
|
+
/**
|
|
1536
|
+
* The Redis client.
|
|
1537
|
+
*
|
|
1538
|
+
* An ECMAScript private field rather than a TypeScript `private` one, which
|
|
1539
|
+
* is erased at runtime: an ioredis instance carries `options.password` as a
|
|
1540
|
+
* plain field, so leaving this enumerable would let anything that serializes
|
|
1541
|
+
* this queue — or the module options that reference it — reach the Redis
|
|
1542
|
+
* credentials.
|
|
1543
|
+
*/
|
|
1544
|
+
#client;
|
|
1527
1545
|
maxPerUser;
|
|
1528
1546
|
ttlSeconds;
|
|
1529
1547
|
/** Zero-pad width for the epoch-millisecond component of the sort key. */
|
|
@@ -1531,7 +1549,7 @@ var RedisOfflineQueue = class _RedisOfflineQueue {
|
|
|
1531
1549
|
/** Zero-pad width for the intra-millisecond counter component of the sort key. */
|
|
1532
1550
|
static COUNTER_PAD = 6;
|
|
1533
1551
|
constructor(options) {
|
|
1534
|
-
this
|
|
1552
|
+
this.#client = options.client;
|
|
1535
1553
|
this.maxPerUser = options.maxPerUser ?? 500;
|
|
1536
1554
|
this.ttlSeconds = options.ttlSeconds ?? 3600;
|
|
1537
1555
|
}
|
|
@@ -1570,7 +1588,7 @@ var RedisOfflineQueue = class _RedisOfflineQueue {
|
|
|
1570
1588
|
}
|
|
1571
1589
|
async append(userId, event) {
|
|
1572
1590
|
const key = this.key(userId);
|
|
1573
|
-
const pipeline = this
|
|
1591
|
+
const pipeline = this.#client.pipeline();
|
|
1574
1592
|
pipeline.zadd(key, 0, this.encodeMember(event));
|
|
1575
1593
|
pipeline.zremrangebyrank(key, 0, -(this.maxPerUser + 1));
|
|
1576
1594
|
pipeline.expire(key, this.ttlSeconds);
|
|
@@ -1581,7 +1599,7 @@ var RedisOfflineQueue = class _RedisOfflineQueue {
|
|
|
1581
1599
|
}
|
|
1582
1600
|
async retrieveSince(userId, sinceId, limit) {
|
|
1583
1601
|
const lowerBound = `[${this.lexKeyNext(sinceId)}`;
|
|
1584
|
-
const raw = await this
|
|
1602
|
+
const raw = await this.#client.zrangebylex(this.key(userId), lowerBound, "+", "LIMIT", 0, limit);
|
|
1585
1603
|
return raw.map(
|
|
1586
1604
|
(entry) => JSON.parse(
|
|
1587
1605
|
entry.slice(entry.indexOf("|") + 1),
|
|
@@ -1591,9 +1609,9 @@ var RedisOfflineQueue = class _RedisOfflineQueue {
|
|
|
1591
1609
|
}
|
|
1592
1610
|
async acknowledge(userId, upToId) {
|
|
1593
1611
|
const upperBound = `(${this.lexKeyNext(upToId)}`;
|
|
1594
|
-
const toRemove = await this
|
|
1612
|
+
const toRemove = await this.#client.zrangebylex(this.key(userId), "-", upperBound);
|
|
1595
1613
|
if (toRemove.length > 0) {
|
|
1596
|
-
await this
|
|
1614
|
+
await this.#client.zrem(this.key(userId), ...toRemove);
|
|
1597
1615
|
}
|
|
1598
1616
|
}
|
|
1599
1617
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bymax-one/nest-realtime",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Realtime backend → frontend communication for NestJS — dual-transport SSE (default) and WebSocket (opt-in) with a unified server-side API.",
|
|
5
5
|
"author": "Bymax One <support@bymax.one>",
|
|
6
6
|
"license": "MIT",
|