@namzu/browser 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/LICENSE.md +110 -0
- package/README.md +149 -0
- package/dist/classifier.d.ts +70 -0
- package/dist/classifier.d.ts.map +1 -0
- package/dist/classifier.js +147 -0
- package/dist/classifier.js.map +1 -0
- package/dist/detect.d.ts +126 -0
- package/dist/detect.d.ts.map +1 -0
- package/dist/detect.js +282 -0
- package/dist/detect.js.map +1 -0
- package/dist/errors.d.ts +64 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +99 -0
- package/dist/errors.js.map +1 -0
- package/dist/host.d.ts +167 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +1024 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/page-scripts.d.ts +51 -0
- package/dist/page-scripts.d.ts.map +1 -0
- package/dist/page-scripts.js +123 -0
- package/dist/page-scripts.js.map +1 -0
- package/dist/policy.d.ts +61 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +150 -0
- package/dist/policy.js.map +1 -0
- package/dist/profiles.d.ts +107 -0
- package/dist/profiles.d.ts.map +1 -0
- package/dist/profiles.js +312 -0
- package/dist/profiles.js.map +1 -0
- package/dist/relay.d.ts +27 -0
- package/dist/relay.d.ts.map +1 -0
- package/dist/relay.js +69 -0
- package/dist/relay.js.map +1 -0
- package/dist/snapshot.d.ts +101 -0
- package/dist/snapshot.d.ts.map +1 -0
- package/dist/snapshot.js +265 -0
- package/dist/snapshot.js.map +1 -0
- package/dist/windows-bridge-script.d.ts +29 -0
- package/dist/windows-bridge-script.d.ts.map +1 -0
- package/dist/windows-bridge-script.js +161 -0
- package/dist/windows-bridge-script.js.map +1 -0
- package/dist/windows-bridge.d.ts +120 -0
- package/dist/windows-bridge.d.ts.map +1 -0
- package/dist/windows-bridge.js +267 -0
- package/dist/windows-bridge.js.map +1 -0
- package/dist/windows-engine.d.ts +55 -0
- package/dist/windows-engine.d.ts.map +1 -0
- package/dist/windows-engine.js +154 -0
- package/dist/windows-engine.js.map +1 -0
- package/dist/wsl.d.ts +52 -0
- package/dist/wsl.d.ts.map +1 -0
- package/dist/wsl.js +134 -0
- package/dist/wsl.js.map +1 -0
- package/package.json +74 -0
- package/src/classifier.ts +191 -0
- package/src/detect.ts +432 -0
- package/src/errors.ts +121 -0
- package/src/host.ts +1217 -0
- package/src/index.ts +79 -0
- package/src/page-scripts.ts +154 -0
- package/src/policy.ts +190 -0
- package/src/profiles.ts +373 -0
- package/src/relay.ts +101 -0
- package/src/snapshot.ts +317 -0
- package/src/windows-bridge-script.ts +160 -0
- package/src/windows-bridge.ts +358 -0
- package/src/windows-engine.ts +208 -0
- package/src/wsl.ts +155 -0
package/src/wsl.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { readdirSync, statSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* WSL facts the Windows browser engine needs: where the Windows drives are
|
|
6
|
+
* mounted, how a Windows path reads from Linux and back, which networking
|
|
7
|
+
* mode the VM runs in, and which interop socket a process without
|
|
8
|
+
* `WSL_INTEROP` (a systemd service) can use to start a Windows program.
|
|
9
|
+
*
|
|
10
|
+
* Every function here is pure over its arguments, except the two listers
|
|
11
|
+
* that read the real file system and say so in their names.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** `wslinfo --networking-mode`'s answers, plus `unknown` when nothing said. */
|
|
15
|
+
export type WslNetworkingMode = 'nat' | 'mirrored' | 'virtioproxy' | 'none' | 'unknown'
|
|
16
|
+
|
|
17
|
+
const NETWORKING_MODES: readonly WslNetworkingMode[] = ['nat', 'mirrored', 'virtioproxy', 'none']
|
|
18
|
+
|
|
19
|
+
/** The default mount root for Windows drives. */
|
|
20
|
+
export const DEFAULT_WSL_MOUNT_ROOT = '/mnt/'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The Linux path of a Windows drive path under `mountRoot` (`/mnt/` unless
|
|
24
|
+
* `/etc/wsl.conf` moved it): `C:\Users\A` → `/mnt/c/Users/A`. `undefined`
|
|
25
|
+
* for anything that is not `<letter>:\…` (a UNC path, a relative path).
|
|
26
|
+
*/
|
|
27
|
+
export function windowsPathToWsl(
|
|
28
|
+
windowsPath: string,
|
|
29
|
+
mountRoot: string = DEFAULT_WSL_MOUNT_ROOT,
|
|
30
|
+
): string | undefined {
|
|
31
|
+
const match = /^([A-Za-z]):(?:[\\/](.*))?$/.exec(windowsPath)
|
|
32
|
+
if (!match) return undefined
|
|
33
|
+
const drive = (match[1] ?? '').toLowerCase()
|
|
34
|
+
const rest = (match[2] ?? '').replace(/[\\/]+/g, '/').replace(/\/$/, '')
|
|
35
|
+
const root = mountRoot.endsWith('/') ? mountRoot : `${mountRoot}/`
|
|
36
|
+
return rest.length > 0 ? `${root}${drive}/${rest}` : `${root}${drive}`
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The Windows path of a Linux path on a mounted Windows drive:
|
|
41
|
+
* `/mnt/c/Program Files/x.exe` → `C:\Program Files\x.exe`. `undefined` for a
|
|
42
|
+
* path outside the drive mounts.
|
|
43
|
+
*/
|
|
44
|
+
export function wslPathToWindows(
|
|
45
|
+
linuxPath: string,
|
|
46
|
+
mountRoot: string = DEFAULT_WSL_MOUNT_ROOT,
|
|
47
|
+
): string | undefined {
|
|
48
|
+
const root = mountRoot.endsWith('/') ? mountRoot : `${mountRoot}/`
|
|
49
|
+
if (!linuxPath.startsWith(root)) return undefined
|
|
50
|
+
const match = /^([a-zA-Z])(?:\/(.*))?$/.exec(linuxPath.slice(root.length))
|
|
51
|
+
if (!match) return undefined
|
|
52
|
+
const drive = (match[1] ?? '').toUpperCase()
|
|
53
|
+
const rest = (match[2] ?? '').replace(/\/+/g, '\\').replace(/\\$/, '')
|
|
54
|
+
return `${drive}:\\${rest}`
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Sections of an INI file (`wsl.conf`, `.wslconfig`), keys lowercased. */
|
|
58
|
+
function parseIni(text: string): Map<string, Map<string, string>> {
|
|
59
|
+
const sections = new Map<string, Map<string, string>>()
|
|
60
|
+
let current: Map<string, string> | undefined
|
|
61
|
+
for (const raw of text.replace(/^\uFEFF/, '').split(/\r?\n/)) {
|
|
62
|
+
const line = raw.trim()
|
|
63
|
+
if (line.length === 0 || line.startsWith('#') || line.startsWith(';')) continue
|
|
64
|
+
const section = /^\[([^\]]+)\]$/.exec(line)
|
|
65
|
+
if (section) {
|
|
66
|
+
const name = (section[1] ?? '').trim().toLowerCase()
|
|
67
|
+
current = sections.get(name) ?? new Map<string, string>()
|
|
68
|
+
sections.set(name, current)
|
|
69
|
+
continue
|
|
70
|
+
}
|
|
71
|
+
const eq = line.indexOf('=')
|
|
72
|
+
if (eq <= 0 || !current) continue
|
|
73
|
+
const key = line.slice(0, eq).trim().toLowerCase()
|
|
74
|
+
let value = line
|
|
75
|
+
.slice(eq + 1)
|
|
76
|
+
.replace(/\s[#;].*$/, '')
|
|
77
|
+
.trim()
|
|
78
|
+
if (/^(["']).*\1$/.test(value)) value = value.slice(1, -1)
|
|
79
|
+
current.set(key, value)
|
|
80
|
+
}
|
|
81
|
+
return sections
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The `[automount] root` of `/etc/wsl.conf`, with a trailing slash; `/mnt/` by default. */
|
|
85
|
+
export function parseWslMountRoot(wslConf: string | undefined): string {
|
|
86
|
+
if (!wslConf) return DEFAULT_WSL_MOUNT_ROOT
|
|
87
|
+
const root = parseIni(wslConf).get('automount')?.get('root')
|
|
88
|
+
if (!root || !root.startsWith('/')) return DEFAULT_WSL_MOUNT_ROOT
|
|
89
|
+
return root.endsWith('/') ? root : `${root}/`
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** `wslinfo --networking-mode`'s output as a mode; `unknown` for anything else. */
|
|
93
|
+
export function parseNetworkingModeOutput(output: string | undefined): WslNetworkingMode {
|
|
94
|
+
const word = (output ?? '').trim().toLowerCase()
|
|
95
|
+
return (NETWORKING_MODES as readonly string[]).includes(word)
|
|
96
|
+
? (word as WslNetworkingMode)
|
|
97
|
+
: 'unknown'
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The networking mode a `.wslconfig` asks for: `[wsl2] networkingMode`, or
|
|
102
|
+
* the older `[experimental] networkingMode`. No setting is NAT, WSL's
|
|
103
|
+
* default; a file that cannot be read is `unknown`.
|
|
104
|
+
*/
|
|
105
|
+
export function parseWslConfigNetworkingMode(text: string | undefined): WslNetworkingMode {
|
|
106
|
+
if (text === undefined) return 'unknown'
|
|
107
|
+
const ini = parseIni(text)
|
|
108
|
+
const value =
|
|
109
|
+
ini.get('wsl2')?.get('networkingmode') ?? ini.get('experimental')?.get('networkingmode')
|
|
110
|
+
if (value === undefined) return 'nat'
|
|
111
|
+
return parseNetworkingModeOutput(value)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** An interop socket under `/run/WSL`, with its modification time. */
|
|
115
|
+
export interface WslInteropSocket {
|
|
116
|
+
readonly path: string
|
|
117
|
+
readonly mtimeMs: number
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export const WSL_RUN_DIR = '/run/WSL'
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The interop socket a process without `WSL_INTEROP` can use: a systemd
|
|
124
|
+
* service's, which WSL starts outside any session. `1_interop` is the one WSL
|
|
125
|
+
* links for systemd to the distro's own init, so it lives as long as the
|
|
126
|
+
* distro; failing that, the newest session's. The CLI's desktop
|
|
127
|
+
* notifications choose the same way.
|
|
128
|
+
*/
|
|
129
|
+
export function findWslInteropSocket(sockets: readonly WslInteropSocket[]): string | undefined {
|
|
130
|
+
const stable = sockets.find((socket) => socket.path === `${WSL_RUN_DIR}/1_interop`)
|
|
131
|
+
if (stable) return stable.path
|
|
132
|
+
return [...sockets].sort((a, b) => b.mtimeMs - a.mtimeMs)[0]?.path
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The interop sockets under `/run/WSL` on this machine. Reads the file system. */
|
|
136
|
+
export function listWslInteropSockets(dir: string = WSL_RUN_DIR): WslInteropSocket[] {
|
|
137
|
+
const sockets: WslInteropSocket[] = []
|
|
138
|
+
let names: string[]
|
|
139
|
+
try {
|
|
140
|
+
names = readdirSync(dir)
|
|
141
|
+
} catch {
|
|
142
|
+
return sockets
|
|
143
|
+
}
|
|
144
|
+
for (const name of names) {
|
|
145
|
+
if (!/^\d+_interop$/.test(name)) continue
|
|
146
|
+
const path = join(dir, name)
|
|
147
|
+
try {
|
|
148
|
+
const stat = statSync(path)
|
|
149
|
+
if (stat.isSocket()) sockets.push({ path, mtimeMs: stat.mtimeMs })
|
|
150
|
+
} catch {
|
|
151
|
+
// Gone between the listing and the stat: a session that just ended.
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return sockets
|
|
155
|
+
}
|