Skip to content

Commit

Permalink
build(deps-dev): replace standard with neostandard (#311)
Browse files Browse the repository at this point in the history
* build(deps-dev): replace standard with neostandard

* chore: add eslint.config.js

* chore:rebase

* Update package.json

Signed-off-by: Frazer Smith <frazer.dev@icloud.com>

* chore: ignore

---------

Signed-off-by: Frazer Smith <frazer.dev@icloud.com>
  • Loading branch information
Fdawgs authored Jan 16, 2025
1 parent 0a6978b commit ddb583b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 104 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![CI](https://github.com/fastify/fastify-websocket/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-websocket/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/@fastify/websocket.svg?style=flat)](https://www.npmjs.com/package/@fastify/websocket)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

WebSocket support for [Fastify](https://github.com/fastify/fastify).
Built upon [ws@8](https://www.npmjs.com/package/ws).
Expand Down
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

module.exports = require('neostandard')({
ignores: require('neostandard').resolveIgnoresFromGitignore(),
ts: true
})
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"type": "commonjs",
"types": "types/index.d.ts",
"scripts": {
"lint": "standard",
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:typescript": "tsd"
Expand Down Expand Up @@ -59,10 +60,11 @@
"@fastify/type-provider-typebox": "^5.0.0",
"@types/node": "^22.0.0",
"@types/ws": "^8.5.10",
"eslint": "^9.17.0",
"fastify": "^5.0.0",
"fastify-tsconfig": "^2.0.0",
"neostandard": "^0.12.0",
"split2": "^4.2.0",
"standard": "^17.1.0",
"tap": "^18.7.1",
"tsd": "^0.31.0"
},
Expand Down
29 changes: 16 additions & 13 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/// <reference types="node" />
import { IncomingMessage, ServerResponse, Server } from 'node:http';
import { FastifyRequest, FastifyPluginCallback, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, RequestGenericInterface, ContextConfigDefault, FastifyInstance, FastifySchema, FastifyTypeProvider, FastifyTypeProviderDefault, FastifyBaseLogger } from 'fastify';
import * as fastify from 'fastify';
import * as WebSocket from 'ws';
import { FastifyReply } from 'fastify/types/reply';
import { preCloseHookHandler, preCloseAsyncHookHandler } from 'fastify/types/hooks';
import { RouteGenericInterface } from 'fastify/types/route';
import { IncomingMessage, ServerResponse, Server } from 'node:http'
import { FastifyRequest, FastifyPluginCallback, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, RequestGenericInterface, ContextConfigDefault, FastifyInstance, FastifySchema, FastifyTypeProvider, FastifyTypeProviderDefault, FastifyBaseLogger } from 'fastify'
import * as fastify from 'fastify'
import * as WebSocket from 'ws'
import { FastifyReply } from 'fastify/types/reply'
import { preCloseHookHandler, preCloseAsyncHookHandler } from 'fastify/types/hooks'
import { RouteGenericInterface } from 'fastify/types/route'

interface WebsocketRouteOptions<
RawServer extends RawServerBase = RawServerDefault,
Expand All @@ -21,6 +21,7 @@ interface WebsocketRouteOptions<

declare module 'fastify' {
interface RouteShorthandOptions<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
RawServer extends RawServerBase = RawServerDefault
> {
websocket?: boolean;
Expand All @@ -29,6 +30,7 @@ declare module 'fastify' {
type InjectWSFn<RawRequest> =
((path?: string, upgradeContext?: Partial<RawRequest>) => Promise<WebSocket>)

// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider> {
websocketServer: WebSocket.Server,
injectWS: InjectWSFn<RawRequest>
Expand All @@ -43,7 +45,7 @@ declare module 'fastify' {
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
Logger extends FastifyBaseLogger = FastifyBaseLogger,
Logger extends FastifyBaseLogger = FastifyBaseLogger
> {
<RequestGeneric extends RequestGenericInterface = RequestGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler extends FastifySchema = FastifySchema, InnerLogger extends Logger = Logger>(
path: string,
Expand All @@ -55,6 +57,7 @@ declare module 'fastify' {
interface RouteOptions<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault,
Expand All @@ -64,11 +67,11 @@ declare module 'fastify' {
> extends WebsocketRouteOptions<RawServer, RawRequest, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> { }
}

type FastifyWebsocket = FastifyPluginCallback<fastifyWebsocket.WebsocketPluginOptions>;
type FastifyWebsocket = FastifyPluginCallback<fastifyWebsocket.WebsocketPluginOptions>

declare namespace fastifyWebsocket {

interface WebSocketServerOptions extends Omit<WebSocket.ServerOptions, "path"> { }
interface WebSocketServerOptions extends Omit<WebSocket.ServerOptions, 'path'> { }
export type WebsocketHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
Expand All @@ -81,7 +84,7 @@ declare namespace fastifyWebsocket {
this: FastifyInstance<Server, IncomingMessage, ServerResponse>,
socket: WebSocket.WebSocket,
request: FastifyRequest<RequestGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>
) => void | Promise<any>;
) => void | Promise<any>
export interface WebsocketPluginOptions {
errorHandler?: (this: FastifyInstance, error: Error, socket: WebSocket.WebSocket, request: FastifyRequest, reply: FastifyReply) => void;
options?: WebSocketServerOptions;
Expand All @@ -98,11 +101,11 @@ declare namespace fastifyWebsocket {
Logger extends FastifyBaseLogger = FastifyBaseLogger
> extends fastify.RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>, WebsocketRouteOptions<RawServer, RawRequest, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> { }

export type WebSocket = WebSocket.WebSocket;
export type WebSocket = WebSocket.WebSocket

export const fastifyWebsocket: FastifyWebsocket
export { fastifyWebsocket as default }
}

declare function fastifyWebsocket(...params: Parameters<FastifyWebsocket>): ReturnType<FastifyWebsocket>
declare function fastifyWebsocket (...params: Parameters<FastifyWebsocket>): ReturnType<FastifyWebsocket>
export = fastifyWebsocket
173 changes: 85 additions & 88 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,116 +1,114 @@
import fastifyWebsocket, { WebsocketHandler, fastifyWebsocket as namedFastifyWebsocket, default as defaultFastifyWebsocket, WebSocket } from '..';
import type { IncomingMessage } from "http";
import fastify, { RouteOptions, FastifyRequest, FastifyInstance, FastifyReply, RequestGenericInterface, FastifyBaseLogger, RawServerDefault, FastifySchema, RawRequestDefaultExpression } from 'fastify';
import { expectType } from 'tsd';
import { Server } from 'ws';
import { RouteGenericInterface } from 'fastify/types/route';
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
// eslint-disable-next-line import-x/no-named-default -- Testing default export
import fastifyWebsocket, { WebsocketHandler, fastifyWebsocket as namedFastifyWebsocket, default as defaultFastifyWebsocket, WebSocket } from '..'
import type { IncomingMessage } from 'http'
import fastify, { RouteOptions, FastifyRequest, FastifyInstance, FastifyReply, RequestGenericInterface, FastifyBaseLogger, RawServerDefault, FastifySchema, RawRequestDefaultExpression } from 'fastify'
import { expectType } from 'tsd'
import { Server } from 'ws'
import { RouteGenericInterface } from 'fastify/types/route'
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
import { Type } from '@sinclair/typebox'

const app: FastifyInstance = fastify();
app.register(fastifyWebsocket);
app.register(fastifyWebsocket, {});
app.register(fastifyWebsocket, { options: { maxPayload: 123 } });
const app: FastifyInstance = fastify()
app.register(fastifyWebsocket)
app.register(fastifyWebsocket, {})
app.register(fastifyWebsocket, { options: { maxPayload: 123 } })
app.register(fastifyWebsocket, {
errorHandler: function errorHandler(error: Error, socket: WebSocket, request: FastifyRequest, reply: FastifyReply): void {
expectType<FastifyInstance>(this);
errorHandler: function errorHandler (error: Error, socket: WebSocket, request: FastifyRequest, reply: FastifyReply): void {
expectType<FastifyInstance>(this)
expectType<Error>(error)
expectType<WebSocket>(socket)
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
}
});
app.register(fastifyWebsocket, { options: { perMessageDeflate: true } });
app.register(fastifyWebsocket, { preClose: function syncPreclose() {} });
app.register(fastifyWebsocket, { preClose: async function asyncPreclose(){} });
})
app.register(fastifyWebsocket, { options: { perMessageDeflate: true } })
app.register(fastifyWebsocket, { preClose: function syncPreclose () {} })
app.register(fastifyWebsocket, { preClose: async function asyncPreclose () {} })

app.get('/websockets-via-inferrence', { websocket: true }, async function (socket, request) {
expectType<FastifyInstance>(this);
expectType<WebSocket>(socket);
expectType<Server>(app.websocketServer);
expectType<FastifyInstance>(this)
expectType<WebSocket>(socket)
expectType<Server>(app.websocketServer)
expectType<FastifyRequest<RequestGenericInterface>>(request)
expectType<boolean>(request.ws);
expectType<FastifyBaseLogger>(request.log);
});
expectType<boolean>(request.ws)
expectType<FastifyBaseLogger>(request.log)
})

const handler: WebsocketHandler = async (socket, request) => {
expectType<WebSocket>(socket);
expectType<Server>(app.websocketServer);
expectType<WebSocket>(socket)
expectType<Server>(app.websocketServer)
expectType<FastifyRequest<RequestGenericInterface>>(request)
}

app.get('/websockets-via-annotated-const', { websocket: true }, handler);
app.get('/websockets-via-annotated-const', { websocket: true }, handler)

app.get('/not-specifed', async (request, reply) => {
expectType<FastifyRequest>(request);
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
expectType<boolean>(request.ws);
});
expectType<boolean>(request.ws)
})

app.get('/not-websockets', { websocket: false }, async (request, reply) => {
expectType<FastifyRequest>(request);
expectType<FastifyReply>(reply);
});
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
})

app.route({
method: 'GET',
url: '/route-full-declaration-syntax',
handler: (request, reply) => {
expectType<FastifyRequest>(request);
expectType<FastifyReply>(reply);
expectType<boolean>(request.ws);
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
expectType<boolean>(request.ws)
},
wsHandler: (socket, request) => {
expectType<WebSocket>(socket);
expectType<FastifyRequest<RouteGenericInterface>>(request);
expectType<boolean>(request.ws);
expectType<WebSocket>(socket)
expectType<FastifyRequest<RouteGenericInterface>>(request)
expectType<boolean>(request.ws)
},
});
})

const augmentedRouteOptions: RouteOptions = {
method: 'GET',
url: '/route-with-exported-augmented-route-options',
handler: (request, reply) => {
expectType<FastifyRequest>(request);
expectType<FastifyReply>(reply);
expectType<FastifyRequest>(request)
expectType<FastifyReply>(reply)
},
wsHandler: (socket, request) => {
expectType<WebSocket>(socket);
expectType<WebSocket>(socket)
expectType<FastifyRequest<RouteGenericInterface>>(request)
},
};
app.route(augmentedRouteOptions);

}
app.route(augmentedRouteOptions)

app.get<{ Params: { foo: string }, Body: { bar: string }, Querystring: { search: string }, Headers: { auth: string } }>('/shorthand-explicit-types', {
websocket: true
}, async (socket, request) => {
expectType<WebSocket>(socket);
expectType<{ foo: string }>(request.params);
expectType<{ bar: string }>(request.body);
expectType<{ search: string }>(request.query);
expectType< IncomingMessage['headers'] & { auth: string }>(request.headers);
});

expectType<WebSocket>(socket)
expectType<{ foo: string }>(request.params)
expectType<{ bar: string }>(request.body)
expectType<{ search: string }>(request.query)
expectType< IncomingMessage['headers'] & { auth: string }>(request.headers)
})

