@nmtjs/core 0.6.4 → 0.7.0
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/dist/constants.js +9 -0
- package/dist/constants.js.map +1 -0
- package/dist/container.js +261 -0
- package/dist/container.js.map +1 -0
- package/dist/enums.js +20 -0
- package/dist/enums.js.map +1 -0
- package/dist/hooks.js +37 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.js +11 -11
- package/dist/index.js.map +1 -1
- package/dist/injectables.js +4 -0
- package/dist/injectables.js.map +1 -0
- package/dist/logger.js +68 -0
- package/dist/logger.js.map +1 -0
- package/dist/plugin.js +6 -0
- package/dist/plugin.js.map +1 -0
- package/dist/registry.js +23 -0
- package/dist/registry.js.map +1 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/functions.js +35 -0
- package/dist/utils/functions.js.map +1 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/pool.js +90 -0
- package/dist/utils/pool.js.map +1 -0
- package/dist/utils/semaphore.js +46 -0
- package/dist/utils/semaphore.js.map +1 -0
- package/package.json +10 -12
- package/{lib → src}/container.ts +4 -4
- package/{lib → src}/hooks.ts +1 -1
- package/src/index.ts +11 -0
- package/{lib → src}/plugin.ts +1 -1
- package/{lib → src}/utils/pool.ts +1 -1
- package/{lib → src}/utils/semaphore.ts +1 -1
- package/dist/lib/constants.js +0 -9
- package/dist/lib/constants.js.map +0 -1
- package/dist/lib/container.js +0 -279
- package/dist/lib/container.js.map +0 -1
- package/dist/lib/enums.js +0 -20
- package/dist/lib/enums.js.map +0 -1
- package/dist/lib/hooks.js +0 -37
- package/dist/lib/hooks.js.map +0 -1
- package/dist/lib/injectables.js +0 -6
- package/dist/lib/injectables.js.map +0 -1
- package/dist/lib/logger.js +0 -72
- package/dist/lib/logger.js.map +0 -1
- package/dist/lib/plugin.js +0 -6
- package/dist/lib/plugin.js.map +0 -1
- package/dist/lib/registry.js +0 -25
- package/dist/lib/registry.js.map +0 -1
- package/dist/lib/types.js +0 -1
- package/dist/lib/types.js.map +0 -1
- package/dist/lib/utils/functions.js +0 -32
- package/dist/lib/utils/functions.js.map +0 -1
- package/dist/lib/utils/index.js.map +0 -1
- package/dist/lib/utils/pool.js +0 -100
- package/dist/lib/utils/pool.js.map +0 -1
- package/dist/lib/utils/semaphore.js +0 -52
- package/dist/lib/utils/semaphore.js.map +0 -1
- package/index.ts +0 -11
- /package/dist/{lib/utils → utils}/index.js +0 -0
- /package/{lib → src}/constants.ts +0 -0
- /package/{lib → src}/enums.ts +0 -0
- /package/{lib → src}/injectables.ts +0 -0
- /package/{lib → src}/logger.ts +0 -0
- /package/{lib → src}/registry.ts +0 -0
- /package/{lib → src}/types.ts +0 -0
- /package/{lib → src}/utils/functions.ts +0 -0
- /package/{lib → src}/utils/index.ts +0 -0
package/dist/lib/hooks.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { kHookCollection } from "./constants.js";
|
|
2
|
-
export class Hooks {
|
|
3
|
-
static merge(from, to) {
|
|
4
|
-
for (const [name, callbacks] of from[kHookCollection]){
|
|
5
|
-
for (const callback of callbacks){
|
|
6
|
-
to.add(name, callback);
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
[kHookCollection] = new Map();
|
|
11
|
-
add(name, callback) {
|
|
12
|
-
let hooks = this[kHookCollection].get(name);
|
|
13
|
-
if (!hooks) this[kHookCollection].set(name, hooks = new Set());
|
|
14
|
-
hooks.add(callback);
|
|
15
|
-
return ()=>this.remove(name, callback);
|
|
16
|
-
}
|
|
17
|
-
remove(name, callback) {
|
|
18
|
-
const hooks = this[kHookCollection].get(name);
|
|
19
|
-
if (hooks) hooks.delete(callback);
|
|
20
|
-
}
|
|
21
|
-
async call(name, options, ...args) {
|
|
22
|
-
const { concurrent = true, reverse = false } = options ?? {};
|
|
23
|
-
const hooks = this[kHookCollection].get(name);
|
|
24
|
-
if (!hooks) return;
|
|
25
|
-
const hooksArr = Array.from(hooks);
|
|
26
|
-
if (concurrent) {
|
|
27
|
-
await Promise.all(hooksArr.map((hook)=>hook(...args)));
|
|
28
|
-
} else {
|
|
29
|
-
if (reverse) hooksArr.reverse();
|
|
30
|
-
for (const hook of hooksArr)await hook(...args);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
clear() {
|
|
34
|
-
this[kHookCollection].clear();
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
export const createErrForHook = (hook)=>`Error during [${hook}] hook`;
|
package/dist/lib/hooks.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/hooks.ts"],"sourcesContent":["import type { Callback } from '@nmtjs/common'\nimport { kHookCollection } from './constants.ts'\nimport type { Hook } from './enums.ts'\n// import type { HookType } from './types.ts'\n\nexport interface HookType {\n [key: string]: (...args: any[]) => any\n // [Hook.AfterInitialize]: () => any\n // [Hook.BeforeStart]: () => any\n // [Hook.AfterStart]: () => any\n // [Hook.BeforeStop]: () => any\n // [Hook.AfterStop]: () => any\n // [Hook.BeforeTerminate]: () => any\n // [Hook.AfterTerminate]: () => any\n // [Hook.OnConnect]: (...args: any[]) => any\n // [Hook.OnDisconnect]: (...args: any[]) => any\n}\n\nexport type CallHook<T extends string> = (\n hook: T,\n ...args: T extends keyof HookType ? Parameters<HookType[T]> : any[]\n) => Promise<void>\n\nexport class Hooks {\n static merge(from: Hooks, to: Hooks) {\n for (const [name, callbacks] of from[kHookCollection]) {\n for (const callback of callbacks) {\n to.add(name, callback)\n }\n }\n }\n\n [kHookCollection] = new Map<string, Set<Callback>>()\n\n add(name: string, callback: Callback) {\n let hooks = this[kHookCollection].get(name)\n if (!hooks) this[kHookCollection].set(name, (hooks = new Set()))\n hooks.add(callback)\n return () => this.remove(name, callback)\n }\n\n remove(name: string, callback: Callback) {\n const hooks = this[kHookCollection].get(name)\n if (hooks) hooks.delete(callback)\n }\n\n async call<T extends string | Hook>(\n name: T,\n options: { concurrent?: boolean; reverse?: boolean } | undefined,\n ...args: T extends Hook ? Parameters<HookType[T]> : any[]\n ) {\n const { concurrent = true, reverse = false } = options ?? {}\n const hooks = this[kHookCollection].get(name)\n if (!hooks) return\n const hooksArr = Array.from(hooks)\n if (concurrent) {\n await Promise.all(hooksArr.map((hook) => hook(...args)))\n } else {\n if (reverse) hooksArr.reverse()\n for (const hook of hooksArr) await hook(...args)\n }\n }\n\n clear() {\n this[kHookCollection].clear()\n }\n}\n\nexport const createErrForHook = (hook: Hook | (object & string)) =>\n `Error during [${hook}] hook`\n"],"names":["kHookCollection","Hooks","merge","from","to","name","callbacks","callback","add","Map","hooks","get","set","Set","remove","delete","call","options","args","concurrent","reverse","hooksArr","Array","Promise","all","map","hook","clear","createErrForHook"],"mappings":"AACA,SAASA,eAAe,QAAQ,iBAAgB;AAsBhD,OAAO,MAAMC;IACX,OAAOC,MAAMC,IAAW,EAAEC,EAAS,EAAE;QACnC,KAAK,MAAM,CAACC,MAAMC,UAAU,IAAIH,IAAI,CAACH,gBAAgB,CAAE;YACrD,KAAK,MAAMO,YAAYD,UAAW;gBAChCF,GAAGI,GAAG,CAACH,MAAME;YACf;QACF;IACF;IAEA,CAACP,gBAAgB,GAAG,IAAIS,MAA4B;IAEpDD,IAAIH,IAAY,EAAEE,QAAkB,EAAE;QACpC,IAAIG,QAAQ,IAAI,CAACV,gBAAgB,CAACW,GAAG,CAACN;QACtC,IAAI,CAACK,OAAO,IAAI,CAACV,gBAAgB,CAACY,GAAG,CAACP,MAAOK,QAAQ,IAAIG;QACzDH,MAAMF,GAAG,CAACD;QACV,OAAO,IAAM,IAAI,CAACO,MAAM,CAACT,MAAME;IACjC;IAEAO,OAAOT,IAAY,EAAEE,QAAkB,EAAE;QACvC,MAAMG,QAAQ,IAAI,CAACV,gBAAgB,CAACW,GAAG,CAACN;QACxC,IAAIK,OAAOA,MAAMK,MAAM,CAACR;IAC1B;IAEA,MAAMS,KACJX,IAAO,EACPY,OAAgE,EAChE,GAAGC,IAAsD,EACzD;QACA,MAAM,EAAEC,aAAa,IAAI,EAAEC,UAAU,KAAK,EAAE,GAAGH,WAAW,CAAC;QAC3D,MAAMP,QAAQ,IAAI,CAACV,gBAAgB,CAACW,GAAG,CAACN;QACxC,IAAI,CAACK,OAAO;QACZ,MAAMW,WAAWC,MAAMnB,IAAI,CAACO;QAC5B,IAAIS,YAAY;YACd,MAAMI,QAAQC,GAAG,CAACH,SAASI,GAAG,CAAC,CAACC,OAASA,QAAQR;QACnD,OAAO;YACL,IAAIE,SAASC,SAASD,OAAO;YAC7B,KAAK,MAAMM,QAAQL,SAAU,MAAMK,QAAQR;QAC7C;IACF;IAEAS,QAAQ;QACN,IAAI,CAAC3B,gBAAgB,CAAC2B,KAAK;IAC7B;AACF;AAEA,OAAO,MAAMC,mBAAmB,CAACF,OAC/B,CAAC,cAAc,EAAEA,KAAK,MAAM,CAAC,CAAA"}
|
package/dist/lib/injectables.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/injectables.ts"],"sourcesContent":["import { createLazyInjectable } from './container.ts'\nimport { Scope } from './enums.ts'\nimport type { Logger } from './logger.ts'\n\nconst logger = createLazyInjectable<Logger>(Scope.Global, 'Logger')\n\nexport const CoreInjectables = { logger }\n"],"names":["createLazyInjectable","Scope","logger","Global","CoreInjectables"],"mappings":"AAAA,SAASA,oBAAoB,QAAQ,iBAAgB;AACrD,SAASC,KAAK,QAAQ,aAAY;AAGlC,MAAMC,SAASF,qBAA6BC,MAAME,MAAM,EAAE;AAE1D,OAAO,MAAMC,kBAAkB;IAAEF;AAAO,EAAC"}
|
package/dist/lib/logger.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { threadId } from 'node:worker_threads';
|
|
2
|
-
import { pino, stdTimeFunctions } from 'pino';
|
|
3
|
-
import { build as pretty } from 'pino-pretty';
|
|
4
|
-
const bg = (value, color)=>`\x1b[${color}m${value}\x1b[0m`;
|
|
5
|
-
const fg = (value, color)=>`\x1b[38;5;${color}m${value}\x1b[0m`;
|
|
6
|
-
const levelColors = {
|
|
7
|
-
10: 100,
|
|
8
|
-
20: 102,
|
|
9
|
-
30: 106,
|
|
10
|
-
40: 104,
|
|
11
|
-
50: 101,
|
|
12
|
-
60: 105,
|
|
13
|
-
[Number.POSITIVE_INFINITY]: 0
|
|
14
|
-
};
|
|
15
|
-
const messageColors = {
|
|
16
|
-
10: 0,
|
|
17
|
-
20: 2,
|
|
18
|
-
30: 6,
|
|
19
|
-
40: 4,
|
|
20
|
-
50: 1,
|
|
21
|
-
60: 5,
|
|
22
|
-
[Number.POSITIVE_INFINITY]: 0
|
|
23
|
-
};
|
|
24
|
-
const levelLabels = {
|
|
25
|
-
10: ' TRACE ',
|
|
26
|
-
20: ' DEBUG ',
|
|
27
|
-
30: ' INFO ',
|
|
28
|
-
40: ' WARN ',
|
|
29
|
-
50: ' ERROR ',
|
|
30
|
-
60: ' FATAL ',
|
|
31
|
-
[Number.POSITIVE_INFINITY]: 'SILENT'
|
|
32
|
-
};
|
|
33
|
-
export const createLogger = (options = {}, $group)=>{
|
|
34
|
-
let { destinations, pinoOptions } = options;
|
|
35
|
-
if (!destinations || !destinations?.length) {
|
|
36
|
-
destinations = [
|
|
37
|
-
createConsolePrettyDestination('info')
|
|
38
|
-
];
|
|
39
|
-
}
|
|
40
|
-
const lowestLevelValue = destinations.reduce((acc, destination)=>Math.min(acc, 'stream' in destination ? pino.levels.values[destination.level] : Number.POSITIVE_INFINITY), Number.POSITIVE_INFINITY);
|
|
41
|
-
const level = pino.levels.labels[lowestLevelValue];
|
|
42
|
-
return pino({
|
|
43
|
-
timestamp: stdTimeFunctions.isoTime,
|
|
44
|
-
...pinoOptions,
|
|
45
|
-
level
|
|
46
|
-
}, pino.multistream(destinations)).child({
|
|
47
|
-
$group,
|
|
48
|
-
$threadId: threadId
|
|
49
|
-
});
|
|
50
|
-
};
|
|
51
|
-
export const createConsolePrettyDestination = (level, sync = true)=>({
|
|
52
|
-
level,
|
|
53
|
-
stream: pretty({
|
|
54
|
-
colorize: true,
|
|
55
|
-
ignore: 'hostname,$group,$threadId',
|
|
56
|
-
errorLikeObjectKeys: [
|
|
57
|
-
'err',
|
|
58
|
-
'error',
|
|
59
|
-
'cause'
|
|
60
|
-
],
|
|
61
|
-
messageFormat: (log, messageKey)=>{
|
|
62
|
-
const group = fg(`[${log.$group}]`, 11);
|
|
63
|
-
const msg = fg(log[messageKey], messageColors[log.level]);
|
|
64
|
-
const thread = fg(`(Thread-${log.$threadId})`, 89);
|
|
65
|
-
return `\x1b[0m${thread} ${group} ${msg}`;
|
|
66
|
-
},
|
|
67
|
-
customPrettifiers: {
|
|
68
|
-
level: (level)=>bg(levelLabels[level], levelColors[level])
|
|
69
|
-
},
|
|
70
|
-
sync
|
|
71
|
-
})
|
|
72
|
-
});
|
package/dist/lib/logger.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/logger.ts"],"sourcesContent":["import { threadId } from 'node:worker_threads'\nimport {\n type DestinationStream,\n type Level,\n type Logger as PinoLogger,\n pino,\n type StreamEntry,\n stdTimeFunctions,\n} from 'pino'\nimport { build as pretty } from 'pino-pretty'\n\nexport type Logger = PinoLogger\nexport type LoggingOptions = {\n destinations?: Array<DestinationStream | StreamEntry<Level>>\n pinoOptions?: any\n}\n\n// TODO: use node:util inspect\nconst bg = (value, color) => `\\x1b[${color}m${value}\\x1b[0m`\nconst fg = (value, color) => `\\x1b[38;5;${color}m${value}\\x1b[0m`\n\nconst levelColors = {\n 10: 100,\n 20: 102,\n 30: 106,\n 40: 104,\n 50: 101,\n 60: 105,\n [Number.POSITIVE_INFINITY]: 0,\n}\nconst messageColors = {\n 10: 0,\n 20: 2,\n 30: 6,\n 40: 4,\n 50: 1,\n 60: 5,\n [Number.POSITIVE_INFINITY]: 0,\n}\n\nconst levelLabels = {\n 10: ' TRACE ',\n 20: ' DEBUG ',\n 30: ' INFO ',\n 40: ' WARN ',\n 50: ' ERROR ',\n 60: ' FATAL ',\n [Number.POSITIVE_INFINITY]: 'SILENT',\n}\n\nexport const createLogger = (options: LoggingOptions = {}, $group: string) => {\n let { destinations, pinoOptions } = options\n\n if (!destinations || !destinations?.length) {\n destinations = [createConsolePrettyDestination('info')]\n }\n\n const lowestLevelValue = destinations!.reduce(\n (acc, destination) =>\n Math.min(\n acc,\n 'stream' in destination\n ? pino.levels.values[destination.level!]\n : Number.POSITIVE_INFINITY,\n ),\n Number.POSITIVE_INFINITY,\n )\n const level = pino.levels.labels[lowestLevelValue]\n return pino(\n {\n timestamp: stdTimeFunctions.isoTime,\n ...pinoOptions,\n level,\n },\n pino.multistream(destinations!),\n ).child({ $group, $threadId: threadId })\n}\n\nexport const createConsolePrettyDestination = (\n level: Level,\n sync = true,\n): StreamEntry => ({\n level,\n stream: pretty({\n colorize: true,\n ignore: 'hostname,$group,$threadId',\n errorLikeObjectKeys: ['err', 'error', 'cause'],\n messageFormat: (log, messageKey) => {\n const group = fg(`[${log.$group}]`, 11)\n const msg = fg(log[messageKey], messageColors[log.level as number])\n const thread = fg(`(Thread-${log.$threadId})`, 89)\n return `\\x1b[0m${thread} ${group} ${msg}`\n },\n customPrettifiers: {\n level: (level: any) => bg(levelLabels[level], levelColors[level]),\n },\n sync,\n }),\n})\n"],"names":["threadId","pino","stdTimeFunctions","build","pretty","bg","value","color","fg","levelColors","Number","POSITIVE_INFINITY","messageColors","levelLabels","createLogger","options","$group","destinations","pinoOptions","length","createConsolePrettyDestination","lowestLevelValue","reduce","acc","destination","Math","min","levels","values","level","labels","timestamp","isoTime","multistream","child","$threadId","sync","stream","colorize","ignore","errorLikeObjectKeys","messageFormat","log","messageKey","group","msg","thread","customPrettifiers"],"mappings":"AAAA,SAASA,QAAQ,QAAQ,sBAAqB;AAC9C,SAIEC,IAAI,EAEJC,gBAAgB,QACX,OAAM;AACb,SAASC,SAASC,MAAM,QAAQ,cAAa;AAS7C,MAAMC,KAAK,C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AAO,MAAMoB,iCAAiC,CAC5CS,OACAO,OAAO,IAAI,GACM,CAAA;QACjBP;QACAQ,QAAQjC,OAAO;YACbkC,UAAU;YACVC,QAAQ;YACRC,qBAAqB;gBAAC;gBAAO;gBAAS;aAAQ;YAC9CC,eAAe,CAACC,KAAKC;gBACnB,MAAMC,QAAQpC,GAAG,CAAC,CAAC,EAAEkC,IAAI1B,MAAM,CAAC,CAAC,CAAC,EAAE;gBACpC,MAAM6B,MAAMrC,GAAGkC,GAAG,CAACC,WAAW,EAAE/B,aAAa,CAAC8B,IAAIb,KAAK,CAAW;gBAClE,MAAMiB,SAAStC,GAAG,CAAC,QAAQ,EAAEkC,IAAIP,SAAS,CAAC,CAAC,CAAC,EAAE;gBAC/C,OAAO,CAAC,OAAO,EAAEW,OAAO,CAAC,EAAEF,MAAM,CAAC,EAAEC,IAAI,CAAC;YAC3C;YACAE,mBAAmB;gBACjBlB,OAAO,CAACA,QAAexB,GAAGQ,WAAW,CAACgB,MAAM,EAAEpB,WAAW,CAACoB,MAAM;YAClE;YACAO;QACF;IACF,CAAA,EAAE"}
|
package/dist/lib/plugin.js
DELETED
package/dist/lib/plugin.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/plugin.ts"],"sourcesContent":["import type { Async } from '@nmtjs/common'\nimport { kPlugin } from './constants.ts'\nimport type { PluginContext } from './types.ts'\n\nexport interface BasePlugin<\n Type = any,\n Options = unknown,\n Context extends PluginContext = PluginContext,\n> {\n name: string\n init: (context: Context, options: Options) => Async<Type>\n}\n\nexport interface Plugin<\n Type = void,\n Options = unknown,\n Context extends PluginContext = PluginContext,\n> extends BasePlugin<Type, Options, Context> {\n [kPlugin]: any\n}\n\nexport const createPlugin = <Options = unknown, Type = void>(\n name: string,\n init: Plugin<Type, Options>['init'],\n): Plugin<Type, Options> => ({ name, init, [kPlugin]: true })\n"],"names":["kPlugin","createPlugin","name","init"],"mappings":"AACA,SAASA,OAAO,QAAQ,iBAAgB;AAoBxC,OAAO,MAAMC,eAAe,CAC1BC,MACAC,OAC2B,CAAA;QAAED;QAAMC;QAAM,CAACH,QAAQ,EAAE;IAAK,CAAA,EAAE"}
|
package/dist/lib/registry.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { getInjectableScope } from "./container.js";
|
|
2
|
-
import { Scope } from "./enums.js";
|
|
3
|
-
import { Hooks } from "./hooks.js";
|
|
4
|
-
export class Registry {
|
|
5
|
-
application;
|
|
6
|
-
hooks;
|
|
7
|
-
constructor(application){
|
|
8
|
-
this.application = application;
|
|
9
|
-
this.hooks = new Hooks();
|
|
10
|
-
}
|
|
11
|
-
registerHooks(hooks) {
|
|
12
|
-
Hooks.merge(hooks, this.hooks);
|
|
13
|
-
}
|
|
14
|
-
registerHook(name, callback) {
|
|
15
|
-
this.hooks.add(name, callback);
|
|
16
|
-
}
|
|
17
|
-
*getDependants() {}
|
|
18
|
-
clear() {
|
|
19
|
-
this.hooks.clear();
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
export const scopeErrorMessage = (name, scope = Scope.Global)=>`${name} must be a ${scope} scope (including all nested dependencies)`;
|
|
23
|
-
export function hasInvalidScopeDeps(injectables, scope = Scope.Global) {
|
|
24
|
-
return injectables.some((injectable)=>getInjectableScope(injectable) !== scope);
|
|
25
|
-
}
|
package/dist/lib/registry.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/registry.ts"],"sourcesContent":["import {\n type AnyInjectable,\n type Dependant,\n getInjectableScope,\n} from './container.ts'\nimport { type Hook, Scope } from './enums.ts'\nimport { Hooks, type HookType } from './hooks.ts'\nimport type { Logger } from './logger.ts'\n\nexport class Registry {\n readonly hooks = new Hooks()\n\n constructor(\n protected readonly application: {\n logger: Logger\n },\n ) {}\n\n registerHooks<T extends Hooks>(hooks: T) {\n Hooks.merge(hooks, this.hooks)\n }\n\n registerHook<T extends Hook>(name: T, callback: HookType[T]) {\n this.hooks.add(name, callback)\n }\n\n *getDependants(): Generator<Dependant> {}\n\n clear() {\n this.hooks.clear()\n }\n}\n\nexport const scopeErrorMessage = (name, scope = Scope.Global) =>\n `${name} must be a ${scope} scope (including all nested dependencies)`\n\nexport function hasInvalidScopeDeps(\n injectables: AnyInjectable[],\n scope = Scope.Global,\n) {\n return injectables.some(\n (injectable) => getInjectableScope(injectable) !== scope,\n )\n}\n"],"names":["getInjectableScope","Scope","Hooks","Registry","hooks","constructor","application","registerHooks","merge","registerHook","name","callback","add","getDependants","clear","scopeErrorMessage","scope","Global","hasInvalidScopeDeps","injectables","some","injectable"],"mappings":"AAAA,SAGEA,kBAAkB,QACb,iBAAgB;AACvB,SAAoBC,KAAK,QAAQ,aAAY;AAC7C,SAASC,KAAK,QAAuB,aAAY;AAGjD,OAAO,MAAMC;;IACFC,MAAmB;IAE5BC,YACE,AAAmBC,WAElB,CACD;aAHmBA,cAAAA;aAHZF,QAAQ,IAAIF;IAMlB;IAEHK,cAA+BH,KAAQ,EAAE;QACvCF,MAAMM,KAAK,CAACJ,OAAO,IAAI,CAACA,KAAK;IAC/B;IAEAK,aAA6BC,IAAO,EAAEC,QAAqB,EAAE;QAC3D,IAAI,CAACP,KAAK,CAACQ,GAAG,CAACF,MAAMC;IACvB;IAEA,CAACE,gBAAsC,CAAC;IAExCC,QAAQ;QACN,IAAI,CAACV,KAAK,CAACU,KAAK;IAClB;AACF;AAEA,OAAO,MAAMC,oBAAoB,CAACL,MAAMM,QAAQf,MAAMgB,MAAM,GAC1D,CAAC,EAAEP,KAAK,WAAW,EAAEM,MAAM,0CAA0C,CAAC,CAAA;AAExE,OAAO,SAASE,oBACdC,WAA4B,EAC5BH,QAAQf,MAAMgB,MAAM;IAEpB,OAAOE,YAAYC,IAAI,CACrB,CAACC,aAAerB,mBAAmBqB,gBAAgBL;AAEvD"}
|
package/dist/lib/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
package/dist/lib/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../lib/types.ts"],"sourcesContent":["import type { Container } from './container.ts'\nimport type { Hooks } from './hooks.ts'\nimport type { Logger } from './logger.ts'\nimport type { Registry } from './registry.ts'\n\nexport interface PluginContext {\n logger: Logger\n registry: Registry\n hooks: Hooks\n container: Container\n}\n\nexport type Pattern = RegExp | string | ((value: string) => boolean)\n"],"names":[],"mappings":"AAYA,WAAoE"}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
export function match(value, pattern) {
|
|
2
|
-
if (typeof pattern === 'function') {
|
|
3
|
-
return pattern(value);
|
|
4
|
-
} else if (typeof pattern === 'string') {
|
|
5
|
-
if (pattern === '*' || pattern === '**') {
|
|
6
|
-
return true;
|
|
7
|
-
} else if (pattern.at(0) === '*' && pattern.at(-1) === '*') {
|
|
8
|
-
return value.includes(pattern.slice(1, -1));
|
|
9
|
-
} else if (pattern.at(-1) === '*') {
|
|
10
|
-
return value.startsWith(pattern.slice(0, -1));
|
|
11
|
-
} else if (pattern.at(0) === '*') {
|
|
12
|
-
return value.endsWith(pattern.slice(1));
|
|
13
|
-
} else {
|
|
14
|
-
return value === pattern;
|
|
15
|
-
}
|
|
16
|
-
} else {
|
|
17
|
-
return pattern.test(value);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
export function isJsFile(name) {
|
|
21
|
-
if (name.endsWith('.d.ts')) return false;
|
|
22
|
-
const leading = name.split('.').slice(1);
|
|
23
|
-
const ext = leading.join('.');
|
|
24
|
-
return [
|
|
25
|
-
'js',
|
|
26
|
-
'mjs',
|
|
27
|
-
'cjs',
|
|
28
|
-
'ts',
|
|
29
|
-
'mts',
|
|
30
|
-
'cts'
|
|
31
|
-
].includes(ext);
|
|
32
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../lib/utils/functions.ts"],"sourcesContent":["import type { Pattern } from '../types.ts'\n\n/**\n * Very simple pattern matching function.\n */\nexport function match(value: string, pattern: Pattern) {\n if (typeof pattern === 'function') {\n return pattern(value)\n } else if (typeof pattern === 'string') {\n if (pattern === '*' || pattern === '**') {\n return true\n } else if (pattern.at(0) === '*' && pattern.at(-1) === '*') {\n return value.includes(pattern.slice(1, -1))\n } else if (pattern.at(-1) === '*') {\n return value.startsWith(pattern.slice(0, -1))\n } else if (pattern.at(0) === '*') {\n return value.endsWith(pattern.slice(1))\n } else {\n return value === pattern\n }\n } else {\n return pattern.test(value)\n }\n}\n\nexport function isJsFile(name: string) {\n if (name.endsWith('.d.ts')) return false\n const leading = name.split('.').slice(1)\n const ext = leading.join('.')\n return ['js', 'mjs', 'cjs', 'ts', 'mts', 'cts'].includes(ext)\n}\n"],"names":["match","value","pattern","at","includes","slice","startsWith","endsWith","test","isJsFile","name","leading","split","ext","join"],"mappings":"AAKA,OAAO,SAASA,MAAMC,KAAa,EAAEC,OAAgB;IACnD,IAAI,OAAOA,YAAY,YAAY;QACjC,OAAOA,QAAQD;IACjB,OAAO,IAAI,OAAOC,YAAY,UAAU;QACtC,IAAIA,YAAY,OAAOA,YAAY,MAAM;YACvC,OAAO;QACT,OAAO,IAAIA,QAAQC,EAAE,CAAC,OAAO,OAAOD,QAAQC,EAAE,CAAC,CAAC,OAAO,KAAK;YAC1D,OAAOF,MAAMG,QAAQ,CAACF,QAAQG,KAAK,CAAC,GAAG,CAAC;QAC1C,OAAO,IAAIH,QAAQC,EAAE,CAAC,CAAC,OAAO,KAAK;YACjC,OAAOF,MAAMK,UAAU,CAACJ,QAAQG,KAAK,CAAC,GAAG,CAAC;QAC5C,OAAO,IAAIH,QAAQC,EAAE,CAAC,OAAO,KAAK;YAChC,OAAOF,MAAMM,QAAQ,CAACL,QAAQG,KAAK,CAAC;QACtC,OAAO;YACL,OAAOJ,UAAUC;QACnB;IACF,OAAO;QACL,OAAOA,QAAQM,IAAI,CAACP;IACtB;AACF;AAEA,OAAO,SAASQ,SAASC,IAAY;IACnC,IAAIA,KAAKH,QAAQ,CAAC,UAAU,OAAO;IACnC,MAAMI,UAAUD,KAAKE,KAAK,CAAC,KAAKP,KAAK,CAAC;IACtC,MAAMQ,MAAMF,QAAQG,IAAI,CAAC;IACzB,OAAO;QAAC;QAAM;QAAO;QAAO;QAAM;QAAO;KAAM,CAACV,QAAQ,CAACS;AAC3D"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../lib/utils/index.ts"],"sourcesContent":["export * from './functions.ts'\nexport * from './pool.ts'\nexport * from './semaphore.ts'\n"],"names":[],"mappings":"AAAA,cAAc,iBAAgB;AAC9B,cAAc,YAAW;AACzB,cAAc,iBAAgB"}
|
package/dist/lib/utils/pool.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
export class PoolError extends Error {
|
|
2
|
-
}
|
|
3
|
-
export class Pool {
|
|
4
|
-
options;
|
|
5
|
-
#items;
|
|
6
|
-
#free;
|
|
7
|
-
#queue;
|
|
8
|
-
#current;
|
|
9
|
-
#size;
|
|
10
|
-
#available;
|
|
11
|
-
constructor(options = {}){
|
|
12
|
-
this.options = options;
|
|
13
|
-
this.#items = [];
|
|
14
|
-
this.#free = [];
|
|
15
|
-
this.#queue = [];
|
|
16
|
-
this.#current = 0;
|
|
17
|
-
this.#size = 0;
|
|
18
|
-
this.#available = 0;
|
|
19
|
-
}
|
|
20
|
-
add(item) {
|
|
21
|
-
if (this.#items.includes(item)) throw new PoolError('Item already exists');
|
|
22
|
-
this.#size++;
|
|
23
|
-
this.#available++;
|
|
24
|
-
this.#items.push(item);
|
|
25
|
-
this.#free.push(true);
|
|
26
|
-
}
|
|
27
|
-
remove(item) {
|
|
28
|
-
if (this.#size === 0) throw new PoolError('Pool is empty');
|
|
29
|
-
const index = this.#items.indexOf(item);
|
|
30
|
-
if (index < 0) throw new PoolError('Item is not in the pool');
|
|
31
|
-
const isCaptured = this.isFree(item);
|
|
32
|
-
if (isCaptured) this.#available--;
|
|
33
|
-
this.#size--;
|
|
34
|
-
this.#current--;
|
|
35
|
-
this.#items.splice(index, 1);
|
|
36
|
-
this.#free.splice(index, 1);
|
|
37
|
-
}
|
|
38
|
-
capture(timeout = this.options.timeout) {
|
|
39
|
-
return this.next(true, timeout);
|
|
40
|
-
}
|
|
41
|
-
async next(exclusive = false, timeout = this.options.timeout) {
|
|
42
|
-
if (this.#size === 0) 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) this.#capture(item);
|
|
48
|
-
resolve(item);
|
|
49
|
-
},
|
|
50
|
-
timer: undefined
|
|
51
|
-
};
|
|
52
|
-
if (timeout) {
|
|
53
|
-
waiting.timer = setTimeout(()=>{
|
|
54
|
-
waiting.resolve = undefined;
|
|
55
|
-
this.#queue.shift();
|
|
56
|
-
reject(new PoolError('Next item timeout'));
|
|
57
|
-
}, timeout);
|
|
58
|
-
}
|
|
59
|
-
this.#queue.push(waiting);
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
let item = undefined;
|
|
63
|
-
let free = false;
|
|
64
|
-
do {
|
|
65
|
-
item = this.#items[this.#current];
|
|
66
|
-
free = this.#free[this.#current];
|
|
67
|
-
this.#current++;
|
|
68
|
-
if (this.#current >= this.#size) this.#current = 0;
|
|
69
|
-
}while (typeof item === 'undefined' || !free)
|
|
70
|
-
if (exclusive) this.#capture(item);
|
|
71
|
-
return item;
|
|
72
|
-
}
|
|
73
|
-
release(item) {
|
|
74
|
-
const index = this.#items.indexOf(item);
|
|
75
|
-
if (index < 0) throw new PoolError('Unexpected item');
|
|
76
|
-
if (this.#free[index]) throw new PoolError('Unable to release not captured item');
|
|
77
|
-
this.#free[index] = true;
|
|
78
|
-
this.#available++;
|
|
79
|
-
if (this.#queue.length > 0) {
|
|
80
|
-
const { resolve, timer } = this.#queue.shift();
|
|
81
|
-
clearTimeout(timer);
|
|
82
|
-
if (resolve) setTimeout(resolve, 0, item);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
isFree(item) {
|
|
86
|
-
const index = this.#items.indexOf(item);
|
|
87
|
-
if (index < 0) return false;
|
|
88
|
-
return this.#free[index];
|
|
89
|
-
}
|
|
90
|
-
get items() {
|
|
91
|
-
return [
|
|
92
|
-
...this.#items
|
|
93
|
-
];
|
|
94
|
-
}
|
|
95
|
-
#capture(item) {
|
|
96
|
-
const index = this.#items.indexOf(item);
|
|
97
|
-
this.#free[index] = false;
|
|
98
|
-
this.#available--;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../lib/utils/pool.ts"],"sourcesContent":["import type { Callback } from '@nmtjs/common'\n\ninterface PoolOptions {\n timeout?: number\n}\n\ninterface PoolQueueItem {\n resolve?: Callback\n timer?: ReturnType<typeof setTimeout>\n}\n\nexport class PoolError extends Error {}\n\n// Fixed pool from https://github.com/metarhia/metautil\nexport class Pool<T = unknown> {\n #items: Array<T> = []\n #free: Array<boolean> = []\n #queue: PoolQueueItem[] = []\n #current: number = 0\n #size: number = 0\n #available: number = 0\n\n constructor(private readonly options: PoolOptions = {}) {}\n\n add(item: T) {\n if (this.#items.includes(item)) throw new PoolError('Item already exists')\n this.#size++\n this.#available++\n this.#items.push(item)\n this.#free.push(true)\n }\n\n remove(item: T) {\n if (this.#size === 0) throw new PoolError('Pool is empty')\n const index = this.#items.indexOf(item)\n if (index < 0) throw new PoolError('Item is not in the pool')\n const isCaptured = this.isFree(item)\n if (isCaptured) this.#available--\n this.#size--\n this.#current--\n this.#items.splice(index, 1)\n this.#free.splice(index, 1)\n }\n\n capture(timeout = this.options.timeout) {\n return this.next(true, timeout)\n }\n\n async next(exclusive = false, timeout = this.options.timeout): Promise<T> {\n if (this.#size === 0) throw new PoolError('Pool is empty')\n if (this.#available === 0) {\n return new Promise((resolve, reject) => {\n const waiting: PoolQueueItem = {\n resolve: (item: T) => {\n if (exclusive) this.#capture(item)\n resolve(item)\n },\n timer: undefined,\n }\n if (timeout) {\n waiting.timer = setTimeout(() => {\n waiting.resolve = undefined\n this.#queue.shift()\n reject(new PoolError('Next item timeout'))\n }, timeout)\n }\n this.#queue.push(waiting)\n })\n }\n let item: T | undefined = undefined\n let free = false\n do {\n item = this.#items[this.#current]\n free = this.#free[this.#current]\n this.#current++\n if (this.#current >= this.#size) this.#current = 0\n } while (typeof item === 'undefined' || !free)\n if (exclusive) this.#capture(item)\n return item\n }\n\n release(item: T) {\n const index = this.#items.indexOf(item)\n if (index < 0) throw new PoolError('Unexpected item')\n if (this.#free[index])\n throw new PoolError('Unable to release not captured item')\n this.#free[index] = true\n this.#available++\n if (this.#queue.length > 0) {\n const { resolve, timer } = this.#queue.shift()!\n clearTimeout(timer)\n if (resolve) setTimeout(resolve, 0, item)\n }\n }\n\n isFree(item: T) {\n const index = this.#items.indexOf(item)\n if (index < 0) return false\n return this.#free[index]\n }\n\n get items() {\n return [...this.#items]\n }\n\n #capture(item: T) {\n const index = this.#items.indexOf(item)\n this.#free[index] = false\n this.#available--\n }\n}\n"],"names":["PoolError","Error","Pool","constructor","options","add","item","includes","push","remove","index","indexOf","isCaptured","isFree","splice","capture","timeout","next","exclusive","Promise","resolve","reject","waiting","timer","undefined","setTimeout","shift","free","release","length","clearTimeout","items"],"mappings":"AAWA,OAAO,MAAMA,kBAAkBC;AAAO;AAGtC,OAAO,MAAMC;;IACX,CAAA,KAAM,CAAe;IACrB,CAAA,IAAK,CAAqB;IAC1B,CAAA,KAAM,CAAsB;IAC5B,CAAA,OAAQ,CAAY;IACpB,CAAA,IAAK,CAAY;IACjB,CAAA,SAAU,CAAY;IAEtBC,YAAY,AAAiBC,UAAuB,CAAC,CAAC,CAAE;aAA3BA,UAAAA;aAP7B,CAAA,KAAM,GAAa,EAAE;aACrB,CAAA,IAAK,GAAmB,EAAE;aAC1B,CAAA,KAAM,GAAoB,EAAE;aAC5B,CAAA,O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GAAGd;QACtC;IACF;IAEAO,OAAOP,IAAO,EAAE;QACd,MAAMI,QAAQ,IAAI,CAAC,CAAA,KAAM,CAACC,OAAO,CAACL;QAClC,IAAII,QAAQ,GAAG,OAAO;QACtB,OAAO,IAAI,CAAC,CAAA,IAAK,CAACA,MAAM;IAC1B;IAEA,IAAIqB,QAAQ;QACV,OAAO;eAAI,IAAI,CAAC,CAAA,KAAM;SAAC;IACzB;IAEA,CAAA,OAAQ,CAACzB,IAAO;QACd,MAAMI,QAAQ,IAAI,CAAC,CAAA,KAAM,CAACC,OAAO,CAACL;QAClC,IAAI,CAAC,CAAA,IAAK,CAACI,MAAM,GAAG;QACpB,IAAI,CAAC,CAAA,SAAU;IACjB;AACF"}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
export class SemaphoreError extends Error {
|
|
2
|
-
}
|
|
3
|
-
export class Semaphore {
|
|
4
|
-
size;
|
|
5
|
-
timeout;
|
|
6
|
-
counter;
|
|
7
|
-
queue;
|
|
8
|
-
constructor(concurrency, size = 0, timeout = 0){
|
|
9
|
-
this.size = size;
|
|
10
|
-
this.timeout = timeout;
|
|
11
|
-
this.queue = [];
|
|
12
|
-
this.counter = concurrency;
|
|
13
|
-
}
|
|
14
|
-
enter() {
|
|
15
|
-
if (this.counter > 0) {
|
|
16
|
-
this.counter--;
|
|
17
|
-
return Promise.resolve();
|
|
18
|
-
} else if (this.queue.length >= this.size) {
|
|
19
|
-
return Promise.reject(new SemaphoreError('Queue is full'));
|
|
20
|
-
} else {
|
|
21
|
-
return new Promise((resolve, reject)=>{
|
|
22
|
-
const waiting = {
|
|
23
|
-
resolve
|
|
24
|
-
};
|
|
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
|
-
} else {
|
|
38
|
-
const item = this.queue.shift();
|
|
39
|
-
if (item) {
|
|
40
|
-
const { resolve, timer } = item;
|
|
41
|
-
if (timer) clearTimeout(timer);
|
|
42
|
-
if (resolve) setTimeout(resolve, 0);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
get isEmpty() {
|
|
47
|
-
return this.queue.length === 0;
|
|
48
|
-
}
|
|
49
|
-
get queueLength() {
|
|
50
|
-
return this.queue.length;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../lib/utils/semaphore.ts"],"sourcesContent":["import type { Callback } from '@nmtjs/common'\n\ninterface SemaphoreQueueItem {\n resolve?: Callback\n timer?: ReturnType<typeof setTimeout>\n}\n\nexport class SemaphoreError extends Error {}\n\n// Semaphore from https://github.com/metarhia/metautil\nexport class Semaphore {\n private counter: number\n\n private readonly queue: SemaphoreQueueItem[] = []\n\n constructor(\n concurrency: number,\n private readonly size: number = 0,\n private readonly timeout: number = 0,\n ) {\n this.counter = concurrency\n }\n\n enter(): Promise<void> {\n if (this.counter > 0) {\n this.counter--\n return Promise.resolve()\n } else if (this.queue.length >= this.size) {\n return Promise.reject(new SemaphoreError('Queue is full'))\n } else {\n return new Promise((resolve, reject) => {\n const waiting: SemaphoreQueueItem = { resolve }\n waiting.timer = setTimeout(() => {\n waiting.resolve = undefined\n this.queue.shift()\n reject(new SemaphoreError('Timeout'))\n }, this.timeout)\n this.queue.push(waiting)\n })\n }\n }\n\n leave() {\n if (this.queue.length === 0) {\n this.counter++\n } else {\n const item = this.queue.shift()\n if (item) {\n const { resolve, timer } = item\n if (timer) clearTimeout(timer)\n if (resolve) setTimeout(resolve, 0)\n }\n }\n }\n\n get isEmpty() {\n return this.queue.length === 0\n }\n\n get queueLength() {\n return this.queue.length\n }\n}\n"],"names":["SemaphoreError","Error","Semaphore","counter","queue","constructor","concurrency","size","timeout","enter","Promise","resolve","length","reject","waiting","timer","setTimeout","undefined","shift","push","leave","item","clearTimeout","isEmpty","queueLength"],"mappings":"AAOA,OAAO,MAAMA,uBAAuBC;AAAO;AAG3C,OAAO,MAAMC;;;IACHC,QAAe;IAENC,MAAgC;IAEjDC,YACEC,WAAmB,EACnB,AAAiBC,OAAe,CAAC,EACjC,AAAiBC,UAAkB,CAAC,CACpC;aAFiBD,OAAAA;aACAC,UAAAA;aALFJ,QAA8B,EAAE;QAO/C,IAAI,CAACD,OAAO,GAAGG;IACjB;IAEAG,QAAuB;QACrB,IAAI,IAAI,CAACN,OAAO,GAAG,GAAG;YACpB,IAAI,CAACA,OAAO;YACZ,OAAOO,QAAQC,OAAO;QACxB,OAAO,IAAI,IAAI,CAACP,KAAK,CAACQ,MAAM,IAAI,IAAI,CAACL,IAAI,EAAE;YACzC,OAAOG,QAAQG,MAAM,CAAC,IAAIb,eAAe;QAC3C,OAAO;YACL,OAAO,IAAIU,QAAQ,CAACC,SAASE;gBAC3B,MAAMC,UAA8B;oBAAEH;gBAAQ;gBAC9CG,QAAQC,KAAK,GAAGC,WAAW;oBACzBF,QAAQH,OAAO,GAAGM;oBAClB,IAAI,CAACb,KAAK,CAACc,KAAK;oBAChBL,OAAO,IAAIb,eAAe;gBAC5B,GAAG,IAAI,CAACQ,OAAO;gBACf,IAAI,CAACJ,KAAK,CAACe,IAAI,CAACL;YAClB;QACF;IACF;IAEAM,QAAQ;QACN,IAAI,IAAI,CAAChB,KAAK,CAACQ,MAAM,KAAK,GAAG;YAC3B,IAAI,CAACT,OAAO;QACd,OAAO;YACL,MAAMkB,OAAO,IAAI,CAACjB,KAAK,CAACc,KAAK;YAC7B,IAAIG,MAAM;gBACR,MAAM,EAAEV,OAAO,EAAEI,KAAK,EAAE,GAAGM;gBAC3B,IAAIN,OAAOO,aAAaP;gBACxB,IAAIJ,SAASK,WAAWL,SAAS;YACnC;QACF;IACF;IAEA,IAAIY,UAAU;QACZ,OAAO,IAAI,CAACnB,KAAK,CAACQ,MAAM,KAAK;IAC/B;IAEA,IAAIY,cAAc;QAChB,OAAO,IAAI,CAACpB,KAAK,CAACQ,MAAM;IAC1B;AACF"}
|
package/index.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export * from './lib/constants.ts'
|
|
2
|
-
export * from './lib/container.ts'
|
|
3
|
-
export * from './lib/enums.ts'
|
|
4
|
-
export * from './lib/hooks.ts'
|
|
5
|
-
export * from './lib/injectables.ts'
|
|
6
|
-
export * from './lib/logger.ts'
|
|
7
|
-
export * from './lib/plugin.ts'
|
|
8
|
-
export * from './lib/registry.ts'
|
|
9
|
-
export * from './lib/types.ts'
|
|
10
|
-
export * from './lib/types.ts'
|
|
11
|
-
export * from './lib/utils/index.ts'
|
|
File without changes
|
|
File without changes
|
/package/{lib → src}/enums.ts
RENAMED
|
File without changes
|
|
File without changes
|
/package/{lib → src}/logger.ts
RENAMED
|
File without changes
|
/package/{lib → src}/registry.ts
RENAMED
|
File without changes
|
/package/{lib → src}/types.ts
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|