Skip to content

Commit

Permalink
feat: remove babel, use esm (#1455)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk authored May 30, 2022
1 parent 675ee69 commit 1f4d836
Show file tree
Hide file tree
Showing 123 changed files with 727 additions and 2,398 deletions.
File renamed without changes.
14 changes: 0 additions & 14 deletions babel.config.js

This file was deleted.

File renamed without changes.
2,052 changes: 216 additions & 1,836 deletions package-lock.json

Large diffs are not rendered by default.

17 changes: 6 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
"version": "8.8.0",
"description": "Emulate AWS λ and API Gateway locally when developing your Serverless project",
"license": "MIT",
"main": "dist/main.js",
"type": "commonjs",
"main": "src/index.js",
"type": "module",
"scripts": {
"build": "rimraf dist && babel src --ignore \"**/__tests__/**/*\" --out-dir dist && copyfiles -u 1 \"src/**/*.{vm,py,rb}\" dist",
"format": "eslint . --fix",
"lint": "eslint .",
"lint:updated": "pipe-git-updated --ext=js -- eslint",
"list-contributors": "echo 'clone https://github.com/mgechev/github-contributors-list.git first, then run npm install' && cd ../github-contributors-list && node bin/githubcontrib --owner dherault --repo serverless-offline --sortBy contributions --showlogin true --sortOrder desc > contributors.md",
"prepare": "husky install && npm run build",
"prepare": "husky install",
"prepare-release": "standard-version && prettier --write CHANGELOG.md",
"prepublishOnly": "npm run lint && npm run build",
"prepublishOnly": "npm run lint",
"prettier-check": "prettier -c --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"",
"prettier-check:updated": "pipe-git-updated --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier -c",
"prettify": "prettier --write --ignore-path .gitignore \"**/*.{css,html,js,json,md,yaml,yml}\"",
"prettify:updated": "pipe-git-updated --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier --write",
"test": "npm run build && mocha 'tests/**/*.test.js' --exclude '**/node_modules/**/*' --exclude '**/old-unit/**/*' --require @babel/register --require ./tests/mochaGlobalSetup.js",
"test": "mocha 'tests/integration/**/*.test.js' --exclude '**/node_modules/**/*' --exclude '**/old-unit/**/*' --require ./tests/mochaGlobalSetup.cjs",
"test:jest": "npm run build && jest --verbose --silent --runInBand",
"test:cov": "npm run build && jest --coverage --silent --runInBand --collectCoverageFrom=src/**/*.js",
"test:log": "npm run build && jest --verbose",
Expand Down Expand Up @@ -218,10 +217,6 @@
"ws": "^8.7.0"
},
"devDependencies": {
"@babel/cli": "^7.17.10",
"@babel/core": "^7.18.2",
"@babel/preset-env": "^7.18.2",
"@babel/register": "^7.17.7",
"archiver": "^5.3.1",
"copyfiles": "^2.4.1",
"eslint": "^8.16.0",
Expand All @@ -240,6 +235,6 @@
"standard-version": "^9.5.0"
},
"peerDependencies": {
"serverless": "^3.0.0"
"serverless": "^3.2.0"
}
}
File renamed without changes.
10 changes: 7 additions & 3 deletions src/ServerlessOffline.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createRequire } from 'node:module'
import process, { env, exit } from 'node:process'
import chalk from 'chalk'
import { parse as semverParse } from 'semver'
import semver from 'semver'
import updateNotifier from 'update-notifier'
import debugLog from './debugLog.js'
import serverlessLog, { logWarning, setLog } from './serverlessLog.js'
Expand All @@ -11,7 +12,10 @@ import {
defaultOptions,
SERVER_SHUTDOWN_TIMEOUT,
} from './config/index.js'
import pkg from '../package.json'
// import pkg from '../package.json'

const require = createRequire(import.meta.url)
const pkg = require('../package.json')

