-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathserver.ts
699 lines (656 loc) · 22.2 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/**
*
* server
*
*/
import * as http from 'http';
import * as WebSocket from 'ws';
import {
OperationTypeNode,
GraphQLSchema,
ExecutionArgs,
parse,
validate,
getOperationAST,
GraphQLError,
SubscriptionArgs,
ExecutionResult,
} from 'graphql';
import { Disposable } from './types';
import { GRAPHQL_TRANSPORT_WS_PROTOCOL } from './protocol';
import {
Message,
MessageType,
stringifyMessage,
parseMessage,
SubscribeMessage,
NextMessage,
ErrorMessage,
CompleteMessage,
} from './message';
import {
isObject,
isAsyncIterable,
hasOwnObjectProperty,
hasOwnStringProperty,
areGraphQLErrors,
} from './utils';
import { ID } from './types';
export type OperationResult =
| Promise<AsyncIterableIterator<ExecutionResult> | ExecutionResult>
| AsyncIterableIterator<ExecutionResult>
| ExecutionResult;
/**
* A concrete GraphQL execution context value type.
*
* Mainly used because TypeScript collapes unions
* with `any` or `unknown` to `any` or `unknown`. So,
* we use a custom type to allow definitions such as
* the `context` server option.
*/
export type GraphQLExecutionContextValue =
// eslint-disable-next-line @typescript-eslint/ban-types
| object // you can literally pass "any" JS object as the context value
| symbol
| number
| string
| boolean
| null;
export interface ServerOptions {
/**
* The GraphQL schema on which the operations
* will be executed and validated against.
*
* If the schema is left undefined, you're trusted to
* provide one in the returned `ExecutionArgs` from the
* `onSubscribe` callback.
*/
schema?: GraphQLSchema;
/**
* A value which is provided to every resolver and holds
* important contextual information like the currently
* logged in user, or access to a database.
*
* If you return from `onSubscribe`, and the returned value is
* missing the `contextValue` field, this context will be used
* instead.
*
* If you use the function signature, the final execution arguments
* will be passed in (also the returned value from `onSubscribe`).
* Since the context is injected on every subscribe, the `SubscribeMessage`
* with the regular `Context` will be passed in through the arguments too.
*/
context?:
| GraphQLExecutionContextValue
| ((
ctx: Context,
message: SubscribeMessage,
args: ExecutionArgs,
) => GraphQLExecutionContextValue);
/**
* The GraphQL root fields or resolvers to go
* alongside the schema. Learn more about them
* here: https://graphql.org/learn/execution/#root-fields-resolvers.
*
* If you return from the `onSubscribe` callback, the
* root field value will NOT be injected. You should add it
* in the returned `ExecutionArgs` from the callback.
*/
roots?: {
[operation in OperationTypeNode]?: Record<
string,
NonNullable<SubscriptionArgs['rootValue']>
>;
};
/**
* Is the `execute` function from GraphQL which is
* used to execute the query and mutation operations.
*
* Throwing an error from within this function will
* close the socket with the `Error` message
* in the close event reason.
*/
execute: (args: ExecutionArgs) => OperationResult;
/**
* Is the `subscribe` function from GraphQL which is
* used to execute the subscription operation.
*
* Throwing an error from within this function will
* close the socket with the `Error` message
* in the close event reason.
*/
subscribe: (args: ExecutionArgs) => OperationResult;
/**
* The amount of time for which the server will wait
* for `ConnectionInit` message.
*
* Set the value to `Infinity`, `''`, `0`, `null` or `undefined` to skip waiting.
*
* If the wait timeout has passed and the client
* has not sent the `ConnectionInit` message,
* the server will terminate the socket by
* dispatching a close event `4408: Connection initialisation timeout`
*
* @default 3 * 1000 (3 seconds)
*/
connectionInitWaitTimeout?: number;
/**
* The timout between dispatched keep-alive messages. Internally the lib
* uses the [WebSocket Ping and Pongs]((https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#Pings_and_Pongs_The_Heartbeat_of_WebSockets)) to check that the link between
* the clients and the server is operating and to prevent the link from being broken due to idling.
*
* Set to nullish value to disable.
*
* @default 12 * 1000 (12 seconds)
*/
keepAlive?: number;
/**
* Is the connection callback called when the
* client requests the connection initialisation
* through the message `ConnectionInit`.
*
* The message payload (`connectionParams` from the
* client) is present in the `Context.connectionParams`.
*
* - Returning `true` or nothing from the callback will
* allow the client to connect.
*
* - Returning `false` from the callback will
* terminate the socket by dispatching the
* close event `4403: Forbidden`.
*
* Throwing an error from within this function will
* close the socket with the `Error` message
* in the close event reason.
*/
onConnect?: (ctx: Context) => Promise<boolean | void> | boolean | void;
/**
* The subscribe callback executed right after
* acknowledging the request before any payload
* processing has been performed.
*
* If you return `ExecutionArgs` from the callback,
* it will be used instead of trying to build one
* internally. In this case, you are responsible
* for providing a ready set of arguments which will
* be directly plugged in the operation execution. Beware,
* the `context` server option is an exception. Only if you
* dont provide a context alongside the returned value
* here, the `context` server option will be used instead.
*
* To report GraphQL errors simply return an array
* of them from the callback, they will be reported
* to the client through the error message.
*
* Useful for preparing the execution arguments
* following a custom logic. A typical use case are
* persisted queries, you can identify the query from
* the subscribe message and create the GraphQL operation
* execution args which are then returned by the function.
*
* Throwing an error from within this function will
* close the socket with the `Error` message
* in the close event reason.
*/
onSubscribe?: (
ctx: Context,
message: SubscribeMessage,
) =>
| Promise<ExecutionArgs | readonly GraphQLError[] | void>
| ExecutionArgs
| readonly GraphQLError[]
| void;
/**
* Executed after the operation call resolves. For streaming
* operations, triggering this callback does not necessarely
* mean that there is already a result available - it means
* that the subscription process for the stream has resolved
* and that the client is now subscribed.
*
* The `OperationResult` argument is the result of operation
* execution. It can be an iterator or already a value.
*
* If you want the single result and the events from a streaming
* operation, use the `onNext` callback.
*
* Use this callback to listen for subscribe operation and
* execution result manipulation.
*
* Throwing an error from within this function will
* close the socket with the `Error` message
* in the close event reason.
*/
onOperation?: (
ctx: Context,
message: SubscribeMessage,
args: ExecutionArgs,
result: OperationResult,
) => Promise<OperationResult | void> | OperationResult | void;
/**
* Executed after an error occured right before it
* has been dispatched to the client.
*
* Use this callback to format the outgoing GraphQL
* errors before they reach the client.
*
* Returned result will be injected in the error message payload.
*
* Throwing an error from within this function will
* close the socket with the `Error` message
* in the close event reason.
*/
onError?: (
ctx: Context,
message: ErrorMessage,
errors: readonly GraphQLError[],
) => Promise<readonly GraphQLError[] | void> | readonly GraphQLError[] | void;
/**
* Executed after an operation has emitted a result right before
* that result has been sent to the client. Results from both
* single value and streaming operations will appear in this callback.
*
* Use this callback if you want to format the execution result
* before it reaches the client.
*
* Returned result will be injected in the next message payload.
*
* Throwing an error from within this function will
* close the socket with the `Error` message
* in the close event reason.
*/
onNext?: (
ctx: Context,
message: NextMessage,
args: ExecutionArgs,
result: ExecutionResult,
) => Promise<ExecutionResult | void> | ExecutionResult | void;
/**
* The complete callback is executed after the
* operation has completed right before sending
* the complete message to the client.
*
* Throwing an error from within this function will
* close the socket with the `Error` message
* in the close event reason.
*/
onComplete?: (ctx: Context, message: CompleteMessage) => Promise<void> | void;
}
export interface Context {
/**
* The actual WebSocket connection between the server and the client.
*/
readonly socket: WebSocket;
/**
* The initial HTTP request before the actual
* socket and connection is established.
*/
readonly request: http.IncomingMessage;
/**
* Indicates that the `ConnectionInit` message
* has been received by the server. If this is
* `true`, the client wont be kicked off after
* the wait timeout has passed.
*/
connectionInitReceived: boolean;
/**
* Indicates that the connection was acknowledged
* by having dispatched the `ConnectionAck` message
* to the related client.
*/
acknowledged: boolean;
/** The parameters passed during the connection initialisation. */
connectionParams?: Readonly<Record<string, unknown>>;
/**
* Holds the active subscriptions for this context.
* Subscriptions are for **streaming operations only**,
* those that resolve once wont be added here.
*/
subscriptions: Record<ID, AsyncIterator<unknown>>;
}
export interface Server extends Disposable {
webSocketServer: WebSocket.Server;
}
// for documentation gen only
type WebSocketServerOptions = WebSocket.ServerOptions;
type WebSocketServer = WebSocket.Server;
/**
* Creates a protocol complient WebSocket GraphQL
* subscription server. Read more about the protocol
* in the PROTOCOL.md documentation file.
*/
export function createServer(
options: ServerOptions,
websocketOptionsOrServer: WebSocketServerOptions | WebSocketServer,
): Server {
const isProd = process.env.NODE_ENV === 'production';
const {
schema,
context,
roots,
execute,
subscribe,
connectionInitWaitTimeout = 3 * 1000, // 3 seconds
keepAlive = 12 * 1000, // 12 seconds
onConnect,
onSubscribe,
onOperation,
onNext,
onError,
onComplete,
} = options;
const webSocketServer = isWebSocketServer(websocketOptionsOrServer)
? websocketOptionsOrServer
: new WebSocket.Server(websocketOptionsOrServer);
function handleConnection(socket: WebSocket, request: http.IncomingMessage) {
if (
Array.isArray(socket.protocol)
? socket.protocol.indexOf(GRAPHQL_TRANSPORT_WS_PROTOCOL) === -1
: socket.protocol !== GRAPHQL_TRANSPORT_WS_PROTOCOL
) {
return socket.close(1002, 'Protocol Error');
}
const ctxRef: { current: Context } = {
current: {
socket,
request,
connectionInitReceived: false,
acknowledged: false,
subscriptions: {},
},
};
// kick the client off (close socket) if the connection has
// not been initialised after the specified wait timeout
const connectionInitWait =
connectionInitWaitTimeout > 0 && isFinite(connectionInitWaitTimeout)
? setTimeout(() => {
if (!ctxRef.current.connectionInitReceived) {
ctxRef.current.socket.close(
4408,
'Connection initialisation timeout',
);
}
}, connectionInitWaitTimeout)
: null;
// keep alive through ping-pong messages
// read more about the websocket heartbeat here: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#Pings_and_Pongs_The_Heartbeat_of_WebSockets
let pongWait: NodeJS.Timeout | null = null;
const pingInterval =
keepAlive > 0 && isFinite(keepAlive)
? setInterval(() => {
// ping pong on open sockets only
if (socket.readyState === WebSocket.OPEN) {
// terminate the connection after pong wait has passed because the client is idle
pongWait = setTimeout(() => {
socket.terminate();
}, keepAlive);
// listen for client's pong and stop socket termination
socket.once('pong', () => {
if (pongWait) {
clearTimeout(pongWait);
pongWait = null;
}
});
socket.ping();
}
}, keepAlive)
: null;
function errorOrCloseHandler(
errorOrClose: WebSocket.ErrorEvent | WebSocket.CloseEvent,
) {
if (connectionInitWait) {
clearTimeout(connectionInitWait);
}
if (pongWait) {
clearTimeout(pongWait);
}
if (pingInterval) {
clearInterval(pingInterval);
}
if (isErrorEvent(errorOrClose)) {
ctxRef.current.socket.close(
1011,
isProd ? 'Internal Error' : errorOrClose.message,
);
}
Object.entries(ctxRef.current.subscriptions).forEach(
([, subscription]) => {
subscription.return?.();
},
);
}
socket.onerror = errorOrCloseHandler;
socket.onclose = errorOrCloseHandler;
socket.onmessage = makeOnMessage(ctxRef.current);
}
webSocketServer.on('connection', handleConnection);
webSocketServer.on('error', (err) => {
for (const client of webSocketServer.clients) {
// report server errors by erroring out all clients with the same error
client.emit('error', err);
}
});
// Sends through a message only if the socket is open.
async function sendMessage<T extends MessageType>(
ctx: Context,
message: Message<T>,
) {
if (ctx.socket.readyState === WebSocket.OPEN) {
return new Promise((resolve, reject) => {
ctx.socket.send(stringifyMessage<T>(message), (err) =>
err ? reject(err) : resolve(),
);
});
}
}
function makeOnMessage(ctx: Context) {
return async function onMessage(event: WebSocket.MessageEvent) {
try {
const message = parseMessage(event.data);
switch (message.type) {
case MessageType.ConnectionInit: {
if (ctx.connectionInitReceived) {
return ctx.socket.close(4429, 'Too many initialisation requests');
}
ctx.connectionInitReceived = true;
if (isObject(message.payload)) {
ctx.connectionParams = message.payload;
}
if (onConnect) {
const permitted = await onConnect(ctx);
if (permitted === false) {
return ctx.socket.close(4403, 'Forbidden');
}
}
await sendMessage<MessageType.ConnectionAck>(ctx, {
type: MessageType.ConnectionAck,
});
ctx.acknowledged = true;
break;
}
case MessageType.Subscribe: {
if (!ctx.acknowledged) {
return ctx.socket.close(4401, 'Unauthorized');
}
const emit = {
next: async (result: ExecutionResult, args: ExecutionArgs) => {
let nextMessage: NextMessage = {
id: message.id,
type: MessageType.Next,
payload: result,
};
if (onNext) {
const maybeResult = await onNext(
ctx,
nextMessage,
args,
result,
);
if (maybeResult) {
nextMessage = {
...nextMessage,
payload: maybeResult,
};
}
}
await sendMessage<MessageType.Next>(ctx, nextMessage);
},
error: async (errors: readonly GraphQLError[]) => {
let errorMessage: ErrorMessage = {
id: message.id,
type: MessageType.Error,
payload: errors,
};
if (onError) {
const maybeErrors = await onError(ctx, errorMessage, errors);
if (maybeErrors) {
errorMessage = {
...errorMessage,
payload: maybeErrors,
};
}
}
await sendMessage<MessageType.Error>(ctx, errorMessage);
},
complete: async () => {
const completeMessage: CompleteMessage = {
id: message.id,
type: MessageType.Complete,
};
await onComplete?.(ctx, completeMessage);
await sendMessage<MessageType.Complete>(ctx, completeMessage);
},
};
let execArgs: ExecutionArgs;
const maybeExecArgsOrErrors = await onSubscribe?.(ctx, message);
if (maybeExecArgsOrErrors) {
if (areGraphQLErrors(maybeExecArgsOrErrors)) {
return await emit.error(maybeExecArgsOrErrors);
}
// not errors, is exec args
execArgs = maybeExecArgsOrErrors;
} else {
if (!schema) {
// you either provide a schema dynamically through
// `onSubscribe` or you set one up during the server setup
return webSocketServer.emit(
'error',
new Error('The GraphQL schema is not provided'),
);
}
const { operationName, query, variables } = message.payload;
execArgs = {
schema,
operationName,
document: parse(query),
variableValues: variables,
};
const validationErrors = validate(
execArgs.schema,
execArgs.document,
);
if (validationErrors.length > 0) {
return await emit.error(validationErrors);
}
}
const operationAST = getOperationAST(
execArgs.document,
execArgs.operationName,
);
if (!operationAST) {
return await emit.error([
new GraphQLError('Unable to identify operation'),
]);
}
// if onsubscribe didnt return anything, inject roots
if (!maybeExecArgsOrErrors) {
execArgs.rootValue = roots?.[operationAST.operation];
}
// inject the context, if provided, before the operation.
// but, only if the `onSubscribe` didnt provide one already
if (context !== undefined && !execArgs.contextValue) {
execArgs.contextValue =
typeof context === 'function'
? context(ctx, message, execArgs)
: context;
}
// the execution arguments have been prepared
// perform the operation and act accordingly
let operationResult;
if (operationAST.operation === 'subscription') {
operationResult = await subscribe(execArgs);
} else {
// operation === 'query' || 'mutation'
operationResult = await execute(execArgs);
}
if (onOperation) {
const maybeResult = await onOperation(
ctx,
message,
execArgs,
operationResult,
);
if (maybeResult) {
operationResult = maybeResult;
}
}
if (isAsyncIterable(operationResult)) {
/** multiple emitted results */
// iterable subscriptions are distinct on ID
if (ctx.subscriptions[message.id]) {
return ctx.socket.close(
4409,
`Subscriber for ${message.id} already exists`,
);
}
ctx.subscriptions[message.id] = operationResult;
for await (const result of operationResult) {
await emit.next(result, execArgs);
}
await emit.complete();
delete ctx.subscriptions[message.id];
} else {
/** single emitted result */
await emit.next(operationResult, execArgs);
await emit.complete();
}
break;
}
case MessageType.Complete: {
await ctx.subscriptions[message.id]?.return?.();
break;
}
default:
throw new Error(
`Unexpected message of type ${message.type} received`,
);
}
} catch (err) {
// TODO-db-201031 we perceive this as a client bad request error, but is it always?
ctx.socket.close(4400, err.message);
}
};
}
return {
webSocketServer,
dispose: async () => {
for (const client of webSocketServer.clients) {
client.close(1001, 'Going away');
}
webSocketServer.removeAllListeners();
await new Promise((resolve, reject) =>
webSocketServer.close((err) => (err ? reject(err) : resolve())),
);
},
};
}
function isErrorEvent(obj: unknown): obj is WebSocket.ErrorEvent {
return (
isObject(obj) &&
hasOwnObjectProperty(obj, 'error') &&
hasOwnStringProperty(obj, 'message') &&
hasOwnStringProperty(obj, 'type')
);
}
function isWebSocketServer(obj: unknown): obj is WebSocketServer {
return isObject(obj) && typeof obj.on === 'function';
}