Skip to content

Commit 269fcd7

Browse files
committed
wip: storage auth
1 parent 0252d70 commit 269fcd7

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

packages/core/src/node/rpc/anonymous/auth.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as p from '@clack/prompts'
22
import { defineRpcFunction } from '@vitejs/devtools-kit'
33
import c from 'ansis'
4+
import { join } from 'pathe'
5+
import { createStorage } from '../../storage'
46

57
export interface DevToolsAuthInput {
68
authId: string
@@ -12,24 +14,33 @@ export interface DevToolsAuthReturn {
1214
isTrusted: boolean
1315
}
1416

15-
// TODO: Replace with a proper storage solution
16-
const TEMPORARY_STORAGE = new Map<string, {
17-
authId: string
18-
ua: string
19-
timestamp: number
20-
}>()
17+
interface AnonymousAuthStorage {
18+
trusted: Record<string, {
19+
authId: string
20+
ua: string
21+
origin: string
22+
timestamp: number
23+
}>
24+
}
2125

2226
export const anonymousAuth = defineRpcFunction({
2327
name: 'vite:anonymous:auth',
2428
type: 'action',
2529
setup: (context) => {
30+
const storage = createStorage<AnonymousAuthStorage>({
31+
filepath: join(context.cwd, 'node_modules/.vite/devtools/auth.json'),
32+
initialValue: {
33+
trusted: {},
34+
},
35+
})
36+
2637
return {
2738
handler: async (query: DevToolsAuthInput): Promise<DevToolsAuthReturn> => {
2839
const session = context.rpc.getCurrentRpcSession()
2940
if (!session)
3041
throw new Error('Failed to retrieve the current RPC session')
3142

32-
if (TEMPORARY_STORAGE.has(query.authId)) {
43+
if (storage.get().trusted[query.authId]) {
3344
session.meta.clientAuthId = query.authId
3445
session.meta.isTrusted = true
3546
return {
@@ -59,10 +70,13 @@ export const anonymousAuth = defineRpcFunction({
5970
})
6071

6172
if (answer) {
62-
TEMPORARY_STORAGE.set(query.authId, {
63-
authId: query.authId,
64-
ua: query.ua,
65-
timestamp: Date.now(),
73+
storage.mutate((state) => {
74+
state.trusted[query.authId] = {
75+
authId: query.authId,
76+
ua: query.ua,
77+
origin: query.origin,
78+
timestamp: Date.now(),
79+
}
6680
})
6781
session.meta.clientAuthId = query.authId
6882
session.meta.isTrusted = true

packages/core/src/node/storage.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import fs from 'node:fs'
22
import { createSharedState } from '@vitejs/devtools-kit/utils/shared-state'
3+
import { dirname } from 'pathe'
34

45
export interface CreateStorageOptions<T extends object> {
56
filepath: string
67
initialValue: T
78
}
89

9-
export async function createStorage<T extends object>(options: CreateStorageOptions<T>) {
10+
export function createStorage<T extends object>(options: CreateStorageOptions<T>) {
1011
let initialState: T
1112
if (fs.existsSync(options.filepath)) {
12-
initialState = JSON.parse(await fs.readFileSync(options.filepath, 'utf-8')) as T
13+
initialState = JSON.parse(fs.readFileSync(options.filepath, 'utf-8')) as T
1314
}
1415
else {
1516
initialState = options.initialValue
@@ -21,6 +22,7 @@ export async function createStorage<T extends object>(options: CreateStorageOpti
2122
})
2223

2324
state.on('updated', (newState) => {
25+
fs.mkdirSync(dirname(options.filepath), { recursive: true })
2426
fs.writeFileSync(options.filepath, `${JSON.stringify(newState, null, 2)}\n`)
2527
})
2628

packages/vite/src/app/composables/rpc.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export async function connect() {
4040
connectionState.error = e
4141
console.error(`[vite-devtools] RPC error on executing "${name}":`)
4242
},
43+
onFunctionError: (e, name) => {
44+
connectionState.error = e
45+
console.error(`[vite-devtools] RPC error on executing "${name}":`)
46+
},
4347
},
4448
})
4549

0 commit comments

Comments
 (0)