summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomicon00 <20146907+randomicon00@users.noreply.github.com>2022-05-14 11:00:02 +0100
committerGitHub <noreply@github.com>2022-05-14 12:00:02 +0200
commitf82a79ffdbc6b8f2fc4bbe6174ad33cb22e64386 (patch)
treeb8a6021de1157fe0cb70036a9eef79a4a2d0cac1
parent20ee3110d84c72f4a28cc5c9083b5dda47181602 (diff)
feat: add userAgent property to Navigator's prototype (#14415)
-rw-r--r--cli/dts/lib.deno.window.d.ts1
-rw-r--r--cli/dts/lib.deno.worker.d.ts1
-rw-r--r--cli/main.rs4
-rw-r--r--cli/standalone.rs2
-rw-r--r--cli/tests/unit/navigator_test.ts5
-rw-r--r--runtime/examples/hello_runtime.rs2
-rw-r--r--runtime/js/99_main.js12
-rw-r--r--runtime/web_worker.rs5
-rw-r--r--runtime/worker.rs7
-rw-r--r--runtime/worker_bootstrap.rs2
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 d0e04a7a8..1600b0eac 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 e6ec9d33c..f613f2800 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 cf53cc9d3..1be8c4076 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 13bc3e70f..95b92839d 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 696b30dd4..010376273 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 a6b7fb06b..07e42f0ff 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 d83a2e6c8..cf2525b06 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 23281ad68..9582d0f7f 100644
--- a/runtime/web_worker.rs
+++ b/runtime/web_worker.rs
@@ -323,7 +323,6 @@ pub struct WebWorkerOptions {
pub extensions: Vec<Extension>,
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
pub root_cert_store: Option<RootCertStore>,
- pub user_agent: String,
pub seed: Option<u64>,
pub module_loader: Rc<dyn ModuleLoader>,
pub create_web_worker_cb: Arc<ops::worker_host::CreateWebWorkerCb>,
@@ -386,7 +385,7 @@ impl WebWorker {
Some(main_module.clone()),
),
deno_fetch::init::<Permissions>(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::<Permissions>(
- 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 7165b04f0..4c38d232f 100644
--- a/runtime/worker.rs
+++ b/runtime/worker.rs
@@ -52,7 +52,6 @@ pub struct WorkerOptions {
pub extensions: Vec<Extension>,
pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
pub root_cert_store: Option<RootCertStore>,
- pub user_agent: String,
pub seed: Option<u64>,
pub module_loader: Rc<dyn ModuleLoader>,
// Callbacks invoked when creating new instance of WebWorker
@@ -111,7 +110,7 @@ impl MainWorker {
options.bootstrap.location.clone(),
),
deno_fetch::init::<Permissions>(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::<Permissions>(
- 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 f1ffd1b3d..68f223be5 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()
}