refactor: reuse vite's websocket

This commit is contained in:
Arash Sheyda
2023-06-18 12:20:57 +03:00
parent 2ba8bdadfc
commit aaef56cb29
9 changed files with 122 additions and 100 deletions

View File

@ -1,3 +1,3 @@
export const PATH = '/__nuxt_mongoose__'
export const PATH_ENTRY = `${PATH}/entry`
export const PATH_CLIENT = `${PATH}/client`
export const WS_EVENT_NAME = 'nuxt:devtools:mongoose:rpc'

View File

@ -2,17 +2,19 @@ import {
addImportsDir,
addServerPlugin,
addTemplate,
addVitePlugin,
createResolver,
defineNuxtModule,
logger,
} from '@nuxt/kit'
import { pathExists } from 'fs-extra'
import { tinyws } from 'tinyws'
import { join } from 'pathe'
import { defu } from 'defu'
import sirv from 'sirv'
import { $fetch } from 'ofetch'
import { version } from '../package.json'
import { PATH_CLIENT, PATH_ENTRY } from './constants'
import { PATH_CLIENT } from './constants'
import type { ModuleOptions } from './types'
import { setupRPC } from './server-rpc'
@ -34,6 +36,13 @@ export default defineNuxtModule<ModuleOptions>({
const { resolve } = createResolver(import.meta.url)
const runtimeConfig = nuxt.options.runtimeConfig
if (nuxt.options.dev) {
$fetch('https://registry.npmjs.org/nuxt-mongoose/latest').then((release) => {
if (release.version > version)
logger.info(`A new version of Nuxt Mongoose (v${release.version}) is available: https://github.com/arashsheyda/nuxt-mongoose/releases/latest`)
}).catch(() => {})
}
addImportsDir(resolve('./runtime/composables'))
if (!options.uri) {
@ -58,11 +67,11 @@ export default defineNuxtModule<ModuleOptions>({
}
const clientPath = distResolve('./client')
const { middleware: rpcMiddleware } = setupRPC(nuxt, options)
const { vitePlugin } = setupRPC(nuxt, options)
addVitePlugin(vitePlugin)
nuxt.hook('vite:serverCreated', async (server) => {
server.middlewares.use(PATH_ENTRY, tinyws() as any)
server.middlewares.use(PATH_ENTRY, rpcMiddleware as any)
if (await pathExists(clientPath))
server.middlewares.use(PATH_CLIENT, sirv(clientPath, { dev: true, single: true }))
})

View File

@ -11,7 +11,7 @@ export async function defineMongooseConnection({ uri, options }: { uri?: string;
try {
await mongoose.connect(mongooseUri, { ...mongooseOptions })
logger.info('Connected to mongoose database')
logger.info('Connected to MONGODB')
}
catch (err) {
logger.error('Error connecting to database', err)

View File

@ -1,12 +1,12 @@
import type { TinyWSRequest } from 'tinyws'
import type { NodeIncomingMessage, NodeServerResponse } from 'h3'
import type { WebSocket } from 'ws'
import { createBirpcGroup } from 'birpc'
import type { ChannelOptions } from 'birpc'
import { parse, stringify } from 'flatted'
import type { Plugin } from 'vite'
import type { Nuxt } from 'nuxt/schema'
import type { ClientFunctions, ModuleOptions, NuxtDevtoolsServerContext, ServerFunctions } from '../types'
import { WS_EVENT_NAME } from '../constants'
import { setupDatabaseRPC } from './database'
import { setupResourceRPC } from './resource'
@ -68,36 +68,50 @@ export function setupRPC(nuxt: Nuxt, options: ModuleOptions): any {
} satisfies Partial<ServerFunctions>)
const wsClients = new Set<WebSocket>()
const middleware = async (req: NodeIncomingMessage & TinyWSRequest, _res: NodeServerResponse, next: Function) => {
// Handle WebSocket
if (req.ws) {
const ws = await req.ws()
wsClients.add(ws)
const channel: ChannelOptions = {
post: d => ws.send(d),
on: fn => ws.on('message', fn),
serialize: stringify,
deserialize: parse,
}
rpc.updateChannels((c) => {
c.push(channel)
})
ws.on('close', () => {
wsClients.delete(ws)
const vitePlugin: Plugin = {
name: 'nuxt:devtools:rpc',
configureServer(server) {
server.ws.on('connection', (ws) => {
wsClients.add(ws)
const channel: ChannelOptions = {
post: d => ws.send(JSON.stringify({
type: 'custom',
event: WS_EVENT_NAME,
data: d,
})),
on: (fn) => {
ws.on('message', (e) => {
try {
const data = JSON.parse(String(e)) || {}
if (data.type === 'custom' && data.event === WS_EVENT_NAME) {
// console.log(data.data)
fn(data.data)
}
}
catch {}
})
},
serialize: stringify,
deserialize: parse,
}
rpc.updateChannels((c) => {
const index = c.indexOf(channel)
if (index >= 0)
c.splice(index, 1)
c.push(channel)
})
ws.on('close', () => {
wsClients.delete(ws)
rpc.updateChannels((c) => {
const index = c.indexOf(channel)
if (index >= 0)
c.splice(index, 1)
})
})
})
}
else {
next()
}
},
}
return {
middleware,
vitePlugin,
...ctx,
}
}