Skip to content

Commit

Permalink
feat(repeater): refrain from utilizing non standard ports (#197)
Browse files Browse the repository at this point in the history
closes #196
  • Loading branch information
ostridm authored Jun 19, 2024
1 parent a8746ee commit aa12bfa
Show file tree
Hide file tree
Showing 41 changed files with 2,661 additions and 1,281 deletions.
777 changes: 676 additions & 101 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,20 @@
"chalk": "^4.1.2",
"ci-info": "^3.3.0",
"content-type": "^1.0.4",
"fast-content-type-parse": "^1.1.0",
"form-data": "^4.0.0",
"http-proxy-agent": "^7.0.2",
"https-proxy-agent": "^7.0.4",
"iconv-lite": "^0.6.3",
"reflect-metadata": "^0.1.13",
"semver": "^7.5.2",
"socket.io-client": "^4.7.5",
"socket.io-msgpack-parser": "^3.0.2",
"socks-proxy-agent": "^6.2.0-beta.0",
"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 @@ -113,7 +118,6 @@
"@types/request-promise": "^4.1.48",
"@types/semver": "^7.3.9",
"@types/uuid": "^8.3.4",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "5.33.1",
"@typescript-eslint/parser": "5.33.1",
"eslint": "8.15.0",
Expand All @@ -129,8 +133,14 @@
"nx": "14.5.6",
"prettier": "2.7.1",
"semantic-release": "~19.0.3",
"socket.io": "^4.7.5",
"ts-jest": "27.1.4",
"ts-mockito": "^2.6.1",
"typescript": "4.7.4"
},
"overrides": {
"socket.io-msgpack-parser": {
"notepack.io": "~3.0.1"
}
}
}
6 changes: 3 additions & 3 deletions packages/repeater/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ describe('Scan', () => {

### Implementation details

Under the hood `Repeater` register `ExecuteRequestEventHandler` in bus,
which in turn uses the `RequestRunner` to proceed with request:
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 All @@ -179,7 +179,7 @@ export interface RequestRunner {
}
```

Package contains `RequestRunner` implementations for both HTTP and WS protocols.
Package contains `RequestRunner` implementations for HTTP protocol only.
To support other protocol new class implementation of `RequestRunner` should be registered in global IoC container:

```ts
Expand Down
1 change: 0 additions & 1 deletion packages/repeater/src/api/DefaultRepeatersManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export class DefaultRepeatersManager implements RepeatersManager {
...(projectId ? { projectIds: [projectId] } : {})
})
);

if (!repeater?.id) {
throw new Error('Cannot create a new repeater.');
}
Expand Down
23 changes: 2 additions & 21 deletions packages/repeater/src/api/ExecuteRequestEventHandler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'reflect-metadata';
import { ExecuteRequestEventHandler } from './ExecuteRequestEventHandler';
import { Protocol } from '../models';
import { Request, RequestRunner } from '../request-runner';
import { anything, capture, instance, mock, reset, when } from 'ts-mockito';
import { RequestRunner } from '../request-runner';
import { anything, instance, mock, reset, when } from 'ts-mockito';

describe('ExecuteRequestEventHandler', () => {
const requestRunnerResponse = {
Expand Down Expand Up @@ -65,24 +65,5 @@ describe('ExecuteRequestEventHandler', () => {

await expect(res).rejects.toThrow(`Unsupported protocol "http"`);
});

it('`correlation_id_regex` should become `correlationIdRegex` in runner input', async () => {
const payload = {
protocol: Protocol.HTTP,
url: 'http://foo.bar/',
headers: {},
correlation_id_regex: 'baz'
};
const handler = new ExecuteRequestEventHandler([
instance(mockedRequestRunner)
]);

await handler.handle(payload);

const [request]: [Request] = capture<Request>(
mockedRequestRunner.run
).first();
expect(request.correlationIdRegex).toBeInstanceOf(RegExp);
});
});
});
4 changes: 1 addition & 3 deletions packages/repeater/src/api/ExecuteRequestEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export class ExecuteRequestEventHandler
throw new Error(`Unsupported protocol "${protocol}"`);
}

