@graphql-hive/pubsub 2.1.1 → 2.2.0-alpha-cf04a323a695c2668e4b2edf5e8755dafaf41e8f
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 +46 -0
- package/dist/index.cjs +9 -7
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -7
- package/dist/nats-jetstream.cjs +135 -0
- package/dist/nats-jetstream.d.cts +70 -0
- package/dist/nats-jetstream.d.ts +70 -0
- package/dist/nats-jetstream.js +133 -0
- package/dist/nats.d.cts +1 -1
- package/dist/nats.d.ts +1 -1
- package/dist/{pubsub-Ds7L5Aet.d.ts → pubsub-VtDb_TZC.d.ts} +4 -2
- package/dist/redis.d.cts +1 -1
- package/dist/redis.d.ts +1 -1
- package/package.json +20 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,51 @@
|
|
|
1
1
|
# @graphql-hive/pubsub
|
|
2
2
|
|
|
3
|
+
## 2.2.0-alpha-cf04a323a695c2668e4b2edf5e8755dafaf41e8f
|
|
4
|
+
### Minor Changes
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
- [#2473](https://github.com/graphql-hive/gateway/pull/2473) [`cf04a32`](https://github.com/graphql-hive/gateway/commit/cf04a323a695c2668e4b2edf5e8755dafaf41e8f) Thanks [@enisdenjo](https://github.com/enisdenjo)! - Add `NATSJetStreamPubSub`, a NATS JetStream transport with optional cursor-based replay
|
|
9
|
+
|
|
10
|
+
Subscribe like any other Hive PubSub, or pass subscribe options to receive each message with an opaque `cursor`. Pass that cursor on a later subscription to resume right after it and recover events missed while disconnected. Without a cursor, only messages published after the subscription starts are delivered.
|
|
11
|
+
|
|
12
|
+
The JetStream stream is not created or configured by this transport; it must already exist and capture the subjects used by the pubsub (`${subjectPrefix}:${topic}`).
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { connect } from '@nats-io/transport-node';
|
|
16
|
+
import { NATSJetStreamPubSub } from '@graphql-hive/pubsub/nats-jetstream';
|
|
17
|
+
|
|
18
|
+
const nats = await connect({ servers: 'localhost:4222' });
|
|
19
|
+
const pubsub = new NATSJetStreamPubSub(nats, {
|
|
20
|
+
subjectPrefix: 'my-service',
|
|
21
|
+
stream: 'EVENTS',
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// live-only: no cursor, only messages published after subscribe starts
|
|
25
|
+
const live = pubsub.subscribe('personCreated', { cursor: undefined });
|
|
26
|
+
const first = await live.next();
|
|
27
|
+
// first.value => { data: { id: '1' }, cursor: '...' }
|
|
28
|
+
await live.return?.();
|
|
29
|
+
|
|
30
|
+
// published while disconnected is retained by JetStream
|
|
31
|
+
await pubsub.publish('personCreated', { id: '2' });
|
|
32
|
+
|
|
33
|
+
// resume right after the last seen cursor — gets { id: '2' }, not a repeat of '1'
|
|
34
|
+
for await (const { data, cursor } of pubsub.subscribe('personCreated', {
|
|
35
|
+
cursor: first.value.cursor,
|
|
36
|
+
})) {
|
|
37
|
+
// persist `cursor` so the next reconnect can resume again
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Patch Changes
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
- [#2473](https://github.com/graphql-hive/gateway/pull/2473) [`34fc18c`](https://github.com/graphql-hive/gateway/commit/34fc18cf61a5b346dface9b24c690f83ad976cd2) Thanks [@enisdenjo](https://github.com/enisdenjo)! - dependencies updates:
|
|
46
|
+
|
|
47
|
+
- Added dependency [`@nats-io/jetstream@^3` ↗︎](https://www.npmjs.com/package/@nats-io/jetstream/v/3.0.0) (to `peerDependencies`)
|
|
48
|
+
|
|
3
49
|
## 2.1.1
|
|
4
50
|
### Patch Changes
|
|
5
51
|
|
package/dist/index.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var repeater = require('@repeaterjs/repeater');
|
|
4
4
|
var disposablestack = require('@whatwg-node/disposablestack');
|
|
5
|
+
var promiseHelpers = require('@whatwg-node/promise-helpers');
|
|
5
6
|
|
|
6
7
|
class MemPubSub {
|
|
7
8
|
#disposed = false;
|
|
@@ -30,13 +31,14 @@ class MemPubSub {
|
|
|
30
31
|
this.#subscribers.set(topic, listeners);
|
|
31
32
|
}
|
|
32
33
|
if (!listener) {
|
|
33
|
-
return new repeater.Repeater(
|
|
34
|
+
return new repeater.Repeater((push, stop) => {
|
|
34
35
|
listeners.set(push, stop);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
return stop.then(() => {
|
|
37
|
+
listeners.delete(push);
|
|
38
|
+
if (listeners.size === 0) {
|
|
39
|
+
this.#subscribers.delete(topic);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
40
42
|
});
|
|
41
43
|
}
|
|
42
44
|
const listenerRef = new WeakRef(listener);
|
|
@@ -61,7 +63,7 @@ class MemPubSub {
|
|
|
61
63
|
this.#subscribers.clear();
|
|
62
64
|
}
|
|
63
65
|
[disposablestack.DisposableSymbols.asyncDispose]() {
|
|
64
|
-
return
|
|
66
|
+
return promiseHelpers.fakePromise(this.dispose());
|
|
65
67
|
}
|
|
66
68
|
}
|
|
67
69
|
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-
|
|
1
|
+
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-VtDb_TZC.js';
|
|
2
2
|
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
3
3
|
import { MaybePromise } from '@whatwg-node/promise-helpers';
|
|
4
4
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-
|
|
1
|
+
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-VtDb_TZC.js';
|
|
2
2
|
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
3
3
|
import { MaybePromise } from '@whatwg-node/promise-helpers';
|
|
4
4
|
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Repeater } from '@repeaterjs/repeater';
|
|
2
2
|
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
3
|
+
import { fakePromise } from '@whatwg-node/promise-helpers';
|
|
3
4
|
|
|
4
5
|
class MemPubSub {
|
|
5
6
|
#disposed = false;
|
|
@@ -28,13 +29,14 @@ class MemPubSub {
|
|
|
28
29
|
this.#subscribers.set(topic, listeners);
|
|
29
30
|
}
|
|
30
31
|
if (!listener) {
|
|
31
|
-
return new Repeater(
|
|
32
|
+
return new Repeater((push, stop) => {
|
|
32
33
|
listeners.set(push, stop);
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
return stop.then(() => {
|
|
35
|
+
listeners.delete(push);
|
|
36
|
+
if (listeners.size === 0) {
|
|
37
|
+
this.#subscribers.delete(topic);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
38
40
|
});
|
|
39
41
|
}
|
|
40
42
|
const listenerRef = new WeakRef(listener);
|
|
@@ -59,7 +61,7 @@ class MemPubSub {
|
|
|
59
61
|
this.#subscribers.clear();
|
|
60
62
|
}
|
|
61
63
|
[DisposableSymbols.asyncDispose]() {
|
|
62
|
-
return
|
|
64
|
+
return fakePromise(this.dispose());
|
|
63
65
|
}
|
|
64
66
|
}
|
|
65
67
|
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jetstream = require('@nats-io/jetstream');
|
|
4
|
+
var repeater = require('@repeaterjs/repeater');
|
|
5
|
+
var disposablestack = require('@whatwg-node/disposablestack');
|
|
6
|
+
var promiseHelpers = require('@whatwg-node/promise-helpers');
|
|
7
|
+
|
|
8
|
+
class NATSJetStreamPubSub {
|
|
9
|
+
#disposed = false;
|
|
10
|
+
#closeOnDispose;
|
|
11
|
+
#nats;
|
|
12
|
+
#js;
|
|
13
|
+
#subjectPrefix;
|
|
14
|
+
#stream;
|
|
15
|
+
#activeConsumers = /* @__PURE__ */ new Map();
|
|
16
|
+
constructor(nats, options) {
|
|
17
|
+
this.#nats = nats;
|
|
18
|
+
this.#subjectPrefix = options.subjectPrefix;
|
|
19
|
+
this.#stream = options.stream;
|
|
20
|
+
this.#closeOnDispose = !options.noCloseOnDispose;
|
|
21
|
+
if (String(this.#subjectPrefix || "").trim() === "") {
|
|
22
|
+
throw new Error("NATSJetStreamPubSub requires a non-empty subjectPrefix");
|
|
23
|
+
}
|
|
24
|
+
if (String(this.#stream || "").trim() === "") {
|
|
25
|
+
throw new Error("NATSJetStreamPubSub requires a non-empty stream");
|
|
26
|
+
}
|
|
27
|
+
this.#js = jetstream.jetstream(nats);
|
|
28
|
+
}
|
|
29
|
+
#topicToSubject(topic) {
|
|
30
|
+
return `${this.#subjectPrefix}:${String(topic)}`;
|
|
31
|
+
}
|
|
32
|
+
/** Cursors are opaque outside of this adapter, they're simply the stream's message sequence. */
|
|
33
|
+
#parseCursor(cursor) {
|
|
34
|
+
const seq = Number(cursor);
|
|
35
|
+
if (!Number.isInteger(seq) || seq <= 0) {
|
|
36
|
+
throw new Error(`Invalid cursor "${cursor}"`);
|
|
37
|
+
}
|
|
38
|
+
return seq;
|
|
39
|
+
}
|
|
40
|
+
async subscribedTopics() {
|
|
41
|
+
const distinctTopics = [];
|
|
42
|
+
for (const topic of this.#activeConsumers.values()) {
|
|
43
|
+
if (!distinctTopics.includes(topic)) {
|
|
44
|
+
distinctTopics.push(topic);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return distinctTopics;
|
|
48
|
+
}
|
|
49
|
+
publish(topic, data) {
|
|
50
|
+
if (this.#disposed) {
|
|
51
|
+
throw new Error("PubSub is disposed, cannot publish data");
|
|
52
|
+
}
|
|
53
|
+
return this.#js.publish(this.#topicToSubject(topic), JSON.stringify(data)).then(() => void 0);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* An ordered consumer is a fresh ephemeral consumer, exactly what we want for a resumable
|
|
57
|
+
* per-subscription cursor: no shared/durable state between subscribers.
|
|
58
|
+
*/
|
|
59
|
+
#createConsumer(topic, cursor) {
|
|
60
|
+
return this.#js.consumers.get(this.#stream, {
|
|
61
|
+
filter_subjects: [this.#topicToSubject(topic)],
|
|
62
|
+
...cursor === void 0 ? { deliver_policy: jetstream.DeliverPolicy.New } : {
|
|
63
|
+
deliver_policy: jetstream.DeliverPolicy.StartSequence,
|
|
64
|
+
opt_start_seq: this.#parseCursor(cursor) + 1
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
async #consume(topic, cursor, callback, finished) {
|
|
69
|
+
const consumer = await this.#createConsumer(topic, cursor);
|
|
70
|
+
const messages = await consumer.consume({ callback });
|
|
71
|
+
const stop = async () => {
|
|
72
|
+
if (this.#activeConsumers.delete(stop)) {
|
|
73
|
+
try {
|
|
74
|
+
await messages.close();
|
|
75
|
+
} finally {
|
|
76
|
+
finished?.();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
this.#activeConsumers.set(stop, topic);
|
|
81
|
+
return stop;
|
|
82
|
+
}
|
|
83
|
+
subscribe(topic, optionsOrListener) {
|
|
84
|
+
if (this.#disposed) {
|
|
85
|
+
throw new Error("PubSub is disposed, cannot subscribe to topics");
|
|
86
|
+
}
|
|
87
|
+
if (typeof optionsOrListener === "function") {
|
|
88
|
+
const listenerRef = {
|
|
89
|
+
ref: optionsOrListener
|
|
90
|
+
};
|
|
91
|
+
const stop = this.#consume(
|
|
92
|
+
topic,
|
|
93
|
+
void 0,
|
|
94
|
+
(msg) => listenerRef.ref?.(msg.json())
|
|
95
|
+
);
|
|
96
|
+
return promiseHelpers.fakePromise(stop).then((stop2) => async () => {
|
|
97
|
+
listenerRef.ref = null;
|
|
98
|
+
await stop2();
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return new repeater.Repeater(
|
|
102
|
+
async (push, stopped) => {
|
|
103
|
+
const stop = await this.#consume(
|
|
104
|
+
topic,
|
|
105
|
+
optionsOrListener?.cursor,
|
|
106
|
+
(msg) => {
|
|
107
|
+
const item = {
|
|
108
|
+
data: msg.json(),
|
|
109
|
+
cursor: String(msg.seq)
|
|
110
|
+
};
|
|
111
|
+
void push(item);
|
|
112
|
+
},
|
|
113
|
+
stopped
|
|
114
|
+
);
|
|
115
|
+
await stopped;
|
|
116
|
+
await stop();
|
|
117
|
+
}
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
async dispose() {
|
|
121
|
+
this.#disposed = true;
|
|
122
|
+
await Promise.all(
|
|
123
|
+
Array.from(this.#activeConsumers.keys()).map((stop) => stop())
|
|
124
|
+
);
|
|
125
|
+
this.#activeConsumers.clear();
|
|
126
|
+
if (this.#closeOnDispose) {
|
|
127
|
+
await this.#nats.close();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
[disposablestack.DisposableSymbols.asyncDispose]() {
|
|
131
|
+
return this.dispose();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
exports.NATSJetStreamPubSub = NATSJetStreamPubSub;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { NatsConnection } from '@nats-io/nats-core';
|
|
2
|
+
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
3
|
+
import { MaybePromise } from '@whatwg-node/promise-helpers';
|
|
4
|
+
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-VtDb_TZC.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Envelope yielded by {@link NATSJetStreamPubSub} subscriptions, pairing the published
|
|
8
|
+
* {@link TopicDataMap data} with an opaque replay `cursor`.
|
|
9
|
+
*/
|
|
10
|
+
type JetStreamTopicDataMap<M extends TopicDataMap> = {
|
|
11
|
+
[Topic in keyof M]: {
|
|
12
|
+
data: M[Topic];
|
|
13
|
+
cursor: string;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
/** Subscribe options for {@link NATSJetStreamPubSub}, omit to only receive new messages. */
|
|
17
|
+
type JetStreamSubscribeOptions = {
|
|
18
|
+
/**
|
|
19
|
+
* Opaque cursor returned by a previous subscription's message, replay resumes right after it.
|
|
20
|
+
* Pass `undefined` to only receive messages published after the subscription starts.
|
|
21
|
+
*/
|
|
22
|
+
cursor: string | undefined;
|
|
23
|
+
};
|
|
24
|
+
interface NATSJetStreamPubSubOptions {
|
|
25
|
+
/**
|
|
26
|
+
* Prefix for NATS publish subjects to avoid conflicts.
|
|
27
|
+
* Intentionally no default because we don't want to accidentally share channels between different services.
|
|
28
|
+
*/
|
|
29
|
+
subjectPrefix: string;
|
|
30
|
+
/**
|
|
31
|
+
* Name of the JetStream stream to publish and subscribe through.
|
|
32
|
+
*
|
|
33
|
+
* The stream is not created nor configured by this pub/sub, it must already exist and be
|
|
34
|
+
* configured to capture the subjects used by this pub/sub (i.e. `${subjectPrefix}:${topic}`).
|
|
35
|
+
*/
|
|
36
|
+
stream: string;
|
|
37
|
+
/**
|
|
38
|
+
* By default, when the pub/sub instance is disposed, it will call
|
|
39
|
+
* `close` on the NATS connection. Set this to `true` if you
|
|
40
|
+
* want to keep the connection alive after disposal.
|
|
41
|
+
*
|
|
42
|
+
* This might be useful if you want to manage the NATS connection's lifecycle
|
|
43
|
+
* outside of the pub/sub instance.
|
|
44
|
+
*
|
|
45
|
+
* @default false
|
|
46
|
+
*/
|
|
47
|
+
noCloseOnDispose?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* {@link PubSub Hive PubSub} implementation using [NATS JetStream](https://docs.nats.io/nats-concepts/jetstream)
|
|
51
|
+
* persisted streams.
|
|
52
|
+
*
|
|
53
|
+
* Subscribe normally to receive published data like any other {@link PubSub}. Pass subscribe
|
|
54
|
+
* options to receive the data alongside an opaque `cursor`. Passing that cursor back on a later
|
|
55
|
+
* subscription resumes delivery right after it, allowing subscribers to recover events missed
|
|
56
|
+
* while disconnected.
|
|
57
|
+
*/
|
|
58
|
+
declare class NATSJetStreamPubSub<M extends TopicDataMap = TopicDataMap> implements PubSub<M, JetStreamSubscribeOptions> {
|
|
59
|
+
#private;
|
|
60
|
+
constructor(nats: NatsConnection, options: NATSJetStreamPubSubOptions);
|
|
61
|
+
subscribedTopics(): Promise<(keyof M)[]>;
|
|
62
|
+
publish<Topic extends keyof M>(topic: Topic, data: M[Topic]): Promise<undefined>;
|
|
63
|
+
subscribe<Topic extends keyof M>(topic: Topic): AsyncIterable<M[Topic]>;
|
|
64
|
+
subscribe<Topic extends keyof M>(topic: Topic, options: JetStreamSubscribeOptions): AsyncIterable<JetStreamTopicDataMap<M>[Topic]>;
|
|
65
|
+
subscribe<Topic extends keyof M>(topic: Topic, listener: PubSubListener<M, Topic>): MaybePromise<() => MaybePromise<void>>;
|
|
66
|
+
dispose(): Promise<void>;
|
|
67
|
+
[DisposableSymbols.asyncDispose](): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export { type JetStreamSubscribeOptions, type JetStreamTopicDataMap, NATSJetStreamPubSub, type NATSJetStreamPubSubOptions };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { NatsConnection } from '@nats-io/nats-core';
|
|
2
|
+
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
3
|
+
import { MaybePromise } from '@whatwg-node/promise-helpers';
|
|
4
|
+
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-VtDb_TZC.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Envelope yielded by {@link NATSJetStreamPubSub} subscriptions, pairing the published
|
|
8
|
+
* {@link TopicDataMap data} with an opaque replay `cursor`.
|
|
9
|
+
*/
|
|
10
|
+
type JetStreamTopicDataMap<M extends TopicDataMap> = {
|
|
11
|
+
[Topic in keyof M]: {
|
|
12
|
+
data: M[Topic];
|
|
13
|
+
cursor: string;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
/** Subscribe options for {@link NATSJetStreamPubSub}, omit to only receive new messages. */
|
|
17
|
+
type JetStreamSubscribeOptions = {
|
|
18
|
+
/**
|
|
19
|
+
* Opaque cursor returned by a previous subscription's message, replay resumes right after it.
|
|
20
|
+
* Pass `undefined` to only receive messages published after the subscription starts.
|
|
21
|
+
*/
|
|
22
|
+
cursor: string | undefined;
|
|
23
|
+
};
|
|
24
|
+
interface NATSJetStreamPubSubOptions {
|
|
25
|
+
/**
|
|
26
|
+
* Prefix for NATS publish subjects to avoid conflicts.
|
|
27
|
+
* Intentionally no default because we don't want to accidentally share channels between different services.
|
|
28
|
+
*/
|
|
29
|
+
subjectPrefix: string;
|
|
30
|
+
/**
|
|
31
|
+
* Name of the JetStream stream to publish and subscribe through.
|
|
32
|
+
*
|
|
33
|
+
* The stream is not created nor configured by this pub/sub, it must already exist and be
|
|
34
|
+
* configured to capture the subjects used by this pub/sub (i.e. `${subjectPrefix}:${topic}`).
|
|
35
|
+
*/
|
|
36
|
+
stream: string;
|
|
37
|
+
/**
|
|
38
|
+
* By default, when the pub/sub instance is disposed, it will call
|
|
39
|
+
* `close` on the NATS connection. Set this to `true` if you
|
|
40
|
+
* want to keep the connection alive after disposal.
|
|
41
|
+
*
|
|
42
|
+
* This might be useful if you want to manage the NATS connection's lifecycle
|
|
43
|
+
* outside of the pub/sub instance.
|
|
44
|
+
*
|
|
45
|
+
* @default false
|
|
46
|
+
*/
|
|
47
|
+
noCloseOnDispose?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* {@link PubSub Hive PubSub} implementation using [NATS JetStream](https://docs.nats.io/nats-concepts/jetstream)
|
|
51
|
+
* persisted streams.
|
|
52
|
+
*
|
|
53
|
+
* Subscribe normally to receive published data like any other {@link PubSub}. Pass subscribe
|
|
54
|
+
* options to receive the data alongside an opaque `cursor`. Passing that cursor back on a later
|
|
55
|
+
* subscription resumes delivery right after it, allowing subscribers to recover events missed
|
|
56
|
+
* while disconnected.
|
|
57
|
+
*/
|
|
58
|
+
declare class NATSJetStreamPubSub<M extends TopicDataMap = TopicDataMap> implements PubSub<M, JetStreamSubscribeOptions> {
|
|
59
|
+
#private;
|
|
60
|
+
constructor(nats: NatsConnection, options: NATSJetStreamPubSubOptions);
|
|
61
|
+
subscribedTopics(): Promise<(keyof M)[]>;
|
|
62
|
+
publish<Topic extends keyof M>(topic: Topic, data: M[Topic]): Promise<undefined>;
|
|
63
|
+
subscribe<Topic extends keyof M>(topic: Topic): AsyncIterable<M[Topic]>;
|
|
64
|
+
subscribe<Topic extends keyof M>(topic: Topic, options: JetStreamSubscribeOptions): AsyncIterable<JetStreamTopicDataMap<M>[Topic]>;
|
|
65
|
+
subscribe<Topic extends keyof M>(topic: Topic, listener: PubSubListener<M, Topic>): MaybePromise<() => MaybePromise<void>>;
|
|
66
|
+
dispose(): Promise<void>;
|
|
67
|
+
[DisposableSymbols.asyncDispose](): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export { type JetStreamSubscribeOptions, type JetStreamTopicDataMap, NATSJetStreamPubSub, type NATSJetStreamPubSubOptions };
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { jetstream, DeliverPolicy } from '@nats-io/jetstream';
|
|
2
|
+
import { Repeater } from '@repeaterjs/repeater';
|
|
3
|
+
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
4
|
+
import { fakePromise } from '@whatwg-node/promise-helpers';
|
|
5
|
+
|
|
6
|
+
class NATSJetStreamPubSub {
|
|
7
|
+
#disposed = false;
|
|
8
|
+
#closeOnDispose;
|
|
9
|
+
#nats;
|
|
10
|
+
#js;
|
|
11
|
+
#subjectPrefix;
|
|
12
|
+
#stream;
|
|
13
|
+
#activeConsumers = /* @__PURE__ */ new Map();
|
|
14
|
+
constructor(nats, options) {
|
|
15
|
+
this.#nats = nats;
|
|
16
|
+
this.#subjectPrefix = options.subjectPrefix;
|
|
17
|
+
this.#stream = options.stream;
|
|
18
|
+
this.#closeOnDispose = !options.noCloseOnDispose;
|
|
19
|
+
if (String(this.#subjectPrefix || "").trim() === "") {
|
|
20
|
+
throw new Error("NATSJetStreamPubSub requires a non-empty subjectPrefix");
|
|
21
|
+
}
|
|
22
|
+
if (String(this.#stream || "").trim() === "") {
|
|
23
|
+
throw new Error("NATSJetStreamPubSub requires a non-empty stream");
|
|
24
|
+
}
|
|
25
|
+
this.#js = jetstream(nats);
|
|
26
|
+
}
|
|
27
|
+
#topicToSubject(topic) {
|
|
28
|
+
return `${this.#subjectPrefix}:${String(topic)}`;
|
|
29
|
+
}
|
|
30
|
+
/** Cursors are opaque outside of this adapter, they're simply the stream's message sequence. */
|
|
31
|
+
#parseCursor(cursor) {
|
|
32
|
+
const seq = Number(cursor);
|
|
33
|
+
if (!Number.isInteger(seq) || seq <= 0) {
|
|
34
|
+
throw new Error(`Invalid cursor "${cursor}"`);
|
|
35
|
+
}
|
|
36
|
+
return seq;
|
|
37
|
+
}
|
|
38
|
+
async subscribedTopics() {
|
|
39
|
+
const distinctTopics = [];
|
|
40
|
+
for (const topic of this.#activeConsumers.values()) {
|
|
41
|
+
if (!distinctTopics.includes(topic)) {
|
|
42
|
+
distinctTopics.push(topic);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return distinctTopics;
|
|
46
|
+
}
|
|
47
|
+
publish(topic, data) {
|
|
48
|
+
if (this.#disposed) {
|
|
49
|
+
throw new Error("PubSub is disposed, cannot publish data");
|
|
50
|
+
}
|
|
51
|
+
return this.#js.publish(this.#topicToSubject(topic), JSON.stringify(data)).then(() => void 0);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* An ordered consumer is a fresh ephemeral consumer, exactly what we want for a resumable
|
|
55
|
+
* per-subscription cursor: no shared/durable state between subscribers.
|
|
56
|
+
*/
|
|
57
|
+
#createConsumer(topic, cursor) {
|
|
58
|
+
return this.#js.consumers.get(this.#stream, {
|
|
59
|
+
filter_subjects: [this.#topicToSubject(topic)],
|
|
60
|
+
...cursor === void 0 ? { deliver_policy: DeliverPolicy.New } : {
|
|
61
|
+
deliver_policy: DeliverPolicy.StartSequence,
|
|
62
|
+
opt_start_seq: this.#parseCursor(cursor) + 1
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
async #consume(topic, cursor, callback, finished) {
|
|
67
|
+
const consumer = await this.#createConsumer(topic, cursor);
|
|
68
|
+
const messages = await consumer.consume({ callback });
|
|
69
|
+
const stop = async () => {
|
|
70
|
+
if (this.#activeConsumers.delete(stop)) {
|
|
71
|
+
try {
|
|
72
|
+
await messages.close();
|
|
73
|
+
} finally {
|
|
74
|
+
finished?.();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
this.#activeConsumers.set(stop, topic);
|
|
79
|
+
return stop;
|
|
80
|
+
}
|
|
81
|
+
subscribe(topic, optionsOrListener) {
|
|
82
|
+
if (this.#disposed) {
|
|
83
|
+
throw new Error("PubSub is disposed, cannot subscribe to topics");
|
|
84
|
+
}
|
|
85
|
+
if (typeof optionsOrListener === "function") {
|
|
86
|
+
const listenerRef = {
|
|
87
|
+
ref: optionsOrListener
|
|
88
|
+
};
|
|
89
|
+
const stop = this.#consume(
|
|
90
|
+
topic,
|
|
91
|
+
void 0,
|
|
92
|
+
(msg) => listenerRef.ref?.(msg.json())
|
|
93
|
+
);
|
|
94
|
+
return fakePromise(stop).then((stop2) => async () => {
|
|
95
|
+
listenerRef.ref = null;
|
|
96
|
+
await stop2();
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return new Repeater(
|
|
100
|
+
async (push, stopped) => {
|
|
101
|
+
const stop = await this.#consume(
|
|
102
|
+
topic,
|
|
103
|
+
optionsOrListener?.cursor,
|
|
104
|
+
(msg) => {
|
|
105
|
+
const item = {
|
|
106
|
+
data: msg.json(),
|
|
107
|
+
cursor: String(msg.seq)
|
|
108
|
+
};
|
|
109
|
+
void push(item);
|
|
110
|
+
},
|
|
111
|
+
stopped
|
|
112
|
+
);
|
|
113
|
+
await stopped;
|
|
114
|
+
await stop();
|
|
115
|
+
}
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
async dispose() {
|
|
119
|
+
this.#disposed = true;
|
|
120
|
+
await Promise.all(
|
|
121
|
+
Array.from(this.#activeConsumers.keys()).map((stop) => stop())
|
|
122
|
+
);
|
|
123
|
+
this.#activeConsumers.clear();
|
|
124
|
+
if (this.#closeOnDispose) {
|
|
125
|
+
await this.#nats.close();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
[DisposableSymbols.asyncDispose]() {
|
|
129
|
+
return this.dispose();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export { NATSJetStreamPubSub };
|
package/dist/nats.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { NatsConnection } from '@nats-io/nats-core';
|
|
2
2
|
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
3
3
|
import { MaybePromise } from '@whatwg-node/promise-helpers';
|
|
4
|
-
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-
|
|
4
|
+
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-VtDb_TZC.js';
|
|
5
5
|
|
|
6
6
|
interface NATSPubSubOptions {
|
|
7
7
|
/**
|
package/dist/nats.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { NatsConnection } from '@nats-io/nats-core';
|
|
2
2
|
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
3
3
|
import { MaybePromise } from '@whatwg-node/promise-helpers';
|
|
4
|
-
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-
|
|
4
|
+
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-VtDb_TZC.js';
|
|
5
5
|
|
|
6
6
|
interface NATSPubSubOptions {
|
|
7
7
|
/**
|
|
@@ -5,7 +5,9 @@ type TopicDataMap = {
|
|
|
5
5
|
[topic: string]: any;
|
|
6
6
|
};
|
|
7
7
|
type PubSubListener<Data extends TopicDataMap, Topic extends keyof Data> = (data: Data[Topic]) => void;
|
|
8
|
-
interface PubSub<M extends TopicDataMap = TopicDataMap
|
|
8
|
+
interface PubSub<M extends TopicDataMap = TopicDataMap, SubscribeOptions extends {
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
} = never> {
|
|
9
11
|
/**
|
|
10
12
|
* Publish {@link data} for a {@link topic}.
|
|
11
13
|
* @returns `void` or a `Promise` that resolves when the data has been successfully published
|
|
@@ -28,7 +30,7 @@ interface PubSub<M extends TopicDataMap = TopicDataMap> {
|
|
|
28
30
|
*
|
|
29
31
|
* @returns an `AsyncIterable` that yields data for the given {@link topic}
|
|
30
32
|
*/
|
|
31
|
-
subscribe<Topic extends keyof M>(topic: Topic): AsyncIterable<M[Topic]>;
|
|
33
|
+
subscribe<Topic extends keyof M>(topic: Topic, ...options: [SubscribeOptions] extends [never] ? [] : [options: SubscribeOptions]): AsyncIterable<M[Topic]>;
|
|
32
34
|
subscribe<Topic extends keyof M>(topic: Topic, listener: PubSubListener<M, Topic>): MaybePromise<() => MaybePromise<void>>;
|
|
33
35
|
/**
|
|
34
36
|
* Closes active subscriptions and disposes of all resources. Publishing and subscribing after disposal
|
package/dist/redis.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
2
2
|
import { MaybePromise } from '@whatwg-node/promise-helpers';
|
|
3
3
|
import { Redis } from 'ioredis';
|
|
4
|
-
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-
|
|
4
|
+
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-VtDb_TZC.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* When a Redis connection enters "subscriber mode" (after calling SUBSCRIBE), it can only execute
|
package/dist/redis.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { DisposableSymbols } from '@whatwg-node/disposablestack';
|
|
2
2
|
import { MaybePromise } from '@whatwg-node/promise-helpers';
|
|
3
3
|
import { Redis } from 'ioredis';
|
|
4
|
-
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-
|
|
4
|
+
import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-VtDb_TZC.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* When a Redis connection enters "subscriber mode" (after calling SUBSCRIBE), it can only execute
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-hive/pubsub",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0-alpha-cf04a323a695c2668e4b2edf5e8755dafaf41e8f",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -49,6 +49,16 @@
|
|
|
49
49
|
"default": "./dist/nats.js"
|
|
50
50
|
}
|
|
51
51
|
},
|
|
52
|
+
"./nats-jetstream": {
|
|
53
|
+
"require": {
|
|
54
|
+
"types": "./dist/nats-jetstream.d.cts",
|
|
55
|
+
"default": "./dist/nats-jetstream.cjs"
|
|
56
|
+
},
|
|
57
|
+
"import": {
|
|
58
|
+
"types": "./dist/nats-jetstream.d.ts",
|
|
59
|
+
"default": "./dist/nats-jetstream.js"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
52
62
|
"./package.json": "./package.json"
|
|
53
63
|
},
|
|
54
64
|
"files": [
|
|
@@ -59,10 +69,14 @@
|
|
|
59
69
|
"prepack": "yarn build"
|
|
60
70
|
},
|
|
61
71
|
"peerDependencies": {
|
|
72
|
+
"@nats-io/jetstream": "^3",
|
|
62
73
|
"@nats-io/nats-core": "^3",
|
|
63
74
|
"ioredis": "^5"
|
|
64
75
|
},
|
|
65
76
|
"peerDependenciesMeta": {
|
|
77
|
+
"@nats-io/jetstream": {
|
|
78
|
+
"optional": true
|
|
79
|
+
},
|
|
66
80
|
"@nats-io/nats-core": {
|
|
67
81
|
"optional": true
|
|
68
82
|
},
|
|
@@ -76,10 +90,11 @@
|
|
|
76
90
|
"@whatwg-node/promise-helpers": "^1.3.2"
|
|
77
91
|
},
|
|
78
92
|
"devDependencies": {
|
|
79
|
-
"@nats-io/
|
|
80
|
-
"@nats-io/
|
|
81
|
-
"
|
|
82
|
-
"
|
|
93
|
+
"@nats-io/jetstream": "^3.4.0",
|
|
94
|
+
"@nats-io/nats-core": "^3.2.0",
|
|
95
|
+
"@nats-io/transport-node": "^3.2.0",
|
|
96
|
+
"ioredis": "^5.8.2",
|
|
97
|
+
"pkgroll": "2.27.1"
|
|
83
98
|
},
|
|
84
99
|
"sideEffects": false
|
|
85
100
|
}
|