1- import { defineHooks , type Message , type WSError , type Peer } from 'crossws' ;
1+ import { defineHooks , type Message , type Peer , type WSError } from 'crossws' ;
22import { CloseCode , type ConnectionInitMessage } from '../common' ;
33import { handleProtocols , makeServer , type ServerOptions } from '../server' ;
44import { limitCloseReason } from '../utils' ;
@@ -16,83 +16,110 @@ export interface Extra {
1616}
1717
1818// TODO: Wait for https://github.com/unjs/crossws/pull/149 to merge
19- type UpgradeRequest = Request | {
20- url : string ;
21- headers : Headers ;
22- } ;
19+ type UpgradeRequest =
20+ | Request
21+ | {
22+ url : string ;
23+ headers : Headers ;
24+ } ;
2325type MaybePromise < T > = T | Promise < T > ;
2426
2527interface Hooks {
26- /** Upgrading */
27- /**
28- *
29- * @param request
30- * @throws {Response }
31- */
32- upgrade : ( request : UpgradeRequest & {
33- context : Peer [ "context" ] ;
34- } ) => MaybePromise < Response | ResponseInit | void > ;
35- /** A message is received */
36- message : ( peer : Peer , message : Message ) => MaybePromise < void > ;
37- /** A socket is opened */
38- open : ( peer : Peer ) => MaybePromise < void > ;
39- /** A socket is closed */
40- close : ( peer : Peer , details : {
41- code ?: number ;
42- reason ?: string ;
43- } ) => MaybePromise < void > ;
44- /** An error occurs */
45- error : ( peer : Peer , error : WSError ) => MaybePromise < void > ;
28+ /** Upgrading */
29+ /**
30+ *
31+ * @param request
32+ * @throws {Response }
33+ */
34+ upgrade : (
35+ request : UpgradeRequest & {
36+ context : Peer [ 'context' ] ;
37+ } ,
38+ ) => MaybePromise < Response | ResponseInit | void > ;
39+ /** A message is received */
40+ message : ( peer : Peer , message : Message ) => MaybePromise < void > ;
41+ /** A socket is opened */
42+ open : ( peer : Peer ) => MaybePromise < void > ;
43+ /** A socket is closed */
44+ close : (
45+ peer : Peer ,
46+ details : {
47+ code ?: number ;
48+ reason ?: string ;
49+ } ,
50+ ) => MaybePromise < void > ;
51+ /** An error occurs */
52+ error : ( peer : Peer , error : WSError ) => MaybePromise < void > ;
53+ }
54+
55+ interface Client {
56+ /**
57+ * Clients may send messages through the socket. This function can be called to handle those incoming messages.
58+ */
59+ handleIncomingMessage : ( data : string ) => Promise < void > ;
60+ /**
61+ * When a clients socket is closed, the graphql server wants to be notified. This function can be called to do that.
62+ */
63+ signalClosure : ( code : number , reason : string ) => Promise < void > ;
4664}
4765
4866export function makeHooks <
4967 P extends ConnectionInitMessage [ 'payload' ] = ConnectionInitMessage [ 'payload' ] ,
5068 E extends Record < PropertyKey , unknown > = Record < PropertyKey , never > ,
51- > ( options : ServerOptions < P , Extra & Partial < E > > & { isProd ?: boolean } ) {
69+ > (
70+ options : ServerOptions < P , Extra & Partial < E > > & {
71+ /**
72+ * If the server is running in production. Defaults to read from `process.env.NODE_ENV`.
73+ * In production the server will not send error messages whichmight contain sensitive info to the client.
74+ */
75+ isProd ?: boolean ;
76+ } ,
77+ ) {
5278 const isProd =
5379 typeof options . isProd === 'boolean'
5480 ? options . isProd
5581 : process . env . NODE_ENV === 'production' ;
5682 const server = makeServer ( options ) ;
5783
58- interface Client {
59- handleMessage : ( data : string ) => Promise < void > ;
60- closed : ( code : number , reason : string ) => Promise < void > ;
61- }
62-
6384 const clients = new WeakMap < Peer , Client > ( ) ;
6485
6586 return defineHooks < Partial < Hooks > > ( {
6687 open ( peer ) {
88+ console . log ( 'opening' ) ;
6789 const client : Client = {
68- handleMessage : ( ) => {
90+ handleIncomingMessage : ( ) => {
6991 throw new Error ( 'Message received before handler was registered' ) ;
7092 } ,
71- closed : ( ) => {
93+ signalClosure : ( ) => {
7294 throw new Error ( 'Closed before handler was registered' ) ;
7395 } ,
7496 } ;
75-
76- client . closed = server . opened (
97+ client . signalClosure = server . opened (
7798 {
7899 protocol :
79100 handleProtocols (
80101 peer . request . headers . get ( 'sec-websocket-protocol' ) ?? '' ,
81102 ) || '' ,
103+
82104 send : async ( message ) => {
105+ console . log ( clients ) ;
106+
83107 // ws might have been destroyed in the meantime, send only if exists
84108 if ( clients . has ( peer ) ) {
109+ console . log ( 'has client' ) ;
110+
85111 peer . send ( message ) ;
112+ } else {
113+ console . log ( 'doesnt have client' ) ;
86114 }
87115 } ,
88116 close : ( code , reason ) => {
89117 if ( clients . has ( peer ) ) {
90118 peer . close ( code , reason ) ;
91119 }
92120 } ,
93- onMessage : ( cb ) => {
94- client . handleMessage = cb ;
95- return cb ;
121+ onMessage : ( handleIncoming ) => {
122+ client . handleIncomingMessage = handleIncoming ;
96123 } ,
97124 } ,
98125 { socket : peer . websocket } as Extra & Partial < E > ,
@@ -101,11 +128,13 @@ export function makeHooks<
101128 clients . set ( peer , client ) ;
102129 } ,
103130 async message ( peer , message ) {
131+ console . log ( 'got message' ) ;
132+
104133 const client = clients . get ( peer ) ;
105134 if ( ! client ) throw new Error ( 'Message received for a missing client' ) ;
106135
107136 try {
108- await client . handleMessage ( message . text ( ) ) ;
137+ await client . handleIncomingMessage ( message . text ( ) ) ;
109138 } catch ( err ) {
110139 console . error (
111140 'Internal error occurred during message handling. ' +
@@ -127,7 +156,7 @@ export function makeHooks<
127156 const client = clients . get ( peer ) ;
128157 if ( ! client ) throw new Error ( 'Closing a missing client' ) ;
129158
130- client . closed (
159+ client . signalClosure (
131160 details ?. code ?? 1000 ,
132161 details . reason || 'Connection closed' ,
133162 ) ;
0 commit comments