diff options
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/js/99_main.js | 49 | ||||
-rw-r--r-- | runtime/worker_bootstrap.rs | 40 |
2 files changed, 78 insertions, 11 deletions
diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index ca96e34b7..5e25a3818 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -45,6 +45,7 @@ const { PromiseResolve, SafeSet, StringPrototypeIncludes, + StringPrototypePadEnd, StringPrototypeSplit, StringPrototypeTrim, Symbol, @@ -709,8 +710,37 @@ function bootstrapMainRuntime(runtimeOptions, warmup = false) { 11: mode, 12: servePort, 13: serveHost, + 14: serveIsMain, + 15: serveWorkerCount, } = runtimeOptions; + if (mode === executionModes.serve) { + if (serveIsMain && serveWorkerCount) { + const origLog = console.log; + const origError = console.error; + const prefix = `[serve-worker-0 ]`; + console.log = (...args) => { + return origLog(prefix, ...new primordials.SafeArrayIterator(args)); + }; + console.error = (...args) => { + return origError(prefix, ...new primordials.SafeArrayIterator(args)); + }; + } else if (serveWorkerCount !== null) { + const origLog = console.log; + const origError = console.error; + const base = `serve-worker-${serveWorkerCount + 1}`; + // 15 = "serve-worker-nn".length, assuming + // serveWorkerCount < 100 + const prefix = `[${StringPrototypePadEnd(base, 15, " ")}]`; + console.log = (...args) => { + return origLog(prefix, ...new primordials.SafeArrayIterator(args)); + }; + console.error = (...args) => { + return origError(prefix, ...new primordials.SafeArrayIterator(args)); + }; + } + } + if (mode === executionModes.run || mode === executionModes.serve) { let serve = undefined; core.addMainModuleHandler((main) => { @@ -725,13 +755,16 @@ function bootstrapMainRuntime(runtimeOptions, warmup = false) { } if (mode === executionModes.serve && !serve) { - console.error( - `%cerror: %cdeno serve requires %cexport default { fetch }%c in the main module, did you mean to run \"deno run\"?`, - "color: yellow;", - "color: inherit;", - "font-weight: bold;", - "font-weight: normal;", - ); + if (serveIsMain) { + // Only error if main worker + console.error( + `%cerror: %cdeno serve requires %cexport default { fetch }%c in the main module, did you mean to run \"deno run\"?`, + "color: yellow;", + "color: inherit;", + "font-weight: bold;", + "font-weight: normal;", + ); + } return; } @@ -746,7 +779,7 @@ function bootstrapMainRuntime(runtimeOptions, warmup = false) { ); } if (mode === executionModes.serve) { - serve({ servePort, serveHost }); + serve({ servePort, serveHost, serveIsMain, serveWorkerCount }); } } }); diff --git a/runtime/worker_bootstrap.rs b/runtime/worker_bootstrap.rs index b13c3c428..afd3242e8 100644 --- a/runtime/worker_bootstrap.rs +++ b/runtime/worker_bootstrap.rs @@ -10,7 +10,6 @@ use deno_terminal::colors; /// The execution mode for this worker. Some modes may have implicit behaviour. #[derive(Copy, Clone)] -#[repr(u8)] pub enum WorkerExecutionMode { /// No special behaviour. None, @@ -28,11 +27,39 @@ pub enum WorkerExecutionMode { /// `deno bench` Bench, /// `deno serve` - Serve, + Serve { + is_main: bool, + worker_count: Option<usize>, + }, /// `deno jupyter` Jupyter, } +impl WorkerExecutionMode { + pub fn discriminant(&self) -> u8 { + match self { + WorkerExecutionMode::None => 0, + WorkerExecutionMode::Worker => 1, + WorkerExecutionMode::Run => 2, + WorkerExecutionMode::Repl => 3, + WorkerExecutionMode::Eval => 4, + WorkerExecutionMode::Test => 5, + WorkerExecutionMode::Bench => 6, + WorkerExecutionMode::Serve { .. } => 7, + WorkerExecutionMode::Jupyter => 8, + } + } + pub fn serve_info(&self) -> (Option<bool>, Option<usize>) { + match *self { + WorkerExecutionMode::Serve { + is_main, + worker_count, + } => (Some(is_main), worker_count), + _ => (None, None), + } + } +} + /// The log level to use when printing diagnostic log messages, warnings, /// or errors in the worker. /// @@ -175,6 +202,10 @@ struct BootstrapV8<'a>( u16, // serve host Option<&'a str>, + // serve is main + Option<bool>, + // serve worker count + Option<usize>, ); impl BootstrapOptions { @@ -186,6 +217,7 @@ impl BootstrapOptions { let scope = RefCell::new(scope); let ser = deno_core::serde_v8::Serializer::new(&scope); + let (serve_is_main, serve_worker_count) = self.mode.serve_info(); let bootstrap = BootstrapV8( self.location.as_ref().map(|l| l.as_str()), self.unstable, @@ -198,9 +230,11 @@ impl BootstrapOptions { self.disable_deprecated_api_warning, self.verbose_deprecated_api_warning, self.future, - self.mode as u8 as _, + self.mode.discriminant() as _, self.serve_port.unwrap_or_default(), self.serve_host.as_deref(), + serve_is_main, + serve_worker_count, ); bootstrap.serialize(ser).unwrap() |