|
| 1 | +const UserError = require('./UserError'); |
| 2 | + |
| 3 | +const methodParams = { |
| 4 | + load: ['query'], |
| 5 | + sql: ['query'], |
| 6 | + meta: [], |
| 7 | + subscribe: ['query'], |
| 8 | + unsubscribe: [] |
| 9 | +}; |
| 10 | + |
| 11 | +class SubscriptionServer { |
| 12 | + constructor(apiGateway, sendMessage, subscriptionStore) { |
| 13 | + this.apiGateway = apiGateway; |
| 14 | + this.sendMessage = sendMessage; |
| 15 | + this.subscriptionStore = subscriptionStore; |
| 16 | + } |
| 17 | + |
| 18 | + resultFn(connectionId, messageId) { |
| 19 | + return (message, { status } = {}) => this.sendMessage(connectionId, { messageId, message, status: status || 200 }); |
| 20 | + } |
| 21 | + |
| 22 | + async processMessage(connectionId, message, isSubscription) { |
| 23 | + let context = {}; |
| 24 | + try { |
| 25 | + if (typeof message === 'string') { |
| 26 | + message = JSON.parse(message); |
| 27 | + } |
| 28 | + if (message.authorization) { |
| 29 | + const newContext = {}; |
| 30 | + await this.apiGateway.checkAuthFn(newContext, message.authorization); |
| 31 | + await this.subscriptionStore.setAuthContext(connectionId, newContext); |
| 32 | + this.sendMessage(connectionId, { handshake: true }); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + if (message.unsubscribe) { |
| 37 | + await this.subscriptionStore.unsubscribe(connectionId, message.unsubscribe); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + if (!message.messageId) { |
| 42 | + throw new UserError(`messageId is required`); |
| 43 | + } |
| 44 | + |
| 45 | + context = await this.subscriptionStore.getAuthContext(connectionId); |
| 46 | + |
| 47 | + if (!context) { |
| 48 | + await this.sendMessage( |
| 49 | + connectionId, |
| 50 | + { |
| 51 | + messageId: message.messageId, |
| 52 | + message: { error: 'Not authorized' }, |
| 53 | + status: 403 |
| 54 | + } |
| 55 | + ); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (!methodParams[message.method]) { |
| 60 | + throw new UserError(`Unsupported method: ${message.method}`); |
| 61 | + } |
| 62 | + |
| 63 | + const allowedParams = methodParams[message.method]; |
| 64 | + const params = allowedParams.map(k => ({ [k]: (message.params || {})[k] })) |
| 65 | + .reduce((a, b) => ({ ...a, ...b }), {}); |
| 66 | + await this.apiGateway[message.method]({ |
| 67 | + ...params, |
| 68 | + context, |
| 69 | + isSubscription, |
| 70 | + res: this.resultFn(connectionId, message.messageId), |
| 71 | + subscriptionState: async () => { |
| 72 | + const subscription = await this.subscriptionStore.getSubscription(connectionId, message.messageId); |
| 73 | + return subscription && subscription.state; |
| 74 | + }, |
| 75 | + subscribe: async (state) => this.subscriptionStore.subscribe(connectionId, message.messageId, { |
| 76 | + message, |
| 77 | + state |
| 78 | + }), |
| 79 | + unsubscribe: async () => this.subscriptionStore.unsubscribe(connectionId, message.messageId) |
| 80 | + }); |
| 81 | + await this.sendMessage(connectionId, { messageProcessedId: message.messageId }); |
| 82 | + } catch (e) { |
| 83 | + this.apiGateway.handleError({ |
| 84 | + e, |
| 85 | + query: message.query, |
| 86 | + res: this.resultFn(connectionId, message.messageId), |
| 87 | + context |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + async processSubscriptions() { |
| 93 | + const allSubscriptions = await this.subscriptionStore.getAllSubscriptions(); |
| 94 | + await Promise.all(allSubscriptions.map(async subscription => { |
| 95 | + await this.processMessage(subscription.connectionId, subscription.message, true); |
| 96 | + })); |
| 97 | + } |
| 98 | + |
| 99 | + async disconnect(connectionId) { |
| 100 | + await this.subscriptionStore.cleanupSubscriptions(connectionId); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +module.exports = SubscriptionServer; |
0 commit comments