try to fix websockets
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
import { Type } from "@sinclair/typebox";
|
||||
import { FirRouteInput, FirRouteOptions } from "../util/routewrap.js";
|
||||
|
||||
const method = "POST";
|
||||
const url = "/api/webhook-gh";
|
||||
|
||||
const payloadT = Type.Any();
|
||||
|
||||
const handler = ({payload}: FirRouteInput<typeof payloadT>) => {
|
||||
console.log(payload);
|
||||
return {};
|
||||
};
|
||||
|
||||
export default {
|
||||
method,
|
||||
url,
|
||||
payloadT,
|
||||
handler,
|
||||
} as const satisfies FirRouteOptions<typeof payloadT>;
|
||||
+1
-1
@@ -9,7 +9,7 @@ const server = Fastify({
|
||||
logger: true,
|
||||
});
|
||||
|
||||
server.register(fastifyWebsocket);
|
||||
server.register<any>(fastifyWebsocket, { server: server.server });
|
||||
|
||||
server.register(fastifyStatic, {
|
||||
root: new URL("public", import.meta.url).toString().slice("file://".length),
|
||||
|
||||
@@ -2,8 +2,7 @@ import echo from "./api/echo.ts";
|
||||
import getAuthor from "./api/getAuthor.ts";
|
||||
import getGame from "./api/getGame.ts";
|
||||
import room from "./api/room.ts";
|
||||
import webhook from "./api/webhook.ts";
|
||||
|
||||
export const routeList = [echo, webhook, getAuthor, room, getGame];
|
||||
export const routeList = [echo, getAuthor, room, getGame];
|
||||
|
||||
export type RouteList = typeof routeList;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Static, TSchema } from "@sinclair/typebox";
|
||||
import { Value } from "@sinclair/typebox/value";
|
||||
import { FastifyInstance, FastifyRequest, HTTPMethods } from "fastify"
|
||||
import { FastifyInstance, FastifyRequest, HTTPMethods } from "fastify";
|
||||
import { RouteOptions } from "fastify/types/route.js";
|
||||
import { type WebSocket } from "@fastify/websocket";
|
||||
|
||||
@@ -9,63 +9,80 @@ type WebsocketConnection = Parameters<Defined<RouteOptions["wsHandler"]>>[0];
|
||||
type URLString = string;
|
||||
|
||||
export type FirRouteInput<TPayloadSchema extends TSchema> = {
|
||||
payload: Static<TPayloadSchema>,
|
||||
}
|
||||
|
||||
export type FirWebsocketInput<TPayloadSchema extends TSchema> = {
|
||||
socket: WebsocketConnection,
|
||||
req: FastifyRequest,
|
||||
payload: Static<TPayloadSchema>,
|
||||
}
|
||||
|
||||
export type FirWebsocketHandler<TIn extends TSchema = TSchema> = {
|
||||
onMessage?(input: FirWebsocketInput<TIn>): void,
|
||||
onOpen?(input: {socket: WebSocket, req: FastifyRequest}): void,
|
||||
onClose?(input: {socket: WebSocket, req: FastifyRequest}): void,
|
||||
onError?(input: {socket: WebSocket, req: FastifyRequest, error: unknown}): void,
|
||||
payload: Static<TPayloadSchema>;
|
||||
};
|
||||
|
||||
export type FirRouteOptions<TIn extends TSchema = TSchema, TOut extends TSchema = TSchema> = {
|
||||
method: HTTPMethods,
|
||||
url: URLString,
|
||||
payloadT: TIn,
|
||||
responseT?: TOut,
|
||||
} & ({
|
||||
handler: (input: FirRouteInput<TIn>) => Static<TOut> | Promise<Static<TOut>>,
|
||||
} | {
|
||||
websocket: FirWebsocketHandler<TIn>,
|
||||
})
|
||||
export type FirWebsocketInput<TPayloadSchema extends TSchema> = {
|
||||
socket: WebsocketConnection;
|
||||
req: FastifyRequest;
|
||||
payload: Static<TPayloadSchema>;
|
||||
};
|
||||
|
||||
export type FirWebsocketHandler<TIn extends TSchema = TSchema> = {
|
||||
onMessage?(input: FirWebsocketInput<TIn>): void;
|
||||
onOpen?(input: { socket: WebSocket; req: FastifyRequest }): void;
|
||||
onClose?(input: { socket: WebSocket; req: FastifyRequest }): void;
|
||||
onError?(input: {
|
||||
socket: WebSocket;
|
||||
req: FastifyRequest;
|
||||
error: unknown;
|
||||
}): void;
|
||||
};
|
||||
|
||||
export type FirRouteOptions<
|
||||
TIn extends TSchema = TSchema,
|
||||
TOut extends TSchema = TSchema,
|
||||
> = {
|
||||
method: HTTPMethods;
|
||||
url: URLString;
|
||||
payloadT: TIn;
|
||||
responseT?: TOut;
|
||||
} & (
|
||||
| {
|
||||
handler: (
|
||||
input: FirRouteInput<TIn>,
|
||||
) => Static<TOut> | Promise<Static<TOut>>;
|
||||
}
|
||||
| {
|
||||
websocket: FirWebsocketHandler<TIn>;
|
||||
}
|
||||
);
|
||||
|
||||
type Defined<T> = T extends undefined ? never : T;
|
||||
|
||||
export const attachRoute = <TIn extends TSchema, TOut extends TSchema>(server: FastifyInstance, routeOptions: FirRouteOptions<TIn, TOut>) => {
|
||||
const {
|
||||
method,
|
||||
url,
|
||||
payloadT,
|
||||
} = routeOptions;
|
||||
export const attachRoute = <TIn extends TSchema, TOut extends TSchema>(
|
||||
server: FastifyInstance,
|
||||
routeOptions: FirRouteOptions<TIn, TOut>,
|
||||
) => {
|
||||
const { method, url, payloadT } = routeOptions;
|
||||
|
||||
if ("websocket" in routeOptions) {
|
||||
console.log('SETTING UP WS');
|
||||
console.log("SETTING UP WS");
|
||||
const { websocket } = routeOptions;
|
||||
server.register(async function (fastify: FastifyInstance) {
|
||||
fastify.get('/api/ws/room', { websocket: true }, (socket: WebSocket, req: FastifyRequest) => {
|
||||
fastify.get(
|
||||
"/api/ws/room",
|
||||
{ websocket: true },
|
||||
(socket: WebSocket, req: FastifyRequest) => {
|
||||
websocket.onOpen && websocket.onOpen({ socket, req });
|
||||
socket.on('message', (message: any) => {
|
||||
socket.on("message", (message: any) => {
|
||||
const payload = JSON.parse(message.toString());
|
||||
if (Value.Check(payloadT, payload)) {
|
||||
websocket.onMessage && websocket.onMessage({socket, payload, req});
|
||||
websocket.onMessage &&
|
||||
websocket.onMessage({ socket, payload, req });
|
||||
} else {
|
||||
throw new Error("Payload wrong shape.");
|
||||
}
|
||||
});
|
||||
socket.on('close', () => {
|
||||
socket.on("close", () => {
|
||||
websocket.onClose && websocket.onClose({ socket, req });
|
||||
});
|
||||
socket.on('error', (error: any) => {
|
||||
websocket.onError && websocket.onError({socket, error, req});
|
||||
socket.on("error", (error: any) => {
|
||||
websocket.onError &&
|
||||
websocket.onError({ socket, error, req });
|
||||
});
|
||||
})
|
||||
},
|
||||
);
|
||||
});
|
||||
return;
|
||||
|
||||
@@ -100,22 +117,21 @@ export const attachRoute = <TIn extends TSchema, TOut extends TSchema>(server: F
|
||||
}
|
||||
|
||||
const { handler } = routeOptions;
|
||||
const augmentedHandler = (request: Parameters<RouteOptions["handler"]>[0]) => {
|
||||
const {
|
||||
body,
|
||||
query,
|
||||
} = request;
|
||||
const augmentedHandler = (
|
||||
request: Parameters<RouteOptions["handler"]>[0],
|
||||
) => {
|
||||
const { body, query } = request;
|
||||
const payload = body ?? query;
|
||||
if (Value.Check(payloadT, payload)) {
|
||||
return handler({ payload });
|
||||
} else {
|
||||
throw new Error("Payload wrong shape.");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
server.route({
|
||||
method,
|
||||
url,
|
||||
handler: augmentedHandler,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user