This repository was archived by the owner on May 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathconnection_init.ts
More file actions
48 lines (45 loc) · 1.77 KB
/
connection_init.ts
File metadata and controls
48 lines (45 loc) · 1.77 KB
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
import { ConnectionInitMessage, MessageType } from 'graphql-ws';
import { MessageHandler } from '../types';
import { postToConnection } from '../utils/postToConnection';
import { deleteConnection } from '../utils/deleteConnection';
import { defaultTTL } from '../utils/defaultTTL';
/** Handler function for 'connection_init' message. */
export const connection_init: MessageHandler<ConnectionInitMessage> =
async ({ server, event, message }) => {
try {
const payload = (await server.onConnectionInit?.({ event, message })) ?? message.payload ?? {}
if (server.pingpong) {
console.error('Missing implementation for pingpong');
// await new StepFunctions()
// .startExecution({
// stateMachineArn: server.pingpong.machine,
// name: event.requestContext.connectionId,
// input: JSON.stringify({
// connectionId: event.requestContext.connectionId,
// domainName: event.requestContext.domainName,
// stage: event.requestContext.stage,
// state: 'PING',
// choice: 'WAIT',
// seconds: server.pingpong.delay,
// } as StateFunctionInput),
// })
// .promise()
}
// Write to persistence
await server.models.connection.put({
id: event.requestContext.connectionId,
createdAt: Date.now(),
requestContext: event.requestContext,
payload,
hasPonged: false,
ttl: defaultTTL(),
})
return postToConnection(server)({
...event.requestContext,
message: { type: MessageType.ConnectionAck },
})
} catch (err) {
await server.onError?.(err, { event, message })
await deleteConnection(server)(event.requestContext)
}
}