@moostjs/otel 0.5.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +5 -513
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +17 -525
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var api = require('@opentelemetry/api');
|
|
4
4
|
var moost = require('moost');
|
|
5
|
-
|
|
5
|
+
require('crypto');
|
|
6
6
|
var node_async_hooks = require('node:async_hooks');
|
|
7
7
|
var sdkTraceBase = require('@opentelemetry/sdk-trace-base');
|
|
8
8
|
|
|
@@ -338,7 +338,7 @@ function enableOtelForMoost() {
|
|
|
338
338
|
moost.replaceContextInjector(new SpanInjector());
|
|
339
339
|
}
|
|
340
340
|
|
|
341
|
-
function getConstructor
|
|
341
|
+
function getConstructor(instance) {
|
|
342
342
|
return isConstructor(instance) ?
|
|
343
343
|
instance : instance.constructor ?
|
|
344
344
|
instance.constructor : Object.getPrototypeOf(instance).constructor;
|
|
@@ -353,7 +353,7 @@ const root = typeof global === 'object' ? global : typeof self === 'object' ? se
|
|
|
353
353
|
function getMetaObject(target, prop) {
|
|
354
354
|
const isParam = typeof prop !== 'undefined';
|
|
355
355
|
const metadata = isParam ? paramMetadata : classMetadata;
|
|
356
|
-
const targetKey = getConstructor
|
|
356
|
+
const targetKey = getConstructor(target);
|
|
357
357
|
let meta = metadata.get(targetKey);
|
|
358
358
|
if (!meta) {
|
|
359
359
|
meta = {};
|
|
@@ -495,7 +495,7 @@ class Mate {
|
|
|
495
495
|
read(target, propKey) {
|
|
496
496
|
var _a;
|
|
497
497
|
const isConstr = isConstructor(target);
|
|
498
|
-
const constructor = isConstr ? target : getConstructor
|
|
498
|
+
const constructor = isConstr ? target : getConstructor(target);
|
|
499
499
|
const proto = constructor.prototype;
|
|
500
500
|
let ownMeta = Reflect$2.getOwnMetadata(this.workspace, typeof propKey === 'string' ? proto : constructor, propKey);
|
|
501
501
|
if (ownMeta && propKey === undefined && ownMeta.params === undefined) {
|
|
@@ -606,469 +606,7 @@ const defaultLevels = [
|
|
|
606
606
|
const defaultMappedLevels = new Map();
|
|
607
607
|
defaultLevels.forEach((type, level) => defaultMappedLevels.set(type, level));
|
|
608
608
|
|
|
609
|
-
|
|
610
|
-
Object.defineProperty(target, name || 'value', {
|
|
611
|
-
get: opts.get,
|
|
612
|
-
set: opts.set,
|
|
613
|
-
});
|
|
614
|
-
return target;
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
const asyncStorage = new node_async_hooks.AsyncLocalStorage();
|
|
618
|
-
function useAsyncEventContext(expectedTypes) {
|
|
619
|
-
let cc = asyncStorage.getStore();
|
|
620
|
-
if (!cc) {
|
|
621
|
-
throw new Error('Event context does not exist at this point.');
|
|
622
|
-
}
|
|
623
|
-
return _getCtxHelpers(cc);
|
|
624
|
-
}
|
|
625
|
-
function _getCtxHelpers(cc) {
|
|
626
|
-
function store(key) {
|
|
627
|
-
const obj = {
|
|
628
|
-
value: null,
|
|
629
|
-
hook,
|
|
630
|
-
init,
|
|
631
|
-
set: setNested,
|
|
632
|
-
get: getNested,
|
|
633
|
-
has: hasNested,
|
|
634
|
-
del: delNested,
|
|
635
|
-
entries,
|
|
636
|
-
clear,
|
|
637
|
-
};
|
|
638
|
-
attachHook(obj, {
|
|
639
|
-
set: v => {
|
|
640
|
-
set(key, v);
|
|
641
|
-
},
|
|
642
|
-
get: () => get(key),
|
|
643
|
-
});
|
|
644
|
-
function init(key2, getter) {
|
|
645
|
-
if (hasNested(key2)) {
|
|
646
|
-
return getNested(key2);
|
|
647
|
-
}
|
|
648
|
-
return setNested(key2, getter());
|
|
649
|
-
}
|
|
650
|
-
function hook(key2) {
|
|
651
|
-
const obj = {
|
|
652
|
-
value: null,
|
|
653
|
-
isDefined: null,
|
|
654
|
-
};
|
|
655
|
-
attachHook(obj, {
|
|
656
|
-
set: v => setNested(key2, v),
|
|
657
|
-
get: () => getNested(key2),
|
|
658
|
-
});
|
|
659
|
-
attachHook(obj, {
|
|
660
|
-
get: () => hasNested(key2),
|
|
661
|
-
}, 'isDefined');
|
|
662
|
-
return obj;
|
|
663
|
-
}
|
|
664
|
-
function setNested(key2, v) {
|
|
665
|
-
if (obj.value === undefined) {
|
|
666
|
-
obj.value = {};
|
|
667
|
-
}
|
|
668
|
-
obj.value[key2] = v;
|
|
669
|
-
return v;
|
|
670
|
-
}
|
|
671
|
-
function delNested(key2) {
|
|
672
|
-
setNested(key2, undefined);
|
|
673
|
-
}
|
|
674
|
-
function getNested(key2) {
|
|
675
|
-
return (obj.value || {})[key2];
|
|
676
|
-
}
|
|
677
|
-
function hasNested(key2) {
|
|
678
|
-
return (obj.value || {})[key2] !== undefined;
|
|
679
|
-
}
|
|
680
|
-
function entries() {
|
|
681
|
-
return Object.entries(obj.value || {});
|
|
682
|
-
}
|
|
683
|
-
function clear() {
|
|
684
|
-
obj.value = {};
|
|
685
|
-
}
|
|
686
|
-
return obj;
|
|
687
|
-
}
|
|
688
|
-
function getCtx() {
|
|
689
|
-
return cc;
|
|
690
|
-
}
|
|
691
|
-
function get(key) {
|
|
692
|
-
return getCtx()[key];
|
|
693
|
-
}
|
|
694
|
-
function set(key, v) {
|
|
695
|
-
getCtx()[key] = v;
|
|
696
|
-
}
|
|
697
|
-
const hasParentCtx = () => !!cc.parentCtx;
|
|
698
|
-
return {
|
|
699
|
-
getCtx,
|
|
700
|
-
store,
|
|
701
|
-
getStore: get,
|
|
702
|
-
setStore: set,
|
|
703
|
-
setParentCtx: (parentCtx) => {
|
|
704
|
-
cc.parentCtx = parentCtx;
|
|
705
|
-
},
|
|
706
|
-
hasParentCtx,
|
|
707
|
-
getParentCtx: () => {
|
|
708
|
-
if (!hasParentCtx()) {
|
|
709
|
-
throw new Error('Parent context is not available');
|
|
710
|
-
}
|
|
711
|
-
return _getCtxHelpers(cc.parentCtx);
|
|
712
|
-
},
|
|
713
|
-
};
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
function useEventId() {
|
|
717
|
-
const { store } = useAsyncEventContext();
|
|
718
|
-
const { init } = store('event');
|
|
719
|
-
const getId = () => init('id', () => crypto.randomUUID());
|
|
720
|
-
return { getId };
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
function getConstructor(instance) {
|
|
724
|
-
return Object.getPrototypeOf(instance).constructor;
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
const globalRegistry = {};
|
|
728
|
-
const UNDEFINED = Symbol('undefined');
|
|
729
|
-
class Infact {
|
|
730
|
-
constructor(options) {
|
|
731
|
-
this.options = options;
|
|
732
|
-
this.registry = {};
|
|
733
|
-
this.instanceRegistries = new WeakMap();
|
|
734
|
-
this.scopes = {};
|
|
735
|
-
this._silent = false;
|
|
736
|
-
this.logger = options.logger || console;
|
|
737
|
-
}
|
|
738
|
-
_cleanup() {
|
|
739
|
-
this.registry = {};
|
|
740
|
-
this.instanceRegistries = new WeakMap();
|
|
741
|
-
this.scopes = {};
|
|
742
|
-
}
|
|
743
|
-
setLogger(logger) {
|
|
744
|
-
this.logger = logger;
|
|
745
|
-
}
|
|
746
|
-
silent(value = 'logs') {
|
|
747
|
-
this._silent = value;
|
|
748
|
-
}
|
|
749
|
-
registerScope(scopeId) {
|
|
750
|
-
if (!this.scopes[scopeId]) {
|
|
751
|
-
this.scopes[scopeId] = {};
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
unregisterScope(scopeId) {
|
|
755
|
-
delete this.scopes[scopeId];
|
|
756
|
-
}
|
|
757
|
-
getForInstance(instance, classConstructor, opts) {
|
|
758
|
-
const registries = this.getInstanceRegistries(instance);
|
|
759
|
-
return this.get(classConstructor, {
|
|
760
|
-
...opts,
|
|
761
|
-
provide: registries.provide || {},
|
|
762
|
-
replace: registries.replace,
|
|
763
|
-
customData: registries.customData,
|
|
764
|
-
});
|
|
765
|
-
}
|
|
766
|
-
async get(classConstructor, opts, optional = false) {
|
|
767
|
-
const result = await this._get(classConstructor, opts, optional);
|
|
768
|
-
if (result) {
|
|
769
|
-
const { instance, mergedProvide, replace } = result;
|
|
770
|
-
if (this.options.storeProvideRegByInstance) {
|
|
771
|
-
this.setInstanceRegistries(instance, mergedProvide, replace, opts === null || opts === void 0 ? void 0 : opts.customData);
|
|
772
|
-
}
|
|
773
|
-
return instance;
|
|
774
|
-
}
|
|
775
|
-
return undefined;
|
|
776
|
-
}
|
|
777
|
-
setInstanceRegistries(instance, provide, replace, customData) {
|
|
778
|
-
this.instanceRegistries.set(instance, { provide, replace, customData });
|
|
779
|
-
}
|
|
780
|
-
getInstanceRegistries(instance) {
|
|
781
|
-
return this.instanceRegistries.get(instance) || {};
|
|
782
|
-
}
|
|
783
|
-
async _get(classConstructor, opts, optional) {
|
|
784
|
-
const hierarchy = (opts === null || opts === void 0 ? void 0 : opts.hierarchy) || [];
|
|
785
|
-
const provide = opts === null || opts === void 0 ? void 0 : opts.provide;
|
|
786
|
-
const replace = opts === null || opts === void 0 ? void 0 : opts.replace;
|
|
787
|
-
const syncContextFn = opts === null || opts === void 0 ? void 0 : opts.syncContextFn;
|
|
788
|
-
hierarchy.push(classConstructor.name);
|
|
789
|
-
let classMeta;
|
|
790
|
-
let instanceKey = Symbol.for(classConstructor);
|
|
791
|
-
if (replace && replace[instanceKey]) {
|
|
792
|
-
classConstructor = replace === null || replace === void 0 ? void 0 : replace[instanceKey];
|
|
793
|
-
instanceKey = Symbol.for(classConstructor);
|
|
794
|
-
}
|
|
795
|
-
try {
|
|
796
|
-
classMeta = this.options.describeClass(classConstructor);
|
|
797
|
-
}
|
|
798
|
-
catch (e) {
|
|
799
|
-
throw this.panicOwnError(`Could not instantiate "${classConstructor.name}". ` +
|
|
800
|
-
`An error occored on "describeClass" function.\n${e.message}`, hierarchy);
|
|
801
|
-
}
|
|
802
|
-
if (!classMeta || !classMeta.injectable) {
|
|
803
|
-
if (provide && provide[instanceKey]) {
|
|
804
|
-
syncContextFn && syncContextFn(classMeta);
|
|
805
|
-
return {
|
|
806
|
-
instance: await getProvidedValue(provide[instanceKey]),
|
|
807
|
-
mergedProvide: provide,
|
|
808
|
-
replace,
|
|
809
|
-
};
|
|
810
|
-
}
|
|
811
|
-
if (!optional) {
|
|
812
|
-
throw this.panicOwnError(`Could not instantiate Injectable "${classConstructor.name}". ` +
|
|
813
|
-
'Please check if the class is injectable or if you properly typed arguments.', hierarchy);
|
|
814
|
-
}
|
|
815
|
-
else {
|
|
816
|
-
return undefined;
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
if (classMeta.scopeId && classMeta.global) {
|
|
820
|
-
throw this.panicOwnError(`Could not instantiate scoped Injectable "${classConstructor.name}" for scope "${classMeta.scopeId}". ` +
|
|
821
|
-
'The scoped Injectable is not supported for Global scope.', hierarchy);
|
|
822
|
-
}
|
|
823
|
-
if (classMeta.scopeId && !this.scopes[classMeta.scopeId]) {
|
|
824
|
-
throw this.panicOwnError(`Could not instantiate scoped Injectable "${classConstructor.name}" for scope "${classMeta.scopeId}". ` +
|
|
825
|
-
'The requested scope isn\'t registered.', hierarchy);
|
|
826
|
-
}
|
|
827
|
-
const scope = classMeta.scopeId
|
|
828
|
-
? this.scopes[classMeta.scopeId]
|
|
829
|
-
: {};
|
|
830
|
-
const mergedProvide = {
|
|
831
|
-
...(provide || {}),
|
|
832
|
-
...(classMeta.provide || {}),
|
|
833
|
-
};
|
|
834
|
-
if (mergedProvide[instanceKey]) {
|
|
835
|
-
syncContextFn && syncContextFn(classMeta);
|
|
836
|
-
return {
|
|
837
|
-
instance: await getProvidedValue(mergedProvide[instanceKey]),
|
|
838
|
-
mergedProvide,
|
|
839
|
-
replace,
|
|
840
|
-
};
|
|
841
|
-
}
|
|
842
|
-
if (!this.registry[instanceKey] &&
|
|
843
|
-
!globalRegistry[instanceKey] &&
|
|
844
|
-
!scope[instanceKey]) {
|
|
845
|
-
const registry = classMeta.scopeId
|
|
846
|
-
? scope
|
|
847
|
-
: classMeta.global
|
|
848
|
-
? globalRegistry
|
|
849
|
-
: this.registry;
|
|
850
|
-
const params = classMeta.constructorParams || [];
|
|
851
|
-
const isCircular = !!params.find((p) => !!p.circular);
|
|
852
|
-
if (isCircular) {
|
|
853
|
-
registry[instanceKey] = Object.create(classConstructor.prototype);
|
|
854
|
-
}
|
|
855
|
-
const resolvedParams = [];
|
|
856
|
-
for (let i = 0; i < params.length; i++) {
|
|
857
|
-
const param = params[i];
|
|
858
|
-
if (param.inject) {
|
|
859
|
-
if (mergedProvide[param.inject]) {
|
|
860
|
-
resolvedParams[i] = getProvidedValue(mergedProvide[param.inject]);
|
|
861
|
-
}
|
|
862
|
-
else if (param.nullable || param.optional) {
|
|
863
|
-
resolvedParams[i] = UNDEFINED;
|
|
864
|
-
}
|
|
865
|
-
else {
|
|
866
|
-
throw this.panicOwnError(`Could not inject ${JSON.stringify(param.inject)} to "${classConstructor.name}" to argument ${param.label
|
|
867
|
-
? `labeled as "${param.label}"`
|
|
868
|
-
: `with index ${i}`}`, hierarchy);
|
|
869
|
-
}
|
|
870
|
-
}
|
|
871
|
-
else if (this.options.resolveParam) {
|
|
872
|
-
resolvedParams[i] = this.options.resolveParam({
|
|
873
|
-
classMeta,
|
|
874
|
-
classConstructor,
|
|
875
|
-
index: i,
|
|
876
|
-
paramMeta: param,
|
|
877
|
-
customData: opts === null || opts === void 0 ? void 0 : opts.customData,
|
|
878
|
-
});
|
|
879
|
-
}
|
|
880
|
-
}
|
|
881
|
-
for (let i = 0; i < resolvedParams.length; i++) {
|
|
882
|
-
const rp = resolvedParams[i];
|
|
883
|
-
if (rp &&
|
|
884
|
-
rp !== UNDEFINED &&
|
|
885
|
-
typeof rp.then === 'function') {
|
|
886
|
-
try {
|
|
887
|
-
syncContextFn && syncContextFn(classMeta);
|
|
888
|
-
resolvedParams[i] = await rp;
|
|
889
|
-
}
|
|
890
|
-
catch (e) {
|
|
891
|
-
const param = params[i];
|
|
892
|
-
throw this.panic(e, `Could not inject "${param.type.name}" to "${classConstructor.name}" ` +
|
|
893
|
-
`constructor at index ${i}${param.label ? ` (${param.label})` : ''}. An exception occured.`, hierarchy);
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
for (let i = 0; i < params.length; i++) {
|
|
898
|
-
const param = params[i];
|
|
899
|
-
if (typeof resolvedParams[i] === 'undefined') {
|
|
900
|
-
if (param.type === undefined && !param.circular) {
|
|
901
|
-
if (this._silent === false) {
|
|
902
|
-
this.logger.warn(`${classConstructor.name}.constructor() expects argument ${param.label
|
|
903
|
-
? `labeled as "${param.label}"`
|
|
904
|
-
: `#${i}`} that is undefined. This might happen when Circular Dependency occurs. To handle Circular Dependencies please specify circular meta for param.`);
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
else if (param.type === undefined && param.circular) {
|
|
908
|
-
param.type = param.circular();
|
|
909
|
-
}
|
|
910
|
-
if (typeof param.type === 'function') {
|
|
911
|
-
if ([String, Number, Date, Array].includes(param.type)) {
|
|
912
|
-
if (!param.nullable && !param.optional) {
|
|
913
|
-
throw this.panicOwnError(`Could not inject "${param.type
|
|
914
|
-
.name}" to "${classConstructor.name}" ` +
|
|
915
|
-
`constructor at index ${i}${param.label
|
|
916
|
-
? ` (${param.label})`
|
|
917
|
-
: ''}. The param was not resolved to a value.`, hierarchy);
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
resolvedParams[i] = this.get(param.type, {
|
|
921
|
-
provide: mergedProvide,
|
|
922
|
-
replace,
|
|
923
|
-
hierarchy,
|
|
924
|
-
syncContextFn,
|
|
925
|
-
customData: opts === null || opts === void 0 ? void 0 : opts.customData,
|
|
926
|
-
}, param.optional || param.nullable);
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
if (resolvedParams[i] === UNDEFINED) {
|
|
930
|
-
resolvedParams[i] = undefined;
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
for (let i = 0; i < resolvedParams.length; i++) {
|
|
934
|
-
const rp = resolvedParams[i];
|
|
935
|
-
if (rp && typeof rp.then === 'function') {
|
|
936
|
-
try {
|
|
937
|
-
syncContextFn && syncContextFn(classMeta);
|
|
938
|
-
resolvedParams[i] = await rp;
|
|
939
|
-
}
|
|
940
|
-
catch (e) {
|
|
941
|
-
const param = params[i];
|
|
942
|
-
throw this.panic(e, `Could not inject "${param.type.name}" to "${classConstructor.name}" ` +
|
|
943
|
-
`constructor at index ${i}${param.label ? ` (${param.label})` : ''}. An exception occured.`, hierarchy);
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
}
|
|
947
|
-
const instance = new classConstructor(...resolvedParams);
|
|
948
|
-
if (isCircular) {
|
|
949
|
-
Object.assign(registry[instanceKey], instance);
|
|
950
|
-
}
|
|
951
|
-
else {
|
|
952
|
-
registry[instanceKey] = instance;
|
|
953
|
-
}
|
|
954
|
-
if (this.options.describeProp &&
|
|
955
|
-
this.options.resolveProp &&
|
|
956
|
-
classMeta.properties &&
|
|
957
|
-
classMeta.properties.length) {
|
|
958
|
-
const resolvedProps = {};
|
|
959
|
-
for (const prop of classMeta.properties) {
|
|
960
|
-
const initialValue = instance[prop];
|
|
961
|
-
let propMeta;
|
|
962
|
-
try {
|
|
963
|
-
propMeta = this.options.describeProp(classConstructor, prop);
|
|
964
|
-
}
|
|
965
|
-
catch (e) {
|
|
966
|
-
throw this.panic(e, `Could not process prop "${prop}" of "${classConstructor.name}". ` +
|
|
967
|
-
`An error occored on "describeProp" function.\n${e.message}`, hierarchy);
|
|
968
|
-
}
|
|
969
|
-
if (propMeta) {
|
|
970
|
-
try {
|
|
971
|
-
resolvedProps[prop] = this.options.resolveProp({
|
|
972
|
-
classMeta,
|
|
973
|
-
classConstructor,
|
|
974
|
-
initialValue,
|
|
975
|
-
key: prop,
|
|
976
|
-
instance,
|
|
977
|
-
propMeta,
|
|
978
|
-
customData: opts === null || opts === void 0 ? void 0 : opts.customData,
|
|
979
|
-
});
|
|
980
|
-
}
|
|
981
|
-
catch (e) {
|
|
982
|
-
throw this.panic(e, `Could not inject prop "${prop}" to "${classConstructor.name}". ` +
|
|
983
|
-
'An exception occured: ' +
|
|
984
|
-
e.message, hierarchy);
|
|
985
|
-
}
|
|
986
|
-
}
|
|
987
|
-
}
|
|
988
|
-
for (const [prop, value] of Object.entries(resolvedProps)) {
|
|
989
|
-
try {
|
|
990
|
-
syncContextFn && syncContextFn(classMeta);
|
|
991
|
-
resolvedProps[prop] = value
|
|
992
|
-
? await value
|
|
993
|
-
: value;
|
|
994
|
-
}
|
|
995
|
-
catch (e) {
|
|
996
|
-
throw this.panic(e, `Could not inject prop "${prop}" to "${classConstructor.name}". ` +
|
|
997
|
-
'An exception occured: ' +
|
|
998
|
-
e.message, hierarchy);
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
Object.assign(instance, resolvedProps);
|
|
1002
|
-
}
|
|
1003
|
-
if (this._silent === false) {
|
|
1004
|
-
this.logger.info(`Class "${'[1m' +
|
|
1005
|
-
classConstructor.name +
|
|
1006
|
-
'[22m' +
|
|
1007
|
-
'[2m'}" instantiated with: ${'[34m'}[${resolvedParams
|
|
1008
|
-
.map((p) => {
|
|
1009
|
-
switch (typeof p) {
|
|
1010
|
-
case 'number':
|
|
1011
|
-
case 'boolean':
|
|
1012
|
-
return p;
|
|
1013
|
-
case 'string':
|
|
1014
|
-
return `"${'[92m'}...${'[34m'}"`;
|
|
1015
|
-
case 'object':
|
|
1016
|
-
if (getConstructor(p))
|
|
1017
|
-
return getConstructor(p).name;
|
|
1018
|
-
return '{}';
|
|
1019
|
-
default:
|
|
1020
|
-
return '*';
|
|
1021
|
-
}
|
|
1022
|
-
})
|
|
1023
|
-
.join(', ')}]`);
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
hierarchy.pop();
|
|
1027
|
-
syncContextFn && syncContextFn(classMeta);
|
|
1028
|
-
return {
|
|
1029
|
-
instance: await (scope[instanceKey] ||
|
|
1030
|
-
this.registry[instanceKey] ||
|
|
1031
|
-
globalRegistry[instanceKey]),
|
|
1032
|
-
mergedProvide,
|
|
1033
|
-
replace,
|
|
1034
|
-
};
|
|
1035
|
-
}
|
|
1036
|
-
panic(origError, text, hierarchy) {
|
|
1037
|
-
if (this._silent === true) ;
|
|
1038
|
-
else {
|
|
1039
|
-
this.logger.error(text +
|
|
1040
|
-
(hierarchy
|
|
1041
|
-
? '\nHierarchy:\n' + hierarchy.join(' -> ')
|
|
1042
|
-
: ''));
|
|
1043
|
-
}
|
|
1044
|
-
return origError;
|
|
1045
|
-
}
|
|
1046
|
-
panicOwnError(text, hierarchy) {
|
|
1047
|
-
const e = new Error(text + (hierarchy ? '\nHierarchy:\n' + hierarchy.join(' -> ') : ''));
|
|
1048
|
-
if (this._silent === true) {
|
|
1049
|
-
return e;
|
|
1050
|
-
}
|
|
1051
|
-
else {
|
|
1052
|
-
this.logger.error(e);
|
|
1053
|
-
return e;
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
}
|
|
1057
|
-
function getProvidedValue(meta) {
|
|
1058
|
-
if (!meta.resolved) {
|
|
1059
|
-
meta.resolved = true;
|
|
1060
|
-
meta.value = meta.fn();
|
|
1061
|
-
}
|
|
1062
|
-
return meta.value;
|
|
1063
|
-
}
|
|
1064
|
-
|
|
1065
|
-
async function runPipes(pipes, initialValue, metas, level) {
|
|
1066
|
-
let v = initialValue;
|
|
1067
|
-
for (const pipe of pipes) {
|
|
1068
|
-
v = await pipe.handler(v, metas, level);
|
|
1069
|
-
}
|
|
1070
|
-
return v;
|
|
1071
|
-
}
|
|
609
|
+
new node_async_hooks.AsyncLocalStorage();
|
|
1072
610
|
|
|
1073
611
|
const METADATA_WORKSPACE = 'moost';
|
|
1074
612
|
const moostMate = new Mate(METADATA_WORKSPACE, {
|
|
@@ -1089,52 +627,6 @@ function getMoostMate() {
|
|
|
1089
627
|
return moostMate;
|
|
1090
628
|
}
|
|
1091
629
|
|
|
1092
|
-
getNewMoostInfact();
|
|
1093
|
-
function getNewMoostInfact() {
|
|
1094
|
-
return new Infact({
|
|
1095
|
-
describeClass(classConstructor) {
|
|
1096
|
-
const meta = getMoostMate().read(classConstructor);
|
|
1097
|
-
return {
|
|
1098
|
-
injectable: !!meta?.injectable,
|
|
1099
|
-
global: false,
|
|
1100
|
-
constructorParams: meta?.params || [],
|
|
1101
|
-
provide: meta?.provide,
|
|
1102
|
-
properties: meta?.properties || [],
|
|
1103
|
-
scopeId: meta?.injectable === 'FOR_EVENT' ? useEventId().getId() : undefined,
|
|
1104
|
-
};
|
|
1105
|
-
},
|
|
1106
|
-
resolveParam({ paramMeta, classMeta, customData, classConstructor, index }) {
|
|
1107
|
-
if (paramMeta && customData?.pipes) {
|
|
1108
|
-
return runPipes(customData.pipes, undefined, {
|
|
1109
|
-
paramMeta,
|
|
1110
|
-
type: classConstructor,
|
|
1111
|
-
key: 'constructor',
|
|
1112
|
-
classMeta: classMeta,
|
|
1113
|
-
index,
|
|
1114
|
-
targetMeta: paramMeta,
|
|
1115
|
-
}, 'PARAM');
|
|
1116
|
-
}
|
|
1117
|
-
},
|
|
1118
|
-
describeProp(classConstructor, key) {
|
|
1119
|
-
const meta = getMoostMate().read(classConstructor, key);
|
|
1120
|
-
return meta;
|
|
1121
|
-
},
|
|
1122
|
-
resolveProp({ instance, key, initialValue, propMeta, classMeta, customData, classConstructor, }) {
|
|
1123
|
-
if (propMeta && customData?.pipes) {
|
|
1124
|
-
return runPipes(customData.pipes, initialValue, {
|
|
1125
|
-
instance,
|
|
1126
|
-
type: classConstructor,
|
|
1127
|
-
key,
|
|
1128
|
-
propMeta,
|
|
1129
|
-
targetMeta: propMeta,
|
|
1130
|
-
classMeta: classMeta,
|
|
1131
|
-
}, 'PROP');
|
|
1132
|
-
}
|
|
1133
|
-
},
|
|
1134
|
-
storeProvideRegByInstance: true,
|
|
1135
|
-
});
|
|
1136
|
-
}
|
|
1137
|
-
|
|
1138
630
|
getMoostMate().decorate(meta => {
|
|
1139
631
|
if (!meta.injectable) {
|
|
1140
632
|
meta.injectable = true;
|
package/dist/index.d.ts
CHANGED
|
@@ -146,6 +146,7 @@ interface TMoostMetadata<H extends TObject = TEmpty> extends TCommonMetaFields,
|
|
|
146
146
|
returnType?: TFunction;
|
|
147
147
|
provide?: TProvideRegistry;
|
|
148
148
|
replace?: TReplaceRegistry;
|
|
149
|
+
loggerTopic?: string;
|
|
149
150
|
params: Array<TMateParamMeta & TMoostParamsMetadata>;
|
|
150
151
|
}
|
|
151
152
|
interface TMoostParamsMetadata extends TCommonMetaFields, TCommonMoostMeta {
|
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { trace, context, metrics, SpanStatusCode, SpanKind } from '@opentelemetry/api';
|
|
2
|
-
import { useAsyncEventContext
|
|
3
|
-
import
|
|
2
|
+
import { useAsyncEventContext, ContextInjector, useControllerContext, getConstructor as getConstructor$1, replaceContextInjector } from 'moost';
|
|
3
|
+
import 'crypto';
|
|
4
4
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
5
5
|
import { BatchSpanProcessor, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
|
|
6
6
|
|
|
7
7
|
const spanStack = [];
|
|
8
8
|
function useOtelContext() {
|
|
9
|
-
const eventContext = useAsyncEventContext
|
|
9
|
+
const eventContext = useAsyncEventContext();
|
|
10
10
|
const store = eventContext.store('otel');
|
|
11
11
|
const csa = eventContext.store('customSpanAttrs');
|
|
12
12
|
const cma = eventContext.store('customMetricAttrs');
|
|
@@ -209,7 +209,7 @@ class SpanInjector extends ContextInjector {
|
|
|
209
209
|
return cb();
|
|
210
210
|
}
|
|
211
211
|
getEventType() {
|
|
212
|
-
return useAsyncEventContext
|
|
212
|
+
return useAsyncEventContext().getCtx().event.type;
|
|
213
213
|
}
|
|
214
214
|
getIgnoreSpan() {
|
|
215
215
|
const { getMethodMeta, getControllerMeta } = useControllerContext();
|
|
@@ -227,7 +227,7 @@ class SpanInjector extends ContextInjector {
|
|
|
227
227
|
ignoreMeter: cMeta?.otelIgnoreMeter || mMeta?.otelIgnoreMeter,
|
|
228
228
|
ignoreSpan: cMeta?.otelIgnoreSpan || mMeta?.otelIgnoreSpan,
|
|
229
229
|
attrs: {
|
|
230
|
-
'moost.controller': controller ? getConstructor$
|
|
230
|
+
'moost.controller': controller ? getConstructor$1(controller).name : undefined,
|
|
231
231
|
'moost.handler': methodName,
|
|
232
232
|
'moost.handler_description': mMeta?.description,
|
|
233
233
|
'moost.handler_label': mMeta?.label,
|
|
@@ -259,7 +259,7 @@ class SpanInjector extends ContextInjector {
|
|
|
259
259
|
this.startEventMetrics(chm.attrs, route);
|
|
260
260
|
}
|
|
261
261
|
else if (name === 'Controller:registered') {
|
|
262
|
-
const _route = useAsyncEventContext
|
|
262
|
+
const _route = useAsyncEventContext().store('otel').get('route');
|
|
263
263
|
const chm = this.getControllerHandlerMeta();
|
|
264
264
|
if (!chm.ignoreMeter) {
|
|
265
265
|
this.startEventMetrics(chm.attrs, _route);
|
|
@@ -276,7 +276,7 @@ class SpanInjector extends ContextInjector {
|
|
|
276
276
|
}
|
|
277
277
|
}
|
|
278
278
|
if (name !== 'Controller:registered') {
|
|
279
|
-
useAsyncEventContext
|
|
279
|
+
useAsyncEventContext().store('otel').set('route', route);
|
|
280
280
|
}
|
|
281
281
|
}
|
|
282
282
|
withSpan(span, cb, opts) {
|
|
@@ -291,7 +291,7 @@ class SpanInjector extends ContextInjector {
|
|
|
291
291
|
}
|
|
292
292
|
}
|
|
293
293
|
if (opts.endSpan) {
|
|
294
|
-
const customAttrs = useAsyncEventContext
|
|
294
|
+
const customAttrs = useAsyncEventContext().store('customSpanAttrs').value;
|
|
295
295
|
if (customAttrs) {
|
|
296
296
|
_span.setAttributes(customAttrs);
|
|
297
297
|
}
|
|
@@ -300,13 +300,13 @@ class SpanInjector extends ContextInjector {
|
|
|
300
300
|
});
|
|
301
301
|
}
|
|
302
302
|
startEventMetrics(a, route) {
|
|
303
|
-
useAsyncEventContext
|
|
303
|
+
useAsyncEventContext().store('otel').set('startTime', Date.now());
|
|
304
304
|
}
|
|
305
305
|
endEventMetrics(a, error) {
|
|
306
|
-
const otelStore = useAsyncEventContext
|
|
306
|
+
const otelStore = useAsyncEventContext().store('otel');
|
|
307
307
|
const route = otelStore.get('route');
|
|
308
308
|
const duration = Date.now() - (otelStore.get('startTime') || Date.now() - 1);
|
|
309
|
-
const customAttrs = useAsyncEventContext
|
|
309
|
+
const customAttrs = useAsyncEventContext().store('customMetricAttrs').value || {};
|
|
310
310
|
const attrs = {
|
|
311
311
|
...customAttrs,
|
|
312
312
|
route,
|
|
@@ -323,10 +323,10 @@ class SpanInjector extends ContextInjector {
|
|
|
323
323
|
this.metrics.moostEventDuration.record(duration, attrs);
|
|
324
324
|
}
|
|
325
325
|
getRequest() {
|
|
326
|
-
return useAsyncEventContext
|
|
326
|
+
return useAsyncEventContext().store('event').get('req');
|
|
327
327
|
}
|
|
328
328
|
getResponse() {
|
|
329
|
-
return useAsyncEventContext
|
|
329
|
+
return useAsyncEventContext()
|
|
330
330
|
.store('event')
|
|
331
331
|
.get('res');
|
|
332
332
|
}
|
|
@@ -336,7 +336,7 @@ function enableOtelForMoost() {
|
|
|
336
336
|
replaceContextInjector(new SpanInjector());
|
|
337
337
|
}
|
|
338
338
|
|
|
339
|
-
function getConstructor
|
|
339
|
+
function getConstructor(instance) {
|
|
340
340
|
return isConstructor(instance) ?
|
|
341
341
|
instance : instance.constructor ?
|
|
342
342
|
instance.constructor : Object.getPrototypeOf(instance).constructor;
|
|
@@ -351,7 +351,7 @@ const root = typeof global === 'object' ? global : typeof self === 'object' ? se
|
|
|
351
351
|
function getMetaObject(target, prop) {
|
|
352
352
|
const isParam = typeof prop !== 'undefined';
|
|
353
353
|
const metadata = isParam ? paramMetadata : classMetadata;
|
|
354
|
-
const targetKey = getConstructor
|
|
354
|
+
const targetKey = getConstructor(target);
|
|
355
355
|
let meta = metadata.get(targetKey);
|
|
356
356
|
if (!meta) {
|
|
357
357
|
meta = {};
|
|
@@ -493,7 +493,7 @@ class Mate {
|
|
|
493
493
|
read(target, propKey) {
|
|
494
494
|
var _a;
|
|
495
495
|
const isConstr = isConstructor(target);
|
|
496
|
-
const constructor = isConstr ? target : getConstructor
|
|
496
|
+
const constructor = isConstr ? target : getConstructor(target);
|
|
497
497
|
const proto = constructor.prototype;
|
|
498
498
|
let ownMeta = Reflect$2.getOwnMetadata(this.workspace, typeof propKey === 'string' ? proto : constructor, propKey);
|
|
499
499
|
if (ownMeta && propKey === undefined && ownMeta.params === undefined) {
|
|
@@ -604,469 +604,7 @@ const defaultLevels = [
|
|
|
604
604
|
const defaultMappedLevels = new Map();
|
|
605
605
|
defaultLevels.forEach((type, level) => defaultMappedLevels.set(type, level));
|
|
606
606
|
|
|
607
|
-
|
|
608
|
-
Object.defineProperty(target, name || 'value', {
|
|
609
|
-
get: opts.get,
|
|
610
|
-
set: opts.set,
|
|
611
|
-
});
|
|
612
|
-
return target;
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
const asyncStorage = new AsyncLocalStorage();
|
|
616
|
-
function useAsyncEventContext(expectedTypes) {
|
|
617
|
-
let cc = asyncStorage.getStore();
|
|
618
|
-
if (!cc) {
|
|
619
|
-
throw new Error('Event context does not exist at this point.');
|
|
620
|
-
}
|
|
621
|
-
return _getCtxHelpers(cc);
|
|
622
|
-
}
|
|
623
|
-
function _getCtxHelpers(cc) {
|
|
624
|
-
function store(key) {
|
|
625
|
-
const obj = {
|
|
626
|
-
value: null,
|
|
627
|
-
hook,
|
|
628
|
-
init,
|
|
629
|
-
set: setNested,
|
|
630
|
-
get: getNested,
|
|
631
|
-
has: hasNested,
|
|
632
|
-
del: delNested,
|
|
633
|
-
entries,
|
|
634
|
-
clear,
|
|
635
|
-
};
|
|
636
|
-
attachHook(obj, {
|
|
637
|
-
set: v => {
|
|
638
|
-
set(key, v);
|
|
639
|
-
},
|
|
640
|
-
get: () => get(key),
|
|
641
|
-
});
|
|
642
|
-
function init(key2, getter) {
|
|
643
|
-
if (hasNested(key2)) {
|
|
644
|
-
return getNested(key2);
|
|
645
|
-
}
|
|
646
|
-
return setNested(key2, getter());
|
|
647
|
-
}
|
|
648
|
-
function hook(key2) {
|
|
649
|
-
const obj = {
|
|
650
|
-
value: null,
|
|
651
|
-
isDefined: null,
|
|
652
|
-
};
|
|
653
|
-
attachHook(obj, {
|
|
654
|
-
set: v => setNested(key2, v),
|
|
655
|
-
get: () => getNested(key2),
|
|
656
|
-
});
|
|
657
|
-
attachHook(obj, {
|
|
658
|
-
get: () => hasNested(key2),
|
|
659
|
-
}, 'isDefined');
|
|
660
|
-
return obj;
|
|
661
|
-
}
|
|
662
|
-
function setNested(key2, v) {
|
|
663
|
-
if (obj.value === undefined) {
|
|
664
|
-
obj.value = {};
|
|
665
|
-
}
|
|
666
|
-
obj.value[key2] = v;
|
|
667
|
-
return v;
|
|
668
|
-
}
|
|
669
|
-
function delNested(key2) {
|
|
670
|
-
setNested(key2, undefined);
|
|
671
|
-
}
|
|
672
|
-
function getNested(key2) {
|
|
673
|
-
return (obj.value || {})[key2];
|
|
674
|
-
}
|
|
675
|
-
function hasNested(key2) {
|
|
676
|
-
return (obj.value || {})[key2] !== undefined;
|
|
677
|
-
}
|
|
678
|
-
function entries() {
|
|
679
|
-
return Object.entries(obj.value || {});
|
|
680
|
-
}
|
|
681
|
-
function clear() {
|
|
682
|
-
obj.value = {};
|
|
683
|
-
}
|
|
684
|
-
return obj;
|
|
685
|
-
}
|
|
686
|
-
function getCtx() {
|
|
687
|
-
return cc;
|
|
688
|
-
}
|
|
689
|
-
function get(key) {
|
|
690
|
-
return getCtx()[key];
|
|
691
|
-
}
|
|
692
|
-
function set(key, v) {
|
|
693
|
-
getCtx()[key] = v;
|
|
694
|
-
}
|
|
695
|
-
const hasParentCtx = () => !!cc.parentCtx;
|
|
696
|
-
return {
|
|
697
|
-
getCtx,
|
|
698
|
-
store,
|
|
699
|
-
getStore: get,
|
|
700
|
-
setStore: set,
|
|
701
|
-
setParentCtx: (parentCtx) => {
|
|
702
|
-
cc.parentCtx = parentCtx;
|
|
703
|
-
},
|
|
704
|
-
hasParentCtx,
|
|
705
|
-
getParentCtx: () => {
|
|
706
|
-
if (!hasParentCtx()) {
|
|
707
|
-
throw new Error('Parent context is not available');
|
|
708
|
-
}
|
|
709
|
-
return _getCtxHelpers(cc.parentCtx);
|
|
710
|
-
},
|
|
711
|
-
};
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
function useEventId() {
|
|
715
|
-
const { store } = useAsyncEventContext();
|
|
716
|
-
const { init } = store('event');
|
|
717
|
-
const getId = () => init('id', () => randomUUID());
|
|
718
|
-
return { getId };
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
function getConstructor(instance) {
|
|
722
|
-
return Object.getPrototypeOf(instance).constructor;
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
const globalRegistry = {};
|
|
726
|
-
const UNDEFINED = Symbol('undefined');
|
|
727
|
-
class Infact {
|
|
728
|
-
constructor(options) {
|
|
729
|
-
this.options = options;
|
|
730
|
-
this.registry = {};
|
|
731
|
-
this.instanceRegistries = new WeakMap();
|
|
732
|
-
this.scopes = {};
|
|
733
|
-
this._silent = false;
|
|
734
|
-
this.logger = options.logger || console;
|
|
735
|
-
}
|
|
736
|
-
_cleanup() {
|
|
737
|
-
this.registry = {};
|
|
738
|
-
this.instanceRegistries = new WeakMap();
|
|
739
|
-
this.scopes = {};
|
|
740
|
-
}
|
|
741
|
-
setLogger(logger) {
|
|
742
|
-
this.logger = logger;
|
|
743
|
-
}
|
|
744
|
-
silent(value = 'logs') {
|
|
745
|
-
this._silent = value;
|
|
746
|
-
}
|
|
747
|
-
registerScope(scopeId) {
|
|
748
|
-
if (!this.scopes[scopeId]) {
|
|
749
|
-
this.scopes[scopeId] = {};
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
unregisterScope(scopeId) {
|
|
753
|
-
delete this.scopes[scopeId];
|
|
754
|
-
}
|
|
755
|
-
getForInstance(instance, classConstructor, opts) {
|
|
756
|
-
const registries = this.getInstanceRegistries(instance);
|
|
757
|
-
return this.get(classConstructor, {
|
|
758
|
-
...opts,
|
|
759
|
-
provide: registries.provide || {},
|
|
760
|
-
replace: registries.replace,
|
|
761
|
-
customData: registries.customData,
|
|
762
|
-
});
|
|
763
|
-
}
|
|
764
|
-
async get(classConstructor, opts, optional = false) {
|
|
765
|
-
const result = await this._get(classConstructor, opts, optional);
|
|
766
|
-
if (result) {
|
|
767
|
-
const { instance, mergedProvide, replace } = result;
|
|
768
|
-
if (this.options.storeProvideRegByInstance) {
|
|
769
|
-
this.setInstanceRegistries(instance, mergedProvide, replace, opts === null || opts === void 0 ? void 0 : opts.customData);
|
|
770
|
-
}
|
|
771
|
-
return instance;
|
|
772
|
-
}
|
|
773
|
-
return undefined;
|
|
774
|
-
}
|
|
775
|
-
setInstanceRegistries(instance, provide, replace, customData) {
|
|
776
|
-
this.instanceRegistries.set(instance, { provide, replace, customData });
|
|
777
|
-
}
|
|
778
|
-
getInstanceRegistries(instance) {
|
|
779
|
-
return this.instanceRegistries.get(instance) || {};
|
|
780
|
-
}
|
|
781
|
-
async _get(classConstructor, opts, optional) {
|
|
782
|
-
const hierarchy = (opts === null || opts === void 0 ? void 0 : opts.hierarchy) || [];
|
|
783
|
-
const provide = opts === null || opts === void 0 ? void 0 : opts.provide;
|
|
784
|
-
const replace = opts === null || opts === void 0 ? void 0 : opts.replace;
|
|
785
|
-
const syncContextFn = opts === null || opts === void 0 ? void 0 : opts.syncContextFn;
|
|
786
|
-
hierarchy.push(classConstructor.name);
|
|
787
|
-
let classMeta;
|
|
788
|
-
let instanceKey = Symbol.for(classConstructor);
|
|
789
|
-
if (replace && replace[instanceKey]) {
|
|
790
|
-
classConstructor = replace === null || replace === void 0 ? void 0 : replace[instanceKey];
|
|
791
|
-
instanceKey = Symbol.for(classConstructor);
|
|
792
|
-
}
|
|
793
|
-
try {
|
|
794
|
-
classMeta = this.options.describeClass(classConstructor);
|
|
795
|
-
}
|
|
796
|
-
catch (e) {
|
|
797
|
-
throw this.panicOwnError(`Could not instantiate "${classConstructor.name}". ` +
|
|
798
|
-
`An error occored on "describeClass" function.\n${e.message}`, hierarchy);
|
|
799
|
-
}
|
|
800
|
-
if (!classMeta || !classMeta.injectable) {
|
|
801
|
-
if (provide && provide[instanceKey]) {
|
|
802
|
-
syncContextFn && syncContextFn(classMeta);
|
|
803
|
-
return {
|
|
804
|
-
instance: await getProvidedValue(provide[instanceKey]),
|
|
805
|
-
mergedProvide: provide,
|
|
806
|
-
replace,
|
|
807
|
-
};
|
|
808
|
-
}
|
|
809
|
-
if (!optional) {
|
|
810
|
-
throw this.panicOwnError(`Could not instantiate Injectable "${classConstructor.name}". ` +
|
|
811
|
-
'Please check if the class is injectable or if you properly typed arguments.', hierarchy);
|
|
812
|
-
}
|
|
813
|
-
else {
|
|
814
|
-
return undefined;
|
|
815
|
-
}
|
|
816
|
-
}
|
|
817
|
-
if (classMeta.scopeId && classMeta.global) {
|
|
818
|
-
throw this.panicOwnError(`Could not instantiate scoped Injectable "${classConstructor.name}" for scope "${classMeta.scopeId}". ` +
|
|
819
|
-
'The scoped Injectable is not supported for Global scope.', hierarchy);
|
|
820
|
-
}
|
|
821
|
-
if (classMeta.scopeId && !this.scopes[classMeta.scopeId]) {
|
|
822
|
-
throw this.panicOwnError(`Could not instantiate scoped Injectable "${classConstructor.name}" for scope "${classMeta.scopeId}". ` +
|
|
823
|
-
'The requested scope isn\'t registered.', hierarchy);
|
|
824
|
-
}
|
|
825
|
-
const scope = classMeta.scopeId
|
|
826
|
-
? this.scopes[classMeta.scopeId]
|
|
827
|
-
: {};
|
|
828
|
-
const mergedProvide = {
|
|
829
|
-
...(provide || {}),
|
|
830
|
-
...(classMeta.provide || {}),
|
|
831
|
-
};
|
|
832
|
-
if (mergedProvide[instanceKey]) {
|
|
833
|
-
syncContextFn && syncContextFn(classMeta);
|
|
834
|
-
return {
|
|
835
|
-
instance: await getProvidedValue(mergedProvide[instanceKey]),
|
|
836
|
-
mergedProvide,
|
|
837
|
-
replace,
|
|
838
|
-
};
|
|
839
|
-
}
|
|
840
|
-
if (!this.registry[instanceKey] &&
|
|
841
|
-
!globalRegistry[instanceKey] &&
|
|
842
|
-
!scope[instanceKey]) {
|
|
843
|
-
const registry = classMeta.scopeId
|
|
844
|
-
? scope
|
|
845
|
-
: classMeta.global
|
|
846
|
-
? globalRegistry
|
|
847
|
-
: this.registry;
|
|
848
|
-
const params = classMeta.constructorParams || [];
|
|
849
|
-
const isCircular = !!params.find((p) => !!p.circular);
|
|
850
|
-
if (isCircular) {
|
|
851
|
-
registry[instanceKey] = Object.create(classConstructor.prototype);
|
|
852
|
-
}
|
|
853
|
-
const resolvedParams = [];
|
|
854
|
-
for (let i = 0; i < params.length; i++) {
|
|
855
|
-
const param = params[i];
|
|
856
|
-
if (param.inject) {
|
|
857
|
-
if (mergedProvide[param.inject]) {
|
|
858
|
-
resolvedParams[i] = getProvidedValue(mergedProvide[param.inject]);
|
|
859
|
-
}
|
|
860
|
-
else if (param.nullable || param.optional) {
|
|
861
|
-
resolvedParams[i] = UNDEFINED;
|
|
862
|
-
}
|
|
863
|
-
else {
|
|
864
|
-
throw this.panicOwnError(`Could not inject ${JSON.stringify(param.inject)} to "${classConstructor.name}" to argument ${param.label
|
|
865
|
-
? `labeled as "${param.label}"`
|
|
866
|
-
: `with index ${i}`}`, hierarchy);
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
else if (this.options.resolveParam) {
|
|
870
|
-
resolvedParams[i] = this.options.resolveParam({
|
|
871
|
-
classMeta,
|
|
872
|
-
classConstructor,
|
|
873
|
-
index: i,
|
|
874
|
-
paramMeta: param,
|
|
875
|
-
customData: opts === null || opts === void 0 ? void 0 : opts.customData,
|
|
876
|
-
});
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
for (let i = 0; i < resolvedParams.length; i++) {
|
|
880
|
-
const rp = resolvedParams[i];
|
|
881
|
-
if (rp &&
|
|
882
|
-
rp !== UNDEFINED &&
|
|
883
|
-
typeof rp.then === 'function') {
|
|
884
|
-
try {
|
|
885
|
-
syncContextFn && syncContextFn(classMeta);
|
|
886
|
-
resolvedParams[i] = await rp;
|
|
887
|
-
}
|
|
888
|
-
catch (e) {
|
|
889
|
-
const param = params[i];
|
|
890
|
-
throw this.panic(e, `Could not inject "${param.type.name}" to "${classConstructor.name}" ` +
|
|
891
|
-
`constructor at index ${i}${param.label ? ` (${param.label})` : ''}. An exception occured.`, hierarchy);
|
|
892
|
-
}
|
|
893
|
-
}
|
|
894
|
-
}
|
|
895
|
-
for (let i = 0; i < params.length; i++) {
|
|
896
|
-
const param = params[i];
|
|
897
|
-
if (typeof resolvedParams[i] === 'undefined') {
|
|
898
|
-
if (param.type === undefined && !param.circular) {
|
|
899
|
-
if (this._silent === false) {
|
|
900
|
-
this.logger.warn(`${classConstructor.name}.constructor() expects argument ${param.label
|
|
901
|
-
? `labeled as "${param.label}"`
|
|
902
|
-
: `#${i}`} that is undefined. This might happen when Circular Dependency occurs. To handle Circular Dependencies please specify circular meta for param.`);
|
|
903
|
-
}
|
|
904
|
-
}
|
|
905
|
-
else if (param.type === undefined && param.circular) {
|
|
906
|
-
param.type = param.circular();
|
|
907
|
-
}
|
|
908
|
-
if (typeof param.type === 'function') {
|
|
909
|
-
if ([String, Number, Date, Array].includes(param.type)) {
|
|
910
|
-
if (!param.nullable && !param.optional) {
|
|
911
|
-
throw this.panicOwnError(`Could not inject "${param.type
|
|
912
|
-
.name}" to "${classConstructor.name}" ` +
|
|
913
|
-
`constructor at index ${i}${param.label
|
|
914
|
-
? ` (${param.label})`
|
|
915
|
-
: ''}. The param was not resolved to a value.`, hierarchy);
|
|
916
|
-
}
|
|
917
|
-
}
|
|
918
|
-
resolvedParams[i] = this.get(param.type, {
|
|
919
|
-
provide: mergedProvide,
|
|
920
|
-
replace,
|
|
921
|
-
hierarchy,
|
|
922
|
-
syncContextFn,
|
|
923
|
-
customData: opts === null || opts === void 0 ? void 0 : opts.customData,
|
|
924
|
-
}, param.optional || param.nullable);
|
|
925
|
-
}
|
|
926
|
-
}
|
|
927
|
-
if (resolvedParams[i] === UNDEFINED) {
|
|
928
|
-
resolvedParams[i] = undefined;
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
for (let i = 0; i < resolvedParams.length; i++) {
|
|
932
|
-
const rp = resolvedParams[i];
|
|
933
|
-
if (rp && typeof rp.then === 'function') {
|
|
934
|
-
try {
|
|
935
|
-
syncContextFn && syncContextFn(classMeta);
|
|
936
|
-
resolvedParams[i] = await rp;
|
|
937
|
-
}
|
|
938
|
-
catch (e) {
|
|
939
|
-
const param = params[i];
|
|
940
|
-
throw this.panic(e, `Could not inject "${param.type.name}" to "${classConstructor.name}" ` +
|
|
941
|
-
`constructor at index ${i}${param.label ? ` (${param.label})` : ''}. An exception occured.`, hierarchy);
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
}
|
|
945
|
-
const instance = new classConstructor(...resolvedParams);
|
|
946
|
-
if (isCircular) {
|
|
947
|
-
Object.assign(registry[instanceKey], instance);
|
|
948
|
-
}
|
|
949
|
-
else {
|
|
950
|
-
registry[instanceKey] = instance;
|
|
951
|
-
}
|
|
952
|
-
if (this.options.describeProp &&
|
|
953
|
-
this.options.resolveProp &&
|
|
954
|
-
classMeta.properties &&
|
|
955
|
-
classMeta.properties.length) {
|
|
956
|
-
const resolvedProps = {};
|
|
957
|
-
for (const prop of classMeta.properties) {
|
|
958
|
-
const initialValue = instance[prop];
|
|
959
|
-
let propMeta;
|
|
960
|
-
try {
|
|
961
|
-
propMeta = this.options.describeProp(classConstructor, prop);
|
|
962
|
-
}
|
|
963
|
-
catch (e) {
|
|
964
|
-
throw this.panic(e, `Could not process prop "${prop}" of "${classConstructor.name}". ` +
|
|
965
|
-
`An error occored on "describeProp" function.\n${e.message}`, hierarchy);
|
|
966
|
-
}
|
|
967
|
-
if (propMeta) {
|
|
968
|
-
try {
|
|
969
|
-
resolvedProps[prop] = this.options.resolveProp({
|
|
970
|
-
classMeta,
|
|
971
|
-
classConstructor,
|
|
972
|
-
initialValue,
|
|
973
|
-
key: prop,
|
|
974
|
-
instance,
|
|
975
|
-
propMeta,
|
|
976
|
-
customData: opts === null || opts === void 0 ? void 0 : opts.customData,
|
|
977
|
-
});
|
|
978
|
-
}
|
|
979
|
-
catch (e) {
|
|
980
|
-
throw this.panic(e, `Could not inject prop "${prop}" to "${classConstructor.name}". ` +
|
|
981
|
-
'An exception occured: ' +
|
|
982
|
-
e.message, hierarchy);
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
}
|
|
986
|
-
for (const [prop, value] of Object.entries(resolvedProps)) {
|
|
987
|
-
try {
|
|
988
|
-
syncContextFn && syncContextFn(classMeta);
|
|
989
|
-
resolvedProps[prop] = value
|
|
990
|
-
? await value
|
|
991
|
-
: value;
|
|
992
|
-
}
|
|
993
|
-
catch (e) {
|
|
994
|
-
throw this.panic(e, `Could not inject prop "${prop}" to "${classConstructor.name}". ` +
|
|
995
|
-
'An exception occured: ' +
|
|
996
|
-
e.message, hierarchy);
|
|
997
|
-
}
|
|
998
|
-
}
|
|
999
|
-
Object.assign(instance, resolvedProps);
|
|
1000
|
-
}
|
|
1001
|
-
if (this._silent === false) {
|
|
1002
|
-
this.logger.info(`Class "${'[1m' +
|
|
1003
|
-
classConstructor.name +
|
|
1004
|
-
'[22m' +
|
|
1005
|
-
'[2m'}" instantiated with: ${'[34m'}[${resolvedParams
|
|
1006
|
-
.map((p) => {
|
|
1007
|
-
switch (typeof p) {
|
|
1008
|
-
case 'number':
|
|
1009
|
-
case 'boolean':
|
|
1010
|
-
return p;
|
|
1011
|
-
case 'string':
|
|
1012
|
-
return `"${'[92m'}...${'[34m'}"`;
|
|
1013
|
-
case 'object':
|
|
1014
|
-
if (getConstructor(p))
|
|
1015
|
-
return getConstructor(p).name;
|
|
1016
|
-
return '{}';
|
|
1017
|
-
default:
|
|
1018
|
-
return '*';
|
|
1019
|
-
}
|
|
1020
|
-
})
|
|
1021
|
-
.join(', ')}]`);
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1024
|
-
hierarchy.pop();
|
|
1025
|
-
syncContextFn && syncContextFn(classMeta);
|
|
1026
|
-
return {
|
|
1027
|
-
instance: await (scope[instanceKey] ||
|
|
1028
|
-
this.registry[instanceKey] ||
|
|
1029
|
-
globalRegistry[instanceKey]),
|
|
1030
|
-
mergedProvide,
|
|
1031
|
-
replace,
|
|
1032
|
-
};
|
|
1033
|
-
}
|
|
1034
|
-
panic(origError, text, hierarchy) {
|
|
1035
|
-
if (this._silent === true) ;
|
|
1036
|
-
else {
|
|
1037
|
-
this.logger.error(text +
|
|
1038
|
-
(hierarchy
|
|
1039
|
-
? '\nHierarchy:\n' + hierarchy.join(' -> ')
|
|
1040
|
-
: ''));
|
|
1041
|
-
}
|
|
1042
|
-
return origError;
|
|
1043
|
-
}
|
|
1044
|
-
panicOwnError(text, hierarchy) {
|
|
1045
|
-
const e = new Error(text + (hierarchy ? '\nHierarchy:\n' + hierarchy.join(' -> ') : ''));
|
|
1046
|
-
if (this._silent === true) {
|
|
1047
|
-
return e;
|
|
1048
|
-
}
|
|
1049
|
-
else {
|
|
1050
|
-
this.logger.error(e);
|
|
1051
|
-
return e;
|
|
1052
|
-
}
|
|
1053
|
-
}
|
|
1054
|
-
}
|
|
1055
|
-
function getProvidedValue(meta) {
|
|
1056
|
-
if (!meta.resolved) {
|
|
1057
|
-
meta.resolved = true;
|
|
1058
|
-
meta.value = meta.fn();
|
|
1059
|
-
}
|
|
1060
|
-
return meta.value;
|
|
1061
|
-
}
|
|
1062
|
-
|
|
1063
|
-
async function runPipes(pipes, initialValue, metas, level) {
|
|
1064
|
-
let v = initialValue;
|
|
1065
|
-
for (const pipe of pipes) {
|
|
1066
|
-
v = await pipe.handler(v, metas, level);
|
|
1067
|
-
}
|
|
1068
|
-
return v;
|
|
1069
|
-
}
|
|
607
|
+
new AsyncLocalStorage();
|
|
1070
608
|
|
|
1071
609
|
const METADATA_WORKSPACE = 'moost';
|
|
1072
610
|
const moostMate = new Mate(METADATA_WORKSPACE, {
|
|
@@ -1087,52 +625,6 @@ function getMoostMate() {
|
|
|
1087
625
|
return moostMate;
|
|
1088
626
|
}
|
|
1089
627
|
|
|
1090
|
-
getNewMoostInfact();
|
|
1091
|
-
function getNewMoostInfact() {
|
|
1092
|
-
return new Infact({
|
|
1093
|
-
describeClass(classConstructor) {
|
|
1094
|
-
const meta = getMoostMate().read(classConstructor);
|
|
1095
|
-
return {
|
|
1096
|
-
injectable: !!meta?.injectable,
|
|
1097
|
-
global: false,
|
|
1098
|
-
constructorParams: meta?.params || [],
|
|
1099
|
-
provide: meta?.provide,
|
|
1100
|
-
properties: meta?.properties || [],
|
|
1101
|
-
scopeId: meta?.injectable === 'FOR_EVENT' ? useEventId().getId() : undefined,
|
|
1102
|
-
};
|
|
1103
|
-
},
|
|
1104
|
-
resolveParam({ paramMeta, classMeta, customData, classConstructor, index }) {
|
|
1105
|
-
if (paramMeta && customData?.pipes) {
|
|
1106
|
-
return runPipes(customData.pipes, undefined, {
|
|
1107
|
-
paramMeta,
|
|
1108
|
-
type: classConstructor,
|
|
1109
|
-
key: 'constructor',
|
|
1110
|
-
classMeta: classMeta,
|
|
1111
|
-
index,
|
|
1112
|
-
targetMeta: paramMeta,
|
|
1113
|
-
}, 'PARAM');
|
|
1114
|
-
}
|
|
1115
|
-
},
|
|
1116
|
-
describeProp(classConstructor, key) {
|
|
1117
|
-
const meta = getMoostMate().read(classConstructor, key);
|
|
1118
|
-
return meta;
|
|
1119
|
-
},
|
|
1120
|
-
resolveProp({ instance, key, initialValue, propMeta, classMeta, customData, classConstructor, }) {
|
|
1121
|
-
if (propMeta && customData?.pipes) {
|
|
1122
|
-
return runPipes(customData.pipes, initialValue, {
|
|
1123
|
-
instance,
|
|
1124
|
-
type: classConstructor,
|
|
1125
|
-
key,
|
|
1126
|
-
propMeta,
|
|
1127
|
-
targetMeta: propMeta,
|
|
1128
|
-
classMeta: classMeta,
|
|
1129
|
-
}, 'PROP');
|
|
1130
|
-
}
|
|
1131
|
-
},
|
|
1132
|
-
storeProvideRegByInstance: true,
|
|
1133
|
-
});
|
|
1134
|
-
}
|
|
1135
|
-
|
|
1136
628
|
getMoostMate().decorate(meta => {
|
|
1137
629
|
if (!meta.injectable) {
|
|
1138
630
|
meta.injectable = true;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/otel",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "@moostjs/otel",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -39,6 +39,6 @@
|
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@opentelemetry/api": "^1.9.0",
|
|
41
41
|
"@opentelemetry/sdk-trace-base": "^1.25.1",
|
|
42
|
-
"moost": "0.5.
|
|
42
|
+
"moost": "0.5.2"
|
|
43
43
|
}
|
|
44
44
|
}
|