From f82a79ffdbc6b8f2fc4bbe6174ad33cb22e64386 Mon Sep 17 00:00:00 2001 From: randomicon00 <20146907+randomicon00@users.noreply.github.com> Date: Sat, 14 May 2022 11:00:02 +0100 Subject: [PATCH] feat: add userAgent property to Navigator's prototype (#14415) --- cli/dts/lib.deno.window.d.ts | 1 + cli/dts/lib.deno.worker.d.ts | 1 + cli/main.rs | 4 ++-- cli/standalone.rs | 2 +- cli/tests/unit/navigator_test.ts | 5 +++++ runtime/examples/hello_runtime.rs | 2 +- runtime/js/99_main.js | 12 +++++++++++- runtime/web_worker.rs | 5 ++--- runtime/worker.rs | 7 +++---- runtime/worker_bootstrap.rs | 2 ++ 10 files changed, 29 insertions(+), 12 deletions(-) diff --git a/cli/dts/lib.deno.window.d.ts b/cli/dts/lib.deno.window.d.ts index d0e04a7a81..1600b0eac7 100644 --- a/cli/dts/lib.deno.window.d.ts +++ b/cli/dts/lib.deno.window.d.ts @@ -71,6 +71,7 @@ declare class Navigator { constructor(); readonly gpu: GPU; readonly hardwareConcurrency: number; + readonly userAgent: string; } declare var navigator: Navigator; diff --git a/cli/dts/lib.deno.worker.d.ts b/cli/dts/lib.deno.worker.d.ts index e6ec9d33c3..f613f28001 100644 --- a/cli/dts/lib.deno.worker.d.ts +++ b/cli/dts/lib.deno.worker.d.ts @@ -51,6 +51,7 @@ declare class WorkerNavigator { constructor(); readonly gpu: GPU; readonly hardwareConcurrency: number; + readonly userAgent: string; } declare var navigator: WorkerNavigator; diff --git a/cli/main.rs b/cli/main.rs index cf53cc9d3c..1be8c40769 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -158,6 +158,7 @@ fn create_web_worker_callback( runtime_version: version::deno(), ts_version: version::TYPESCRIPT.to_string(), unstable: ps.flags.unstable, + user_agent: version::get_user_agent(), }, extensions, unsafely_ignore_certificate_errors: ps @@ -165,7 +166,6 @@ fn create_web_worker_callback( .unsafely_ignore_certificate_errors .clone(), root_cert_store: ps.root_cert_store.clone(), - user_agent: version::get_user_agent(), seed: ps.flags.seed, module_loader, create_web_worker_cb, @@ -254,6 +254,7 @@ pub fn create_main_worker( runtime_version: version::deno(), ts_version: version::TYPESCRIPT.to_string(), unstable: ps.flags.unstable, + user_agent: version::get_user_agent(), }, extensions, unsafely_ignore_certificate_errors: ps @@ -261,7 +262,6 @@ pub fn create_main_worker( .unsafely_ignore_certificate_errors .clone(), root_cert_store: ps.root_cert_store.clone(), - user_agent: version::get_user_agent(), seed: ps.flags.seed, source_map_getter: Some(Box::new(ps.clone())), format_js_error_fn: Some(Arc::new(format_js_error)), diff --git a/cli/standalone.rs b/cli/standalone.rs index 13bc3e70ff..95b92839d3 100644 --- a/cli/standalone.rs +++ b/cli/standalone.rs @@ -286,9 +286,9 @@ pub async fn run( runtime_version: version::deno(), ts_version: version::TYPESCRIPT.to_string(), unstable: metadata.unstable, + user_agent: version::get_user_agent(), }, extensions: ops::cli_exts(ps.clone(), true), - user_agent: version::get_user_agent(), unsafely_ignore_certificate_errors: metadata .unsafely_ignore_certificate_errors, root_cert_store: Some(root_cert_store), diff --git a/cli/tests/unit/navigator_test.ts b/cli/tests/unit/navigator_test.ts index 696b30dd40..0103762734 100644 --- a/cli/tests/unit/navigator_test.ts +++ b/cli/tests/unit/navigator_test.ts @@ -4,3 +4,8 @@ import { assert } from "./test_util.ts"; Deno.test(function navigatorNumCpus() { assert(navigator.hardwareConcurrency > 0); }); + +Deno.test(function navigatorUserAgent() { + const pattern = /Deno\/\d+\.\d+\.\d+/; + assert(pattern.test(navigator.userAgent)); +}); diff --git a/runtime/examples/hello_runtime.rs b/runtime/examples/hello_runtime.rs index a6b7fb06b2..07e42f0ffa 100644 --- a/runtime/examples/hello_runtime.rs +++ b/runtime/examples/hello_runtime.rs @@ -38,11 +38,11 @@ async fn main() -> Result<(), AnyError> { runtime_version: "x".to_string(), ts_version: "x".to_string(), unstable: false, + user_agent: "hello_runtime".to_string(), }, extensions: vec![], unsafely_ignore_certificate_errors: None, root_cert_store: None, - user_agent: "hello_runtime".to_string(), seed: None, source_map_getter: None, format_js_error_fn: None, diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index d83a2e6c81..cf2525b062 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -301,7 +301,7 @@ delete Object.prototype.__proto__; const navigator = webidl.createBranded(Navigator); - let numCpus; + let numCpus, userAgent; ObjectDefineProperties(Navigator.prototype, { gpu: { @@ -320,6 +320,14 @@ delete Object.prototype.__proto__; return numCpus; }, }, + userAgent: { + configurable: true, + enumerable: true, + get() { + webidl.assertBranded(this, NavigatorPrototype); + return userAgent; + }, + }, }); const NavigatorPrototype = Navigator.prototype; @@ -575,6 +583,7 @@ delete Object.prototype.__proto__; ppid, unstableFlag, cpuCount, + userAgent: userAgentInfo, } = runtimeOptions; colors.setNoColor(noColor || !isTty); @@ -582,6 +591,7 @@ delete Object.prototype.__proto__; location.setLocationHref(locationHref); } numCpus = cpuCount; + userAgent = userAgentInfo; registerErrors(); const internalSymbol = Symbol("Deno.internal"); diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 23281ad68d..9582d0f7f8 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -323,7 +323,6 @@ pub struct WebWorkerOptions { pub extensions: Vec, pub unsafely_ignore_certificate_errors: Option>, pub root_cert_store: Option, - pub user_agent: String, pub seed: Option, pub module_loader: Rc, pub create_web_worker_cb: Arc, @@ -386,7 +385,7 @@ impl WebWorker { Some(main_module.clone()), ), deno_fetch::init::(deno_fetch::Options { - user_agent: options.user_agent.clone(), + user_agent: options.bootstrap.user_agent.clone(), root_cert_store: options.root_cert_store.clone(), unsafely_ignore_certificate_errors: options .unsafely_ignore_certificate_errors @@ -395,7 +394,7 @@ impl WebWorker { ..Default::default() }), deno_websocket::init::( - options.user_agent.clone(), + options.bootstrap.user_agent.clone(), options.root_cert_store.clone(), options.unsafely_ignore_certificate_errors.clone(), ), diff --git a/runtime/worker.rs b/runtime/worker.rs index 7165b04f07..4c38d232f5 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -52,7 +52,6 @@ pub struct WorkerOptions { pub extensions: Vec, pub unsafely_ignore_certificate_errors: Option>, pub root_cert_store: Option, - pub user_agent: String, pub seed: Option, pub module_loader: Rc, // Callbacks invoked when creating new instance of WebWorker @@ -111,7 +110,7 @@ impl MainWorker { options.bootstrap.location.clone(), ), deno_fetch::init::(deno_fetch::Options { - user_agent: options.user_agent.clone(), + user_agent: options.bootstrap.user_agent.clone(), root_cert_store: options.root_cert_store.clone(), unsafely_ignore_certificate_errors: options .unsafely_ignore_certificate_errors @@ -120,7 +119,7 @@ impl MainWorker { ..Default::default() }), deno_websocket::init::( - options.user_agent.clone(), + options.bootstrap.user_agent.clone(), options.root_cert_store.clone(), options.unsafely_ignore_certificate_errors.clone(), ), @@ -376,9 +375,9 @@ mod tests { runtime_version: "x".to_string(), ts_version: "x".to_string(), unstable: false, + user_agent: "x".to_string(), }, extensions: vec![], - user_agent: "x".to_string(), unsafely_ignore_certificate_errors: None, root_cert_store: None, seed: None, diff --git a/runtime/worker_bootstrap.rs b/runtime/worker_bootstrap.rs index f1ffd1b3db..68f223be55 100644 --- a/runtime/worker_bootstrap.rs +++ b/runtime/worker_bootstrap.rs @@ -20,6 +20,7 @@ pub struct BootstrapOptions { /// Sets `Deno.version.typescript` in JS runtime. pub ts_version: String, pub unstable: bool, + pub user_agent: String, } impl BootstrapOptions { @@ -42,6 +43,7 @@ impl BootstrapOptions { "ppid": ppid(), "target": env!("TARGET"), "v8Version": deno_core::v8_version(), + "userAgent": self.user_agent, }); serde_json::to_string_pretty(&payload).unwrap() }