Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/devtools-kit/src/_types/client-api.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type {} from '@nuxt/schema'
import type { BirpcReturn } from 'birpc'
import type { Hookable } from 'hookable'
import type { NuxtApp } from 'nuxt/app'
import type { AppConfig } from 'nuxt/schema'
import type { $Fetch } from 'ofetch'
import type { BuiltinLanguage } from 'shiki'
import type { Ref } from 'vue'
import type { HookInfo, LoadingTimeMetric, PluginMetric } from './integrations'
import type { ClientFunctions, ServerFunctions } from './rpc'
import type { ServerFunctions } from './rpc'
import type { TimelineMetrics } from './timeline-metrics'

export interface DevToolsFrameState {
Expand Down Expand Up @@ -106,16 +105,20 @@ export interface CodeHighlightOptions {
grammarContextCode?: string
}

export type AsyncServerFunctions = {
[K in keyof ServerFunctions]: (...args: Parameters<ServerFunctions[K]>) => Promise<Awaited<ReturnType<ServerFunctions[K]>>>
}

export interface NuxtDevtoolsClient {
rpc: BirpcReturn<ServerFunctions, ClientFunctions>
rpc: AsyncServerFunctions
renderCodeHighlight: (code: string, lang?: BuiltinLanguage, options?: CodeHighlightOptions) => {
code: string
supported: boolean
}
renderMarkdown: (markdown: string) => string
colorMode: string

extendClientRpc: <ServerFunctions extends object = Record<string, unknown>, ClientFunctions extends object = Record<string, unknown>>(name: string, functions: ClientFunctions) => BirpcReturn<ServerFunctions, ClientFunctions>
extendClientRpc: <ServerFunctions extends object = Record<string, unknown>, ClientFunctions extends object = Record<string, unknown>>(name: string, functions: ClientFunctions) => ServerFunctions
}

export interface NuxtDevtoolsIframeClient {
Expand Down
7 changes: 1 addition & 6 deletions packages/devtools-kit/src/_types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ export interface ModuleOptions {
viteInspect?: boolean

/**
* Disable dev time authorization check.
*
* **NOT RECOMMENDED**, only use this if you know what you are doing.
*
* @see https://github.com/nuxt/devtools/pull/257
* @default false
* @deprecated Auth is now handled by Vite DevTools. This option is ignored.
*/
disableAuthorization?: boolean

Expand Down
26 changes: 22 additions & 4 deletions packages/devtools-kit/src/_types/server-ctx.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
import type { BirpcGroup } from 'birpc'
import type { Nuxt, NuxtDebugModuleMutationRecord } from 'nuxt/schema'
import type { ModuleOptions } from './options'
import type { ClientFunctions, ServerFunctions } from './rpc'

/**
* Compatibility RPC interface that supports broadcast and function access.
* Backed by Vite DevTools Kit's RpcFunctionsHost internally.
*/
export interface NuxtDevtoolsRpc {
/**
* Broadcast proxy for calling client functions.
* Supports `rpc.broadcast.refresh.asEvent(event)` pattern for backward compatibility.
*/
broadcast: {
[K in keyof ClientFunctions]: ClientFunctions[K] & { asEvent: ClientFunctions[K] }
}

/**
* Proxy for accessing server functions locally.
*/
functions: ServerFunctions
}

/**
* @internal
*/
export interface NuxtDevtoolsServerContext {
nuxt: Nuxt
options: ModuleOptions

rpc: BirpcGroup<ClientFunctions, ServerFunctions>
rpc: NuxtDevtoolsRpc

/**
* Hook to open file in editor
Expand All @@ -23,11 +41,11 @@ export interface NuxtDevtoolsServerContext {
refresh: (event: keyof ServerFunctions) => void

/**
* Ensure dev auth token is valid, throw if not
* @deprecated Auth is now handled by Vite DevTools. This is a noop.
*/
ensureDevAuthToken: (token: string) => Promise<void>

extendServerRpc: <ClientFunctions extends object = Record<string, unknown>, ServerFunctions extends object = Record<string, unknown>>(name: string, functions: ServerFunctions) => BirpcGroup<ClientFunctions, ServerFunctions>
extendServerRpc: <ClientFunctions extends object = Record<string, unknown>, ServerFunctions extends object = Record<string, unknown>>(name: string, functions: ServerFunctions) => { broadcast: ClientFunctions }
}

export interface NuxtDevtoolsInfo {
Expand Down
8 changes: 6 additions & 2 deletions packages/devtools-kit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { BirpcGroup } from 'birpc'
import type { ChildProcess } from 'node:child_process'
import type { Result } from 'tinyexec'
import type { ModuleCustomTab, NuxtDevtoolsInfo, NuxtDevtoolsServerContext, SubprocessOptions, TerminalState } from './types'
Expand Down Expand Up @@ -132,11 +131,16 @@ export function startSubprocess(
}
}

/**
* Extend server RPC with namespaced functions.
*
* Returns an object with a `broadcast` proxy for calling client functions.
*/
export function extendServerRpc<ClientFunctions extends object = Record<string, unknown>, ServerFunctions extends object = Record<string, unknown>>(
namespace: string,
functions: ServerFunctions,
nuxt = useNuxt(),
): BirpcGroup<ClientFunctions, ServerFunctions> {
): { broadcast: ClientFunctions } {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't that be a breaking change ?

const ctx = _getContext(nuxt)
if (!ctx)
throw new Error('[Nuxt DevTools] Failed to get devtools context.')
Expand Down
16 changes: 1 addition & 15 deletions packages/devtools/client/components/AuthConfirmDialog.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
<script setup lang="ts">
import { AuthConfirm } from '~/composables/dialog'
</script>

<template>
<AuthConfirm v-slot="{ resolve }">
<NDialog :model-value="!isDevAuthed" class="border-none" @close="resolve(false)">
<AuthRequiredPanel>
<template #actions>
<NButton @click="resolve(false)">
Cancel
</NButton>
</template>
</AuthRequiredPanel>
</NDialog>
</AuthConfirm>
<div />
</template>
76 changes: 2 additions & 74 deletions packages/devtools/client/components/AuthRequiredPanel.vue
Original file line number Diff line number Diff line change
@@ -1,76 +1,4 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { devAuthToken, isDevAuthed, requestForAuth, updateDevAuthToken } from '~/composables/dev-auth'
import { rpc } from '~/composables/rpc'

onMounted(async () => {
if (!isDevAuthed.value) {
if (devAuthToken.value) {
const result = await rpc.verifyAuthToken(devAuthToken.value)
if (result)
isDevAuthed.value = true
}
}
if (!isDevAuthed.value)
requestForAuth()
})

const authInput = ref('')
const isFailed = ref(false)

async function input() {
const token = authInput.value.trim()
isFailed.value = false
await rpc.verifyAuthToken(token)
.then((result) => {
if (result) {
isDevAuthed.value = true
updateDevAuthToken(token)
}
else {
isFailed.value = true
}
})
}
</script>

<template>
<NPanelGrids v-if="!isDevAuthed">
<NCard flex="~ col gap-2" mxa p6>
<h3 class="mb2 text-lg font-medium leading-6" flex="~ items-center gap-1" text-orange>
<span class="i-carbon-information-square" /> Permissions required
</h3>
<p>
This operation requires permissions for running command and access files from the browser.
</p>
<p>
A request is sent to the server.<br>
Please check your terminal for the instructions and then come back.
</p>
<div flex="~ gap-3" mt5 justify-between>
<form relative flex="~ col gap-2" @submit.prevent="input">
<p text-xs op-50>
Or you can manually paste the token below:
</p>
<div flex="~ inline gap-2 items-center">
<NTextInput
v-model="authInput" placeholder="Enter token here"
:n="isFailed ? 'red' : undefined"
@keydown.enter="input"
/>
<NButton n="green" h-full icon="i-carbon-arrow-right" @click="input" />
</div>
</form>
<div flex="~ gap-2 items-end">
<slot name="actions" />
<NButton disabled icon="i-carbon-time">
Waiting for authorization...
</NButton>
</div>
</div>
</NCard>
</NPanelGrids>
<template v-else>
<slot />
</template>
<!-- Auth is now handled by Vite DevTools -->
<slot />
</template>
86 changes: 12 additions & 74 deletions packages/devtools/client/composables/dev-auth.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,26 @@
import { devtoolsUiShowNotification } from '#imports'
import { until } from '@vueuse/core'
import { parseUA } from 'ua-parser-modern'
import { ref } from 'vue'
import { AuthConfirm } from './dialog'
import { rpc } from './rpc'

export const devAuthToken = ref<string | null>(localStorage.getItem('__nuxt_dev_token__'))
/** @deprecated Auth is now handled by Vite DevTools */
export const devAuthToken = ref<string | null>('disabled')

export const isDevAuthed = ref(false)
/** @deprecated Auth is now handled by Vite DevTools */
export const isDevAuthed = ref(true)

const bc = new BroadcastChannel('__nuxt_dev_token__')

bc.addEventListener('message', (e) => {
if (e.data.event === 'new-token') {
if (e.data.data === devAuthToken.value)
return
const token = e.data.data
rpc.verifyAuthToken(token)
.then((result) => {
devAuthToken.value = result ? token : null
isDevAuthed.value = result
})
}
})

export function updateDevAuthToken(token: string) {
devAuthToken.value = token
isDevAuthed.value = true
localStorage.setItem('__nuxt_dev_token__', token)
bc.postMessage({ event: 'new-token', data: token })
/** @deprecated Auth is now handled by Vite DevTools */
export function updateDevAuthToken(_token: string) {
console.warn('[nuxt-devtools] `updateDevAuthToken` is deprecated. Auth is now handled by Vite DevTools.')
}

/** @deprecated Auth is now handled by Vite DevTools */
export async function ensureDevAuthToken() {
if (isDevAuthed.value)
return devAuthToken.value!

if (!devAuthToken.value)
await authConfirmAction()

isDevAuthed.value = await rpc.verifyAuthToken(devAuthToken.value!)
if (!isDevAuthed.value) {
devAuthToken.value = null
devtoolsUiShowNotification({
message: 'Invalid auth token, action canceled',
icon: 'i-carbon-warning-alt',
classes: 'text-red',
})
await authConfirmAction()
throw new Error('[Nuxt DevTools] Invalid auth token')
}

return devAuthToken.value!
console.warn('[nuxt-devtools] `ensureDevAuthToken` is deprecated. Auth is now handled by Vite DevTools.')
return ''
}

export const userAgentInfo = parseUA(navigator.userAgent)

/** @deprecated Auth is now handled by Vite DevTools */
export async function requestForAuth() {
const desc = [
userAgentInfo.browser.name,
userAgentInfo.browser.version,
'|',
userAgentInfo.os.name,
userAgentInfo.os.version,
userAgentInfo.device.type,
].filter(i => i).join(' ')
return await rpc.requestForAuth(desc, window.location.origin)
}

async function authConfirmAction() {
if (!devAuthToken.value)
requestForAuth()

const result = await Promise.race([
AuthConfirm.start(),
until(devAuthToken.value).toBeTruthy(),
])

if (result === false) {
// @unocss-include
devtoolsUiShowNotification({
message: 'Action canceled',
icon: 'carbon-close',
classes: 'text-orange',
})
throw new Error('[Nuxt DevTools] User canceled auth')
}
console.warn('[nuxt-devtools] `requestForAuth` is deprecated. Auth is now handled by Vite DevTools.')
}
Loading
Loading