app.route<{ Params: { foo: string }, Body: { bar: string }, Querystring: { search: string }, Headers: { auth: string } }>({
method: 'GET',
url: '/longhand-explicit-types',
handler: (request, _reply) => {
expectType<{ foo: string }>(request.params);
expectType<{ bar: string }>(request.body);
expectType<{ search: string }>(request.query);
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers);
expectType<{ foo: string }>(request.params)
expectType<{ bar: string }>(request.body)
expectType<{ search: string }>(request.query)
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers)
},
wsHandler: (socket, request) => {
expectType<WebSocket>(socket);
expectType<{ foo: string }>(request.params);
expectType<{ bar: string }>(request.body);
expectType<{ search: string }>(request.query);
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers);
expectType<WebSocket>(socket)
expectType<{ foo: string }>(request.params)
expectType<{ bar: string }>(request.body)
expectType<{ search: string }>(request.query)
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers)
},
});

})

const schema = {
params: Type.Object({
Expand All @@ -125,43 +123,42 @@ const schema = {
headers: Type.Object({
auth: Type.String()
})
};
}

const server = app.withTypeProvider<TypeBoxTypeProvider>();
const server = app.withTypeProvider<TypeBoxTypeProvider>()

server.route({
method: 'GET',
url: '/longhand-type-inference',
schema,
handler: (request, _reply) => {
expectType<{ foo: string }>(request.params);
expectType<{ bar: string }>(request.body);
expectType<{ search: string }>(request.query);
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers);
expectType<{ foo: string }>(request.params)
expectType<{ bar: string }>(request.body)
expectType<{ search: string }>(request.query)
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers)
},
wsHandler: (socket, request) => {
expectType<WebSocket>(socket);
expectType<{ foo: string }>(request.params);
expectType<{ bar: string }>(request.body);
expectType<{ search: string }>(request.query);
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers);
expectType<WebSocket>(socket)
expectType<{ foo: string }>(request.params)
expectType<{ bar: string }>(request.body)
expectType<{ search: string }>(request.query)
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers)
},
});
})

