-
Notifications
You must be signed in to change notification settings - Fork 202
chore: migrate RPC to Vite DevTools Kit's RPC layer #957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
antfu
wants to merge
3
commits into
main
Choose a base branch
from
antfu/migrate-rpc-vite-devtools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.') | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?