Skip to content

Commit

Permalink
fix(runtime): navigator.userAgent in web worker (#20129)
Browse files Browse the repository at this point in the history
Fixes #20079

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
  • Loading branch information
await-ovo and bartlomieju authored Aug 15, 2023
1 parent ddbb5fd commit 41cad21
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
20 changes: 20 additions & 0 deletions cli/tests/testdata/workers/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ Deno.test({
},
});

Deno.test({
name: "worker navigator",
fn: async function () {
const workerOptions: WorkerOptions = { type: "module" };
const w = new Worker(
import.meta.resolve("./worker_navigator.ts"),
workerOptions,
);

const promise = deferred();
w.onmessage = (e) => {
promise.resolve(e.data);
};

w.postMessage("Hello, world!");
assertEquals(await promise, "string, object, string, number");
w.terminate();
},
});

Deno.test({
name: "worker fetch API",
fn: async function () {
Expand Down
11 changes: 11 additions & 0 deletions cli/tests/testdata/workers/worker_navigator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
onmessage = function () {
postMessage(
[
typeof navigator.language,
typeof navigator.languages,
typeof navigator.userAgent,
typeof navigator.hardwareConcurrency,
].join(", "),
);
close();
};
36 changes: 22 additions & 14 deletions runtime/js/98_global_scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,29 @@ ObjectDefineProperties(WorkerNavigator.prototype, {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return numCpus;
},
language: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return language;
},
},
userAgent: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return userAgent;
},
},
language: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return language;
},
languages: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return [language];
},
},
languages: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return [language];
},
},
});
Expand Down
3 changes: 2 additions & 1 deletion runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ function bootstrapWorkerRuntime(
10: pid,
11: target,
12: v8Version,
// 13: userAgent,
13: userAgent,
// 14: inspectFlag,
15: enableTestingFeaturesFlag,
} = runtimeOptions;
Expand Down Expand Up @@ -633,6 +633,7 @@ function bootstrapWorkerRuntime(
location.setLocationHref(location_);

setNumCpus(cpuCount);
setUserAgent(userAgent);
setLanguage(locale);

globalThis.pollForMessages = pollForMessages;
Expand Down
9 changes: 8 additions & 1 deletion tools/wpt/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -6177,7 +6177,14 @@
"taintEnabled",
"oscpu"
],
"navigator.any.worker.html": false,
"navigator.any.worker.html": [
"appCodeName",
"appName",
"appVersion",
"platform",
"product",
"userAgent value"
],
"per-global.window.html": false
}
}
Expand Down

0 comments on commit 41cad21

Please sign in to comment.