This commit is contained in:
2025-07-28 15:12:11 +00:00
parent 8f5c88b47e
commit 7dcc5f3fe2
13 changed files with 175 additions and 78 deletions

View File

@@ -1,6 +1,7 @@
import * as plugins from './plugins.js';
import * as types from './types.js';
import { CoreResponse } from './response.js';
import { CoreRequest as AbstractCoreRequest } from '../core_base/request.js';
// Keep-alive agents for connection pooling
const httpAgent = new plugins.agentkeepalive.HttpAgent({
@@ -26,30 +27,9 @@ const httpsAgentKeepAliveFalse = new plugins.agentkeepalive.HttpsAgent({
});
/**
* Core Request class that handles all HTTP/HTTPS requests
* Node.js implementation of Core Request class that handles all HTTP/HTTPS requests
*/
export class CoreRequest {
/**
* Tests if a URL is a unix socket
*/
static isUnixSocket(url: string): boolean {
const unixRegex = /^(http:\/\/|https:\/\/|)unix:/;
return unixRegex.test(url);
}
/**
* Parses socket path and route from unix socket URL
*/
static parseUnixSocketUrl(url: string): { socketPath: string; path: string } {
const parseRegex = /(.*):(.*)/;
const result = parseRegex.exec(url);
return {
socketPath: result[1],
path: result[2],
};
}
private url: string;
private options: types.ICoreRequestOptions;
export class CoreRequest extends AbstractCoreRequest<types.ICoreRequestOptions, CoreResponse> {
private requestDataFunc: ((req: plugins.http.ClientRequest) => void) | null;
constructor(
@@ -57,8 +37,7 @@ export class CoreRequest {
options: types.ICoreRequestOptions = {},
requestDataFunc: ((req: plugins.http.ClientRequest) => void) | null = null
) {
this.url = url;
this.options = options;
super(url, options);
this.requestDataFunc = requestDataFunc;
}

View File

@@ -1,13 +1,13 @@
import * as plugins from './plugins.js';
import * as types from './types.js';
import { CoreResponse as AbstractCoreResponse } from '../core_base/response.js';
/**
* Core Response class that provides a fetch-like API
* Node.js implementation of Core Response class that provides a fetch-like API
*/
export class CoreResponse<T = any> implements types.ICoreResponse<T> {
export class CoreResponse<T = any> extends AbstractCoreResponse<T> implements types.ICoreResponse<T> {
private incomingMessage: plugins.http.IncomingMessage;
private bodyBufferPromise: Promise<Buffer> | null = null;
private consumed = false;
// Public properties
public readonly ok: boolean;
@@ -17,6 +17,7 @@ export class CoreResponse<T = any> implements types.ICoreResponse<T> {
public readonly url: string;
constructor(incomingMessage: plugins.http.IncomingMessage, url: string) {
super();
this.incomingMessage = incomingMessage;
this.url = url;
this.status = incomingMessage.statusCode || 0;
@@ -25,16 +26,6 @@ export class CoreResponse<T = any> implements types.ICoreResponse<T> {
this.headers = incomingMessage.headers;
}
/**
* Ensures the body can only be consumed once
*/
private ensureNotConsumed(): void {
if (this.consumed) {
throw new Error('Body has already been consumed');
}
this.consumed = true;
}
/**
* Collects the body as a buffer
*/

View File

@@ -1,25 +1,19 @@
import * as plugins from './plugins.js';
import * as baseTypes from '../core_base/types.js';
// Re-export base types
export * from '../core_base/types.js';
/**
* Core request options extending Node.js RequestOptions
*/
export interface ICoreRequestOptions extends plugins.https.RequestOptions {
export interface ICoreRequestOptions extends plugins.https.RequestOptions, Omit<baseTypes.IAbstractRequestOptions, 'method' | 'headers'> {
keepAlive?: boolean;
requestBody?: any;
queryParams?: { [key: string]: string };
hardDataCuttingTimeout?: number;
}
/**
* HTTP Methods supported
*/
export type THttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
/**
* Response types supported
*/
export type ResponseType = 'json' | 'text' | 'binary' | 'stream';
/**
* Extended IncomingMessage with body property (legacy compatibility)
*/
@@ -28,27 +22,9 @@ export interface IExtendedIncomingMessage<T = any> extends plugins.http.Incoming
}
/**
* Form field data for multipart/form-data requests
* Core response object that provides fetch-like API with Node.js specific methods
*/
export interface IFormField {
name: string;
value: string | Buffer;
filename?: string;
contentType?: string;
}
/**
* URL encoded form field
*/
export interface IUrlEncodedField {
key: string;
value: string;
}
/**
* Core response object that provides fetch-like API
*/
export interface ICoreResponse<T = any> {
export interface ICoreResponse<T = any> extends baseTypes.IAbstractResponse<T> {
// Properties
ok: boolean;
status: number;