-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlambdaConnection.ts
More file actions
161 lines (144 loc) · 4.76 KB
/
Copy pathlambdaConnection.ts
File metadata and controls
161 lines (144 loc) · 4.76 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
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
import { AwsCredentials } from './awsCredentials.js';
import { Configuration } from './configuration.js';
import { IoTMessage, IoTService, IoTServiceConnection } from './ioTService.js';
import { Logger } from './logger.js';
import { NodeHandler } from './nodeHandler.js';
let ioTServiceConnection: IoTServiceConnection;
let topic: string;
const lambdasProcessingObservableMode = new Set<string>();
/**
* Connect to the IoT Service
*/
async function connect() {
topic = `${Configuration.config.debuggerId}/events`;
let region = Configuration.config.region;
if (!region && Configuration.config.localStack) {
region = process.env.AWS_REGION || 'us-east-1';
Logger.verbose(
`Empty region detected with LocalStack, defaulting to ${region}`,
);
}
ioTServiceConnection = await IoTService.connect({
onMessage: onMessageFromLambda,
topic,
region: Configuration.config.region,
credentialsProvider: AwsCredentials.getCredentialsProvider({
profile: Configuration.config.profile,
region: Configuration.config.region,
role: Configuration.config.role,
}),
});
}
/**
* Handle incoming messages from the IoT Service
* @param message IoT message
*/
async function onMessageFromLambda(message: IoTMessage) {
if (!Configuration.config.observable) {
//immediately respond to the ping message to confirm the local debugging is alive
await ioTServiceConnection.publish(
<IoTMessage>{
type: 'PING',
data: {
workerId: message.data.workerId,
requestId: message.data.requestId,
functionId: message.data.functionId,
},
},
`${topic}/${message.data.workerId}`,
);
}
if (message.type !== 'INVOKE') {
throw new Error(`Unexpected message type: ${message.type}`);
}
try {
if (Configuration.config.observable) {
// if we are in observable mode, we don't want to process the same
// worker at the same time
if (lambdasProcessingObservableMode.has(message.data.functionId)) {
return;
}
lambdasProcessingObservableMode.add(message.data.functionId);
// waitX5 seconds then remove the worker from the processing list
// so we can get new event every X seconds
if (Configuration.config.interval > 0) {
setTimeout(() => {
lambdasProcessingObservableMode.delete(message.data.functionId);
}, Configuration.config.interval);
}
}
if (Configuration.config.verbose) {
Logger.log(
`[Function ${message.data.functionId}] Response: `,
JSON.stringify(message.data, null, 2),
);
} else {
// first 50 characters of the response
const requestPretty = message.data
? JSON.stringify(message.data).substring(0, 100)
: '';
Logger.log(
`[Function ${message.data.functionId}] Request: ${requestPretty}${requestPretty.length < 50 ? '' : '...'}`,
);
}
const response = await NodeHandler.invokeLambda(message.data);
if (Configuration.config.verbose) {
Logger.log(
`[Function ${message.data.functionId}] Response: `,
JSON.stringify(response, null, 2),
);
} else {
// first 50 characters of the response
const responsePretty = response
? JSON.stringify(response).substring(0, 100)
: '';
Logger.log(
`[Function ${message.data.functionId}] Response: ${responsePretty}${responsePretty.length < 50 ? '' : '...'}`,
);
}
if (Configuration.config.observable) {
// if we are in observable mode, mark the worker as processed
lambdasProcessingObservableMode.delete(message.data.functionId);
}
const payload: IoTMessage = {
type: 'SUCCESS',
data: {
functionId: message.data.functionId,
requestId: message.data.requestId,
workerId: message.data.workerId,
body: response,
},
};
if (!Configuration.config.observable) {
await ioTServiceConnection.publish(
payload,
`${topic}/${message.data.workerId}`,
);
}
} catch (e: any) {
Logger.error(`[Function ${message.data.functionId}] Error: `, e);
const payload: IoTMessage = {
type: 'ERROR',
data: {
functionId: message.data.functionId,
requestId: message.data.requestId,
workerId: message.data.workerId,
errorType: e.errorType,
errorMessage: e.errorMessage,
trace: e.trace,
},
};
if (!Configuration.config.observable) {
await ioTServiceConnection.publish(
payload,
`${topic}/${message.data.workerId}`,
);
} else {
// if we are in Observability mode, mark the worker as processed
lambdasProcessingObservableMode.delete(message.data.functionId);
}
}
}
export const LambdaConnection = {
connect,
};