@onebun/nats 0.1.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/README.md +141 -0
- package/package.json +52 -0
- package/src/index.ts +26 -0
- package/src/jetstream.adapter.ts +588 -0
- package/src/nats-client.ts +198 -0
- package/src/nats.adapter.ts +467 -0
- package/src/types.ts +81 -0
package/README.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# @onebun/nats
|
|
2
|
+
|
|
3
|
+
NATS and JetStream integration for OneBun framework.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @onebun/nats
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **NatsQueueAdapter** - Basic pub/sub messaging (no persistence)
|
|
14
|
+
- **JetStreamQueueAdapter** - Persistent messaging with acknowledgments
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
### Basic NATS (Pub/Sub)
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { OneBunApplication } from '@onebun/core';
|
|
22
|
+
import { NatsQueueAdapter } from '@onebun/nats';
|
|
23
|
+
|
|
24
|
+
const app = await OneBunApplication.create(AppModule, {
|
|
25
|
+
queue: {
|
|
26
|
+
adapter: NatsQueueAdapter,
|
|
27
|
+
options: {
|
|
28
|
+
servers: 'nats://localhost:4222',
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### JetStream (Persistent)
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { OneBunApplication } from '@onebun/core';
|
|
38
|
+
import { JetStreamQueueAdapter } from '@onebun/nats';
|
|
39
|
+
|
|
40
|
+
const app = await OneBunApplication.create(AppModule, {
|
|
41
|
+
queue: {
|
|
42
|
+
adapter: JetStreamQueueAdapter,
|
|
43
|
+
options: {
|
|
44
|
+
servers: 'nats://localhost:4222',
|
|
45
|
+
stream: 'EVENTS',
|
|
46
|
+
createStream: true,
|
|
47
|
+
streamConfig: {
|
|
48
|
+
subjects: ['events.>'],
|
|
49
|
+
retention: 'limits',
|
|
50
|
+
maxMsgs: 1000000,
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Service with Queue Handlers
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { Subscribe, Message, OnQueueReady } from '@onebun/core';
|
|
61
|
+
|
|
62
|
+
export class OrderService {
|
|
63
|
+
@OnQueueReady()
|
|
64
|
+
handleReady() {
|
|
65
|
+
console.log('Connected to NATS');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@Subscribe('orders.created')
|
|
69
|
+
async handleOrderCreated(message: Message<OrderData>) {
|
|
70
|
+
console.log('New order:', message.data);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
@Subscribe('orders.*', { ackMode: 'manual', group: 'order-processors' })
|
|
74
|
+
async handleOrder(message: Message<OrderData>) {
|
|
75
|
+
try {
|
|
76
|
+
await this.processOrder(message.data);
|
|
77
|
+
await message.ack();
|
|
78
|
+
} catch (error) {
|
|
79
|
+
await message.nack(true); // requeue
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Feature Comparison
|
|
86
|
+
|
|
87
|
+
| Feature | NatsQueueAdapter | JetStreamQueueAdapter |
|
|
88
|
+
|---------|------------------|----------------------|
|
|
89
|
+
| Pattern subscriptions | Yes | Yes |
|
|
90
|
+
| Consumer groups | Yes | Yes |
|
|
91
|
+
| Persistence | No | Yes |
|
|
92
|
+
| Acknowledgments | No | Yes |
|
|
93
|
+
| Dead Letter Queue | No | Yes |
|
|
94
|
+
| Retry | No | Yes |
|
|
95
|
+
| Delayed messages | No | No |
|
|
96
|
+
| Priority | No | No |
|
|
97
|
+
|
|
98
|
+
## Configuration Options
|
|
99
|
+
|
|
100
|
+
### NatsConnectionOptions
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
interface NatsConnectionOptions {
|
|
104
|
+
servers: string | string[]; // NATS server URL(s)
|
|
105
|
+
name?: string; // Connection name
|
|
106
|
+
token?: string; // Auth token
|
|
107
|
+
user?: string; // Username
|
|
108
|
+
pass?: string; // Password
|
|
109
|
+
maxReconnectAttempts?: number;
|
|
110
|
+
reconnectTimeWait?: number;
|
|
111
|
+
timeout?: number;
|
|
112
|
+
tls?: boolean;
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### JetStreamAdapterOptions
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
interface JetStreamAdapterOptions extends NatsConnectionOptions {
|
|
120
|
+
stream: string; // Stream name
|
|
121
|
+
createStream?: boolean; // Create stream if not exists
|
|
122
|
+
streamConfig?: {
|
|
123
|
+
subjects?: string[]; // Subjects to store
|
|
124
|
+
retention?: 'limits' | 'interest' | 'workqueue';
|
|
125
|
+
maxMsgs?: number;
|
|
126
|
+
maxBytes?: number;
|
|
127
|
+
maxAge?: number;
|
|
128
|
+
storage?: 'file' | 'memory';
|
|
129
|
+
replicas?: number;
|
|
130
|
+
};
|
|
131
|
+
consumerConfig?: {
|
|
132
|
+
ackWait?: number; // Ack timeout (nanoseconds)
|
|
133
|
+
maxDeliver?: number; // Max delivery attempts
|
|
134
|
+
maxAckPending?: number;
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## License
|
|
140
|
+
|
|
141
|
+
LGPL-3.0
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@onebun/nats",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "NATS and JetStream integration for OneBun framework",
|
|
5
|
+
"license": "LGPL-3.0",
|
|
6
|
+
"author": "RemRyahirev",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"onebun",
|
|
9
|
+
"nats",
|
|
10
|
+
"jetstream",
|
|
11
|
+
"message-queue",
|
|
12
|
+
"bun",
|
|
13
|
+
"typescript",
|
|
14
|
+
"effect"
|
|
15
|
+
],
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/RemRyahirev/onebun.git",
|
|
19
|
+
"directory": "packages/nats"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/RemRyahirev/onebun/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/RemRyahirev/onebun/tree/master/packages/nats#readme",
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public",
|
|
27
|
+
"registry": "https://registry.npmjs.org/"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"src",
|
|
31
|
+
"README.md"
|
|
32
|
+
],
|
|
33
|
+
"main": "src/index.ts",
|
|
34
|
+
"module": "src/index.ts",
|
|
35
|
+
"types": "src/index.ts",
|
|
36
|
+
"scripts": {
|
|
37
|
+
"dev": "bun run --watch src/index.ts",
|
|
38
|
+
"test": "bun test"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@onebun/core": "workspace:*",
|
|
42
|
+
"@nats-io/transport-node": "^3.0.0-31",
|
|
43
|
+
"@nats-io/jetstream": "^3.0.0-31",
|
|
44
|
+
"effect": "^3.13.10"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"bun-types": "^1.2.20"
|
|
48
|
+
},
|
|
49
|
+
"engines": {
|
|
50
|
+
"bun": ">=1.2.9"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NATS and JetStream Module
|
|
3
|
+
*
|
|
4
|
+
* Provides queue adapters for NATS pub/sub and JetStream.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Types
|
|
8
|
+
export type {
|
|
9
|
+
NatsConnectionOptions,
|
|
10
|
+
NatsAdapterOptions,
|
|
11
|
+
JetStreamAdapterOptions,
|
|
12
|
+
} from './types';
|
|
13
|
+
|
|
14
|
+
// NATS Client
|
|
15
|
+
export {
|
|
16
|
+
NatsClient,
|
|
17
|
+
createNatsClient,
|
|
18
|
+
type NatsMessage,
|
|
19
|
+
type NatsSubscriptionHandle,
|
|
20
|
+
} from './nats-client';
|
|
21
|
+
|
|
22
|
+
// NATS Queue Adapter (pub/sub)
|
|
23
|
+
export { NatsQueueAdapter, createNatsQueueAdapter } from './nats.adapter';
|
|
24
|
+
|
|
25
|
+
// JetStream Queue Adapter (persistent)
|
|
26
|
+
export { JetStreamQueueAdapter, createJetStreamQueueAdapter } from './jetstream.adapter';
|