Skip to content

Commit

Permalink
refactor: use private methods (#1423)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk authored May 17, 2022
1 parent c0556a8 commit a332dbf
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 160 deletions.
30 changes: 10 additions & 20 deletions src/ServerlessOffline.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,11 @@ export default class ServerlessOffline {
'offline:start:init': this.start.bind(this),
'offline:start:ready': this.ready.bind(this),
'offline:functionsUpdated:cleanup': this.cleanupFunctions.bind(this),
'offline:start': this._startWithExplicitEnd.bind(this),
'offline:start': this.#startWithExplicitEnd.bind(this),
'offline:start:end': this.end.bind(this),
}
}

_printBlankLine() {
if (env.NODE_ENV !== 'test') {
if (this.log) {
this.log.notice()
} else {
console.log()
}
}
}

// Entry point for the plugin (sls offline) when running 'sls offline start'
async start() {
// Put here so available everywhere, not just in handlers
Expand All @@ -83,7 +73,7 @@ export default class ServerlessOffline {
// check if update is available
updateNotifier({ pkg }).notify()

this._verifyServerlessVersionCompatibility()
this.#verifyServerlessVersionCompatibility()

this._mergeOptions()

Expand All @@ -101,19 +91,19 @@ export default class ServerlessOffline {
}

if (!this.#options.disableScheduledEvents && scheduleEvents.length > 0) {
eventModules.push(this._createSchedule(scheduleEvents))
eventModules.push(this.#createSchedule(scheduleEvents))
}

if (webSocketEvents.length > 0) {
eventModules.push(this._createWebSocket(webSocketEvents))
eventModules.push(this.#createWebSocket(webSocketEvents))
}

await Promise.all(eventModules)
}

async ready() {
if (env.NODE_ENV !== 'test') {
await this._listenForTermination()
await this.#listenForTermination()
}
}

Expand Down Expand Up @@ -168,13 +158,13 @@ export default class ServerlessOffline {
* by downstream plugins. When running sls offline that can be expected, but docs say that
* 'sls offline start' will provide the init and end hooks for other plugins to consume
* */
async _startWithExplicitEnd() {
async #startWithExplicitEnd() {
await this.start()
await this.ready()
this.end()
}

async _listenForTermination() {
async #listenForTermination() {
const command = await new Promise((resolve) => {
process
// SIGINT will be usually sent when user presses ctrl+c
Expand Down Expand Up @@ -231,7 +221,7 @@ export default class ServerlessOffline {
}
}

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

this.#schedule = new Schedule(
Expand All @@ -243,7 +233,7 @@ export default class ServerlessOffline {
this.#schedule.create(events)
}

async _createWebSocket(events) {
async #createWebSocket(events) {
const { default: WebSocket } = await import('./events/websocket/index.js')

this.#webSocket = new WebSocket(
Expand Down Expand Up @@ -464,7 +454,7 @@ export default class ServerlessOffline {
}

// TODO: missing tests
_verifyServerlessVersionCompatibility() {
#verifyServerlessVersionCompatibility() {
const currentVersion = this.#serverless.version
const requiredVersionRange = pkg.peerDependencies.serverless

Expand Down
12 changes: 6 additions & 6 deletions src/events/http/Endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ export default class Endpoint {

// TODO FIXME
// eslint-disable-next-line no-constructor-return
return this._generate()
return this.#generate()
}

// determine whether we have function level overrides for velocity templates
// if not we will use defaults
_setVmTemplates(fullEndpoint) {
#setVmTemplates(fullEndpoint) {
// determine requestTemplate
// first check if requestTemplate is set through serverless
const fep = fullEndpoint
Expand Down Expand Up @@ -107,7 +107,7 @@ export default class Endpoint {

// loosely based on:
// https://github.com/serverless/serverless/blob/v1.59.2/lib/plugins/aws/package/compile/events/apiGateway/lib/validate.js#L380
_getIntegration(http) {
#getIntegration(http) {
const { integration, async: isAsync } = http
if (integration) {
const normalizedIntegration = integration.toUpperCase().replace('-', '_')
Expand All @@ -128,19 +128,19 @@ export default class Endpoint {
}

// return fully generated Endpoint
_generate() {
#generate() {
const offlineEndpoint = new OfflineEndpoint()

const fullEndpoint = {
...offlineEndpoint,
...this.#http,
}

fullEndpoint.integration = this._getIntegration(this.#http)
fullEndpoint.integration = this.#getIntegration(this.#http)

if (fullEndpoint.integration === 'AWS') {
// determine request and response templates or use defaults
return this._setVmTemplates(fullEndpoint)
return this.#setVmTemplates(fullEndpoint)
}

return fullEndpoint
Expand Down
Loading

0 comments on commit a332dbf

Please sign in to comment.