1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 09:10:40 -05:00

feat: add userAgent property to Navigator's prototype (#14415)

This commit is contained in:
randomicon00 2022-05-14 11:00:02 +01:00 committed by GitHub
parent 20ee3110d8
commit f82a79ffdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 29 additions and 12 deletions

View file

@ -71,6 +71,7 @@ declare class Navigator {
constructor();
readonly gpu: GPU;
readonly hardwareConcurrency: number;
readonly userAgent: string;
}
declare var navigator: Navigator;

View file

@ -51,6 +51,7 @@ declare class WorkerNavigator {
constructor();
readonly gpu: GPU;
readonly hardwareConcurrency: number;
readonly userAgent: string;
}
declare var navigator: WorkerNavigator;

View file

@ -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)),

View file

@ -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),

View file

@ -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));
});

View file

@ -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,

View file

@ -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");

View file

@ -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(),
),

View file

@ -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,

View file

@ -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()
}