Skip to content

Commit

Permalink
Add httpServer reference for socket.io (#1018)
Browse files Browse the repository at this point in the history
- added viteDevServer.httpServer in dev mode
- added polka.server for start-node in build mode

Co-authored-by: Ryan Carniato <[email protected]>
  • Loading branch information
aronmal and ryansolid authored Aug 29, 2023
1 parent 2e3fd35 commit 15650ac
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/start-node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function createServer({ handler, paths, env }) {
})
: (_req, _res, next) => next();

const render = async (req, res) => {
const render = async (req, res, server) => {
try {
env.getStaticHTML = async assetPath => {
let text = await readFile(join(paths.assets, assetPath + ".html"), "utf8");
Expand All @@ -50,6 +50,7 @@ export function createServer({ handler, paths, env }) {
console.log("[internal]", url.method, url.href);
return handler({
request: request,
httpServer: server,
clientAddress: req.socket.remoteAddress,
locals: {},
env,
Expand All @@ -59,6 +60,7 @@ export function createServer({ handler, paths, env }) {

const webRes = await handler({
request: createRequest(req),
httpServer: server,
clientAddress: req.socket.remoteAddress,
locals: {},
env,
Expand All @@ -74,7 +76,8 @@ export function createServer({ handler, paths, env }) {
}
};

const server = polka().use("/", comp, assets_handler).use(comp, render);
const server = polka();
server.use("/", comp, assets_handler).use(comp, (req, res) => render(req, res, server.server));

return server;
}
1 change: 1 addition & 0 deletions packages/start/api/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const apiRoutes: Middleware = ({ forward }) => {
if (apiHandler) {
let apiEvent = Object.freeze({
request: event.request,
httpServer: event.httpServer,
clientAddress: event.clientAddress,
locals: event.locals,
params: apiHandler.params,
Expand Down
2 changes: 2 additions & 0 deletions packages/start/dev/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,15 @@ export function createDevHandler(viteServer, config, options) {

return entry({
request: internalRequest,
httpServer: viteServer.httpServer,
env: devEnv,
fetch: internalFetch
});
}

return await entry({
request,
httpServer: viteServer.httpServer,
env: devEnv,
clientAddress,
locals,
Expand Down
3 changes: 3 additions & 0 deletions packages/start/server/types.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Server } from "http";

export type ManifestEntry = {
type: string;
href: string;
Expand Down Expand Up @@ -74,6 +76,7 @@ declare global {

export interface FetchEvent {
request: Request;
httpServer?: Server;
env: Env;
fetch(url: string, init?: RequestInit): Promise<Response>;
clientAddress: string;
Expand Down

0 comments on commit 15650ac

Please sign in to comment.