assemblerjs 1.1.13 → 1.1.15

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/index35.mjs CHANGED
@@ -1,52 +1,46 @@
1
- import { DebugLogger } from './index39.mjs';
1
+ import { HookManager } from './index34.mjs';
2
+ import { DebugLogger } from './index37.mjs';
3
+ import { setDefinitionValue } from './index30.mjs';
2
4
 
3
- function t(e) {
4
- return typeof e === 'symbol' ? 'symbol' : 'string';
5
- }
6
- class ObjectManager {
7
- use(r, o) {
8
- if (this.has(r)) {
9
- const o = typeof r === 'symbol' ? r.toString() : String(r);
10
- const s = `Object/value '${o}' is already registered (cannot register twice).`;
11
- DebugLogger.getInstance().log('error', 'Duplicate object/value registration', {
12
- identifier: o,
13
- type: t(r),
14
- error: s
15
- });
16
- throw new Error(s);
17
- }
18
- this.objects.set(r, o);
19
- return o;
20
- }
21
- has(e) {
22
- return this.objects.has(e);
23
- }
24
- require(r) {
25
- if (!this.objects.has(r)) {
26
- const o = typeof r === 'symbol' ? r.toString() : String(r);
27
- const s = `Object/value '${o}' has not been registered in the object store.`;
28
- DebugLogger.getInstance().log('error', 'Object/value not found', {
29
- identifier: o,
30
- type: t(r),
31
- error: s
32
- });
33
- throw new Error(s);
5
+ class AssemblerBuilder {
6
+ build(t, a) {
7
+ const n = DebugLogger.getInstance();
8
+ setDefinitionValue('singleton', true, t);
9
+ n.logPhaseStart('registration');
10
+ const r = this.assembler.register([
11
+ t
12
+ ]);
13
+ const i = this.assembler.injectableManager.getRegisteredIdentifiers();
14
+ n.logPhaseEnd('registration', undefined, {
15
+ registered: i
16
+ });
17
+ n.logPhaseStart('resolution');
18
+ const l = this.assembler.require(r.identifier, a);
19
+ n.logPhaseEnd('resolution');
20
+ const h = this.assembler.hookManager.getCache().find((e)=>e.instance === l);
21
+ if (!h) {
22
+ throw new Error('Root instance not found in assemblages cache.');
34
23
  }
35
- return this.objects.get(r);
36
- }
37
- addGlobal(e, t) {
38
- if (this.globals.has(e)) {
39
- throw new Error(`Global value with key '${e}' has already been registered.`);
40
- }
41
- this.globals.set(e, t);
42
- }
43
- global(e) {
44
- return this.globals.get(e);
24
+ const g = this.assembler.hookManager.getCache().indexOf(h);
25
+ this.assembler.hookManager.getCache().splice(g, 1);
26
+ n.logPhaseStart('hooks:onInit');
27
+ this.assembler.hookManager.callInitHooks(this.assembler.publicContext);
28
+ const c = a ? {
29
+ ...r.configuration,
30
+ ...a
31
+ } : r.configuration;
32
+ HookManager.callHookImmediate(l, 'onInit', this.assembler.publicContext, c);
33
+ n.logPhaseEnd('hooks:onInit');
34
+ n.logPhaseStart('hooks:onInited');
35
+ this.assembler.hookManager.callInitedHooks(this.assembler.publicContext);
36
+ HookManager.callHookImmediate(l, 'onInited', this.assembler.publicContext, c);
37
+ n.logPhaseEnd('hooks:onInited');
38
+ this.assembler.hookManager.clearCache();
39
+ return l;
45
40
  }
46
- constructor(){
47
- this.objects = new Map();
48
- this.globals = new Map();
41
+ constructor(e){
42
+ this.assembler = e;
49
43
  }
50
44
  }
51
45
 
52
- export { ObjectManager };
46
+ export { AssemblerBuilder };
package/dist/index36.js CHANGED
@@ -2,84 +2,36 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const core = require('@assemblerjs/core');
6
- const debugLogger = require('./index39.js');
7
-
8
- class HookManager {
9
- prepareInitHook(o, t) {
10
- this.initCache.push({
11
- instance: o,
12
- configuration: t
13
- });
14
- return this.initCache;
15
- }
16
- callInitHooks(o) {
17
- for (const t of this.initCache){
18
- HookManager.callHookImmediate(t.instance, 'onInit', o, t.configuration);
19
- }
20
- }
21
- callInitedHooks(o) {
22
- for (const t of [
23
- ...this.initCache
24
- ].reverse()){
25
- HookManager.callHookImmediate(t.instance, 'onInited', o, t.configuration);
26
- }
5
+ class ContextProvider {
6
+ createPublicContext() {
7
+ return {
8
+ has: this.assembler.has.bind(this.assembler),
9
+ require: this.assembler.require.bind(this.assembler),
10
+ concrete: this.assembler.concrete.bind(this.assembler),
11
+ tagged: this.assembler.tagged.bind(this.assembler),
12
+ dispose: this.assembler.dispose.bind(this.assembler),
13
+ global: this.assembler.global.bind(this.assembler),
14
+ on: this.assembler.on.bind(this.assembler),
15
+ once: this.assembler.once.bind(this.assembler),
16
+ off: this.assembler.off.bind(this.assembler),
17
+ events: this.assembler.channels
18
+ };
27
19
  }
28
- clearCache() {
29
- this.initCache.length = 0;
20
+ createPrivateContext(s) {
21
+ return {
22
+ ...s,
23
+ register: this.assembler.register.bind(this.assembler),
24
+ use: this.assembler.use.bind(this.assembler),
25
+ addGlobal: this.assembler.addGlobal.bind(this.assembler),
26
+ prepareInitHook: this.assembler.prepareInitHook.bind(this.assembler),
27
+ emit: this.assembler.emit.bind(this.assembler),
28
+ addChannels: this.assembler.addChannels.bind(this.assembler),
29
+ removeChannels: this.assembler.removeChannels.bind(this.assembler)
30
+ };
30
31
  }
31
- getCache() {
32
- return this.initCache;
33
- }
34
- constructor(){
35
- this.initCache = [];
32
+ constructor(s){
33
+ this.assembler = s;
36
34
  }
37
35
  }
38
- HookManager.callHook = (e, i, n, a)=>{
39
- const c = debugLogger.DebugLogger.getInstance();
40
- const r = c.logHook(i, e, a);
41
- return new Promise((t, c)=>{
42
- const s = e[i];
43
- if (s) {
44
- if (core.isAsync(s)) {
45
- s.bind(e)(n, a).then(()=>{
46
- if (r) r();
47
- t();
48
- }).catch((o)=>{
49
- if (r) r();
50
- c(o);
51
- });
52
- return;
53
- }
54
- try {
55
- s.bind(e)(n, a);
56
- if (r) r();
57
- t();
58
- } catch (o) {
59
- if (r) r();
60
- c(o);
61
- }
62
- } else {
63
- if (r) r();
64
- t();
65
- }
66
- });
67
- };
68
- HookManager.callHookImmediate = (e, i, n, a)=>{
69
- const c = debugLogger.DebugLogger.getInstance();
70
- const r = c.logHook(i, e, a);
71
- const s = e[i];
72
- if (s) {
73
- if (core.isAsync(s)) {
74
- s.bind(e)(n, a).catch(()=>{});
75
- if (r) r();
76
- return;
77
- }
78
- s.bind(e)(n, a);
79
- if (r) r();
80
- } else {
81
- if (r) r();
82
- }
83
- };
84
36
 
85
- exports.HookManager = HookManager;
37
+ exports.ContextProvider = ContextProvider;
package/dist/index36.mjs CHANGED
@@ -1,81 +1,33 @@
1
- import { isAsync } from '@assemblerjs/core';
2
- import { DebugLogger } from './index39.mjs';
3
-
4
- class HookManager {
5
- prepareInitHook(o, t) {
6
- this.initCache.push({
7
- instance: o,
8
- configuration: t
9
- });
10
- return this.initCache;
11
- }
12
- callInitHooks(o) {
13
- for (const t of this.initCache){
14
- HookManager.callHookImmediate(t.instance, 'onInit', o, t.configuration);
15
- }
16
- }
17
- callInitedHooks(o) {
18
- for (const t of [
19
- ...this.initCache
20
- ].reverse()){
21
- HookManager.callHookImmediate(t.instance, 'onInited', o, t.configuration);
22
- }
1
+ class ContextProvider {
2
+ createPublicContext() {
3
+ return {
4
+ has: this.assembler.has.bind(this.assembler),
5
+ require: this.assembler.require.bind(this.assembler),
6
+ concrete: this.assembler.concrete.bind(this.assembler),
7
+ tagged: this.assembler.tagged.bind(this.assembler),
8
+ dispose: this.assembler.dispose.bind(this.assembler),
9
+ global: this.assembler.global.bind(this.assembler),
10
+ on: this.assembler.on.bind(this.assembler),
11
+ once: this.assembler.once.bind(this.assembler),
12
+ off: this.assembler.off.bind(this.assembler),
13
+ events: this.assembler.channels
14
+ };
23
15
  }
24
- clearCache() {
25
- this.initCache.length = 0;
16
+ createPrivateContext(s) {
17
+ return {
18
+ ...s,
19
+ register: this.assembler.register.bind(this.assembler),
20
+ use: this.assembler.use.bind(this.assembler),
21
+ addGlobal: this.assembler.addGlobal.bind(this.assembler),
22
+ prepareInitHook: this.assembler.prepareInitHook.bind(this.assembler),
23
+ emit: this.assembler.emit.bind(this.assembler),
24
+ addChannels: this.assembler.addChannels.bind(this.assembler),
25
+ removeChannels: this.assembler.removeChannels.bind(this.assembler)
26
+ };
26
27
  }
27
- getCache() {
28
- return this.initCache;
29
- }
30
- constructor(){
31
- this.initCache = [];
28
+ constructor(s){
29
+ this.assembler = s;
32
30
  }
33
31
  }
34
- HookManager.callHook = (e, i, n, a)=>{
35
- const c = DebugLogger.getInstance();
36
- const r = c.logHook(i, e, a);
37
- return new Promise((t, c)=>{
38
- const s = e[i];
39
- if (s) {
40
- if (isAsync(s)) {
41
- s.bind(e)(n, a).then(()=>{
42
- if (r) r();
43
- t();
44
- }).catch((o)=>{
45
- if (r) r();
46
- c(o);
47
- });
48
- return;
49
- }
50
- try {
51
- s.bind(e)(n, a);
52
- if (r) r();
53
- t();
54
- } catch (o) {
55
- if (r) r();
56
- c(o);
57
- }
58
- } else {
59
- if (r) r();
60
- t();
61
- }
62
- });
63
- };
64
- HookManager.callHookImmediate = (e, i, n, a)=>{
65
- const c = DebugLogger.getInstance();
66
- const r = c.logHook(i, e, a);
67
- const s = e[i];
68
- if (s) {
69
- if (isAsync(s)) {
70
- s.bind(e)(n, a).catch(()=>{});
71
- if (r) r();
72
- return;
73
- }
74
- s.bind(e)(n, a);
75
- if (r) r();
76
- } else {
77
- if (r) r();
78
- }
79
- };
80
32
 
81
- export { HookManager };
33
+ export { ContextProvider };
package/dist/index37.js CHANGED
@@ -2,49 +2,167 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const hookManager = require('./index36.js');
6
- const debugLogger = require('./index39.js');
7
- const schema = require('./index30.js');
8
-
9
- class AssemblerBuilder {
10
- build(t, a) {
11
- const n = debugLogger.DebugLogger.getInstance();
12
- schema.setDefinitionValue('singleton', true, t);
13
- n.logPhaseStart('registration');
14
- const r = this.assembler.register([
15
- t
16
- ]);
17
- const i = this.assembler.injectableManager.getRegisteredIdentifiers();
18
- n.logPhaseEnd('registration', undefined, {
19
- registered: i
5
+ function o(o) {
6
+ if (o === undefined) return 'undefined';
7
+ if (o === null) return 'null';
8
+ if (typeof o === 'function') {
9
+ if (o.name) return o.name;
10
+ if (o.constructor?.name && o.constructor.name !== 'Function') {
11
+ return o.constructor.name;
12
+ }
13
+ return 'AnonymousFunction';
14
+ }
15
+ if (typeof o === 'object') {
16
+ if (o.name && typeof o.name === 'string') {
17
+ return o.name;
18
+ }
19
+ const e = o.constructor?.name;
20
+ if (e && e !== 'Object') {
21
+ return e;
22
+ }
23
+ return '[Object]';
24
+ }
25
+ return String(o);
26
+ }
27
+ let NoOpDebugLogger = class NoOpDebugLogger {
28
+ configure(o) {}
29
+ log(o, e, t) {}
30
+ logBuildStart(o) {}
31
+ logBuildEnd(o, e) {}
32
+ logRegistration(o) {}
33
+ logHook(o, e, t) {}
34
+ logPhaseStart(o, e) {}
35
+ logPhaseEnd(o, e, t) {}
36
+ logResolution(o, e, t) {}
37
+ logConstruction(o) {}
38
+ };
39
+ let ActiveDebugLogger = class ActiveDebugLogger {
40
+ configure(o) {
41
+ this.options = {
42
+ ...this.options,
43
+ ...o,
44
+ logPhases: {
45
+ ...this.options.logPhases,
46
+ ...o.logPhases || {}
47
+ }
48
+ };
49
+ }
50
+ logBuildStart(o) {
51
+ this.log('info', 'Build started', {
52
+ entry: o.name
53
+ });
54
+ }
55
+ logBuildEnd(o, e) {
56
+ const t = {
57
+ entry: o.name
58
+ };
59
+ if (e !== undefined) t.duration = `${e.toFixed(2)}ms`;
60
+ this.log('info', 'Build completed', t);
61
+ }
62
+ logRegistration(e) {
63
+ if (!this.shouldLog('registration')) return;
64
+ this.log('info', 'Registration', {
65
+ identifier: o(e.identifier),
66
+ isSingleton: e.isSingleton,
67
+ dependencies: e.dependencies.map((e)=>o(e)),
68
+ tags: e.tags
69
+ });
70
+ }
71
+ logHook(e, t, n) {
72
+ if (!this.shouldLog('hooks')) return;
73
+ const s = this.options.logTimings ? performance.now() : 0;
74
+ this.log('info', `Hook: ${e}`, {
75
+ target: o(t),
76
+ config: n
77
+ });
78
+ if (this.options.logTimings) {
79
+ return ()=>{
80
+ const o = performance.now() - s;
81
+ this.log('info', `Hook: ${e} completed`, {
82
+ duration: `${o.toFixed(2)}ms`
83
+ });
84
+ };
85
+ }
86
+ }
87
+ logPhaseStart(o, e) {
88
+ this.log('info', `Phase: ${o} started`, e);
89
+ }
90
+ logPhaseEnd(o, e, t) {
91
+ const n = e !== undefined ? {
92
+ duration: `${e.toFixed(2)}ms`
93
+ } : {};
94
+ if (t) {
95
+ Object.assign(n, t);
96
+ }
97
+ this.log('info', `Phase: ${o} ended`, Object.keys(n).length > 0 ? n : undefined);
98
+ }
99
+ logResolution(o, e, t) {
100
+ if (!this.shouldLog('resolution')) return;
101
+ this.log('info', `Resolving: ${o}`, {
102
+ strategy: `${e} strategy`,
103
+ cache: t ? 'hit' : 'miss'
20
104
  });
21
- n.logPhaseStart('resolution');
22
- const l = this.assembler.require(r.identifier, a);
23
- n.logPhaseEnd('resolution');
24
- const h = this.assembler.hookManager.getCache().find((e)=>e.instance === l);
25
- if (!h) {
26
- throw new Error('Root instance not found in assemblages cache.');
105
+ }
106
+ logConstruction(o) {
107
+ if (!this.shouldLog('construction')) return;
108
+ this.log('info', `Constructing: ${o}`);
109
+ }
110
+ shouldLog(o) {
111
+ return !this.options.logPhases || this.options.logPhases[o] !== false;
112
+ }
113
+ log(o, e, t) {
114
+ if (this.options.logger) {
115
+ this.options.logger(o, e, t);
116
+ } else {
117
+ const n = `[Assembler:${o}]`;
118
+ const s = this.options.useColors !== false ? this.colorize(o, n) : n;
119
+ if (t) {
120
+ console.log(`${s} ${e}`, t);
121
+ } else {
122
+ console.log(`${s} ${e}`);
123
+ }
27
124
  }
28
- const g = this.assembler.hookManager.getCache().indexOf(h);
29
- this.assembler.hookManager.getCache().splice(g, 1);
30
- n.logPhaseStart('hooks:onInit');
31
- this.assembler.hookManager.callInitHooks(this.assembler.publicContext);
32
- const c = a ? {
33
- ...r.configuration,
34
- ...a
35
- } : r.configuration;
36
- hookManager.HookManager.callHookImmediate(l, 'onInit', this.assembler.publicContext, c);
37
- n.logPhaseEnd('hooks:onInit');
38
- n.logPhaseStart('hooks:onInited');
39
- this.assembler.hookManager.callInitedHooks(this.assembler.publicContext);
40
- hookManager.HookManager.callHookImmediate(l, 'onInited', this.assembler.publicContext, c);
41
- n.logPhaseEnd('hooks:onInited');
42
- this.assembler.hookManager.clearCache();
43
- return l;
44
- }
45
- constructor(e){
46
- this.assembler = e;
125
+ }
126
+ colorize(o, e) {
127
+ const t = {
128
+ info: '\x1b[36m',
129
+ warn: '\x1b[33m',
130
+ error: '\x1b[31m',
131
+ reset: '\x1b[0m'
132
+ };
133
+ const n = t[o] || t.info;
134
+ return `${n}${e}${t.reset}`;
135
+ }
136
+ constructor(){
137
+ this.options = {
138
+ enabled: true,
139
+ logPhases: {
140
+ registration: true,
141
+ resolution: true,
142
+ construction: true,
143
+ hooks: true,
144
+ cache: true
145
+ },
146
+ logTimings: false,
147
+ logDependencyTree: true,
148
+ useColors: true
149
+ };
150
+ }
151
+ };
152
+ class DebugLogger {
153
+ static getInstance() {
154
+ return DebugLogger.instance;
155
+ }
156
+ static enable(o) {
157
+ DebugLogger.instance = new ActiveDebugLogger();
158
+ if (o) {
159
+ DebugLogger.instance.configure(o);
160
+ }
161
+ }
162
+ static disable() {
163
+ DebugLogger.instance = new NoOpDebugLogger();
47
164
  }
48
165
  }
166
+ DebugLogger.instance = new NoOpDebugLogger();
49
167
 
50
- exports.AssemblerBuilder = AssemblerBuilder;
168
+ exports.DebugLogger = DebugLogger;