Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/httpBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { BlockResultOut, DescriptHttpBlockResult, DescriptBlockOptions, Des
import type ContextClass from './context';
import type Cancel from './cancel';
import type DepsDomain from './depsDomain';
import type Logger from './logger';
import type { LoggerInterface } from './logger';

// --------------------------------------------------------------------------------------------------------------- //

Expand Down Expand Up @@ -216,7 +216,7 @@ class HttpBlock<
return x;
}

protected logger: Logger<Context>;
protected logger: LoggerInterface;

constructor({ block, options }: {
block?: DescriptHttpBlockDescription<ParamsOut, Context, HttpResult>;
Expand Down Expand Up @@ -334,7 +334,7 @@ class HttpBlock<
let error;

try {
result = await request(options, this.logger, context, blockCancel);
result = await request(options, this.logger, blockCancel);
headers = result.headers;

} catch (e) {
Expand Down
6 changes: 5 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import RunContext from './context';
import { ERROR_ID, createError, isError, DescriptError } from './error';

import Cancel from './cancel';
import Logger from './logger';
import Logger, { EVENT } from './logger';
import type { LoggerEvent, LoggerInterface } from './logger';
import Cache, { CacheInterface } from './cache';

import request from './request';
Expand Down Expand Up @@ -177,6 +178,9 @@ const run = function<

export {
Logger,
LoggerEvent,
LoggerInterface,
EVENT as LOGGER_EVENT,
Cache,
CacheInterface,
request,
Expand Down
46 changes: 20 additions & 26 deletions lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,34 @@ export type EventTimestamps = {
end?: number;
};

interface BaseLoggerEvent {
requestOptions: RequestOptions;

timestamps?: EventTimestamps;

request?: http.ClientRequest;

}

interface SuccessLoggerEvent extends BaseLoggerEvent {
export interface SuccessLoggerEvent {
type: EVENT.REQUEST_SUCCESS;
result: DescriptHttpResult;
request: http.ClientRequest;
requestOptions: RequestOptions;
timestamps: EventTimestamps;
}


interface ErrorLoggerEvent extends BaseLoggerEvent {
export interface ErrorLoggerEvent {
type: EVENT.REQUEST_ERROR;
error: DescriptError;
request: http.ClientRequest;
requestOptions: RequestOptions;
timestamps: EventTimestamps;
}

interface StartLoggerEvent extends BaseLoggerEvent {
export interface StartLoggerEvent {
type: EVENT.REQUEST_START;
requestOptions: RequestOptions;
}

export type LoggerEvent = SuccessLoggerEvent | ErrorLoggerEvent | StartLoggerEvent;

export interface LoggerInterface<Event = LoggerEvent> {
log(event: Event): void;
}

class Logger<Context> {
class Logger implements LoggerInterface<LoggerEvent> {
static EVENT = EVENT;

private _debug = false;
Expand All @@ -61,13 +61,13 @@ class Logger<Context> {
this._debug = config.debug || false;
}

log(event: LoggerEvent, context: Context) {
log(event: LoggerEvent) {
switch (event.type) {
case EVENT.REQUEST_START: {
if (this._debug) {
const message = `[DEBUG] ${ event.requestOptions.httpOptions.method } ${ event.requestOptions.url }`;

logToStream(process.stdout, message, context);
logToStream(process.stdout, message);
}

break;
Expand All @@ -85,7 +85,7 @@ class Logger<Context> {
}
}

logToStream(process.stdout, message, context);
logToStream(process.stdout, message);

break;
}
Expand All @@ -110,7 +110,7 @@ class Logger<Context> {
}
message += ` ${ total(event) } ${ event.requestOptions.httpOptions.method } ${ event.requestOptions.url }`;

logToStream(process.stderr, message, context);
logToStream(process.stderr, message);

break;
}
Expand All @@ -119,19 +119,13 @@ class Logger<Context> {

}

// --------------------------------------------------------------------------------------------------------------- //


// --------------------------------------------------------------------------------------------------------------- //

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function logToStream<Context>(stream: typeof process.stderr | typeof process.stdout, message: string, context: Context) {
function logToStream(stream: typeof process.stderr | typeof process.stdout, message: string) {
const date = new Date().toISOString();

stream.write(`${ date } ${ message }\n`);
}

function total(event: LoggerEvent) {
function total(event: SuccessLoggerEvent | ErrorLoggerEvent) {
let total = `${ (event.timestamps?.end || 0) - (event.timestamps?.start || 0) }ms`;

const retries = event.requestOptions.retries;
Expand Down
57 changes: 31 additions & 26 deletions lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import { decompress } from '@fengkx/zstd-napi';
import type { TransformOptions, TransformCallback } from 'stream';
import { Transform } from 'stream';

import type { EventTimestamps, LoggerEvent } from './logger';
import type Logger from './logger';
import type { ErrorLoggerEvent, EventTimestamps, LoggerEvent, LoggerInterface, StartLoggerEvent, SuccessLoggerEvent } from './logger';
import { EVENT } from './logger';

import type { Deffered } from './getDeferred';
Expand Down Expand Up @@ -281,10 +280,9 @@ export class RequestOptions {

// --------------------------------------------------------------------------------------------------------------- //

class DescriptRequest<Context> {
context: Context;
class DescriptRequest {
options: RequestOptions;
logger: Logger<Context>;
logger: LoggerInterface<LoggerEvent>;
cancel: Cancel;
timestamps: EventTimestamps;
hTimeout: number | null;
Expand All @@ -293,11 +291,10 @@ class DescriptRequest<Context> {

deferred: Deffered<DescriptHttpResult, DescriptError>;

constructor(options: RequestOptions, logger: Logger<Context>, context: Context, cancel: Cancel) {
constructor(options: RequestOptions, logger: LoggerInterface<LoggerEvent>, cancel: Cancel) {
this.options = options;
this.logger = logger;
this.cancel = cancel;
this.context = context;

this.timestamps = {};
this.hTimeout = null;
Expand All @@ -306,10 +303,11 @@ class DescriptRequest<Context> {
}

start(): Promise<DescriptHttpResult> {
this.log({
const logEvent: StartLoggerEvent = {
type: EVENT.REQUEST_START,
requestOptions: this.options,
});
};
this.log(logEvent);

this.timestamps.start = Date.now();

Expand Down Expand Up @@ -410,12 +408,16 @@ class DescriptRequest<Context> {

this.timestamps.end = this.timestamps.end || Date.now();

this.log({
type: EVENT.REQUEST_SUCCESS,
requestOptions: this.options,
result: result,
timestamps: this.timestamps,
});
if (this.req) {
const logEvent: SuccessLoggerEvent = {
type: EVENT.REQUEST_SUCCESS,
request: this.req,
requestOptions: this.options,
result: result,
timestamps: this.timestamps,
};
this.log(logEvent);
}

this.isResolved = true;

Expand All @@ -433,12 +435,16 @@ class DescriptRequest<Context> {

const error = createError(reason);

this.log({
type: EVENT.REQUEST_ERROR,
requestOptions: this.options,
error: error,
timestamps: this.timestamps,
});
if (this.req) {
const logEvent: ErrorLoggerEvent = {
type: EVENT.REQUEST_ERROR,
error: error,
request: this.req,
requestOptions: this.options,
timestamps: this.timestamps,
};
this.log(logEvent);
}

this.isResolved = true;

Expand Down Expand Up @@ -540,23 +546,22 @@ class DescriptRequest<Context> {
};
}

log(event: LoggerEvent) {
log(event: SuccessLoggerEvent | ErrorLoggerEvent | StartLoggerEvent) {
if (this.logger) {
event.request = this.req;
this.logger.log(event, this.context);
this.logger.log(event);
}
}

}

// --------------------------------------------------------------------------------------------------------------- //

async function request<Context>(options: DescriptRequestOptions, logger: Logger<Context>, context: Context, cancel: Cancel): Promise<DescriptHttpResult> {
async function request(options: DescriptRequestOptions, logger: LoggerInterface<LoggerEvent>, cancel: Cancel): Promise<DescriptHttpResult> {
const requestOptions = new RequestOptions(options);

// eslint-disable-next-line no-constant-condition
while (true) {
const req = new DescriptRequest(requestOptions, logger, context, cancel);
const req = new DescriptRequest(requestOptions, logger, cancel);

try {
const result = await req.start();
Expand Down
5 changes: 2 additions & 3 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import type BaseBlock from './block';
import type { DescriptBlockDeps, DescriptBlockId } from './depsDomain';
import type { DescriptError } from './error';
import type { CacheInterface } from './cache';
import type DescriptLogger from './logger';
import type { IncomingHttpHeaders } from 'http';
import type { EventTimestamps } from './logger';
import type { EventTimestamps, LoggerInterface } from './logger';
import type { RequestOptions } from './request';
import type HttpBlock from './httpBlock';

Expand Down Expand Up @@ -204,5 +203,5 @@ export interface DescriptBlockOptions<

required?: boolean;

logger?: DescriptLogger<Context>;
logger?: LoggerInterface;
}
21 changes: 9 additions & 12 deletions tests/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@ import https_ from 'https';
import http from 'http';
import fs_ from 'fs';
import path_ from 'path';
import type Cancel from '../lib/cancel';
import type Logger from '../lib/logger';
import type { Cancel, LoggerInterface } from '../lib';


// --------------------------------------------------------------------------------------------------------------- //

function getDoRequest<Context>(defaultOptions: DescriptRequestOptions, context: Context) {
return function doRequest(options: DescriptRequestOptions = {}, logger?: Logger<Context>, cancel?: Cancel) {
function getDoRequest(defaultOptions: DescriptRequestOptions) {
return function doRequest(options: DescriptRequestOptions = {}, logger?: LoggerInterface, cancel?: Cancel) {
logger = logger || new de.Logger({ debug: true });
cancel = cancel || new de.Cancel();

return request({ ...defaultOptions, ...options }, logger, context, cancel);
return request({ ...defaultOptions, ...options }, logger, cancel);
};
}

const context = {};

// --------------------------------------------------------------------------------------------------------------- //

describe('request', () => {
Expand All @@ -46,7 +43,7 @@ describe('request', () => {
hostname: '127.0.0.1',
port: PORT,
pathname: '/',
}, context);
});

const fake = new Server({
module: http,
Expand Down Expand Up @@ -968,7 +965,7 @@ describe('request', () => {
hostname: '127.0.0.1',
port: PORT,
pathname: '/',
}, context);
});

let serverKey;
let serverCert;
Expand Down Expand Up @@ -1072,7 +1069,7 @@ describe('request', () => {
hostname: '127.0.0.1',
port: PORT,
pathname: '/',
}, context);
});

const path = getPath();

Expand Down Expand Up @@ -1102,7 +1099,7 @@ describe('request', () => {
protocol: 'http:',
hostname: '127.0.0.1',
port: PORT,
}, context);
});

beforeAll(() => serverListen(server, PORT));
afterAll(() => serverClose(server));
Expand Down Expand Up @@ -1150,7 +1147,7 @@ describe('request', () => {
protocol: 'http:',
hostname: '127.0.0.1',
port: PORT,
}, context);
});

beforeAll(() => serverListen(server, PORT));
afterAll(() => serverClose(server));
Expand Down