Skip to content

Commit f760713

Browse files
committed
feat: internal context
1 parent 269fcd7 commit f760713

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { DevToolsNodeContext } from '@vitejs/devtools-kit'
2+
import type { SharedState } from '@vitejs/devtools-kit/utils/shared-state'
3+
import { join } from 'pathe'
4+
import { createStorage } from './storage'
5+
6+
export interface InternalAnonymousAuthStorage {
7+
trusted: Record<string, {
8+
authId: string
9+
ua: string
10+
origin: string
11+
timestamp: number
12+
}>
13+
}
14+
15+
export interface DevToolsInternalContext {
16+
storage: {
17+
auth: SharedState<InternalAnonymousAuthStorage>
18+
}
19+
}
20+
21+
export const internalContextMap = new WeakMap<DevToolsNodeContext, DevToolsInternalContext>()
22+
23+
export function getInternalContext(context: DevToolsNodeContext): DevToolsInternalContext {
24+
if (!internalContextMap.has(context)) {
25+
const internalContext: DevToolsInternalContext = {
26+
storage: {
27+
auth: createStorage<InternalAnonymousAuthStorage>({
28+
filepath: join(context.workspaceRoot, 'node_modules/.vite/devtools/auth.json'),
29+
initialValue: {
30+
trusted: {},
31+
},
32+
}),
33+
},
34+
}
35+
internalContextMap.set(context, internalContext)
36+
}
37+
return internalContextMap.get(context)!
38+
}

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

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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'
4+
import { getInternalContext } from '../../context-internal'
65

76
export interface DevToolsAuthInput {
87
authId: string
@@ -14,25 +13,12 @@ export interface DevToolsAuthReturn {
1413
isTrusted: boolean
1514
}
1615

17-
interface AnonymousAuthStorage {
18-
trusted: Record<string, {
19-
authId: string
20-
ua: string
21-
origin: string
22-
timestamp: number
23-
}>
24-
}
25-
2616
export const anonymousAuth = defineRpcFunction({
2717
name: 'vite:anonymous:auth',
2818
type: 'action',
2919
setup: (context) => {
30-
const storage = createStorage<AnonymousAuthStorage>({
31-
filepath: join(context.cwd, 'node_modules/.vite/devtools/auth.json'),
32-
initialValue: {
33-
trusted: {},
34-
},
35-
})
20+
const internal = getInternalContext(context)
21+
const storage = internal.storage.auth
3622

3723
return {
3824
handler: async (query: DevToolsAuthInput): Promise<DevToolsAuthReturn> => {

0 commit comments

Comments
 (0)