browser.ts
utils/browser.ts
No strong subsystem tag
69
Lines
1867
Bytes
2
Exports
1
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with general runtime concerns. It contains 69 lines, 1 detected imports, and 2 detected exports.
Important relationships
Detected exports
openPathopenBrowser
Keywords
codeplatformexecfilenothrowparsedurlprotocolopenpathcommandbrowserenvcatch
Detected imports
./execFileNoThrow.js
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
import { execFileNoThrow } from './execFileNoThrow.js'
function validateUrl(url: string): void {
let parsedUrl: URL
try {
parsedUrl = new URL(url)
} catch (_error) {
throw new Error(`Invalid URL format: ${url}`)
}
// Validate URL protocol for security
if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {
throw new Error(
`Invalid URL protocol: must use http:// or https://, got ${parsedUrl.protocol}`,
)
}
}
/**
* Open a file or folder path using the system's default handler.
* Uses `open` on macOS, `explorer` on Windows, `xdg-open` on Linux.
*/
export async function openPath(path: string): Promise<boolean> {
try {
const platform = process.platform
if (platform === 'win32') {
const { code } = await execFileNoThrow('explorer', [path])
return code === 0
}
const command = platform === 'darwin' ? 'open' : 'xdg-open'
const { code } = await execFileNoThrow(command, [path])
return code === 0
} catch (_) {
return false
}
}
export async function openBrowser(url: string): Promise<boolean> {
try {
// Parse and validate the URL
validateUrl(url)
const browserEnv = process.env.BROWSER
const platform = process.platform
if (platform === 'win32') {
if (browserEnv) {
// browsers require shell, else they will treat this as a file:/// handle
const { code } = await execFileNoThrow(browserEnv, [`"${url}"`])
return code === 0
}
const { code } = await execFileNoThrow(
'rundll32',
['url,OpenURL', url],
{},
)
return code === 0
} else {
const command =
browserEnv || (platform === 'darwin' ? 'open' : 'xdg-open')
const { code } = await execFileNoThrow(command, [url])
return code === 0
}
} catch (_) {
return false
}
}