From 5555ba70407b68680fdfe8d1c4a2a41d0e49c00e Mon Sep 17 00:00:00 2001 From: WebStreamr <210764791+webstreamr@users.noreply.github.com> Date: Fri, 18 Jul 2025 08:18:43 +0000 Subject: [PATCH] refactor: extract context creation --- src/controller/StreamController.ts | 11 ++----- src/utils/__snapshots__/context.test.ts.snap | 23 +++++++++++++++ src/utils/context.test.ts | 31 ++++++++++++++++++++ src/utils/context.ts | 12 ++++++++ src/utils/index.ts | 1 + 5 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 src/utils/__snapshots__/context.test.ts.snap create mode 100644 src/utils/context.test.ts create mode 100644 src/utils/context.ts diff --git a/src/controller/StreamController.ts b/src/controller/StreamController.ts index 3ceda43..f7a799f 100644 --- a/src/controller/StreamController.ts +++ b/src/controller/StreamController.ts @@ -2,8 +2,7 @@ import { Request, Response, Router } from 'express'; import { ContentType } from 'stremio-addon-sdk'; import winston from 'winston'; import { Source } from '../source'; -import { Config, Context } from '../types'; -import { envIsProd, getDefaultConfig, ImdbId, StreamResolver } from '../utils'; +import { contextFromRequest, envIsProd, ImdbId, StreamResolver } from '../utils'; export class StreamController { public readonly router: Router; @@ -23,16 +22,10 @@ export class StreamController { } private async getStream(req: Request, res: Response) { - const config: Config = req.params['config'] ? JSON.parse(req.params['config']) : getDefaultConfig(); const type: ContentType = (req.params['type'] || '') as ContentType; const id: string = req.params['id'] || ''; - const ctx: Context = { - hostUrl: new URL(`${req.protocol}://${req.host}`), - id: res.getHeader('X-Request-ID') as string, - ...(req.ip && { ip: req.ip }), - config, - }; + const ctx = contextFromRequest(req); this.logger.info(`Search stream for type "${type}" and id "${id}" for ip ${ctx.ip}`, ctx); diff --git a/src/utils/__snapshots__/context.test.ts.snap b/src/utils/__snapshots__/context.test.ts.snap new file mode 100644 index 0000000..b54620c --- /dev/null +++ b/src/utils/__snapshots__/context.test.ts.snap @@ -0,0 +1,23 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`contextFromRequest with config and ip 1`] = ` +{ + "config": { + "de": "on", + }, + "hostUrl": "https://localhost/", + "id": "fake-id", + "ip": "127.0.0.1", +} +`; + +exports[`contextFromRequest without config 1`] = ` +{ + "config": { + "en": "on", + "multi": "on", + }, + "hostUrl": "https://localhost/", + "id": "fake-id", +} +`; diff --git a/src/utils/context.test.ts b/src/utils/context.test.ts new file mode 100644 index 0000000..68345cf --- /dev/null +++ b/src/utils/context.test.ts @@ -0,0 +1,31 @@ +import { Request } from 'express'; +import { contextFromRequest } from './context'; + +describe('contextFromRequest', () => { + test('with config and ip', () => { + const req = { + protocol: 'https', + host: 'localhost', + headers: { + 'X-Request-ID': 'fake-id', + }, + ip: '127.0.0.1', + params: { config: '{"de":"on"}' }, + }; + + expect(contextFromRequest(req as unknown as Request)).toMatchSnapshot(); + }); + + test('without config', () => { + const req = { + protocol: 'https', + host: 'localhost', + headers: { + 'X-Request-ID': 'fake-id', + }, + params: { }, + }; + + expect(contextFromRequest(req as unknown as Request)).toMatchSnapshot(); + }); +}); diff --git a/src/utils/context.ts b/src/utils/context.ts new file mode 100644 index 0000000..92076c2 --- /dev/null +++ b/src/utils/context.ts @@ -0,0 +1,12 @@ +import { Request } from 'express'; +import { Context } from '../types'; +import { getDefaultConfig } from './config'; + +export const contextFromRequest = (req: Request): Context => { + return { + hostUrl: new URL(`${req.protocol}://${req.host}`), + id: req.headers['X-Request-ID'] as string, + ...(req.ip && { ip: req.ip }), + config: req.params['config'] ? JSON.parse(req.params['config']) : getDefaultConfig(), + }; +}; diff --git a/src/utils/index.ts b/src/utils/index.ts index 2e549e9..23be2d8 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -2,6 +2,7 @@ export * from './Fetcher'; export * from './FetcherMock'; export * from './StreamResolver'; export * from './config'; +export * from './context'; export * from './embed'; export * from './env'; export * from './height';