@nmtjs/core 0.15.0-beta.1 → 0.15.0-beta.2

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.
@@ -0,0 +1,9 @@
1
+ export const kOptionalDependency = Symbol.for('neemata:OptionalDependencyKey');
2
+ export const kInjectable = Symbol.for('neemata:InjectableKey');
3
+ export const kLazyInjectable = Symbol.for('neemata:LazyInjectableKey');
4
+ export const kValueInjectable = Symbol.for('neemata:ValueInjectableKey');
5
+ export const kFactoryInjectable = Symbol.for('neemata:FactoryInjectableKey');
6
+ export const kProvider = Symbol.for('neemata:ProviderKey');
7
+ export const kPlugin = Symbol.for('neemata:PluginKey');
8
+ export const kMetadata = Symbol.for('neemata:MetadataKey');
9
+ export const kHook = Symbol.for('neemata:HookKey');
@@ -0,0 +1,328 @@
1
+ import assert from 'node:assert';
2
+ import { tryCaptureStackTrace } from '@nmtjs/common';
3
+ import { Scope } from "./enums.js";
4
+ import { CoreInjectables, compareScope, createValueInjectable, getDepedencencyInjectable, isFactoryInjectable, isInjectable, isLazyInjectable, isOptionalInjectable, isValueInjectable, provide, } from "./injectables.js";
5
+ export class Container {
6
+ runtime;
7
+ scope;
8
+ parent;
9
+ instances = new Map();
10
+ resolvers = new Map();
11
+ injectables = new Set();
12
+ dependants = new Map();
13
+ disposing = false;
14
+ constructor(runtime, scope = Scope.Global, parent) {
15
+ this.runtime = runtime;
16
+ this.scope = scope;
17
+ this.parent = parent;
18
+ if (scope === Scope.Transient) {
19
+ throw new Error('Invalid scope');
20
+ }
21
+ this.provide(CoreInjectables.inject, this.createInjectFunction());
22
+ this.provide(CoreInjectables.dispose, this.createDisposeFunction());
23
+ }
24
+ async initialize(injectables) {
25
+ const measurements = [];
26
+ const traverse = (dependencies) => {
27
+ for (const key in dependencies) {
28
+ const dependency = dependencies[key];
29
+ const injectable = getDepedencencyInjectable(dependency);
30
+ if (injectable.scope === this.scope) {
31
+ this.injectables.add(injectable);
32
+ }
33
+ traverse(injectable.dependencies);
34
+ }
35
+ };
36
+ for (const dependant of injectables) {
37
+ traverse(dependant.dependencies);
38
+ }
39
+ await Promise.all([...this.injectables].map((injectable) => this.resolve(injectable, measurements)));
40
+ return measurements;
41
+ }
42
+ fork(scope) {
43
+ return new Container(this.runtime, scope, this);
44
+ }
45
+ find(scope) {
46
+ if (this.scope === scope) {
47
+ return this;
48
+ }
49
+ else {
50
+ return this.parent?.find(scope);
51
+ }
52
+ }
53
+ async [Symbol.asyncDispose]() {
54
+ await this.dispose();
55
+ }
56
+ async dispose() {
57
+ this.runtime.logger.trace('Disposing [%s] scope context...', this.scope);
58
+ // Prevent new resolutions during disposal
59
+ this.disposing = true;
60
+ // Get proper disposal order using topological sort
61
+ const disposalOrder = this.getDisposalOrder();
62
+ try {
63
+ // Dispose in the correct order
64
+ for (const injectable of disposalOrder) {
65
+ if (this.instances.has(injectable)) {
66
+ await this.disposeInjectableInstances(injectable);
67
+ }
68
+ }
69
+ }
70
+ catch (error) {
71
+ this.runtime.logger.fatal({ error }, 'Potential memory leak: error during container disposal');
72
+ }
73
+ this.instances.clear();
74
+ this.injectables.clear();
75
+ this.resolvers.clear();
76
+ this.dependants.clear();
77
+ this.disposing = false;
78
+ }
79
+ containsWithinSelf(injectable) {
80
+ return this.instances.has(injectable) || this.resolvers.has(injectable);
81
+ }
82
+ contains(injectable) {
83
+ return (this.containsWithinSelf(injectable) ||
84
+ (this.parent?.contains(injectable) ?? false));
85
+ }
86
+ get(injectable) {
87
+ if (injectable.scope === Scope.Transient) {
88
+ throw new Error('Cannot get transient injectable directly');
89
+ }
90
+ if (this.instances.has(injectable)) {
91
+ return this.instances.get(injectable).at(0).public;
92
+ }
93
+ if (this.parent?.contains(injectable)) {
94
+ return this.parent.get(injectable);
95
+ }
96
+ throw new Error('No instance found');
97
+ }
98
+ resolve(injectable, measurements) {
99
+ return this.resolveInjectable(injectable, undefined, undefined, measurements);
100
+ }
101
+ async createContext(dependencies) {
102
+ return this.createInjectableContext(dependencies);
103
+ }
104
+ async provide(injectable, ...[value]) {
105
+ const injections = Array.isArray(injectable)
106
+ ? injectable
107
+ : [provide(injectable, value)];
108
+ await Promise.all(injections.map(async ({ token, value }) => {
109
+ if (compareScope(token.scope, '>', this.scope)) {
110
+ // TODO: more informative error
111
+ throw new Error('Invalid scope');
112
+ }
113
+ const _value = isInjectable(value) ? await this.resolve(value) : value;
114
+ this.instances.set(token, [
115
+ { private: _value, public: _value, context: undefined },
116
+ ]);
117
+ }));
118
+ }
119
+ satisfies(injectable) {
120
+ return compareScope(injectable.scope, '<=', this.scope);
121
+ }
122
+ async disposeInjectableInstances(injectable) {
123
+ try {
124
+ if (this.instances.has(injectable)) {
125
+ const wrappers = this.instances.get(injectable);
126
+ await Promise.all(wrappers.map((wrapper) => this.disposeInjectableInstance(injectable, wrapper.private, wrapper.context)));
127
+ }
128
+ }
129
+ catch (cause) {
130
+ const error = new Error('Injectable disposal error. Potential memory leak', { cause });
131
+ this.runtime.logger.error(error);
132
+ }
133
+ finally {
134
+ this.instances.delete(injectable);
135
+ }
136
+ }
137
+ async disposeInjectableInstance(injectable, instance, context) {
138
+ if (isFactoryInjectable(injectable)) {
139
+ const { dispose } = injectable;
140
+ if (dispose)
141
+ await dispose(instance, context);
142
+ }
143
+ }
144
+ async createInjectableContext(dependencies, dependant, measurements) {
145
+ const injections = {};
146
+ const deps = Object.entries(dependencies);
147
+ const resolvers = Array(deps.length);
148
+ for (let i = 0; i < deps.length; i++) {
149
+ const [key, dependency] = deps[i];
150
+ const isOptional = isOptionalInjectable(dependency);
151
+ const injectable = getDepedencencyInjectable(dependency);
152
+ const resolver = this.resolveInjectable(injectable, dependant, isOptional, measurements);
153
+ resolvers[i] = resolver.then((value) => (injections[key] = value));
154
+ }
155
+ await Promise.all(resolvers);
156
+ return Object.freeze(injections);
157
+ }
158
+ resolveInjectable(injectable, dependant, isOptional, measurements) {
159
+ if (this.disposing) {
160
+ return Promise.reject(new Error('Cannot resolve during disposal'));
161
+ }
162
+ if (dependant && compareScope(dependant.scope, '<', injectable.scope)) {
163
+ // TODO: more informative error
164
+ return Promise.reject(new Error('Invalid scope: dependant is looser than injectable'));
165
+ }
166
+ if (isValueInjectable(injectable)) {
167
+ return Promise.resolve(injectable.value);
168
+ }
169
+ else if (this.parent?.contains(injectable) ||
170
+ (this.parent?.satisfies(injectable) &&
171
+ compareScope(this.parent.scope, '<', this.scope))) {
172
+ return this.parent.resolveInjectable(injectable, dependant, undefined, measurements);
173
+ }
174
+ else {
175
+ const { stack, label } = injectable;
176
+ if (dependant) {
177
+ let dependants = this.dependants.get(injectable);
178
+ if (!dependants) {
179
+ this.dependants.set(injectable, (dependants = new Set()));
180
+ }
181
+ dependants.add(dependant);
182
+ }
183
+ const isTransient = injectable.scope === Scope.Transient;
184
+ if (!isTransient && this.instances.has(injectable)) {
185
+ return Promise.resolve(this.instances.get(injectable).at(0).public);
186
+ }
187
+ else if (!isTransient && this.resolvers.has(injectable)) {
188
+ return this.resolvers.get(injectable);
189
+ }
190
+ else {
191
+ const isLazy = isLazyInjectable(injectable);
192
+ if (isLazy) {
193
+ if (isOptional)
194
+ return Promise.resolve(undefined);
195
+ return Promise.reject(new Error(`No instance provided for ${label || 'an'} injectable:\n${stack}`));
196
+ }
197
+ else {
198
+ const measure = measurements
199
+ ? performance.measure(injectable.label || injectable.stack || '')
200
+ : null;
201
+ const resolution = this.createResolution(injectable, measurements).finally(() => {
202
+ this.resolvers.delete(injectable);
203
+ // biome-ignore lint: false
204
+ // @ts-ignore
205
+ if (measurements && measure)
206
+ measurements.push(measure);
207
+ });
208
+ if (injectable.scope !== Scope.Transient) {
209
+ this.resolvers.set(injectable, resolution);
210
+ }
211
+ return resolution;
212
+ }
213
+ }
214
+ }
215
+ }
216
+ async createResolution(injectable, measurements) {
217
+ const { dependencies } = injectable;
218
+ const context = await this.createInjectableContext(dependencies, injectable, measurements);
219
+ const wrapper = {
220
+ private: null,
221
+ public: null,
222
+ context,
223
+ };
224
+ if (isFactoryInjectable(injectable)) {
225
+ wrapper.private = await Promise.resolve(injectable.factory(wrapper.context));
226
+ wrapper.public = injectable.pick(wrapper.private);
227
+ }
228
+ else {
229
+ throw new Error('Invalid injectable type');
230
+ }
231
+ let instances = this.instances.get(injectable);
232
+ if (!instances) {
233
+ instances = [];
234
+ this.instances.set(injectable, instances);
235
+ }
236
+ instances.push(wrapper);
237
+ return wrapper.public;
238
+ }
239
+ createInjectFunction() {
240
+ const inject = (injectable, context, scope = this.scope) => {
241
+ const container = this.find(scope);
242
+ if (!container)
243
+ throw new Error('No container found for the specified scope');
244
+ const dependencies = { ...injectable.dependencies };
245
+ for (const key in context) {
246
+ const dep = context[key];
247
+ if (isInjectable(dep) || isOptionalInjectable(dep)) {
248
+ dependencies[key] = dep;
249
+ }
250
+ else {
251
+ dependencies[key] = createValueInjectable(dep);
252
+ }
253
+ }
254
+ const newInjectable = {
255
+ ...injectable,
256
+ dependencies,
257
+ scope: Scope.Transient,
258
+ stack: tryCaptureStackTrace(1),
259
+ };
260
+ return container.resolve(newInjectable);
261
+ };
262
+ const explicit = async (injectable, context, scope = this.scope) => {
263
+ if ('asyncDispose' in Symbol === false) {
264
+ throw new Error('Symbol.asyncDispose is not supported in this environment');
265
+ }
266
+ const container = this.find(scope);
267
+ if (!container)
268
+ throw new Error('No container found for the specified scope');
269
+ const instance = await inject(injectable, context);
270
+ const dispose = container.createDisposeFunction();
271
+ return {
272
+ instance,
273
+ [Symbol.asyncDispose]: async () => {
274
+ await dispose(injectable, instance);
275
+ },
276
+ };
277
+ };
278
+ return Object.assign(inject, { explicit });
279
+ }
280
+ createDisposeFunction() {
281
+ return async (injectable, instance) => {
282
+ if (injectable.scope === Scope.Transient) {
283
+ assert(instance, 'Instance is required for transient injectable disposal');
284
+ const wrappers = this.instances.get(injectable);
285
+ if (wrappers) {
286
+ for (const wrapper of wrappers) {
287
+ if (wrapper.public === instance) {
288
+ await this.disposeInjectableInstance(injectable, wrapper.private, wrapper.context);
289
+ const index = wrappers.indexOf(wrapper);
290
+ wrappers.splice(index, 1);
291
+ }
292
+ }
293
+ if (wrappers.length === 0) {
294
+ this.instances.delete(injectable);
295
+ }
296
+ }
297
+ }
298
+ else {
299
+ await this.disposeInjectableInstances(injectable);
300
+ }
301
+ };
302
+ }
303
+ getDisposalOrder() {
304
+ const visited = new Set();
305
+ const result = [];
306
+ const visit = (injectable) => {
307
+ if (visited.has(injectable))
308
+ return;
309
+ visited.add(injectable);
310
+ const dependants = this.dependants.get(injectable);
311
+ if (dependants) {
312
+ for (const dependant of dependants) {
313
+ if (this.instances.has(dependant)) {
314
+ visit(dependant);
315
+ }
316
+ }
317
+ }
318
+ // Only add to result if this container owns the instance
319
+ if (this.instances.has(injectable)) {
320
+ result.push(injectable);
321
+ }
322
+ };
323
+ for (const injectable of this.instances.keys()) {
324
+ visit(injectable);
325
+ }
326
+ return result;
327
+ }
328
+ }
package/dist/enums.js ADDED
@@ -0,0 +1,7 @@
1
+ export var Scope;
2
+ (function (Scope) {
3
+ Scope["Global"] = "Global";
4
+ Scope["Connection"] = "Connection";
5
+ Scope["Call"] = "Call";
6
+ Scope["Transient"] = "Transient";
7
+ })(Scope || (Scope = {}));
package/dist/hooks.js ADDED
@@ -0,0 +1,11 @@
1
+ import { Hookable } from 'hookable';
2
+ export class Hooks extends Hookable {
3
+ _;
4
+ createSignal(hook) {
5
+ const controller = new AbortController();
6
+ const unregister = this.hookOnce(String(hook),
7
+ //@ts-expect-error
8
+ () => controller.abort());
9
+ return { controller, signal: controller.signal, unregister };
10
+ }
11
+ }
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ // // biome-ignore lint/correctness/noUnusedImports: TSC wants it
2
+ // // biome-ignore assist/source/organizeImports: TSC wants it
3
+ // import {} from 'pino'
4
+ export * from "./constants.js";
5
+ export * from "./container.js";
6
+ export * from "./enums.js";
7
+ export * from "./hooks.js";
8
+ export * from "./injectables.js";
9
+ export * from "./logger.js";
10
+ export * from "./metadata.js";
11
+ export * from "./plugin.js";
12
+ export * from "./types.js";
13
+ export * from "./utils/index.js";
@@ -0,0 +1,141 @@
1
+ import { tryCaptureStackTrace } from '@nmtjs/common';
2
+ import { kFactoryInjectable, kInjectable, kLazyInjectable, kOptionalDependency, kValueInjectable, } from "./constants.js";
3
+ import { Scope } from "./enums.js";
4
+ const ScopeStrictness = {
5
+ [Scope.Transient]: Number.NaN, // this should make it always fail to compare with other scopes
6
+ [Scope.Global]: 1,
7
+ [Scope.Connection]: 2,
8
+ [Scope.Call]: 3,
9
+ };
10
+ export const isLazyInjectable = (injectable) => injectable[kLazyInjectable];
11
+ export const isFactoryInjectable = (injectable) => injectable[kFactoryInjectable];
12
+ export const isValueInjectable = (injectable) => injectable[kValueInjectable];
13
+ export const isInjectable = (injectable) => injectable[kInjectable];
14
+ export const isOptionalInjectable = (injectable) => injectable[kOptionalDependency];
15
+ export function getInjectableScope(injectable) {
16
+ let scope = injectable.scope;
17
+ const deps = injectable.dependencies;
18
+ for (const key in deps) {
19
+ const dependency = deps[key];
20
+ const injectable = getDepedencencyInjectable(dependency);
21
+ const dependencyScope = getInjectableScope(injectable);
22
+ if (dependencyScope !== Scope.Transient &&
23
+ scope !== Scope.Transient &&
24
+ compareScope(dependencyScope, '>', scope)) {
25
+ scope = dependencyScope;
26
+ }
27
+ }
28
+ return scope;
29
+ }
30
+ export function getDepedencencyInjectable(dependency) {
31
+ if (kOptionalDependency in dependency) {
32
+ return dependency.injectable;
33
+ }
34
+ return dependency;
35
+ }
36
+ export function createOptionalInjectable(injectable) {
37
+ return Object.freeze({
38
+ [kOptionalDependency]: true,
39
+ injectable,
40
+ });
41
+ }
42
+ export function createLazyInjectable(scope = Scope.Global, label, stackTraceDepth = 0) {
43
+ const injectable = Object.freeze({
44
+ scope,
45
+ dependencies: {},
46
+ label,
47
+ stack: tryCaptureStackTrace(stackTraceDepth),
48
+ optional: () => createOptionalInjectable(injectable),
49
+ $withType: () => injectable,
50
+ [kInjectable]: true,
51
+ [kLazyInjectable]: true,
52
+ });
53
+ return injectable;
54
+ }
55
+ export function createValueInjectable(value, label, stackTraceDepth = 0) {
56
+ return Object.freeze({
57
+ value,
58
+ scope: Scope.Global,
59
+ dependencies: {},
60
+ label,
61
+ stack: tryCaptureStackTrace(stackTraceDepth),
62
+ [kInjectable]: true,
63
+ [kValueInjectable]: true,
64
+ });
65
+ }
66
+ export function createFactoryInjectable(paramsOrFactory, label, stackTraceDepth = 0) {
67
+ const isFactory = typeof paramsOrFactory === 'function';
68
+ const params = isFactory ? { factory: paramsOrFactory } : paramsOrFactory;
69
+ const injectable = {
70
+ dependencies: (params.dependencies ?? {}),
71
+ scope: (params.scope ?? Scope.Global),
72
+ factory: params.factory,
73
+ dispose: params.dispose,
74
+ pick: params.pick ?? ((instance) => instance),
75
+ label,
76
+ stack: tryCaptureStackTrace(stackTraceDepth),
77
+ optional: () => createOptionalInjectable(injectable),
78
+ [kInjectable]: true,
79
+ [kFactoryInjectable]: true,
80
+ };
81
+ injectable.scope = resolveInjectableScope(typeof params.scope === 'undefined', injectable);
82
+ return Object.freeze(injectable);
83
+ }
84
+ export function substitute(injectable, substitution, stackTraceDepth = 0) {
85
+ const dependencies = { ...injectable.dependencies };
86
+ const depth = stackTraceDepth + 1;
87
+ for (const key in substitution) {
88
+ const value = substitution[key];
89
+ if (key in dependencies) {
90
+ const original = dependencies[key];
91
+ if (isInjectable(value)) {
92
+ dependencies[key] = value;
93
+ }
94
+ else if (isFactoryInjectable(original)) {
95
+ dependencies[key] = substitute(original, value, depth);
96
+ }
97
+ }
98
+ }
99
+ if (isFactoryInjectable(injectable)) {
100
+ // @ts-expect-error
101
+ return createFactoryInjectable({ ...injectable, dependencies }, injectable.label, depth);
102
+ }
103
+ throw new Error('Invalid injectable type');
104
+ }
105
+ export function compareScope(left, operator, right) {
106
+ const leftScope = ScopeStrictness[left];
107
+ const rightScope = ScopeStrictness[right];
108
+ switch (operator) {
109
+ case '=':
110
+ return leftScope === rightScope;
111
+ case '!=':
112
+ return leftScope !== rightScope;
113
+ case '>':
114
+ return leftScope > rightScope;
115
+ case '<':
116
+ return leftScope < rightScope;
117
+ case '>=':
118
+ return leftScope >= rightScope;
119
+ case '<=':
120
+ return leftScope <= rightScope;
121
+ default:
122
+ throw new Error('Invalid operator');
123
+ }
124
+ }
125
+ const logger = Object.assign((label) => createFactoryInjectable({
126
+ dependencies: { logger },
127
+ scope: Scope.Global,
128
+ factory: ({ logger }) => logger.child({ $label: label }),
129
+ }), createLazyInjectable(Scope.Global, 'Logger'));
130
+ const inject = createLazyInjectable(Scope.Global, 'Inject function');
131
+ const dispose = createLazyInjectable(Scope.Global, 'Dispose function');
132
+ function resolveInjectableScope(isDefaultScope, injectable) {
133
+ const actualScope = getInjectableScope(injectable);
134
+ if (!isDefaultScope && compareScope(actualScope, '>', injectable.scope))
135
+ throw new Error(`Invalid scope ${injectable.scope} for an injectable: dependencies have stricter scope - ${actualScope}`);
136
+ return actualScope;
137
+ }
138
+ export const CoreInjectables = { logger, inject, dispose };
139
+ export const provide = (token, value) => {
140
+ return { token, value };
141
+ };
package/dist/logger.js ADDED
@@ -0,0 +1,66 @@
1
+ import { threadId } from 'node:worker_threads';
2
+ import { pino, stdTimeFunctions } from 'pino';
3
+ import { build as pretty } from 'pino-pretty';
4
+ import { errWithCause } from 'pino-std-serializers';
5
+ // TODO: use node:util inspect
6
+ const bg = (value, color) => `\x1b[${color}m${value}\x1b[0m`;
7
+ const fg = (value, color) => `\x1b[38;5;${color}m${value}\x1b[0m`;
8
+ const levelColors = {
9
+ 10: 100,
10
+ 20: 102,
11
+ 30: 106,
12
+ 40: 104,
13
+ 50: 101,
14
+ 60: 105,
15
+ [Number.POSITIVE_INFINITY]: 0,
16
+ };
17
+ const messageColors = {
18
+ 10: 0,
19
+ 20: 2,
20
+ 30: 6,
21
+ 40: 4,
22
+ 50: 1,
23
+ 60: 5,
24
+ [Number.POSITIVE_INFINITY]: 0,
25
+ };
26
+ const levelLabels = {
27
+ 10: ' TRACE ',
28
+ 20: ' DEBUG ',
29
+ 30: ' INFO ',
30
+ 40: ' WARN ',
31
+ 50: ' ERROR ',
32
+ 60: ' FATAL ',
33
+ [Number.POSITIVE_INFINITY]: 'SILENT',
34
+ };
35
+ export const createLogger = (options = {}, $lable) => {
36
+ let { destinations, pinoOptions } = options;
37
+ if (!destinations || !destinations?.length) {
38
+ destinations = [
39
+ createConsolePrettyDestination((options.pinoOptions?.level || 'info')),
40
+ ];
41
+ }
42
+ const lowestLevelValue = destinations.reduce((acc, destination) => Math.min(acc, 'stream' in destination
43
+ ? pino.levels.values[destination.level]
44
+ : Number.POSITIVE_INFINITY), Number.POSITIVE_INFINITY);
45
+ const level = pino.levels.labels[lowestLevelValue];
46
+ const serializers = { ...pinoOptions?.serializers, err: errWithCause };
47
+ return pino({ timestamp: stdTimeFunctions.isoTime, ...pinoOptions, level, serializers }, pino.multistream(destinations)).child({ $lable, $threadId: threadId });
48
+ };
49
+ export const createConsolePrettyDestination = (level, sync = true) => ({
50
+ level,
51
+ stream: pretty({
52
+ colorize: true,
53
+ ignore: 'hostname,$lable,$threadId',
54
+ errorLikeObjectKeys: ['err', 'error', 'cause'],
55
+ messageFormat: (log, messageKey) => {
56
+ const group = fg(`[${log.$lable}]`, 11);
57
+ const msg = fg(log[messageKey], messageColors[log.level]);
58
+ const thread = fg(`(Thread-${log.$threadId})`, 89);
59
+ return `\x1b[0m${thread} ${group} ${msg}`;
60
+ },
61
+ customPrettifiers: {
62
+ level: (level) => bg(levelLabels[level], levelColors[level]),
63
+ },
64
+ sync,
65
+ }),
66
+ });
@@ -0,0 +1,15 @@
1
+ import { kMetadata } from "./constants.js";
2
+ export const createMetadataKey = (key) => {
3
+ const metadataKey = {
4
+ [kMetadata]: key,
5
+ as(value) {
6
+ return { key: metadataKey, value };
7
+ },
8
+ };
9
+ return metadataKey;
10
+ };
11
+ export class MetadataStore extends Map {
12
+ get(key) {
13
+ return super.get(key);
14
+ }
15
+ }
package/dist/plugin.js ADDED
@@ -0,0 +1,3 @@
1
+ import { kPlugin } from "./constants.js";
2
+ export const createPlugin = (name, factory) => ({ name, factory, [kPlugin]: true });
3
+ export const isPlugin = (value) => kPlugin in value;
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ export { match } from '@nmtjs/common';
2
+ export function isJsFile(name) {
3
+ if (name.endsWith('.d.ts'))
4
+ return false;
5
+ const leading = name.split('.').slice(1);
6
+ const ext = leading.join('.');
7
+ return ['js', 'mjs', 'cjs', 'ts', 'mts', 'cts'].includes(ext);
8
+ }
9
+ export function pick(obj, keys) {
10
+ const result = {};
11
+ for (const key in keys) {
12
+ if (key in obj) {
13
+ result[key] = obj[key];
14
+ }
15
+ }
16
+ return result;
17
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./functions.js";
2
+ export * from "./pool.js";
3
+ export * from "./semaphore.js";
@@ -0,0 +1,105 @@
1
+ export class PoolError extends Error {
2
+ }
3
+ // Fixed pool from https://github.com/metarhia/metautil
4
+ export class Pool {
5
+ options;
6
+ #items = [];
7
+ #free = [];
8
+ #queue = [];
9
+ #current = 0;
10
+ #size = 0;
11
+ #available = 0;
12
+ constructor(options = {}) {
13
+ this.options = options;
14
+ }
15
+ add(item) {
16
+ if (this.#items.includes(item))
17
+ throw new PoolError('Item already exists');
18
+ this.#size++;
19
+ this.#available++;
20
+ this.#items.push(item);
21
+ this.#free.push(true);
22
+ }
23
+ remove(item) {
24
+ if (this.#size === 0)
25
+ throw new PoolError('Pool is empty');
26
+ const index = this.#items.indexOf(item);
27
+ if (index < 0)
28
+ throw new PoolError('Item is not in the pool');
29
+ const isCaptured = this.isFree(item);
30
+ if (isCaptured)
31
+ this.#available--;
32
+ this.#size--;
33
+ this.#current--;
34
+ this.#items.splice(index, 1);
35
+ this.#free.splice(index, 1);
36
+ }
37
+ capture(timeout = this.options.timeout) {
38
+ return this.next(true, timeout);
39
+ }
40
+ async next(exclusive = false, timeout = this.options.timeout) {
41
+ if (this.#size === 0)
42
+ throw new PoolError('Pool is empty');
43
+ if (this.#available === 0) {
44
+ return new Promise((resolve, reject) => {
45
+ const waiting = {
46
+ resolve: (item) => {
47
+ if (exclusive)
48
+ this.#capture(item);
49
+ resolve(item);
50
+ },
51
+ timer: undefined,
52
+ };
53
+ if (timeout) {
54
+ waiting.timer = setTimeout(() => {
55
+ waiting.resolve = undefined;
56
+ this.#queue.shift();
57
+ reject(new PoolError('Next item timeout'));
58
+ }, timeout);
59
+ }
60
+ this.#queue.push(waiting);
61
+ });
62
+ }
63
+ let item;
64
+ let free = false;
65
+ do {
66
+ item = this.#items[this.#current];
67
+ free = this.#free[this.#current];
68
+ this.#current++;
69
+ if (this.#current >= this.#size)
70
+ this.#current = 0;
71
+ } while (typeof item === 'undefined' || !free);
72
+ if (exclusive)
73
+ this.#capture(item);
74
+ return item;
75
+ }
76
+ release(item) {
77
+ const index = this.#items.indexOf(item);
78
+ if (index < 0)
79
+ throw new PoolError('Unexpected item');
80
+ if (this.#free[index])
81
+ throw new PoolError('Unable to release not captured item');
82
+ this.#free[index] = true;
83
+ this.#available++;
84
+ if (this.#queue.length > 0) {
85
+ const { resolve, timer } = this.#queue.shift();
86
+ clearTimeout(timer);
87
+ if (resolve)
88
+ setTimeout(resolve, 0, item);
89
+ }
90
+ }
91
+ isFree(item) {
92
+ const index = this.#items.indexOf(item);
93
+ if (index < 0)
94
+ return false;
95
+ return this.#free[index];
96
+ }
97
+ get items() {
98
+ return [...this.#items];
99
+ }
100
+ #capture(item) {
101
+ const index = this.#items.indexOf(item);
102
+ this.#free[index] = false;
103
+ this.#available--;
104
+ }
105
+ }
@@ -0,0 +1,55 @@
1
+ export class SemaphoreError extends Error {
2
+ }
3
+ // Semaphore from https://github.com/metarhia/metautil
4
+ export class Semaphore {
5
+ size;
6
+ timeout;
7
+ counter;
8
+ queue = [];
9
+ constructor(concurrency, size = 0, timeout = 0) {
10
+ this.size = size;
11
+ this.timeout = timeout;
12
+ this.counter = concurrency;
13
+ }
14
+ enter() {
15
+ if (this.counter > 0) {
16
+ this.counter--;
17
+ return Promise.resolve();
18
+ }
19
+ else if (this.queue.length >= this.size) {
20
+ return Promise.reject(new SemaphoreError('Queue is full'));
21
+ }
22
+ else {
23
+ return new Promise((resolve, reject) => {
24
+ const waiting = { resolve };
25
+ waiting.timer = setTimeout(() => {
26
+ waiting.resolve = undefined;
27
+ this.queue.shift();
28
+ reject(new SemaphoreError('Timeout'));
29
+ }, this.timeout);
30
+ this.queue.push(waiting);
31
+ });
32
+ }
33
+ }
34
+ leave() {
35
+ if (this.queue.length === 0) {
36
+ this.counter++;
37
+ }
38
+ else {
39
+ const item = this.queue.shift();
40
+ if (item) {
41
+ const { resolve, timer } = item;
42
+ if (timer)
43
+ clearTimeout(timer);
44
+ if (resolve)
45
+ setTimeout(resolve, 0);
46
+ }
47
+ }
48
+ }
49
+ get isEmpty() {
50
+ return this.queue.length === 0;
51
+ }
52
+ get queueLength() {
53
+ return this.queue.length;
54
+ }
55
+ }
package/package.json CHANGED
@@ -8,15 +8,15 @@
8
8
  "peerDependencies": {
9
9
  "pino": "^9.6.0",
10
10
  "pino-pretty": "^13.0.0",
11
- "@nmtjs/common": "0.15.0-beta.1",
12
- "@nmtjs/type": "0.15.0-beta.1"
11
+ "@nmtjs/common": "0.15.0-beta.2",
12
+ "@nmtjs/type": "0.15.0-beta.2"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@types/node": "^24",
16
16
  "pino": "^9.6.0",
17
17
  "pino-pretty": "^13.0.0",
18
- "@nmtjs/common": "0.15.0-beta.1",
19
- "@nmtjs/type": "0.15.0-beta.1"
18
+ "@nmtjs/common": "0.15.0-beta.2",
19
+ "@nmtjs/type": "0.15.0-beta.2"
20
20
  },
21
21
  "files": [
22
22
  "dist",
@@ -27,7 +27,7 @@
27
27
  "hookable": "6.0.0-rc.1",
28
28
  "pino-std-serializers": "^7.0.0"
29
29
  },
30
- "version": "0.15.0-beta.1",
30
+ "version": "0.15.0-beta.2",
31
31
  "scripts": {
32
32
  "clean-build": "rm -rf ./dist",
33
33
  "build": "tsc",