1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

fix(ext/node): fix prismjs compatibiliy in Web Worker (#25062)

PrismJS uses `WorkerGlobalScope` and `self` for detecting browser's Web
Worker context:
59e5a34713/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
This commit is contained in:
Yoshiya Hinosawa 2024-08-17 11:16:43 +09:00 committed by GitHub
parent 2eeea0a1d2
commit b6cdb31c05
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 41 additions and 3 deletions

View file

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

View file

@ -0,0 +1,7 @@
if (typeof self !== "undefined") {
throw new Error("self is defined");
}
if (typeof WorkerGlobalScope !== "undefined") {
throw new Error("WorkerGlobalScope is defined");
}

View file

@ -0,0 +1,5 @@
{
"name": "@denotest/check-worker-globals",
"version": "1.0.0",
"main": "index.js"
}

View file

@ -2,4 +2,5 @@
1
2
3
4
[UNORDERED_END]

View file

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

View file

@ -0,0 +1,4 @@
import "npm:@denotest/check-worker-globals";
console.log(4);
self.close();