@@ -47,6 +47,11 @@ class AngularConfig {
47
47
* @see REQUEST_CONTEXT
48
48
*/
49
49
serverBaseUrl : string = '' ;
50
+
51
+ /**
52
+ * Value of `import.meta.url` in the main entry point module (usually app.ts/server.ts).
53
+ */
54
+ moduleUrl : string = '' ;
50
55
}
51
56
52
57
/**
@@ -62,26 +67,27 @@ class AngularRequestContext {
62
67
}
63
68
}
64
69
65
- let _ngApp : AngularNodeAppEngine | undefined = undefined ;
70
+ class AngularState {
71
+ ngApp : AngularNodeAppEngine = new AngularNodeAppEngine ( )
72
+ }
66
73
67
74
class AngularListener {
68
75
constructor (
69
76
public requestContext : AngularRequestContext ,
77
+ public state : AngularState ,
70
78
) {
71
79
}
72
80
73
81
@eventDispatcher . listen ( httpWorkflow . onRoute , 102 ) //102 after 101=static listener, 100=default listener
74
82
async onRoute ( event : typeof httpWorkflow . onRoute . event ) {
75
83
if ( event . route ) return ; //already found
76
- if ( ! _ngApp ) return ;
77
84
78
- const promise = _ngApp . handle ( event . request , this . requestContext ) ;
85
+ const promise = this . state . ngApp . handle ( event . request , this . requestContext ) ;
79
86
if ( ! promise ) return ;
80
87
81
88
event . routeFound ( new RouteConfig ( 'angular' , [ 'GET' ] , event . request . url || '' , {
82
89
type : 'function' ,
83
90
fn : async ( req : HttpRequest , res : HttpResponse ) => {
84
- if ( ! _ngApp ) return ;
85
91
const response = await promise ;
86
92
if ( ! response ) {
87
93
throw new HttpNotFoundError ( ) ;
@@ -99,6 +105,12 @@ class AngularStaticListener {
99
105
public localPath : string = '' ;
100
106
public path : string = '/' ; //public path
101
107
108
+ constructor (
109
+ protected config : AngularConfig ,
110
+ ) {
111
+ this . localPath = resolve ( dirname ( fileURLToPath ( this . config . moduleUrl ) ) , '../browser' ) ;
112
+ }
113
+
102
114
@eventDispatcher . listen ( httpWorkflow . onRoute , 101 ) //after default route listener at 100
103
115
onRoute ( event : typeof httpWorkflow . onRoute . event ) {
104
116
if ( event . sent ) return ;
@@ -123,6 +135,7 @@ export class RequestHandler {
123
135
protected started : boolean = false ;
124
136
125
137
constructor (
138
+ protected config : AngularConfig ,
126
139
protected logger : Logger ,
127
140
protected requestContext : AngularRequestContext ,
128
141
protected http : HttpKernel ,
@@ -131,19 +144,17 @@ export class RequestHandler {
131
144
) {
132
145
}
133
146
134
- create ( url : string , ngApp : AngularNodeAppEngine ) : NodeRequestHandlerFunction {
135
- this . staticListener . localPath = resolve ( dirname ( fileURLToPath ( url ) ) , '../browser' ) ;
136
-
147
+ create ( ) : NodeRequestHandlerFunction {
137
148
const global = ( ( globalThis as any ) . deepkitAngular ||= { } ) as {
138
149
server ?: ApplicationServer ,
139
150
started ?: boolean ,
140
151
} ;
141
152
142
153
const waitForClose = global . server ? global . server . close ( ) : Promise . resolve ( ) ;
143
- _ngApp = ngApp ;
154
+ const mainModule = isMainModule ( this . config . moduleUrl ) ;
144
155
145
156
// We only listen for process signals in the main module.
146
- const listenOnSignals = isMainModule ( url ) ;
157
+ const listenOnSignals = mainModule ;
147
158
global . server = this . server ;
148
159
149
160
const waitBootstrap = waitForClose . then ( ( ) => this . server . start ( {
@@ -157,7 +168,7 @@ export class RequestHandler {
157
168
this . requestContext . serverBaseUrl = `http://${ host } ` ;
158
169
159
170
if ( ! this . requestContext . serverBaseUrl ) {
160
- if ( isMainModule ( url ) ) {
171
+ if ( mainModule ) {
161
172
this . requestContext . serverBaseUrl = 'http://localhost:8080' ;
162
173
} else {
163
174
//angular dev server
@@ -200,11 +211,13 @@ export class AngularModule extends createModuleClass({
200
211
RequestHandler ,
201
212
AngularRequestContext ,
202
213
AngularStaticListener ,
214
+ AngularState ,
203
215
] ,
204
216
exports : [
205
217
RequestHandler ,
206
218
AngularRequestContext ,
207
219
AngularStaticListener ,
220
+ AngularState ,
208
221
] ,
209
222
listeners : [
210
223
AngularListener ,
0 commit comments