-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
78 lines (70 loc) · 2.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Let's check for some basic globals. If these aren't present, game over.
*/
function hasNodeGlobals() {
return typeof global === 'object' &&
typeof globalThis === 'object' &&
global === globalThis &&
global.global === global &&
globalThis.globalThis === globalThis &&
typeof process === 'object'
}
/**
* Now, we check for the Bun global object. The Bun object is
* non-configurable, so it would be very hard to get past this if trying to
* pretend to be Node.js.
*/
function hasBunGlobal() {
return typeof Bun !== 'undefined' ||
typeof global.Bun !== 'undefined'
}
/**
* We'll do the same here for Deno. The Deno object _can_ be deleted, so this
* could be faked around, so we need the subsequent test as well.
*/
function hasDenoGlobal() {
return typeof Deno === 'object' ||
typeof global.Deno === 'object'
}
/**
* V8 has "natives syntax" which allows us to do some sneaky things. We'll now
* test that those sneaky things are there and working. If it works, then
* we're definitely not in Bun, which uses JSC. If it does, we'll try to
* disable it, ruling out Deno.
*/
async function isNodeIshV8() {
let v8
try {
v8 = await import('node:v8')
} catch {
return false
}
function privateSymbol() {
try {
const sym = eval('%CreatePrivateSymbol("testing")')
const obj = {}
obj[sym] = 3
return !Reflect.ownKeys(obj).includes(sym) && obj[sym] === 3
} catch {
return false
}
}
// JSC/Bun doesn't support V8's native syntax.
if (privateSymbol()) {
// Natives syntax was on. Try turning it off. That' won't work in Deno.
v8.setFlagsFromString('--no-allow-natives-syntax')
return !privateSymbol()
}
// Deno "supports" this, but it does nothing. So if it failed before, it
// will fail again.
v8.setFlagsFromString('--allow-natives-syntax')
return privateSymbol()
}
/**
* True if we're definitely running inside Node.js.
*/
const isReallyNode = hasNodeGlobals() &&
!hasBunGlobal() &&
!hasDenoGlobal() &&
await isNodeIshV8()
export default isReallyNode;