Skip to content

Commit

Permalink
refactor: use ExtendedContext definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik committed Jan 29, 2024
1 parent bc3f44a commit a0c4972
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/lib/http/middleware/authenticate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Context, Next } from 'koa';
import { auth } from '../auth.js';
import type { ExtendedMiddleware } from '../../../types.js';

export const authenticate = async (ctx: Context, next: Next) => {
export const authenticate: ExtendedMiddleware = async (ctx, next) => {
const { headers } = ctx.request;

if (headers && headers.authorization) {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/rate-limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { RateLimiterRedis, RateLimiterRes } from 'rate-limiter-flexible';
import requestIp from 'request-ip';
import { createPersistentRedisClient } from './redis/persistent-client.js';
import createHttpError from 'http-errors';
import type { ExtendedContext } from '../types.js';

const redisClient = await createPersistentRedisClient({ legacyMode: true });

Expand All @@ -21,7 +22,7 @@ export const authenticatedRateLimiter = new RateLimiterRedis({
duration: config.get<number>('measurement.rateLimitReset'),
});

export const rateLimit = async (ctx: Context, numberOfProbes: number) => {
export const rateLimit = async (ctx: ExtendedContext, numberOfProbes: number) => {
if (ctx['isAdmin']) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/measurement/route/create-measurement.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import config from 'config';
import type { Context } from 'koa';
import type Router from '@koa/router';
import { getMeasurementRunner } from '../runner.js';
import { bodyParser } from '../../lib/http/middleware/body-parser.js';
import { corsAuthHandler } from '../../lib/http/middleware/cors.js';
import { validate } from '../../lib/http/middleware/validate.js';
import { authenticate } from '../../lib/http/middleware/authenticate.js';
import { schema } from '../schema/global-schema.js';
import type { ExtendedContext } from '../../types.js';

const hostConfig = config.get<string>('server.host');
const runner = getMeasurementRunner();

const handle = async (ctx: Context): Promise<void> => {
const handle = async (ctx: ExtendedContext): Promise<void> => {
const { measurementId, probesCount } = await runner.run(ctx);

ctx.status = 202;
Expand Down
4 changes: 2 additions & 2 deletions src/measurement/runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Context } from 'koa';
import config from 'config';
import type { Server } from 'socket.io';
import createHttpError from 'http-errors';
Expand All @@ -10,6 +9,7 @@ import type { MeasurementStore } from './store.js';
import { getMeasurementStore } from './store.js';
import type { MeasurementRequest, MeasurementResultMessage, MeasurementProgressMessage, UserRequest } from './types.js';
import { rateLimit } from '../lib/rate-limiter.js';
import type { ExtendedContext } from '../types.js';

export class MeasurementRunner {
constructor (
Expand All @@ -20,7 +20,7 @@ export class MeasurementRunner {
private readonly metrics: MetricsAgent,
) {}

async run (ctx: Context): Promise<{measurementId: string; probesCount: number;}> {
async run (ctx: ExtendedContext): Promise<{measurementId: string; probesCount: number;}> {
const userRequest = ctx.request.body as UserRequest;
const { onlineProbesMap, allProbes, request } = await this.router.findMatchingProbes(userRequest);

Expand Down
2 changes: 2 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ import type { DocsLinkContext } from './lib/http/middleware/docs-link.js';

export type CustomState = Koa.DefaultState;
export type CustomContext = Koa.DefaultContext & DocsLinkContext;

export type ExtendedContext = Router.RouterContext<CustomState, CustomContext>;
export type ExtendedMiddleware = Router.Middleware<CustomState, CustomContext>;
12 changes: 6 additions & 6 deletions test/tests/unit/measurement/runner.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Context } from 'koa';
import * as sinon from 'sinon';
import { Server } from 'socket.io';
import { expect } from 'chai';
Expand All @@ -10,6 +9,7 @@ import type { Probe } from '../../../../src/probe/types.js';
import type { MeasurementRunner } from '../../../../src/measurement/runner.js';
import type { MeasurementRecord, MeasurementResultMessage } from '../../../../src/measurement/types.js';
import createHttpError from 'http-errors';
import type { ExtendedContext } from '../../../../src/types.js';

const getProbe = (id: number) => ({ client: id } as unknown as Probe);

Expand Down Expand Up @@ -73,7 +73,7 @@ describe('MeasurementRunner', () => {
request: {
body: request,
},
} as unknown as Context);
} as unknown as ExtendedContext);


expect(router.findMatchingProbes.callCount).to.equal(1);
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('MeasurementRunner', () => {
request: {
body: request,
},
} as unknown as Context);
} as unknown as ExtendedContext);


expect(router.findMatchingProbes.callCount).to.equal(1);
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('MeasurementRunner', () => {
request: {
body: request,
},
} as unknown as Context;
} as unknown as ExtendedContext;

await runner.run(ctx);

Expand Down Expand Up @@ -322,7 +322,7 @@ describe('MeasurementRunner', () => {
request: {
body: request,
},
} as unknown as Context).catch((err: unknown) => err);
} as unknown as ExtendedContext).catch((err: unknown) => err);
expect(err).to.deep.equal(createHttpError(422, 'No suitable probes found.', { type: 'no_probes_found' }));
expect(store.markFinished.callCount).to.equal(0);
});
Expand Down Expand Up @@ -351,7 +351,7 @@ describe('MeasurementRunner', () => {
request: {
body: request,
},
} as unknown as Context);
} as unknown as ExtendedContext);

expect(store.markFinished.callCount).to.equal(1);
expect(store.markFinished.args[0]).to.deep.equal([ 'measurementid' ]);
Expand Down

0 comments on commit a0c4972

Please sign in to comment.