forked from anomalyco/sst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.js
More file actions
639 lines (579 loc) · 17.9 KB
/
Copy pathstart.js
File metadata and controls
639 lines (579 loc) · 17.9 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
"use strict";
const path = require("path");
const array = require("../lib/array");
const fs = require("fs-extra");
const chalk = require("chalk");
const detect = require("detect-port-alt");
const {
logger,
getChildLogger,
STACK_DEPLOY_STATUS,
Runtime,
Bridge,
State,
useStacksBuilder,
useFunctionBuilder,
useLocalServer,
} = require("@serverless-stack/core");
const paths = require("./util/paths");
const {
synth,
deploy,
prepareCdk,
writeConfig,
checkFileExists,
writeOutputsFile,
} = require("./util/cdkHelpers");
const objectUtil = require("../lib/object");
const spawn = require("cross-spawn");
let isConsoleEnabled = false;
// This flag is currently used by the "sst.Script" construct to make the "BuiltAt"
// remain the same when rebuilding infrastructure.
const debugStartedAt = Date.now();
const IS_TEST = process.env.__TEST__ === "true";
// Setup logger
const clientLogger = {
debug: (...m) => {
getChildLogger("client").debug(...m);
},
trace: (...m) => {
// If console is not enabled, print trace in terminal (ie. request logs)
isConsoleEnabled
? getChildLogger("client").trace(...m)
: getChildLogger("client").info(...m);
},
// This is a temporary workaround to send metadata alongside log message to
// browser. After we decide if we want to keep both the terminal and console modes
// we can clean this up. Ideally all logs sent to the browser should have metadata
// attached. Note that you cannot log multiple arguments with
// "traceWithMetadata()", the first arg is the log message and the second arg
// is the metadata.
traceWithMetadata: (m) => {
// If console is not enabled, print trace in terminal (ie. request logs)
isConsoleEnabled
? getChildLogger("client").trace(m)
: getChildLogger("client").info(m);
},
info: (...m) => {
getChildLogger("client").info(...m);
},
warn: (...m) => {
getChildLogger("client").warn(...m);
},
error: (...m) => {
getChildLogger("client").error(...m);
},
};
module.exports = async function (argv, config, cliInfo) {
await prepareCdk(argv, cliInfo, config);
// Deploy debug stack
const debugStackOutputs = await deployDebugStack(config, cliInfo);
const debugEndpoint = debugStackOutputs.Endpoint;
const debugBucketArn = debugStackOutputs.BucketArn;
const debugBucketName = debugStackOutputs.BucketName;
// Startup UDP
const bridge = new Bridge.Server();
if (argv.udp) {
clientLogger.info(chalk.grey(`Using UDP connection`));
config.debugBridge = await bridge.start();
}
// Deploy app
const { deployRet: appStackDeployRet } = await deployApp(
argv,
{
...config,
debugEndpoint,
debugBucketArn,
debugBucketName,
},
cliInfo
);
await updateStaticSiteEnvironmentOutputs(appStackDeployRet);
if (IS_TEST) {
process.exit(0);
}
logger.info("");
logger.info("==========================");
logger.info(" Starting Live Lambda Dev");
logger.info("==========================");
logger.info("");
const funcs = State.Function.read(paths.appPath);
// Startup Websocket
const ws = new Runtime.WS();
ws.onMessage.add((msg) => {
switch (msg.action) {
case "register":
bridge.addPeer(msg.body);
bridge.ping();
break;
case "server.clientRegistered":
clientLogger.info("Debug session started. Listening for requests...");
clientLogger.debug(`Client connection id: ${msg.clientConnectionId}`);
break;
case "server.clientDisconnectedDueToNewClient":
clientLogger.warn(
"A new debug session has been started. This session will be closed..."
);
break;
case "server.failedToSendResponseDueToStubDisconnected":
clientLogger.error(
chalk.grey(msg.debugRequestId) +
" Failed to send response because the Lambda function is disconnected"
);
break;
}
});
ws.start(debugEndpoint, debugBucketName);
const server = new Runtime.Server({
port: argv.port || (await chooseServerPort(12557)),
});
const local = useLocalServer({
port: await chooseServerPort(13557),
app: config.name,
stage: config.stage,
region: config.region,
});
server.onStdErr.add((arg) => {
arg.data.endsWith("\n")
? clientLogger.trace(arg.data.slice(0, -1))
: clientLogger.trace(arg.data);
});
server.onStdOut.add((arg) => {
arg.data.endsWith("\n")
? clientLogger.trace(arg.data.slice(0, -1))
: clientLogger.trace(arg.data);
});
server.onStdErr.add((arg) => {
local.updateFunction(arg.funcId, (s) => {
const entry = s.invocations.find((i) => i.id === arg.requestId);
entry.logs.push({
timestamp: Date.now(),
message: arg.data,
});
});
});
server.onStdOut.add((arg) => {
local.updateFunction(arg.funcId, (s) => {
const entry = s.invocations.find((i) => i.id === arg.requestId);
entry.logs.push({
timestamp: Date.now(),
message: arg.data,
});
});
});
server.listen();
const watcher = new Runtime.Watcher();
watcher.reload(paths.appPath, config);
const functionBuilder = useFunctionBuilder({
root: paths.appPath,
checks: {
type: config.typeCheck,
lint: config.lint,
},
});
functionBuilder.reload();
functionBuilder.onTransition.add((evt) => {
const { value, context } = evt.state;
local.updateFunction(context.info.id, (draft) => {
draft.warm = context.warm;
draft.state = value;
draft.issues = context.issues;
});
if (value === "building")
clientLogger.info(
chalk.gray(
`Functions: Building ${context.info.srcPath} ${context.info.handler}...`
)
);
if (value === "checking") {
clientLogger.info(
chalk.gray(
`Functions: Done building ${context.info.srcPath} ${
context.info.handler
} (${Date.now() - context.buildStart}ms)`
)
);
server.drain(context.info);
}
});
watcher.onChange.add((evt) => {
logger.debug("File changed: ", evt.files);
functionBuilder.broadcast({
type: "FILE_CHANGE",
file: evt.files[0],
});
});
const stacksBuilder = useStacksBuilder(
paths.appPath,
config,
cliInfo.cdkOptions,
async (opts) => {
const result = await deploy(opts);
if (result.some((r) => r.status === "failed"))
throw new Error("Stacks failed to deploy");
}
);
stacksBuilder.onTransition(async (state) => {
local.updateState((draft) => {
draft.stacks.status = state.value;
});
if (state.value.idle) {
if (state.value.idle === "unchanged") {
clientLogger.info(chalk.grey("Stacks: No changes to deploy."));
}
if (state.value.idle === "deployed") {
watcher.reload(paths.appPath, config);
functionBuilder.reload();
// TODO: Move all this to functionBuilder state machine
await Promise.all(funcs.map((f) => server.drain(f).catch(() => {})));
funcs.splice(0, funcs.length, ...State.Function.read(paths.appPath));
}
}
if (state.value === "building") {
clientLogger.info(chalk.grey("Stacks: Building changes..."));
}
if (state.value === "synthing") {
clientLogger.info(chalk.grey("Stacks: Synthesizing changes..."));
}
if (state.value === "deployable") {
clientLogger.info(
chalk.cyan(
"Stacks: There are new infrastructure changes. Press ENTER to redeploy."
)
);
}
});
local.onDeploy.add(() => stacksBuilder.send("TRIGGER_DEPLOY"));
if (!IS_TEST)
process.stdin.on("data", () => stacksBuilder.send("TRIGGER_DEPLOY"));
// Handle requests from udp or ws
async function handleRequest(req) {
const timeoutAt = Date.now() + req.debugRequestTimeoutInMs;
const func = funcs.find((f) => f.id === req.functionId);
if (!func) {
console.error("Unable to find function", req.functionId);
return {
type: "failure",
body: "Failed to find function",
};
}
functionBuilder.send(func.id, { type: "INVOKE" });
const eventSource = parseEventSource(req.event);
const eventSourceDesc =
eventSource === null ? " invoked" : ` invoked by ${eventSource}`;
clientLogger.traceWithMetadata(
chalk.grey(
`${req.context.awsRequestId} REQUEST ${req.env.AWS_LAMBDA_FUNCTION_NAME} [${func.handler}]${eventSourceDesc}`
),
{ event: req.event }
);
local.updateFunction(func.id, (draft) => {
if (draft.invocations.length >= 25) draft.invocations.pop();
draft.invocations.unshift({
id: req.context.awsRequestId,
request: req.event,
times: {
start: Date.now(),
},
logs: [],
});
});
clientLogger.debug("Invoking local function...");
const result = await server.invoke({
function: {
...func,
root: paths.appPath,
},
env: {
...getSystemEnv(),
...req.env,
},
payload: {
event: req.event,
context: req.context,
deadline: timeoutAt,
},
});
local.updateFunction(func.id, (draft) => {
const invocation = draft.invocations.find(
(x) => x.id === req.context.awsRequestId
);
invocation.response = result;
invocation.times.end = Date.now();
});
clientLogger.debug("Response", result);
if (result.type === "success") {
clientLogger.traceWithMetadata(
chalk.grey(
`${req.context.awsRequestId} RESPONSE ${objectUtil.truncate(
result.data,
{
totalLength: 1500,
arrayLength: 10,
stringLength: 100,
}
)}`
),
{ response: result.data }
);
return {
type: "success",
body: result.data,
};
}
if (result.type === "failure") {
clientLogger.info(
`${chalk.grey(req.context.awsRequestId)} ${chalk.red("ERROR")}`,
result.error.errorType + ":",
result.error.errorMessage,
"\n",
(result.error.stackTrace || []).join("\n")
);
return {
type: "failure",
body: {
errorMessage: result.error.errorMessage,
errorType: result.error.errorType,
stackTrace: result.error.stackTrace,
},
};
}
}
bridge.onRequest(handleRequest);
ws.onRequest(handleRequest);
// TODO: Figure out how to abstract this
const data = fs.readJSONSync(State.resolve(paths.appPath, "constructs.json"));
for (let construct of data) {
if (
construct.type === "Api" &&
construct.local &&
construct.local.codegen
) {
const proc = spawn("npx", [
"graphql-codegen",
"--watch",
"-c",
construct.local.codegen,
]);
proc.stdout.on("data", (data) => {
const line = data.toString();
clientLogger.debug(line);
if (line.includes("Parse configuration [started]"))
clientLogger.info(chalk.grey("Running GraphQL code generation..."));
if (line.includes("Generate outputs [failed]"))
clientLogger.info(chalk.red("Failed to load GraphQL schema"));
if (line.includes("with") && line.includes("error"))
clientLogger.info(chalk.red(line));
if (line.includes("Generate outputs [completed]"))
clientLogger.info(chalk.grey("Finished GraphQL code generation"));
});
}
}
clientLogger.info(
`SST Console: https://console.serverless-stack.com/${config.name}/${
config.stage
}/local${local.port !== 13557 ? "?_port=" + local.port : ""}`
);
};
async function deployDebugStack(config, cliInfo) {
// Do not deploy if running test
if (IS_TEST) {
return {
Endpoint: "ws://test-endpoint",
BucketArn: "bucket-arn",
BucketName: "bucket-name",
};
}
logger.info("");
logger.info("=======================");
logger.info(" Deploying debug stack");
logger.info("=======================");
logger.info("");
const stackName = `${config.stage}-${config.name}-debug-stack`;
const cdkOptions = {
...cliInfo.cdkOptions,
app: `node bin/index.js ${stackName} ${config.stage} ${config.region} ${
paths.appPath
} ${State.stacksPath(paths.appPath)}`,
output: "cdk.out",
};
// Change working directory
// Note: When deploying the debug stack, the current working directory is user's app.
// Setting the current working directory to debug stack cdk app directory to allow
// Lambda Function construct be able to reference code with relative path.
process.chdir(path.join(paths.ownPath, "assets", "debug-stack"));
// Build
await synth(cdkOptions);
// Deploy
const deployRet = await deploy(cdkOptions);
logger.debug("deployRet", deployRet);
// Restore working directory
process.chdir(paths.appPath);
// Get WebSocket endpoint
if (
!deployRet ||
deployRet.length !== 1 ||
deployRet[0].status === STACK_DEPLOY_STATUS.FAILED
) {
throw new Error(`Failed to deploy debug stack ${stackName}`);
} else if (!deployRet[0].outputs || !deployRet[0].outputs.Endpoint) {
throw new Error(
`Failed to get the endpoint from the deployed debug stack ${stackName}`
);
}
return deployRet[0].outputs;
}
async function deployApp(argv, config, cliInfo) {
logger.info("");
logger.info("===============");
logger.info(" Deploying app");
logger.info("===============");
logger.info("");
await writeConfig({
...config,
debugStartedAt,
debugIncreaseTimeout: argv.increaseTimeout || false,
});
// Build
await synth(cliInfo.cdkOptions);
let deployRet;
if (IS_TEST) {
deployRet = [];
} else {
// Deploy
deployRet = await deploy({
...cliInfo.cdkOptions,
hotswap: true,
});
// Check all stacks deployed successfully
if (
deployRet.some((stack) => stack.status === STACK_DEPLOY_STATUS.FAILED)
) {
throw new Error(`Failed to deploy the app`);
}
}
// Write outputsFile
if (argv.outputsFile) {
await writeOutputsFile(
deployRet,
path.join(paths.appPath, argv.outputsFile),
cliInfo.cdkOptions
);
}
return { deployRet };
}
////////////////////
// Util functions //
////////////////////
async function updateStaticSiteEnvironmentOutputs(deployRet) {
// ie. environments outputs
// [{
// id: "MyFrontend",
// path: "src/sites/react-app",
// stack: "dev-playground-another",
// environmentOutputs: {
// "REACT_APP_API_URL":"FrontendSSTSTATICSITEENVREACTAPPAPIURLFAEF5D8C",
// "ABC":"FrontendSSTSTATICSITEENVABC527391D2"
// }
// }]
//
// ie. deployRet
// [{
// name: "dev-playground-another",
// outputs: {
// "FrontendSSTSTATICSITEENVREACTAPPAPIURLFAEF5D8C":"https://...",
// "FrontendSSTSTATICSITEENVABC527391D2":"hi"
// }
// }]
const environmentOutputKeysPath = path.join(
paths.appPath,
paths.appBuildDir,
"static-site-environment-output-keys.json"
);
const environmentOutputValuesPath = path.join(
paths.appPath,
paths.appBuildDir,
"static-site-environment-output-values.json"
);
if (!(await checkFileExists(environmentOutputKeysPath))) {
throw new Error(`Failed to get the StaticSite info from the app`);
}
// Replace output value with stack output
const environments = await fs.readJson(environmentOutputKeysPath);
environments.forEach(({ stack, environmentOutputs }) => {
const stackData = deployRet.find(({ name }) => name === stack);
if (stackData) {
Object.entries(environmentOutputs).forEach(([envName, outputName]) => {
environmentOutputs[envName] = stackData.outputs[outputName];
});
}
});
// Update file
await fs.writeJson(environmentOutputValuesPath, environments);
}
async function chooseServerPort(defaultPort) {
const host = "0.0.0.0";
logger.debug(`Checking port ${defaultPort} on host ${host}`);
try {
return detect(defaultPort, host);
} catch (err) {
throw new Error(
chalk.red(`Could not find an open port at ${chalk.bold(host)}.`) +
"\n" +
("Network error message: " + err.message || err) +
"\n"
);
}
}
function getSystemEnv() {
const env = { ...process.env };
// AWS_PROFILE is defined if users run `AWS_PROFILE=xx sst start`, and in
// aws sdk v3, AWS_PROFILE takes precedence over AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY.
// Hence we need to remove it to ensure the invoked function uses the IAM
// credentials from the remote Lambda.
delete env.AWS_PROFILE;
return env;
}
function parseEventSource(event) {
try {
// HTTP
if (["2.0", "1.0"].includes(event.version) && event.requestContext.apiId) {
return event.version === "1.0"
? `API ${event.httpMethod} ${event.path}`
: `API ${event.requestContext.http.method} ${event.rawPath}`;
}
// HTTP Authorizer
if (["TOKEN", "REQUEST"].includes(event.type) && event.methodArn) {
return "API authorizer";
}
if (event.Records && event.Records.length > 0) {
// SNS
if (event.Records[0].EventSource === "aws:sns") {
// TopicArn: arn:aws:sns:us-east-1:123456789012:ExampleTopic
const topics = array.unique(
event.Records.map((record) => record.Sns.TopicArn.split(":").pop())
);
return topics.length === 1
? `SNS topic ${topics[0]}`
: `SNS topics: ${topics.join(", ")}`;
}
// SQS
if (event.Records.EventSource === "aws:sqs") {
// eventSourceARN: arn:aws:sqs:us-east-1:123456789012:MyQueue
const names = array.unique(
event.Records.map((record) => record.eventSourceARN.split(":").pop())
);
return names.length === 1
? `SQS queue ${names[0]}`
: `SQS queues: ${names.join(", ")}`;
}
// DynamoDB
if (event.Records.EventSource === "aws:dynamodb") {
return "DynamoDB";
}
}
} catch (e) {
clientLogger.debug("Failed to parse event source", e);
}
return null;
}