Skip to content

Commit

Permalink
refactor: export internals
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Jul 16, 2022
1 parent c652242 commit 37ff876
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
43 changes: 32 additions & 11 deletions src/ServerlessOffline.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,19 @@ export default class ServerlessOffline {
// Put here so available everywhere, not just in handlers
env.IS_OFFLINE = true

this._mergeOptions()
this.#mergeOptions()

const { httpEvents, lambdas, scheduleEvents, webSocketEvents } =
this._getEvents()
this.#getEvents()

// if (lambdas.length > 0) {
await this._createLambda(lambdas)
await this.#createLambda(lambdas)
// }

const eventModules = []

if (httpEvents.length > 0) {
eventModules.push(this._createHttp(httpEvents))
eventModules.push(this.#createHttp(httpEvents))
}

if (!this.#options.disableScheduledEvents && scheduleEvents.length > 0) {
Expand Down Expand Up @@ -154,7 +154,7 @@ export default class ServerlessOffline {
log.info(`Got ${command} signal. Offline Halting...`)
}

async _createLambda(lambdas, skipStart) {
async #createLambda(lambdas, skipStart) {
const { default: Lambda } = await import('./lambda/index.js')

this.#lambda = new Lambda(this.#serverless, this.#options)
Expand All @@ -166,7 +166,7 @@ export default class ServerlessOffline {
}
}

async _createHttp(events, skipStart) {
async #createHttp(events, skipStart) {
const { default: Http } = await import('./events/http/index.js')

this.#http = new Http(this.#serverless, this.#options, this.#lambda)
Expand Down Expand Up @@ -213,7 +213,7 @@ export default class ServerlessOffline {
return this.#webSocket.start()
}

_mergeOptions() {
#mergeOptions() {
const {
service: { custom = {}, provider },
} = this.#serverless
Expand Down Expand Up @@ -260,7 +260,7 @@ export default class ServerlessOffline {
log.debug('options:', this.#options)
}

_getEvents() {
#getEvents() {
const { service } = this.#serverless

const httpEvents = []
Expand Down Expand Up @@ -384,8 +384,29 @@ export default class ServerlessOffline {
}
}

// TEMP FIXME quick fix to expose gateway server for testing, look for better solution
getApiGatewayServer() {
return this.#http.getServer()
// TODO FIXME
// TEMP quick fix to expose for testing, look for better solution
internals() {
return {
createHttp: (events, skipStart) => {
return this.#createHttp(events, skipStart)
},

createLambda: (lambdas, skipStart) => {
return this.#createLambda(lambdas, skipStart)
},

getApiGatewayServer: () => {
return this.#http.getServer()
},

getEvents: () => {
return this.#getEvents()
},

mergeOptions: () => {
this.#mergeOptions()
},
}
}
}
6 changes: 3 additions & 3 deletions tests/manual/websocket/main/test/e2e/ws.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@ describe('serverless', () => {
})

it('should request to upgade to WebSocket when receving an HTTP request', async () => {
const _req = chai
const request = chai
.request(
`${endpoint
.replace('ws://', 'http://')
.replace('wss://', 'https://')}`,
)
.keepOpen()
let res = await _req.get(`/${Date.now()}`) // .set('Authorization', user.accessToken);
let res = await request.get(`/${Date.now()}`) // .set('Authorization', user.accessToken);

expect(res).to.have.status(426)

res = await _req.get(`/${Date.now()}/${Date.now()}`) // .set('Authorization', user.accessToken);
res = await request.get(`/${Date.now()}/${Date.now()}`) // .set('Authorization', user.accessToken);

expect(res).to.have.status(426)
}).timeout(timeout)
Expand Down
16 changes: 9 additions & 7 deletions tests/old-unit/support/OfflineBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export default class OfflineBuilder {
functionConfig.handler,
)

const _handlerPath = join('.', handlerPath)
const handlerpath = join('.', handlerPath)

this.#handlers[_handlerPath] = {
this.#handlers[handlerpath] = {
[handlerName]: handler,
}

Expand All @@ -51,13 +51,15 @@ export default class OfflineBuilder {
this.#options,
)

this.#serverlessOffline._mergeOptions()
this.#serverlessOffline.internals().mergeOptions()

const { httpEvents, lambdas } = this.#serverlessOffline._getEvents()
await this.#serverlessOffline._createLambda(lambdas, true)
await this.#serverlessOffline._createHttp(httpEvents, true)
const { httpEvents, lambdas } = this.#serverlessOffline
.internals()
.getEvents()
await this.#serverlessOffline.internals().createLambda(lambdas, true)
await this.#serverlessOffline.internals().createHttp(httpEvents, true)

return this.#serverlessOffline.getApiGatewayServer()
return this.#serverlessOffline.internals().getApiGatewayServer()
}

end(skipExit) {
Expand Down

0 comments on commit 37ff876

Please sign in to comment.