Skip to content

Commit bb4bdf2

Browse files
committed
refactor: stabilize token service and release checks
1 parent f3057f6 commit bb4bdf2

File tree

4 files changed

+99
-74
lines changed

4 files changed

+99
-74
lines changed

.github/workflows/release.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ jobs:
3131
- name: Install dependencies
3232
run: pnpm install --frozen-lockfile
3333

34+
- name: Validate tag matches package version
35+
run: node -e "const fs=require('fs'); const pkg=JSON.parse(fs.readFileSync('package.json','utf8')); const tag=process.env.GITHUB_REF_NAME; const expected='v'+pkg.version; if(tag!==expected){console.error('Tag/version mismatch. tag='+tag+', package.json='+pkg.version); process.exit(1)}"
36+
3437
- name: Build and Publish
3538
run: pnpm run release
3639
env:

services/tokenService.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,30 @@ class TokenService {
55
constructor(options = {}) {
66
this.fsImpl = options.fsImpl || fs;
77
this.env = options.env || process.env;
8+
this.platform = options.platform || process.platform;
89
}
910

1011
loadLocalToken() {
12+
if (this.platform !== "win32") {
13+
return { token: null, error: "Unsupported platform. Local token extraction is only available on Windows." };
14+
}
15+
1116
const appdata = this.env.APPDATA;
1217
const localappdata = this.env.LOCALAPPDATA;
13-
14-
if (!appdata || !localappdata) {
15-
return { token: null, error: "Windows Environment Variables (APPDATA/LOCALAPPDATA) not found." };
18+
19+
const dirs = [];
20+
if (appdata) {
21+
dirs.push(path.join(appdata, "slobs-client", "Local Storage", "leveldb"));
22+
}
23+
if (localappdata) {
24+
dirs.push(path.join(localappdata, "Google", "Chrome", "User Data", "Default", "Local Storage", "leveldb"));
25+
dirs.push(path.join(localappdata, "BraveSoftware", "Brave-Browser", "User Data", "Default", "Local Storage", "leveldb"));
26+
dirs.push(path.join(localappdata, "Microsoft", "Edge", "User Data", "Default", "Local Storage", "leveldb"));
1627
}
1728

18-
const dirs = [
19-
path.join(appdata, "slobs-client", "Local Storage", "leveldb"),
20-
path.join(localappdata, "Google", "Chrome", "User Data", "Default", "Local Storage", "leveldb"),
21-
path.join(localappdata, "BraveSoftware", "Brave-Browser", "User Data", "Default", "Local Storage", "leveldb"),
22-
path.join(localappdata, "Microsoft", "Edge", "User Data", "Default", "Local Storage", "leveldb")
23-
];
29+
if (dirs.length === 0) {
30+
return { token: null, error: "Windows Environment Variables (APPDATA/LOCALAPPDATA) not found." };
31+
}
2432

2533
const tokenRegex = /"apiToken":"([a-f0-9]+)"/gi;
2634

@@ -36,6 +44,7 @@ class TokenService {
3644
for (const file of files) {
3745
try {
3846
const raw = this.fsImpl.readFileSync(file, "utf8").replace(/\x00/g, "");
47+
tokenRegex.lastIndex = 0;
3948
let match;
4049
let last = null;
4150
while ((match = tokenRegex.exec(raw)) !== null) {
@@ -57,4 +66,4 @@ class TokenService {
5766
}
5867
}
5968

60-
module.exports = { TokenService };
69+
module.exports = { TokenService };

src/App.jsx

Lines changed: 17 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import LiveSetup from './pages/LiveSetup'
1414
import TokenVault from './pages/TokenVault'
1515
import Pulse from './pages/Pulse'
1616
import Settings from './pages/Settings'
17+
import { useDriverReadiness } from './hooks/useDriverReadiness'
1718

1819
const CHROME_DOWNLOAD_URL = 'https://www.google.com/chrome/'
1920

@@ -123,26 +124,15 @@ const App = () => {
123124
}
124125
}, [showModal, t])
125126

126-
const bootstrapDriver = async () => {
127-
setIsLoading(true)
128-
setLoadingMessage(t('driver.preparing'))
129-
const res = await window.api.bootstrapDriver()
130-
setIsLoading(false)
131-
132-
if (res.ok) {
133-
setIsDriverMissing(false)
134-
return true
135-
}
136-
137-
if (res.code === 'CHROME_NOT_FOUND') {
138-
await showChromeMissingModal()
139-
setIsDriverMissing(true)
140-
return false
141-
}
142-
143-
pushToast(res.error || t('common.error'), 'error')
144-
return false
145-
}
127+
const ensureDriverReady = useDriverReadiness({
128+
t,
129+
showModal,
130+
showChromeMissingModal,
131+
pushToast,
132+
setIsLoading,
133+
setLoadingMessage,
134+
setIsDriverMissing
135+
})
146136

147137
const closeModal = useCallback((value) => {
148138
setModal(prev => { if (prev.resolve) prev.resolve({ value }); return { ...prev, show: false, resolve: null } })
@@ -216,26 +206,9 @@ const App = () => {
216206

217207
const loadWebToken = async (existingAccountId = null) => {
218208
pushStatus('Web token capture started', 'info')
219-
const driverExists = await window.api.checkDriverExists()
220-
221-
if (!driverExists) {
222-
const choice = await showModal(
223-
t('driver.missing_title'),
224-
t('driver.missing_desc'),
225-
[
226-
{ label: t('common.cancel'), value: 'cancel', primary: false },
227-
{ label: t('driver.download_now'), value: 'download', primary: true }
228-
]
229-
)
230-
if (choice.value === 'download') {
231-
const bootstrapped = await bootstrapDriver()
232-
if (!bootstrapped) {
233-
return
234-
}
235-
} else {
236-
setIsDriverMissing(true)
237-
return
238-
}
209+
const driverStatus = await ensureDriverReady()
210+
if (!driverStatus.ok) {
211+
return
239212
}
240213

241214
setIsWebLoading(true)
@@ -334,31 +307,11 @@ const App = () => {
334307
i18n.changeLanguage('en')
335308
}
336309
setLoadProgress(60); setLoadingMessage('Verifying system dependencies...')
337-
const driverExists = await window.api.checkDriverExists()
338-
if (!driverExists) {
339-
setIsDriverMissing(true)
340-
const choice = await showModal(
341-
t('driver.missing_title'),
342-
t('driver.missing_desc'),
343-
[
344-
{ label: t('common.cancel'), value: 'cancel', primary: false },
345-
{ label: t('driver.download_now'), value: 'download', primary: true }
346-
]
347-
)
348-
349-
if (choice.value === 'download') {
350-
const bootstrapped = await bootstrapDriver()
351-
if (!bootstrapped) {
352-
return
353-
}
354-
setLoadProgress(85)
355-
} else {
356-
return
357-
}
358-
} else {
359-
setIsDriverMissing(false)
360-
setLoadProgress(85)
310+
const driverStatus = await ensureDriverReady()
311+
if (!driverStatus.ok) {
312+
return
361313
}
314+
setLoadProgress(85)
362315
if (data?.settings?.autoRefresh && (token || data?.token)) {
363316
setLoadingMessage('Synchronizing account status...')
364317
await refreshAccountInfo(token || data.token, activeAccountId || data.activeAccountId);

src/hooks/useDriverReadiness.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { useCallback } from 'react'
2+
3+
export const useDriverReadiness = ({
4+
t,
5+
showModal,
6+
showChromeMissingModal,
7+
pushToast,
8+
setIsLoading,
9+
setLoadingMessage,
10+
setIsDriverMissing
11+
}) => {
12+
return useCallback(async () => {
13+
const driverExists = await window.api.checkDriverExists()
14+
if (driverExists) {
15+
setIsDriverMissing(false)
16+
return { ok: true, alreadyExists: true }
17+
}
18+
19+
const choice = await showModal(
20+
t('driver.missing_title'),
21+
t('driver.missing_desc'),
22+
[
23+
{ label: t('common.cancel'), value: 'cancel', primary: false },
24+
{ label: t('driver.download_now'), value: 'download', primary: true }
25+
]
26+
)
27+
28+
if (choice.value !== 'download') {
29+
setIsDriverMissing(true)
30+
return { ok: false, canceled: true }
31+
}
32+
33+
setIsLoading(true)
34+
setLoadingMessage(t('driver.preparing'))
35+
const res = await window.api.bootstrapDriver()
36+
setIsLoading(false)
37+
38+
if (res.ok) {
39+
setIsDriverMissing(false)
40+
return { ok: true, alreadyExists: false }
41+
}
42+
43+
if (res.code === 'CHROME_NOT_FOUND') {
44+
await showChromeMissingModal()
45+
setIsDriverMissing(true)
46+
return { ok: false, chromeMissing: true }
47+
}
48+
49+
pushToast(res.error || t('common.error'), 'error')
50+
return { ok: false, failed: true }
51+
}, [
52+
pushToast,
53+
setIsDriverMissing,
54+
setIsLoading,
55+
setLoadingMessage,
56+
showChromeMissingModal,
57+
showModal,
58+
t
59+
])
60+
}

0 commit comments

Comments
 (0)