Skip to content

Commit

Permalink
refactor: use desm (#1641)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk authored Jan 4, 2023
1 parent f9f1f79 commit 572097e
Show file tree
Hide file tree
Showing 82 changed files with 252 additions and 472 deletions.
52 changes: 32 additions & 20 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"array-unflat-js": "^0.1.3",
"boxen": "^7.0.1",
"chalk": "^5.2.0",
"desm": "^1.3.0",
"execa": "^6.1.0",
"fs-extra": "^11.1.0",
"is-wsl": "^2.2.0",
Expand All @@ -109,12 +110,12 @@
"archiver": "^5.3.1",
"eslint": "^8.31.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-unicorn": "^45.0.2",
"git-list-updated": "^1.2.1",
"husky": "^8.0.2",
"husky": "^8.0.3",
"lint-staged": "^13.1.0",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
Expand Down
9 changes: 3 additions & 6 deletions src/events/http/Endpoint.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { existsSync, readFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { log } from '@serverless/utils/log.js'
import { join } from 'desm'
import OfflineEndpoint from './OfflineEndpoint.js'

const { entries } = Object

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

// velocity template defaults
const defaultRequestTemplate = readFileSync(
resolve(__dirname, 'templates/offline-default.req.vm'),
join(import.meta.url, 'templates/offline-default.req.vm'),
'utf8',
)
const defaultResponseTemplate = readFileSync(
resolve(__dirname, 'templates/offline-default.res.vm'),
join(import.meta.url, 'templates/offline-default.res.vm'),
'utf8',
)

Expand Down
7 changes: 2 additions & 5 deletions src/lambda/__tests__/LambdaFunction.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
// import { performance } from 'node:perf_hooks'
import { fileURLToPath } from 'node:url'
import { join } from 'desm'
import LambdaFunction from '../LambdaFunction.js'
import { DEFAULT_LAMBDA_TIMEOUT } from '../../config/index.js'

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

describe('LambdaFunction', () => {
const functionName = 'foo'

const serverless = {
config: {
serverlessPath: '',
servicePath: resolve(__dirname),
servicePath: join(import.meta.url),
},
service: {
provider: {
Expand Down
10 changes: 4 additions & 6 deletions src/lambda/handler-runner/python-runner/PythonRunner.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { spawn } from 'node:child_process'
import { EOL, platform } from 'node:os'
import { delimiter, dirname, join, relative, resolve } from 'node:path'
import { delimiter, join as pathJoin, relative } from 'node:path'
import process, { cwd, nextTick } from 'node:process'
import { createInterface } from 'node:readline'
import { fileURLToPath } from 'node:url'
import { log } from '@serverless/utils/log.js'
import { join } from 'desm'
import { splitHandlerPathAndName } from '../../../utils/index.js'

const { parse, stringify } = JSON
const { assign, hasOwn } = Object

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

export default class PythonRunner {
static #payloadIdentifier = '__offline_payload__'

Expand All @@ -32,7 +30,7 @@ export default class PythonRunner {
const runtimeDir = platform() === 'win32' ? 'Scripts' : 'bin'

process.env.PATH = [
join(process.env.VIRTUAL_ENV, runtimeDir),
pathJoin(process.env.VIRTUAL_ENV, runtimeDir),
delimiter,
process.env.PATH,
].join('')
Expand All @@ -44,7 +42,7 @@ export default class PythonRunner {
pythonExecutable,
[
'-u',
resolve(__dirname, 'invoke.py'),
join(import.meta.url, 'invoke.py'),
relative(cwd(), handlerPath),
handlerName,
],
Expand Down
8 changes: 3 additions & 5 deletions src/lambda/handler-runner/ruby-runner/RubyRunner.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { EOL, platform } from 'node:os'
import { dirname, relative, resolve } from 'node:path'
import { relative } from 'node:path'
import { cwd } from 'node:process'
import { fileURLToPath } from 'node:url'
import { log } from '@serverless/utils/log.js'
import { join } from 'desm'
import { execa } from 'execa'
import { splitHandlerPathAndName } from '../../../utils/index.js'

const { parse, stringify } = JSON
const { hasOwn } = Object

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

export default class RubyRunner {
static #payloadIdentifier = '__offline_payload__'

Expand Down Expand Up @@ -86,7 +84,7 @@ export default class RubyRunner {
const { stderr, stdout } = await execa(
runtime,
[
resolve(__dirname, 'invoke.rb'),
join(import.meta.url, 'invoke.rb'),
relative(cwd(), this.#handlerPath),
this.#handlerName,
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { MessageChannel, Worker } from 'node:worker_threads'

const __dirname = dirname(fileURLToPath(import.meta.url))
const workerThreadHelperPath = resolve(__dirname, 'workerThreadHelper.js')
import { join } from 'desm'

export default class WorkerThreadRunner {
#workerThread = null

constructor(funOptions, env) {
const { codeDir, functionKey, handler, servicePath, timeout } = funOptions

this.#workerThread = new Worker(workerThreadHelperPath, {
// don't pass process.env from the main process!
env,
workerData: {
codeDir,
functionKey,
handler,
servicePath,
timeout,
this.#workerThread = new Worker(
join(import.meta.url, 'workerThreadHelper.js'),
{
// don't pass process.env from the main process!
env,
workerData: {
codeDir,
functionKey,
handler,
servicePath,
timeout,
},
},
})
)
}

// () => Promise<number>
Expand Down
9 changes: 3 additions & 6 deletions tests/_testHelpers/setupTeardown.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { dirname, resolve } from 'node:path'
import process, { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import { join } from 'desm'
import { execaNode } from 'execa'

let serverlessProcess

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

const serverlessPath = resolve(
__dirname,
const serverlessPath = join(
import.meta.url,
'../../node_modules/serverless/bin/serverless',
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { env } from 'node:process'
import { fileURLToPath } from 'node:url'
import { join } from 'desm'
import { BASE_URL } from '../../config.js'
import { setup, teardown } from '../../_testHelpers/index.js'

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

describe('environment variables', function desc() {
const ENV_VAR_QUOTED = 'I am ENV_VAR_1'
const ENV_VAR_UNQUOTED = 'I am ENV_VAR_2'
Expand All @@ -20,7 +17,7 @@ describe('environment variables', function desc() {
env.ENV_VAR_MAPPED_FROM_ANOTHER = ENV_VAR_MAPPED

await setup({
servicePath: resolve(__dirname, 'src'),
servicePath: join(import.meta.url, 'src'),
})

const url = new URL('/dev/hello', BASE_URL)
Expand Down
11 changes: 4 additions & 7 deletions tests/end-to-end/optionParameters/optionParameters.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { join } from 'desm'
import { BASE_URL } from '../../config.js'
import { setup, teardown } from '../../_testHelpers/index.js'

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

describe('noPrependStageInUrl option', function desc() {
beforeEach(() =>
setup({
args: ['--noPrependStageInUrl'],
servicePath: resolve(__dirname, 'src'),
servicePath: join(import.meta.url, 'src'),
}),
)

Expand Down Expand Up @@ -61,7 +58,7 @@ describe('prefix option', function desc() {
beforeEach(() =>
setup({
args: ['--prefix', 'someprefix'],
servicePath: resolve(__dirname, 'src'),
servicePath: join(import.meta.url, 'src'),
}),
)

Expand Down Expand Up @@ -114,7 +111,7 @@ describe('noPrependStageInUrl option with prefix option', function desc() {
beforeEach(() =>
setup({
args: ['--noPrependStageInUrl', '--prefix', 'someprefix'],
servicePath: resolve(__dirname, 'src'),
servicePath: join(import.meta.url, 'src'),
}),
)

Expand Down
7 changes: 2 additions & 5 deletions tests/end-to-end/starRoutes/starRoutes.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import assert from 'node:assert'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { join } from 'desm'
import { BASE_URL } from '../../config.js'
import { setup, teardown } from '../../_testHelpers/index.js'

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

describe('star routes', function desc() {
beforeEach(() =>
setup({
servicePath: resolve(__dirname, 'src'),
servicePath: join(import.meta.url, 'src'),
}),
)

Expand Down
Loading

0 comments on commit 572097e

Please sign in to comment.