Skip to content

Commit 61f4caa

Browse files
committed
refactor: enhance browser profile handling and improve browser data path resolution in NodeArweaveWallet
1 parent ce133e4 commit 61f4caa

File tree

2 files changed

+60
-54
lines changed

2 files changed

+60
-54
lines changed

src/index.ts

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -989,11 +989,9 @@ export class NodeArweaveWallet {
989989

990990
try {
991991
if (this.config.browser) {
992-
const browserName = this.getBrowserName(this.config.browser)
992+
const openOptions: any = { app: { name: this.getBrowserName(this.config.browser) } }
993993

994-
const openOptions: any = { app: { name: browserName } }
995-
996-
if (this.config.browserProfile) {
994+
if (this.config.browserProfile && this.config.browser !== 'opera') {
997995
openOptions.app.arguments = this.getBrowserProfileArgs(this.config.browser)
998996
openOptions.newInstance = true
999997
}
@@ -1016,56 +1014,67 @@ export class NodeArweaveWallet {
10161014
edge: apps.edge,
10171015
brave: apps.brave,
10181016
}
1019-
return browserMap[browserLower] || browser
1017+
return browserMap[browserLower] ?? browser
1018+
}
1019+
1020+
private getBrowserDataPath(browser: string): string | null {
1021+
const platform = process.platform
1022+
const home = homedir()
1023+
1024+
const paths: Record<string, Record<string, string>> = {
1025+
chrome: {
1026+
darwin: join(home, 'Library/Application Support/Google/Chrome'),
1027+
win32: join(home, 'AppData/Local/Google/Chrome/User Data'),
1028+
linux: join(home, '.config/google-chrome'),
1029+
},
1030+
brave: {
1031+
darwin: join(home, 'Library/Application Support/BraveSoftware/Brave-Browser'),
1032+
win32: join(home, 'AppData/Local/BraveSoftware/Brave-Browser/User Data'),
1033+
linux: join(home, '.config/BraveSoftware/Brave-Browser'),
1034+
},
1035+
edge: {
1036+
darwin: join(home, 'Library/Application Support/Microsoft Edge'),
1037+
win32: join(home, 'AppData/Local/Microsoft/Edge/User Data'),
1038+
linux: join(home, '.config/microsoft-edge'),
1039+
},
1040+
}
1041+
1042+
return paths[browser]?.[platform] ?? null
10201043
}
10211044

10221045
private resolveProfileName(browser: string, profileName: string): string {
10231046
const browserLower = browser.toLowerCase()
10241047

1025-
if (browserLower === 'chrome' || browserLower === 'edge' || browserLower === 'brave') {
1026-
try {
1027-
const platform = process.platform
1028-
const browserDataPaths: Record<string, string> = {
1029-
chrome:
1030-
platform === 'darwin'
1031-
? join(homedir(), 'Library/Application Support/Google/Chrome')
1032-
: platform === 'win32'
1033-
? join(homedir(), 'AppData/Local/Google/Chrome/User Data')
1034-
: join(homedir(), '.config/google-chrome'),
1035-
brave:
1036-
platform === 'darwin'
1037-
? join(homedir(), 'Library/Application Support/BraveSoftware/Brave-Browser')
1038-
: platform === 'win32'
1039-
? join(homedir(), 'AppData/Local/BraveSoftware/Brave-Browser/User Data')
1040-
: join(homedir(), '.config/BraveSoftware/Brave-Browser'),
1041-
edge:
1042-
platform === 'darwin'
1043-
? join(homedir(), 'Library/Application Support/Microsoft Edge')
1044-
: platform === 'win32'
1045-
? join(homedir(), 'AppData/Local/Microsoft/Edge/User Data')
1046-
: join(homedir(), '.config/microsoft-edge'),
1047-
}
1048+
// Only Chromium-based browsers support profile directory resolution
1049+
if (!['chrome', 'edge', 'brave', 'opera', 'vivaldi'].includes(browserLower)) {
1050+
return profileName
1051+
}
10481052

1049-
const basePath = browserDataPaths[browserLower]
1050-
if (basePath && existsSync(basePath)) {
1051-
const localStatePath = join(basePath, 'Local State')
1052-
if (existsSync(localStatePath)) {
1053-
const localState = JSON.parse(readFileSync(localStatePath, 'utf-8'))
1054-
const profileInfo = localState?.profile?.info_cache
1055-
1056-
if (profileInfo) {
1057-
for (const [dirName, info] of Object.entries(profileInfo)) {
1058-
const profileData = info as any
1059-
if (profileData.name === profileName || profileData.gaia_name === profileName) {
1060-
return dirName
1061-
}
1062-
}
1063-
}
1064-
}
1053+
try {
1054+
const basePath = this.getBrowserDataPath(browserLower)
1055+
if (!basePath || !existsSync(basePath)) {
1056+
return profileName
1057+
}
1058+
1059+
const localStatePath = join(basePath, 'Local State')
1060+
if (!existsSync(localStatePath)) {
1061+
return profileName
1062+
}
1063+
1064+
const localState = JSON.parse(readFileSync(localStatePath, 'utf-8'))
1065+
const profileInfo = localState?.profile?.info_cache
1066+
1067+
if (!profileInfo) return profileName
1068+
1069+
// Search for matching profile by name or gaia_name
1070+
for (const [dirName, info] of Object.entries(profileInfo)) {
1071+
const profileData = info as any
1072+
if (profileData.name === profileName || profileData.gaia_name === profileName) {
1073+
return dirName
10651074
}
1066-
} catch {
1067-
// Ignore errors
10681075
}
1076+
} catch {
1077+
// Silently fall back to original profile name on any error
10691078
}
10701079

10711080
return profileName
@@ -1079,14 +1088,12 @@ export class NodeArweaveWallet {
10791088

10801089
const resolvedProfile = this.resolveProfileName(browserLower, profile)
10811090

1082-
if (browserLower === 'chrome' || browserLower === 'edge' || browserLower === 'brave') {
1083-
return [`--profile-directory=${resolvedProfile}`]
1084-
}
1085-
1086-
if (browserLower === 'firefox') {
1091+
// Firefox uses different profile arguments
1092+
if (browserLower === 'firefox' || browserLower === 'zen') {
10871093
return ['-P', resolvedProfile]
10881094
}
10891095

1096+
// Chromium-based browsers (Chrome, Edge, Brave) and others
10901097
return [`--profile-directory=${resolvedProfile}`]
10911098
}
10921099

src/types.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,10 @@ export interface NodeArweaveWalletConfig {
138138
requestTimeout?: number
139139
/**
140140
* Browser to open (default: system default browser)
141-
* - Use browser name: 'chrome', 'firefox', 'edge', 'brave'
142-
* - Or provide full path: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
141+
* - Use browser name: 'chrome', 'firefox', 'edge', 'brave', 'safari', 'opera', 'zen', 'vivaldi' (or any other browser name)
143142
* - Or use false to disable auto-opening
144143
*/
145-
browser?: string | false
144+
browser?: 'chrome' | 'firefox' | 'edge' | 'brave' | 'safari' | 'opera' | 'zen' | 'vivaldi' | (string & {}) | false
146145
/**
147146
* Browser profile to use (optional)
148147
* - Chrome/Edge: Profile name (e.g., 'Default', 'Profile 1', 'Work')

0 commit comments

Comments
 (0)