-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
465 lines (417 loc) · 12.6 KB
/
index.ts
File metadata and controls
465 lines (417 loc) · 12.6 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import { DescribeEndpointCommand, IoTClient } from "@aws-sdk/client-iot";
import {
GetFunctionConfigurationCommand,
GetFunctionConfigurationCommandOutput,
LambdaClient,
UpdateFunctionCodeCommand,
UpdateFunctionConfigurationCommand,
UpdateFunctionConfigurationCommandInput,
} from "@aws-sdk/client-lambda";
import { toUtf8 } from "@aws-sdk/util-utf8-browser";
import * as archiver from "archiver";
import { iot, mqtt5 } from "aws-iot-device-sdk-v2";
import { exec } from "child_process";
import * as crypto from "crypto";
import * as esbuild from "esbuild";
import { once } from "events";
import * as fs from "fs";
import * as path from "path";
import { Argv } from "yargs";
import * as os from "os";
interface Args {
name?: string;
path?: string;
skipDeploy?: boolean;
}
export const builder = (yargs: Argv<Args>) => {
return;
};
const invokeByPath = async (path: string, event: any, context: any) => {
const { handler: fn } = await import(
`${path}?cacheBust=${Number(new Date())}`
);
try {
const response = await fn(event, context);
return response;
} catch (e) {
throw e;
} finally {
for (const path in require.cache) {
if (path.endsWith(".js")) {
// only clear *.js, not *.node
delete require.cache[path];
}
}
}
};
const getIotWebsocketEndpoint = async (region: string) => {
const iot = new IoTClient({
region,
});
const params = {
endpointType: "iot:Data-ATS",
};
return iot.send(new DescribeEndpointCommand(params));
};
const creatClientConfig = ({
websocketEndpoint,
region,
}: {
websocketEndpoint: string;
region: string;
}) => {
const builder =
iot.AwsIotMqtt5ClientConfigBuilder.newWebsocketMqttBuilderWithSigv4Auth(
websocketEndpoint,
{
region,
}
);
builder.withConnectProperties({
keepAliveIntervalSeconds: 1200,
});
return builder.build();
};
const createMqttClient = ({
websocketEndpoint,
region,
}: {
websocketEndpoint: string;
region: string;
}): mqtt5.Mqtt5Client => {
const config: mqtt5.Mqtt5ClientConfig = creatClientConfig({
websocketEndpoint,
region,
});
return new mqtt5.Mqtt5Client(config);
};
const updateLambdaFunctionCode = async (
functionName: string,
zipFile: Buffer,
region: string
) => {
const lambdaClient = new LambdaClient({ region });
const params = {
FunctionName: functionName,
ZipFile: zipFile,
};
const command = new UpdateFunctionCodeCommand(params);
return lambdaClient.send(command);
};
const updateLambdaFunctionConfiguration = async ({
functionName,
region,
runtime,
handler,
}: {
functionName: string;
region: string;
runtime: string;
handler: string;
}) => {
const lambdaClient = new LambdaClient({ region });
const params: UpdateFunctionConfigurationCommandInput = {
// @ts-ignore
Runtime: runtime,
Handler: handler,
FunctionName: functionName,
};
const command = new UpdateFunctionConfigurationCommand(params);
return lambdaClient.send(command);
};
const getLambdaConfiguration = async (functionName: string, region: string) => {
const lambdaClient = new LambdaClient({ region });
const params = {
FunctionName: functionName,
};
const command = new GetFunctionConfigurationCommand(params);
return lambdaClient.send(command);
};
const copyDirectorySync = (source: string, target: string) => {
fs.readdirSync(source).forEach((file) => {
const sourceFile = path.join(source, file);
const targetFile = path.join(target, file);
if (fs.lstatSync(sourceFile).isDirectory()) {
if (!fs.existsSync(targetFile)) {
fs.mkdirSync(targetFile);
}
copyDirectorySync(sourceFile, targetFile);
} else {
fs.copyFileSync(sourceFile, targetFile);
}
});
};
const zipDirectory = (source: string, out: string) => {
const archive = archiver.default("zip", { zlib: { level: 9 } });
const stream = fs.createWriteStream(out);
return new Promise((resolve, reject) => {
archive
.directory(source, false)
.on("error", (err: any) => reject(err))
.pipe(stream);
stream.on("close", () => resolve(""));
archive.finalize();
});
};
const hashDirectory = (directory: string) => {
const hash = crypto.createHash("sha256");
const files = fs.readdirSync(directory);
for (const file of files) {
const filePath = path.join(directory, file);
const stat = fs.statSync(filePath);
if (stat.isFile()) {
const fileData = fs.readFileSync(filePath);
hash.update(fileData);
} else if (stat.isDirectory()) {
const nestedHash = hashDirectory(filePath);
hash.update(nestedHash);
}
}
return hash.digest("hex");
};
const runNpmInstall = async (directory: string): Promise<void> => {
const { consola } = await import("consola");
return new Promise((resolve, reject) => {
exec("npm install", { cwd: directory }, (error, stdout, stderr) => {
if (error) {
consola.fatal(`Error running npm install: ${error}`);
reject(error);
return;
}
consola.debug(`npm install output: ${stdout}`);
resolve();
});
});
};
const buildProxyHandler = async (
handlerName: string,
tmpDir: string
): Promise<string> => {
await esbuild.build({
entryPoints: [path.resolve(__dirname, `./local_proxy/iot-publish.ts`)],
outfile: path.resolve(tmpDir, `dist/${handlerName?.split(".")?.[0]}.js`),
bundle: true,
platform: "node",
target: "node20",
external: ["aws-crt"],
});
copyDirectorySync(
path.resolve(__dirname, `./local_proxy`),
path.resolve(tmpDir, "dist")
);
await runNpmInstall(path.resolve(tmpDir, "dist"));
const hash = hashDirectory(path.resolve(tmpDir, "dist"));
const zipName = `asset.${hash}.zip`;
await zipDirectory(
path.resolve(tmpDir, `dist`),
path.resolve(tmpDir, zipName)
);
return zipName;
};
const exitHandler = async ({
event,
context,
}: {
event: string;
context: {
functionName: string;
region: string;
runtime: string;
tmpDir: string;
};
}) => {
const { consola } = await import("consola");
const { functionName, region, tmpDir, runtime } = context;
consola.warn(`Received ${event} event. Cleaning up...`);
if (runtime !== "nodejs20.x") {
const filePath = path.resolve(tmpDir, "backup", `${functionName}.json`);
const lambdaConfiguration: GetFunctionConfigurationCommandOutput =
JSON.parse(
fs.readFileSync(filePath).toString()
) as GetFunctionConfigurationCommandOutput;
await updateLambdaFunctionConfiguration({
functionName,
region,
runtime: lambdaConfiguration.Runtime!,
handler: lambdaConfiguration.Handler!,
});
}
fs.rmSync(tmpDir, { recursive: true });
process.exit(0);
};
// Use the cli argument to invoke the local function
const run = async ({
region,
functionName,
localPath,
topic,
skipDeploy,
}: {
region: string;
functionName: string;
localPath: string;
topic: string;
skipDeploy?: boolean;
}) => {
// TODO: we should be able to import this normally, but commonjs seems to be broken in v3. We should fix this if they patch it.
const { consola } = await import("consola");
consola.start("Deploying local proxy...");
const tmpdir = fs.mkdtempSync("tmp-proxy-");
const fullyResolvedTmpDir = path.resolve(os.tmpdir(), tmpdir);
consola.info("Fetching lambda configuration for", functionName);
let lambdaConfig = await getLambdaConfiguration(functionName, region);
if (lambdaConfig.Runtime !== "nodejs20.x") {
consola.info("Lambda function is not using nodejs20.x runtime");
consola.info("Backing up lambda function configuration...");
fs.mkdirSync(path.resolve(fullyResolvedTmpDir, "backup"), {
recursive: true,
});
const backupFile = path.resolve(
fullyResolvedTmpDir,
`backup/${functionName}.json`
);
fs.writeFileSync(backupFile, JSON.stringify(lambdaConfig, null, 2));
consola.info("Updating lambda function configuration to nodejs20.x");
await updateLambdaFunctionConfiguration({
functionName,
region,
runtime: "nodejs20.x",
handler: "index.handler",
});
lambdaConfig = await getLambdaConfiguration(functionName, region);
}
const handlerName = lambdaConfig.Handler!;
consola.info("Building local proxy with handler", handlerName);
const zipName = await buildProxyHandler(handlerName, fullyResolvedTmpDir);
consola.info("Swapping lambda function code with proxy...");
if (!skipDeploy) {
await updateLambdaFunctionCode(
functionName,
fs.readFileSync(path.resolve(fullyResolvedTmpDir, zipName)),
region
);
}
const env = lambdaConfig.Environment?.Variables!;
if (env) {
env["AWS_DEFAULT_REGION"] = region;
for (const key in env) {
process.env[key] = env?.[key];
}
consola.info("Lambda environment variables applied locally.");
}
//get the path relative to the directory that the cli is run from
const pathofFileRelativeToCli = path.join(process.cwd(), localPath);
consola.info("Connecting to websocket endpoint...");
const websocketEndpoint = await getIotWebsocketEndpoint(region);
if (!websocketEndpoint.endpointAddress) {
throw new Error("No websocket endpoint found");
}
const mqttClient: mqtt5.Mqtt5Client = createMqttClient({
websocketEndpoint: websocketEndpoint.endpointAddress!,
region,
});
const connectionSuccess = once(mqttClient, "connectionSuccess");
mqttClient.start();
await connectionSuccess;
consola.success("Connected. Ready for requests...");
await mqttClient.subscribe({
subscriptions: [{ qos: mqtt5.QoS.AtLeastOnce, topicFilter: topic }],
});
mqttClient.on(
"messageReceived",
async (eventData: mqtt5.MessageReceivedEvent) => {
try {
if (eventData.message.payload) {
const payload = JSON.parse(
toUtf8(new Uint8Array(eventData.message.payload as ArrayBuffer))
);
if (payload.event) {
consola.box(
`********** Received Event (${payload.context.awsRequestId}) **********`
);
let result;
if (pathofFileRelativeToCli.endsWith(".js")) {
result = await invokeByPath(
pathofFileRelativeToCli,
payload.event,
payload.context
);
} else {
consola.warn("File extension not supported. Must be '.js'");
}
consola.info(JSON.stringify(result, null, 2));
const publishedResult = {
context: payload.context,
result,
};
await mqttClient.publish({
qos: mqtt5.QoS.AtLeastOnce,
topicName: topic,
payload: JSON.stringify(publishedResult),
});
consola.box(
`********** Sent Response (${payload.context.awsRequestId}) **********`
);
}
}
} catch (e) {
consola.error(e);
// Swallow the error so that the proxy doesn't crash
}
}
);
mqttClient.on("error", async (e) => {
consola.error(e);
});
const exitContext = {
functionName,
region,
runtime: lambdaConfig.Runtime!,
tmpDir: fullyResolvedTmpDir,
};
setTimeout(() => {
consola.warn("45 minute timeout reached. Exiting...");
exitHandler({ event: "timeout", context: exitContext });
}, 45 * 60 * 1000); // 45 minutes - attempting to keep this shorter than the 1 hour timeout for awsume
process.on(
"exit",
exitHandler.bind(null, { event: "exit", context: exitContext })
);
process.on(
"SIGINT",
exitHandler.bind(null, { event: "SIGINT", context: exitContext })
);
process.on(
"SIGUSR1",
exitHandler.bind(null, { event: "SIGUSR1", context: exitContext })
);
process.on(
"SIGUSR2",
exitHandler.bind(null, { event: "SIGUSR2", context: exitContext })
);
process.on("uncaughtException", (...args) => {
consola.error(args);
exitHandler({ event: "uncaughtException", context: exitContext });
});
};
export const handler = async (argv: Args) => {
let { name: functionName, path: localPath } = argv;
const { consola } = await import("consola");
let region = "us-east-1";
while (!localPath || !fs.existsSync(path.resolve(process.cwd(), localPath))) {
localPath = (await consola.prompt(
"Please provide the path to the local file relative to the current working directory"
)) as string;
if (!fs.existsSync(path.resolve(process.cwd(), localPath))) {
consola.error("File does not exist");
}
}
const topic = `local/${functionName}`;
await run({
region,
functionName: functionName!,
localPath,
topic,
skipDeploy: argv.skipDeploy,
});
};