diff --git a/lib/httpBlock.ts b/lib/httpBlock.ts index 310b3d4..cdc6d63 100644 --- a/lib/httpBlock.ts +++ b/lib/httpBlock.ts @@ -216,7 +216,7 @@ class HttpBlock< return x; } - protected logger: Logger; + protected logger: Logger; constructor({ block, options }: { block?: DescriptHttpBlockDescription; @@ -334,7 +334,7 @@ class HttpBlock< let error; try { - result = await request(options, this.logger, blockCancel); + result = await request(options, this.logger, context, blockCancel); headers = result.headers; } catch (e) { diff --git a/lib/logger.ts b/lib/logger.ts index 0926909..ab7a1d3 100644 --- a/lib/logger.ts +++ b/lib/logger.ts @@ -50,7 +50,7 @@ interface StartLoggerEvent extends BaseLoggerEvent { export type LoggerEvent = SuccessLoggerEvent | ErrorLoggerEvent | StartLoggerEvent; -class Logger { +class Logger { static EVENT = EVENT; private _debug = false; @@ -61,13 +61,13 @@ class Logger { this._debug = config.debug || false; } - log(event: LoggerEvent) { + log(event: LoggerEvent, context: Context) { switch (event.type) { case EVENT.REQUEST_START: { if (this._debug) { const message = `[DEBUG] ${ event.requestOptions.httpOptions.method } ${ event.requestOptions.url }`; - logToStream(process.stdout, message); + logToStream(process.stdout, message, context); } break; @@ -85,7 +85,7 @@ class Logger { } } - logToStream(process.stdout, message); + logToStream(process.stdout, message, context); break; } @@ -110,7 +110,7 @@ class Logger { } message += ` ${ total(event) } ${ event.requestOptions.httpOptions.method } ${ event.requestOptions.url }`; - logToStream(process.stderr, message); + logToStream(process.stderr, message, context); break; } @@ -124,7 +124,7 @@ class Logger { // --------------------------------------------------------------------------------------------------------------- // -function logToStream(stream: typeof process.stderr | typeof process.stdout, message: string) { +function logToStream(stream: typeof process.stderr | typeof process.stdout, message: string, context: Context) { const date = new Date().toISOString(); stream.write(`${ date } ${ message }\n`); diff --git a/lib/request.ts b/lib/request.ts index c9e9022..3a6ee79 100644 --- a/lib/request.ts +++ b/lib/request.ts @@ -281,10 +281,10 @@ export class RequestOptions { // --------------------------------------------------------------------------------------------------------------- // -class DescriptRequest { - +class DescriptRequest { + context: Context; options: RequestOptions; - logger: Logger; + logger: Logger; cancel: Cancel; timestamps: EventTimestamps; hTimeout: number | null; @@ -293,10 +293,11 @@ class DescriptRequest { deferred: Deffered; - constructor(options: RequestOptions, logger: Logger, cancel: Cancel) { + constructor(options: RequestOptions, logger: Logger, context: Context, cancel: Cancel) { this.options = options; this.logger = logger; this.cancel = cancel; + this.context = context; this.timestamps = {}; this.hTimeout = null; @@ -542,7 +543,7 @@ class DescriptRequest { log(event: LoggerEvent) { if (this.logger) { event.request = this.req; - this.logger.log(event); + this.logger.log(event, this.context); } } @@ -550,12 +551,12 @@ class DescriptRequest { // --------------------------------------------------------------------------------------------------------------- // -async function request(options: DescriptRequestOptions, logger: Logger, cancel: Cancel): Promise { +async function request(options: DescriptRequestOptions, logger: Logger, context: Context, cancel: Cancel): Promise { const requestOptions = new RequestOptions(options); // eslint-disable-next-line no-constant-condition while (true) { - const req = new DescriptRequest(requestOptions, logger, cancel); + const req = new DescriptRequest(requestOptions, logger, context, cancel); try { const result = await req.start(); diff --git a/lib/types.ts b/lib/types.ts index d2f6c06..d62e767 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -204,5 +204,5 @@ export interface DescriptBlockOptions< required?: boolean; - logger?: DescriptLogger; + logger?: DescriptLogger; } diff --git a/tests/request.test.ts b/tests/request.test.ts index 9b2059d..13dd290 100644 --- a/tests/request.test.ts +++ b/tests/request.test.ts @@ -21,15 +21,17 @@ import type Logger from '../lib/logger'; // --------------------------------------------------------------------------------------------------------------- // -function getDoRequest(defaultOptions: DescriptRequestOptions) { - return function doRequest(options: DescriptRequestOptions = {}, logger?: Logger, cancel?: Cancel) { +function getDoRequest(defaultOptions: DescriptRequestOptions, context: Context) { + return function doRequest(options: DescriptRequestOptions = {}, logger?: Logger, cancel?: Cancel) { logger = logger || new de.Logger({ debug: true }); cancel = cancel || new de.Cancel(); - return request({ ...defaultOptions, ...options }, logger, cancel); + return request({ ...defaultOptions, ...options }, logger, context, cancel); }; } +const context = {}; + // --------------------------------------------------------------------------------------------------------------- // describe('request', () => { @@ -44,7 +46,7 @@ describe('request', () => { hostname: '127.0.0.1', port: PORT, pathname: '/', - }); + }, context); const fake = new Server({ module: http, @@ -966,7 +968,7 @@ describe('request', () => { hostname: '127.0.0.1', port: PORT, pathname: '/', - }); + }, context); let serverKey; let serverCert; @@ -1070,7 +1072,7 @@ describe('request', () => { hostname: '127.0.0.1', port: PORT, pathname: '/', - }); + }, context); const path = getPath(); @@ -1100,7 +1102,7 @@ describe('request', () => { protocol: 'http:', hostname: '127.0.0.1', port: PORT, - }); + }, context); beforeAll(() => serverListen(server, PORT)); afterAll(() => serverClose(server)); @@ -1148,7 +1150,7 @@ describe('request', () => { protocol: 'http:', hostname: '127.0.0.1', port: PORT, - }); + }, context); beforeAll(() => serverListen(server, PORT)); afterAll(() => serverClose(server));