|
| 1 | +import * as fs from 'fs'; |
| 2 | +import { EventEmitter } from 'events'; |
| 3 | +import * as path from 'path'; |
| 4 | + |
| 5 | +import getPort from 'get-port'; |
| 6 | +import { dirSync as tmpDirSync, DirResult } from 'tmp'; |
| 7 | + |
| 8 | +import { SAMTemplate } from './SAMTemplate'; |
| 9 | +import { ConfigLambda, SAMLocalAPICLIOptions } from './types'; |
| 10 | +import { unzipToLocation } from './utils'; |
| 11 | +import { createSAMLocal, SAMLocal } from './SAMLocal'; |
| 12 | + |
| 13 | +type GeneratorType = 'api' | 'sdk'; |
| 14 | + |
| 15 | +type SAMInstance = { |
| 16 | + port: number; |
| 17 | + host: string; |
| 18 | + endpoint: string; |
| 19 | + region: string; |
| 20 | +}; |
| 21 | + |
| 22 | +class LoggerEmitter extends EventEmitter {} |
| 23 | + |
| 24 | +/** |
| 25 | + * Generator for an AWS SAM CLI instance |
| 26 | + */ |
| 27 | +class SAMGenerator { |
| 28 | + loggerEmitter: LoggerEmitter; |
| 29 | + SAM?: SAMLocal; |
| 30 | + SAMInstance?: SAMInstance; |
| 31 | + template: SAMTemplate; |
| 32 | + tmpDir: DirResult; |
| 33 | + type: GeneratorType; |
| 34 | + |
| 35 | + constructor(type: GeneratorType) { |
| 36 | + this.loggerEmitter = new LoggerEmitter(); |
| 37 | + this.tmpDir = tmpDirSync({ unsafeCleanup: true }); |
| 38 | + this.template = new SAMTemplate(); |
| 39 | + this.type = type; |
| 40 | + } |
| 41 | + |
| 42 | + // Explicit `this` binding |
| 43 | + private onData = (message: any) => { |
| 44 | + this.loggerEmitter.emit('data', message); |
| 45 | + }; |
| 46 | + |
| 47 | + // Explicit `this` binding |
| 48 | + private onError = (message: any) => { |
| 49 | + this.loggerEmitter.emit('error', message); |
| 50 | + }; |
| 51 | + |
| 52 | + /** |
| 53 | + * Unpacks the lambdas into a temporary workDir and creates a SAM Template |
| 54 | + * that can be passed to AWS SAM CLI |
| 55 | + * |
| 56 | + * @param lambdas - Key/Value object with the lambdas that should be created |
| 57 | + * @param cwd |
| 58 | + */ |
| 59 | + async generateLambdas( |
| 60 | + lambdas: Record<string, ConfigLambda>, |
| 61 | + cwd: string = process.cwd() |
| 62 | + ) { |
| 63 | + // Unpack all lambdas |
| 64 | + for (const [functionName, lambda] of Object.entries(lambdas)) { |
| 65 | + const functionSourcePath = path.isAbsolute(lambda.filename) |
| 66 | + ? lambda.filename |
| 67 | + : path.join(cwd, lambda.filename); |
| 68 | + |
| 69 | + await unzipToLocation( |
| 70 | + functionSourcePath, |
| 71 | + path.join(this.tmpDir.name, functionName) |
| 72 | + ); |
| 73 | + |
| 74 | + this.template.addLambda(functionName, { |
| 75 | + Type: 'AWS::Serverless::Function', |
| 76 | + Properties: { |
| 77 | + Handler: `${functionName}/${lambda.handler}`, |
| 78 | + Description: functionName, |
| 79 | + Runtime: lambda.runtime, |
| 80 | + MemorySize: lambda.memorySize ?? 128, |
| 81 | + Timeout: 29, // Max timeout from API Gateway |
| 82 | + Environment: { |
| 83 | + Variables: lambda.environment ?? {}, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + if (lambda.route) { |
| 89 | + this.template.addRoute(functionName, 'api', { |
| 90 | + Type: 'HttpApi', |
| 91 | + Properties: { |
| 92 | + Path: lambda.route, |
| 93 | + Method: lambda.method ?? 'any', |
| 94 | + TimeoutInMillis: 29000, // Max timeout |
| 95 | + PayloadFormatVersion: '2.0', |
| 96 | + }, |
| 97 | + }); |
| 98 | + } else if (lambda.routes) { |
| 99 | + for (const routeKey in lambda.routes) { |
| 100 | + this.template.addRoute(functionName, routeKey, { |
| 101 | + Type: 'HttpApi', |
| 102 | + Properties: { |
| 103 | + Path: lambda.routes[routeKey], |
| 104 | + Method: lambda.method ?? 'any', |
| 105 | + TimeoutInMillis: 29000, // Max timeout |
| 106 | + PayloadFormatVersion: '2.0', |
| 107 | + }, |
| 108 | + }); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Write the SAM template |
| 113 | + fs.writeFileSync( |
| 114 | + path.join(this.tmpDir.name, 'template.yml'), |
| 115 | + this.template.toYaml() |
| 116 | + ); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Start the AWS SAM CLI |
| 122 | + * |
| 123 | + * @param cliOptions |
| 124 | + */ |
| 125 | + async start(cliOptions: SAMLocalAPICLIOptions = {}) { |
| 126 | + const port = cliOptions.port || (await getPort()); |
| 127 | + const host = cliOptions.host || '127.0.0.1'; |
| 128 | + const endpoint = `http://${host}:${port}`; |
| 129 | + const region = cliOptions.region || 'local'; |
| 130 | + this.SAM = await createSAMLocal(this.type, this.tmpDir.name, { |
| 131 | + onData: this.onData, |
| 132 | + onError: this.onError, |
| 133 | + cliOptions: { ...cliOptions, port, host, region }, |
| 134 | + }); |
| 135 | + |
| 136 | + this.SAMInstance = { |
| 137 | + port, |
| 138 | + host, |
| 139 | + endpoint, |
| 140 | + region, |
| 141 | + }; |
| 142 | + |
| 143 | + return this.SAMInstance; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Stop the AWS SAM CLI |
| 148 | + */ |
| 149 | + async stop() { |
| 150 | + if (this.SAM) { |
| 151 | + await this.SAM.kill(); |
| 152 | + } |
| 153 | + |
| 154 | + this.tmpDir.removeCallback(); |
| 155 | + this.SAMInstance = undefined; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Subscribe to data or error messages from the CLI |
| 160 | + * @param topic |
| 161 | + * @param callback |
| 162 | + */ |
| 163 | + on(topic: 'data' | 'error', callback: (data: string) => void) { |
| 164 | + this.loggerEmitter.on(topic, callback); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Unsubscribe to data or error messages from the CLI |
| 169 | + * @param topic |
| 170 | + * @param callback |
| 171 | + */ |
| 172 | + off(topic: 'data' | 'error', callback: (data: string) => void) { |
| 173 | + this.loggerEmitter.off(topic, callback); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +export { SAMGenerator }; |
0 commit comments