export default class ServerlessOffline {
#cliOptions = null
Expand Down Expand Up @@ -458,7 +462,7 @@ export default class ServerlessOffline {
const currentVersion = this.#serverless.version
const requiredVersionRange = pkg.peerDependencies.serverless

if (semverParse(currentVersion).prerelease.length) {
if (semver.parse(currentVersion).prerelease.length) {
// Do not validate, if run against serverless pre-release
return
}
Expand Down
5 changes: 4 additions & 1 deletion src/events/http/Endpoint.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { existsSync, readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import OfflineEndpoint from './OfflineEndpoint.js'
import debugLog from '../../debugLog.js'

const { keys } = Object

const __dirname = dirname(fileURLToPath(import.meta.url))

function readFile(filePath) {
return readFileSync(filePath, 'utf8')
}
Expand Down
10 changes: 4 additions & 6 deletions src/events/http/lambda-events/renderVelocityTemplateObject.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Compile, parse as velocityParse } from 'velocityjs'
import velocityjs from 'velocityjs'
import runInPollutedScope from '../javaHelpers.js'
import debugLog from '../../../debugLog.js'
import { isPlainObject } from '../../../utils/index.js'
Expand All @@ -25,11 +25,9 @@ function renderVelocityString(velocityString, context, v3Utils) {
// Quick args explanation:
// { escape: false } --> otherwise would escape &, < and > chars with html (&amp;, &lt; and &gt;)
// render(context, null, true) --> null: no custom macros; true: silent mode, just like APIG
new Compile(velocityParse(velocityString), { escape: false }).render(
context,
null,
true,
),
new velocityjs.Compile(velocityjs.parse(velocityString), {
escape: false,
}).render(context, null, true),
)

if (log) {
Expand Down
8 changes: 3 additions & 5 deletions src/events/http/payloadSchemaValidator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'
import { validate } from 'jsonschema'

const { validate: validateJsonSchema } = require('jsonschema')

exports.validate = function validate(model, body) {
const result = validateJsonSchema(body, model)
export default function payloadSchemaValidator(model, body) {
const result = validate(body, model)

if (result.errors.length > 0) {
throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions src/events/websocket/WebSocketClients.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OPEN } from 'ws'
import WebSocket from 'ws'
import { isBoom } from '@hapi/boom'
import {
WebSocketConnectEvent,
Expand Down Expand Up @@ -272,7 +272,7 @@ export default class WebSocketClients {
}

const sendError = (err) => {
if (websocketClient.readyState === OPEN) {
if (websocketClient.readyState === WebSocket.OPEN) {
websocketClient.send(
stringify({
connectionId,
Expand Down
4 changes: 2 additions & 2 deletions src/events/websocket/WebSocketServer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Server } from 'ws'
import WebSocket from 'ws'
import debugLog from '../../debugLog.js'
import serverlessLog from '../../serverlessLog.js'
import { createUniqueId } from '../../utils/index.js'
Expand All @@ -19,7 +19,7 @@ export default class WebSocketServer {
this.v3Utils = v3Utils
}

const server = new Server({
const server = new WebSocket.WebSocketServer({
server: sharedServer,
verifyClient: ({ req }, cb) => {
const connectionId = createUniqueId()
Expand Down
14 changes: 13 additions & 1 deletion src/lambda/handler-runner/in-process-runner/InProcessRunner.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { readdirSync } from 'node:fs'
import { createRequire } from 'node:module'
import { dirname, resolve } from 'node:path'
import { performance } from 'node:perf_hooks'
import process from 'node:process'

const { assign, keys } = Object

const require = createRequire(import.meta.url)

function clearModule(fP, opts) {
const options = opts ?? {}
let filePath = fP
Expand Down Expand Up @@ -110,7 +113,16 @@ export default class InProcessRunner {
if (!this.#allowCache) {
clearModule(this.#handlerPath, { cleanup: true })
}
const { [this.#handlerName]: handler } = await import(this.#handlerPath)

let handler

try {
// const { [this.#handlerName]: handler } = await import(this.#handlerPath)
// eslint-disable-next-line import/no-dynamic-require
;({ [this.#handlerName]: handler } = require(this.#handlerPath))
} catch (err) {
console.log(err)
}

if (typeof handler !== 'function') {
throw new Error(
Expand Down
13 changes: 0 additions & 13 deletions src/main.js

This file was deleted.

8 changes: 4 additions & 4 deletions src/utils/satisfiesVersionRange.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { satisfies, valid, validRange } from 'semver'
import semver from 'semver'

export default function satisfiesVersionRange(version, range) {
if (valid(version) == null) {
if (semver.valid(version) == null) {
throw new Error(`Not a valid semver version: ${version}`)
}

if (validRange(range) == null) {
if (semver.validRange(range) == null) {
throw new Error(`Not a valid semver range: ${range}`)
}

return satisfies(version, range)
return semver.satisfies(version, range)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import {
joinUrl,
setup,
teardown,
} from '../../integration/_testHelpers/index.js'

const __dirname = dirname(fileURLToPath(import.meta.url))

describe('environment variables', function desc() {
this.timeout(30000)

Expand All @@ -23,7 +26,7 @@ describe('environment variables', function desc() {
env.ENV_VAR_MAPPED_FROM_ANOTHER = ENV_VAR_MAPPED

await setup({
servicePath: resolve(__dirname),
servicePath: resolve(__dirname, 'src'),
})

const url = joinUrl(env.TEST_BASE_URL, '/dev/hello')
Expand Down
3 changes: 3 additions & 0 deletions tests/endToEnd/environmentVariables/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
service: uncategorized-tests

plugins:
- ../../../
- ../../../../

provider:
environment:
Expand Down
9 changes: 6 additions & 3 deletions tests/endToEnd/optionParameters/optionParameters.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import {
joinUrl,
setup,
teardown,
} from '../../integration/_testHelpers/index.js'

const __dirname = dirname(fileURLToPath(import.meta.url))

describe('noPrependStageInUrl option', function desc() {
this.timeout(30000)

beforeEach(() =>
setup({
servicePath: resolve(__dirname),
servicePath: resolve(__dirname, 'src'),
args: ['--noPrependStageInUrl'],
}),
)
Expand Down Expand Up @@ -47,7 +50,7 @@ describe('prefix option', function desc() {
beforeEach(() =>
setup({
args: ['--prefix', 'someprefix'],
servicePath: resolve(__dirname),
servicePath: resolve(__dirname, 'src'),
}),
)

Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions tests/endToEnd/optionParameters/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
service: uncategorized-tests

plugins:
- ../../../
- ../../../../

provider:
memorySize: 128
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions tests/endToEnd/starRoutes/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
service: uncategorized-tests

plugins:
- ../../../
- ../../../../

provider:
memorySize: 128
Expand Down
7 changes: 5 additions & 2 deletions tests/endToEnd/starRoutes/starRoutes.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import {
joinUrl,
setup,
teardown,
} from '../../integration/_testHelpers/index.js'

const __dirname = dirname(fileURLToPath(import.meta.url))

describe('star routes', function desc() {
this.timeout(30000)

beforeEach(() =>
setup({
servicePath: resolve(__dirname),
servicePath: resolve(__dirname, 'src'),
}),
)

Expand Down
3 changes: 3 additions & 0 deletions tests/endToEnd/starRoutesWithProperties/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
service: uncategorized-tests

plugins:
- ../../../
- ../../../../

provider:
memorySize: 128
Expand Down
7 changes: 5 additions & 2 deletions tests/endToEnd/starRoutesWithProperties/starRoutes.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import assert from 'node:assert'
import { resolve } from 'node:path'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import fetch from 'node-fetch'
import {
joinUrl,
setup,
teardown,
} from '../../integration/_testHelpers/index.js'

const __dirname = dirname(fileURLToPath(import.meta.url))

describe('star routes with properties', function desc() {
this.timeout(30000)

beforeEach(() =>
setup({
servicePath: resolve(__dirname),
servicePath: resolve(__dirname, 'src'),
}),
)

Expand Down
File renamed without changes.
Loading

0 comments on commit 1f4d836

Please sign in to comment.