Skip to content

Commit

Permalink
Merge pull request #50 from AikidoSec/patch-max-one-class
Browse files Browse the repository at this point in the history
Max 1 class per file
  • Loading branch information
willem-delbare authored Feb 27, 2024
2 parents 9d2b163 + ce8f756 commit a551b75
Show file tree
Hide file tree
Showing 33 changed files with 518 additions and 450 deletions.
1 change: 1 addition & 0 deletions library/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
"max-lines-per-function": ["error", { max: 50, skipBlankLines: true }],
"func-names": ["error", "as-needed"],
camelcase: "error",
"max-classes-per-file": ["error", 1],
},
overrides: [
{
Expand Down
203 changes: 0 additions & 203 deletions library/src/agent/API.ts

This file was deleted.

5 changes: 3 additions & 2 deletions library/src/agent/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { hostname, platform, release } from "node:os";
import * as t from "tap";
import { ip } from "../helpers/ipAddress";
import { Agent } from "./Agent";
import { APIForTesting, Token } from "./API";
import { LoggerNoop } from "./Logger";
import { APIForTesting } from "./api/APIForTesting";
import { Token } from "./api/Token";
import { LoggerNoop } from "./logger/LoggerNoop";

t.test("it sends started event", async (t) => {
const logger = new LoggerNoop();
Expand Down
6 changes: 4 additions & 2 deletions library/src/agent/Agent.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { hostname, platform, release } from "node:os";
import { ip } from "../helpers/ipAddress";
import { API, AgentInfo, Token, Stats, Kind } from "./API";
import { Logger } from "./Logger";
import { API } from "./api/API";
import { AgentInfo, Kind, Stats } from "./api/Event";
import { Token } from "./api/Token";
import { Context } from "./Context";
import { resolve } from "path";
import { Logger } from "./logger/Logger";
import { Source } from "./Source";

export class Agent {
Expand Down
145 changes: 1 addition & 144 deletions library/src/agent/Wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,147 +1,4 @@
type Interceptor = (args: unknown[], subject: unknown) => void;

export class MethodInterceptor {
constructor(
private readonly name: string,
private readonly interceptor: Interceptor
) {
if (!this.name) {
throw new Error("Method name is required");
}
}

getName() {
return this.name;
}

getInterceptor() {
return this.interceptor;
}
}

type ModifyingArgumentsInterceptor = (
args: unknown[],
subject: unknown
) => unknown[];

export class ModifyingArgumentsMethodInterceptor {
constructor(
private readonly name: string,
private readonly interceptor: ModifyingArgumentsInterceptor
) {
if (!this.name) {
throw new Error("Method name is required");
}
}

getName() {
return this.name;
}

getInterceptor() {
return this.interceptor;
}
}

class Selector {
private methods: (MethodInterceptor | ModifyingArgumentsMethodInterceptor)[] =
[];

constructor(private readonly selector: (exports: unknown) => unknown) {}

inspect(methodName: string, interceptor: Interceptor) {
const method = new MethodInterceptor(methodName, interceptor);
this.methods.push(method);

return method;
}

modifyArguments(
methodName: string,
interceptor: ModifyingArgumentsInterceptor
) {
const method = new ModifyingArgumentsMethodInterceptor(
methodName,
interceptor
);
this.methods.push(method);

return method;
}

getSelector() {
return this.selector;
}

getMethodInterceptors() {
return this.methods;
}
}

class VersionedPackage {
private selectors: Selector[] = [];

constructor(private readonly range: string) {
if (!this.range) {
throw new Error("Version range is required");
}
}

getRange() {
return this.range;
}

subject(selector: (exports: any) => unknown): Selector {
const fn = new Selector(selector);
this.selectors.push(fn);

return fn;
}

getSelectors() {
return this.selectors;
}
}

class Package {
private versions: VersionedPackage[] = [];

constructor(private readonly packageName: string) {}

getName() {
return this.packageName;
}

withVersion(range: string): VersionedPackage {
const pkg = new VersionedPackage(range);
this.versions.push(pkg);

return pkg;
}

getVersions() {
return this.versions;
}
}

export class Hooks {
private readonly packages: Package[] = [];

package(packageName: string): Package {
if (!packageName) {
throw new Error("Package name is required");
}

const pkg = new Package(packageName);
this.packages.push(pkg);

return pkg;
}

getPackages() {
return this.packages;
}
}
import { Hooks } from "./hooks/Hooks";

export interface Wrapper {
wrap(hooks: Hooks): void;
Expand Down
6 changes: 6 additions & 0 deletions library/src/agent/api/API.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Event } from "./Event";
import { Token } from "./Token";

export interface API {
report(token: Token, event: Event): Promise<void>;
}
Loading

0 comments on commit a551b75

Please sign in to comment.