Skip to content

Commit

Permalink
feat: Add version headers to all responses
Browse files Browse the repository at this point in the history
  • Loading branch information
VolcanoCookies authored and ShibireX committed Apr 15, 2023
1 parent 6a413ee commit a86eaf5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { FastifyInstance } from 'fastify';
import segmentRoutes from './segments/routes';
import manifestRoutes from './manifests/routes';
import { generateHeartbeatResponse } from './shared/utils';
import {
generateHeartbeatResponse,
addCustomVersionHeader
} from './shared/utils';

const apiPrefix = (version: number): string => `api/v${version}`;

Expand All @@ -17,4 +20,5 @@ export function registerRoutes(app: FastifyInstance) {
const opts = { prefix: apiPrefix(2) };
app.register(segmentRoutes, opts);
app.register(manifestRoutes, opts);
addCustomVersionHeader(app);
}
27 changes: 27 additions & 0 deletions src/server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import fastify from 'fastify';
import { registerRoutes } from './routes';
import {
HLS_PROXY_MASTER,
HLS_PROXY_MEDIA,
SEGMENTS_PROXY_SEGMENT
} from './segments/constants';

describe('Chaos Stream Proxy server', () => {
let app = null;
beforeEach(() => {
app = fastify();
registerRoutes(app);
});

it.each([HLS_PROXY_MASTER, HLS_PROXY_MEDIA, SEGMENTS_PROXY_SEGMENT])(
'route %p contains x-version header',
async (route) => {
const response = await app.inject(route);
expect(response.headers).toEqual(
expect.objectContaining({
'x-version': process.env.npm_package_version
})
);
}
);
});
34 changes: 34 additions & 0 deletions src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { ReadStream } from 'fs';
import { IncomingHttpHeaders } from 'http';
import path from 'path';
import { CorruptorConfigMap } from '../manifests/utils/configs';
import {
FastifyReply,
FastifyRequest,
RequestPayload,
FastifyInstance
} from 'fastify';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { version } = require('../../package.json');
Expand Down Expand Up @@ -239,3 +245,31 @@ export function segmentUrlParamString(

export const SERVICE_ORIGIN =
process.env.SERVICE_ORIGIN || 'http://localhost:8000';

export function joinNotNull(strings: (string | null)[], delimiter: string) {
return strings.filter((s) => s != null).join(delimiter);
}

export function addCustomVersionHeader(app: FastifyInstance): void {
app.addHook(
'onSend',
async (
request: FastifyRequest,
reply: FastifyReply,
payload: RequestPayload
): Promise<RequestPayload> => {
reply.headers({
'Access-Control-Allow-Headers': joinNotNull(
[reply.getHeader('Access-Control-Allow-Headers'), 'X-Version'],
', '
),
'Access-Control-Expose-Headers': joinNotNull(
[reply.getHeader('Access-Control-Expose-Headers'), 'X-Version'],
', '
),
'X-Version': process.env.npm_package_version
});
return payload;
}
);
}

0 comments on commit a86eaf5

Please sign in to comment.