@rxdi/graphql-nats 0.7.240
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/jest-setup.ts +1 -0
- package/package.json +66 -0
- package/src/graphql-nats-pubsub.service.spec.ts +305 -0
- package/src/graphql-nats-pubsub.service.ts +41 -0
- package/src/index.ts +15 -0
- package/src/interfaces.ts +13 -0
- package/src/nats-pubsub.class.ts +150 -0
- package/test/graphql-nats-pubsub.service.spec.ts +101 -0
- package/test/pubsub.spec.ts +251 -0
- package/tsconfig.json +26 -0
- package/tslint.json +7 -0
package/jest-setup.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import 'jest';
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rxdi/graphql-nats",
|
|
3
|
+
"version": "0.7.240",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "https://github.com/rxdi/graphql-nats"
|
|
7
|
+
},
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"module": "./dist/index.js",
|
|
11
|
+
"typings": "./dist/index.d.ts",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "jest",
|
|
14
|
+
"build": "tsc",
|
|
15
|
+
"lint": "echo 'Lint skipped'"
|
|
16
|
+
},
|
|
17
|
+
"author": {
|
|
18
|
+
"name": "Kristian Tachev(Stradivario)",
|
|
19
|
+
"email": "kristiqn.tachev@gmail.com"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"graphql",
|
|
23
|
+
"nats",
|
|
24
|
+
"subscriptions",
|
|
25
|
+
"gapi",
|
|
26
|
+
"node"
|
|
27
|
+
],
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/rxdi/graphql-nats/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/rxdi/graphql-nats#readme",
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@rxdi/nats": "^0.7.239",
|
|
35
|
+
"graphql-subscriptions": "^3.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@rxdi/core": "^0.7.239",
|
|
39
|
+
"@rxdi/graphql": "^0.7.239",
|
|
40
|
+
"@rxdi/graphql-pubsub": "^0.7.239",
|
|
41
|
+
"@types/node": "^25.0.3",
|
|
42
|
+
"tslint": "^5.20.1",
|
|
43
|
+
"tslint-language-service": "^0.9.9",
|
|
44
|
+
"typescript": "^5.9.3"
|
|
45
|
+
},
|
|
46
|
+
"jest": {
|
|
47
|
+
"testEnvironment": "node",
|
|
48
|
+
"testPathIgnorePatterns": [
|
|
49
|
+
"/node_modules/"
|
|
50
|
+
],
|
|
51
|
+
"rootDir": "./",
|
|
52
|
+
"transform": {
|
|
53
|
+
"^.+\\.tsx?$": [
|
|
54
|
+
"ts-jest",
|
|
55
|
+
{
|
|
56
|
+
"useESM": true,
|
|
57
|
+
"diagnostics": false
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
},
|
|
61
|
+
"testRegex": "/src/.*\\.spec\\.(ts|tsx|js)$",
|
|
62
|
+
"moduleNameMapper": {
|
|
63
|
+
"^(\\.{1,2}/.*)\\.js$": "$1"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { Container } from '@rxdi/core';
|
|
2
|
+
import {
|
|
3
|
+
NatsPubSub,
|
|
4
|
+
NatsPubSubAsyncIterator,
|
|
5
|
+
createNatsPubSub,
|
|
6
|
+
} from './nats-pubsub.class';
|
|
7
|
+
import {
|
|
8
|
+
PubSubProtocol,
|
|
9
|
+
NatsPubSubInterface,
|
|
10
|
+
} from './interfaces';
|
|
11
|
+
import { GraphQLNatsPubSubService } from './graphql-nats-pubsub.service';
|
|
12
|
+
import { GraphQLNatsModule } from './index';
|
|
13
|
+
|
|
14
|
+
function createMockNatsClient() {
|
|
15
|
+
const subscriptions = new Map<number, (msg: any) => void>();
|
|
16
|
+
const subscriptionHandlers = new Map<number, { channel: string; callback: (msg: any) => void }>();
|
|
17
|
+
let natsSubIdCounter = 0;
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
publish: jest.fn().mockResolvedValue(undefined),
|
|
21
|
+
subscribe: jest.fn().mockImplementation((channel: string, callback: (msg: any) => void) => {
|
|
22
|
+
const natsSubId = ++natsSubIdCounter;
|
|
23
|
+
subscriptions.set(natsSubId, callback);
|
|
24
|
+
subscriptionHandlers.set(natsSubId, { channel, callback });
|
|
25
|
+
return Promise.resolve(natsSubId);
|
|
26
|
+
}),
|
|
27
|
+
unsubscribe: jest.fn().mockImplementation((natsSubId: number) => {
|
|
28
|
+
subscriptions.delete(natsSubId);
|
|
29
|
+
subscriptionHandlers.delete(natsSubId);
|
|
30
|
+
}),
|
|
31
|
+
_getSubscriptions: () => subscriptions,
|
|
32
|
+
_getSubscriptionHandler: (natsSubId: number) => subscriptionHandlers.get(natsSubId),
|
|
33
|
+
_triggerSubscription: (natsSubId: number, msg: any) => {
|
|
34
|
+
const callback = subscriptions.get(natsSubId);
|
|
35
|
+
if (callback) {
|
|
36
|
+
callback(msg);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
describe('NatsPubSub', () => {
|
|
43
|
+
let natsClient: ReturnType<typeof createMockNatsClient>;
|
|
44
|
+
let natsPubSub: NatsPubSub;
|
|
45
|
+
|
|
46
|
+
beforeEach(() => {
|
|
47
|
+
natsClient = createMockNatsClient();
|
|
48
|
+
natsPubSub = new NatsPubSub(natsClient);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('publish', () => {
|
|
52
|
+
it('should call natsClient.publish with trigger and payload', async () => {
|
|
53
|
+
await natsPubSub.publish('test.channel', { message: 'hello' });
|
|
54
|
+
expect(natsClient.publish).toHaveBeenCalledWith('test.channel', { message: 'hello' });
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should handle string payloads', async () => {
|
|
58
|
+
await natsPubSub.publish('string.channel', 'plain string');
|
|
59
|
+
expect(natsClient.publish).toHaveBeenCalledWith('string.channel', 'plain string');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should handle null payloads', async () => {
|
|
63
|
+
await natsPubSub.publish('null.channel', null);
|
|
64
|
+
expect(natsClient.publish).toHaveBeenCalledWith('null.channel', null);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should handle object payloads', async () => {
|
|
68
|
+
const payload = { nested: { data: [1, 2, 3] } };
|
|
69
|
+
await natsPubSub.publish('object.channel', payload);
|
|
70
|
+
expect(natsClient.publish).toHaveBeenCalledWith('object.channel', payload);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('subscribe', () => {
|
|
75
|
+
it('should return a subscription id', async () => {
|
|
76
|
+
const subId = await natsPubSub.subscribe('test.channel', async (msg) => {});
|
|
77
|
+
expect(typeof subId).toBe('number');
|
|
78
|
+
expect(subId).toBeGreaterThanOrEqual(0);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('should call natsClient.subscribe for first subscription to a channel', async () => {
|
|
82
|
+
await natsPubSub.subscribe('test.channel', async (msg) => {});
|
|
83
|
+
expect(natsClient.subscribe).toHaveBeenCalledWith('test.channel', expect.any(Function));
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should not call natsClient.subscribe for subsequent subscriptions to same channel', async () => {
|
|
87
|
+
await natsPubSub.subscribe('test.channel', async (msg) => {});
|
|
88
|
+
await natsPubSub.subscribe('test.channel', async (msg) => {});
|
|
89
|
+
expect(natsClient.subscribe).toHaveBeenCalledTimes(1);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('should return different ids for different channels', async () => {
|
|
93
|
+
const subId1 = await natsPubSub.subscribe('channel1', async (msg) => {});
|
|
94
|
+
const subId2 = await natsPubSub.subscribe('channel2', async (msg) => {});
|
|
95
|
+
expect(subId1).not.toBe(subId2);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should notify subscriber when message arrives on their channel', async () => {
|
|
99
|
+
const handler = jest.fn();
|
|
100
|
+
await natsPubSub.subscribe('test.channel', handler);
|
|
101
|
+
|
|
102
|
+
const natsSubId = await natsClient.subscribe.mock.results[0].value;
|
|
103
|
+
natsClient._triggerSubscription(natsSubId, { data: 'test message' });
|
|
104
|
+
|
|
105
|
+
expect(handler).toHaveBeenCalledWith({ data: 'test message' });
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('should notify all subscribers on the same channel', async () => {
|
|
109
|
+
const handler1 = jest.fn();
|
|
110
|
+
const handler2 = jest.fn();
|
|
111
|
+
await natsPubSub.subscribe('test.channel', handler1);
|
|
112
|
+
await natsPubSub.subscribe('test.channel', handler2);
|
|
113
|
+
|
|
114
|
+
const natsSubId = await natsClient.subscribe.mock.results[0].value;
|
|
115
|
+
natsClient._triggerSubscription(natsSubId, { data: 'broadcast' });
|
|
116
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
117
|
+
|
|
118
|
+
expect(handler1).toHaveBeenCalledWith({ data: 'broadcast' });
|
|
119
|
+
expect(handler2).toHaveBeenCalledWith({ data: 'broadcast' });
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe('unsubscribe', () => {
|
|
124
|
+
it('should remove subscription from subscription map', async () => {
|
|
125
|
+
const subId = await natsPubSub.subscribe('test.channel', async (msg) => {});
|
|
126
|
+
await natsPubSub.unsubscribe(subId);
|
|
127
|
+
|
|
128
|
+
const entry = (natsPubSub as any).subscriptionMap.get(subId);
|
|
129
|
+
expect(entry).toBeUndefined();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('should call natsClient.unsubscribe when last subscriber unsubscribes', async () => {
|
|
133
|
+
const subId = await natsPubSub.subscribe('test.channel', async (msg) => {});
|
|
134
|
+
await natsPubSub.unsubscribe(subId);
|
|
135
|
+
|
|
136
|
+
expect(natsClient.unsubscribe).toHaveBeenCalled();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should not call natsClient.unsubscribe when other subscribers remain', async () => {
|
|
140
|
+
const subId1 = await natsPubSub.subscribe('test.channel', async (msg) => {});
|
|
141
|
+
await natsPubSub.subscribe('test.channel', async (msg) => {});
|
|
142
|
+
|
|
143
|
+
await natsPubSub.unsubscribe(subId1);
|
|
144
|
+
expect(natsClient.unsubscribe).not.toHaveBeenCalled();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should handle unsubscribing non-existent subscription', async () => {
|
|
148
|
+
await expect(natsPubSub.unsubscribe(9999)).resolves.toBeUndefined();
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('should clean up subsRefsMap when last subscriber unsubscribes', async () => {
|
|
152
|
+
const subId = await natsPubSub.subscribe('test.channel', async (msg) => {});
|
|
153
|
+
await natsPubSub.unsubscribe(subId);
|
|
154
|
+
|
|
155
|
+
const refs = (natsPubSub as any).subsRefsMap.get('test.channel');
|
|
156
|
+
expect(refs).toBeUndefined();
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe('asyncIterator', () => {
|
|
161
|
+
it('should return NatsPubSubAsyncIterator instance', () => {
|
|
162
|
+
const iterator = natsPubSub.asyncIterator('test.channel');
|
|
163
|
+
expect(iterator).toBeInstanceOf(NatsPubSubAsyncIterator);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('should accept array of triggers', () => {
|
|
167
|
+
const iterator = natsPubSub.asyncIterator(['channel1', 'channel2']);
|
|
168
|
+
expect(iterator).toBeInstanceOf(NatsPubSubAsyncIterator);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe('asyncIterableIterator', () => {
|
|
173
|
+
it('should return NatsPubSubAsyncIterator instance', () => {
|
|
174
|
+
const iterator = natsPubSub.asyncIterableIterator('test.channel');
|
|
175
|
+
expect(iterator).toBeInstanceOf(NatsPubSubAsyncIterator);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
describe('NatsPubSubAsyncIterator', () => {
|
|
181
|
+
let natsClient: ReturnType<typeof createMockNatsClient>;
|
|
182
|
+
let pubsub: NatsPubSubInterface;
|
|
183
|
+
|
|
184
|
+
beforeEach(() => {
|
|
185
|
+
natsClient = createMockNatsClient();
|
|
186
|
+
pubsub = new NatsPubSub(natsClient);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
describe('asyncIterator symbol', () => {
|
|
190
|
+
it('should return itself when asyncIterator is called', () => {
|
|
191
|
+
const iterator = pubsub.asyncIterator('test.channel') as NatsPubSubAsyncIterator<any>;
|
|
192
|
+
expect(iterator[Symbol.asyncIterator]()).toBe(iterator);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe('initial state', () => {
|
|
197
|
+
it('should start with null subscriptionId', () => {
|
|
198
|
+
const iterator = pubsub.asyncIterator('test.channel') as NatsPubSubAsyncIterator<any>;
|
|
199
|
+
expect((iterator as any).subscriptionId).toBeNull();
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('should start with empty listeners array', () => {
|
|
203
|
+
const iterator = pubsub.asyncIterator('test.channel') as NatsPubSubAsyncIterator<any>;
|
|
204
|
+
expect((iterator as any).listeners).toEqual([]);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
describe('createNatsPubSub', () => {
|
|
210
|
+
it('should create NatsPubSub instance with given nats client', () => {
|
|
211
|
+
const natsClient = createMockNatsClient();
|
|
212
|
+
const pubsub = createNatsPubSub(natsClient);
|
|
213
|
+
|
|
214
|
+
expect(pubsub).toBeInstanceOf(NatsPubSub);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('should use the provided nats client for operations', async () => {
|
|
218
|
+
const natsClient = createMockNatsClient();
|
|
219
|
+
const pubsub = createNatsPubSub(natsClient);
|
|
220
|
+
|
|
221
|
+
await pubsub.publish('test', { data: 'test' });
|
|
222
|
+
expect(natsClient.publish).toHaveBeenCalled();
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe('PubSubProtocol', () => {
|
|
227
|
+
it('should have DEFAULT protocol set to DEFAULT string', () => {
|
|
228
|
+
expect(PubSubProtocol.DEFAULT).toBe('DEFAULT');
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('should have NATS protocol set to NATS string', () => {
|
|
232
|
+
expect(PubSubProtocol.NATS).toBe('NATS');
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('should have RABBITMQ protocol set to RABBITMQ string', () => {
|
|
236
|
+
expect(PubSubProtocol.RABBITMQ).toBe('RABBITMQ');
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
describe('GraphQLNatsModule', () => {
|
|
241
|
+
describe('forRoot', () => {
|
|
242
|
+
it('should have moduleName in metadata', () => {
|
|
243
|
+
const config = GraphQLNatsModule.forRoot() as any;
|
|
244
|
+
expect(config.metadata?.moduleName).toBe('GraphQLNatsModule');
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('should have moduleHash in metadata', () => {
|
|
248
|
+
const config = GraphQLNatsModule.forRoot() as any;
|
|
249
|
+
expect(config.metadata?.moduleHash).toBeDefined();
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('should have type as module in metadata', () => {
|
|
253
|
+
const config = GraphQLNatsModule.forRoot() as any;
|
|
254
|
+
expect(config.metadata?.type).toBe('module');
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('should have providers array with GraphQLNatsPubSubService in metadata raw source', () => {
|
|
258
|
+
const config = GraphQLNatsModule.forRoot() as any;
|
|
259
|
+
const raw = config.metadata?.raw || '';
|
|
260
|
+
expect(raw).toContain('GraphQLNatsPubSubService');
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
describe('NatsPubSubInterface contract', () => {
|
|
266
|
+
let natsClient: ReturnType<typeof createMockNatsClient>;
|
|
267
|
+
let pubsub: NatsPubSubInterface;
|
|
268
|
+
|
|
269
|
+
beforeEach(() => {
|
|
270
|
+
natsClient = createMockNatsClient();
|
|
271
|
+
pubsub = new NatsPubSub(natsClient);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('should implement publish method', () => {
|
|
275
|
+
expect(typeof pubsub.publish).toBe('function');
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('should implement subscribe method', () => {
|
|
279
|
+
expect(typeof pubsub.subscribe).toBe('function');
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
it('should implement unsubscribe method', () => {
|
|
283
|
+
expect(typeof pubsub.unsubscribe).toBe('function');
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('should implement asyncIterator method', () => {
|
|
287
|
+
expect(typeof pubsub.asyncIterator).toBe('function');
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('should implement asyncIterableIterator method', () => {
|
|
291
|
+
expect(typeof pubsub.asyncIterableIterator).toBe('function');
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('publish should return a promise', () => {
|
|
295
|
+
const result = pubsub.publish('test', {});
|
|
296
|
+
expect(result).toBeInstanceOf(Promise);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it('subscribe should return a promise that resolves to number', async () => {
|
|
300
|
+
const result = pubsub.subscribe('test', async () => {});
|
|
301
|
+
expect(result).toBeInstanceOf(Promise);
|
|
302
|
+
const subId = await result;
|
|
303
|
+
expect(typeof subId).toBe('number');
|
|
304
|
+
});
|
|
305
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Service } from '@rxdi/core';
|
|
2
|
+
import { Container } from '@rxdi/core';
|
|
3
|
+
import { NatsPubSub } from './nats-pubsub.class';
|
|
4
|
+
import { NatsPubSubInterface } from './interfaces';
|
|
5
|
+
|
|
6
|
+
@Service()
|
|
7
|
+
export class GraphQLNatsPubSubService implements NatsPubSubInterface {
|
|
8
|
+
natsPubSub: NatsPubSub;
|
|
9
|
+
|
|
10
|
+
constructor() {
|
|
11
|
+
const natsClient = (global as any).NatsClientService || Container.get('NatsClientService');
|
|
12
|
+
this.natsPubSub = new NatsPubSub(natsClient);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async publish(trigger: string, payload: any): Promise<void> {
|
|
16
|
+
return this.natsPubSub.publish(trigger, payload);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async subscribe<T>(
|
|
20
|
+
trigger: string,
|
|
21
|
+
onMessage: (m: T) => Promise<void>
|
|
22
|
+
): Promise<number> {
|
|
23
|
+
return this.natsPubSub.subscribe(trigger, onMessage);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async unsubscribe(subId: number): Promise<void> {
|
|
27
|
+
return this.natsPubSub.unsubscribe(subId);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
asyncIterator<T>(event: any): AsyncIterator<T> {
|
|
31
|
+
return this.natsPubSub.asyncIterableIterator<T>(event);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
asyncIterableIterator<T>(event: any): AsyncIterator<T> {
|
|
35
|
+
return this.natsPubSub.asyncIterableIterator<T>(event);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
publishSignal(signal: string, data: any): Promise<void> {
|
|
39
|
+
return this.natsPubSub.publish(signal, data);
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Module } from '@rxdi/core';
|
|
2
|
+
import { GraphQLNatsPubSubService } from './graphql-nats-pubsub.service';
|
|
3
|
+
|
|
4
|
+
@Module()
|
|
5
|
+
export class GraphQLNatsModule {
|
|
6
|
+
static forRoot() {
|
|
7
|
+
return {
|
|
8
|
+
module: GraphQLNatsModule,
|
|
9
|
+
providers: [GraphQLNatsPubSubService],
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export * from './nats-pubsub.class';
|
|
15
|
+
export * from './graphql-nats-pubsub.service';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export enum PubSubProtocol {
|
|
2
|
+
DEFAULT = 'DEFAULT',
|
|
3
|
+
NATS = 'NATS',
|
|
4
|
+
RABBITMQ = 'RABBITMQ',
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface NatsPubSubInterface {
|
|
8
|
+
publish(trigger: string, payload: any): Promise<void>;
|
|
9
|
+
subscribe<T>(trigger: string, onMessage: (m: T) => Promise<void>): Promise<number>;
|
|
10
|
+
unsubscribe(subId: number): void;
|
|
11
|
+
asyncIterator<T>(triggers: string | string[]): AsyncIterator<T>;
|
|
12
|
+
asyncIterableIterator<T>(triggers: string | string[]): AsyncIterator<T>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { NatsPubSubInterface } from './interfaces';
|
|
2
|
+
|
|
3
|
+
export class NatsPubSub implements NatsPubSubInterface {
|
|
4
|
+
private subscriptionMap = new Map<number, [string, (m: any) => Promise<void>]>();
|
|
5
|
+
private subsRefsMap = new Map<string, number[]>();
|
|
6
|
+
private currentSubscriptionId = 0;
|
|
7
|
+
private unsubscribeMap = new Map<string, () => void>();
|
|
8
|
+
private subscriptionIds: Map<string, number> = new Map();
|
|
9
|
+
private natsClient: any;
|
|
10
|
+
|
|
11
|
+
constructor(natsClient: any) {
|
|
12
|
+
this.natsClient = natsClient;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async publish(trigger: string, payload: any): Promise<void> {
|
|
16
|
+
await this.natsClient.publish(trigger, payload);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async subscribe<T>(
|
|
20
|
+
trigger: string,
|
|
21
|
+
onMessage: (m: T) => Promise<void>
|
|
22
|
+
): Promise<number> {
|
|
23
|
+
const id = this.currentSubscriptionId++;
|
|
24
|
+
|
|
25
|
+
this.subscriptionMap.set(id, [trigger, onMessage as (m: any) => Promise<void>]);
|
|
26
|
+
|
|
27
|
+
const refs = this.subsRefsMap.get(trigger);
|
|
28
|
+
if (refs?.length) {
|
|
29
|
+
this.subsRefsMap.set(trigger, [...refs, id]);
|
|
30
|
+
return id;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const subId = await this.natsClient.subscribe(trigger, async (msg: any) => {
|
|
34
|
+
const subscribers = this.subsRefsMap.get(trigger);
|
|
35
|
+
if (!subscribers?.length) return;
|
|
36
|
+
|
|
37
|
+
for (const sId of subscribers) {
|
|
38
|
+
const entry = this.subscriptionMap.get(sId);
|
|
39
|
+
if (!entry) continue;
|
|
40
|
+
const [, listener] = entry;
|
|
41
|
+
await listener(msg);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
this.subscriptionIds.set(trigger, subId);
|
|
46
|
+
this.subsRefsMap.set(trigger, [id]);
|
|
47
|
+
|
|
48
|
+
return id;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async unsubscribe(subId: number): Promise<void> {
|
|
52
|
+
const entry = this.subscriptionMap.get(subId);
|
|
53
|
+
const triggerName = entry?.[0];
|
|
54
|
+
|
|
55
|
+
if (!triggerName) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const refs = this.subsRefsMap.get(triggerName);
|
|
60
|
+
if (!refs) return;
|
|
61
|
+
|
|
62
|
+
if (refs.length === 1) {
|
|
63
|
+
const natsSubId = this.subscriptionIds.get(triggerName);
|
|
64
|
+
if (natsSubId !== undefined) {
|
|
65
|
+
this.natsClient.unsubscribe(natsSubId);
|
|
66
|
+
this.subscriptionIds.delete(triggerName);
|
|
67
|
+
}
|
|
68
|
+
this.subsRefsMap.delete(triggerName);
|
|
69
|
+
} else {
|
|
70
|
+
this.subsRefsMap.set(
|
|
71
|
+
triggerName,
|
|
72
|
+
refs.filter((id) => id !== subId)
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
this.subscriptionMap.delete(subId);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
asyncIterator<T>(triggers: string | string[]): AsyncIterator<T> {
|
|
80
|
+
return new NatsPubSubAsyncIterator<T>(this, triggers);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
asyncIterableIterator<T>(triggers: string | string[]): AsyncIterator<T> {
|
|
84
|
+
return new NatsPubSubAsyncIterator<T>(this, triggers);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export class NatsPubSubAsyncIterator<T> implements AsyncIterator<T> {
|
|
89
|
+
private subscriptionId: number | null = null;
|
|
90
|
+
private listeners: Array<(value: T) => void> = [];
|
|
91
|
+
private resolveReturn!: (value: { value: T; done: boolean }) => void;
|
|
92
|
+
private rejectPromise!: (reason: any) => void;
|
|
93
|
+
private returnPromise: Promise<{ value: T; done: boolean }>;
|
|
94
|
+
|
|
95
|
+
constructor(
|
|
96
|
+
private pubsub: NatsPubSubInterface,
|
|
97
|
+
private triggers: string | string[]
|
|
98
|
+
) {
|
|
99
|
+
this.returnPromise = new Promise<{ value: T; done: boolean }>((resolve, reject) => {
|
|
100
|
+
this.resolveReturn = resolve;
|
|
101
|
+
this.rejectPromise = reject;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async next(): Promise<{ value: T; done: boolean }> {
|
|
106
|
+
if (!this.subscriptionId) {
|
|
107
|
+
const trigger = Array.isArray(this.triggers) ? this.triggers[0] : this.triggers;
|
|
108
|
+
this.subscriptionId = await this.pubsub.subscribe(
|
|
109
|
+
trigger,
|
|
110
|
+
async (message: T) => {
|
|
111
|
+
for (const listener of this.listeners) {
|
|
112
|
+
listener(message);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
) as any;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return new Promise<{ value: T; done: boolean }>((resolve) => {
|
|
119
|
+
const listener = (value: T) => {
|
|
120
|
+
this.listeners = this.listeners.filter((l) => l !== listener);
|
|
121
|
+
resolve({ value, done: false });
|
|
122
|
+
};
|
|
123
|
+
this.listeners.push(listener);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async return(): Promise<{ value: T; done: boolean }> {
|
|
128
|
+
if (this.subscriptionId) {
|
|
129
|
+
await this.pubsub.unsubscribe(this.subscriptionId);
|
|
130
|
+
this.subscriptionId = null;
|
|
131
|
+
}
|
|
132
|
+
return { value: undefined as any, done: true };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async throw?(error?: any): Promise<never> {
|
|
136
|
+
if (this.subscriptionId) {
|
|
137
|
+
await this.pubsub.unsubscribe(this.subscriptionId);
|
|
138
|
+
this.subscriptionId = null;
|
|
139
|
+
}
|
|
140
|
+
return Promise.reject(error);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
[Symbol.asyncIterator](): AsyncIterator<T> {
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function createNatsPubSub(natsClient: any): NatsPubSub {
|
|
149
|
+
return new NatsPubSub(natsClient);
|
|
150
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { GraphQLNatsPubSubService } from '../src/graphql-nats-pubsub.service';
|
|
2
|
+
import { NatsPubSub } from '../src/nats-pubsub.class';
|
|
3
|
+
|
|
4
|
+
jest.mock('../src/nats-pubsub.class', () => ({
|
|
5
|
+
NatsPubSub: jest.fn().mockImplementation(() => ({
|
|
6
|
+
publish: jest.fn().mockResolvedValue(undefined),
|
|
7
|
+
subscribe: jest.fn().mockResolvedValue(1),
|
|
8
|
+
unsubscribe: jest.fn().mockResolvedValue(undefined),
|
|
9
|
+
asyncIterableIterator: jest.fn().mockReturnValue({
|
|
10
|
+
next: jest.fn(),
|
|
11
|
+
return: jest.fn(),
|
|
12
|
+
throw: jest.fn(),
|
|
13
|
+
[Symbol.asyncIterator]: function() { return this; }
|
|
14
|
+
}),
|
|
15
|
+
asyncIterator: jest.fn().mockReturnValue({
|
|
16
|
+
next: jest.fn(),
|
|
17
|
+
return: jest.fn(),
|
|
18
|
+
throw: jest.fn(),
|
|
19
|
+
[Symbol.asyncIterator]: function() { return this; }
|
|
20
|
+
}),
|
|
21
|
+
})),
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
jest.mock('@rxdi/core', () => ({
|
|
25
|
+
Container: {
|
|
26
|
+
get: jest.fn().mockReturnValue({
|
|
27
|
+
isReady: jest.fn().mockReturnValue(true),
|
|
28
|
+
}),
|
|
29
|
+
},
|
|
30
|
+
Service: () => () => {},
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
(global as any).NatsClientService = {
|
|
34
|
+
isReady: jest.fn().mockReturnValue(true),
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
describe('GraphQLNatsPubSubService', () => {
|
|
38
|
+
let service: GraphQLNatsPubSubService;
|
|
39
|
+
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
jest.clearAllMocks();
|
|
42
|
+
service = new GraphQLNatsPubSubService();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('constructor', () => {
|
|
46
|
+
it('should create NatsPubSub instance', () => {
|
|
47
|
+
expect(NatsPubSub).toHaveBeenCalled();
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('publish', () => {
|
|
52
|
+
it('should call natsPubSub.publish', async () => {
|
|
53
|
+
await service.publish('game.crash', { crashPoint: 2.5 });
|
|
54
|
+
expect(service.natsPubSub.publish).toHaveBeenCalledWith('game.crash', { crashPoint: 2.5 });
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should publish string data', async () => {
|
|
58
|
+
await service.publish('test.channel', 'hello');
|
|
59
|
+
expect(service.natsPubSub.publish).toHaveBeenCalledWith('test.channel', 'hello');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('subscribe', () => {
|
|
64
|
+
it('should call natsPubSub.subscribe', async () => {
|
|
65
|
+
const callback = jest.fn();
|
|
66
|
+
const subId = await service.subscribe('test.channel', callback);
|
|
67
|
+
expect(service.natsPubSub.subscribe).toHaveBeenCalledWith('test.channel', callback);
|
|
68
|
+
expect(subId).toBe(1);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('unsubscribe', () => {
|
|
73
|
+
it('should call natsPubSub.unsubscribe', async () => {
|
|
74
|
+
await service.unsubscribe(1);
|
|
75
|
+
expect(service.natsPubSub.unsubscribe).toHaveBeenCalledWith(1);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe('asyncIterator', () => {
|
|
80
|
+
it('should return iterator from natsPubSub', () => {
|
|
81
|
+
const iterator = service.asyncIterator('test.channel');
|
|
82
|
+
expect(service.natsPubSub.asyncIterableIterator).toHaveBeenCalledWith('test.channel');
|
|
83
|
+
expect(iterator).toBeDefined();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('asyncIterableIterator', () => {
|
|
88
|
+
it('should return iterator from natsPubSub', () => {
|
|
89
|
+
const iterator = service.asyncIterableIterator('test.channel');
|
|
90
|
+
expect(service.natsPubSub.asyncIterableIterator).toHaveBeenCalledWith('test.channel');
|
|
91
|
+
expect(iterator).toBeDefined();
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('publishSignal', () => {
|
|
96
|
+
it('should call natsPubSub.publish', async () => {
|
|
97
|
+
await service.publishSignal('game.crash', { crashPoint: 1.5 });
|
|
98
|
+
expect(service.natsPubSub.publish).toHaveBeenCalledWith('game.crash', { crashPoint: 1.5 });
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { NatsPubSub, NatsPubSubAsyncIterator, createNatsPubSub } from '../src/nats-pubsub.class';
|
|
2
|
+
import { NatsPubSubInterface } from '../src/interfaces';
|
|
3
|
+
|
|
4
|
+
const createMockNatsClient = () => {
|
|
5
|
+
const subscriptions = new Map<number, { unsubscribe: jest.Mock }>();
|
|
6
|
+
let subscriptionId = 0;
|
|
7
|
+
|
|
8
|
+
return {
|
|
9
|
+
publish: jest.fn().mockResolvedValue(undefined),
|
|
10
|
+
subscribe: jest.fn().mockImplementation((channel: string, callback: (msg: any) => void) => {
|
|
11
|
+
const id = ++subscriptionId;
|
|
12
|
+
subscriptions.set(id, { unsubscribe: jest.fn() });
|
|
13
|
+
return id;
|
|
14
|
+
}),
|
|
15
|
+
unsubscribe: jest.fn().mockImplementation((id: number) => {
|
|
16
|
+
const sub = subscriptions.get(id);
|
|
17
|
+
if (sub) {
|
|
18
|
+
sub.unsubscribe();
|
|
19
|
+
subscriptions.delete(id);
|
|
20
|
+
}
|
|
21
|
+
}),
|
|
22
|
+
isReady: jest.fn().mockReturnValue(true),
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
describe('NatsPubSub', () => {
|
|
27
|
+
let mockNatsClient: ReturnType<typeof createMockNatsClient>;
|
|
28
|
+
let pubsub: NatsPubSub;
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
mockNatsClient = createMockNatsClient();
|
|
32
|
+
pubsub = new NatsPubSub(mockNatsClient);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
jest.clearAllMocks();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('publish', () => {
|
|
40
|
+
it('should publish message to trigger channel', async () => {
|
|
41
|
+
await pubsub.publish('game.crash', { crashPoint: 2.5 });
|
|
42
|
+
expect(mockNatsClient.publish).toHaveBeenCalledWith('game.crash', { crashPoint: 2.5 });
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should publish with string payload', async () => {
|
|
46
|
+
await pubsub.publish('test.channel', 'hello');
|
|
47
|
+
expect(mockNatsClient.publish).toHaveBeenCalledWith('test.channel', 'hello');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should publish multiple messages', async () => {
|
|
51
|
+
await pubsub.publish('channel.1', { data: 1 });
|
|
52
|
+
await pubsub.publish('channel.2', { data: 2 });
|
|
53
|
+
await pubsub.publish('channel.3', { data: 3 });
|
|
54
|
+
expect(mockNatsClient.publish).toHaveBeenCalledTimes(3);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe('subscribe', () => {
|
|
59
|
+
it('should subscribe to channel and return subscription id', async () => {
|
|
60
|
+
const callback = jest.fn();
|
|
61
|
+
const subId = await pubsub.subscribe('test.channel', callback);
|
|
62
|
+
expect(subId).toBeDefined();
|
|
63
|
+
expect(typeof subId).toBe('number');
|
|
64
|
+
expect(mockNatsClient.subscribe).toHaveBeenCalledWith('test.channel', expect.any(Function));
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should call callback when message is published', async () => {
|
|
68
|
+
const callback = jest.fn();
|
|
69
|
+
await pubsub.subscribe('game.crash', callback);
|
|
70
|
+
const subscribeCallback = mockNatsClient.subscribe.mock.calls[0][1];
|
|
71
|
+
const message = { crashPoint: 1.5, timestamp: Date.now() };
|
|
72
|
+
await subscribeCallback(message);
|
|
73
|
+
expect(callback).toHaveBeenCalledWith(message);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should handle multiple subscribers on same channel', async () => {
|
|
77
|
+
const callback1 = jest.fn();
|
|
78
|
+
const callback2 = jest.fn();
|
|
79
|
+
const subId1 = await pubsub.subscribe('game.crash', callback1);
|
|
80
|
+
const subId2 = await pubsub.subscribe('game.crash', callback2);
|
|
81
|
+
expect(subId1).not.toBe(subId2);
|
|
82
|
+
const subscribeCallback = mockNatsClient.subscribe.mock.calls[0][1];
|
|
83
|
+
const message = { crashPoint: 2.0 };
|
|
84
|
+
await subscribeCallback(message);
|
|
85
|
+
expect(callback1).toHaveBeenCalledWith(message);
|
|
86
|
+
expect(callback2).toHaveBeenCalledWith(message);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should return different subscription ids for different channels', async () => {
|
|
90
|
+
const callback1 = jest.fn();
|
|
91
|
+
const callback2 = jest.fn();
|
|
92
|
+
const subId1 = await pubsub.subscribe('channel.1', callback1);
|
|
93
|
+
const subId2 = await pubsub.subscribe('channel.2', callback2);
|
|
94
|
+
expect(subId1).not.toBe(subId2);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('unsubscribe', () => {
|
|
99
|
+
it('should unsubscribe from channel', async () => {
|
|
100
|
+
const callback = jest.fn();
|
|
101
|
+
const subId = await pubsub.subscribe('test.channel', callback);
|
|
102
|
+
await pubsub.unsubscribe(subId);
|
|
103
|
+
expect(mockNatsClient.unsubscribe).toHaveBeenCalled();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('should handle unsubscribe from non-existent subscription', async () => {
|
|
107
|
+
await expect(pubsub.unsubscribe(9999)).resolves.not.toThrow();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should not unsubscribe nats client when multiple subscribers exist', async () => {
|
|
111
|
+
const callback1 = jest.fn();
|
|
112
|
+
const callback2 = jest.fn();
|
|
113
|
+
const subId1 = await pubsub.subscribe('game.crash', callback1);
|
|
114
|
+
const subId2 = await pubsub.subscribe('game.crash', callback2);
|
|
115
|
+
await pubsub.unsubscribe(subId1);
|
|
116
|
+
expect(mockNatsClient.unsubscribe).not.toHaveBeenCalled();
|
|
117
|
+
await pubsub.unsubscribe(subId2);
|
|
118
|
+
expect(mockNatsClient.unsubscribe).toHaveBeenCalled();
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('asyncIterator', () => {
|
|
123
|
+
it('should return NatsPubSubAsyncIterator instance', () => {
|
|
124
|
+
const iterator = pubsub.asyncIterator('test.channel');
|
|
125
|
+
expect(iterator).toBeInstanceOf(NatsPubSubAsyncIterator);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('should return iterator for asyncIterableIterator', () => {
|
|
129
|
+
const iterator = pubsub.asyncIterableIterator('test.channel');
|
|
130
|
+
expect(iterator).toBeInstanceOf(NatsPubSubAsyncIterator);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe('NatsPubSubAsyncIterator', () => {
|
|
136
|
+
let mockNatsClient: ReturnType<typeof createMockNatsClient>;
|
|
137
|
+
let mockPubSub: NatsPubSubInterface;
|
|
138
|
+
let iterator: NatsPubSubAsyncIterator<{ crashPoint: number }>;
|
|
139
|
+
|
|
140
|
+
beforeEach(() => {
|
|
141
|
+
mockNatsClient = createMockNatsClient();
|
|
142
|
+
mockPubSub = new NatsPubSub(mockNatsClient);
|
|
143
|
+
iterator = new NatsPubSubAsyncIterator(mockPubSub, 'game.crash');
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe('next', () => {
|
|
147
|
+
it('should subscribe to trigger on first call', async () => {
|
|
148
|
+
iterator.next();
|
|
149
|
+
await Promise.resolve();
|
|
150
|
+
expect(mockNatsClient.subscribe).toHaveBeenCalledWith('game.crash', expect.any(Function));
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe('return', () => {
|
|
155
|
+
it('should return done: true', async () => {
|
|
156
|
+
const result = await iterator.return();
|
|
157
|
+
expect(result).toEqual({ value: undefined, done: true });
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe('throw', () => {
|
|
162
|
+
it('should reject with error', async () => {
|
|
163
|
+
await expect(iterator.throw!(new Error('test error'))).rejects.toThrow('test error');
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe('Symbol.asyncIterator', () => {
|
|
168
|
+
it('should return itself', () => {
|
|
169
|
+
expect(iterator[Symbol.asyncIterator]()).toBe(iterator);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe('createNatsPubSub factory', () => {
|
|
175
|
+
it('should create NatsPubSub instance', () => {
|
|
176
|
+
const mockClient = createMockNatsClient();
|
|
177
|
+
const pubsub = createNatsPubSub(mockClient);
|
|
178
|
+
expect(pubsub).toBeInstanceOf(NatsPubSub);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe('NatsPubSub Integration Style Tests', () => {
|
|
183
|
+
let mockNatsClient: ReturnType<typeof createMockNatsClient>;
|
|
184
|
+
let pubsub: NatsPubSub;
|
|
185
|
+
|
|
186
|
+
beforeEach(() => {
|
|
187
|
+
mockNatsClient = createMockNatsClient();
|
|
188
|
+
pubsub = new NatsPubSub(mockNatsClient);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('should handle pubsub workflow like crash game events', async () => {
|
|
192
|
+
const crashEvents: any[] = [];
|
|
193
|
+
const cashOutEvents: any[] = [];
|
|
194
|
+
|
|
195
|
+
const crashSubId = await pubsub.subscribe('game.crash', (msg) => {
|
|
196
|
+
crashEvents.push(msg);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const cashOutSubId = await pubsub.subscribe('game.cashout', (msg) => {
|
|
200
|
+
cashOutEvents.push(msg);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
expect(crashSubId).toBeDefined();
|
|
204
|
+
expect(cashOutSubId).toBeDefined();
|
|
205
|
+
expect(crashSubId).not.toBe(cashOutSubId);
|
|
206
|
+
|
|
207
|
+
const crashCallback = mockNatsClient.subscribe.mock.calls[0][1];
|
|
208
|
+
const cashOutCallback = mockNatsClient.subscribe.mock.calls[1][1];
|
|
209
|
+
|
|
210
|
+
await crashCallback({ crashPoint: 1.23, timestamp: Date.now() });
|
|
211
|
+
await cashOutCallback({ playerId: 'player1', amount: 50 });
|
|
212
|
+
|
|
213
|
+
expect(crashEvents).toHaveLength(1);
|
|
214
|
+
expect(crashEvents[0].crashPoint).toBe(1.23);
|
|
215
|
+
expect(cashOutEvents).toHaveLength(1);
|
|
216
|
+
expect(cashOutEvents[0].playerId).toBe('player1');
|
|
217
|
+
|
|
218
|
+
await pubsub.unsubscribe(crashSubId);
|
|
219
|
+
|
|
220
|
+
await crashCallback({ crashPoint: 5.67, timestamp: Date.now() });
|
|
221
|
+
|
|
222
|
+
expect(crashEvents).toHaveLength(1);
|
|
223
|
+
expect(cashOutEvents).toHaveLength(1);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('should handle bet placement workflow', async () => {
|
|
227
|
+
const betPlacedEvents: any[] = [];
|
|
228
|
+
const subId = await pubsub.subscribe('game.bet.placed', async (msg) => {
|
|
229
|
+
betPlacedEvents.push(msg);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
const callback = mockNatsClient.subscribe.mock.calls[0][1];
|
|
233
|
+
|
|
234
|
+
await callback({
|
|
235
|
+
betId: 'bet_123',
|
|
236
|
+
playerId: 'player_abc',
|
|
237
|
+
amount: 100,
|
|
238
|
+
autoCashOut: 2.0
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
expect(betPlacedEvents).toHaveLength(1);
|
|
242
|
+
expect(betPlacedEvents[0]).toEqual({
|
|
243
|
+
betId: 'bet_123',
|
|
244
|
+
playerId: 'player_abc',
|
|
245
|
+
amount: 100,
|
|
246
|
+
autoCashOut: 2.0
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
await pubsub.unsubscribe(subId);
|
|
250
|
+
});
|
|
251
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2018",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"lib": ["ES2018"],
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"noImplicitAny": true,
|
|
9
|
+
"strictNullChecks": true,
|
|
10
|
+
"noImplicitThis": true,
|
|
11
|
+
"alwaysStrict": true,
|
|
12
|
+
"noUnusedLocals": false,
|
|
13
|
+
"noUnusedParameters": false,
|
|
14
|
+
"noImplicitReturns": true,
|
|
15
|
+
"noFallthroughCasesInSwitch": false,
|
|
16
|
+
"experimentalDecorators": true,
|
|
17
|
+
"emitDecoratorMetadata": true,
|
|
18
|
+
"outDir": "./dist",
|
|
19
|
+
"rootDir": "./src",
|
|
20
|
+
"sourceMap": true,
|
|
21
|
+
"skipLibCheck": true,
|
|
22
|
+
"moduleResolution": "node"
|
|
23
|
+
},
|
|
24
|
+
"include": ["src/**/*"],
|
|
25
|
+
"exclude": ["node_modules", "dist", "**/*.spec.ts"]
|
|
26
|
+
}
|