Skip to content

Commit c4b1f5d

Browse files
committed
refactor: server
1 parent 9198101 commit c4b1f5d

File tree

9 files changed

+122
-124
lines changed

9 files changed

+122
-124
lines changed

client/app/pages/inspector.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const lastUpdate = computed(() => {
1414
1515
function initWebSocket() {
1616
wsStatus.value = 'pending'
17-
ws = new WebSocket(`ws://${window.location.host}/_ws/inspector`)
17+
ws = new WebSocket(`ws://${window.location.host}/ws/inspector`)
1818
ws.onopen = () => {
1919
wsStatus.value = 'success'
2020
}

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
"dist"
9292
],
9393
"engines": {
94-
"node": ">=18"
94+
"node": ">=20"
9595
},
9696
"scripts": {
9797
"build": "unbuild && pnpm run build:client",
@@ -165,8 +165,7 @@
165165
"mrmime": "^2.0.1",
166166
"oxc-parser": "^0.63.0",
167167
"pathe": "^2.0.3",
168-
"unplugin": "^2.3.2",
169-
"uuid": "^11.1.0"
168+
"unplugin": "^2.3.2"
170169
},
171170
"devDependencies": {
172171
"@antfu/eslint-config": "^4.12.0",

pnpm-lock.yaml

Lines changed: 103 additions & 102 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/server/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { createServer as _createServer } from 'node:http'
33
import { env } from 'node:process'
44
import wsAdapter from 'crossws/adapters/node'
55
import { createApp, toNodeListener } from 'h3'
6-
import inspectorHandler from './_ws/inspector'
76
import filePathMap from './filePathMap'
87
import health from './health'
98
import { launchEditor as launchEditorHandler } from './launchEditor'
109
import send from './send'
1110
import serveStatic from './serveStatic'
12-
import ws from './ws'
11+
import inspectorHandler from './ws/inspector'
12+
import passLogsHandler from './ws/passLogs'
1313

1414
export async function createServer(options: Options) {
1515
const { server, launchEditor, passLogs, inspector } = options
@@ -29,11 +29,11 @@ export async function createServer(options: Options) {
2929
app.use('/health', health)
3030

3131
if (inspector)
32-
app.use('/_ws/inspector', inspectorHandler)
32+
app.use('/ws/inspector', inspectorHandler)
3333

3434
if (passLogs) {
3535
// Pass server log route
36-
app.use('/ws', ws)
36+
app.use('/ws/passLogs', passLogsHandler)
3737
.use('/send', send)
3838
}
3939

@@ -46,7 +46,7 @@ export async function createServer(options: Options) {
4646

4747
const server = _createServer(toNodeListener(app))
4848

49-
const { handleUpgrade } = wsAdapter(app.websocket)
49+
const { handleUpgrade } = wsAdapter(app.websocket as any)
5050

5151
server.on('upgrade', handleUpgrade)
5252

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import type { Peer } from 'crossws'
21
import { effect } from 'alien-signals'
32
import { defineWebSocketHandler } from 'h3'
43
import { version } from '../../../../package.json'
54
import { expressionsMapState } from '../../utils/state'
65

7-
const connections = new Map<string, Peer>()
6+
const connections = new Map<string, any>()
87

98
function formatedResponse(expressionsMap: any) {
109
return {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-console */
22
import { defineWebSocketHandler } from 'h3'
3-
import { PLUGIN_NAME } from '../constants'
4-
import { peersState } from '../utils/state'
3+
import { PLUGIN_NAME } from '../../constants'
4+
import { peersState } from '../../utils/state'
55

66
export default defineWebSocketHandler({
77
open(peer) {

src/core/utils/state.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { Peer } from 'crossws'
21
import type { ExpressionMeta, ExpressionsMap } from '../../types'
2+
import { randomUUID } from 'node:crypto'
33
import { signal } from 'alien-signals'
4-
import { v4 as uuidv4 } from 'uuid'
54

65
export const filePathMapState = signal(new Map<string, string>())
76

@@ -19,7 +18,7 @@ export function addExpression(relativePath: string, expressionMeta: ExpressionMe
1918
)) {
2019
const newMap = new Map(currentMap)
2120
newMap.set(relativePath, {
22-
id: uuidv4(),
21+
id: randomUUID(),
2322
filePath: relativePath,
2423
expressions: [...expressions, expressionMeta],
2524
})
@@ -28,4 +27,4 @@ export function addExpression(relativePath: string, expressionMeta: ExpressionMe
2827
}
2928

3029
export const serverState = signal(false)
31-
export const peersState = signal(new Set<Peer<any>>())
30+
export const peersState = signal(new Set<any>())

src/core/utils/virtualModules.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
export function initVirtualModulesGenerator(port: number, isProd: boolean) {
1+
export function initVirtualModulesGenerator(host: string, port: number, isProd: boolean) {
22
if (isProd)
33
return ''
44

55
return /* js */`
66
;(() => {
77
if (globalThis.window) {
8-
const socket = new WebSocket('ws://localhost:${port}/ws')
8+
const socket = new WebSocket('ws://${host}:${port}/ws/passLogs')
99
globalThis.window.UNPLUGIN_TURBO_CONSOLE_CLIENT_SOCKET = socket
1010
socket.addEventListener('message', (event) => {
1111
try {

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { UnpluginFactory } from 'unplugin'
22
import type { Context, Options } from './types'
3+
import { randomUUID } from 'node:crypto'
34
import { cwd, env } from 'node:process'
45
import { checkPort, getRandomPort } from 'get-port-please'
56
import { relative } from 'pathe'
67
import { createUnplugin } from 'unplugin'
7-
import { v4 as uuidv4 } from 'uuid'
88
import { PLUGIN_NAME, VirtualModules } from './core/constants'
99
import { resolveOptions } from './core/options'
1010
import { createServer } from './core/server/index'
@@ -52,7 +52,7 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (rawOptions
5252
id = id.slice(1)
5353

5454
if (id === VirtualModules.Init) {
55-
return initVirtualModulesGenerator(options.server.port!, env.NODE_ENV === 'production')
55+
return initVirtualModulesGenerator(options.server.host!, options.server.port!, env.NODE_ENV === 'production')
5656
}
5757
else if (id === VirtualModules.ThemeDetect) {
5858
return themeDetectVirtualModule(env.NODE_ENV === 'production')
@@ -138,7 +138,7 @@ export const unpluginFactory: UnpluginFactory<Options | undefined> = (rawOptions
138138
const currentMap = expressionsMapState()
139139
const newMap = new Map(currentMap)
140140
newMap.set(relativePath, {
141-
id: uuidv4(),
141+
id: randomUUID(),
142142
filePath: relativePath,
143143
expressions: [],
144144
})

0 commit comments

Comments
 (0)