/
websocketHelpers.js
114 lines (102 loc) · 3.28 KB
/
websocketHelpers.js
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
const { DateTime } = require('luxon');
const { createUniqueId } = require('./utils');
// TODO this should be probably moved to utils, and combined with other header
// functions and utilities
function createMultiValueHeaders(headers) {
return Object.entries(headers).reduce((acc, [key, value]) => {
acc[key] = [value];
return acc;
}, {});
}
// CLF -> Common Log Format
// https://httpd.apache.org/docs/1.3/logs.html#common
// [day/month/year:hour:minute:second zone]
// day = 2*digit
// month = 3*letter
// year = 4*digit
// hour = 2*digit
// minute = 2*digit
// second = 2*digit
// zone = (`+' | `-') 4*digit
function formatToClfTime(date) {
return DateTime.fromJSDate(date).toFormat('dd/MMM/yyyy:HH:mm:ss ZZZ');
}
const createRequestContext = (action, eventType, connection) => {
const now = new Date();
const requestContext = {
apiId: 'private',
connectedAt: connection.connectionTime,
connectionId:connection.connectionId,
domainName: 'localhost',
eventType,
extendedRequestId: `${createUniqueId()}`,
identity: {
accountId: null,
accessKey: null,
caller: null,
cognitoAuthenticationProvider: null,
cognitoAuthenticationType: null,
cognitoIdentityId: null,
cognitoIdentityPoolId: null,
principalOrgId: null,
sourceIp: '127.0.0.1',
user: null,
userAgent: null,
userArn: null,
},
messageDirection: 'IN',
messageId: `${createUniqueId()}`,
requestId: `${createUniqueId()}`,
requestTime: formatToClfTime(now),
requestTimeEpoch: now.getTime(),
routeKey: action,
stage: 'local',
};
return requestContext;
};
exports.createEvent = (action, eventType, connection, payload, options) => {
const event = {
apiGatewayUrl: `http${options.httpsProtocol ? 's' : ''}://${options.host}:${options.port + 1}`,
body: JSON.stringify(payload),
isBase64Encoded: false,
requestContext: createRequestContext(action, eventType, connection),
};
return event;
};
exports.createConnectEvent = (action, eventType, connection, options) => {
const headers = {
Host: 'localhost',
'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
'Sec-WebSocket-Key': `${createUniqueId()}`,
'Sec-WebSocket-Version': '13',
'X-Amzn-Trace-Id': `Root=${createUniqueId()}`,
'X-Forwarded-For': '127.0.0.1',
'X-Forwarded-Port': `${options.port + 1}`,
'X-Forwarded-Proto': `http${options.httpsProtocol ? 's' : ''}`,
};
const multiValueHeaders = createMultiValueHeaders(headers);
const event = {
apiGatewayUrl: `http${options.httpsProtocol ? 's' : ''}://${options.host}:${options.port + 1}`,
headers,
isBase64Encoded: false,
multiValueHeaders,
requestContext: createRequestContext(action, eventType, connection),
};
return event;
};
exports.createDisconnectEvent = (action, eventType, connection, options) => {
const headers = {
Host: 'localhost',
'x-api-key': '',
'x-restapi': '',
};
const multiValueHeaders = createMultiValueHeaders(headers);
const event = {
apiGatewayUrl: `http${options.httpsProtocol ? 's' : ''}://${options.host}:${options.port + 1}`,
headers,
isBase64Encoded: false,
multiValueHeaders,
requestContext: createRequestContext(action, eventType, connection),
};
return event;
};