diff --git a/library/src/agent/protect.ts b/library/src/agent/protect.ts index a46f161d0..93f9fb36b 100644 --- a/library/src/agent/protect.ts +++ b/library/src/agent/protect.ts @@ -10,6 +10,7 @@ import { MongoDB } from "../sinks/MongoDB"; import * as shimmer from "shimmer"; import { Logger, LoggerConsole, LoggerNoop } from "./Logger"; import { Wrapper } from "./Wrapper"; +import { Options, getOptions } from "../helpers/getOptions"; function wrapInstalledPackages() { const packages: Record = { @@ -40,16 +41,6 @@ function wrapInstalledPackages() { return wrapped; } -type Options = { - debug: boolean; - block: boolean; -}; - -const defaultOptions: Options = { - debug: false, - block: true, -}; - function getLogger(options: Options): Logger { if (options.debug) { return new LoggerConsole(); @@ -81,13 +72,6 @@ function getTokenFromEnv(): Token | undefined { : undefined; } -function dryModeEnabled(): boolean { - return ( - process.env.AIKIDO_NO_BLOCKING === "true" || - process.env.AIKIDO_NO_BLOCKING === "1" - ); -} - function getAgent({ options, serverless, @@ -116,16 +100,6 @@ function getAgent({ return agent; } -function getOptions(partialOptions?: Partial): Options { - const options = { ...defaultOptions, ...partialOptions }; - - if (dryModeEnabled()) { - options.block = false; - } - - return options; -} - function disableShimmerLogging() { shimmer({ logger: () => {} }); } diff --git a/library/src/helpers/getOptions.test.ts b/library/src/helpers/getOptions.test.ts new file mode 100644 index 000000000..17934971a --- /dev/null +++ b/library/src/helpers/getOptions.test.ts @@ -0,0 +1,30 @@ +import * as t from "tap"; +import { getOptions } from "./getOptions"; + +t.test("getOptions(), should return default options", async () => { + // Create testing environment + process.env.AIKIDO_NO_BLOCKING = "false"; + + t.same(getOptions(), { + debug: false, + block: true, + }); +}); + +t.test("getOptions(), should check for dryMode", async () => { + // Create testing environment + process.env.AIKIDO_NO_BLOCKING = "true"; + + t.same(getOptions(), { + debug: false, + block: false, + }); +}); + +t.test("getOptions(), should allow you to set other options", async () => { + // Create testing environment + process.env.AIKIDO_NO_BLOCKING = "false"; + + t.notOk(getOptions({ debug: false }).debug); + t.ok(getOptions({ debug: true }).debug); +}); diff --git a/library/src/helpers/getOptions.ts b/library/src/helpers/getOptions.ts new file mode 100644 index 000000000..4f25c3089 --- /dev/null +++ b/library/src/helpers/getOptions.ts @@ -0,0 +1,40 @@ +/** + * This module contains the default options and the function {@link getOptions} + * @module helpers/getOptions + */ + +/** + * It gets the options for an Agent + * @param partialOptions Your own values which will overwrite the default options + * @returns Options which you can then pass onto the Agent + */ +export function getOptions(partialOptions?: Partial): Options { + const options = { ...defaultOptions, ...partialOptions }; + + if (dryModeEnabled()) { + options.block = false; + } + + return options; +} + +export type Options = { + debug: boolean; + block: boolean; +}; + +const defaultOptions: Options = { + debug: false, + block: true, +}; + +/** + * This function checks the "AIKIDO_NO_BLOCKING" environment variable, when this is set to true or 1, + * the function returns true, otherwise the function returns false. + */ +function dryModeEnabled(): boolean { + return ( + process.env.AIKIDO_NO_BLOCKING === "true" || + process.env.AIKIDO_NO_BLOCKING === "1" + ); +}