@commandkit/queue 0.1.0-dev.20250709035032
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 +11 -0
- package/README.md +54 -0
- package/dist/discordjs/index.d.ts +12 -0
- package/dist/discordjs/index.js +34 -0
- package/dist/driver.d.ts +6 -0
- package/dist/driver.js +3 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +51 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Copyright 2025 Avraj Sahota
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
4
|
+
documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
|
|
5
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
6
|
+
persons to whom the Software is furnished to do so.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
9
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
10
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
11
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# @commandkit/queue
|
|
2
|
+
|
|
3
|
+
Service agnostic message queue api for commandkit
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @commandkit/queue
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Driver configuration
|
|
14
|
+
|
|
15
|
+
#### `@discordjs/brokers`
|
|
16
|
+
|
|
17
|
+
First you need to install the dependencies:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @discordjs/brokers ioredis
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import Redis from 'ioredis';
|
|
25
|
+
import { PubSubRedisBroker } from '@discordjs/brokers';
|
|
26
|
+
import { RedisPubSubDriver } from '@commandkit/queue/discordjs';
|
|
27
|
+
import { setDriver } from '@commandkit/queue';
|
|
28
|
+
|
|
29
|
+
const broker = new PubSubRedisBroker(new Redis());
|
|
30
|
+
const driver = new RedisPubSubDriver(broker);
|
|
31
|
+
|
|
32
|
+
setDriver(driver);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Example of sending and receiving a message
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { send, receive } from '@commandkit/queue';
|
|
39
|
+
|
|
40
|
+
// publisher
|
|
41
|
+
await send('topic', { message: 'Hello World!' });
|
|
42
|
+
|
|
43
|
+
// subscriber
|
|
44
|
+
await receive('topic', (m) => {
|
|
45
|
+
console.log(m.message); // "Hello World!"
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Documentation
|
|
50
|
+
https://commandkit.dev/docs/next/api-reference/queue
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
MIT © Avraj Sahota
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Awaitable, MessageQueue } from '../driver';
|
|
2
|
+
import type { PubSubRedisBroker } from '@discordjs/brokers';
|
|
3
|
+
type Handler<TEvent extends Record<string, any> = Record<string, any>> = (message: TEvent[keyof TEvent]) => Awaitable<void>;
|
|
4
|
+
export declare class RedisPubSubDriver<TEvent extends Record<string, any> = Record<string, any>> implements MessageQueue {
|
|
5
|
+
readonly broker: PubSubRedisBroker<TEvent>;
|
|
6
|
+
private handlers;
|
|
7
|
+
constructor(broker: PubSubRedisBroker<TEvent>);
|
|
8
|
+
send<T extends keyof TEvent>(topic: T, message: TEvent[T]): Promise<void>;
|
|
9
|
+
receive<T extends keyof TEvent>(topic: T, handler: Handler<TEvent>): Promise<void>;
|
|
10
|
+
close(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RedisPubSubDriver = void 0;
|
|
4
|
+
class RedisPubSubDriver {
|
|
5
|
+
constructor(broker) {
|
|
6
|
+
this.broker = broker;
|
|
7
|
+
this.handlers = new Map();
|
|
8
|
+
}
|
|
9
|
+
async send(topic, message) {
|
|
10
|
+
await this.broker.publish(topic, message);
|
|
11
|
+
}
|
|
12
|
+
async receive(topic, handler) {
|
|
13
|
+
await this.broker.subscribe([topic]);
|
|
14
|
+
if (!this.handlers.has(topic)) {
|
|
15
|
+
const fn = async ({ data, ack }) => {
|
|
16
|
+
await handler(data);
|
|
17
|
+
void ack();
|
|
18
|
+
};
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
this.broker.on(topic, fn);
|
|
21
|
+
this.handlers.set(topic, fn);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async close() {
|
|
25
|
+
for (const [topic, handler] of this.handlers) {
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
this.broker.removeListener(topic, handler);
|
|
28
|
+
}
|
|
29
|
+
this.handlers.clear();
|
|
30
|
+
await this.broker.destroy();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.RedisPubSubDriver = RedisPubSubDriver;
|
|
34
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZGl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FLLEVBQUUsT0FBTyxDQUFDLElBQUksSUFBSSxDQUFDLFFBQVEsRUFBRSxDQUFDO1lBQzdDLGFBQWE7WUFDYixJQUFJLENBQUMsTUFBTSxDQUFDLGNBQWMsQ0FBQyxLQUFLLEVBQUUsT0FBTyxDQUFDLENBQUM7UUFDN0MsQ0FBQztRQUNELElBQUksQ0FBQyxRQUFRLENBQUMsS0FBSyxFQUFFLENBQUM7UUFFdEIsTUFBTSxJQUFJLENBQUMsTUFBTSxDQUFDLE9BQU8sRUFBRSxDQUFDO0lBQzlCLENBQUM7Q0FDRjtBQXpDRCw4Q0F5Q0MifQ==
|
package/dist/driver.d.ts
ADDED
package/dist/driver.js
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { MessageQueue, Awaitable } from './driver';
|
|
2
|
+
/**
|
|
3
|
+
* Set the message queue driver.
|
|
4
|
+
* @param driver The message queue driver to use.
|
|
5
|
+
*/
|
|
6
|
+
export declare function setDriver(driver: MessageQueue): void;
|
|
7
|
+
/**
|
|
8
|
+
* Send a message to a topic.
|
|
9
|
+
* @param topic The topic to send the message to.
|
|
10
|
+
* @param message The message to send.
|
|
11
|
+
*/
|
|
12
|
+
export declare function send<T>(topic: string, message: T): Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* Receive a message from a topic.
|
|
15
|
+
* @param topic The topic to receive the message from.
|
|
16
|
+
* @param handler The handler to process the message.
|
|
17
|
+
*/
|
|
18
|
+
export declare function receive<T>(topic: string, handler: (message: T) => Awaitable<void>): Promise<void>;
|
|
19
|
+
export * from './driver';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.setDriver = setDriver;
|
|
18
|
+
exports.send = send;
|
|
19
|
+
exports.receive = receive;
|
|
20
|
+
let _driver = null;
|
|
21
|
+
/**
|
|
22
|
+
* Set the message queue driver.
|
|
23
|
+
* @param driver The message queue driver to use.
|
|
24
|
+
*/
|
|
25
|
+
function setDriver(driver) {
|
|
26
|
+
_driver = driver;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Send a message to a topic.
|
|
30
|
+
* @param topic The topic to send the message to.
|
|
31
|
+
* @param message The message to send.
|
|
32
|
+
*/
|
|
33
|
+
function send(topic, message) {
|
|
34
|
+
if (!_driver) {
|
|
35
|
+
throw new Error('Message queue driver has not been set');
|
|
36
|
+
}
|
|
37
|
+
return _driver.send(topic, message);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Receive a message from a topic.
|
|
41
|
+
* @param topic The topic to receive the message from.
|
|
42
|
+
* @param handler The handler to process the message.
|
|
43
|
+
*/
|
|
44
|
+
function receive(topic, handler) {
|
|
45
|
+
if (!_driver) {
|
|
46
|
+
throw new Error('Message queue driver has not been set');
|
|
47
|
+
}
|
|
48
|
+
return _driver.receive(topic, handler);
|
|
49
|
+
}
|
|
50
|
+
__exportStar(require("./driver"), exports);
|
|
51
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7Ozs7OztBQVFBLDhCQUVDO0FBT0Qsb0JBTUM7QUFPRCwwQkFTQztBQXJDRCxJQUFJLE9BQU8sR0FBd0IsSUFBSSxDQUFDO0FBRXhDOzs7R0FHRztBQUNILFNBQWdCLFNBQVMsQ0FBQyxNQUFvQjtJQUM1QyxPQUFPLEdBQUcsTUFBTSxDQUFDO0FBQ25CLENBQUM7QUFFRDs7OztHQUlHO0FBQ0gsU0FBZ0IsSUFBSSxDQUFJLEtBQWEsRUFBRSxPQUFVO0lBQy9DLElBQUksQ0FBQyxPQUFPLEVBQUUsQ0FBQztRQUNiLE1BQU0sSUFBSSxLQUFLLENBQUMsdUNBQXVDLENBQUMsQ0FBQztJQUMzRCxDQUFDO0lBRUQsT0FBTyxPQUFPLENBQUMsSUFBSSxDQUFDLEtBQUssRUFBRSxPQUFPLENBQUMsQ0FBQztBQUN0QyxDQUFDO0FBRUQ7Ozs7R0FJRztBQUNILFNBQWdCLE9BQU8sQ0FDckIsS0FBYSxFQUNiLE9BQXdDO0lBRXhDLElBQUksQ0FBQyxPQUFPLEVBQUUsQ0FBQztRQUNiLE1BQU0sSUFBSSxLQUFLLENBQUMsdUNBQXVDLENBQUMsQ0FBQztJQUMzRCxDQUFDO0lBRUQsT0FBTyxPQUFPLENBQUMsT0FBTyxDQUFDLEtBQUssRUFBRSxPQUFPLENBQUMsQ0FBQztBQUN6QyxDQUFDO0FBRUQsMkNBQXlCIn0=
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@commandkit/queue",
|
|
3
|
+
"version": "0.1.0-dev.20250709035032",
|
|
4
|
+
"description": "Service agnostic message queue api for commandkit",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./discordjs": {
|
|
16
|
+
"types": "./dist/discordjs/index.d.ts",
|
|
17
|
+
"import": "./dist/discordjs/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/underctrl-io/commandkit.git",
|
|
23
|
+
"directory": "packages/queue"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"commandkit",
|
|
27
|
+
"queue"
|
|
28
|
+
],
|
|
29
|
+
"contributors": [
|
|
30
|
+
"Twilight <hello@twlite.dev>",
|
|
31
|
+
"Avraj <avraj@underctrl.io>"
|
|
32
|
+
],
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/underctrl-io/commandkit/issues"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/underctrl-io/commandkit#readme",
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@discordjs/brokers": "^1.0.0",
|
|
40
|
+
"typescript": "^5.8.3",
|
|
41
|
+
"tsconfig": "0.0.0-dev.20250709035032"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"lint": "tsc --noEmit",
|
|
45
|
+
"build": "tsc"
|
|
46
|
+
}
|
|
47
|
+
}
|