const response: Response = await runner.run(
new Request({ ...event, correlationIdRegex: event.correlation_id_regex })
);
const response: Response = await runner.run(new Request({ ...event }));

const { statusCode, message, errorCode, body, headers } = response;

Expand Down
29 changes: 0 additions & 29 deletions packages/repeater/src/api/commands/RegisterRepeaterCommand.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/repeater/src/api/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
export { CreateRepeaterRequest } from './CreateRepeaterRequest';
export { DeleteRepeaterRequest } from './DeleteRepeaterRequest';
export { GetRepeaterRequest } from './GetRepeaterRequest';
export {
RegisterRepeaterCommand,
RegisterRepeaterCommandPayload,
RegisterRepeaterResult,
RepeaterRegisteringError
} from './RegisterRepeaterCommand';
13 changes: 0 additions & 13 deletions packages/repeater/src/api/events/RepeaterStatusEvent.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/repeater/src/api/events/index.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/repeater/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
export {
RepeaterRegisteringError,
RegisterRepeaterResult,
RegisterRepeaterCommandPayload,
RegisterRepeaterCommand
} from './commands';
export { RepeaterStatusEvent } from './events';
export * from './RepeatersManager';
export * from './DefaultRepeatersManager';
export * from './ExecuteRequestEventHandler';
8 changes: 1 addition & 7 deletions packages/repeater/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import './register';

export {
RepeatersManager,
ExecuteRequestEventHandler,
ExecuteRequestPayload,
ExecuteRequestResult
} from './api';
export { RepeatersManager } from './api';
export * from './lib';
export * from './models';
export * from './request-runner';
62 changes: 62 additions & 0 deletions packages/repeater/src/lib/DefaultRepeaterCommands.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { DefaultRepeaterCommands } from './DefaultRepeaterCommands';
import { Protocol } from '../models/Protocol';
import { RequestRunner, Request, Response } from '../request-runner';
import { instance, mock, reset, when } from 'ts-mockito';

describe('DefaultRepeaterCommands', () => {
let sut!: DefaultRepeaterCommands;

const mockedRequestRunner = mock<RequestRunner>();

beforeEach(() => {
sut = new DefaultRepeaterCommands([instance(mockedRequestRunner)]);
});

afterEach(() => reset<RequestRunner>(mockedRequestRunner));

describe('sendRequest', () => {
it('should send', async () => {
// arrange
const request = new Request({
protocol: Protocol.HTTP,
url: 'http://foo.bar',
method: 'GET'
});

const response = new Response({
protocol: Protocol.HTTP,
statusCode: 200
});

when(mockedRequestRunner.protocol).thenReturn(Protocol.HTTP);
when(mockedRequestRunner.run(request)).thenResolve(response);

// act
const result = await sut.sendRequest(request);

// assert
expect(result).toEqual(response);
});

it('should throw when there are no suitable protocol handler', async () => {
// arrange
const request = new Request({
protocol: Protocol.HTTP,
url: 'http://foo.bar',
method: 'GET'
});

when(mockedRequestRunner.protocol).thenReturn(
'someOtherProtocol' as Protocol
);

// act
const act = () => sut.sendRequest(request);

// assert
await expect(act).rejects.toThrow(
`Unsupported protocol "${Protocol.HTTP}"`
);
});
});
});
25 changes: 25 additions & 0 deletions packages/repeater/src/lib/DefaultRepeaterCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { RepeaterCommands } from './RepeaterCommands';
import { Request, Response, RequestRunner } from '../request-runner';
import { injectable, injectAll } from 'tsyringe';

@injectable()
export class DefaultRepeaterCommands implements RepeaterCommands {
constructor(
@injectAll(RequestRunner)
private readonly requestRunners: RequestRunner[]
) {}

public async sendRequest(request: Request): Promise<Response> {
const { protocol } = request;

const requestRunner = this.requestRunners.find(
x => x.protocol === protocol
);

if (!requestRunner) {
throw new Error(`Unsupported protocol "${protocol}"`);
}

return requestRunner.run(request);
}
}
Loading

0 comments on commit aa12bfa

Please sign in to comment.