@@ -7,6 +7,7 @@ use deno_core::JsRuntime;
7
7
use deno_core:: RuntimeOptions ;
8
8
use import_map:: { parse_from_json, ImportMap , ImportMapDiagnostic } ;
9
9
use log:: { debug, error, warn} ;
10
+ use std:: collections:: HashMap ;
10
11
use std:: fs;
11
12
use std:: panic;
12
13
use std:: path:: Path ;
@@ -24,6 +25,7 @@ pub mod module_loader;
24
25
pub mod net_override;
25
26
pub mod permissions;
26
27
pub mod runtime;
28
+ pub mod types;
27
29
28
30
use module_loader:: DefaultModuleLoader ;
29
31
use permissions:: Permissions ;
@@ -34,6 +36,7 @@ pub async fn serve(
34
36
worker_timeout_ms : u64 ,
35
37
no_module_cache : bool ,
36
38
import_map_path : Option < String > ,
39
+ env_vars : HashMap < String , String > ,
37
40
tcp_stream : TcpStream ,
38
41
) -> Result < ( ) , Error > {
39
42
let service_path_clone = service_path. clone ( ) ;
@@ -50,11 +53,13 @@ pub async fn serve(
50
53
worker_timeout_ms,
51
54
no_module_cache,
52
55
import_map_path,
56
+ env_vars,
53
57
tcp_stream_rx,
54
58
shutdown_tx,
55
59
)
56
60
} ) ;
57
61
62
+ // send the tcp stram to the worker
58
63
tcp_stream_tx. send ( tcp_stream) ?;
59
64
60
65
debug ! ( "js worker for {} started" , service_name) ;
@@ -94,12 +99,14 @@ fn print_import_map_diagnostics(diagnostics: &[ImportMapDiagnostic]) {
94
99
) ;
95
100
}
96
101
}
102
+
97
103
fn start_runtime (
98
104
service_path : PathBuf ,
99
105
memory_limit_mb : u64 ,
100
106
worker_timeout_ms : u64 ,
101
107
no_module_cache : bool ,
102
108
import_map_path : Option < String > ,
109
+ env_vars : HashMap < String , String > ,
103
110
tcp_stream_rx : mpsc:: UnboundedReceiver < TcpStream > ,
104
111
shutdown_tx : oneshot:: Sender < ( ) > ,
105
112
) {
@@ -114,7 +121,7 @@ fn start_runtime(
114
121
// TODO: check for other potential main paths (eg: index.js, index.tsx)
115
122
let main_module_url = base_url. join ( "index.ts" ) . unwrap ( ) ;
116
123
117
- // Note: this will load Mozilla's CAs (we may also need to support
124
+ // Note: this will load Mozilla's CAs (we may also need to support system certs)
118
125
let root_cert_store = deno_tls:: create_default_root_cert_store ( ) ;
119
126
120
127
let extensions_with_js = vec ! [
@@ -165,8 +172,8 @@ fn start_runtime(
165
172
..Default :: default ( )
166
173
} ) ;
167
174
168
- let v8_thread_safe_handle = js_runtime. v8_isolate ( ) . thread_safe_handle ( ) ;
169
175
let ( memory_limit_tx, memory_limit_rx) = mpsc:: unbounded_channel :: < u64 > ( ) ;
176
+ let ( halt_execution_tx, mut halt_execution_rx) = oneshot:: channel :: < ( ) > ( ) ;
170
177
171
178
// add a callback when a worker reaches its memory limit
172
179
js_runtime. add_near_heap_limit_callback ( move |cur, _init| {
@@ -199,9 +206,16 @@ fn start_runtime(
199
206
let op_state_rc = js_runtime. op_state ( ) ;
200
207
let mut op_state = op_state_rc. borrow_mut ( ) ;
201
208
op_state. put :: < mpsc:: UnboundedReceiver < TcpStream > > ( tcp_stream_rx) ;
209
+ op_state. put :: < types:: EnvVars > ( env_vars) ;
202
210
}
203
211
204
- start_controller_thread ( v8_thread_safe_handle, worker_timeout_ms, memory_limit_rx) ;
212
+ let v8_thread_safe_handle = js_runtime. v8_isolate ( ) . thread_safe_handle ( ) ;
213
+ start_controller_thread (
214
+ v8_thread_safe_handle,
215
+ worker_timeout_ms,
216
+ memory_limit_rx,
217
+ halt_execution_tx,
218
+ ) ;
205
219
206
220
let runtime = tokio:: runtime:: Builder :: new_current_thread ( )
207
221
. enable_all ( )
@@ -211,7 +225,15 @@ fn start_runtime(
211
225
let future = async move {
212
226
let mod_id = js_runtime. load_main_module ( & main_module_url, None ) . await ?;
213
227
let result = js_runtime. mod_evaluate ( mod_id) ;
214
- js_runtime. run_event_loop ( false ) . await ?;
228
+
229
+ tokio:: select! {
230
+ _ = js_runtime. run_event_loop( false ) => {
231
+ debug!( "event loop completed" ) ;
232
+ }
233
+ _ = & mut halt_execution_rx => {
234
+ debug!( "worker exectution halted" ) ;
235
+ }
236
+ }
215
237
216
238
result. await ?
217
239
} ;
@@ -230,6 +252,7 @@ fn start_controller_thread(
230
252
v8_thread_safe_handle : v8:: IsolateHandle ,
231
253
worker_timeout_ms : u64 ,
232
254
mut memory_limit_rx : mpsc:: UnboundedReceiver < u64 > ,
255
+ halt_execution_tx : oneshot:: Sender < ( ) > ,
233
256
) {
234
257
thread:: spawn ( move || {
235
258
let rt = tokio:: runtime:: Builder :: new_current_thread ( )
@@ -255,5 +278,9 @@ fn start_controller_thread(
255
278
} else {
256
279
debug ! ( "worker already terminated" ) ;
257
280
}
281
+
282
+ halt_execution_tx
283
+ . send ( ( ) )
284
+ . expect ( "failed to send halt execution signal" ) ;
258
285
} ) ;
259
286
}
0 commit comments