server.get('/websockets-no-type-inference',
{ websocket: true },
async function (socket, request) {
expectType<FastifyInstance>(this);
expectType<WebSocket>(socket);
expectType<Server>(app.websocketServer);
expectType<FastifyRequest<RequestGenericInterface, RawServerDefault, RawRequestDefaultExpression, FastifySchema, TypeBoxTypeProvider, unknown, FastifyBaseLogger>>(request);
expectType<boolean>(request.ws);
expectType<unknown>(request.params);
expectType<unknown>(request.body);
expectType<unknown>(request.query);
expectType<IncomingMessage['headers']>(request.headers);
});

expectType<typeof fastifyWebsocket>(namedFastifyWebsocket);
expectType<typeof fastifyWebsocket>(defaultFastifyWebsocket);
expectType<FastifyInstance>(this)
expectType<WebSocket>(socket)
expectType<Server>(app.websocketServer)
expectType<FastifyRequest<RequestGenericInterface, RawServerDefault, RawRequestDefaultExpression, FastifySchema, TypeBoxTypeProvider, unknown, FastifyBaseLogger>>(request)
expectType<boolean>(request.ws)
expectType<unknown>(request.params)
expectType<unknown>(request.body)
expectType<unknown>(request.query)
expectType<IncomingMessage['headers']>(request.headers)
})

expectType<typeof fastifyWebsocket>(namedFastifyWebsocket)
expectType<typeof fastifyWebsocket>(defaultFastifyWebsocket)

0 comments on commit ddb583b

Please sign in to comment.