Skip to content

Commit 594555f

Browse files
authored
nodenext compatibility (#247)
1 parent a8e7826 commit 594555f

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,5 @@ module.exports = fp(fastifyWebsocket, {
194194
fastify: '>= 4.0.0',
195195
name: '@fastify/websocket'
196196
})
197+
module.exports.default = fastifyWebsocket
198+
module.exports.fastifyWebsocket = fastifyWebsocket

types/index.d.ts

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface WebsocketRouteOptions<
1616
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
1717
Logger extends FastifyBaseLogger = FastifyBaseLogger
1818
> {
19-
wsHandler?: WebsocketHandler<RawServer, RawRequest, RequestGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>;
19+
wsHandler?: fastifyWebsocket.WebsocketHandler<RawServer, RawRequest, RequestGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>;
2020
}
2121

2222
declare module 'fastify' {
@@ -44,7 +44,7 @@ declare module 'fastify' {
4444
<RequestGeneric extends RequestGenericInterface = RequestGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler extends FastifySchema = FastifySchema, Logger extends FastifyBaseLogger = FastifyBaseLogger>(
4545
path: string,
4646
opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> & { websocket: true }, // this creates an overload that only applies these different types if the handler is for websockets
47-
handler?: WebsocketHandler<RawServer, RawRequest, RequestGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
47+
handler?: fastifyWebsocket.WebsocketHandler<RawServer, RawRequest, RequestGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
4848
): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
4949
}
5050

@@ -60,43 +60,50 @@ declare module 'fastify' {
6060
> extends WebsocketRouteOptions<RawServer, RawRequest, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> { }
6161
}
6262

63-
declare const websocketPlugin: FastifyPluginCallback<WebsocketPluginOptions>;
63+
type FastifyWebsocket = FastifyPluginCallback<fastifyWebsocket.WebsocketPluginOptions>;
6464

65-
interface WebSocketServerOptions extends Omit<WebSocket.ServerOptions, "path"> { }
65+
declare namespace fastifyWebsocket {
6666

67-
export type WebsocketHandler<
68-
RawServer extends RawServerBase = RawServerDefault,
69-
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
70-
RequestGeneric extends RequestGenericInterface = RequestGenericInterface,
71-
ContextConfig = ContextConfigDefault,
72-
SchemaCompiler extends FastifySchema = FastifySchema,
73-
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
74-
Logger extends FastifyBaseLogger = FastifyBaseLogger
75-
> = (
76-
this: FastifyInstance<Server, IncomingMessage, ServerResponse>,
77-
connection: SocketStream,
78-
request: FastifyRequest<RequestGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>
79-
) => void | Promise<any>;
67+
interface WebSocketServerOptions extends Omit<WebSocket.ServerOptions, "path"> { }
68+
69+
export type WebsocketHandler<
70+
RawServer extends RawServerBase = RawServerDefault,
71+
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
72+
RequestGeneric extends RequestGenericInterface = RequestGenericInterface,
73+
ContextConfig = ContextConfigDefault,
74+
SchemaCompiler extends FastifySchema = FastifySchema,
75+
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
76+
Logger extends FastifyBaseLogger = FastifyBaseLogger
77+
> = (
78+
this: FastifyInstance<Server, IncomingMessage, ServerResponse>,
79+
connection: SocketStream,
80+
request: FastifyRequest<RequestGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>
81+
) => void | Promise<any>;
82+
83+
export interface SocketStream extends Duplex {
84+
socket: WebSocket;
85+
}
86+
87+
export interface WebsocketPluginOptions {
88+
errorHandler?: (this: FastifyInstance, error: Error, connection: SocketStream, request: FastifyRequest, reply: FastifyReply) => void;
89+
options?: WebSocketServerOptions;
90+
connectionOptions?: DuplexOptions;
91+
}
8092

81-
export interface SocketStream extends Duplex {
82-
socket: WebSocket;
83-
}
93+
export interface RouteOptions<
94+
RawServer extends RawServerBase = RawServerDefault,
95+
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
96+
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
97+
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
98+
ContextConfig = ContextConfigDefault,
99+
SchemaCompiler extends fastify.FastifySchema = fastify.FastifySchema,
100+
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
101+
Logger extends FastifyBaseLogger = FastifyBaseLogger
102+
> extends fastify.RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>, WebsocketRouteOptions<RawServer, RawRequest, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> { }
84103

85-
export interface WebsocketPluginOptions {
86-
errorHandler?: (this: FastifyInstance, error: Error, connection: SocketStream, request: FastifyRequest, reply: FastifyReply) => void;
87-
options?: WebSocketServerOptions;
88-
connectionOptions?: DuplexOptions;
104+
export const websocketPlugin: FastifyWebsocket
105+
export { websocketPlugin as default }
89106
}
90107

91-
export interface RouteOptions<
92-
RawServer extends RawServerBase = RawServerDefault,
93-
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
94-
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
95-
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
96-
ContextConfig = ContextConfigDefault,
97-
SchemaCompiler extends fastify.FastifySchema = fastify.FastifySchema,
98-
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
99-
Logger extends FastifyBaseLogger = FastifyBaseLogger
100-
> extends fastify.RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>, WebsocketRouteOptions<RawServer, RawRequest, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> { }
101-
102-
export default websocketPlugin;
108+
declare function fastifyWebsocket(...params: Parameters<FastifyWebsocket>): ReturnType<FastifyWebsocket>
109+
export = fastifyWebsocket

0 commit comments

Comments
 (0)