@trpc/server
Advanced tools
Comparing version 11.0.0-rc.528 to 11.0.0-rc.530
@@ -30,3 +30,3 @@ import type { IncomingMessage } from 'http'; | ||
* Heartbeat interval in milliseconds | ||
* @default 30000 | ||
* @default 30_000 | ||
*/ | ||
@@ -36,8 +36,14 @@ pingMs?: number; | ||
* Terminate the WebSocket if no pong is received after this many milliseconds | ||
* @default 5000 | ||
* @default 5_000 | ||
*/ | ||
pongWaitMs?: number; | ||
}; | ||
/** | ||
* Disable responding to ping messages from the client | ||
* **Not recommended** - this is mainly used for testing | ||
* @default false | ||
*/ | ||
dangerouslyDisablePong?: boolean; | ||
}; | ||
export declare function getWSConnectionHandler<TRouter extends AnyRouter>(opts: WSConnectionHandlerOptions<TRouter>): (client: ws.WebSocket, req: IncomingMessage) => Promise<void>; | ||
export declare function getWSConnectionHandler<TRouter extends AnyRouter>(opts: WSSHandlerOptions<TRouter>): (client: ws.WebSocket, req: IncomingMessage) => Promise<void>; | ||
/** | ||
@@ -44,0 +50,0 @@ * Handle WebSocket keep-alive messages |
@@ -26,2 +26,6 @@ 'use strict'; | ||
const abortController = new AbortController(); | ||
if (opts.keepAlive?.enabled) { | ||
const { pingMs , pongWaitMs } = opts.keepAlive; | ||
handleKeepAlive(client, pingMs, pongWaitMs); | ||
} | ||
function respond(untransformedJSON) { | ||
@@ -304,3 +308,10 @@ client.send(JSON.stringify(transformer.transformTRPCResponse(router$1._def._config, untransformedJSON))); | ||
try { | ||
const msgJSON = JSON.parse(message.toString()); | ||
const str = message.toString(); | ||
if (str === 'PING') { | ||
if (!opts.dangerouslyDisablePong) { | ||
client.send('PONG'); | ||
} | ||
return; | ||
} | ||
const msgJSON = JSON.parse(str); | ||
const msgs = Array.isArray(msgJSON) ? msgJSON : [ | ||
@@ -351,4 +362,3 @@ msgJSON | ||
if (ctxPromise !== unsetContextPromiseSymbol) { | ||
// prevent unhandled promise rejection errors | ||
await ctxPromise.catch(()=>null); | ||
await ctxPromise; | ||
} | ||
@@ -383,13 +393,22 @@ }; | ||
function applyWSSHandler(opts) { | ||
const { wss , prefix , keepAlive } = opts; | ||
const onConnection = getWSConnectionHandler(opts); | ||
wss.on('connection', async (client, req)=>{ | ||
if (prefix && !req.url?.startsWith(prefix)) { | ||
opts.wss.on('connection', (client, req)=>{ | ||
if (opts.prefix && !req.url?.startsWith(opts.prefix)) { | ||
return; | ||
} | ||
await onConnection(client, req); | ||
if (keepAlive?.enabled) { | ||
const { pingMs , pongWaitMs } = keepAlive; | ||
handleKeepAlive(client, pingMs, pongWaitMs); | ||
} | ||
onConnection(client, req).catch((cause)=>{ | ||
opts.onError?.({ | ||
error: new TRPCError.TRPCError({ | ||
code: 'INTERNAL_SERVER_ERROR', | ||
cause, | ||
message: 'Failed to handle WebSocket connection' | ||
}), | ||
req: req, | ||
path: undefined, | ||
type: 'unknown', | ||
ctx: undefined, | ||
input: undefined | ||
}); | ||
client.close(); | ||
}); | ||
}); | ||
@@ -403,3 +422,3 @@ return { | ||
const data = JSON.stringify(response); | ||
for (const client of wss.clients){ | ||
for (const client of opts.wss.clients){ | ||
if (client.readyState === WEBSOCKET_OPEN) { | ||
@@ -406,0 +425,0 @@ client.send(data); |
{ | ||
"bundleSize": 135647, | ||
"bundleOrigSize": 185375, | ||
"bundleReduction": 26.83, | ||
"bundleSize": 136257, | ||
"bundleOrigSize": 186007, | ||
"bundleReduction": 26.75, | ||
"modules": [ | ||
@@ -17,3 +17,3 @@ { | ||
], | ||
"percent": 13.2, | ||
"percent": 13.15, | ||
"reduction": 0 | ||
@@ -35,3 +35,3 @@ }, | ||
], | ||
"percent": 12.95, | ||
"percent": 12.89, | ||
"reduction": 4.31 | ||
@@ -41,4 +41,4 @@ }, | ||
"id": "/src/adapters/ws.ts", | ||
"size": 15782, | ||
"origSize": 14714, | ||
"size": 16392, | ||
"origSize": 15346, | ||
"renderedExports": [ | ||
@@ -53,3 +53,3 @@ "getWSConnectionHandler", | ||
], | ||
"percent": 11.63, | ||
"percent": 12.03, | ||
"reduction": 0 | ||
@@ -69,3 +69,3 @@ }, | ||
], | ||
"percent": 5.67, | ||
"percent": 5.64, | ||
"reduction": 0 | ||
@@ -88,3 +88,3 @@ }, | ||
], | ||
"percent": 4.73, | ||
"percent": 4.71, | ||
"reduction": 40.18 | ||
@@ -106,3 +106,3 @@ }, | ||
], | ||
"percent": 4.36, | ||
"percent": 4.34, | ||
"reduction": 15.22 | ||
@@ -122,3 +122,3 @@ }, | ||
], | ||
"percent": 4.27, | ||
"percent": 4.25, | ||
"reduction": 63.05 | ||
@@ -137,3 +137,3 @@ }, | ||
], | ||
"percent": 3.99, | ||
"percent": 3.98, | ||
"reduction": 11.38 | ||
@@ -158,3 +158,3 @@ }, | ||
], | ||
"percent": 3.03, | ||
"percent": 3.02, | ||
"reduction": 0 | ||
@@ -173,3 +173,3 @@ }, | ||
], | ||
"percent": 2.37, | ||
"percent": 2.36, | ||
"reduction": 22.57 | ||
@@ -188,3 +188,3 @@ }, | ||
], | ||
"percent": 2.11, | ||
"percent": 2.1, | ||
"reduction": 6.07 | ||
@@ -205,3 +205,3 @@ }, | ||
], | ||
"percent": 2.03, | ||
"percent": 2.02, | ||
"reduction": 0 | ||
@@ -222,7 +222,7 @@ }, | ||
"/src/unstable-core-do-not-import.ts", | ||
"/src/unstable-core-do-not-import/http/resolveResponse.ts", | ||
"/src/unstable-core-do-not-import/initTRPC.ts", | ||
"/src/unstable-core-do-not-import/router.ts", | ||
"/src/unstable-core-do-not-import/http/resolveResponse.ts" | ||
"/src/unstable-core-do-not-import/router.ts" | ||
], | ||
"percent": 2.02, | ||
"percent": 2.01, | ||
"reduction": 45.94 | ||
@@ -241,3 +241,3 @@ }, | ||
], | ||
"percent": 1.99, | ||
"percent": 1.98, | ||
"reduction": 40.91 | ||
@@ -262,3 +262,3 @@ }, | ||
], | ||
"percent": 1.94, | ||
"percent": 1.93, | ||
"reduction": 55.77 | ||
@@ -279,3 +279,3 @@ }, | ||
], | ||
"percent": 1.72, | ||
"percent": 1.71, | ||
"reduction": 0 | ||
@@ -294,3 +294,3 @@ }, | ||
], | ||
"percent": 1.66, | ||
"percent": 1.65, | ||
"reduction": 2.17 | ||
@@ -310,3 +310,3 @@ }, | ||
], | ||
"percent": 1.62, | ||
"percent": 1.61, | ||
"reduction": 13.79 | ||
@@ -325,3 +325,3 @@ }, | ||
], | ||
"percent": 1.51, | ||
"percent": 1.5, | ||
"reduction": 5.79 | ||
@@ -355,6 +355,6 @@ }, | ||
"/src/unstable-core-do-not-import.ts", | ||
"/src/unstable-core-do-not-import/http/parseConnectionParams.ts", | ||
"/src/unstable-core-do-not-import/http/resolveResponse.ts", | ||
"/src/unstable-core-do-not-import/middleware.ts", | ||
"/src/unstable-core-do-not-import/router.ts", | ||
"/src/unstable-core-do-not-import/http/parseConnectionParams.ts", | ||
"/src/unstable-core-do-not-import/http/resolveResponse.ts", | ||
"/src/unstable-core-do-not-import/http/contentType.ts", | ||
@@ -365,3 +365,3 @@ "/src/unstable-core-do-not-import/procedureBuilder.ts", | ||
], | ||
"percent": 1.28, | ||
"percent": 1.27, | ||
"reduction": 19.47 | ||
@@ -391,3 +391,3 @@ }, | ||
"dependents": [], | ||
"percent": 1.19, | ||
"percent": 1.18, | ||
"reduction": 27.47 | ||
@@ -404,3 +404,3 @@ }, | ||
"dependents": [], | ||
"percent": 1.14, | ||
"percent": 1.13, | ||
"reduction": 26.94 | ||
@@ -419,4 +419,4 @@ }, | ||
"/src/unstable-core-do-not-import.ts", | ||
"/src/unstable-core-do-not-import/error/getErrorShape.ts", | ||
"/src/unstable-core-do-not-import/http/resolveResponse.ts" | ||
"/src/unstable-core-do-not-import/http/resolveResponse.ts", | ||
"/src/unstable-core-do-not-import/error/getErrorShape.ts" | ||
], | ||
@@ -438,3 +438,3 @@ "percent": 1.06, | ||
], | ||
"percent": 0.96, | ||
"percent": 0.95, | ||
"reduction": 55.21 | ||
@@ -458,2 +458,5 @@ }, | ||
"/src/unstable-core-do-not-import.ts", | ||
"/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts", | ||
"/src/unstable-core-do-not-import/http/parseConnectionParams.ts", | ||
"/src/unstable-core-do-not-import/http/resolveResponse.ts", | ||
"/src/unstable-core-do-not-import/error/TRPCError.ts", | ||
@@ -463,8 +466,5 @@ "/src/unstable-core-do-not-import/transformer.ts", | ||
"/src/unstable-core-do-not-import/router.ts", | ||
"/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts", | ||
"/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts", | ||
"/src/unstable-core-do-not-import/http/parseConnectionParams.ts", | ||
"/src/unstable-core-do-not-import/http/resolveResponse.ts", | ||
"/src/unstable-core-do-not-import/http/contentType.ts", | ||
"/src/unstable-core-do-not-import/procedureBuilder.ts", | ||
"/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts", | ||
"/src/unstable-core-do-not-import/stream/jsonl.ts", | ||
@@ -535,3 +535,3 @@ "/src/unstable-core-do-not-import/stream/sse.ts" | ||
], | ||
"percent": 0.73, | ||
"percent": 0.72, | ||
"reduction": 13.65 | ||
@@ -571,3 +571,3 @@ }, | ||
], | ||
"percent": 0.59, | ||
"percent": 0.58, | ||
"reduction": 44.65 | ||
@@ -760,8 +760,10 @@ }, | ||
{ | ||
"id": "/src/index.ts", | ||
"id": "/src/http.ts", | ||
"size": 0, | ||
"origSize": 32, | ||
"origSize": 37, | ||
"renderedExports": [], | ||
"removedExports": [], | ||
"dependents": [], | ||
"dependents": [ | ||
"/src/adapters/ws.ts" | ||
], | ||
"percent": 0, | ||
@@ -771,5 +773,5 @@ "reduction": 100 | ||
{ | ||
"id": "/src/rpc.ts", | ||
"id": "/src/index.ts", | ||
"size": 0, | ||
"origSize": 36, | ||
"origSize": 32, | ||
"renderedExports": [], | ||
@@ -782,9 +784,10 @@ "removedExports": [], | ||
{ | ||
"id": "/src/http.ts", | ||
"id": "/src/unstable-core-do-not-import.ts", | ||
"size": 0, | ||
"origSize": 37, | ||
"origSize": 2306, | ||
"renderedExports": [], | ||
"removedExports": [], | ||
"dependents": [ | ||
"/src/adapters/ws.ts" | ||
"/src/adapters/ws.ts", | ||
"/src/adapters/next-app-dir/nextAppDirCaller.ts" | ||
], | ||
@@ -795,5 +798,5 @@ "percent": 0, | ||
{ | ||
"id": "/src/shared.ts", | ||
"id": "/src/rpc.ts", | ||
"size": 0, | ||
"origSize": 653, | ||
"origSize": 36, | ||
"renderedExports": [], | ||
@@ -806,11 +809,8 @@ "removedExports": [], | ||
{ | ||
"id": "/src/unstable-core-do-not-import.ts", | ||
"id": "/src/shared.ts", | ||
"size": 0, | ||
"origSize": 2306, | ||
"origSize": 653, | ||
"renderedExports": [], | ||
"removedExports": [], | ||
"dependents": [ | ||
"/src/adapters/ws.ts", | ||
"/src/adapters/next-app-dir/nextAppDirCaller.ts" | ||
], | ||
"dependents": [], | ||
"percent": 0, | ||
@@ -817,0 +817,0 @@ "reduction": 100 |
{ | ||
"name": "@trpc/server", | ||
"version": "11.0.0-rc.528+32e6b1285", | ||
"version": "11.0.0-rc.530+d1e8f33f6", | ||
"description": "The tRPC server library", | ||
@@ -152,3 +152,3 @@ "author": "KATT", | ||
], | ||
"gitHead": "32e6b1285dd844776d323ae23fbea638312d676e" | ||
"gitHead": "d1e8f33f6bc6104003f4a0d4030c6cf306e6dec2" | ||
} |
@@ -82,3 +82,3 @@ import type { IncomingMessage } from 'http'; | ||
* Heartbeat interval in milliseconds | ||
* @default 30000 | ||
* @default 30_000 | ||
*/ | ||
@@ -88,6 +88,12 @@ pingMs?: number; | ||
* Terminate the WebSocket if no pong is received after this many milliseconds | ||
* @default 5000 | ||
* @default 5_000 | ||
*/ | ||
pongWaitMs?: number; | ||
}; | ||
/** | ||
* Disable responding to ping messages from the client | ||
* **Not recommended** - this is mainly used for testing | ||
* @default false | ||
*/ | ||
dangerouslyDisablePong?: boolean; | ||
}; | ||
@@ -97,3 +103,3 @@ | ||
export function getWSConnectionHandler<TRouter extends AnyRouter>( | ||
opts: WSConnectionHandlerOptions<TRouter>, | ||
opts: WSSHandlerOptions<TRouter>, | ||
) { | ||
@@ -107,2 +113,7 @@ const { createContext, router } = opts; | ||
if (opts.keepAlive?.enabled) { | ||
const { pingMs, pongWaitMs } = opts.keepAlive; | ||
handleKeepAlive(client, pingMs, pongWaitMs); | ||
} | ||
function respond(untransformedJSON: TRPCResponseMessage) { | ||
@@ -410,3 +421,11 @@ client.send( | ||
try { | ||
const msgJSON: unknown = JSON.parse(message.toString()); | ||
const str = message.toString(); | ||
if (str === 'PING') { | ||
if (!opts.dangerouslyDisablePong) { | ||
client.send('PONG'); | ||
} | ||
return; | ||
} | ||
const msgJSON: unknown = JSON.parse(str); | ||
const msgs: unknown[] = Array.isArray(msgJSON) ? msgJSON : [msgJSON]; | ||
@@ -461,4 +480,3 @@ const promises = msgs | ||
if (ctxPromise !== unsetContextPromiseSymbol) { | ||
// prevent unhandled promise rejection errors | ||
await ctxPromise.catch(() => null); | ||
await ctxPromise; | ||
} | ||
@@ -473,4 +491,4 @@ }; | ||
client: ws.WebSocket, | ||
pingMs = 30000, | ||
pongWaitMs = 5000, | ||
pingMs = 30_000, | ||
pongWaitMs = 5_000, | ||
) { | ||
@@ -503,15 +521,24 @@ let heartbeatTimeout: NodeJS.Timeout | undefined; | ||
) { | ||
const { wss, prefix, keepAlive } = opts; | ||
const onConnection = getWSConnectionHandler(opts); | ||
wss.on('connection', async (client, req) => { | ||
if (prefix && !req.url?.startsWith(prefix)) { | ||
opts.wss.on('connection', (client, req) => { | ||
if (opts.prefix && !req.url?.startsWith(opts.prefix)) { | ||
return; | ||
} | ||
await onConnection(client, req); | ||
if (keepAlive?.enabled) { | ||
const { pingMs, pongWaitMs } = keepAlive; | ||
handleKeepAlive(client, pingMs, pongWaitMs); | ||
} | ||
onConnection(client, req).catch((cause) => { | ||
opts.onError?.({ | ||
error: new TRPCError({ | ||
code: 'INTERNAL_SERVER_ERROR', | ||
cause, | ||
message: 'Failed to handle WebSocket connection', | ||
}), | ||
req: req, | ||
path: undefined, | ||
type: 'unknown', | ||
ctx: undefined, | ||
input: undefined, | ||
}); | ||
client.close(); | ||
}); | ||
}); | ||
@@ -526,3 +553,3 @@ | ||
const data = JSON.stringify(response); | ||
for (const client of wss.clients) { | ||
for (const client of opts.wss.clients) { | ||
if (client.readyState === WEBSOCKET_OPEN) { | ||
@@ -529,0 +556,0 @@ client.send(data); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
723637
18818