diff --git a/README.md b/README.md index 093da96d0..10cfe1a03 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,10 @@ aws lambda invoke /dev/null \ --function-name myServiceName-dev-invokedHandler ``` +## The `process.env.IS_OFFLINE` variable + +Will be `"true"` in your handlers and thourough the plugin. + ## Token authorizers As defined in the [Serverless Documentation](https://serverless.com/framework/docs/providers/aws/events/apigateway/#setting-api-keys-for-your-rest-api) you can use API Keys as a simple authentication method. diff --git a/examples/events/http/handler.js b/examples/events/http/handler.js index 32b6c1ee3..1e0a5ef07 100644 --- a/examples/events/http/handler.js +++ b/examples/events/http/handler.js @@ -1,10 +1,8 @@ 'use strict' -const { stringify } = JSON - exports.hello = async function hello() { return { - body: stringify({ foo: 'bar' }), + body: JSON.stringify({ foo: 'bar', IS_OFFLINE: process.env.IS_OFFLINE }), statusCode: 200, } } diff --git a/package.json b/package.json index c08dc4705..347f82e1e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "dedicatedTo": "Blue, a great migrating bird.", "name": "serverless-offline", - "version": "6.2.0", + "version": "6.3.0", "description": "Emulate AWS λ and API Gateway locally when developing your Serverless project", "license": "MIT", "main": "dist/main.js", diff --git a/src/ServerlessOffline.js b/src/ServerlessOffline.js index 5f7ad09b4..9c9611414 100644 --- a/src/ServerlessOffline.js +++ b/src/ServerlessOffline.js @@ -58,6 +58,9 @@ 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 + // check if update is available updateNotifier({ pkg }).notify() diff --git a/src/lambda/handler-runner/in-process-runner/InProcessRunner.js b/src/lambda/handler-runner/in-process-runner/InProcessRunner.js index cb2d57cd8..6ec07b76e 100644 --- a/src/lambda/handler-runner/in-process-runner/InProcessRunner.js +++ b/src/lambda/handler-runner/in-process-runner/InProcessRunner.js @@ -1,7 +1,5 @@ import { performance } from 'perf_hooks' -const { assign } = Object - export default class InProcessRunner { #env = null #functionKey = null @@ -35,7 +33,7 @@ export default class InProcessRunner { // NOTE: Don't use Object spread (...) here! // otherwise the values of the attached props are not coerced to a string // e.g. process.env.foo = 1 should be coerced to '1' (string) - assign(process.env, this.#env) + Object.assign(process.env, this.#env) // lazy load handler with first usage