-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
98 lines (81 loc) · 2.54 KB
/
index.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
import * as AWS from "aws-sdk";
const ddb = new AWS.DynamoDB.DocumentClient({ apiVersion: "2012-08-10" });
export const handler = async (event: any, context: any): Promise<any> => {
console.log(event);
const {
requestContext: { connectionId, routeKey },
body,
} = event;
if (routeKey === "$connect") return handleConnect(connectionId);
if (routeKey === "$disconnect") return handleDisconnect(connectionId);
const parsedBody = JSON.parse(body);
console.log(parsedBody);
return handleDefault(connectionId, parsedBody);
};
async function handleConnect(connectionId: string) {
const putParams = {
TableName: process.env.TABLE_NAME!,
Item: { connectionId },
};
try {
await ddb.put(putParams).promise();
} catch (err) {
console.log(err);
return { statusCode: 500, body: "Failed to connect." };
}
return { statusCode: 200, body: `Connected.` };
}
async function handleDisconnect(connectionId: string) {
const deleteParams = {
TableName: process.env.TABLE_NAME!,
Key: { connectionId },
};
try {
await ddb.delete(deleteParams).promise();
} catch (err) {
console.log(err);
return { statusCode: 500, body: "Failed to disconnect." };
}
return {
statusCode: 200,
body: `Disconnected.`,
};
}
async function handleDefault(
connectionIdSender: string,
parsedBody: { message: string; senderName: string; senderGravatar: string }
) {
let connectionData;
try {
connectionData = await ddb.scan({ TableName: process.env.TABLE_NAME! }).promise();
} catch (e) {
console.log(e);
return { statusCode: 500, body: "Operation failed." };
}
const apiGW = new AWS.ApiGatewayManagementApi({
endpoint: process.env.APIGW_ENDPOINT,
});
if (connectionData && connectionData.Items) {
const data = JSON.stringify(parsedBody);
const postCalls = connectionData.Items.map(async ({ connectionId }) => {
if (connectionIdSender !== connectionId) {
try {
await apiGW.postToConnection({ ConnectionId: connectionId, Data: data }).promise();
} catch (e) {
if ((e as any).statusCode === 410) {
console.log(`Found stale connection, deleting ${connectionId}`);
await ddb.delete({ TableName: process.env.TABLE_NAME!, Key: { connectionIdDB: connectionId } }).promise();
} else {
throw e;
}
}
}
});
try {
await Promise.all(postCalls);
} catch (e) {
return { statusCode: 500, body: (e as any).stack };
}
}
return { statusCode: 200, body: "Data sent." };
}