Skip to content

Commit

Permalink
Fix duplicated check for WSL
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Sep 22, 2024
1 parent d68aae3 commit 7ece34b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 32 deletions.
7 changes: 5 additions & 2 deletions src/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ export interface BrowserOptions {
}

export abstract class Browser {
abstract kind: BrowserKind
abstract protocol: BrowserProtocol
static readonly kind: BrowserKind
static readonly protocol: BrowserProtocol

// ---

purpose: BrowserPurpose

constructor(opts: BrowserOptions) {
Expand Down
4 changes: 2 additions & 2 deletions src/browser/browsers/chrome-cdp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Browser } from '../browser'

export class ChromeCdpBrowser extends Browser {
kind = 'chrome' as const
protocol = 'cdp' as const
static readonly kind = 'chrome' as const
static readonly protocol = 'cdp' as const
}
4 changes: 2 additions & 2 deletions src/browser/browsers/chrome.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Browser } from '../browser'

export class ChromeBrowser extends Browser {
kind = 'chrome' as const
protocol = 'webdriver-bidi' as const
static readonly kind = 'chrome' as const
static readonly protocol = 'webdriver-bidi' as const
}
4 changes: 2 additions & 2 deletions src/browser/browsers/firefox.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Browser } from '../browser'

export class FirefoxBrowser extends Browser {
kind = 'firefox' as const
protocol = 'webdriver-bidi' as const
static readonly kind = 'firefox' as const
static readonly protocol = 'webdriver-bidi' as const
}
59 changes: 35 additions & 24 deletions src/utils/wsl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { execFile, spawnSync } from 'node:child_process'
import { readFileSync } from 'node:fs'
import fs from 'node:fs'
import { debug } from './debug'

let isWsl: number | undefined
let isWsl: number | Promise<number> | undefined

export const resolveWSLPathToHost = async (path: string): Promise<string> =>
await new Promise<string>((res, rej) => {
Expand Down Expand Up @@ -30,31 +31,41 @@ export const resolveWindowsEnvSync = (key: string): string | undefined => {
return ret.startsWith(`${key}=`) ? ret.slice(key.length + 1) : undefined
}

const wsl2VerMatcher = /microsoft-standard-wsl2/i

export const isWSL = async (): Promise<number> => {
if (isWsl === undefined) {
if ((await import('is-wsl')).default) {
// Detect whether WSL version is 2
// https://github.com/microsoft/WSL/issues/4555#issuecomment-700213318
const isWSL2 = (() => {
if (process.env.WSL_DISTRO_NAME && process.env.WSL_INTEROP) return true

try {
const verStr = readFileSync('/proc/version', 'utf8').toLowerCase()
if (verStr.includes('microsoft-standard-wsl2')) return true

const gccMatched = verStr.match(/gcc[^,]+?(\d+)\.\d+\.\d+/)
if (gccMatched && Number.parseInt(gccMatched[1], 10) >= 8) return true
} catch {
// no ops
}
})()

isWsl = isWSL2 ? 2 : 1
} else {
isWsl = 0
}
isWsl = (async () => {
if ((await import('is-wsl')).default) {
// Detect whether WSL version is 2
// https://github.com/microsoft/WSL/issues/4555#issuecomment-700213318
const isWSL2 = await (async () => {
if (process.env.WSL_DISTRO_NAME && process.env.WSL_INTEROP)
return true

try {
const verStr = await fs.promises.readFile('/proc/version', 'utf8')
if (wsl2VerMatcher.test(verStr)) return true

const gccMatched = verStr.match(/gcc[^,]+?(\d+)\.\d+\.\d+/)
if (gccMatched && Number.parseInt(gccMatched[1], 10) >= 8)
return true
} catch {
// no ops
}
})()

const wslVersion = isWSL2 ? 2 : 1
debug('Detected WSL version: %s', wslVersion)

return wslVersion
} else {
return 0
}
})().then((correctedIsWsl) => (isWsl = correctedIsWsl))
}
return isWsl

return await isWsl
}

export const isChromeInWSLHost = async (chromePath: string | undefined) =>
Expand Down

0 comments on commit 7ece34b

Please sign in to comment.