Skip to content

Commit

Permalink
firefox compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
nitedani committed Jul 26, 2024
1 parent 95e1df3 commit 896c0c3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 49 deletions.
38 changes: 14 additions & 24 deletions packages/vike-node/src/plugin/plugins/devServerPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fork } from 'child_process'
import { ServerResponse, createServer, type IncomingMessage } from 'http'
import { createServer, type IncomingMessage, type Server } from 'http'
import type { Plugin, ViteDevServer } from 'vite'
import { globalStore } from '../../runtime/globalStore.js'
import type { ConfigVikeNodeResolved } from '../../types.js'
Expand All @@ -15,6 +15,7 @@ export function devServerPlugin(): Plugin {
let resolvedConfig: ConfigVikeNodeResolved
let entryAbs: string
let HMRServer: ReturnType<typeof createServer> | undefined
let setupHMRProxyDone = false
return {
name: 'vite-node:devserver',
apply: 'serve',
Expand Down Expand Up @@ -60,7 +61,7 @@ export function devServerPlugin(): Plugin {

viteDevServer = vite
globalStore.viteDevServer = vite
globalStore.HMRProxy = HMRProxy
globalStore.setupHMRProxy = setupHMRProxy
patchViteServer(vite)
initializeServerEntry(vite)
}
Expand Down Expand Up @@ -95,30 +96,19 @@ export function devServerPlugin(): Plugin {
vite.ssrLoadModule(entryAbs)
}

function HMRProxy(req: IncomingMessage, res: ServerResponse, next?: (err?: unknown) => void): boolean {
const canHandle = req.url === VITE_HMR_PATH && req.headers.upgrade === 'websocket'
if (!canHandle) {
next?.()
return false
function setupHMRProxy(req: IncomingMessage) {
if (setupHMRProxyDone || isBun) {
return
}

// Pause the socket to prevent data loss
req.socket.pause()

// Prepare the socket for upgrade
res.detachSocket(req.socket)
req.socket.setTimeout(0)
req.socket.setNoDelay(true)
req.socket.setKeepAlive(true, 0)

// Emit the upgrade event
assert(HMRServer)
HMRServer.emit('upgrade', req, req.socket, Buffer.alloc(0))

// Resume the socket
req.socket.resume()

return true
setupHMRProxyDone = true
const server = (req.socket as any).server as Server
server.on('upgrade', (clientReq, clientSocket, wsHead) => {
if (clientReq.url === VITE_HMR_PATH) {
assert(HMRServer)
HMRServer.emit('upgrade', clientReq, clientSocket, wsHead)
}
})
}
}

Expand Down
7 changes: 1 addition & 6 deletions packages/vike-node/src/runtime/frameworks/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ function vike<PlatformRequest extends IncomingMessage, PlatformResponse extends
): (req: PlatformRequest, res: PlatformResponse, next?: NextFunction) => void {
const handler = createHandler(options)
return (req, res, next) => {
const handled = globalStore.HMRProxy(req, res)
if (handled) {
next?.()
return
}

globalStore.setupHMRProxy(req)
handler({
req,
res,
Expand Down
5 changes: 1 addition & 4 deletions packages/vike-node/src/runtime/frameworks/fastify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ function vike(options?: VikeOptions<FastifyRequest>): FastifyPluginCallback {
const handler = createHandler(options)
return function plugin(instance, _options, done) {
instance.get('*', async (req, reply) => {
const handled = globalStore.HMRProxy(req.raw, reply.raw)
if (handled) {
return
}
globalStore.setupHMRProxy(req.raw)
const { res, onReadable } = createServerResponse(req.raw)
;(async () => {
const { readable, headers, statusCode } = await onReadable
Expand Down
9 changes: 1 addition & 8 deletions packages/vike-node/src/runtime/frameworks/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ function vike(options?: VikeOptions): MiddlewareHandler {
const handler = createHandler<HonoRequest>(options)
return async function middleware(ctx, next) {
const req = ctx.env.incoming as IncomingMessage
const res = ctx.env.outgoing as ServerResponse
const handled = globalStore.HMRProxy(req, res)

if (handled) {
res.writeHead = () => res
return new Response()
}

globalStore.setupHMRProxy(req)
const response = await connectToWeb((req, res, next) =>
handler({
req,
Expand Down
11 changes: 4 additions & 7 deletions packages/vike-node/src/runtime/globalStore.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import type { IncomingMessage, ServerResponse } from 'http'
import type { IncomingMessage } from 'http'
import type { ViteDevServer } from 'vite'
import { NextFunction } from './types.js'

// @ts-expect-error
export const globalStore = (globalThis.__vikeNode ||= {
isPluginLoaded: false,
// This is overridden in devServerPlugin
HMRProxy: (req: IncomingMessage, res: ServerResponse, next?: NextFunction) => {
next?.()
return false
}
// in production it's a no-op
setupHMRProxy: () => {}
}) as {
isPluginLoaded: boolean
viteDevServer?: ViteDevServer
HMRProxy: (req: IncomingMessage, res: ServerResponse, next?: NextFunction) => boolean
setupHMRProxy: (req: IncomingMessage) => void
}

0 comments on commit 896c0c3

Please sign in to comment.