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

feat(repeater): refrain from utilizing non standard ports #197

Merged
merged 18 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 8 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
ostridm marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@
"tslib": "~2.3.1",
"tsyringe": "^4.6.0",
"tty-table": "^4.1.5",
"uuid": "^8.3.2",
"ws": "^8.17.1"
"uuid": "^8.3.2"
},
"devDependencies": {
"@commitlint/cli": "^17.0.3",
Expand All @@ -119,7 +118,7 @@
"@types/request-promise": "^4.1.48",
"@types/semver": "^7.3.9",
"@types/uuid": "^8.3.4",
"@types/ws": "^8.5.3",
"@types/ws": "^8.5.10",
ostridm marked this conversation as resolved.
Show resolved Hide resolved
"@typescript-eslint/eslint-plugin": "5.33.1",
"@typescript-eslint/parser": "5.33.1",
"eslint": "8.15.0",
Expand Down
31 changes: 29 additions & 2 deletions packages/repeater/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ export interface RequestRunnerOptions {
}
```

The `RepeaterFactory` also provides a method to create a `Repeater` instance using an existing repeater ID. You can use the `createRepeaterFromExisting` method to accomplish this:

```ts
const existingRepeaterId = '<your repater ID>';
const repeater = await repeaterFactory.createRepeaterFromExisting(
existingRepeaterId
);
```

This method retrieves the existing repeater's details from the cloud using its ID and returns a `Repeater` instance associated with the specified ID.

You can also customize the request runner options for the existing repeater by passing them as options:

```ts
const existingRepeaterId = '<your repater ID>';
const repeater = await repeaterFactory.createRepeaterFromExisting(
existingRepeaterId,
{
requestRunnerOptions: {
timeout: 10000,
maxContentLength: 200,
allowedMimes: ['text/html']
}
}
);
```

The `Repeater` instance provides the `start` method. This method is required to establish a connection with the Bright cloud engine and interact with other services.

```ts
Expand Down Expand Up @@ -142,8 +169,8 @@ describe('Scan', () => {

### Implementation details

Under the hood `Repeater` connects to the Bright engine using web socket protocol, then listens for incoming commands from the engine.
Which in turn get executed with the `RequestRunner` to proceed with the request coming from the engine:
Under the hood, `Repeater` connects to the Bright engine using the WebSocket protocol and then listens for incoming commands from the engine.
These commands are executed by the `RequestRunner` to process the requests coming from the engine:

```ts
export interface RequestRunner {
Expand Down
32 changes: 31 additions & 1 deletion packages/repeater/src/api/DefaultRepeatersManager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'reflect-metadata';
import { CreateRepeaterRequest, DeleteRepeaterRequest } from './commands';
import {
CreateRepeaterRequest,
DeleteRepeaterRequest,
GetRepeaterRequest
} from './commands';
import { DefaultRepeatersManager } from './DefaultRepeatersManager';
import { RepeatersManager } from './RepeatersManager';
import { CommandDispatcher } from '@sectester/core';
Expand Down Expand Up @@ -63,6 +67,32 @@ describe('DefaultRepeatersManager', () => {
});
});

describe('getRepeater', () => {
it('should return repeater', async () => {
const repeaterId = '142';
when(
mockedCommandDispatcher.execute(anyOfClass(GetRepeaterRequest))
).thenResolve({ id: repeaterId });

const result = await manager.getRepeater(repeaterId);

verify(
mockedCommandDispatcher.execute(anyOfClass(GetRepeaterRequest))
).once();
expect(result).toMatchObject({ repeaterId });
});

it('should throw an error if cannot find repeater', async () => {
when(
mockedCommandDispatcher.execute(anyOfClass(GetRepeaterRequest))
).thenResolve();

const act = manager.getRepeater('123');

await expect(act).rejects.toThrow('Cannot find repeater');
});
});

describe('deleteRepeater', () => {
it('should remove repeater', async () => {
when(
Expand Down
21 changes: 19 additions & 2 deletions packages/repeater/src/api/DefaultRepeatersManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { RepeatersManager } from './RepeatersManager';
import { CreateRepeaterRequest, DeleteRepeaterRequest } from './commands';
import {
CreateRepeaterRequest,
DeleteRepeaterRequest,
GetRepeaterRequest
} from './commands';
import { inject, injectable } from 'tsyringe';
import { CommandDispatcher } from '@sectester/core';

Expand All @@ -10,6 +14,20 @@ export class DefaultRepeatersManager implements RepeatersManager {
private readonly commandDispatcher: CommandDispatcher
) {}

public async getRepeater(
repeaterId: string
): Promise<{ repeaterId: string }> {
const repeater = await this.commandDispatcher.execute(
new GetRepeaterRequest(repeaterId)
);

if (!repeater?.id) {
throw new Error('Cannot find repeater');
}

return { repeaterId: repeater.id };
}

public async createRepeater({
projectId,
...options
Expand All @@ -24,7 +42,6 @@ export class DefaultRepeatersManager implements RepeatersManager {
...(projectId ? { projectIds: [projectId] } : {})
})
);

if (!repeater?.id) {
throw new Error('Cannot create a new repeater.');
}
Expand Down
2 changes: 2 additions & 0 deletions packages/repeater/src/api/RepeatersManager.ts
ostridm marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface RepeatersManager {
getRepeater(repeaterId: string): Promise<{ repeaterId: string }>;

createRepeater(options: {
name: string;
projectId?: string;
Expand Down
20 changes: 20 additions & 0 deletions packages/repeater/src/api/commands/GetRepeaterRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { HttpRequest } from '@sectester/bus';

export interface GetRepeaterResponsePayload {
id: string;
name: string;
projectIds: string[];
}

export class GetRepeaterRequest extends HttpRequest<
undefined,
GetRepeaterResponsePayload
> {
constructor(repeaterId: string) {
super({
url: `/api/v1/repeaters/${repeaterId}`,
method: 'GET',
payload: undefined
});
}
}
1 change: 1 addition & 0 deletions packages/repeater/src/api/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { CreateRepeaterRequest } from './CreateRepeaterRequest';
export { DeleteRepeaterRequest } from './DeleteRepeaterRequest';
export { GetRepeaterRequest } from './GetRepeaterRequest';
Loading
Loading