Skip to content

Commit

Permalink
refactor: import process explicit (#1418)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed May 16, 2022
1 parent 90d5169 commit 8893c67
Show file tree
Hide file tree
Showing 55 changed files with 159 additions and 109 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Expand Up @@ -36,6 +36,10 @@ module.exports = {
name: 'Buffer',
message: "Import 'Buffer' from 'buffer' module instead",
},
{
name: 'process',
message: "Import 'process' from 'process' module instead",
},
],
// we use underscores to indicate private fields in classes
'no-underscore-dangle': 'off',
Expand Down
4 changes: 3 additions & 1 deletion examples/events/http/handler.js
@@ -1,10 +1,12 @@
'use strict'

const { env } = require('process')

const { stringify } = JSON

exports.hello = async function hello() {
return {
body: stringify({ foo: 'bar', IS_OFFLINE: process.env.IS_OFFLINE }),
body: stringify({ foo: 'bar', IS_OFFLINE: env.IS_OFFLINE }),
statusCode: 200,
}
}
8 changes: 4 additions & 4 deletions jest.config.js
@@ -1,15 +1,15 @@
'use strict'

const { AWS_ENDPOINT } = process.env
const { env } = require('process')

module.exports = {
bail: true,
globals: {
RUN_TEST_AGAINST_AWS: AWS_ENDPOINT != null,
TEST_BASE_URL: AWS_ENDPOINT || 'http://localhost:3000',
RUN_TEST_AGAINST_AWS: env.AWS_ENDPOINT != null,
TEST_BASE_URL: env.AWS_ENDPOINT || 'http://localhost:3000',
},
modulePathIgnorePatterns: ['src/lambda/__tests__/fixtures/'],
...(!process.env.SKIP_SETUP && {
...(!env.SKIP_SETUP && {
globalSetup: './tests/_setupTeardown/npmInstall.js',
}),
}
11 changes: 6 additions & 5 deletions src/ServerlessOffline.js
@@ -1,3 +1,4 @@
import process, { env, exit } from 'process'
import updateNotifier from 'update-notifier'
import chalk from 'chalk'
import { parse as semverParse } from 'semver'
Expand Down Expand Up @@ -65,7 +66,7 @@ export default class ServerlessOffline {
}

_printBlankLine() {
if (process.env.NODE_ENV !== 'test') {
if (env.NODE_ENV !== 'test') {
if (this.log) {
this.log.notice()
} else {
Expand All @@ -77,7 +78,7 @@ export default class ServerlessOffline {
// Entry point for the plugin (sls offline) when running 'sls offline start'
async start() {
// Put here so available everywhere, not just in handlers
process.env.IS_OFFLINE = true
env.IS_OFFLINE = true

// check if update is available
updateNotifier({ pkg }).notify()
Expand Down Expand Up @@ -111,14 +112,14 @@ export default class ServerlessOffline {
}

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

async end(skipExit) {
// TEMP FIXME
if (process.env.NODE_ENV === 'test' && skipExit === undefined) {
if (env.NODE_ENV === 'test' && skipExit === undefined) {
return
}

Expand Down Expand Up @@ -150,7 +151,7 @@ export default class ServerlessOffline {
await Promise.all(eventModules)

if (!skipExit) {
process.exit(0)
exit(0)
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/debugLog.js
@@ -1,3 +1,5 @@
export default typeof process.env.SLS_DEBUG !== 'undefined'
import { env } from 'process'

export default typeof env.SLS_DEBUG !== 'undefined'
? console.log.bind(null, '[offline]')
: () => null
7 changes: 4 additions & 3 deletions src/events/http/HttpServer.js
@@ -1,6 +1,7 @@
import { Buffer } from 'buffer'
import { readFileSync } from 'fs'
import { join, resolve } from 'path'
import process, { env, exit } from 'process'
import h2o2 from '@hapi/h2o2'
import { Server } from '@hapi/hapi'
import { createRequire } from 'module'
Expand Down Expand Up @@ -200,7 +201,7 @@ export default class HttpServer {
err,
)
}
process.exit(1)
exit(1)
}

// TODO move the following block
Expand All @@ -220,7 +221,7 @@ export default class HttpServer {
serverlessLog('Enter "rp" to replay the last request')
}

if (process.env.NODE_ENV !== 'test') {
if (env.NODE_ENV !== 'test') {
process.openStdin().addListener('data', (data) => {
// note: data is an object, and when converted to a string it will
// end with a linefeed. so we (rather crudely) account for that
Expand Down Expand Up @@ -257,7 +258,7 @@ export default class HttpServer {
// }

_printBlankLine() {
if (process.env.NODE_ENV !== 'test') {
if (env.NODE_ENV !== 'test') {
if (this.log) {
this.log.notice()
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/events/http/lambda-events/LambdaIntegrationEvent.js
@@ -1,3 +1,4 @@
import { env } from 'process'
import renderVelocityTemplateObject from './renderVelocityTemplateObject.js'
import VelocityContext from './VelocityContext.js'

Expand All @@ -18,9 +19,9 @@ export default class LambdaIntegrationEvent {
}

create() {
if (process.env.AUTHORIZER) {
if (env.AUTHORIZER) {
try {
const authorizerContext = parse(process.env.AUTHORIZER)
const authorizerContext = parse(env.AUTHORIZER)
if (authorizerContext) {
this.#request.auth = {
...this.#request.auth,
Expand Down
27 changes: 14 additions & 13 deletions src/events/http/lambda-events/LambdaProxyIntegrationEvent.js
@@ -1,4 +1,5 @@
import { Buffer } from 'buffer'
import { env } from 'process'
import { decode } from 'jsonwebtoken'
import {
createUniqueId,
Expand Down Expand Up @@ -62,17 +63,17 @@ export default class LambdaProxyIntegrationEvent {

let authAuthorizer

if (process.env.AUTHORIZER) {
if (env.AUTHORIZER) {
try {
authAuthorizer = parse(process.env.AUTHORIZER)
authAuthorizer = parse(env.AUTHORIZER)
} catch (error) {
if (this.log) {
this.log.error(
'Could not parse process.env.AUTHORIZER, make sure it is correct JSON',
'Could not parse env.AUTHORIZER, make sure it is correct JSON',
)
} else {
console.error(
'Serverless-offline: Could not parse process.env.AUTHORIZER, make sure it is correct JSON.',
'Serverless-offline: Could not parse env.AUTHORIZER, make sure it is correct JSON.',
)
}
}
Expand Down Expand Up @@ -195,7 +196,7 @@ export default class LambdaProxyIntegrationEvent {
// 'principalId' should have higher priority
principalId:
authPrincipalId ||
process.env.PRINCIPAL_ID ||
env.PRINCIPAL_ID ||
'offlineContext_authorizer_principalId', // See #24
}),
domainName: 'offlineContext_domainName',
Expand All @@ -204,23 +205,23 @@ export default class LambdaProxyIntegrationEvent {
httpMethod,
identity: {
accessKey: null,
accountId: process.env.SLS_ACCOUNT_ID || 'offlineContext_accountId',
apiKey: process.env.SLS_API_KEY || 'offlineContext_apiKey',
apiKeyId: process.env.SLS_API_KEY_ID || 'offlineContext_apiKeyId',
caller: process.env.SLS_CALLER || 'offlineContext_caller',
accountId: env.SLS_ACCOUNT_ID || 'offlineContext_accountId',
apiKey: env.SLS_API_KEY || 'offlineContext_apiKey',
apiKeyId: env.SLS_API_KEY_ID || 'offlineContext_apiKeyId',
caller: env.SLS_CALLER || 'offlineContext_caller',
cognitoAuthenticationProvider:
_headers['cognito-authentication-provider'] ||
process.env.SLS_COGNITO_AUTHENTICATION_PROVIDER ||
env.SLS_COGNITO_AUTHENTICATION_PROVIDER ||
'offlineContext_cognitoAuthenticationProvider',
cognitoAuthenticationType:
process.env.SLS_COGNITO_AUTHENTICATION_TYPE ||
env.SLS_COGNITO_AUTHENTICATION_TYPE ||
'offlineContext_cognitoAuthenticationType',
cognitoIdentityId:
_headers['cognito-identity-id'] ||
process.env.SLS_COGNITO_IDENTITY_ID ||
env.SLS_COGNITO_IDENTITY_ID ||
'offlineContext_cognitoIdentityId',
cognitoIdentityPoolId:
process.env.SLS_COGNITO_IDENTITY_POOL_ID ||
env.SLS_COGNITO_IDENTITY_POOL_ID ||
'offlineContext_cognitoIdentityPoolId',
principalOrgId: null,
sourceIp: remoteAddress,
Expand Down
@@ -1,4 +1,5 @@
import { Buffer } from 'buffer'
import { env } from 'process'
import { decode } from 'jsonwebtoken'
import {
formatToClfTime,
Expand Down Expand Up @@ -49,9 +50,9 @@ export default class LambdaProxyIntegrationEventV2 {

let authAuthorizer

if (process.env.AUTHORIZER) {
if (env.AUTHORIZER) {
try {
authAuthorizer = parse(process.env.AUTHORIZER)
authAuthorizer = parse(env.AUTHORIZER)
} catch (error) {
if (this.log) {
this.log.error(
Expand Down
3 changes: 2 additions & 1 deletion src/events/http/lambda-events/VelocityContext.js
@@ -1,4 +1,5 @@
import { Buffer } from 'buffer'
import { env } from 'process'
import jsEscapeString from 'js-string-escape'
import { decode } from 'jsonwebtoken'
import {
Expand Down Expand Up @@ -74,7 +75,7 @@ export default class VelocityContext {

authorizer.principalId =
authPrincipalId ||
process.env.PRINCIPAL_ID ||
env.PRINCIPAL_ID ||
'offlineContext_authorizer_principalId' // See #24

if (token) {
Expand Down
3 changes: 2 additions & 1 deletion src/events/websocket/HttpServer.js
@@ -1,3 +1,4 @@
import { exit } from 'process'
import { Server } from '@hapi/hapi'
import { catchAllRoute, connectionsRoutes } from './http-routes/index.js'
import serverlessLog from '../../serverlessLog.js'
Expand Down Expand Up @@ -57,7 +58,7 @@ export default class HttpServer {
err,
)
}
process.exit(1)
exit(1)
}

if (this.log) {
Expand Down
3 changes: 2 additions & 1 deletion src/lambda/HttpServer.js
@@ -1,4 +1,5 @@
import { Server } from '@hapi/hapi'
import { exit } from 'process'
import { invocationsRoute, invokeAsyncRoute } from './routes/index.js'
import serverlessLog from '../serverlessLog.js'
import debugLog from '../debugLog.js'
Expand Down Expand Up @@ -56,7 +57,7 @@ export default class HttpServer {
err,
)
}
process.exit(1)
exit(1)
}

if (this.log) {
Expand Down
@@ -1,3 +1,4 @@
import process, { argv } from 'process'
import InProcessRunner from '../in-process-runner/index.js'

// TODO handle this:
Expand All @@ -20,7 +21,7 @@ process.on('uncaughtException', (err) => {
})
})

const [, , functionKey, handlerName, handlerPath] = process.argv
const [, , functionKey, handlerName, handlerPath] = argv

process.on('message', async (messageData) => {
const { context, event, allowCache, timeout } = messageData
Expand Down
4 changes: 2 additions & 2 deletions src/lambda/handler-runner/go-runner/GoRunner.js
@@ -1,11 +1,11 @@
import { EOL } from 'os'
import { promises as fsPromises } from 'fs'
import { EOL } from 'os'
import { sep, resolve, parse as pathParse } from 'path'
import process, { chdir, cwd } from 'process'
import execa, { sync } from 'execa'

const { writeFile, readFile, mkdir, rmdir } = fsPromises
const { parse, stringify } = JSON
const { cwd, chdir } = process

const PAYLOAD_IDENTIFIER = 'offline_payload'

Expand Down
@@ -1,6 +1,7 @@
import { performance } from 'perf_hooks'
import { readdirSync } from 'fs'
import { dirname, resolve } from 'path'
import { performance } from 'perf_hooks'
import process from 'process'

const { assign, keys } = Object

Expand Down
1 change: 1 addition & 0 deletions src/lambda/handler-runner/java-runner/JavaRunner.js
@@ -1,4 +1,5 @@
import { EOL } from 'os'
import process from 'process'
import fetch from 'node-fetch'
import { invokeJavaLocal } from 'java-invoke-local'

Expand Down
4 changes: 2 additions & 2 deletions src/lambda/handler-runner/python-runner/PythonRunner.js
@@ -1,10 +1,10 @@
import { spawn } from 'child_process'
import { EOL, platform } from 'os'
import { delimiter, join, relative, resolve } from 'path'
import { spawn } from 'child_process'
import process, { cwd } from 'process'
import readline from 'readline'

const { parse, stringify } = JSON
const { cwd } = process
const { assign } = Object
const { has } = Reflect

Expand Down
2 changes: 1 addition & 1 deletion src/lambda/handler-runner/ruby-runner/RubyRunner.js
@@ -1,9 +1,9 @@
import { EOL, platform } from 'os'
import { relative, resolve } from 'path'
import { cwd } from 'process'
import execa from 'execa'

const { parse, stringify } = JSON
const { cwd } = process
const { has } = Reflect

export default class RubyRunner {
Expand Down
@@ -1,3 +1,4 @@
import { env } from 'process'
import { parentPort, workerData } from 'worker_threads' // eslint-disable-line import/no-unresolved
import InProcessRunner from '../in-process-runner/index.js'

Expand All @@ -13,7 +14,7 @@ parentPort.on('message', async (messageData) => {
handlerPath,
handlerName,
handlerModuleNesting,
process.env,
env,
timeout,
allowCache,
)
Expand Down

0 comments on commit 8893c67

Please sign in to comment.