-
-
Notifications
You must be signed in to change notification settings - Fork 569
/
subscriptions.ts
487 lines (450 loc) 路 17.5 KB
/
subscriptions.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
import { Server, IncomingMessage, ServerResponse, OutgoingHttpHeaders } from 'http';
import { HttpRequestHandler, mixed, Middleware } from '../../interfaces';
import {
subscribe as graphqlSubscribe,
ExecutionResult,
ExecutionArgs,
specifiedRules,
validate,
GraphQLError,
parse,
DocumentNode,
execute,
} from 'graphql';
import * as WebSocket from 'ws';
import { SubscriptionServer, ConnectionContext, ExecutionParams } from 'subscriptions-transport-ws';
import { GRAPHQL_TRANSPORT_WS_PROTOCOL } from 'graphql-ws';
import { useServer } from 'graphql-ws/lib/use/ws';
import parseUrl = require('parseurl');
import { pluginHookFromOptions } from '../pluginHook';
import { isEmpty } from './createPostGraphileHttpRequestHandler';
import liveSubscribe from './liveSubscribe';
interface Deferred<T> extends Promise<T> {
resolve: (input?: T | PromiseLike<T> | undefined) => void;
reject: (error: Error) => void;
}
function lowerCaseKeys(obj: Record<string, any>): Record<string, any> {
return Object.keys(obj).reduce((memo, key) => {
memo[key.toLowerCase()] = obj[key];
return memo;
}, {});
}
function deferred<T = void>(): Deferred<T> {
let resolve: (input?: T | PromiseLike<T> | undefined) => void;
let reject: (error: Error) => void;
const promise = new Promise<T>((_resolve, _reject): void => {
resolve = _resolve;
reject = _reject;
});
// tslint:disable-next-line prefer-object-spread
return Object.assign(promise, {
// @ts-ignore This isn't used before being defined.
resolve,
// @ts-ignore This isn't used before being defined.
reject,
});
}
export async function enhanceHttpServerWithWebSockets<
Request extends IncomingMessage = IncomingMessage,
Response extends ServerResponse = ServerResponse
>(
websocketServer: Server,
postgraphileMiddleware: HttpRequestHandler,
subscriptionServerOptions?: {
keepAlive?: number;
graphqlRoute?: string;
},
): Promise<void> {
if (websocketServer['__postgraphileSubscriptionsEnabled']) {
return;
}
websocketServer['__postgraphileSubscriptionsEnabled'] = true;
const {
options,
getGraphQLSchema,
withPostGraphileContextFromReqRes,
handleErrors,
} = postgraphileMiddleware;
const pluginHook = pluginHookFromOptions(options);
const graphqlRoute =
(subscriptionServerOptions && subscriptionServerOptions.graphqlRoute) ||
(options.externalUrlBase || '') + (options.graphqlRoute || '/graphql');
const { subscriptions, live, websockets = subscriptions || live ? ['v0', 'v1'] : [] } = options;
// enhance with WebSockets shouldnt be called if there are no websocket versions
if (!websockets?.length) {
throw new Error(`Invalid value for \`websockets\` option: '${JSON.stringify(websockets)}'`);
}
const schema = await getGraphQLSchema();
const keepalivePromisesByContextKey: { [contextKey: string]: Deferred<void> | null } = {};
const contextKey = (ws: WebSocket, opId: string): string => ws['postgraphileId'] + '|' + opId;
const releaseContextForSocketAndOpId = (ws: WebSocket, opId: string): void => {
const promise = keepalivePromisesByContextKey[contextKey(ws, opId)];
if (promise) {
promise.resolve();
keepalivePromisesByContextKey[contextKey(ws, opId)] = null;
}
};
const addContextForSocketAndOpId = (
context: mixed,
ws: WebSocket,
opId: string,
): Deferred<void> => {
releaseContextForSocketAndOpId(ws, opId);
const promise = deferred();
promise['context'] = context;
keepalivePromisesByContextKey[contextKey(ws, opId)] = promise;
return promise;
};
const applyMiddleware = async (
middlewares: Array<Middleware<Request, Response>> = [],
req: Request,
res: Response,
): Promise<void> => {
for (const middleware of middlewares) {
// TODO: add Koa support
await new Promise<void>((resolve, reject): void => {
middleware(req, res, err => (err ? reject(err) : resolve()));
});
}
};
const reqResFromSocket = async (
socket: WebSocket,
): Promise<{
req: Request;
res: Response;
}> => {
const req = socket['__postgraphileReq'];
if (!req) {
throw new Error('req could not be extracted');
}
let dummyRes: Response | undefined = socket['__postgraphileRes'];
if (req.res) {
throw new Error(
"Please get in touch with Benjie; we weren't expecting req.res to be present but we want to reserve it for future usage.",
);
}
if (!dummyRes) {
dummyRes = new ServerResponse(req) as Response;
dummyRes.writeHead = (
statusCode: number,
_statusMessage?: OutgoingHttpHeaders | string | undefined,
headers?: OutgoingHttpHeaders | undefined,
): void => {
if (statusCode && statusCode > 200) {
// tslint:disable-next-line no-console
console.error(
`Something used 'writeHead' to write a '${statusCode}' error for websockets - check the middleware you're passing!`,
);
socket.close();
} else if (headers) {
// tslint:disable-next-line no-console
console.error(
"Passing headers to 'writeHead' is not supported with websockets currently - check the middleware you're passing",
);
socket.close();
}
};
await applyMiddleware(options.websocketMiddlewares, req, dummyRes);
// reqResFromSocket is only called once per socket, so there's no race condition here
// eslint-disable-next-line require-atomic-updates
socket['__postgraphileRes'] = dummyRes;
}
return { req, res: dummyRes };
};
const getContext = (socket: WebSocket, opId: string): Promise<mixed> => {
return new Promise((resolve, reject): void => {
reqResFromSocket(socket)
.then(({ req, res }) =>
withPostGraphileContextFromReqRes(req, res, { singleStatement: true }, context => {
const promise = addContextForSocketAndOpId(context, socket, opId);
resolve(promise['context']);
return promise;
}),
)
.then(null, reject);
});
};
const staticValidationRules = pluginHook('postgraphile:validationRules:static', specifiedRules, {
options,
});
let socketId = 0;
let v0Wss: WebSocket.Server | null = null;
if (websockets.includes('v0')) {
v0Wss = new WebSocket.Server({ noServer: true });
SubscriptionServer.create(
{
schema,
validationRules: staticValidationRules,
execute:
options.websocketOperations === 'all'
? execute
: () => {
throw new Error('Only subscriptions are allowed over websocket transport');
},
subscribe: options.live ? liveSubscribe : graphqlSubscribe,
onConnect(
connectionParams: Record<string, any>,
_socket: WebSocket,
connectionContext: ConnectionContext,
) {
const { socket, request } = connectionContext;
socket['postgraphileId'] = ++socketId;
if (!request) {
throw new Error('No request!');
}
const normalizedConnectionParams = lowerCaseKeys(connectionParams);
request['connectionParams'] = connectionParams;
request['normalizedConnectionParams'] = normalizedConnectionParams;
socket['__postgraphileReq'] = request;
if (!request.headers.authorization && normalizedConnectionParams['authorization']) {
/*
* Enable JWT support through connectionParams.
*
* For other headers you'll need to do this yourself for security
* reasons (e.g. we don't want to allow overriding of Origin /
* Referer / etc)
*/
request.headers.authorization = String(normalizedConnectionParams['authorization']);
}
socket['postgraphileHeaders'] = {
...normalizedConnectionParams,
// The original headers must win (for security)
...request.headers,
};
},
// tslint:disable-next-line no-any
async onOperation(message: any, params: ExecutionParams, socket: WebSocket) {
const opId = message.id;
const context = await getContext(socket, opId);
// Override schema (for --watch)
params.schema = await getGraphQLSchema();
Object.assign(params.context, context);
const { req, res } = await reqResFromSocket(socket);
const meta = {};
const formatResponse = <TExecutionResult extends ExecutionResult = ExecutionResult>(
response: TExecutionResult,
): TExecutionResult => {
if (response.errors) {
response.errors = handleErrors(response.errors, req, res);
}
if (!isEmpty(meta)) {
response['meta'] = meta;
}
return response;
};
// onOperation is only called once per params object, so there's no race condition here
// eslint-disable-next-line require-atomic-updates
params.formatResponse = formatResponse;
const hookedParams = pluginHook
? pluginHook('postgraphile:ws:onOperation', params, {
message,
params,
socket,
options,
})
: params;
const finalParams: typeof hookedParams & { query: DocumentNode } = {
...hookedParams,
query:
typeof hookedParams.query !== 'string'
? hookedParams.query
: parse(hookedParams.query),
};
// You are strongly encouraged to use
// `postgraphile:validationRules:static` if possible - you should
// only use this one if you need access to variables.
const moreValidationRules = pluginHook('postgraphile:validationRules', [], {
options,
req,
res,
variables: params.variables,
operationName: params.operationName,
meta,
});
if (moreValidationRules.length) {
const validationErrors: ReadonlyArray<GraphQLError> = validate(
params.schema,
finalParams.query,
moreValidationRules,
);
if (validationErrors.length) {
const error = new Error(
'Query validation failed: \n' + validationErrors.map(e => e.message).join('\n'),
);
error['errors'] = validationErrors;
return Promise.reject(error);
}
}
return finalParams;
},
onOperationComplete(socket: WebSocket, opId: string) {
releaseContextForSocketAndOpId(socket, opId);
},
/*
* Heroku times out after 55s:
* https://devcenter.heroku.com/articles/error-codes#h15-idle-connection
*
* The subscriptions-transport-ws client times out by default 30s after last keepalive:
* https://github.com/apollographql/subscriptions-transport-ws/blob/52758bfba6190169a28078ecbafd2e457a2ff7a8/src/defaults.ts#L1
*
* GraphQL Playground times out after 20s:
* https://github.com/prisma/graphql-playground/blob/fa91e1b6d0488e6b5563d8b472682fe728ee0431/packages/graphql-playground-react/src/state/sessions/fetchingSagas.ts#L81
*
* Pick a number under these ceilings.
*/
keepAlive: 15000,
...subscriptionServerOptions,
},
v0Wss,
);
}
let v1Wss: WebSocket.Server | null = null;
if (websockets.includes('v1')) {
v1Wss = new WebSocket.Server({ noServer: true });
useServer(
{
schema,
execute:
options.websocketOperations === 'all'
? execute
: () => {
throw new Error('Only subscriptions are allowed over WebSocket transport');
},
subscribe: options.live ? liveSubscribe : graphqlSubscribe,
onConnect(ctx) {
const { socket, request } = ctx.extra;
socket['postgraphileId'] = ++socketId;
socket['__postgraphileReq'] = request;
const normalizedConnectionParams = lowerCaseKeys(ctx.connectionParams || {});
request['connectionParams'] = ctx.connectionParams || {};
request['normalizedConnectionParams'] = normalizedConnectionParams;
if (!request.headers.authorization && normalizedConnectionParams['authorization']) {
/*
* Enable JWT support through connectionParams.
*
* For other headers you'll need to do this yourself for security
* reasons (e.g. we don't want to allow overriding of Origin /
* Referer / etc)
*/
request.headers.authorization = String(normalizedConnectionParams['authorization']);
}
socket['postgraphileHeaders'] = {
...normalizedConnectionParams,
// The original headers must win (for security)
...request.headers,
};
},
async onSubscribe(ctx, msg) {
const context = await getContext(ctx.extra.socket, msg.id);
// Override schema (for --watch)
const schema = await getGraphQLSchema();
const { payload } = msg;
const args = {
schema,
contextValue: context,
operationName: payload.operationName,
document: payload.query ? parse(payload.query) : null, // parse if there is something to parse
variableValues: payload.variables,
};
// for supplying custom execution arguments. if not already
// complete, the pluginHook should fill in the gaps
const hookedArgs = (pluginHook
? pluginHook('postgraphile:ws:onSubscribe', args, {
context: ctx,
message: msg,
options,
})
: args) as ExecutionArgs;
// when supplying custom execution args from the
// onSubscribe, you're trusted to do the validation
const validationErrors = validate(
hookedArgs.schema,
hookedArgs.document,
staticValidationRules,
);
if (validationErrors.length) {
return validationErrors;
}
// You are strongly encouraged to use
// `postgraphile:validationRules:static` if possible - you should
// only use this one if you need access to variables.
const { req, res } = await reqResFromSocket(ctx.extra.socket);
const moreValidationRules = pluginHook('postgraphile:validationRules', [], {
options,
req,
res,
variables: hookedArgs.variableValues,
operationName: hookedArgs.operationName,
// no meta because validation errors returned from here will be
// served through the error message. it contains just the GraphQLErrors
// (there is no result to add the meta to)
});
if (moreValidationRules.length) {
const moreValidationErrors = validate(
hookedArgs.schema,
hookedArgs.document,
moreValidationRules,
);
if (moreValidationErrors.length) {
return moreValidationErrors;
}
}
return hookedArgs;
},
async onError(ctx, msg, errors) {
// errors returned from onSubscribe
releaseContextForSocketAndOpId(ctx.extra.socket, msg.id);
const { req, res } = await reqResFromSocket(ctx.extra.socket);
return handleErrors(errors, req, res);
},
async onNext(ctx, _msg, _args, result) {
if (result.errors) {
// operation execution errors
const { req, res } = await reqResFromSocket(ctx.extra.socket);
result.errors = handleErrors(result.errors, req, res);
return result;
}
},
onComplete(ctx, msg) {
releaseContextForSocketAndOpId(ctx.extra.socket, msg.id);
},
},
v1Wss,
/*
* Heroku times out after 55s:
* https://devcenter.heroku.com/articles/error-codes#h15-idle-connection
*
* GraphQL Playground times out after 20s:
* https://github.com/prisma/graphql-playground/blob/fa91e1b6d0488e6b5563d8b472682fe728ee0431/packages/graphql-playground-react/src/state/sessions/fetchingSagas.ts#L81
*
* Pick a number under these ceilings.
*/
subscriptionServerOptions?.keepAlive, // default is 12 seconds
);
}
// listen for upgrades and delegate requests according to the WS subprotocol
websocketServer.on('upgrade', (req: IncomingMessage, socket, head) => {
const { pathname = '' } = parseUrl(req) || {};
const isGraphqlRoute = pathname === graphqlRoute;
if (isGraphqlRoute) {
const protocol = req.headers['sec-websocket-protocol'];
const protocols = Array.isArray(protocol)
? protocol
: protocol?.split(',').map(p => p.trim());
const wss =
v0Wss &&
protocols?.includes('graphql-ws') &&
!protocols.includes(GRAPHQL_TRANSPORT_WS_PROTOCOL)
? v0Wss
: // v1 will welcome its own subprotocol `graphql-transport-ws`
// and gracefully reject invalid ones. if the client supports
// both v0 and v1, v1 will prevail
v1Wss;
if (wss) {
wss.handleUpgrade(req, socket, head, ws => {
wss.emit('connection', ws, req);
});
}
}
});
}