@objectstack/service-queue 3.0.7
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/.turbo/turbo-build.log +22 -0
- package/CHANGELOG.md +10 -0
- package/LICENSE +202 -0
- package/dist/index.cjs +131 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +108 -0
- package/dist/index.d.ts +108 -0
- package/dist/index.js +102 -0
- package/dist/index.js.map +1 -0
- package/package.json +29 -0
- package/src/bullmq-queue-adapter.ts +58 -0
- package/src/index.ts +8 -0
- package/src/memory-queue-adapter.test.ts +87 -0
- package/src/memory-queue-adapter.ts +82 -0
- package/src/queue-service-plugin.ts +62 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
> @objectstack/service-queue@3.0.7 build /home/runner/work/spec/spec/packages/services/service-queue
|
|
3
|
+
> tsup --config ../../../tsup.config.ts
|
|
4
|
+
|
|
5
|
+
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v8.5.1
|
|
8
|
+
[34mCLI[39m Using tsup config: /home/runner/work/spec/spec/tsup.config.ts
|
|
9
|
+
[34mCLI[39m Target: es2020
|
|
10
|
+
[34mCLI[39m Cleaning output folder
|
|
11
|
+
[34mESM[39m Build start
|
|
12
|
+
[34mCJS[39m Build start
|
|
13
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m3.89 KB[39m
|
|
14
|
+
[32mCJS[39m [1mdist/index.cjs.map [22m[32m8.90 KB[39m
|
|
15
|
+
[32mCJS[39m ⚡️ Build success in 75ms
|
|
16
|
+
[32mESM[39m [1mdist/index.js [22m[32m2.78 KB[39m
|
|
17
|
+
[32mESM[39m [1mdist/index.js.map [22m[32m8.33 KB[39m
|
|
18
|
+
[32mESM[39m ⚡️ Build success in 76ms
|
|
19
|
+
[34mDTS[39m Build start
|
|
20
|
+
[32mDTS[39m ⚡️ Build success in 12557ms
|
|
21
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m3.63 KB[39m
|
|
22
|
+
[32mDTS[39m [1mdist/index.d.cts [22m[32m3.63 KB[39m
|
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute
|
|
108
|
+
must include a readable copy of the attribution notices
|
|
109
|
+
contained within such NOTICE file, excluding those notices
|
|
110
|
+
that do not pertain to any part of the Derivative Works,
|
|
111
|
+
in at least one of the following places: within a NOTICE
|
|
112
|
+
text file distributed as part of the Derivative Works; within
|
|
113
|
+
the Source form or documentation, if provided along with
|
|
114
|
+
the Derivative Works; or, within a display generated by the
|
|
115
|
+
Derivative Works, if and wherever such third-party notices
|
|
116
|
+
normally appear. The contents of the NOTICE file are for
|
|
117
|
+
informational purposes only and do not modify the License.
|
|
118
|
+
You may add Your own attribution notices within Derivative
|
|
119
|
+
Works that You distribute, alongside or as an addendum to
|
|
120
|
+
the NOTICE text from the Work, provided that such additional
|
|
121
|
+
attribution notices cannot be construed as modifying the
|
|
122
|
+
License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 ObjectStack
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
BullMQQueueAdapter: () => BullMQQueueAdapter,
|
|
24
|
+
MemoryQueueAdapter: () => MemoryQueueAdapter,
|
|
25
|
+
QueueServicePlugin: () => QueueServicePlugin
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// src/memory-queue-adapter.ts
|
|
30
|
+
var MemoryQueueAdapter = class {
|
|
31
|
+
constructor(options = {}) {
|
|
32
|
+
this.handlers = /* @__PURE__ */ new Map();
|
|
33
|
+
this.deadLetters = [];
|
|
34
|
+
this.msgCounter = 0;
|
|
35
|
+
this.maxQueueSize = options.maxQueueSize ?? 0;
|
|
36
|
+
}
|
|
37
|
+
async publish(queue, data, options) {
|
|
38
|
+
const id = `msg-${++this.msgCounter}`;
|
|
39
|
+
const msg = {
|
|
40
|
+
id,
|
|
41
|
+
data,
|
|
42
|
+
attempts: 0,
|
|
43
|
+
timestamp: Date.now()
|
|
44
|
+
};
|
|
45
|
+
const fns = this.handlers.get(queue) ?? [];
|
|
46
|
+
if (fns.length === 0) {
|
|
47
|
+
if (this.maxQueueSize === 0 || this.deadLetters.length < this.maxQueueSize) {
|
|
48
|
+
this.deadLetters.push(msg);
|
|
49
|
+
}
|
|
50
|
+
return id;
|
|
51
|
+
}
|
|
52
|
+
const maxRetries = options?.retries ?? 0;
|
|
53
|
+
for (const handler of fns) {
|
|
54
|
+
let attempt = 0;
|
|
55
|
+
let success = false;
|
|
56
|
+
while (!success && attempt <= maxRetries) {
|
|
57
|
+
try {
|
|
58
|
+
msg.attempts = attempt + 1;
|
|
59
|
+
await handler(msg);
|
|
60
|
+
success = true;
|
|
61
|
+
} catch {
|
|
62
|
+
attempt++;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return id;
|
|
67
|
+
}
|
|
68
|
+
async subscribe(queue, handler) {
|
|
69
|
+
const existing = this.handlers.get(queue) ?? [];
|
|
70
|
+
this.handlers.set(queue, [...existing, handler]);
|
|
71
|
+
}
|
|
72
|
+
async unsubscribe(queue) {
|
|
73
|
+
this.handlers.delete(queue);
|
|
74
|
+
}
|
|
75
|
+
async getQueueSize(_queue) {
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
async purge(queue) {
|
|
79
|
+
this.handlers.delete(queue);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// src/queue-service-plugin.ts
|
|
84
|
+
var QueueServicePlugin = class {
|
|
85
|
+
constructor(options = {}) {
|
|
86
|
+
this.name = "com.objectstack.service.queue";
|
|
87
|
+
this.version = "1.0.0";
|
|
88
|
+
this.type = "standard";
|
|
89
|
+
this.options = { adapter: "memory", ...options };
|
|
90
|
+
}
|
|
91
|
+
async init(ctx) {
|
|
92
|
+
const adapter = this.options.adapter;
|
|
93
|
+
if (adapter === "bullmq") {
|
|
94
|
+
throw new Error(
|
|
95
|
+
'BullMQ queue adapter is not yet implemented. Use adapter: "memory" or provide a custom IQueueService via ctx.registerService("queue", impl).'
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
const queue = new MemoryQueueAdapter(this.options.memory);
|
|
99
|
+
ctx.registerService("queue", queue);
|
|
100
|
+
ctx.logger.info("QueueServicePlugin: registered memory queue adapter");
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// src/bullmq-queue-adapter.ts
|
|
105
|
+
var BullMQQueueAdapter = class {
|
|
106
|
+
constructor(options) {
|
|
107
|
+
this.redisUrl = options.redisUrl;
|
|
108
|
+
}
|
|
109
|
+
async publish(_queue, _data, _options) {
|
|
110
|
+
throw new Error(`BullMQQueueAdapter not yet implemented (url: ${this.redisUrl})`);
|
|
111
|
+
}
|
|
112
|
+
async subscribe(_queue, _handler) {
|
|
113
|
+
throw new Error("BullMQQueueAdapter not yet implemented");
|
|
114
|
+
}
|
|
115
|
+
async unsubscribe(_queue) {
|
|
116
|
+
throw new Error("BullMQQueueAdapter not yet implemented");
|
|
117
|
+
}
|
|
118
|
+
async getQueueSize(_queue) {
|
|
119
|
+
throw new Error("BullMQQueueAdapter not yet implemented");
|
|
120
|
+
}
|
|
121
|
+
async purge(_queue) {
|
|
122
|
+
throw new Error("BullMQQueueAdapter not yet implemented");
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
126
|
+
0 && (module.exports = {
|
|
127
|
+
BullMQQueueAdapter,
|
|
128
|
+
MemoryQueueAdapter,
|
|
129
|
+
QueueServicePlugin
|
|
130
|
+
});
|
|
131
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/memory-queue-adapter.ts","../src/queue-service-plugin.ts","../src/bullmq-queue-adapter.ts"],"sourcesContent":["// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nexport { QueueServicePlugin } from './queue-service-plugin.js';\nexport type { QueueServicePluginOptions } from './queue-service-plugin.js';\nexport { MemoryQueueAdapter } from './memory-queue-adapter.js';\nexport type { MemoryQueueAdapterOptions } from './memory-queue-adapter.js';\nexport { BullMQQueueAdapter } from './bullmq-queue-adapter.js';\nexport type { BullMQQueueAdapterOptions } from './bullmq-queue-adapter.js';\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { IQueueService, QueuePublishOptions, QueueMessage, QueueHandler } from '@objectstack/spec/contracts';\n\n/**\n * Configuration options for MemoryQueueAdapter.\n */\nexport interface MemoryQueueAdapterOptions {\n /** Maximum number of messages retained per queue (0 = unlimited) */\n maxQueueSize?: number;\n}\n\n/**\n * In-memory queue adapter implementing IQueueService.\n *\n * Provides synchronous in-process pub/sub delivery.\n * Suitable for single-process environments, development, and testing.\n */\nexport class MemoryQueueAdapter implements IQueueService {\n private readonly handlers = new Map<string, QueueHandler[]>();\n private readonly deadLetters: QueueMessage[] = [];\n private msgCounter = 0;\n private readonly maxQueueSize: number;\n\n constructor(options: MemoryQueueAdapterOptions = {}) {\n this.maxQueueSize = options.maxQueueSize ?? 0;\n }\n\n async publish<T = unknown>(queue: string, data: T, options?: QueuePublishOptions): Promise<string> {\n const id = `msg-${++this.msgCounter}`;\n const msg: QueueMessage<T> = {\n id,\n data,\n attempts: 0,\n timestamp: Date.now(),\n };\n\n const fns = this.handlers.get(queue) ?? [];\n if (fns.length === 0) {\n // No subscribers — retain as dead letter if within limits\n if (this.maxQueueSize === 0 || this.deadLetters.length < this.maxQueueSize) {\n this.deadLetters.push(msg);\n }\n return id;\n }\n\n const maxRetries = options?.retries ?? 0;\n for (const handler of fns) {\n let attempt = 0;\n let success = false;\n while (!success && attempt <= maxRetries) {\n try {\n msg.attempts = attempt + 1;\n await handler(msg as QueueMessage);\n success = true;\n } catch {\n attempt++;\n }\n }\n }\n\n return id;\n }\n\n async subscribe<T = unknown>(queue: string, handler: QueueHandler<T>): Promise<void> {\n const existing = this.handlers.get(queue) ?? [];\n this.handlers.set(queue, [...existing, handler as QueueHandler]);\n }\n\n async unsubscribe(queue: string): Promise<void> {\n this.handlers.delete(queue);\n }\n\n async getQueueSize(_queue: string): Promise<number> {\n // In-memory: no persistent queue depth tracking\n return 0;\n }\n\n async purge(queue: string): Promise<void> {\n this.handlers.delete(queue);\n }\n}\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { Plugin, PluginContext } from '@objectstack/core';\nimport { MemoryQueueAdapter } from './memory-queue-adapter.js';\nimport type { MemoryQueueAdapterOptions } from './memory-queue-adapter.js';\n\n/**\n * Configuration options for the QueueServicePlugin.\n */\nexport interface QueueServicePluginOptions {\n /** Queue adapter type (default: 'memory') */\n adapter?: 'memory' | 'bullmq';\n /** Options for the memory queue adapter */\n memory?: MemoryQueueAdapterOptions;\n /** Redis connection URL (used when adapter is 'bullmq') */\n redisUrl?: string;\n}\n\n/**\n * QueueServicePlugin — Production IQueueService implementation.\n *\n * Registers a queue service with the kernel during the init phase.\n * Supports in-memory and BullMQ adapters.\n *\n * @example\n * ```ts\n * import { ObjectKernel } from '@objectstack/core';\n * import { QueueServicePlugin } from '@objectstack/service-queue';\n *\n * const kernel = new ObjectKernel();\n * kernel.use(new QueueServicePlugin({ adapter: 'memory' }));\n * await kernel.bootstrap();\n *\n * const queue = kernel.getService('queue');\n * await queue.publish('orders', { orderId: 123 });\n * ```\n */\nexport class QueueServicePlugin implements Plugin {\n name = 'com.objectstack.service.queue';\n version = '1.0.0';\n type = 'standard';\n\n private readonly options: QueueServicePluginOptions;\n\n constructor(options: QueueServicePluginOptions = {}) {\n this.options = { adapter: 'memory', ...options };\n }\n\n async init(ctx: PluginContext): Promise<void> {\n const adapter = this.options.adapter;\n if (adapter === 'bullmq') {\n throw new Error(\n 'BullMQ queue adapter is not yet implemented. ' +\n 'Use adapter: \"memory\" or provide a custom IQueueService via ctx.registerService(\"queue\", impl).'\n );\n }\n\n const queue = new MemoryQueueAdapter(this.options.memory);\n ctx.registerService('queue', queue);\n ctx.logger.info('QueueServicePlugin: registered memory queue adapter');\n }\n}\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { IQueueService, QueuePublishOptions, QueueHandler } from '@objectstack/spec/contracts';\n\n/**\n * Configuration for the BullMQ queue adapter.\n */\nexport interface BullMQQueueAdapterOptions {\n /** Redis connection URL (e.g. 'redis://localhost:6379') */\n redisUrl: string;\n /** Default job options */\n defaultJobOptions?: {\n /** Number of retry attempts */\n attempts?: number;\n /** Backoff strategy */\n backoff?: { type: 'fixed' | 'exponential'; delay: number };\n };\n}\n\n/**\n * BullMQ queue adapter skeleton implementing IQueueService.\n *\n * This is a placeholder for future BullMQ integration.\n * Concrete implementation will use the `bullmq` package.\n *\n * @example\n * ```ts\n * const queue = new BullMQQueueAdapter({ redisUrl: 'redis://localhost:6379' });\n * await queue.publish('orders', { orderId: 123 });\n * ```\n */\nexport class BullMQQueueAdapter implements IQueueService {\n private readonly redisUrl: string;\n\n constructor(options: BullMQQueueAdapterOptions) {\n this.redisUrl = options.redisUrl;\n }\n\n async publish<T = unknown>(_queue: string, _data: T, _options?: QueuePublishOptions): Promise<string> {\n throw new Error(`BullMQQueueAdapter not yet implemented (url: ${this.redisUrl})`);\n }\n\n async subscribe<T = unknown>(_queue: string, _handler: QueueHandler<T>): Promise<void> {\n throw new Error('BullMQQueueAdapter not yet implemented');\n }\n\n async unsubscribe(_queue: string): Promise<void> {\n throw new Error('BullMQQueueAdapter not yet implemented');\n }\n\n async getQueueSize(_queue: string): Promise<number> {\n throw new Error('BullMQQueueAdapter not yet implemented');\n }\n\n async purge(_queue: string): Promise<void> {\n throw new Error('BullMQQueueAdapter not yet implemented');\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACkBO,IAAM,qBAAN,MAAkD;AAAA,EAMvD,YAAY,UAAqC,CAAC,GAAG;AALrD,SAAiB,WAAW,oBAAI,IAA4B;AAC5D,SAAiB,cAA8B,CAAC;AAChD,SAAQ,aAAa;AAInB,SAAK,eAAe,QAAQ,gBAAgB;AAAA,EAC9C;AAAA,EAEA,MAAM,QAAqB,OAAe,MAAS,SAAgD;AACjG,UAAM,KAAK,OAAO,EAAE,KAAK,UAAU;AACnC,UAAM,MAAuB;AAAA,MAC3B;AAAA,MACA;AAAA,MACA,UAAU;AAAA,MACV,WAAW,KAAK,IAAI;AAAA,IACtB;AAEA,UAAM,MAAM,KAAK,SAAS,IAAI,KAAK,KAAK,CAAC;AACzC,QAAI,IAAI,WAAW,GAAG;AAEpB,UAAI,KAAK,iBAAiB,KAAK,KAAK,YAAY,SAAS,KAAK,cAAc;AAC1E,aAAK,YAAY,KAAK,GAAG;AAAA,MAC3B;AACA,aAAO;AAAA,IACT;AAEA,UAAM,aAAa,SAAS,WAAW;AACvC,eAAW,WAAW,KAAK;AACzB,UAAI,UAAU;AACd,UAAI,UAAU;AACd,aAAO,CAAC,WAAW,WAAW,YAAY;AACxC,YAAI;AACF,cAAI,WAAW,UAAU;AACzB,gBAAM,QAAQ,GAAmB;AACjC,oBAAU;AAAA,QACZ,QAAQ;AACN;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,UAAuB,OAAe,SAAyC;AACnF,UAAM,WAAW,KAAK,SAAS,IAAI,KAAK,KAAK,CAAC;AAC9C,SAAK,SAAS,IAAI,OAAO,CAAC,GAAG,UAAU,OAAuB,CAAC;AAAA,EACjE;AAAA,EAEA,MAAM,YAAY,OAA8B;AAC9C,SAAK,SAAS,OAAO,KAAK;AAAA,EAC5B;AAAA,EAEA,MAAM,aAAa,QAAiC;AAElD,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,MAAM,OAA8B;AACxC,SAAK,SAAS,OAAO,KAAK;AAAA,EAC5B;AACF;;;AC5CO,IAAM,qBAAN,MAA2C;AAAA,EAOhD,YAAY,UAAqC,CAAC,GAAG;AANrD,gBAAO;AACP,mBAAU;AACV,gBAAO;AAKL,SAAK,UAAU,EAAE,SAAS,UAAU,GAAG,QAAQ;AAAA,EACjD;AAAA,EAEA,MAAM,KAAK,KAAmC;AAC5C,UAAM,UAAU,KAAK,QAAQ;AAC7B,QAAI,YAAY,UAAU;AACxB,YAAM,IAAI;AAAA,QACR;AAAA,MAEF;AAAA,IACF;AAEA,UAAM,QAAQ,IAAI,mBAAmB,KAAK,QAAQ,MAAM;AACxD,QAAI,gBAAgB,SAAS,KAAK;AAClC,QAAI,OAAO,KAAK,qDAAqD;AAAA,EACvE;AACF;;;AC9BO,IAAM,qBAAN,MAAkD;AAAA,EAGvD,YAAY,SAAoC;AAC9C,SAAK,WAAW,QAAQ;AAAA,EAC1B;AAAA,EAEA,MAAM,QAAqB,QAAgB,OAAU,UAAiD;AACpG,UAAM,IAAI,MAAM,gDAAgD,KAAK,QAAQ,GAAG;AAAA,EAClF;AAAA,EAEA,MAAM,UAAuB,QAAgB,UAA0C;AACrF,UAAM,IAAI,MAAM,wCAAwC;AAAA,EAC1D;AAAA,EAEA,MAAM,YAAY,QAA+B;AAC/C,UAAM,IAAI,MAAM,wCAAwC;AAAA,EAC1D;AAAA,EAEA,MAAM,aAAa,QAAiC;AAClD,UAAM,IAAI,MAAM,wCAAwC;AAAA,EAC1D;AAAA,EAEA,MAAM,MAAM,QAA+B;AACzC,UAAM,IAAI,MAAM,wCAAwC;AAAA,EAC1D;AACF;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Plugin, PluginContext } from '@objectstack/core';
|
|
2
|
+
import { IQueueService, QueuePublishOptions, QueueHandler } from '@objectstack/spec/contracts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Configuration options for MemoryQueueAdapter.
|
|
6
|
+
*/
|
|
7
|
+
interface MemoryQueueAdapterOptions {
|
|
8
|
+
/** Maximum number of messages retained per queue (0 = unlimited) */
|
|
9
|
+
maxQueueSize?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* In-memory queue adapter implementing IQueueService.
|
|
13
|
+
*
|
|
14
|
+
* Provides synchronous in-process pub/sub delivery.
|
|
15
|
+
* Suitable for single-process environments, development, and testing.
|
|
16
|
+
*/
|
|
17
|
+
declare class MemoryQueueAdapter implements IQueueService {
|
|
18
|
+
private readonly handlers;
|
|
19
|
+
private readonly deadLetters;
|
|
20
|
+
private msgCounter;
|
|
21
|
+
private readonly maxQueueSize;
|
|
22
|
+
constructor(options?: MemoryQueueAdapterOptions);
|
|
23
|
+
publish<T = unknown>(queue: string, data: T, options?: QueuePublishOptions): Promise<string>;
|
|
24
|
+
subscribe<T = unknown>(queue: string, handler: QueueHandler<T>): Promise<void>;
|
|
25
|
+
unsubscribe(queue: string): Promise<void>;
|
|
26
|
+
getQueueSize(_queue: string): Promise<number>;
|
|
27
|
+
purge(queue: string): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Configuration options for the QueueServicePlugin.
|
|
32
|
+
*/
|
|
33
|
+
interface QueueServicePluginOptions {
|
|
34
|
+
/** Queue adapter type (default: 'memory') */
|
|
35
|
+
adapter?: 'memory' | 'bullmq';
|
|
36
|
+
/** Options for the memory queue adapter */
|
|
37
|
+
memory?: MemoryQueueAdapterOptions;
|
|
38
|
+
/** Redis connection URL (used when adapter is 'bullmq') */
|
|
39
|
+
redisUrl?: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* QueueServicePlugin — Production IQueueService implementation.
|
|
43
|
+
*
|
|
44
|
+
* Registers a queue service with the kernel during the init phase.
|
|
45
|
+
* Supports in-memory and BullMQ adapters.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { ObjectKernel } from '@objectstack/core';
|
|
50
|
+
* import { QueueServicePlugin } from '@objectstack/service-queue';
|
|
51
|
+
*
|
|
52
|
+
* const kernel = new ObjectKernel();
|
|
53
|
+
* kernel.use(new QueueServicePlugin({ adapter: 'memory' }));
|
|
54
|
+
* await kernel.bootstrap();
|
|
55
|
+
*
|
|
56
|
+
* const queue = kernel.getService('queue');
|
|
57
|
+
* await queue.publish('orders', { orderId: 123 });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
declare class QueueServicePlugin implements Plugin {
|
|
61
|
+
name: string;
|
|
62
|
+
version: string;
|
|
63
|
+
type: string;
|
|
64
|
+
private readonly options;
|
|
65
|
+
constructor(options?: QueueServicePluginOptions);
|
|
66
|
+
init(ctx: PluginContext): Promise<void>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Configuration for the BullMQ queue adapter.
|
|
71
|
+
*/
|
|
72
|
+
interface BullMQQueueAdapterOptions {
|
|
73
|
+
/** Redis connection URL (e.g. 'redis://localhost:6379') */
|
|
74
|
+
redisUrl: string;
|
|
75
|
+
/** Default job options */
|
|
76
|
+
defaultJobOptions?: {
|
|
77
|
+
/** Number of retry attempts */
|
|
78
|
+
attempts?: number;
|
|
79
|
+
/** Backoff strategy */
|
|
80
|
+
backoff?: {
|
|
81
|
+
type: 'fixed' | 'exponential';
|
|
82
|
+
delay: number;
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* BullMQ queue adapter skeleton implementing IQueueService.
|
|
88
|
+
*
|
|
89
|
+
* This is a placeholder for future BullMQ integration.
|
|
90
|
+
* Concrete implementation will use the `bullmq` package.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* const queue = new BullMQQueueAdapter({ redisUrl: 'redis://localhost:6379' });
|
|
95
|
+
* await queue.publish('orders', { orderId: 123 });
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
declare class BullMQQueueAdapter implements IQueueService {
|
|
99
|
+
private readonly redisUrl;
|
|
100
|
+
constructor(options: BullMQQueueAdapterOptions);
|
|
101
|
+
publish<T = unknown>(_queue: string, _data: T, _options?: QueuePublishOptions): Promise<string>;
|
|
102
|
+
subscribe<T = unknown>(_queue: string, _handler: QueueHandler<T>): Promise<void>;
|
|
103
|
+
unsubscribe(_queue: string): Promise<void>;
|
|
104
|
+
getQueueSize(_queue: string): Promise<number>;
|
|
105
|
+
purge(_queue: string): Promise<void>;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export { BullMQQueueAdapter, type BullMQQueueAdapterOptions, MemoryQueueAdapter, type MemoryQueueAdapterOptions, QueueServicePlugin, type QueueServicePluginOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Plugin, PluginContext } from '@objectstack/core';
|
|
2
|
+
import { IQueueService, QueuePublishOptions, QueueHandler } from '@objectstack/spec/contracts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Configuration options for MemoryQueueAdapter.
|
|
6
|
+
*/
|
|
7
|
+
interface MemoryQueueAdapterOptions {
|
|
8
|
+
/** Maximum number of messages retained per queue (0 = unlimited) */
|
|
9
|
+
maxQueueSize?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* In-memory queue adapter implementing IQueueService.
|
|
13
|
+
*
|
|
14
|
+
* Provides synchronous in-process pub/sub delivery.
|
|
15
|
+
* Suitable for single-process environments, development, and testing.
|
|
16
|
+
*/
|
|
17
|
+
declare class MemoryQueueAdapter implements IQueueService {
|
|
18
|
+
private readonly handlers;
|
|
19
|
+
private readonly deadLetters;
|
|
20
|
+
private msgCounter;
|
|
21
|
+
private readonly maxQueueSize;
|
|
22
|
+
constructor(options?: MemoryQueueAdapterOptions);
|
|
23
|
+
publish<T = unknown>(queue: string, data: T, options?: QueuePublishOptions): Promise<string>;
|
|
24
|
+
subscribe<T = unknown>(queue: string, handler: QueueHandler<T>): Promise<void>;
|
|
25
|
+
unsubscribe(queue: string): Promise<void>;
|
|
26
|
+
getQueueSize(_queue: string): Promise<number>;
|
|
27
|
+
purge(queue: string): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Configuration options for the QueueServicePlugin.
|
|
32
|
+
*/
|
|
33
|
+
interface QueueServicePluginOptions {
|
|
34
|
+
/** Queue adapter type (default: 'memory') */
|
|
35
|
+
adapter?: 'memory' | 'bullmq';
|
|
36
|
+
/** Options for the memory queue adapter */
|
|
37
|
+
memory?: MemoryQueueAdapterOptions;
|
|
38
|
+
/** Redis connection URL (used when adapter is 'bullmq') */
|
|
39
|
+
redisUrl?: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* QueueServicePlugin — Production IQueueService implementation.
|
|
43
|
+
*
|
|
44
|
+
* Registers a queue service with the kernel during the init phase.
|
|
45
|
+
* Supports in-memory and BullMQ adapters.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { ObjectKernel } from '@objectstack/core';
|
|
50
|
+
* import { QueueServicePlugin } from '@objectstack/service-queue';
|
|
51
|
+
*
|
|
52
|
+
* const kernel = new ObjectKernel();
|
|
53
|
+
* kernel.use(new QueueServicePlugin({ adapter: 'memory' }));
|
|
54
|
+
* await kernel.bootstrap();
|
|
55
|
+
*
|
|
56
|
+
* const queue = kernel.getService('queue');
|
|
57
|
+
* await queue.publish('orders', { orderId: 123 });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
declare class QueueServicePlugin implements Plugin {
|
|
61
|
+
name: string;
|
|
62
|
+
version: string;
|
|
63
|
+
type: string;
|
|
64
|
+
private readonly options;
|
|
65
|
+
constructor(options?: QueueServicePluginOptions);
|
|
66
|
+
init(ctx: PluginContext): Promise<void>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Configuration for the BullMQ queue adapter.
|
|
71
|
+
*/
|
|
72
|
+
interface BullMQQueueAdapterOptions {
|
|
73
|
+
/** Redis connection URL (e.g. 'redis://localhost:6379') */
|
|
74
|
+
redisUrl: string;
|
|
75
|
+
/** Default job options */
|
|
76
|
+
defaultJobOptions?: {
|
|
77
|
+
/** Number of retry attempts */
|
|
78
|
+
attempts?: number;
|
|
79
|
+
/** Backoff strategy */
|
|
80
|
+
backoff?: {
|
|
81
|
+
type: 'fixed' | 'exponential';
|
|
82
|
+
delay: number;
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* BullMQ queue adapter skeleton implementing IQueueService.
|
|
88
|
+
*
|
|
89
|
+
* This is a placeholder for future BullMQ integration.
|
|
90
|
+
* Concrete implementation will use the `bullmq` package.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* const queue = new BullMQQueueAdapter({ redisUrl: 'redis://localhost:6379' });
|
|
95
|
+
* await queue.publish('orders', { orderId: 123 });
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
declare class BullMQQueueAdapter implements IQueueService {
|
|
99
|
+
private readonly redisUrl;
|
|
100
|
+
constructor(options: BullMQQueueAdapterOptions);
|
|
101
|
+
publish<T = unknown>(_queue: string, _data: T, _options?: QueuePublishOptions): Promise<string>;
|
|
102
|
+
subscribe<T = unknown>(_queue: string, _handler: QueueHandler<T>): Promise<void>;
|
|
103
|
+
unsubscribe(_queue: string): Promise<void>;
|
|
104
|
+
getQueueSize(_queue: string): Promise<number>;
|
|
105
|
+
purge(_queue: string): Promise<void>;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export { BullMQQueueAdapter, type BullMQQueueAdapterOptions, MemoryQueueAdapter, type MemoryQueueAdapterOptions, QueueServicePlugin, type QueueServicePluginOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// src/memory-queue-adapter.ts
|
|
2
|
+
var MemoryQueueAdapter = class {
|
|
3
|
+
constructor(options = {}) {
|
|
4
|
+
this.handlers = /* @__PURE__ */ new Map();
|
|
5
|
+
this.deadLetters = [];
|
|
6
|
+
this.msgCounter = 0;
|
|
7
|
+
this.maxQueueSize = options.maxQueueSize ?? 0;
|
|
8
|
+
}
|
|
9
|
+
async publish(queue, data, options) {
|
|
10
|
+
const id = `msg-${++this.msgCounter}`;
|
|
11
|
+
const msg = {
|
|
12
|
+
id,
|
|
13
|
+
data,
|
|
14
|
+
attempts: 0,
|
|
15
|
+
timestamp: Date.now()
|
|
16
|
+
};
|
|
17
|
+
const fns = this.handlers.get(queue) ?? [];
|
|
18
|
+
if (fns.length === 0) {
|
|
19
|
+
if (this.maxQueueSize === 0 || this.deadLetters.length < this.maxQueueSize) {
|
|
20
|
+
this.deadLetters.push(msg);
|
|
21
|
+
}
|
|
22
|
+
return id;
|
|
23
|
+
}
|
|
24
|
+
const maxRetries = options?.retries ?? 0;
|
|
25
|
+
for (const handler of fns) {
|
|
26
|
+
let attempt = 0;
|
|
27
|
+
let success = false;
|
|
28
|
+
while (!success && attempt <= maxRetries) {
|
|
29
|
+
try {
|
|
30
|
+
msg.attempts = attempt + 1;
|
|
31
|
+
await handler(msg);
|
|
32
|
+
success = true;
|
|
33
|
+
} catch {
|
|
34
|
+
attempt++;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return id;
|
|
39
|
+
}
|
|
40
|
+
async subscribe(queue, handler) {
|
|
41
|
+
const existing = this.handlers.get(queue) ?? [];
|
|
42
|
+
this.handlers.set(queue, [...existing, handler]);
|
|
43
|
+
}
|
|
44
|
+
async unsubscribe(queue) {
|
|
45
|
+
this.handlers.delete(queue);
|
|
46
|
+
}
|
|
47
|
+
async getQueueSize(_queue) {
|
|
48
|
+
return 0;
|
|
49
|
+
}
|
|
50
|
+
async purge(queue) {
|
|
51
|
+
this.handlers.delete(queue);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// src/queue-service-plugin.ts
|
|
56
|
+
var QueueServicePlugin = class {
|
|
57
|
+
constructor(options = {}) {
|
|
58
|
+
this.name = "com.objectstack.service.queue";
|
|
59
|
+
this.version = "1.0.0";
|
|
60
|
+
this.type = "standard";
|
|
61
|
+
this.options = { adapter: "memory", ...options };
|
|
62
|
+
}
|
|
63
|
+
async init(ctx) {
|
|
64
|
+
const adapter = this.options.adapter;
|
|
65
|
+
if (adapter === "bullmq") {
|
|
66
|
+
throw new Error(
|
|
67
|
+
'BullMQ queue adapter is not yet implemented. Use adapter: "memory" or provide a custom IQueueService via ctx.registerService("queue", impl).'
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
const queue = new MemoryQueueAdapter(this.options.memory);
|
|
71
|
+
ctx.registerService("queue", queue);
|
|
72
|
+
ctx.logger.info("QueueServicePlugin: registered memory queue adapter");
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// src/bullmq-queue-adapter.ts
|
|
77
|
+
var BullMQQueueAdapter = class {
|
|
78
|
+
constructor(options) {
|
|
79
|
+
this.redisUrl = options.redisUrl;
|
|
80
|
+
}
|
|
81
|
+
async publish(_queue, _data, _options) {
|
|
82
|
+
throw new Error(`BullMQQueueAdapter not yet implemented (url: ${this.redisUrl})`);
|
|
83
|
+
}
|
|
84
|
+
async subscribe(_queue, _handler) {
|
|
85
|
+
throw new Error("BullMQQueueAdapter not yet implemented");
|
|
86
|
+
}
|
|
87
|
+
async unsubscribe(_queue) {
|
|
88
|
+
throw new Error("BullMQQueueAdapter not yet implemented");
|
|
89
|
+
}
|
|
90
|
+
async getQueueSize(_queue) {
|
|
91
|
+
throw new Error("BullMQQueueAdapter not yet implemented");
|
|
92
|
+
}
|
|
93
|
+
async purge(_queue) {
|
|
94
|
+
throw new Error("BullMQQueueAdapter not yet implemented");
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
export {
|
|
98
|
+
BullMQQueueAdapter,
|
|
99
|
+
MemoryQueueAdapter,
|
|
100
|
+
QueueServicePlugin
|
|
101
|
+
};
|
|
102
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/memory-queue-adapter.ts","../src/queue-service-plugin.ts","../src/bullmq-queue-adapter.ts"],"sourcesContent":["// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { IQueueService, QueuePublishOptions, QueueMessage, QueueHandler } from '@objectstack/spec/contracts';\n\n/**\n * Configuration options for MemoryQueueAdapter.\n */\nexport interface MemoryQueueAdapterOptions {\n /** Maximum number of messages retained per queue (0 = unlimited) */\n maxQueueSize?: number;\n}\n\n/**\n * In-memory queue adapter implementing IQueueService.\n *\n * Provides synchronous in-process pub/sub delivery.\n * Suitable for single-process environments, development, and testing.\n */\nexport class MemoryQueueAdapter implements IQueueService {\n private readonly handlers = new Map<string, QueueHandler[]>();\n private readonly deadLetters: QueueMessage[] = [];\n private msgCounter = 0;\n private readonly maxQueueSize: number;\n\n constructor(options: MemoryQueueAdapterOptions = {}) {\n this.maxQueueSize = options.maxQueueSize ?? 0;\n }\n\n async publish<T = unknown>(queue: string, data: T, options?: QueuePublishOptions): Promise<string> {\n const id = `msg-${++this.msgCounter}`;\n const msg: QueueMessage<T> = {\n id,\n data,\n attempts: 0,\n timestamp: Date.now(),\n };\n\n const fns = this.handlers.get(queue) ?? [];\n if (fns.length === 0) {\n // No subscribers — retain as dead letter if within limits\n if (this.maxQueueSize === 0 || this.deadLetters.length < this.maxQueueSize) {\n this.deadLetters.push(msg);\n }\n return id;\n }\n\n const maxRetries = options?.retries ?? 0;\n for (const handler of fns) {\n let attempt = 0;\n let success = false;\n while (!success && attempt <= maxRetries) {\n try {\n msg.attempts = attempt + 1;\n await handler(msg as QueueMessage);\n success = true;\n } catch {\n attempt++;\n }\n }\n }\n\n return id;\n }\n\n async subscribe<T = unknown>(queue: string, handler: QueueHandler<T>): Promise<void> {\n const existing = this.handlers.get(queue) ?? [];\n this.handlers.set(queue, [...existing, handler as QueueHandler]);\n }\n\n async unsubscribe(queue: string): Promise<void> {\n this.handlers.delete(queue);\n }\n\n async getQueueSize(_queue: string): Promise<number> {\n // In-memory: no persistent queue depth tracking\n return 0;\n }\n\n async purge(queue: string): Promise<void> {\n this.handlers.delete(queue);\n }\n}\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { Plugin, PluginContext } from '@objectstack/core';\nimport { MemoryQueueAdapter } from './memory-queue-adapter.js';\nimport type { MemoryQueueAdapterOptions } from './memory-queue-adapter.js';\n\n/**\n * Configuration options for the QueueServicePlugin.\n */\nexport interface QueueServicePluginOptions {\n /** Queue adapter type (default: 'memory') */\n adapter?: 'memory' | 'bullmq';\n /** Options for the memory queue adapter */\n memory?: MemoryQueueAdapterOptions;\n /** Redis connection URL (used when adapter is 'bullmq') */\n redisUrl?: string;\n}\n\n/**\n * QueueServicePlugin — Production IQueueService implementation.\n *\n * Registers a queue service with the kernel during the init phase.\n * Supports in-memory and BullMQ adapters.\n *\n * @example\n * ```ts\n * import { ObjectKernel } from '@objectstack/core';\n * import { QueueServicePlugin } from '@objectstack/service-queue';\n *\n * const kernel = new ObjectKernel();\n * kernel.use(new QueueServicePlugin({ adapter: 'memory' }));\n * await kernel.bootstrap();\n *\n * const queue = kernel.getService('queue');\n * await queue.publish('orders', { orderId: 123 });\n * ```\n */\nexport class QueueServicePlugin implements Plugin {\n name = 'com.objectstack.service.queue';\n version = '1.0.0';\n type = 'standard';\n\n private readonly options: QueueServicePluginOptions;\n\n constructor(options: QueueServicePluginOptions = {}) {\n this.options = { adapter: 'memory', ...options };\n }\n\n async init(ctx: PluginContext): Promise<void> {\n const adapter = this.options.adapter;\n if (adapter === 'bullmq') {\n throw new Error(\n 'BullMQ queue adapter is not yet implemented. ' +\n 'Use adapter: \"memory\" or provide a custom IQueueService via ctx.registerService(\"queue\", impl).'\n );\n }\n\n const queue = new MemoryQueueAdapter(this.options.memory);\n ctx.registerService('queue', queue);\n ctx.logger.info('QueueServicePlugin: registered memory queue adapter');\n }\n}\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport type { IQueueService, QueuePublishOptions, QueueHandler } from '@objectstack/spec/contracts';\n\n/**\n * Configuration for the BullMQ queue adapter.\n */\nexport interface BullMQQueueAdapterOptions {\n /** Redis connection URL (e.g. 'redis://localhost:6379') */\n redisUrl: string;\n /** Default job options */\n defaultJobOptions?: {\n /** Number of retry attempts */\n attempts?: number;\n /** Backoff strategy */\n backoff?: { type: 'fixed' | 'exponential'; delay: number };\n };\n}\n\n/**\n * BullMQ queue adapter skeleton implementing IQueueService.\n *\n * This is a placeholder for future BullMQ integration.\n * Concrete implementation will use the `bullmq` package.\n *\n * @example\n * ```ts\n * const queue = new BullMQQueueAdapter({ redisUrl: 'redis://localhost:6379' });\n * await queue.publish('orders', { orderId: 123 });\n * ```\n */\nexport class BullMQQueueAdapter implements IQueueService {\n private readonly redisUrl: string;\n\n constructor(options: BullMQQueueAdapterOptions) {\n this.redisUrl = options.redisUrl;\n }\n\n async publish<T = unknown>(_queue: string, _data: T, _options?: QueuePublishOptions): Promise<string> {\n throw new Error(`BullMQQueueAdapter not yet implemented (url: ${this.redisUrl})`);\n }\n\n async subscribe<T = unknown>(_queue: string, _handler: QueueHandler<T>): Promise<void> {\n throw new Error('BullMQQueueAdapter not yet implemented');\n }\n\n async unsubscribe(_queue: string): Promise<void> {\n throw new Error('BullMQQueueAdapter not yet implemented');\n }\n\n async getQueueSize(_queue: string): Promise<number> {\n throw new Error('BullMQQueueAdapter not yet implemented');\n }\n\n async purge(_queue: string): Promise<void> {\n throw new Error('BullMQQueueAdapter not yet implemented');\n }\n}\n"],"mappings":";AAkBO,IAAM,qBAAN,MAAkD;AAAA,EAMvD,YAAY,UAAqC,CAAC,GAAG;AALrD,SAAiB,WAAW,oBAAI,IAA4B;AAC5D,SAAiB,cAA8B,CAAC;AAChD,SAAQ,aAAa;AAInB,SAAK,eAAe,QAAQ,gBAAgB;AAAA,EAC9C;AAAA,EAEA,MAAM,QAAqB,OAAe,MAAS,SAAgD;AACjG,UAAM,KAAK,OAAO,EAAE,KAAK,UAAU;AACnC,UAAM,MAAuB;AAAA,MAC3B;AAAA,MACA;AAAA,MACA,UAAU;AAAA,MACV,WAAW,KAAK,IAAI;AAAA,IACtB;AAEA,UAAM,MAAM,KAAK,SAAS,IAAI,KAAK,KAAK,CAAC;AACzC,QAAI,IAAI,WAAW,GAAG;AAEpB,UAAI,KAAK,iBAAiB,KAAK,KAAK,YAAY,SAAS,KAAK,cAAc;AAC1E,aAAK,YAAY,KAAK,GAAG;AAAA,MAC3B;AACA,aAAO;AAAA,IACT;AAEA,UAAM,aAAa,SAAS,WAAW;AACvC,eAAW,WAAW,KAAK;AACzB,UAAI,UAAU;AACd,UAAI,UAAU;AACd,aAAO,CAAC,WAAW,WAAW,YAAY;AACxC,YAAI;AACF,cAAI,WAAW,UAAU;AACzB,gBAAM,QAAQ,GAAmB;AACjC,oBAAU;AAAA,QACZ,QAAQ;AACN;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,UAAuB,OAAe,SAAyC;AACnF,UAAM,WAAW,KAAK,SAAS,IAAI,KAAK,KAAK,CAAC;AAC9C,SAAK,SAAS,IAAI,OAAO,CAAC,GAAG,UAAU,OAAuB,CAAC;AAAA,EACjE;AAAA,EAEA,MAAM,YAAY,OAA8B;AAC9C,SAAK,SAAS,OAAO,KAAK;AAAA,EAC5B;AAAA,EAEA,MAAM,aAAa,QAAiC;AAElD,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,MAAM,OAA8B;AACxC,SAAK,SAAS,OAAO,KAAK;AAAA,EAC5B;AACF;;;AC5CO,IAAM,qBAAN,MAA2C;AAAA,EAOhD,YAAY,UAAqC,CAAC,GAAG;AANrD,gBAAO;AACP,mBAAU;AACV,gBAAO;AAKL,SAAK,UAAU,EAAE,SAAS,UAAU,GAAG,QAAQ;AAAA,EACjD;AAAA,EAEA,MAAM,KAAK,KAAmC;AAC5C,UAAM,UAAU,KAAK,QAAQ;AAC7B,QAAI,YAAY,UAAU;AACxB,YAAM,IAAI;AAAA,QACR;AAAA,MAEF;AAAA,IACF;AAEA,UAAM,QAAQ,IAAI,mBAAmB,KAAK,QAAQ,MAAM;AACxD,QAAI,gBAAgB,SAAS,KAAK;AAClC,QAAI,OAAO,KAAK,qDAAqD;AAAA,EACvE;AACF;;;AC9BO,IAAM,qBAAN,MAAkD;AAAA,EAGvD,YAAY,SAAoC;AAC9C,SAAK,WAAW,QAAQ;AAAA,EAC1B;AAAA,EAEA,MAAM,QAAqB,QAAgB,OAAU,UAAiD;AACpG,UAAM,IAAI,MAAM,gDAAgD,KAAK,QAAQ,GAAG;AAAA,EAClF;AAAA,EAEA,MAAM,UAAuB,QAAgB,UAA0C;AACrF,UAAM,IAAI,MAAM,wCAAwC;AAAA,EAC1D;AAAA,EAEA,MAAM,YAAY,QAA+B;AAC/C,UAAM,IAAI,MAAM,wCAAwC;AAAA,EAC1D;AAAA,EAEA,MAAM,aAAa,QAAiC;AAClD,UAAM,IAAI,MAAM,wCAAwC;AAAA,EAC1D;AAAA,EAEA,MAAM,MAAM,QAA+B;AACzC,UAAM,IAAI,MAAM,wCAAwC;AAAA,EAC1D;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@objectstack/service-queue",
|
|
3
|
+
"version": "3.0.7",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"description": "Queue Service for ObjectStack — implements IQueueService with in-memory and BullMQ adapters",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@objectstack/core": "3.0.7",
|
|
18
|
+
"@objectstack/spec": "3.0.7"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"typescript": "^5.0.0",
|
|
22
|
+
"vitest": "^4.0.18",
|
|
23
|
+
"@types/node": "^25.2.3"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsup --config ../../../tsup.config.ts",
|
|
27
|
+
"test": "vitest run"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
+
|
|
3
|
+
import type { IQueueService, QueuePublishOptions, QueueHandler } from '@objectstack/spec/contracts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for the BullMQ queue adapter.
|
|
7
|
+
*/
|
|
8
|
+
export interface BullMQQueueAdapterOptions {
|
|
9
|
+
/** Redis connection URL (e.g. 'redis://localhost:6379') */
|
|
10
|
+
redisUrl: string;
|
|
11
|
+
/** Default job options */
|
|
12
|
+
defaultJobOptions?: {
|
|
13
|
+
/** Number of retry attempts */
|
|
14
|
+
attempts?: number;
|
|
15
|
+
/** Backoff strategy */
|
|
16
|
+
backoff?: { type: 'fixed' | 'exponential'; delay: number };
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* BullMQ queue adapter skeleton implementing IQueueService.
|
|
22
|
+
*
|
|
23
|
+
* This is a placeholder for future BullMQ integration.
|
|
24
|
+
* Concrete implementation will use the `bullmq` package.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const queue = new BullMQQueueAdapter({ redisUrl: 'redis://localhost:6379' });
|
|
29
|
+
* await queue.publish('orders', { orderId: 123 });
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export class BullMQQueueAdapter implements IQueueService {
|
|
33
|
+
private readonly redisUrl: string;
|
|
34
|
+
|
|
35
|
+
constructor(options: BullMQQueueAdapterOptions) {
|
|
36
|
+
this.redisUrl = options.redisUrl;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async publish<T = unknown>(_queue: string, _data: T, _options?: QueuePublishOptions): Promise<string> {
|
|
40
|
+
throw new Error(`BullMQQueueAdapter not yet implemented (url: ${this.redisUrl})`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async subscribe<T = unknown>(_queue: string, _handler: QueueHandler<T>): Promise<void> {
|
|
44
|
+
throw new Error('BullMQQueueAdapter not yet implemented');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async unsubscribe(_queue: string): Promise<void> {
|
|
48
|
+
throw new Error('BullMQQueueAdapter not yet implemented');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async getQueueSize(_queue: string): Promise<number> {
|
|
52
|
+
throw new Error('BullMQQueueAdapter not yet implemented');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async purge(_queue: string): Promise<void> {
|
|
56
|
+
throw new Error('BullMQQueueAdapter not yet implemented');
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
+
|
|
3
|
+
export { QueueServicePlugin } from './queue-service-plugin.js';
|
|
4
|
+
export type { QueueServicePluginOptions } from './queue-service-plugin.js';
|
|
5
|
+
export { MemoryQueueAdapter } from './memory-queue-adapter.js';
|
|
6
|
+
export type { MemoryQueueAdapterOptions } from './memory-queue-adapter.js';
|
|
7
|
+
export { BullMQQueueAdapter } from './bullmq-queue-adapter.js';
|
|
8
|
+
export type { BullMQQueueAdapterOptions } from './bullmq-queue-adapter.js';
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
+
|
|
3
|
+
import { describe, it, expect } from 'vitest';
|
|
4
|
+
import { MemoryQueueAdapter } from './memory-queue-adapter';
|
|
5
|
+
import type { IQueueService, QueueMessage } from '@objectstack/spec/contracts';
|
|
6
|
+
|
|
7
|
+
describe('MemoryQueueAdapter', () => {
|
|
8
|
+
it('should implement IQueueService contract', () => {
|
|
9
|
+
const queue: IQueueService = new MemoryQueueAdapter();
|
|
10
|
+
expect(typeof queue.publish).toBe('function');
|
|
11
|
+
expect(typeof queue.subscribe).toBe('function');
|
|
12
|
+
expect(typeof queue.unsubscribe).toBe('function');
|
|
13
|
+
expect(typeof queue.getQueueSize).toBe('function');
|
|
14
|
+
expect(typeof queue.purge).toBe('function');
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should publish and deliver to subscriber', async () => {
|
|
18
|
+
const queue = new MemoryQueueAdapter();
|
|
19
|
+
const received: QueueMessage[] = [];
|
|
20
|
+
|
|
21
|
+
await queue.subscribe('orders', async (msg) => {
|
|
22
|
+
received.push(msg);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const id = await queue.publish('orders', { orderId: 123 });
|
|
26
|
+
expect(id).toBe('msg-1');
|
|
27
|
+
expect(received).toHaveLength(1);
|
|
28
|
+
expect(received[0].data).toEqual({ orderId: 123 });
|
|
29
|
+
expect(received[0].attempts).toBe(1);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should support multiple subscribers', async () => {
|
|
33
|
+
const queue = new MemoryQueueAdapter();
|
|
34
|
+
const log1: unknown[] = [];
|
|
35
|
+
const log2: unknown[] = [];
|
|
36
|
+
|
|
37
|
+
await queue.subscribe('events', async (msg) => { log1.push(msg.data); });
|
|
38
|
+
await queue.subscribe('events', async (msg) => { log2.push(msg.data); });
|
|
39
|
+
|
|
40
|
+
await queue.publish('events', 'hello');
|
|
41
|
+
expect(log1).toEqual(['hello']);
|
|
42
|
+
expect(log2).toEqual(['hello']);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should unsubscribe from a queue', async () => {
|
|
46
|
+
const queue = new MemoryQueueAdapter();
|
|
47
|
+
const received: unknown[] = [];
|
|
48
|
+
|
|
49
|
+
await queue.subscribe('q1', async (msg) => { received.push(msg.data); });
|
|
50
|
+
await queue.unsubscribe('q1');
|
|
51
|
+
await queue.publish('q1', 'data');
|
|
52
|
+
expect(received).toHaveLength(0);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should retain dead letters when no subscribers', async () => {
|
|
56
|
+
const queue = new MemoryQueueAdapter();
|
|
57
|
+
const id = await queue.publish('orphan', { lost: true });
|
|
58
|
+
expect(id).toBe('msg-1');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should return queue size of 0 for in-memory queue', async () => {
|
|
62
|
+
const queue = new MemoryQueueAdapter();
|
|
63
|
+
expect(await queue.getQueueSize('test')).toBe(0);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('should purge a queue by removing handlers', async () => {
|
|
67
|
+
const queue = new MemoryQueueAdapter();
|
|
68
|
+
const received: unknown[] = [];
|
|
69
|
+
|
|
70
|
+
await queue.subscribe('q1', async (msg) => { received.push(msg.data); });
|
|
71
|
+
await queue.purge('q1');
|
|
72
|
+
await queue.publish('q1', 'data');
|
|
73
|
+
expect(received).toHaveLength(0);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('should increment message counter across publishes', async () => {
|
|
77
|
+
const queue = new MemoryQueueAdapter();
|
|
78
|
+
const ids: string[] = [];
|
|
79
|
+
|
|
80
|
+
await queue.subscribe('q', async () => {});
|
|
81
|
+
ids.push(await queue.publish('q', 'a'));
|
|
82
|
+
ids.push(await queue.publish('q', 'b'));
|
|
83
|
+
ids.push(await queue.publish('q', 'c'));
|
|
84
|
+
|
|
85
|
+
expect(ids).toEqual(['msg-1', 'msg-2', 'msg-3']);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
+
|
|
3
|
+
import type { IQueueService, QueuePublishOptions, QueueMessage, QueueHandler } from '@objectstack/spec/contracts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for MemoryQueueAdapter.
|
|
7
|
+
*/
|
|
8
|
+
export interface MemoryQueueAdapterOptions {
|
|
9
|
+
/** Maximum number of messages retained per queue (0 = unlimited) */
|
|
10
|
+
maxQueueSize?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* In-memory queue adapter implementing IQueueService.
|
|
15
|
+
*
|
|
16
|
+
* Provides synchronous in-process pub/sub delivery.
|
|
17
|
+
* Suitable for single-process environments, development, and testing.
|
|
18
|
+
*/
|
|
19
|
+
export class MemoryQueueAdapter implements IQueueService {
|
|
20
|
+
private readonly handlers = new Map<string, QueueHandler[]>();
|
|
21
|
+
private readonly deadLetters: QueueMessage[] = [];
|
|
22
|
+
private msgCounter = 0;
|
|
23
|
+
private readonly maxQueueSize: number;
|
|
24
|
+
|
|
25
|
+
constructor(options: MemoryQueueAdapterOptions = {}) {
|
|
26
|
+
this.maxQueueSize = options.maxQueueSize ?? 0;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async publish<T = unknown>(queue: string, data: T, options?: QueuePublishOptions): Promise<string> {
|
|
30
|
+
const id = `msg-${++this.msgCounter}`;
|
|
31
|
+
const msg: QueueMessage<T> = {
|
|
32
|
+
id,
|
|
33
|
+
data,
|
|
34
|
+
attempts: 0,
|
|
35
|
+
timestamp: Date.now(),
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const fns = this.handlers.get(queue) ?? [];
|
|
39
|
+
if (fns.length === 0) {
|
|
40
|
+
// No subscribers — retain as dead letter if within limits
|
|
41
|
+
if (this.maxQueueSize === 0 || this.deadLetters.length < this.maxQueueSize) {
|
|
42
|
+
this.deadLetters.push(msg);
|
|
43
|
+
}
|
|
44
|
+
return id;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const maxRetries = options?.retries ?? 0;
|
|
48
|
+
for (const handler of fns) {
|
|
49
|
+
let attempt = 0;
|
|
50
|
+
let success = false;
|
|
51
|
+
while (!success && attempt <= maxRetries) {
|
|
52
|
+
try {
|
|
53
|
+
msg.attempts = attempt + 1;
|
|
54
|
+
await handler(msg as QueueMessage);
|
|
55
|
+
success = true;
|
|
56
|
+
} catch {
|
|
57
|
+
attempt++;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return id;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async subscribe<T = unknown>(queue: string, handler: QueueHandler<T>): Promise<void> {
|
|
66
|
+
const existing = this.handlers.get(queue) ?? [];
|
|
67
|
+
this.handlers.set(queue, [...existing, handler as QueueHandler]);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async unsubscribe(queue: string): Promise<void> {
|
|
71
|
+
this.handlers.delete(queue);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async getQueueSize(_queue: string): Promise<number> {
|
|
75
|
+
// In-memory: no persistent queue depth tracking
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async purge(queue: string): Promise<void> {
|
|
80
|
+
this.handlers.delete(queue);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
+
|
|
3
|
+
import type { Plugin, PluginContext } from '@objectstack/core';
|
|
4
|
+
import { MemoryQueueAdapter } from './memory-queue-adapter.js';
|
|
5
|
+
import type { MemoryQueueAdapterOptions } from './memory-queue-adapter.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Configuration options for the QueueServicePlugin.
|
|
9
|
+
*/
|
|
10
|
+
export interface QueueServicePluginOptions {
|
|
11
|
+
/** Queue adapter type (default: 'memory') */
|
|
12
|
+
adapter?: 'memory' | 'bullmq';
|
|
13
|
+
/** Options for the memory queue adapter */
|
|
14
|
+
memory?: MemoryQueueAdapterOptions;
|
|
15
|
+
/** Redis connection URL (used when adapter is 'bullmq') */
|
|
16
|
+
redisUrl?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* QueueServicePlugin — Production IQueueService implementation.
|
|
21
|
+
*
|
|
22
|
+
* Registers a queue service with the kernel during the init phase.
|
|
23
|
+
* Supports in-memory and BullMQ adapters.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { ObjectKernel } from '@objectstack/core';
|
|
28
|
+
* import { QueueServicePlugin } from '@objectstack/service-queue';
|
|
29
|
+
*
|
|
30
|
+
* const kernel = new ObjectKernel();
|
|
31
|
+
* kernel.use(new QueueServicePlugin({ adapter: 'memory' }));
|
|
32
|
+
* await kernel.bootstrap();
|
|
33
|
+
*
|
|
34
|
+
* const queue = kernel.getService('queue');
|
|
35
|
+
* await queue.publish('orders', { orderId: 123 });
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export class QueueServicePlugin implements Plugin {
|
|
39
|
+
name = 'com.objectstack.service.queue';
|
|
40
|
+
version = '1.0.0';
|
|
41
|
+
type = 'standard';
|
|
42
|
+
|
|
43
|
+
private readonly options: QueueServicePluginOptions;
|
|
44
|
+
|
|
45
|
+
constructor(options: QueueServicePluginOptions = {}) {
|
|
46
|
+
this.options = { adapter: 'memory', ...options };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async init(ctx: PluginContext): Promise<void> {
|
|
50
|
+
const adapter = this.options.adapter;
|
|
51
|
+
if (adapter === 'bullmq') {
|
|
52
|
+
throw new Error(
|
|
53
|
+
'BullMQ queue adapter is not yet implemented. ' +
|
|
54
|
+
'Use adapter: "memory" or provide a custom IQueueService via ctx.registerService("queue", impl).'
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const queue = new MemoryQueueAdapter(this.options.memory);
|
|
59
|
+
ctx.registerService('queue', queue);
|
|
60
|
+
ctx.logger.info('QueueServicePlugin: registered memory queue adapter');
|
|
61
|
+
}
|
|
62
|
+
}
|