@fastify/websocket
Advanced tools
Comparing version 8.2.0 to 8.3.1
'use strict' | ||
const { ServerResponse } = require('http') | ||
const { ServerResponse } = require('node:http') | ||
const fp = require('fastify-plugin') | ||
@@ -5,0 +5,0 @@ const WebSocket = require('ws') |
{ | ||
"name": "@fastify/websocket", | ||
"version": "8.2.0", | ||
"version": "8.3.1", | ||
"description": "basic websocket support for fastify", | ||
"main": "index.js", | ||
"type": "commonjs", | ||
"types": "types/index.d.ts", | ||
@@ -32,7 +33,8 @@ "scripts": { | ||
"@types/ws": "^8.2.2", | ||
"fastify": "^4.16.0", | ||
"fastify": "^4.25.0", | ||
"fastify-tsconfig": "^2.0.0", | ||
"split2": "^4.1.0", | ||
"standard": "^17.0.0", | ||
"tap": "^16.0.0", | ||
"tsd": "^0.28.0" | ||
"tsd": "^0.29.0" | ||
}, | ||
@@ -39,0 +41,0 @@ "dependencies": { |
@@ -150,3 +150,3 @@ # @fastify/websocket | ||
If you need to handle both HTTP requests and incoming socket connections on the same route, you can still do it using the [full declaration syntax](https://www.fastify.io/docs/latest/Routes/#full-declaration), adding a `wsHandler` property. | ||
If you need to handle both HTTP requests and incoming socket connections on the same route, you can still do it using the [full declaration syntax](https://fastify.dev/docs/latest/Reference/Routes/#full-declaration), adding a `wsHandler` property. | ||
@@ -153,0 +153,0 @@ ```js |
'use strict' | ||
const http = require('http') | ||
const util = require('util') | ||
const http = require('node:http') | ||
const util = require('node:util') | ||
const split = require('split2') | ||
@@ -10,7 +10,7 @@ const test = require('tap').test | ||
const WebSocket = require('ws') | ||
const { once, on } = require('events') | ||
const { once, on } = require('node:events') | ||
let timersPromises | ||
try { | ||
timersPromises = require('timers/promises') | ||
timersPromises = require('node:timers/promises') | ||
} catch {} | ||
@@ -328,8 +328,3 @@ | ||
fastify.get('/', { websocket: true }, (connection) => { | ||
// readableObjectMode was added in Node v12.3.0 so for earlier versions | ||
// we check the encapsulated readable state directly | ||
const mode = (typeof connection.readableObjectMode === 'undefined') | ||
? connection._readableState.objectMode | ||
: connection.readableObjectMode | ||
t.equal(mode, true) | ||
t.equal(connection.readableObjectMode, true) | ||
connection.socket.binaryType = 'arraybuffer' | ||
@@ -336,0 +331,0 @@ |
'use strict' | ||
const test = require('tap').test | ||
const net = require('net') | ||
const net = require('node:net') | ||
const Fastify = require('fastify') | ||
@@ -6,0 +6,0 @@ const fastifyWebsocket = require('..') |
'use strict' | ||
const net = require('net') | ||
const net = require('node:net') | ||
const test = require('tap').test | ||
@@ -8,3 +8,3 @@ const Fastify = require('fastify') | ||
const WebSocket = require('ws') | ||
const get = require('http').get | ||
const get = require('node:http').get | ||
@@ -11,0 +11,0 @@ test('Should expose a websocket on prefixed route', t => { |
@@ -5,3 +5,3 @@ /// <reference types="node" /> | ||
import * as fastify from 'fastify'; | ||
import WebSocket from 'ws'; | ||
import * as WebSocket from 'ws'; | ||
import { Duplex, DuplexOptions } from 'stream'; | ||
@@ -32,3 +32,3 @@ import { FastifyReply } from 'fastify/types/reply'; | ||
interface FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider> { | ||
get: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>, | ||
get: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider, Logger>, | ||
websocketServer: WebSocket.Server, | ||
@@ -46,8 +46,9 @@ } | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, | ||
Logger extends FastifyBaseLogger = FastifyBaseLogger, | ||
> { | ||
<RequestGeneric extends RequestGenericInterface = RequestGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler extends FastifySchema = FastifySchema, Logger extends FastifyBaseLogger = FastifyBaseLogger>( | ||
<RequestGeneric extends RequestGenericInterface = RequestGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler extends FastifySchema = FastifySchema, InnerLogger extends Logger = Logger>( | ||
path: string, | ||
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 | ||
handler?: fastifyWebsocket.WebsocketHandler<RawServer, RawRequest, RequestGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> | ||
): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>; | ||
opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig, SchemaCompiler, TypeProvider, InnerLogger> & { websocket: true }, // this creates an overload that only applies these different types if the handler is for websockets | ||
handler?: fastifyWebsocket.WebsocketHandler<RawServer, RawRequest, RequestGeneric, ContextConfig, SchemaCompiler, TypeProvider, InnerLogger> | ||
): FastifyInstance<RawServer, RawRequest, RawReply, InnerLogger, TypeProvider>; | ||
} | ||
@@ -109,7 +110,7 @@ | ||
export const websocketPlugin: FastifyWebsocket | ||
export { websocketPlugin as default } | ||
export const fastifyWebsocket: FastifyWebsocket | ||
export { fastifyWebsocket as default } | ||
} | ||
declare function fastifyWebsocket(...params: Parameters<FastifyWebsocket>): ReturnType<FastifyWebsocket> | ||
export = fastifyWebsocket | ||
export = fastifyWebsocket |
@@ -1,4 +0,4 @@ | ||
import wsPlugin, { WebsocketHandler, SocketStream } from '..'; | ||
import fastifyWebsocket, { WebsocketHandler, SocketStream, fastifyWebsocket as namedFastifyWebsocket, default as defaultFastifyWebsocket } from '..'; | ||
import type { IncomingMessage } from "http"; | ||
import fastify, { RouteOptions, FastifyRequest, FastifyInstance, FastifyReply, RequestGenericInterface, FastifyBaseLogger, RawServerDefault, FastifySchema, RawRequestDefaultExpression, RawServerBase, ContextConfigDefault, RawReplyDefaultExpression } from 'fastify'; | ||
import fastify, { RouteOptions, FastifyRequest, FastifyInstance, FastifyReply, RequestGenericInterface, FastifyBaseLogger, RawServerDefault, FastifySchema, RawRequestDefaultExpression } from 'fastify'; | ||
import { expectType } from 'tsd'; | ||
@@ -8,10 +8,9 @@ import { Server } from 'ws'; | ||
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'; | ||
import { Static, Type } from '@sinclair/typebox' | ||
import { ResolveFastifyRequestType } from 'fastify/types/type-provider'; | ||
import { Type } from '@sinclair/typebox' | ||
const app: FastifyInstance = fastify(); | ||
app.register(wsPlugin); | ||
app.register(wsPlugin, {}); | ||
app.register(wsPlugin, { options: { maxPayload: 123 } }); | ||
app.register(wsPlugin, { | ||
app.register(fastifyWebsocket); | ||
app.register(fastifyWebsocket, {}); | ||
app.register(fastifyWebsocket, { options: { maxPayload: 123 } }); | ||
app.register(fastifyWebsocket, { | ||
errorHandler: function errorHandler(error: Error, connection: SocketStream, request: FastifyRequest, reply: FastifyReply): void { | ||
@@ -25,5 +24,5 @@ expectType<FastifyInstance>(this); | ||
}); | ||
app.register(wsPlugin, { options: { perMessageDeflate: true } }); | ||
app.register(wsPlugin, { preClose: function syncPreclose() {} }); | ||
app.register(wsPlugin, { preClose: async function asyncPreclose(){} }); | ||
app.register(fastifyWebsocket, { options: { perMessageDeflate: true } }); | ||
app.register(fastifyWebsocket, { preClose: function syncPreclose() {} }); | ||
app.register(fastifyWebsocket, { preClose: async function asyncPreclose(){} }); | ||
@@ -36,2 +35,3 @@ app.get('/websockets-via-inferrence', { websocket: true }, async function (connection, request) { | ||
expectType<boolean>(request.ws); | ||
expectType<FastifyBaseLogger>(request.log); | ||
}); | ||
@@ -132,8 +132,2 @@ | ||
}; | ||
type SchemaType = { | ||
params: Static<typeof schema.params>; | ||
querystring: Static<typeof schema.querystring>; | ||
body: Static<typeof schema.body>; | ||
headers: Static<typeof schema.headers>; | ||
}; | ||
@@ -161,33 +155,2 @@ const server = app.withTypeProvider<TypeBoxTypeProvider>(); | ||
server.get('/websockets-type-inference', | ||
{ | ||
websocket: true, | ||
schema | ||
}, | ||
async function (connection, request) { | ||
expectType<FastifyInstance>(this); | ||
expectType<SocketStream>(connection); | ||
expectType<Server>(app.websocketServer); | ||
expectType<FastifyRequest<RequestGenericInterface, RawServerDefault, IncomingMessage, SchemaType, TypeBoxTypeProvider, unknown, FastifyBaseLogger>>(request); | ||
expectType<boolean>(request.ws); | ||
expectType<{ foo: string }>(request.params); | ||
expectType<{ bar: string }>(request.body); | ||
expectType<{ search: string }>(request.query); | ||
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers); | ||
}); | ||
server.get('/not-websockets-type-inference', | ||
{ | ||
websocket: false, | ||
schema | ||
}, | ||
async (request, reply) => { | ||
expectType<FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, SchemaType, TypeBoxTypeProvider, unknown, FastifyBaseLogger, ResolveFastifyRequestType<TypeBoxTypeProvider, FastifySchema, RouteGenericInterface>>>(request); | ||
expectType<FastifyReply<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, RouteGenericInterface, ContextConfigDefault, SchemaType, TypeBoxTypeProvider>>(reply); | ||
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', | ||
@@ -205,2 +168,6 @@ { websocket: true }, | ||
expectType<IncomingMessage['headers']>(request.headers); | ||
}); | ||
}); | ||
expectType<typeof fastifyWebsocket>(namedFastifyWebsocket); | ||
expectType<typeof fastifyWebsocket>(defaultFastifyWebsocket); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
15
0
83503
10
1875