Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@
"Vasiliy Solovey (https://github.com/miltador)",
"Dima Krutolianov (https://github.com/dimadk24)",
"Bryan Vaz (https://github.com/bryanvaz)",
"Justin Ng (https://github.com/njyjn)"
"Justin Ng (https://github.com/njyjn)",
"Ahmed Bayoumy (https://github.com/bayoumymac)"
],
"engines": {
"node": ">=10.13.0"
Expand Down
34 changes: 33 additions & 1 deletion src/ServerlessOffline.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class ServerlessOffline {
#schedule = null
#webSocket = null
#lambda = null
#alb = null
#serverless = null

constructor(serverless, cliOptions) {
Expand Down Expand Up @@ -73,6 +74,7 @@ export default class ServerlessOffline {
lambdas,
scheduleEvents,
webSocketEvents,
albEvents,
} = this._getEvents()

// if (lambdas.length > 0) {
Expand All @@ -93,6 +95,10 @@ export default class ServerlessOffline {
eventModules.push(this._createWebSocket(webSocketEvents))
}

if (albEvents.length > 0) {
eventModules.push(this._createAlb(albEvents))
}

await Promise.all(eventModules)
}

Expand Down Expand Up @@ -129,6 +135,10 @@ export default class ServerlessOffline {
eventModules.push(this.#webSocket.stop(SERVER_SHUTDOWN_TIMEOUT))
}

if (this.#alb) {
eventModules.push(this.#alb.stop(SERVER_SHUTDOWN_TIMEOUT))
}

await Promise.all(eventModules)

if (!skipExit) {
Expand Down Expand Up @@ -196,6 +206,18 @@ export default class ServerlessOffline {
}
}

async _createAlb(events, skipStart) {
const { default: Alb } = await import('./events/alb/index.js')

this.#alb = new Alb(this.#serverless, this.#options, this.#lambda)

this.#alb.create(events)

if (!skipStart) {
await this.#alb.start()
}
}

async _createSchedule(events) {
const { default: Schedule } = await import('./events/schedule/index.js')

Expand Down Expand Up @@ -269,6 +291,7 @@ export default class ServerlessOffline {
const lambdas = []
const scheduleEvents = []
const webSocketEvents = []
const albEvents = []

const functionKeys = service.getAllFunctions()

Expand All @@ -282,7 +305,7 @@ export default class ServerlessOffline {
const events = service.getAllEventsInFunction(functionKey) || []

events.forEach((event) => {
const { http, httpApi, schedule, websocket } = event
const { http, httpApi, schedule, websocket, alb } = event

if ((http || httpApi) && functionDefinition.handler) {
const httpEvent = {
Expand Down Expand Up @@ -348,6 +371,14 @@ export default class ServerlessOffline {
websocket,
})
}

if (alb) {
albEvents.push({
functionKey,
handler: functionDefinition.handler,
alb,
})
}
})
})

Expand All @@ -369,6 +400,7 @@ export default class ServerlessOffline {
lambdas,
scheduleEvents,
webSocketEvents,
albEvents,
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/events/alb/Alb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import AlbEventDefinition from './AlbEventDefinition.js'
import HttpServer from './HttpServer.js'

export default class Alb {
#httpServer = null

constructor(serverless, options, lambda) {
this.#httpServer = new HttpServer(serverless, options, lambda)
}

start() {
return this.#httpServer.start()
}

// stops the server
stop(timeout) {
return this.#httpServer.stop(timeout)
}

_create(functionKey, rawALBEventDefinition, handler) {
const httpEvent = new AlbEventDefinition(rawALBEventDefinition)

this.#httpServer.createRoutes(functionKey, httpEvent, handler)
}

create(events) {
events.forEach(({ functionKey, handler, alb }) => {
this._create(functionKey, alb, handler)
})

this.#httpServer.writeRoutesTerminal()
}
}
22 changes: 22 additions & 0 deletions src/events/alb/AlbEventDefinition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { assign } = Object

export default class AlbEventDefinition {
constructor(rawAlbEventDefinition) {
let listenerArn
let priority
let conditions
let rest

if (typeof rawAlbEventDefinition === 'string') {
;[listenerArn, priority, conditions] = rawAlbEventDefinition.split(' ')
} else {
;({ listenerArn, priority, conditions, ...rest } = rawAlbEventDefinition)
}

this.listenerArn = listenerArn
this.priority = priority
this.conditions = conditions

assign(this, rest)
}
}
Loading