summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYoshiya Hinosawa <stibium121@gmail.com>2024-08-17 11:16:43 +0900
committerGitHub <noreply@github.com>2024-08-17 11:16:43 +0900
commitb6cdb31c0591ffd4dd9fee7a44554f5d915ba58e (patch)
treeb815eb1d3c847b1a57b04631c6e6dc246baaffaa
parent2eeea0a1d2011567d3a41065d66802097366dc8c (diff)
fix(ext/node): fix prismjs compatibiliy in Web Worker (#25062)
PrismJS uses `WorkerGlobalScope` and `self` for detecting browser's Web Worker context: https://github.com/PrismJS/prism/blob/59e5a3471377057de1f401ba38337aca27b80e03/prism.js#L11 Now the detection logic above is broken when it's imported from Deno's Web Worker context because we only hide `self` (Prism assumes when `WorkerGlobalScope` is available, `self` is also available). This change fixes the above by also hiding `WorkerGlobalScope` global in Node compat mode. closes #25008
-rw-r--r--ext/node/global.rs24
-rw-r--r--tests/registry/npm/@denotest/check-worker-globals/1.0.0/index.js7
-rw-r--r--tests/registry/npm/@denotest/check-worker-globals/1.0.0/package.json5
-rw-r--r--tests/specs/npm/workers/main.out1
-rw-r--r--tests/specs/npm/workers/main.ts3
-rw-r--r--tests/specs/npm/workers/worker4.ts4
6 files changed, 41 insertions, 3 deletions
diff --git a/ext/node/global.rs b/ext/node/global.rs
index 0fc215bd4..0b4adfc7d 100644
--- a/ext/node/global.rs
+++ b/ext/node/global.rs
@@ -63,8 +63,9 @@ const fn str_to_utf16<const N: usize>(s: &str) -> [u16; N] {
// UTF-16 encodings of the managed globals. THIS LIST MUST BE SORTED.
#[rustfmt::skip]
-const MANAGED_GLOBALS: [&[u16]; 13] = [
+const MANAGED_GLOBALS: [&[u16]; 14] = [
&str_to_utf16::<6>("Buffer"),
+ &str_to_utf16::<17>("WorkerGlobalScope"),
&str_to_utf16::<14>("clearImmediate"),
&str_to_utf16::<13>("clearInterval"),
&str_to_utf16::<12>("clearTimeout"),
@@ -79,8 +80,25 @@ const MANAGED_GLOBALS: [&[u16]; 13] = [
&str_to_utf16::<6>("window"),
];
-const SHORTEST_MANAGED_GLOBAL: usize = 4;
-const LONGEST_MANAGED_GLOBAL: usize = 14;
+// Calculates the shortest & longest length of global var names
+const MANAGED_GLOBALS_INFO: (usize, usize) = {
+ let l = MANAGED_GLOBALS[0].len();
+ let (mut longest, mut shortest, mut i) = (l, l, 1);
+ while i < MANAGED_GLOBALS.len() {
+ let l = MANAGED_GLOBALS[i].len();
+ if l > longest {
+ longest = l
+ }
+ if l < shortest {
+ shortest = l
+ }
+ i += 1;
+ }
+ (shortest, longest)
+};
+
+const SHORTEST_MANAGED_GLOBAL: usize = MANAGED_GLOBALS_INFO.0;
+const LONGEST_MANAGED_GLOBAL: usize = MANAGED_GLOBALS_INFO.1;
#[derive(Debug, Clone, Copy)]
enum Mode {
diff --git a/tests/registry/npm/@denotest/check-worker-globals/1.0.0/index.js b/tests/registry/npm/@denotest/check-worker-globals/1.0.0/index.js
new file mode 100644
index 000000000..8da68f791
--- /dev/null
+++ b/tests/registry/npm/@denotest/check-worker-globals/1.0.0/index.js
@@ -0,0 +1,7 @@
+if (typeof self !== "undefined") {
+ throw new Error("self is defined");
+}
+
+if (typeof WorkerGlobalScope !== "undefined") {
+ throw new Error("WorkerGlobalScope is defined");
+}
diff --git a/tests/registry/npm/@denotest/check-worker-globals/1.0.0/package.json b/tests/registry/npm/@denotest/check-worker-globals/1.0.0/package.json
new file mode 100644
index 000000000..b78fa210a
--- /dev/null
+++ b/tests/registry/npm/@denotest/check-worker-globals/1.0.0/package.json
@@ -0,0 +1,5 @@
+{
+ "name": "@denotest/check-worker-globals",
+ "version": "1.0.0",
+ "main": "index.js"
+}
diff --git a/tests/specs/npm/workers/main.out b/tests/specs/npm/workers/main.out
index 55ff51cd3..f1f2d6a36 100644
--- a/tests/specs/npm/workers/main.out
+++ b/tests/specs/npm/workers/main.out
@@ -2,4 +2,5 @@
1
2
3
+4
[UNORDERED_END]
diff --git a/tests/specs/npm/workers/main.ts b/tests/specs/npm/workers/main.ts
index f51cf4d90..575580790 100644
--- a/tests/specs/npm/workers/main.ts
+++ b/tests/specs/npm/workers/main.ts
@@ -7,3 +7,6 @@ new Worker(new URL("./worker2.ts", import.meta.url), {
new Worker(new URL("./worker3.ts", import.meta.url), {
type: "module",
});
+new Worker(new URL("./worker4.ts", import.meta.url), {
+ type: "module",
+});
diff --git a/tests/specs/npm/workers/worker4.ts b/tests/specs/npm/workers/worker4.ts
new file mode 100644
index 000000000..fab3f4f9a
--- /dev/null
+++ b/tests/specs/npm/workers/worker4.ts
@@ -0,0 +1,4 @@
+import "npm:@denotest/check-worker-globals";
+
+console.log(4);
+self.close();