11import * as p from '@clack/prompts'
22import { defineRpcFunction } from '@vitejs/devtools-kit'
33import c from 'ansis'
4+ import { join } from 'pathe'
5+ import { createStorage } from '../../storage'
46
57export 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
2226export 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
0 commit comments