|
| 1 | +import {resolve, join} from 'node:path' |
| 2 | +import {cwd} from 'node:process' |
| 3 | + |
| 4 | +export default class ServerlessOfflineLambdaFunctionUrls { |
| 5 | + constructor(serverless) { |
| 6 | + const configuration = serverless.config.serverless.configurationInput |
| 7 | + this.serverless = serverless |
| 8 | + this.configuration = configuration |
| 9 | + this.hooks = { |
| 10 | + 'offline:start:init': () => this.init(), |
| 11 | + } |
| 12 | + } |
| 13 | + getLambdas(functions) { |
| 14 | + return Object.entries(functions).reduce( |
| 15 | + (lambdas, [functionKey, functionDefinition]) => [ |
| 16 | + ...lambdas, |
| 17 | + { |
| 18 | + functionKey, |
| 19 | + functionDefinition: { |
| 20 | + ...functionDefinition, |
| 21 | + handler: this.getTranspiledHandlerFilepath(functionDefinition.handler), |
| 22 | + }, |
| 23 | + }, |
| 24 | + ], |
| 25 | + [] |
| 26 | + ) |
| 27 | + } |
| 28 | + filterNonUrlEnabledFunctions(configuration) { |
| 29 | + return Object.entries(configuration.functions).reduce((functions, [functionKey, functionDefinition]) => { |
| 30 | + if (!functionDefinition.url) { |
| 31 | + return functions |
| 32 | + } |
| 33 | + return {...functions, [functionKey]: functionDefinition} |
| 34 | + }, {}) |
| 35 | + } |
| 36 | + getEvents(functions) { |
| 37 | + return Object.entries(functions).reduce((events, [functionKey, {handler}]) => { |
| 38 | + const path = `/${encodeURIComponent(functionKey)}` |
| 39 | + return [ |
| 40 | + ...events, |
| 41 | + {functionKey, handler, http: {path, method: 'GET'}}, |
| 42 | + {functionKey, handler, http: {path, method: 'POST'}}, |
| 43 | + ] |
| 44 | + }, []) |
| 45 | + } |
| 46 | + mergeServerlessOfflineOptions(options) { |
| 47 | + const stage = this.serverless.variables.options?.stage ?? this.configuration.provider?.stage |
| 48 | + const serverlessOfflineOptions = this.configuration.custom['serverless-offline'] |
| 49 | + return { |
| 50 | + ...serverlessOfflineOptions, |
| 51 | + stage, |
| 52 | + httpPort: serverlessOfflineOptions['urlLambdaFunctionsHttpPort'] ?? 3003, |
| 53 | + ...options, |
| 54 | + } |
| 55 | + } |
| 56 | + getTranspiledHandlerFilepath(handler) { |
| 57 | + return join('.esbuild', '.build', handler) |
| 58 | + } |
| 59 | + getFullPath(...args) { |
| 60 | + return resolve(cwd(), ...args) |
| 61 | + } |
| 62 | + async init() { |
| 63 | + const {default: Lambda} = await import(this.getFullPath('node_modules', 'serverless-offline/src/lambda/Lambda.js')) |
| 64 | + const {default: Http} = await import(this.getFullPath('node_modules', 'serverless-offline/src/events/http/Http.js')) |
| 65 | + const functions = this.filterNonUrlEnabledFunctions(this.configuration) |
| 66 | + |
| 67 | + const lambda = new Lambda(this.serverless, this.mergeServerlessOfflineOptions({noTimeout: true})) |
| 68 | + lambda.create(this.getLambdas(functions)) |
| 69 | + |
| 70 | + const http = new Http(this.serverless, this.mergeServerlessOfflineOptions(), lambda) |
| 71 | + |
| 72 | + await http.createServer() |
| 73 | + |
| 74 | + http.create(this.getEvents(functions)) |
| 75 | + http.createResourceRoutes() |
| 76 | + http.create404Route() |
| 77 | + |
| 78 | + await http.start() |
| 79 | + } |
| 80 | +} |
0 commit comments