@tachybase/utils 0.23.8
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 +9 -0
- package/LICENSE +201 -0
- package/lib/assign.d.ts +9 -0
- package/lib/assign.js +136 -0
- package/lib/client.d.ts +21 -0
- package/lib/client.js +77 -0
- package/lib/cluster.d.ts +2 -0
- package/lib/cluster.js +39 -0
- package/lib/collections-graph.d.ts +16 -0
- package/lib/collections-graph.js +98 -0
- package/lib/common.d.ts +7 -0
- package/lib/common.js +98 -0
- package/lib/currencyUtils.d.ts +44 -0
- package/lib/currencyUtils.js +75 -0
- package/lib/date.d.ts +22 -0
- package/lib/date.js +186 -0
- package/lib/dayjs.d.ts +10 -0
- package/lib/dayjs.js +73 -0
- package/lib/decorators.d.ts +16 -0
- package/lib/decorators.js +94 -0
- package/lib/dom.d.ts +4 -0
- package/lib/dom.js +52 -0
- package/lib/forEach.d.ts +1 -0
- package/lib/forEach.js +36 -0
- package/lib/fs-exists.d.ts +1 -0
- package/lib/fs-exists.js +37 -0
- package/lib/getCurrentStacks.d.ts +1 -0
- package/lib/getCurrentStacks.js +35 -0
- package/lib/getValuesByPath.d.ts +1 -0
- package/lib/getValuesByPath.js +62 -0
- package/lib/index.d.ts +31 -0
- package/lib/index.js +97 -0
- package/lib/isPortalInBody.d.ts +5 -0
- package/lib/isPortalInBody.js +45 -0
- package/lib/json-templates.d.ts +1 -0
- package/lib/json-templates.js +145 -0
- package/lib/koa-multer.d.ts +6 -0
- package/lib/koa-multer.js +83 -0
- package/lib/log.d.ts +1 -0
- package/lib/log.js +30 -0
- package/lib/measure-execution-time.d.ts +1 -0
- package/lib/measure-execution-time.js +35 -0
- package/lib/merge.d.ts +1 -0
- package/lib/merge.js +46 -0
- package/lib/mixin/AsyncEmitter.d.ts +3 -0
- package/lib/mixin/AsyncEmitter.js +73 -0
- package/lib/mixin/index.d.ts +1 -0
- package/lib/mixin/index.js +39 -0
- package/lib/notification.d.ts +1 -0
- package/lib/notification.js +34 -0
- package/lib/number.d.ts +1 -0
- package/lib/number.js +36 -0
- package/lib/parse-date.d.ts +3 -0
- package/lib/parse-date.js +237 -0
- package/lib/parse-filter.d.ts +146 -0
- package/lib/parse-filter.js +305 -0
- package/lib/parseHTML.d.ts +7 -0
- package/lib/parseHTML.js +33 -0
- package/lib/perf-hooks.d.ts +6 -0
- package/lib/perf-hooks.js +60 -0
- package/lib/plugin-symlink.d.ts +4 -0
- package/lib/plugin-symlink.js +131 -0
- package/lib/registry.d.ts +14 -0
- package/lib/registry.js +60 -0
- package/lib/requireModule.d.ts +3 -0
- package/lib/requireModule.js +64 -0
- package/lib/toposort.d.ts +13 -0
- package/lib/toposort.js +90 -0
- package/lib/typedi/container-instance.class.d.ts +109 -0
- package/lib/typedi/container-instance.class.js +349 -0
- package/lib/typedi/container-registry.class.d.ts +51 -0
- package/lib/typedi/container-registry.class.js +97 -0
- package/lib/typedi/decorators/inject-many.decorator.d.ts +8 -0
- package/lib/typedi/decorators/inject-many.decorator.js +56 -0
- package/lib/typedi/decorators/inject.decorator.d.ts +9 -0
- package/lib/typedi/decorators/inject.decorator.js +56 -0
- package/lib/typedi/decorators/service.decorator.d.ts +6 -0
- package/lib/typedi/decorators/service.decorator.js +49 -0
- package/lib/typedi/empty.const.d.ts +6 -0
- package/lib/typedi/empty.const.js +27 -0
- package/lib/typedi/error/cannot-inject-value.error.d.ts +11 -0
- package/lib/typedi/error/cannot-inject-value.error.js +40 -0
- package/lib/typedi/error/cannot-instantiate-value.error.d.ts +11 -0
- package/lib/typedi/error/cannot-instantiate-value.error.js +49 -0
- package/lib/typedi/error/service-not-found.error.d.ts +11 -0
- package/lib/typedi/error/service-not-found.error.js +49 -0
- package/lib/typedi/index.d.ts +17 -0
- package/lib/typedi/index.js +64 -0
- package/lib/typedi/interfaces/container-options.interface.d.ts +45 -0
- package/lib/typedi/interfaces/container-options.interface.js +15 -0
- package/lib/typedi/interfaces/handler.interface.d.ts +27 -0
- package/lib/typedi/interfaces/handler.interface.js +15 -0
- package/lib/typedi/interfaces/service-metadata.interface.d.ts +53 -0
- package/lib/typedi/interfaces/service-metadata.interface.js +15 -0
- package/lib/typedi/interfaces/service-options.interface.d.ts +6 -0
- package/lib/typedi/interfaces/service-options.interface.js +15 -0
- package/lib/typedi/token.class.d.ts +11 -0
- package/lib/typedi/token.class.js +37 -0
- package/lib/typedi/types/abstract-constructable.type.d.ts +9 -0
- package/lib/typedi/types/abstract-constructable.type.js +15 -0
- package/lib/typedi/types/constructable.type.d.ts +10 -0
- package/lib/typedi/types/constructable.type.js +15 -0
- package/lib/typedi/types/container-identifier.type.d.ts +4 -0
- package/lib/typedi/types/container-identifier.type.js +15 -0
- package/lib/typedi/types/container-scope.type.d.ts +1 -0
- package/lib/typedi/types/container-scope.type.js +15 -0
- package/lib/typedi/types/service-identifier.type.d.ts +8 -0
- package/lib/typedi/types/service-identifier.type.js +15 -0
- package/lib/typedi/utils/resolve-to-type-wrapper.util.d.ts +15 -0
- package/lib/typedi/utils/resolve-to-type-wrapper.util.js +39 -0
- package/lib/uid.d.ts +1 -0
- package/lib/uid.js +35 -0
- package/lib/url.d.ts +1 -0
- package/lib/url.js +37 -0
- package/package.json +49 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var requireModule_exports = {};
|
|
30
|
+
__export(requireModule_exports, {
|
|
31
|
+
default: () => requireModule_default,
|
|
32
|
+
importModule: () => importModule,
|
|
33
|
+
requireModule: () => requireModule
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(requireModule_exports);
|
|
36
|
+
var import_path = __toESM(require("path"));
|
|
37
|
+
var import_url = require("url");
|
|
38
|
+
function requireModule(m) {
|
|
39
|
+
if (typeof m === "string") {
|
|
40
|
+
m = require(m);
|
|
41
|
+
}
|
|
42
|
+
if (typeof m !== "object") {
|
|
43
|
+
return m;
|
|
44
|
+
}
|
|
45
|
+
return m.__esModule ? m.default : m;
|
|
46
|
+
}
|
|
47
|
+
__name(requireModule, "requireModule");
|
|
48
|
+
var requireModule_default = requireModule;
|
|
49
|
+
async function importModule(m) {
|
|
50
|
+
if (!process.env.VITEST) {
|
|
51
|
+
return requireModule(m);
|
|
52
|
+
}
|
|
53
|
+
if (import_path.default.isAbsolute(m)) {
|
|
54
|
+
m = (0, import_url.pathToFileURL)(m).href;
|
|
55
|
+
}
|
|
56
|
+
const r = (await import(m)).default;
|
|
57
|
+
return r.__esModule ? r.default : r;
|
|
58
|
+
}
|
|
59
|
+
__name(importModule, "importModule");
|
|
60
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
61
|
+
0 && (module.exports = {
|
|
62
|
+
importModule,
|
|
63
|
+
requireModule
|
|
64
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import Topo from '@hapi/topo';
|
|
2
|
+
export interface ToposortOptions extends Topo.Options {
|
|
3
|
+
tag?: string;
|
|
4
|
+
group?: string;
|
|
5
|
+
unique?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare class Toposort<T> extends Topo.Sorter<T> {
|
|
8
|
+
#private;
|
|
9
|
+
unshift(...items: any[]): void;
|
|
10
|
+
push(...items: any[]): void;
|
|
11
|
+
add(nodes: T | T[], options?: ToposortOptions): T[];
|
|
12
|
+
}
|
|
13
|
+
export default Toposort;
|
package/lib/toposort.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __typeError = (msg) => {
|
|
8
|
+
throw TypeError(msg);
|
|
9
|
+
};
|
|
10
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
33
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
34
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
35
|
+
var toposort_exports = {};
|
|
36
|
+
__export(toposort_exports, {
|
|
37
|
+
Toposort: () => Toposort,
|
|
38
|
+
default: () => toposort_default
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(toposort_exports);
|
|
41
|
+
var import_topo = __toESM(require("@hapi/topo"));
|
|
42
|
+
var _tags;
|
|
43
|
+
const _Toposort = class _Toposort extends import_topo.default.Sorter {
|
|
44
|
+
constructor() {
|
|
45
|
+
super(...arguments);
|
|
46
|
+
__privateAdd(this, _tags, /* @__PURE__ */ new Set());
|
|
47
|
+
}
|
|
48
|
+
unshift(...items) {
|
|
49
|
+
this._items.unshift(
|
|
50
|
+
...items.map((node) => ({
|
|
51
|
+
node,
|
|
52
|
+
seq: this._items.length,
|
|
53
|
+
sort: 0,
|
|
54
|
+
before: [],
|
|
55
|
+
after: [],
|
|
56
|
+
group: "?"
|
|
57
|
+
}))
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
push(...items) {
|
|
61
|
+
this._items.push(
|
|
62
|
+
...items.map((node) => ({
|
|
63
|
+
node,
|
|
64
|
+
seq: this._items.length,
|
|
65
|
+
sort: 0,
|
|
66
|
+
before: [],
|
|
67
|
+
after: [],
|
|
68
|
+
group: "?"
|
|
69
|
+
}))
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
add(nodes, options) {
|
|
73
|
+
if (options == null ? void 0 : options.tag) {
|
|
74
|
+
options.group = options.tag;
|
|
75
|
+
if (__privateGet(this, _tags).has(options.tag) && options.unique) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
__privateGet(this, _tags).add(options.tag);
|
|
79
|
+
}
|
|
80
|
+
return super.add(nodes, options);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
_tags = new WeakMap();
|
|
84
|
+
__name(_Toposort, "Toposort");
|
|
85
|
+
let Toposort = _Toposort;
|
|
86
|
+
var toposort_default = Toposort;
|
|
87
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
88
|
+
0 && (module.exports = {
|
|
89
|
+
Toposort
|
|
90
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { Handler } from './interfaces/handler.interface';
|
|
2
|
+
import { ServiceOptions } from './interfaces/service-options.interface';
|
|
3
|
+
import { ContainerIdentifier } from './types/container-identifier.type';
|
|
4
|
+
import { ServiceIdentifier } from './types/service-identifier.type';
|
|
5
|
+
/**
|
|
6
|
+
* TypeDI can have multiple containers.
|
|
7
|
+
* One container is ContainerInstance.
|
|
8
|
+
*/
|
|
9
|
+
export declare class ContainerInstance {
|
|
10
|
+
/** Container instance id. */
|
|
11
|
+
readonly id: ContainerIdentifier;
|
|
12
|
+
/** Metadata for all registered services in this container. */
|
|
13
|
+
private metadataMap;
|
|
14
|
+
/**
|
|
15
|
+
* Services registered with 'multiple: true' are saved as simple services
|
|
16
|
+
* with a generated token and the mapping between the original ID and the
|
|
17
|
+
* generated one is stored here. This is handled like this to allow simplifying
|
|
18
|
+
* the inner workings of the service instance.
|
|
19
|
+
*/
|
|
20
|
+
private multiServiceIds;
|
|
21
|
+
/**
|
|
22
|
+
* All registered handlers. The @Inject() decorator uses handlers internally to mark a property for injection.
|
|
23
|
+
**/
|
|
24
|
+
private readonly handlers;
|
|
25
|
+
/**
|
|
26
|
+
* The default global container. By default services are registered into this
|
|
27
|
+
* container when registered via `Container.set()` or `@Service` decorator.
|
|
28
|
+
*/
|
|
29
|
+
private static _default;
|
|
30
|
+
static get default(): ContainerInstance;
|
|
31
|
+
/**
|
|
32
|
+
* Indicates if the container has been disposed or not.
|
|
33
|
+
* Any function call should fail when called after being disposed.
|
|
34
|
+
*
|
|
35
|
+
* NOTE: Currently not in used
|
|
36
|
+
*/
|
|
37
|
+
private disposed;
|
|
38
|
+
constructor(id: ContainerIdentifier);
|
|
39
|
+
/**
|
|
40
|
+
* Checks if the service with given name or type is registered service container.
|
|
41
|
+
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
|
|
42
|
+
*/
|
|
43
|
+
has<T = unknown>(identifier: ServiceIdentifier<T>): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Retrieves the service with given name or type from the service container.
|
|
46
|
+
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
|
|
47
|
+
*/
|
|
48
|
+
get<T = unknown>(identifier: ServiceIdentifier<T>): T;
|
|
49
|
+
/**
|
|
50
|
+
* Gets all instances registered in the container of the given service identifier.
|
|
51
|
+
* Used when service defined with multiple: true flag.
|
|
52
|
+
*/
|
|
53
|
+
getMany<T = unknown>(identifier: ServiceIdentifier<T>): T[];
|
|
54
|
+
/**
|
|
55
|
+
* Sets a value for the given type or service name in the container.
|
|
56
|
+
*/
|
|
57
|
+
set<T = unknown>(serviceOptions: ServiceOptions<T>): this;
|
|
58
|
+
/**
|
|
59
|
+
* Removes services with a given service identifiers.
|
|
60
|
+
*/
|
|
61
|
+
remove(identifierOrIdentifierArray: ServiceIdentifier | ServiceIdentifier[]): this;
|
|
62
|
+
/**
|
|
63
|
+
* Gets a separate container instance for the given instance id.
|
|
64
|
+
*/
|
|
65
|
+
of(containerId?: ContainerIdentifier): ContainerInstance;
|
|
66
|
+
/**
|
|
67
|
+
* Registers a new handler.
|
|
68
|
+
*/
|
|
69
|
+
registerHandler(handler: Handler): ContainerInstance;
|
|
70
|
+
/**
|
|
71
|
+
* Helper method that imports given services.
|
|
72
|
+
*/
|
|
73
|
+
import(services: Function[]): ContainerInstance;
|
|
74
|
+
/**
|
|
75
|
+
* Completely resets the container by removing all previously registered services from it.
|
|
76
|
+
*/
|
|
77
|
+
reset(options?: {
|
|
78
|
+
strategy: 'resetValue' | 'resetServices';
|
|
79
|
+
}): this;
|
|
80
|
+
dispose(): Promise<void>;
|
|
81
|
+
private throwIfDisposed;
|
|
82
|
+
/**
|
|
83
|
+
* Gets the value belonging to passed in `ServiceMetadata` instance.
|
|
84
|
+
*
|
|
85
|
+
* - if `serviceMetadata.value` is already set it is immediately returned
|
|
86
|
+
* - otherwise the requested type is resolved to the value saved to `serviceMetadata.value` and returned
|
|
87
|
+
*/
|
|
88
|
+
private getServiceValue;
|
|
89
|
+
/**
|
|
90
|
+
* Initializes all parameter types for a given target service class.
|
|
91
|
+
*/
|
|
92
|
+
private initializeParams;
|
|
93
|
+
/**
|
|
94
|
+
* Checks if given parameter type is primitive type or not.
|
|
95
|
+
*/
|
|
96
|
+
private isPrimitiveParamType;
|
|
97
|
+
/**
|
|
98
|
+
* Applies all registered handlers on a given target class.
|
|
99
|
+
*/
|
|
100
|
+
private applyPropertyHandlers;
|
|
101
|
+
/**
|
|
102
|
+
* Checks if the given service metadata contains a destroyable service instance and destroys it in place. If the service
|
|
103
|
+
* contains a callable function named `destroy` it is called but not awaited and the return value is ignored..
|
|
104
|
+
*
|
|
105
|
+
* @param serviceMetadata the service metadata containing the instance to destroy
|
|
106
|
+
* @param force when true the service will be always destroyed even if it's cannot be re-created
|
|
107
|
+
*/
|
|
108
|
+
private disposeServiceInstance;
|
|
109
|
+
}
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
21
|
+
var container_instance_class_exports = {};
|
|
22
|
+
__export(container_instance_class_exports, {
|
|
23
|
+
ContainerInstance: () => ContainerInstance
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(container_instance_class_exports);
|
|
26
|
+
var import_container_registry = require("./container-registry.class");
|
|
27
|
+
var import_empty = require("./empty.const");
|
|
28
|
+
var import_cannot_instantiate_value = require("./error/cannot-instantiate-value.error");
|
|
29
|
+
var import_service_not_found = require("./error/service-not-found.error");
|
|
30
|
+
var import_token = require("./token.class");
|
|
31
|
+
const _ContainerInstance = class _ContainerInstance {
|
|
32
|
+
/** Container instance id. */
|
|
33
|
+
id;
|
|
34
|
+
/** Metadata for all registered services in this container. */
|
|
35
|
+
metadataMap = /* @__PURE__ */ new Map();
|
|
36
|
+
/**
|
|
37
|
+
* Services registered with 'multiple: true' are saved as simple services
|
|
38
|
+
* with a generated token and the mapping between the original ID and the
|
|
39
|
+
* generated one is stored here. This is handled like this to allow simplifying
|
|
40
|
+
* the inner workings of the service instance.
|
|
41
|
+
*/
|
|
42
|
+
multiServiceIds = /* @__PURE__ */ new Map();
|
|
43
|
+
/**
|
|
44
|
+
* All registered handlers. The @Inject() decorator uses handlers internally to mark a property for injection.
|
|
45
|
+
**/
|
|
46
|
+
handlers = [];
|
|
47
|
+
static get default() {
|
|
48
|
+
if (!this._default) {
|
|
49
|
+
this._default = new _ContainerInstance("default");
|
|
50
|
+
import_container_registry.ContainerRegistry.registerContainer(this._default);
|
|
51
|
+
}
|
|
52
|
+
return this._default;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Indicates if the container has been disposed or not.
|
|
56
|
+
* Any function call should fail when called after being disposed.
|
|
57
|
+
*
|
|
58
|
+
* NOTE: Currently not in used
|
|
59
|
+
*/
|
|
60
|
+
disposed = false;
|
|
61
|
+
constructor(id) {
|
|
62
|
+
this.id = id;
|
|
63
|
+
if (id !== "default") {
|
|
64
|
+
import_container_registry.ContainerRegistry.registerContainer(this);
|
|
65
|
+
this.handlers = _ContainerInstance.default.handlers || [];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Checks if the service with given name or type is registered service container.
|
|
70
|
+
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
|
|
71
|
+
*/
|
|
72
|
+
has(identifier) {
|
|
73
|
+
this.throwIfDisposed();
|
|
74
|
+
return !!this.metadataMap.has(identifier) || !!this.multiServiceIds.has(identifier);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Retrieves the service with given name or type from the service container.
|
|
78
|
+
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
|
|
79
|
+
*/
|
|
80
|
+
get(identifier) {
|
|
81
|
+
this.throwIfDisposed();
|
|
82
|
+
const global = _ContainerInstance.default.metadataMap.get(identifier);
|
|
83
|
+
const local = this.metadataMap.get(identifier);
|
|
84
|
+
const metadata = (global == null ? void 0 : global.scope) === "singleton" ? global : local;
|
|
85
|
+
if (metadata && metadata.multiple === true) {
|
|
86
|
+
throw new Error(`Cannot resolve multiple values for ${identifier.toString()} service!`);
|
|
87
|
+
}
|
|
88
|
+
if (metadata) {
|
|
89
|
+
return this.getServiceValue(metadata);
|
|
90
|
+
}
|
|
91
|
+
if (global && this !== _ContainerInstance.default) {
|
|
92
|
+
const clonedService = { ...global };
|
|
93
|
+
clonedService.value = import_empty.EMPTY_VALUE;
|
|
94
|
+
this.set(clonedService);
|
|
95
|
+
const value = this.getServiceValue(clonedService);
|
|
96
|
+
this.set({ ...clonedService, value });
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
throw new import_service_not_found.ServiceNotFoundError(identifier);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Gets all instances registered in the container of the given service identifier.
|
|
103
|
+
* Used when service defined with multiple: true flag.
|
|
104
|
+
*/
|
|
105
|
+
getMany(identifier) {
|
|
106
|
+
this.throwIfDisposed();
|
|
107
|
+
const globalIdMap = _ContainerInstance.default.multiServiceIds.get(identifier);
|
|
108
|
+
const localIdMap = this.multiServiceIds.get(identifier);
|
|
109
|
+
if ((globalIdMap == null ? void 0 : globalIdMap.scope) === "singleton") {
|
|
110
|
+
return globalIdMap.tokens.map((generatedId) => _ContainerInstance.default.get(generatedId));
|
|
111
|
+
}
|
|
112
|
+
if (localIdMap) {
|
|
113
|
+
return localIdMap.tokens.map((generatedId) => this.get(generatedId));
|
|
114
|
+
}
|
|
115
|
+
throw new import_service_not_found.ServiceNotFoundError(identifier);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Sets a value for the given type or service name in the container.
|
|
119
|
+
*/
|
|
120
|
+
set(serviceOptions) {
|
|
121
|
+
this.throwIfDisposed();
|
|
122
|
+
if (serviceOptions.scope === "singleton" && _ContainerInstance.default !== this) {
|
|
123
|
+
_ContainerInstance.default.set(serviceOptions);
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
const newMetadata = {
|
|
127
|
+
/**
|
|
128
|
+
* Typescript cannot understand that if ID doesn't exists then type must exists based on the
|
|
129
|
+
* typing so we need to explicitly cast this to a `ServiceIdentifier`
|
|
130
|
+
*/
|
|
131
|
+
id: serviceOptions.id || serviceOptions.type,
|
|
132
|
+
type: serviceOptions.type || null,
|
|
133
|
+
factory: serviceOptions.factory,
|
|
134
|
+
value: serviceOptions.value || import_empty.EMPTY_VALUE,
|
|
135
|
+
multiple: serviceOptions.multiple || false,
|
|
136
|
+
eager: serviceOptions.eager || false,
|
|
137
|
+
scope: serviceOptions.scope || "container",
|
|
138
|
+
/** We allow overriding the above options via the received config object. */
|
|
139
|
+
...serviceOptions,
|
|
140
|
+
referencedBy: (/* @__PURE__ */ new Map()).set(this.id, this)
|
|
141
|
+
};
|
|
142
|
+
if (serviceOptions.multiple) {
|
|
143
|
+
const maskedToken = new import_token.Token(`MultiMaskToken-${newMetadata.id.toString()}`);
|
|
144
|
+
const existingMultiGroup = this.multiServiceIds.get(newMetadata.id);
|
|
145
|
+
if (existingMultiGroup) {
|
|
146
|
+
existingMultiGroup.tokens.push(maskedToken);
|
|
147
|
+
} else {
|
|
148
|
+
this.multiServiceIds.set(newMetadata.id, { scope: newMetadata.scope, tokens: [maskedToken] });
|
|
149
|
+
}
|
|
150
|
+
newMetadata.id = maskedToken;
|
|
151
|
+
newMetadata.multiple = false;
|
|
152
|
+
}
|
|
153
|
+
const existingMetadata = this.metadataMap.get(newMetadata.id);
|
|
154
|
+
if (existingMetadata) {
|
|
155
|
+
Object.assign(existingMetadata, newMetadata);
|
|
156
|
+
} else {
|
|
157
|
+
this.metadataMap.set(newMetadata.id, newMetadata);
|
|
158
|
+
}
|
|
159
|
+
if (newMetadata.eager && newMetadata.scope !== "transient") {
|
|
160
|
+
this.get(newMetadata.id);
|
|
161
|
+
}
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Removes services with a given service identifiers.
|
|
166
|
+
*/
|
|
167
|
+
remove(identifierOrIdentifierArray) {
|
|
168
|
+
this.throwIfDisposed();
|
|
169
|
+
if (Array.isArray(identifierOrIdentifierArray)) {
|
|
170
|
+
identifierOrIdentifierArray.forEach((id) => this.remove(id));
|
|
171
|
+
} else {
|
|
172
|
+
const serviceMetadata = this.metadataMap.get(identifierOrIdentifierArray);
|
|
173
|
+
if (serviceMetadata) {
|
|
174
|
+
this.disposeServiceInstance(serviceMetadata);
|
|
175
|
+
this.metadataMap.delete(identifierOrIdentifierArray);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Gets a separate container instance for the given instance id.
|
|
182
|
+
*/
|
|
183
|
+
of(containerId = "default") {
|
|
184
|
+
this.throwIfDisposed();
|
|
185
|
+
if (containerId === "default") {
|
|
186
|
+
return _ContainerInstance.default;
|
|
187
|
+
}
|
|
188
|
+
let container;
|
|
189
|
+
if (import_container_registry.ContainerRegistry.hasContainer(containerId)) {
|
|
190
|
+
container = import_container_registry.ContainerRegistry.getContainer(containerId);
|
|
191
|
+
} else {
|
|
192
|
+
container = new _ContainerInstance(containerId);
|
|
193
|
+
}
|
|
194
|
+
return container;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Registers a new handler.
|
|
198
|
+
*/
|
|
199
|
+
registerHandler(handler) {
|
|
200
|
+
this.handlers.push(handler);
|
|
201
|
+
return this;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Helper method that imports given services.
|
|
205
|
+
*/
|
|
206
|
+
import(services) {
|
|
207
|
+
this.throwIfDisposed();
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Completely resets the container by removing all previously registered services from it.
|
|
212
|
+
*/
|
|
213
|
+
reset(options = { strategy: "resetValue" }) {
|
|
214
|
+
this.throwIfDisposed();
|
|
215
|
+
switch (options.strategy) {
|
|
216
|
+
case "resetValue":
|
|
217
|
+
this.metadataMap.forEach((service) => this.disposeServiceInstance(service));
|
|
218
|
+
break;
|
|
219
|
+
case "resetServices":
|
|
220
|
+
this.metadataMap.forEach((service) => this.disposeServiceInstance(service));
|
|
221
|
+
this.metadataMap.clear();
|
|
222
|
+
this.multiServiceIds.clear();
|
|
223
|
+
break;
|
|
224
|
+
default:
|
|
225
|
+
throw new Error("Received invalid reset strategy.");
|
|
226
|
+
}
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
229
|
+
async dispose() {
|
|
230
|
+
this.reset({ strategy: "resetServices" });
|
|
231
|
+
this.disposed = true;
|
|
232
|
+
await Promise.resolve();
|
|
233
|
+
}
|
|
234
|
+
throwIfDisposed() {
|
|
235
|
+
if (this.disposed) {
|
|
236
|
+
throw new Error("Cannot use container after it has been disposed.");
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Gets the value belonging to passed in `ServiceMetadata` instance.
|
|
241
|
+
*
|
|
242
|
+
* - if `serviceMetadata.value` is already set it is immediately returned
|
|
243
|
+
* - otherwise the requested type is resolved to the value saved to `serviceMetadata.value` and returned
|
|
244
|
+
*/
|
|
245
|
+
getServiceValue(serviceMetadata) {
|
|
246
|
+
let value = import_empty.EMPTY_VALUE;
|
|
247
|
+
if (serviceMetadata.value !== import_empty.EMPTY_VALUE) {
|
|
248
|
+
return serviceMetadata.value;
|
|
249
|
+
}
|
|
250
|
+
if (!serviceMetadata.factory && typeof serviceMetadata.type === "undefined") {
|
|
251
|
+
throw new import_cannot_instantiate_value.CannotInstantiateValueError(serviceMetadata.id);
|
|
252
|
+
}
|
|
253
|
+
if (serviceMetadata.factory) {
|
|
254
|
+
if (serviceMetadata.factory instanceof Array) {
|
|
255
|
+
let factoryInstance;
|
|
256
|
+
try {
|
|
257
|
+
factoryInstance = this.get(serviceMetadata.factory[0]);
|
|
258
|
+
} catch (error) {
|
|
259
|
+
if (error instanceof import_service_not_found.ServiceNotFoundError) {
|
|
260
|
+
factoryInstance = new serviceMetadata.factory[0]();
|
|
261
|
+
} else {
|
|
262
|
+
throw error;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
value = factoryInstance[serviceMetadata.factory[1]](this, serviceMetadata.id);
|
|
266
|
+
} else {
|
|
267
|
+
value = serviceMetadata.factory(this, serviceMetadata.id);
|
|
268
|
+
}
|
|
269
|
+
} else if (typeof serviceMetadata.type === "function") {
|
|
270
|
+
value = new serviceMetadata.type();
|
|
271
|
+
}
|
|
272
|
+
if (serviceMetadata.scope !== "transient" && value !== import_empty.EMPTY_VALUE) {
|
|
273
|
+
serviceMetadata.value = value;
|
|
274
|
+
}
|
|
275
|
+
if (value === import_empty.EMPTY_VALUE) {
|
|
276
|
+
throw new import_cannot_instantiate_value.CannotInstantiateValueError(serviceMetadata.id);
|
|
277
|
+
}
|
|
278
|
+
if (serviceMetadata.type) {
|
|
279
|
+
this.applyPropertyHandlers(serviceMetadata.type, value);
|
|
280
|
+
}
|
|
281
|
+
return value;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Initializes all parameter types for a given target service class.
|
|
285
|
+
*/
|
|
286
|
+
initializeParams(target, paramTypes) {
|
|
287
|
+
return paramTypes.map((paramType, index) => {
|
|
288
|
+
const paramHandler = this.handlers.find((handler) => {
|
|
289
|
+
return handler.object === target && handler.index === index;
|
|
290
|
+
}) || this.handlers.find((handler) => {
|
|
291
|
+
return handler.object === Object.getPrototypeOf(target) && handler.index === index;
|
|
292
|
+
});
|
|
293
|
+
if (paramHandler) return paramHandler.value(this);
|
|
294
|
+
if (paramType && paramType.name && !this.isPrimitiveParamType(paramType.name)) {
|
|
295
|
+
return this.get(paramType);
|
|
296
|
+
}
|
|
297
|
+
return void 0;
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Checks if given parameter type is primitive type or not.
|
|
302
|
+
*/
|
|
303
|
+
isPrimitiveParamType(paramTypeName) {
|
|
304
|
+
return ["string", "boolean", "number", "object"].includes(paramTypeName.toLowerCase());
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Applies all registered handlers on a given target class.
|
|
308
|
+
*/
|
|
309
|
+
applyPropertyHandlers(target, instance) {
|
|
310
|
+
this.handlers.forEach((handler) => {
|
|
311
|
+
if (typeof handler.index === "number") return;
|
|
312
|
+
if (handler.object !== target && !(target.prototype instanceof handler.object)) return;
|
|
313
|
+
if (handler.propertyName) {
|
|
314
|
+
instance[handler.propertyName] = handler.value(this);
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Checks if the given service metadata contains a destroyable service instance and destroys it in place. If the service
|
|
320
|
+
* contains a callable function named `destroy` it is called but not awaited and the return value is ignored..
|
|
321
|
+
*
|
|
322
|
+
* @param serviceMetadata the service metadata containing the instance to destroy
|
|
323
|
+
* @param force when true the service will be always destroyed even if it's cannot be re-created
|
|
324
|
+
*/
|
|
325
|
+
disposeServiceInstance(serviceMetadata, force = false) {
|
|
326
|
+
this.throwIfDisposed();
|
|
327
|
+
const shouldResetValue = force || !!serviceMetadata.type || !!serviceMetadata.factory;
|
|
328
|
+
if (shouldResetValue) {
|
|
329
|
+
if (typeof (serviceMetadata == null ? void 0 : serviceMetadata.value)["dispose"] === "function") {
|
|
330
|
+
try {
|
|
331
|
+
serviceMetadata.value.dispose();
|
|
332
|
+
} catch (error) {
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
serviceMetadata.value = import_empty.EMPTY_VALUE;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
__name(_ContainerInstance, "ContainerInstance");
|
|
340
|
+
/**
|
|
341
|
+
* The default global container. By default services are registered into this
|
|
342
|
+
* container when registered via `Container.set()` or `@Service` decorator.
|
|
343
|
+
*/
|
|
344
|
+
__publicField(_ContainerInstance, "_default");
|
|
345
|
+
let ContainerInstance = _ContainerInstance;
|
|
346
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
347
|
+
0 && (module.exports = {
|
|
348
|
+
ContainerInstance
|
|
349
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { ContainerInstance } from './container-instance.class';
|
|
2
|
+
import { ContainerIdentifier } from './types/container-identifier.type';
|
|
3
|
+
/**
|
|
4
|
+
* The container registry is responsible for holding the default and every
|
|
5
|
+
* created container instance for later access.
|
|
6
|
+
*
|
|
7
|
+
* _Note: This class is for internal use and it's API may break in minor or
|
|
8
|
+
* patch releases without warning._
|
|
9
|
+
*/
|
|
10
|
+
export declare class ContainerRegistry {
|
|
11
|
+
/**
|
|
12
|
+
* The list of all known container. Created containers are automatically added
|
|
13
|
+
* to this list. Two container cannot be registered with the same ID.
|
|
14
|
+
*
|
|
15
|
+
* This map doesn't contains the default container.
|
|
16
|
+
*/
|
|
17
|
+
private static readonly containerMap;
|
|
18
|
+
/**
|
|
19
|
+
* Registers the given container instance or throws an error.
|
|
20
|
+
*
|
|
21
|
+
* _Note: This function is auto-called when a Container instance is created,
|
|
22
|
+
* it doesn't need to be called manually!_
|
|
23
|
+
*
|
|
24
|
+
* @param container the container to add to the registry
|
|
25
|
+
*/
|
|
26
|
+
static registerContainer(container: ContainerInstance): void;
|
|
27
|
+
/**
|
|
28
|
+
* Returns true if a container exists with the given ID or false otherwise.
|
|
29
|
+
*
|
|
30
|
+
* @param container the ID of the container
|
|
31
|
+
*/
|
|
32
|
+
static hasContainer(id: ContainerIdentifier): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Returns the container for requested ID or throws an error if no container
|
|
35
|
+
* is registered with the given ID.
|
|
36
|
+
*
|
|
37
|
+
* @param container the ID of the container
|
|
38
|
+
*/
|
|
39
|
+
static getContainer(id: ContainerIdentifier): ContainerInstance;
|
|
40
|
+
/**
|
|
41
|
+
* Removes the given container from the registry and disposes all services
|
|
42
|
+
* registered only in this container.
|
|
43
|
+
*
|
|
44
|
+
* This function throws an error if no
|
|
45
|
+
* - container exists with the given ID
|
|
46
|
+
* - any of the registered services threw an error during it's disposal
|
|
47
|
+
*
|
|
48
|
+
* @param container the container to remove from the registry
|
|
49
|
+
*/
|
|
50
|
+
static removeContainer(container: ContainerInstance): Promise<void>;
|
|
51
|
+
}
|