@nmtjs/core 0.14.4 → 0.15.0-beta.1

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.
@@ -1,119 +0,0 @@
1
- import type { Async, ClassConstructor, ClassConstructorArgs, ClassInstance } from '@nmtjs/common';
2
- import type { Hook } from './enums.ts';
3
- import type { HookType } from './hooks.ts';
4
- import type { Logger } from './logger.ts';
5
- import type { Registry } from './registry.ts';
6
- import { kClassInjectable, kClassInjectableCreate, kClassInjectableDispose, kFactoryInjectable, kInjectable, kLazyInjectable, kOptionalDependency, kValueInjectable } from './constants.ts';
7
- import { Scope } from './enums.ts';
8
- import { Hooks } from './hooks.ts';
9
- export type DependencyOptional<T extends AnyInjectable = AnyInjectable> = {
10
- [kOptionalDependency]: any;
11
- injectable: T;
12
- };
13
- export type Depedency = DependencyOptional | AnyInjectable;
14
- export type Dependencies = Record<string, Depedency>;
15
- export type ResolveInjectableType<T extends AnyInjectable> = T extends Injectable<infer Type, any, any> ? Type : never;
16
- export interface Dependant<Deps extends Dependencies = Dependencies> {
17
- dependencies: Deps;
18
- label?: string;
19
- stack?: string;
20
- }
21
- export type DependencyInjectable<T extends Depedency> = T extends AnyInjectable ? T : T extends DependencyOptional ? T['injectable'] : never;
22
- export type DependencyContext<Deps extends Dependencies> = {
23
- readonly [K in keyof Deps as Deps[K] extends AnyInjectable ? K : never]: Deps[K] extends AnyInjectable ? ResolveInjectableType<Deps[K]> : never;
24
- } & {
25
- readonly [K in keyof Deps as Deps[K] extends DependencyOptional ? K : never]?: Deps[K] extends DependencyOptional ? ResolveInjectableType<Deps[K]['injectable']> : never;
26
- };
27
- export type InjectableFactoryType<InjectableType, InjectableDeps extends Dependencies> = (context: DependencyContext<InjectableDeps>) => Async<InjectableType>;
28
- export type InjectablePickType<Input, Output> = (injectable: Input) => Output;
29
- export type InjectableDisposeType<InjectableType, InjectableDeps extends Dependencies> = (instance: InjectableType, context: DependencyContext<InjectableDeps>) => any;
30
- export interface LazyInjectable<T, S extends Scope = Scope.Global> extends Dependant<{}> {
31
- scope: S;
32
- [kInjectable]: any;
33
- [kLazyInjectable]: T;
34
- }
35
- export interface ValueInjectable<T> extends Dependant<{}> {
36
- scope: Scope.Global;
37
- value: T;
38
- [kInjectable]: any;
39
- [kValueInjectable]: any;
40
- }
41
- export interface FactoryInjectable<T, D extends Dependencies = {}, S extends Scope = Scope.Global, P = T> extends Dependant<D> {
42
- scope: S;
43
- factory: InjectableFactoryType<P, D>;
44
- pick: InjectablePickType<P, T>;
45
- dispose?: InjectableDisposeType<P, D>;
46
- [kInjectable]: any;
47
- [kFactoryInjectable]: any;
48
- }
49
- export interface BaseClassInjectable<T, D extends Dependencies = {}, S extends Scope = Scope.Global> extends Dependant<D> {
50
- new (...args: any[]): T;
51
- scope: S;
52
- [kInjectable]: any;
53
- [kClassInjectable]: any;
54
- }
55
- export interface ClassInjectable<T, D extends Dependencies = {}, S extends Scope = Scope.Global, A extends any[] = []> extends Dependant<D> {
56
- new ($context: DependencyContext<D>, ...args: A): T & {
57
- $context: DependencyContext<D>;
58
- [kClassInjectableCreate]?: () => Promise<void>;
59
- [kClassInjectableDispose]?: () => Promise<void>;
60
- };
61
- scope: S;
62
- [kInjectable]: any;
63
- [kClassInjectable]: any;
64
- }
65
- export type Injectable<V = any, D extends Dependencies = {}, S extends Scope = Scope> = LazyInjectable<V, S> | ValueInjectable<V> | FactoryInjectable<V, D, S, any> | BaseClassInjectable<V, D, S>;
66
- export type AnyInjectable<T = any, S extends Scope = Scope> = Injectable<T, any, S>;
67
- export declare const isLazyInjectable: (injectable: any) => injectable is LazyInjectable<any>;
68
- export declare const isFactoryInjectable: (injectable: any) => injectable is FactoryInjectable<any>;
69
- export declare const isClassInjectable: (injectable: any) => injectable is ClassInjectable<any>;
70
- export declare const isValueInjectable: (injectable: any) => injectable is ValueInjectable<any>;
71
- export declare const isInjectable: (injectable: any) => injectable is AnyInjectable<any>;
72
- export declare const isOptionalInjectable: (injectable: any) => injectable is DependencyOptional<any>;
73
- export declare function getInjectableScope(injectable: AnyInjectable): Scope;
74
- export declare function getDepedencencyInjectable(dependency: Depedency): AnyInjectable;
75
- export declare function createOptionalInjectable<T extends AnyInjectable>(injectable: T): DependencyOptional<T>;
76
- export declare function createLazyInjectable<T, S extends Scope = Scope.Global>(scope?: S, label?: string, stackTraceDepth?: number): LazyInjectable<T, S>;
77
- export declare function createValueInjectable<T>(value: T, label?: string, stackTraceDepth?: number): ValueInjectable<T>;
78
- export declare function createFactoryInjectable<T, D extends Dependencies = {}, S extends Scope = Scope.Global, P = T>(paramsOrFactory: {
79
- dependencies?: D;
80
- scope?: S;
81
- pick?: InjectablePickType<P, T>;
82
- factory: InjectableFactoryType<P, D>;
83
- dispose?: InjectableDisposeType<P, D>;
84
- } | InjectableFactoryType<P, D>, label?: string, stackTraceDepth?: number): FactoryInjectable<null extends T ? P : T, D, S, P>;
85
- export declare const createClassInjectable: <D extends Dependencies = {}, S extends Scope = Scope.Global>(dependencies?: D, scope?: S, stackTraceDepth?: number) => ClassInjectable<ClassInstance<{
86
- new ($context: DependencyContext<D>): {
87
- $context: DependencyContext<D>;
88
- [kClassInjectableCreate](): Promise<void>;
89
- [kClassInjectableDispose](): Promise<void>;
90
- };
91
- dependencies: D;
92
- scope: S;
93
- stack: string | undefined;
94
- get label(): string;
95
- [kInjectable]: boolean;
96
- [kClassInjectable]: boolean;
97
- }>, D, S>;
98
- export declare function createExtendableClassInjectable<B extends ClassConstructor<any>, D extends Dependencies = {}, S extends Scope = Scope.Global>(baseClass: B, dependencies?: D, scope?: S, stackTraceDepth?: number): B extends ClassInjectable<any> ? ClassInjectable<ClassInstance<B>, D, S> : ClassInjectable<ClassInstance<B>, D, S, ClassConstructorArgs<B, []>>;
99
- export type DependenciesSubstitution<T extends Dependencies> = {
100
- [K in keyof T]?: T[K] extends AnyInjectable<infer Type> ? AnyInjectable<Type> | DependenciesSubstitution<T[K]['dependencies']> : never;
101
- };
102
- export declare function substitute<T extends FactoryInjectable<any, any, Scope> | BaseClassInjectable<any, any, Scope>>(injectable: T, substitution: DependenciesSubstitution<T['dependencies']>, stackTraceDepth?: number): T;
103
- export declare function compareScope(left: Scope, operator: '>' | '<' | '>=' | '<=' | '=' | '!=', right: Scope): boolean;
104
- export declare const CoreInjectables: {
105
- logger: LazyInjectable<Logger, Scope.Global>;
106
- registry: LazyInjectable<Registry, Scope.Global>;
107
- inject: LazyInjectable<(<T extends AnyInjectable>(injectable: T, context: { [K in keyof T["dependencies"]]?: ResolveInjectableType<T["dependencies"][K]> | AnyInjectable<ResolveInjectableType<T["dependencies"][K]>> | undefined; }) => Promise<ResolveInjectableType<T>>) & {
108
- explicit: <T extends AnyInjectable>(injectable: T, context: { [K in keyof T["dependencies"]]?: ResolveInjectableType<T["dependencies"][K]> | AnyInjectable<ResolveInjectableType<T["dependencies"][K]>> | undefined; }) => Promise<Awaited<ResolveInjectableType<T>> & {
109
- [Symbol.asyncDispose]: () => Promise<void>;
110
- }>;
111
- }, Scope.Global>;
112
- dispose: LazyInjectable<(<T extends AnyInjectable>(injectable: T, instance?: any) => Promise<void>), Scope.Global>;
113
- hook: FactoryInjectable<(<T extends Hook>(name: T, callback: HookType[T]) => () => void), {
114
- registry: LazyInjectable<Registry, Scope.Global>;
115
- }, Scope.Transient, {
116
- hooks: Hooks;
117
- on: <T extends Hook>(name: T, callback: HookType[T]) => () => void;
118
- }>;
119
- };
@@ -1,221 +0,0 @@
1
- import { tryCaptureStackTrace } from '@nmtjs/common';
2
- import { kClassInjectable, kClassInjectableCreate, kClassInjectableDispose, kFactoryInjectable, kHookCollection, kInjectable, kLazyInjectable, kOptionalDependency, kValueInjectable, } from "./constants.js";
3
- import { Scope } from "./enums.js";
4
- import { Hooks } from "./hooks.js";
5
- const ScopeStrictness = {
6
- [Scope.Transient]: Number.NaN, // this should make it always fail to compare with other scopes
7
- [Scope.Global]: 1,
8
- [Scope.Connection]: 2,
9
- [Scope.Call]: 3,
10
- };
11
- export const isLazyInjectable = (injectable) => injectable[kLazyInjectable];
12
- export const isFactoryInjectable = (injectable) => injectable[kFactoryInjectable];
13
- export const isClassInjectable = (injectable) => injectable[kClassInjectable];
14
- export const isValueInjectable = (injectable) => injectable[kValueInjectable];
15
- export const isInjectable = (injectable) => injectable[kInjectable];
16
- export const isOptionalInjectable = (injectable) => injectable[kOptionalDependency];
17
- export function getInjectableScope(injectable) {
18
- let scope = injectable.scope;
19
- const deps = injectable.dependencies;
20
- for (const key in deps) {
21
- const dependency = deps[key];
22
- const injectable = getDepedencencyInjectable(dependency);
23
- const dependencyScope = getInjectableScope(injectable);
24
- if (dependencyScope !== Scope.Transient &&
25
- scope !== Scope.Transient &&
26
- compareScope(dependencyScope, '>', scope)) {
27
- scope = dependencyScope;
28
- }
29
- }
30
- return scope;
31
- }
32
- export function getDepedencencyInjectable(dependency) {
33
- if (kOptionalDependency in dependency) {
34
- return dependency.injectable;
35
- }
36
- return dependency;
37
- }
38
- export function createOptionalInjectable(injectable) {
39
- return Object.freeze({
40
- [kOptionalDependency]: true,
41
- injectable,
42
- });
43
- }
44
- export function createLazyInjectable(scope = Scope.Global, label, stackTraceDepth = 0) {
45
- return Object.freeze({
46
- scope,
47
- dependencies: {},
48
- label,
49
- stack: tryCaptureStackTrace(stackTraceDepth),
50
- [kInjectable]: true,
51
- [kLazyInjectable]: true,
52
- });
53
- }
54
- export function createValueInjectable(value, label, stackTraceDepth = 0) {
55
- return Object.freeze({
56
- value,
57
- scope: Scope.Global,
58
- dependencies: {},
59
- label,
60
- stack: tryCaptureStackTrace(stackTraceDepth),
61
- [kInjectable]: true,
62
- [kValueInjectable]: true,
63
- });
64
- }
65
- export function createFactoryInjectable(paramsOrFactory, label, stackTraceDepth = 0) {
66
- const isFactory = typeof paramsOrFactory === 'function';
67
- const params = isFactory ? { factory: paramsOrFactory } : paramsOrFactory;
68
- const injectable = {
69
- dependencies: (params.dependencies ?? {}),
70
- scope: (params.scope ?? Scope.Global),
71
- factory: params.factory,
72
- dispose: params.dispose,
73
- pick: params.pick ?? ((instance) => instance),
74
- label,
75
- stack: tryCaptureStackTrace(stackTraceDepth),
76
- [kInjectable]: true,
77
- [kFactoryInjectable]: true,
78
- };
79
- injectable.scope = resolveInjectableScope(typeof params.scope === 'undefined', injectable);
80
- return Object.freeze(injectable);
81
- }
82
- export const createClassInjectable = (dependencies = {}, scope, stackTraceDepth = 0) => {
83
- const InjectableClass = class {
84
- $context;
85
- static dependencies = dependencies;
86
- static scope = (scope ?? Scope.Global);
87
- static stack = tryCaptureStackTrace(stackTraceDepth + 2);
88
- static [kInjectable] = true;
89
- static [kClassInjectable] = true;
90
- static get label() {
91
- // biome-ignore lint/complexity/noThisInStatic: ok
92
- return this.name;
93
- }
94
- constructor($context) {
95
- this.$context = $context;
96
- }
97
- async [kClassInjectableCreate]() { }
98
- async [kClassInjectableDispose]() { }
99
- };
100
- InjectableClass.scope = resolveInjectableScope(typeof scope === 'undefined', InjectableClass);
101
- return InjectableClass;
102
- };
103
- export function createExtendableClassInjectable(baseClass, dependencies = {}, scope, stackTraceDepth = 0) {
104
- if (isClassInjectable(baseClass)) {
105
- if (scope && compareScope(baseClass.scope, '>', scope)) {
106
- throw new Error(`Invalid scope ${scope} for an extendable class injectable: base class have stricter scope - ${baseClass.scope}`);
107
- }
108
- else {
109
- scope = scope ?? baseClass.scope;
110
- }
111
- dependencies = Object.assign({}, baseClass.dependencies, dependencies);
112
- }
113
- const InjectableClass = class extends baseClass {
114
- static dependencies = dependencies;
115
- static scope = (scope ?? Scope.Global);
116
- static stack = tryCaptureStackTrace(stackTraceDepth);
117
- static [kInjectable] = true;
118
- static [kClassInjectable] = true;
119
- static get label() {
120
- // biome-ignore lint/complexity/noThisInStatic: ok
121
- return this.name;
122
- }
123
- $context;
124
- constructor(...args) {
125
- const [$context, ...baseClassArgs] = args;
126
- if (isClassInjectable(baseClass)) {
127
- super($context);
128
- }
129
- else {
130
- super(...baseClassArgs);
131
- }
132
- this.$context = $context;
133
- }
134
- async $onCreate() {
135
- await super.$onCreate?.();
136
- }
137
- async $onDispose() {
138
- await super.$onDispose?.();
139
- }
140
- };
141
- InjectableClass.scope = resolveInjectableScope(typeof scope === 'undefined', InjectableClass);
142
- // @ts-expect-error
143
- return InjectableClass;
144
- }
145
- export function substitute(injectable, substitution, stackTraceDepth = 0) {
146
- const dependencies = { ...injectable.dependencies };
147
- const depth = stackTraceDepth + 1;
148
- for (const key in substitution) {
149
- const value = substitution[key];
150
- if (key in dependencies) {
151
- const original = dependencies[key];
152
- if (isInjectable(value)) {
153
- dependencies[key] = value;
154
- }
155
- else if (isClassInjectable(original) || isFactoryInjectable(original)) {
156
- dependencies[key] = substitute(original, value, depth);
157
- }
158
- }
159
- }
160
- if (isClassInjectable(injectable)) {
161
- // @ts-expect-error
162
- return createExtendableClassInjectable(injectable, dependencies, injectable.scope, depth);
163
- }
164
- else if (isFactoryInjectable(injectable)) {
165
- // @ts-expect-error
166
- return createFactoryInjectable({ ...injectable, dependencies }, injectable.label, depth);
167
- }
168
- throw new Error('Invalid injectable type');
169
- }
170
- export function compareScope(left, operator, right) {
171
- const leftScope = ScopeStrictness[left];
172
- const rightScope = ScopeStrictness[right];
173
- switch (operator) {
174
- case '=':
175
- return leftScope === rightScope;
176
- case '!=':
177
- return leftScope !== rightScope;
178
- case '>':
179
- return leftScope > rightScope;
180
- case '<':
181
- return leftScope < rightScope;
182
- case '>=':
183
- return leftScope >= rightScope;
184
- case '<=':
185
- return leftScope <= rightScope;
186
- default:
187
- throw new Error('Invalid operator');
188
- }
189
- }
190
- const logger = createLazyInjectable(Scope.Global, 'Logger');
191
- const registry = createLazyInjectable(Scope.Global, 'Registry');
192
- const inject = createLazyInjectable(Scope.Global, 'Inject function');
193
- const dispose = createLazyInjectable(Scope.Global, 'Dispose function');
194
- const hook = createFactoryInjectable({
195
- scope: Scope.Transient,
196
- dependencies: { registry },
197
- factory: ({ registry }) => {
198
- const hooks = new Hooks();
199
- const on = (name, callback) => {
200
- hooks.add(name, callback);
201
- return registry.hooks.add(name, callback);
202
- };
203
- return { hooks, on };
204
- },
205
- pick: ({ on }) => on,
206
- dispose: ({ hooks }, { registry }) => {
207
- for (const [hook, callbacks] of hooks[kHookCollection].entries()) {
208
- for (const callback of callbacks) {
209
- registry.hooks.remove(hook, callback);
210
- }
211
- }
212
- hooks.clear();
213
- },
214
- });
215
- function resolveInjectableScope(isDefaultScope, injectable) {
216
- const actualScope = getInjectableScope(injectable);
217
- if (!isDefaultScope && compareScope(actualScope, '>', injectable.scope))
218
- throw new Error(`Invalid scope ${injectable.scope} for an injectable: dependencies have stricter scope - ${actualScope}`);
219
- return actualScope;
220
- }
221
- export const CoreInjectables = { logger, registry, inject, dispose, hook };
package/dist/logger.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import type { DestinationStream, Level, LoggerOptions, Logger as PinoLogger, StreamEntry } from 'pino';
2
- import { pino } from 'pino';
3
- export type { StreamEntry } from 'pino';
4
- export type Logger = PinoLogger;
5
- export type LoggingOptions = {
6
- destinations?: Array<DestinationStream | StreamEntry<Level>>;
7
- pinoOptions?: LoggerOptions;
8
- };
9
- export declare const createLogger: (options: LoggingOptions | undefined, $group: string) => pino.Logger<never, boolean>;
10
- export declare const createConsolePrettyDestination: (level: Level, sync?: boolean) => StreamEntry;
package/dist/logger.js DELETED
@@ -1,62 +0,0 @@
1
- import { threadId } from 'node:worker_threads';
2
- import { pino, stdTimeFunctions } from 'pino';
3
- import { build as pretty } from 'pino-pretty';
4
- // TODO: use node:util inspect
5
- const bg = (value, color) => `\x1b[${color}m${value}\x1b[0m`;
6
- const fg = (value, color) => `\x1b[38;5;${color}m${value}\x1b[0m`;
7
- const levelColors = {
8
- 10: 100,
9
- 20: 102,
10
- 30: 106,
11
- 40: 104,
12
- 50: 101,
13
- 60: 105,
14
- [Number.POSITIVE_INFINITY]: 0,
15
- };
16
- const messageColors = {
17
- 10: 0,
18
- 20: 2,
19
- 30: 6,
20
- 40: 4,
21
- 50: 1,
22
- 60: 5,
23
- [Number.POSITIVE_INFINITY]: 0,
24
- };
25
- const levelLabels = {
26
- 10: ' TRACE ',
27
- 20: ' DEBUG ',
28
- 30: ' INFO ',
29
- 40: ' WARN ',
30
- 50: ' ERROR ',
31
- 60: ' FATAL ',
32
- [Number.POSITIVE_INFINITY]: 'SILENT',
33
- };
34
- export const createLogger = (options = {}, $group) => {
35
- let { destinations, pinoOptions } = options;
36
- if (!destinations || !destinations?.length) {
37
- destinations = [createConsolePrettyDestination('info')];
38
- }
39
- const lowestLevelValue = destinations.reduce((acc, destination) => Math.min(acc, 'stream' in destination
40
- ? pino.levels.values[destination.level]
41
- : Number.POSITIVE_INFINITY), Number.POSITIVE_INFINITY);
42
- const level = pino.levels.labels[lowestLevelValue];
43
- return pino({ timestamp: stdTimeFunctions.isoTime, ...pinoOptions, level }, pino.multistream(destinations)).child({ $group, $threadId: threadId });
44
- };
45
- export const createConsolePrettyDestination = (level, sync = true) => ({
46
- level,
47
- stream: pretty({
48
- colorize: true,
49
- ignore: 'hostname,$group,$threadId',
50
- errorLikeObjectKeys: ['err', 'error', 'cause'],
51
- messageFormat: (log, messageKey) => {
52
- const group = fg(`[${log.$group}]`, 11);
53
- const msg = fg(log[messageKey], messageColors[log.level]);
54
- const thread = fg(`(Thread-${log.$threadId})`, 89);
55
- return `\x1b[0m${thread} ${group} ${msg}`;
56
- },
57
- customPrettifiers: {
58
- level: (level) => bg(levelLabels[level], levelColors[level]),
59
- },
60
- sync,
61
- }),
62
- });
@@ -1,13 +0,0 @@
1
- import { kMetadata } from './constants.ts';
2
- export type Metadata<T = any> = {
3
- key: MetadataKey<T>;
4
- value: T;
5
- };
6
- export type MetadataKey<T = any> = {
7
- [kMetadata]: string;
8
- as(value: T): Metadata<T>;
9
- };
10
- export declare const createMetadataKey: <T>(key: string) => MetadataKey<T>;
11
- export declare class MetadataStore extends Map<MetadataKey, Metadata> {
12
- get<T>(key: MetadataKey<T>): T | undefined;
13
- }
package/dist/metadata.js DELETED
@@ -1,15 +0,0 @@
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.d.ts DELETED
@@ -1,12 +0,0 @@
1
- import type { Async } from '@nmtjs/common';
2
- import type { PluginContext } from './types.ts';
3
- import { kPlugin } from './constants.ts';
4
- export interface BasePlugin<Type = any, Options = unknown, Context extends PluginContext = PluginContext> {
5
- name: string;
6
- init: (context: Context, options: Options) => Async<Type>;
7
- }
8
- export interface Plugin<Type = void, Options = unknown, Context extends PluginContext = PluginContext> extends BasePlugin<Type, Options, Context> {
9
- [kPlugin]: any;
10
- }
11
- export declare const createPlugin: <Options = unknown, Type = void>(name: string, init: Plugin<Type, Options>["init"]) => Plugin<Type, Options>;
12
- export declare const isPlugin: (value: any) => value is Plugin;
package/dist/plugin.js DELETED
@@ -1,3 +0,0 @@
1
- import { kPlugin } from "./constants.js";
2
- export const createPlugin = (name, init) => ({ name, init, [kPlugin]: true });
3
- export const isPlugin = (value) => kPlugin in value;
@@ -1,21 +0,0 @@
1
- import type { Hook } from './enums.ts';
2
- import type { HookType } from './hooks.ts';
3
- import type { AnyInjectable, Dependant } from './injectables.ts';
4
- import type { Logger } from './logger.ts';
5
- import { Scope } from './enums.ts';
6
- import { Hooks } from './hooks.ts';
7
- export declare class Registry {
8
- protected readonly application: {
9
- logger: Logger;
10
- };
11
- readonly hooks: Hooks;
12
- constructor(application: {
13
- logger: Logger;
14
- });
15
- registerHooks<T extends Hooks>(hooks: T): void;
16
- registerHook<T extends Hook>(name: T, callback: HookType[T]): void;
17
- getDependants(): Generator<Dependant>;
18
- clear(): void;
19
- }
20
- export declare const scopeErrorMessage: (name: any, scope?: Scope) => string;
21
- export declare function hasInvalidScopeDeps(injectables: AnyInjectable[], scope?: Scope): boolean;
package/dist/registry.js DELETED
@@ -1,24 +0,0 @@
1
- import { Scope } from "./enums.js";
2
- import { Hooks } from "./hooks.js";
3
- import { getInjectableScope } from "./injectables.js";
4
- export class Registry {
5
- application;
6
- hooks = new Hooks();
7
- constructor(application) {
8
- this.application = application;
9
- }
10
- registerHooks(hooks) {
11
- Hooks.merge(hooks, this.hooks);
12
- }
13
- registerHook(name, callback) {
14
- this.hooks.add(name, callback);
15
- }
16
- *getDependants() { }
17
- clear() {
18
- this.hooks.clear();
19
- }
20
- }
21
- export const scopeErrorMessage = (name, scope = Scope.Global) => `${name} must be a ${scope} scope (including all nested dependencies)`;
22
- export function hasInvalidScopeDeps(injectables, scope = Scope.Global) {
23
- return injectables.some((injectable) => getInjectableScope(injectable) !== scope);
24
- }
package/dist/types.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import type { Container } from './container.ts';
2
- import type { Hooks } from './hooks.ts';
3
- import type { Logger } from './logger.ts';
4
- import type { Registry } from './registry.ts';
5
- export interface PluginContext {
6
- logger: Logger;
7
- registry: Registry;
8
- hooks: Hooks;
9
- container: Container;
10
- }
11
- export type Pattern = RegExp | string | ((value: string) => boolean);
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -1,6 +0,0 @@
1
- import type { Pattern } from '../types.ts';
2
- /**
3
- * Very simple pattern matching function.
4
- */
5
- export declare function match(value: string, pattern: Pattern): boolean;
6
- export declare function isJsFile(name: string): boolean;
@@ -1,35 +0,0 @@
1
- /**
2
- * Very simple pattern matching function.
3
- */
4
- export function match(value, pattern) {
5
- if (typeof pattern === 'function') {
6
- return pattern(value);
7
- }
8
- else if (typeof pattern === 'string') {
9
- if (pattern === '*' || pattern === '**') {
10
- return true;
11
- }
12
- else if (pattern.at(0) === '*' && pattern.at(-1) === '*') {
13
- return value.includes(pattern.slice(1, -1));
14
- }
15
- else if (pattern.at(-1) === '*') {
16
- return value.startsWith(pattern.slice(0, -1));
17
- }
18
- else if (pattern.at(0) === '*') {
19
- return value.endsWith(pattern.slice(1));
20
- }
21
- else {
22
- return value === pattern;
23
- }
24
- }
25
- else {
26
- return pattern.test(value);
27
- }
28
- }
29
- export function isJsFile(name) {
30
- if (name.endsWith('.d.ts'))
31
- return false;
32
- const leading = name.split('.').slice(1);
33
- const ext = leading.join('.');
34
- return ['js', 'mjs', 'cjs', 'ts', 'mts', 'cts'].includes(ext);
35
- }
@@ -1,3 +0,0 @@
1
- export * from './functions.ts';
2
- export * from './pool.ts';
3
- export * from './semaphore.ts';
@@ -1,3 +0,0 @@
1
- export * from "./functions.js";
2
- export * from "./pool.js";
3
- export * from "./semaphore.js";
@@ -1,18 +0,0 @@
1
- interface PoolOptions {
2
- timeout?: number;
3
- }
4
- export declare class PoolError extends Error {
5
- }
6
- export declare class Pool<T = unknown> {
7
- #private;
8
- private readonly options;
9
- constructor(options?: PoolOptions);
10
- add(item: T): void;
11
- remove(item: T): void;
12
- capture(timeout?: number | undefined): Promise<T>;
13
- next(exclusive?: boolean, timeout?: number | undefined): Promise<T>;
14
- release(item: T): void;
15
- isFree(item: T): boolean;
16
- get items(): T[];
17
- }
18
- export {};