Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AIK-2404: Make getOptions a helper function #25

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 1 addition & 27 deletions library/src/agent/protect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, { range: string; wrapper: Wrapper }> = {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -116,16 +100,6 @@ function getAgent({
return agent;
}

function getOptions(partialOptions?: Partial<Options>): Options {
const options = { ...defaultOptions, ...partialOptions };

if (dryModeEnabled()) {
options.block = false;
}

return options;
}

function disableShimmerLogging() {
shimmer({ logger: () => {} });
}
Expand Down
30 changes: 30 additions & 0 deletions library/src/helpers/getOptions.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
40 changes: 40 additions & 0 deletions library/src/helpers/getOptions.ts
Original file line number Diff line number Diff line change
@@ -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>): 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"
);
}
Loading