@jr200-labs/xstate-nats 0.6.0
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/LICENSE +21 -0
- package/README.md +251 -0
- package/dist/actions/connection.d.ts +28 -0
- package/dist/actions/connection.d.ts.map +1 -0
- package/dist/actions/connection.js +102 -0
- package/dist/actions/connection.js.map +1 -0
- package/dist/actions/kv.d.ts +21 -0
- package/dist/actions/kv.d.ts.map +1 -0
- package/dist/actions/kv.js +66 -0
- package/dist/actions/kv.js.map +1 -0
- package/dist/actions/subject.d.ts +39 -0
- package/dist/actions/subject.d.ts.map +1 -0
- package/dist/actions/subject.js +79 -0
- package/dist/actions/subject.js.map +1 -0
- package/dist/actions/types.d.ts +8 -0
- package/dist/actions/types.d.ts.map +1 -0
- package/dist/actions/types.js +2 -0
- package/dist/actions/types.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/machines/kv.d.ts +190 -0
- package/dist/machines/kv.d.ts.map +1 -0
- package/dist/machines/kv.js +273 -0
- package/dist/machines/kv.js.map +1 -0
- package/dist/machines/root.d.ts +510 -0
- package/dist/machines/root.d.ts.map +1 -0
- package/dist/machines/root.js +245 -0
- package/dist/machines/root.js.map +1 -0
- package/dist/machines/subject.d.ts +95 -0
- package/dist/machines/subject.d.ts.map +1 -0
- package/dist/machines/subject.js +162 -0
- package/dist/machines/subject.js.map +1 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +27 -0
- package/dist/utils.js.map +1 -0
- package/package.json +55 -0
- package/src/actions/connection.test.ts +324 -0
- package/src/actions/connection.ts +135 -0
- package/src/actions/kv.test.ts +439 -0
- package/src/actions/kv.ts +92 -0
- package/src/actions/subject.test.ts +460 -0
- package/src/actions/subject.ts +127 -0
- package/src/actions/types.ts +7 -0
- package/src/index.ts +20 -0
- package/src/machines/kv.test.ts +720 -0
- package/src/machines/kv.ts +327 -0
- package/src/machines/root.test.ts +329 -0
- package/src/machines/root.ts +286 -0
- package/src/machines/subject.test.ts +272 -0
- package/src/machines/subject.ts +205 -0
- package/src/utils.test.ts +35 -0
- package/src/utils.ts +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jayshan Raghunandan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
# @jr200-labs/xstate-nats
|
|
2
|
+
|
|
3
|
+
A state machine library that integrates [XState v5](https://xstate.js.org/) with [NATS](https://nats.io/) messaging system, providing a type-safe way to manage NATS connections, subscriptions, and Key-Value operations.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **State Machine Management**: Built on XState for predictable state transitions and side effects
|
|
8
|
+
- **NATS Integration**: Full support for NATS Core, JetStream, and Key-Value operations
|
|
9
|
+
- **Authentication Support**: Multiple auth types (decentralised, userpass, token)
|
|
10
|
+
- **Connection Management**: Automatic connection handling with retry logic and error recovery
|
|
11
|
+
- **Subject Management**: Subscribe, publish, and request-reply operations with state tracking
|
|
12
|
+
- **Key-Value Store**: KV bucket and key management with real-time subscriptions
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pnpm add @jr200-labs/xstate-nats
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
### Basic Setup
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { natsMachine } from '@jr200-labs/xstate-nats'
|
|
26
|
+
import { useActor } from '@xstate/react'
|
|
27
|
+
|
|
28
|
+
function MyComponent() {
|
|
29
|
+
const [state, send] = useActor(natsMachine)
|
|
30
|
+
|
|
31
|
+
const connect = () => {
|
|
32
|
+
send({
|
|
33
|
+
type: 'CONFIGURE',
|
|
34
|
+
config: {
|
|
35
|
+
opts: {
|
|
36
|
+
servers: ['nats://localhost:4222'],
|
|
37
|
+
},
|
|
38
|
+
auth: {
|
|
39
|
+
type: 'userpass',
|
|
40
|
+
user: 'myuser',
|
|
41
|
+
pass: 'mypass',
|
|
42
|
+
},
|
|
43
|
+
maxRetries: 3,
|
|
44
|
+
},
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
send({ type: 'CONNECT' })
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<div>
|
|
52
|
+
<p>Status: {state.value}</p>
|
|
53
|
+
<button onClick={connect}>Connect</button>
|
|
54
|
+
</div>
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Subject Operations
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
// Subscribe to a subject
|
|
63
|
+
send({
|
|
64
|
+
type: 'SUBJECT.SUBSCRIBE',
|
|
65
|
+
config: {
|
|
66
|
+
subject: 'user.events',
|
|
67
|
+
callback: (data) => {
|
|
68
|
+
console.log('Received:', data)
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
// Publish to a subject
|
|
74
|
+
send({
|
|
75
|
+
type: 'SUBJECT.PUBLISH',
|
|
76
|
+
subject: 'user.events',
|
|
77
|
+
payload: { userId: 123, action: 'login' },
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
// Request-reply pattern
|
|
81
|
+
send({
|
|
82
|
+
type: 'SUBJECT.REQUEST',
|
|
83
|
+
subject: 'user.get',
|
|
84
|
+
payload: { userId: 123 },
|
|
85
|
+
callback: (reply) => {
|
|
86
|
+
console.log('Reply:', reply)
|
|
87
|
+
},
|
|
88
|
+
})
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Key-Value Operations
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
// Create a KV bucket
|
|
95
|
+
send({
|
|
96
|
+
type: 'KV.BUCKET_CREATE',
|
|
97
|
+
bucket: 'user-sessions',
|
|
98
|
+
onResult: (result) => {
|
|
99
|
+
if (result.ok) {
|
|
100
|
+
console.log('Bucket created successfully')
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
// Put a value
|
|
106
|
+
send({
|
|
107
|
+
type: 'KV.PUT',
|
|
108
|
+
bucket: 'user-sessions',
|
|
109
|
+
key: 'user-123',
|
|
110
|
+
value: { sessionId: 'abc123', expiresAt: Date.now() },
|
|
111
|
+
onResult: (result) => {
|
|
112
|
+
if (result.ok) {
|
|
113
|
+
console.log('Value stored successfully')
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
// Get a value
|
|
119
|
+
send({
|
|
120
|
+
type: 'KV.GET',
|
|
121
|
+
bucket: 'user-sessions',
|
|
122
|
+
key: 'user-123',
|
|
123
|
+
onResult: (result) => {
|
|
124
|
+
if ('error' in result) {
|
|
125
|
+
console.error('Error:', result.error)
|
|
126
|
+
} else {
|
|
127
|
+
console.log('Value:', result)
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
// Subscribe to KV changes
|
|
133
|
+
send({
|
|
134
|
+
type: 'KV.SUBSCRIBE',
|
|
135
|
+
config: {
|
|
136
|
+
bucket: 'user-sessions',
|
|
137
|
+
key: 'user-123',
|
|
138
|
+
callback: (entry) => {
|
|
139
|
+
console.log('KV Update:', entry)
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
})
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## State Machine States
|
|
146
|
+
|
|
147
|
+
The NATS machine operates in the following states:
|
|
148
|
+
|
|
149
|
+
- **`not_configured`**: Initial state, waiting for configuration
|
|
150
|
+
- **`configured`**: Configuration received, ready to connect
|
|
151
|
+
- **`connecting`**: Attempting to establish NATS connection
|
|
152
|
+
- **`initialise_managers`**: Setting up subject and KV managers
|
|
153
|
+
- **`connected`**: Fully connected and operational
|
|
154
|
+
- **`closing`**: Gracefully disconnecting
|
|
155
|
+
- **`closed`**: Connection closed, can reconnect
|
|
156
|
+
- **`error`**: Error state, can reset and retry
|
|
157
|
+
|
|
158
|
+
## API Reference
|
|
159
|
+
|
|
160
|
+
### Main Exports
|
|
161
|
+
|
|
162
|
+
- `natsMachine`: The main XState machine for NATS operations
|
|
163
|
+
- `KvSubscriptionKey`: Type for KV subscription keys
|
|
164
|
+
- `parseNatsResult`: Utility for parsing NATS operation results
|
|
165
|
+
- `AuthConfig`: Type for authentication configuration
|
|
166
|
+
|
|
167
|
+
### Authentication
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
// Decentralised auth
|
|
171
|
+
auth: {
|
|
172
|
+
type: 'decentralised',
|
|
173
|
+
sentinelB64: 'base64-encoded-sentinel',
|
|
174
|
+
user: 'username',
|
|
175
|
+
pass: 'password'
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// User/password auth
|
|
179
|
+
auth: {
|
|
180
|
+
type: 'userpass',
|
|
181
|
+
user: 'username',
|
|
182
|
+
pass: 'password'
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Token auth
|
|
186
|
+
auth: {
|
|
187
|
+
type: 'token',
|
|
188
|
+
token: 'your-token'
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Events
|
|
193
|
+
|
|
194
|
+
#### Connection Events
|
|
195
|
+
|
|
196
|
+
- `CONFIGURE`: Set connection configuration
|
|
197
|
+
- `CONNECT`: Establish connection
|
|
198
|
+
- `DISCONNECT`: Close connection
|
|
199
|
+
- `RESET`: Reset to initial state
|
|
200
|
+
|
|
201
|
+
#### Subject Events
|
|
202
|
+
|
|
203
|
+
- `SUBJECT.SUBSCRIBE`: Subscribe to a subject
|
|
204
|
+
- `SUBJECT.UNSUBSCRIBE`: Unsubscribe from a subject
|
|
205
|
+
- `SUBJECT.PUBLISH`: Publish to a subject
|
|
206
|
+
- `SUBJECT.REQUEST`: Send request-reply
|
|
207
|
+
- `SUBJECT.UNSUBSCRIBE_ALL`: Clear all subscriptions
|
|
208
|
+
|
|
209
|
+
#### KV Events
|
|
210
|
+
|
|
211
|
+
- `KV.BUCKET_CREATE`: Create a KV bucket
|
|
212
|
+
- `KV.BUCKET_DELETE`: Delete a KV bucket
|
|
213
|
+
- `KV.BUCKET_LIST`: List KV buckets
|
|
214
|
+
- `KV.PUT`: Store a value
|
|
215
|
+
- `KV.GET`: Retrieve a value
|
|
216
|
+
- `KV.DELETE`: Delete a value
|
|
217
|
+
- `KV.SUBSCRIBE`: Subscribe to KV changes
|
|
218
|
+
- `KV.UNSUBSCRIBE`: Unsubscribe from KV changes
|
|
219
|
+
- `KV.UNSUBSCRIBE_ALL`: Unsubscribe from all KV changes
|
|
220
|
+
|
|
221
|
+
## Examples
|
|
222
|
+
|
|
223
|
+
Check out the [React example](./examples/react-test/) for a complete working implementation.
|
|
224
|
+
|
|
225
|
+
## Development
|
|
226
|
+
|
|
227
|
+
### Prerequisites
|
|
228
|
+
|
|
229
|
+
- Node.js 22+
|
|
230
|
+
- pnpm
|
|
231
|
+
|
|
232
|
+
### Setup
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
make install
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### Commands
|
|
239
|
+
|
|
240
|
+
```bash
|
|
241
|
+
make check # Run prettier + lint (tsc --noEmit)
|
|
242
|
+
make test # Run tests
|
|
243
|
+
make build # Build the library
|
|
244
|
+
make bump PART=patch # Bump version (major|minor|patch)
|
|
245
|
+
make release # Tag, push, and create GitHub release
|
|
246
|
+
make publish # Build, test, and publish to npm
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
## License
|
|
250
|
+
|
|
251
|
+
MIT
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ConnectionOptions, Msg, NatsConnection, Status } from '@nats-io/nats-core';
|
|
2
|
+
import { KvEntry } from '@nats-io/kv';
|
|
3
|
+
import { type AuthConfig } from './types';
|
|
4
|
+
export type InternalStatusEvents = {
|
|
5
|
+
type: 'NATS_CONNECTION.DISCONNECTED';
|
|
6
|
+
status: Status;
|
|
7
|
+
} | {
|
|
8
|
+
type: 'NATS_CONNECTION.RECONNECT';
|
|
9
|
+
status: Status;
|
|
10
|
+
} | {
|
|
11
|
+
type: 'NATS_CONNECTION.ERROR';
|
|
12
|
+
status: Status;
|
|
13
|
+
} | {
|
|
14
|
+
type: 'NATS_CONNECTION.CLOSE';
|
|
15
|
+
status: Status;
|
|
16
|
+
} | {
|
|
17
|
+
type: 'NATS_CONNECTION.RECONNECTING';
|
|
18
|
+
status: Status;
|
|
19
|
+
};
|
|
20
|
+
export declare const connectToNats: import("xstate").PromiseActorLogic<NatsConnection, {
|
|
21
|
+
opts: ConnectionOptions;
|
|
22
|
+
auth?: AuthConfig;
|
|
23
|
+
}, import("xstate").EventObject>;
|
|
24
|
+
export declare const disconnectNats: import("xstate").PromiseActorLogic<void, {
|
|
25
|
+
connection: NatsConnection | null;
|
|
26
|
+
}, import("xstate").EventObject>;
|
|
27
|
+
export declare const parseNatsResult: (msg: Msg | KvEntry | null | Error) => unknown;
|
|
28
|
+
//# sourceMappingURL=connection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/actions/connection.ts"],"names":[],"mappings":"AACA,OAAO,EACL,iBAAiB,EAEjB,GAAG,EACH,cAAc,EACd,MAAM,EAEP,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AACrC,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,SAAS,CAAA;AA6BzC,MAAM,MAAM,oBAAoB,GAC5B;IAAE,IAAI,EAAE,8BAA8B,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACxD;IAAE,IAAI,EAAE,2BAA2B,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACrD;IAAE,IAAI,EAAE,uBAAuB,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACjD;IAAE,IAAI,EAAE,uBAAuB,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACjD;IAAE,IAAI,EAAE,8BAA8B,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAA;AAE5D,eAAO,MAAM,aAAa;UAIP,iBAAiB;WAAS,UAAU;gCAwDtD,CAAA;AAED,eAAO,MAAM,cAAc;gBACgB,cAAc,GAAG,IAAI;gCAM/D,CAAA;AAED,eAAO,MAAM,eAAe,GAAI,KAAK,GAAG,GAAG,OAAO,GAAG,IAAI,GAAG,KAAK,YAiBhE,CAAA"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { fromPromise } from 'xstate';
|
|
2
|
+
import { credsAuthenticator, wsconnect, } from '@nats-io/nats-core';
|
|
3
|
+
import { sendParent } from 'xstate';
|
|
4
|
+
const makeAuthConfig = (auth) => {
|
|
5
|
+
if (!auth) {
|
|
6
|
+
return {};
|
|
7
|
+
}
|
|
8
|
+
if (auth.type === 'decentralised') {
|
|
9
|
+
const decodedSentinel = atob(auth.sentinelB64);
|
|
10
|
+
return {
|
|
11
|
+
authenticator: credsAuthenticator(new TextEncoder().encode(decodedSentinel)),
|
|
12
|
+
user: auth.user,
|
|
13
|
+
pass: auth.pass,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
else if (auth.type === 'userpass') {
|
|
17
|
+
return {
|
|
18
|
+
user: auth.user,
|
|
19
|
+
pass: auth.pass,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
else if (auth.type === 'token') {
|
|
23
|
+
return {
|
|
24
|
+
token: auth.token,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
throw new Error(`Unsupported auth config type ${auth.type}`);
|
|
28
|
+
};
|
|
29
|
+
export const connectToNats = fromPromise(async ({ input, }) => {
|
|
30
|
+
const mergedOpts = {
|
|
31
|
+
...input.opts,
|
|
32
|
+
...makeAuthConfig(input.auth),
|
|
33
|
+
};
|
|
34
|
+
const nc = await wsconnect(mergedOpts);
|
|
35
|
+
(async () => {
|
|
36
|
+
for await (const status of nc.status()) {
|
|
37
|
+
console.log('Status loop received status', status);
|
|
38
|
+
const { type } = status;
|
|
39
|
+
switch (type) {
|
|
40
|
+
case 'disconnect':
|
|
41
|
+
sendParent({ type: 'NATS_CONNECTION.DISCONNECTED', status });
|
|
42
|
+
break;
|
|
43
|
+
case 'reconnect':
|
|
44
|
+
sendParent({ type: 'NATS_CONNECTION.RECONNECT', status });
|
|
45
|
+
break;
|
|
46
|
+
case 'error':
|
|
47
|
+
sendParent({ type: 'NATS_CONNECTION.ERROR', status });
|
|
48
|
+
break;
|
|
49
|
+
case 'close':
|
|
50
|
+
sendParent({ type: 'NATS_CONNECTION.CLOSE', status });
|
|
51
|
+
break;
|
|
52
|
+
case 'ldm':
|
|
53
|
+
console.debug('LDM', status);
|
|
54
|
+
break;
|
|
55
|
+
case 'ping':
|
|
56
|
+
// console.debug('Received ping, pong sent automatically')
|
|
57
|
+
break;
|
|
58
|
+
case 'forceReconnect':
|
|
59
|
+
sendParent({ type: 'NATS_CONNECTION.RECONNECT', status });
|
|
60
|
+
break;
|
|
61
|
+
case 'reconnecting':
|
|
62
|
+
sendParent({ type: 'NATS_CONNECTION.RECONNECTING', status });
|
|
63
|
+
break;
|
|
64
|
+
case 'slowConsumer':
|
|
65
|
+
console.debug('SLOW_CONSUMER', status);
|
|
66
|
+
break;
|
|
67
|
+
case 'staleConnection':
|
|
68
|
+
console.debug('STALE_CONNECTION', status);
|
|
69
|
+
break;
|
|
70
|
+
case 'update':
|
|
71
|
+
console.debug('NATS_CONNECTION.UPDATE', status);
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
console.log('Exiting nats status loop');
|
|
76
|
+
})();
|
|
77
|
+
return nc;
|
|
78
|
+
});
|
|
79
|
+
export const disconnectNats = fromPromise(async ({ input }) => {
|
|
80
|
+
if (input.connection) {
|
|
81
|
+
await input.connection.drain();
|
|
82
|
+
await input.connection.close();
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
export const parseNatsResult = (msg) => {
|
|
86
|
+
if (!msg) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
if (msg instanceof Error) {
|
|
90
|
+
return msg;
|
|
91
|
+
}
|
|
92
|
+
let data;
|
|
93
|
+
try {
|
|
94
|
+
data = msg.json();
|
|
95
|
+
}
|
|
96
|
+
catch (jsonError) {
|
|
97
|
+
// If JSON parsing fails, use the raw string
|
|
98
|
+
data = msg.string();
|
|
99
|
+
}
|
|
100
|
+
return data;
|
|
101
|
+
};
|
|
102
|
+
//# sourceMappingURL=connection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../../src/actions/connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAA;AACpC,OAAO,EAEL,kBAAkB,EAIlB,SAAS,GACV,MAAM,oBAAoB,CAAA;AAG3B,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAA;AAEnC,MAAM,cAAc,GAAG,CAAC,IAAiB,EAAE,EAAE;IAC3C,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,EAAE,CAAA;IACX,CAAC;IAED,IAAI,IAAI,CAAC,IAAI,KAAK,eAAe,EAAE,CAAC;QAClC,MAAM,eAAe,GAAG,IAAI,CAAC,IAAK,CAAC,WAAY,CAAC,CAAA;QAChD,OAAO;YACL,aAAa,EAAE,kBAAkB,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;YAC5E,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,IAAI,EAAE,IAAI,CAAC,IAAI;SAChB,CAAA;IACH,CAAC;SAAM,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;QACpC,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,IAAI,EAAE,IAAI,CAAC,IAAI;SAChB,CAAA;IACH,CAAC;SAAM,IAAI,IAAI,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;QACjC,OAAO;YACL,KAAK,EAAE,IAAI,CAAC,KAAK;SAClB,CAAA;IACH,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,gCAAgC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAA;AAC9D,CAAC,CAAA;AASD,MAAM,CAAC,MAAM,aAAa,GAAG,WAAW,CACtC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, QueuedIterator } from '@nats-io/nats-core';
|
|
2
|
+
import { Kvm, KvWatchEntry, KvWatchOptions } from '@nats-io/kv';
|
|
3
|
+
import { Pair } from '../utils';
|
|
4
|
+
export declare class KvSubscriptionKey extends Pair<string, string> {
|
|
5
|
+
}
|
|
6
|
+
export type KvSubscriptionConfig = {
|
|
7
|
+
bucket: string;
|
|
8
|
+
key: string;
|
|
9
|
+
callback: (data: any) => void;
|
|
10
|
+
opts?: KvWatchOptions;
|
|
11
|
+
replayOnReconnect?: boolean;
|
|
12
|
+
};
|
|
13
|
+
export declare const kvConsolidateState: import("xstate").PromiseActorLogic<{
|
|
14
|
+
subscriptions: Map<string, QueuedIterator<KvWatchEntry>>;
|
|
15
|
+
}, {
|
|
16
|
+
kvm: Kvm | null;
|
|
17
|
+
connection: NatsConnection | null;
|
|
18
|
+
currentState: Map<string, QueuedIterator<KvWatchEntry>>;
|
|
19
|
+
targetState: Map<string, KvSubscriptionConfig>;
|
|
20
|
+
}, import("xstate").EventObject>;
|
|
21
|
+
//# sourceMappingURL=kv.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kv.d.ts","sourceRoot":"","sources":["../../src/actions/kv.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnE,OAAO,EAAE,GAAG,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAC/D,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAA;AAG/B,qBAAa,iBAAkB,SAAQ,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC;CAAG;AAE9D,MAAM,MAAM,oBAAoB,GAAG;IACjC,MAAM,EAAE,MAAM,CAAA;IACd,GAAG,EAAE,MAAM,CAAA;IACX,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAA;IAC7B,IAAI,CAAC,EAAE,cAAc,CAAA;IACrB,iBAAiB,CAAC,EAAE,OAAO,CAAA;CAC5B,CAAA;AAED,eAAO,MAAM,kBAAkB;mBAWZ,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC;;SANjD,GAAG,GAAG,IAAI;gBACH,cAAc,GAAG,IAAI;kBACnB,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC;iBAC1C,GAAG,CAAC,MAAM,EAAE,oBAAoB,CAAC;gCAoEnD,CAAA"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Pair } from '../utils';
|
|
2
|
+
import { fromPromise } from 'xstate';
|
|
3
|
+
export class KvSubscriptionKey extends Pair {
|
|
4
|
+
}
|
|
5
|
+
export const kvConsolidateState = fromPromise(async ({ input, }) => {
|
|
6
|
+
if (!input.connection || !input.kvm) {
|
|
7
|
+
throw new Error('NATS connection or KVM is not available');
|
|
8
|
+
}
|
|
9
|
+
const { currentState, targetState } = input;
|
|
10
|
+
const syncedState = new Map(currentState);
|
|
11
|
+
// Unsubscribe from items that are in currentState but not in targetState
|
|
12
|
+
for (const [kvKey, subscription] of currentState) {
|
|
13
|
+
if (!targetState.has(kvKey)) {
|
|
14
|
+
try {
|
|
15
|
+
syncedState.delete(kvKey);
|
|
16
|
+
subscription.stop();
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
console.error(`Error unsubscribing from subject "${kvKey}"`, error);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
// Subscribe to new subjects that are in targetState but not in currentState
|
|
24
|
+
for (const [kvKey, config] of targetState) {
|
|
25
|
+
if (!currentState.has(kvKey)) {
|
|
26
|
+
// the problem is kv key is triggered on successive calls to consolidateState
|
|
27
|
+
// the match is not working?
|
|
28
|
+
try {
|
|
29
|
+
const kv = await input.kvm.open(config.bucket);
|
|
30
|
+
const watchOptions = config;
|
|
31
|
+
const watcher = await kv.watch(watchOptions);
|
|
32
|
+
syncedState.set(kvKey, watcher);
|
|
33
|
+
(async () => {
|
|
34
|
+
try {
|
|
35
|
+
for await (const e of watcher) {
|
|
36
|
+
if (e.operation !== 'DEL') {
|
|
37
|
+
let parsedValue;
|
|
38
|
+
try {
|
|
39
|
+
parsedValue = JSON.parse(e.string());
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
parsedValue = e.string();
|
|
43
|
+
}
|
|
44
|
+
config.callback({
|
|
45
|
+
bucket: config.bucket,
|
|
46
|
+
key: config.key,
|
|
47
|
+
value: parsedValue,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
console.error(`KV_SUBSCRIBE (connected): Watcher loop error for ${kvKey}:`, error);
|
|
54
|
+
}
|
|
55
|
+
})();
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
console.error(`Error subscribing to subject "${kvKey}"`, error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
subscriptions: syncedState,
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
//# sourceMappingURL=kv.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kv.js","sourceRoot":"","sources":["../../src/actions/kv.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAA;AAC/B,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAA;AAEpC,MAAM,OAAO,iBAAkB,SAAQ,IAAoB;CAAG;AAU9D,MAAM,CAAC,MAAM,kBAAkB,GAAG,WAAW,CAC3C,KAAK,EAAE,EACL,KAAK,GAQN,EAEE,EAAE;IACH,IAAI,CAAC,KAAK,CAAC,UAAU,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;QACpC,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAA;IAC5D,CAAC;IAED,MAAM,EAAE,YAAY,EAAE,WA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AC,CAAC,MAAM,EAAE,CAAC,CAAA;gCACtC,CAAC;gCAAC,MAAM,CAAC;oCACP,WAAW,GAAG,CAAC,CAAC,MAAM,EAAE,CAAA;gCAC1B,CAAC;gCAED,MAAM,CAAC,QAAQ,CAAC;oCACd,MAAM,EAAE,MAAM,CAAC,MAAM;oCACrB,GAAG,EAAE,MAAM,CAAC,GAAG;oCACf,KAAK,EAAE,WAAW;iCACnB,CAAC,CAAA;4BACJ,CAAC;wBACH,CAAC;oBACH,CAAC;oBAAC,OAAO,KAAK,EAAE,CAAC;wBACf,OAAO,CAAC,KAAK,CAAC,oDAAoD,KAAK,GAAG,EAAE,KAAK,CAAC,CAAA;oBACpF,CAAC;gBACH,CAAC,CAAC,EAAE,CAAA;YACN,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,CAAC,KAAK,CAAC,iCAAiC,KAAK,GAAG,EAAE,KAAK,CAAC,CAAA;YACjE,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO;QACL,aAAa,EAAE,WAAW;KAC3B,CAAA;AACH,CAAC,CACF,CAAA"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { NatsConnection, PublishOptions, RequestOptions, Subscription, SubscriptionOptions } from '@nats-io/nats-core';
|
|
2
|
+
export type SubjectSubscriptionConfig = {
|
|
3
|
+
subject: string;
|
|
4
|
+
callback: (data: any) => void;
|
|
5
|
+
opts?: SubscriptionOptions;
|
|
6
|
+
};
|
|
7
|
+
export declare const subjectConsolidateState: ({ input, }: {
|
|
8
|
+
input: {
|
|
9
|
+
connection: NatsConnection | null;
|
|
10
|
+
currentSubscriptions: Map<string, Subscription>;
|
|
11
|
+
targetSubscriptions: Map<string, SubjectSubscriptionConfig>;
|
|
12
|
+
};
|
|
13
|
+
}) => {
|
|
14
|
+
subscriptions: Map<string, Subscription>;
|
|
15
|
+
};
|
|
16
|
+
export declare const subjectRequest: ({ input, }: {
|
|
17
|
+
input: {
|
|
18
|
+
connection: NatsConnection | null;
|
|
19
|
+
subject: string;
|
|
20
|
+
payload: any;
|
|
21
|
+
opts?: RequestOptions;
|
|
22
|
+
callback: (data: any) => void;
|
|
23
|
+
};
|
|
24
|
+
}) => void;
|
|
25
|
+
export declare const subjectPublish: ({ input, }: {
|
|
26
|
+
input: {
|
|
27
|
+
connection: NatsConnection | null;
|
|
28
|
+
subject: string;
|
|
29
|
+
payload: any;
|
|
30
|
+
options?: PublishOptions;
|
|
31
|
+
onPublishResult?: (result: {
|
|
32
|
+
ok: true;
|
|
33
|
+
} | {
|
|
34
|
+
ok: false;
|
|
35
|
+
error: Error;
|
|
36
|
+
}) => void;
|
|
37
|
+
};
|
|
38
|
+
}) => void;
|
|
39
|
+
//# sourceMappingURL=subject.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subject.d.ts","sourceRoot":"","sources":["../../src/actions/subject.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,cAAc,EACd,cAAc,EACd,cAAc,EACd,YAAY,EACZ,mBAAmB,EACpB,MAAM,oBAAoB,CAAA;AAG3B,MAAM,MAAM,yBAAyB,GAAG;IACtC,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAA;IAC7B,IAAI,CAAC,EAAE,mBAAmB,CAAA;CAC3B,CAAA;AAED,eAAO,MAAM,uBAAuB,GAAI,YAErC;IACD,KAAK,EAAE;QACL,UAAU,EAAE,cAAc,GAAG,IAAI,CAAA;QACjC,oBAAoB,EAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,CAAA;QAC/C,mBAAmB,EAAE,GAAG,CAAC,MAAM,EAAE,yBAAyB,CAAC,CAAA;KAC5D,CAAA;CACF;;CAmDA,CAAA;AAED,eAAO,MAAM,cAAc,GAAI,YAE5B;IACD,KAAK,EAAE;QACL,UAAU,EAAE,cAAc,GAAG,IAAI,CAAA;QACjC,OAAO,EAAE,MAAM,CAAA;QACf,OAAO,EAAE,GAAG,CAAA;QACZ,IAAI,CAAC,EAAE,cAAc,CAAA;QACrB,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAA;KAC9B,CAAA;CACF,SAcA,CAAA;AAED,eAAO,MAAM,cAAc,GAAI,YAE5B;IACD,KAAK,EAAE;QACL,UAAU,EAAE,cAAc,GAAG,IAAI,CAAA;QACjC,OAAO,EAAE,MAAM,CAAA;QACf,OAAO,EAAE,GAAG,CAAA;QACZ,OAAO,CAAC,EAAE,cAAc,CAAA;QACxB,eAAe,CAAC,EAAE,CAAC,MAAM,EAAE;YAAE,EAAE,EAAE,IAAI,CAAA;SAAE,GAAG;YAAE,EAAE,EAAE,KAAK,CAAC;YAAC,KAAK,EAAE,KAAK,CAAA;SAAE,KAAK,IAAI,CAAA;KAC/E,CAAA;CACF,SAaA,CAAA"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { parseNatsResult } from './connection';
|
|
2
|
+
export const subjectConsolidateState = ({ input, }) => {
|
|
3
|
+
const { connection, currentSubscriptions, targetSubscriptions } = input;
|
|
4
|
+
if (!connection) {
|
|
5
|
+
throw new Error('NATS connection is not available');
|
|
6
|
+
}
|
|
7
|
+
const syncedSubscriptions = new Map(currentSubscriptions);
|
|
8
|
+
// Unsubscribe from subjects that are in currentSubscriptions but not in targetSubscriptions
|
|
9
|
+
for (const [subject, subscription] of currentSubscriptions) {
|
|
10
|
+
if (!targetSubscriptions.has(subject)) {
|
|
11
|
+
try {
|
|
12
|
+
syncedSubscriptions.delete(subject);
|
|
13
|
+
subscription.unsubscribe();
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
console.error(`Error unsubscribing from subject "${subject}"`, error);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// Subscribe to new subjects that are in targetSubscriptions but not in currentSubscriptions
|
|
21
|
+
for (const [subject, subscriptionConfig] of targetSubscriptions) {
|
|
22
|
+
if (!currentSubscriptions.has(subject)) {
|
|
23
|
+
try {
|
|
24
|
+
const sub = connection.subscribe(subject, subscriptionConfig.opts);
|
|
25
|
+
(async () => {
|
|
26
|
+
try {
|
|
27
|
+
for await (const msg of sub) {
|
|
28
|
+
try {
|
|
29
|
+
subscriptionConfig?.callback(parseNatsResult(msg));
|
|
30
|
+
}
|
|
31
|
+
catch (callbackError) {
|
|
32
|
+
console.error(`Callback error for subject "${subject}"`, callbackError);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch (iteratorError) {
|
|
37
|
+
console.error(`Iterator error for subject "${subject}"`, iteratorError);
|
|
38
|
+
}
|
|
39
|
+
})();
|
|
40
|
+
syncedSubscriptions.set(subject, sub);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error(`Error subscribing to subject "${subject}"`, error);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
subscriptions: syncedSubscriptions,
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
export const subjectRequest = ({ input, }) => {
|
|
52
|
+
const { connection, subject, payload, opts, callback } = input;
|
|
53
|
+
if (!connection) {
|
|
54
|
+
throw new Error('NATS connection is not available');
|
|
55
|
+
}
|
|
56
|
+
connection
|
|
57
|
+
.request(subject, payload, opts)
|
|
58
|
+
.then((msg) => {
|
|
59
|
+
callback(parseNatsResult(msg));
|
|
60
|
+
})
|
|
61
|
+
.catch((err) => {
|
|
62
|
+
console.error(`RequestReply error for subject "${subject}"`, err);
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
export const subjectPublish = ({ input, }) => {
|
|
66
|
+
const { connection, subject, payload, options, onPublishResult } = input;
|
|
67
|
+
if (!connection) {
|
|
68
|
+
throw new Error('NATS connection is not available');
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
connection.publish(subject, payload, options);
|
|
72
|
+
onPublishResult?.({ ok: true });
|
|
73
|
+
}
|
|
74
|
+
catch (callbackError) {
|
|
75
|
+
console.error(`Publish callback error for subject "${subject}"`, callbackError);
|
|
76
|
+
onPublishResult?.({ ok: false, error: callbackError });
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
//# sourceMappingURL=subject.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subject.js","sourceRoot":"","sources":["../../src/actions/subject.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAQ9C,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,EACtC,KAAK,GAON,EAAE,EAAE;IACH,MAAM,EAAE,UAAU,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,GAAG,KAAK,CAAA;IACvE,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAA;IACrD,CAAC;IAED,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAC,oBAAoB,CAAC,CAAA;IAEzD,4FAA4F;IAC5F,KAAK,MAAM,CAAC,OAAO,EAAE,YAAY,CAAC,IAAI,oBAAoB,EAAE,CAAC;QAC3D,IAAI,CAAC,mBAAmB,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;YACtC,IAAI,CAAC;gBACH,mBAAmB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;gBACnC,YAAY,CAAC,WAAW,EAAE,CAAA;YAC5B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,CAAC,KAAK,CAAC,qCAAqC,OAAO,GAAG,EAAE,KAAK,CAAC,CAAA;YACvE,CAAC;QACH,CAAC;IACH,CAAC;IAED,4FAA4F;IAC5F,KAAK,MAAM,CAAC,OAAO,EAAE,kBAAkB,CAAC,IAAI,mBAAmB,EAAE,CAAC;QAChE,IAAI,CAAC,oBAAoB,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;YACvC,IAAI,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AAM,CAAA;CACf"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/actions/types.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { natsMachine } from './machines/root';
|
|
2
|
+
export { subjectManagerLogic, type Context as SubjectContext, type ExternalEvents as SubjectEvent, } from './machines/subject';
|
|
3
|
+
export { kvManagerLogic, type Context as KvContext, type ExternalEvents as KvEvent, } from './machines/kv';
|
|
4
|
+
export { KvSubscriptionKey, type KvSubscriptionConfig } from './actions/kv';
|
|
5
|
+
export { parseNatsResult } from './actions/connection';
|
|
6
|
+
export { type AuthConfig } from './actions/types';
|
|
7
|
+
export { type NatsConnectionConfig, type Context as NatsContext, type ExternalEvents as NatsEvent, } from './machines/root';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|