Skip to content

Commit

Permalink
use a library to set the port
Browse files Browse the repository at this point in the history
  • Loading branch information
cnuss committed Jun 4, 2024
1 parent 655c961 commit 1ebdcb1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"desm": "^1.3.1",
"execa": "^8.0.1",
"fs-extra": "^11.2.0",
"get-port": "^7.1.0",
"is-wsl": "^3.1.0",
"java-invoke-local": "0.0.6",
"jose": "^5.2.1",
Expand Down
23 changes: 13 additions & 10 deletions src/lambda/RuntimeServer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import crypto from "node:crypto"
import { Server } from "@hapi/hapi"
import getPort from "get-port"
import { log } from "../utils/log.js"

/**
Expand All @@ -23,16 +24,9 @@ export default class RuntimeServer {
#callback = null

constructor(event, context, timeout) {
// DEVNOTE: excluding "port", the hapi server will randomly assign one...
// this is because each execution has a dedicated AWS_LAMBDA_RUNTIME_API endpoint
const serverOptions = {
host: "127.0.0.1",
}

this.#event = event
this.#context = context
this.#timeout = timeout
this.#server = new Server(serverOptions)
}

async start(startCb, payloadCb) {
Expand All @@ -43,28 +37,37 @@ export default class RuntimeServer {
const nextRoute = this.nextRoute()
const responseRoute = this.responseRoute()

// TODO: error route
// DEVNOTE: Each invocation gets a s short-lived server on a random port
this.#server = new Server({
host: "127.0.0.1",
port: await getPort(),
})

// TODO: error route
this.#server.route([nextRoute, responseRoute])

try {
await this.#server.start()
} catch (err) {
throw new Error(
`Unexpected error while starting serverless-offline lambda rie server: ${err}`,
`Unexpected error while starting serverless-offline lambda runtime server: ${err}`,
)
}

this.#runtimeApi = `${this.#server.info.host}:${this.#server.info.port}`

log.verbose(
`Offline [http for lambda rie] listening on http://${this.#runtimeApi}`,
`Offline [http for lambda runtime] listening on http://${this.#runtimeApi}`,
)

startCb(this.#runtimeApi)
}

stop(timeout) {
if (!this.#server) {
return Promise.resolve()
}

return this.#server
.stop({
timeout,
Expand Down

0 comments on commit 1ebdcb1

Please sign in to comment.