11import diagnosticsChannel from 'node:diagnostics_channel' ;
2+ import type { Channel } from 'node:diagnostics_channel' ;
23import { EventEmitter } from 'node:events' ;
34import { createReadStream } from 'node:fs' ;
45import { STATUS_CODES } from 'node:http' ;
5- import { LookupFunction } from 'node:net' ;
6+ import type { LookupFunction } from 'node:net' ;
67import { basename } from 'node:path' ;
78import { performance } from 'node:perf_hooks' ;
89import querystring from 'node:querystring' ;
@@ -23,13 +24,14 @@ import { request as undiciRequest, Dispatcher, Agent, getGlobalDispatcher, Pool
2324import undiciSymbols from 'undici/lib/core/symbols.js' ;
2425
2526import { initDiagnosticsChannel } from './diagnosticsChannel.js' ;
26- import { FetchOpaque } from './FetchOpaqueInterceptor.js' ;
27+ import type { FetchOpaque } from './FetchOpaqueInterceptor.js' ;
2728import { FormData } from './FormData.js' ;
28- import { HttpAgent , CheckAddressFunction } from './HttpAgent.js' ;
29+ import { HttpAgent } from './HttpAgent.js' ;
30+ import type { CheckAddressFunction } from './HttpAgent.js' ;
2931import { HttpClientConnectTimeoutError , HttpClientRequestTimeoutError } from './HttpClientError.js' ;
3032import type { IncomingHttpHeaders } from './IncomingHttpHeaders.js' ;
31- import { RequestURL , RequestOptions , HttpMethod , RequestMeta } from './Request.js' ;
32- import { RawResponseWithMeta , HttpClientResponse , SocketInfo } from './Response.js' ;
33+ import type { RequestURL , RequestOptions , HttpMethod , RequestMeta } from './Request.js' ;
34+ import type { RawResponseWithMeta , HttpClientResponse , SocketInfo } from './Response.js' ;
3335import symbols from './symbols.js' ;
3436import { parseJSON , digestAuthHeader , globalId , performanceTime , isReadable , updateSocketInfo } from './utils.js' ;
3537
@@ -38,7 +40,7 @@ type UndiciRequestOption = Exists<Parameters<typeof undiciRequest>[1]>;
3840type PropertyShouldBe < T , K extends keyof T , V > = Omit < T , K > & { [ P in K ] : V } ;
3941type IUndiciRequestOption = PropertyShouldBe < UndiciRequestOption , 'headers' , IncomingHttpHeaders > ;
4042
41- export const PROTO_RE = / ^ h t t p s ? : \/ \/ / i;
43+ export const PROTO_RE : RegExp = / ^ h t t p s ? : \/ \/ / i;
4244
4345export interface UndiciTimingInfo {
4446 startTime : number ;
@@ -64,7 +66,7 @@ export interface UndiciTimingInfo {
6466// keep typo compatibility
6567export interface UnidiciTimingInfo extends UndiciTimingInfo { }
6668
67- function noop ( ) {
69+ function noop ( ) : void {
6870 // noop
6971}
7072
@@ -108,19 +110,19 @@ export type ClientOptions = {
108110 } ;
109111} ;
110112
111- export const VERSION = 'VERSION' ;
113+ export const VERSION : string = 'VERSION' ;
112114// 'node-urllib/4.0.0 Node.js/18.19.0 (darwin; x64)'
113- export const HEADER_USER_AGENT = `node-urllib/${ VERSION } Node.js/${ process . version . substring ( 1 ) } (${ process . platform } ; ${ process . arch } )` ;
115+ export const HEADER_USER_AGENT : string = `node-urllib/${ VERSION } Node.js/${ process . version . substring ( 1 ) } (${ process . platform } ; ${ process . arch } )` ;
114116
115- function getFileName ( stream : Readable ) {
117+ function getFileName ( stream : Readable ) : string {
116118 const filePath : string = ( stream as any ) . path ;
117119 if ( filePath ) {
118120 return basename ( filePath ) ;
119121 }
120122 return '' ;
121123}
122124
123- function defaultIsRetry ( response : HttpClientResponse ) {
125+ function defaultIsRetry ( response : HttpClientResponse ) : boolean {
124126 return response . status >= 500 ;
125127}
126128
@@ -132,7 +134,12 @@ export type RequestContext = {
132134 history : string [ ] ;
133135} ;
134136
135- export const channels = {
137+ export const channels : {
138+ request : Channel ;
139+ response : Channel ;
140+ fetchRequest : Channel ;
141+ fetchResponse : Channel ;
142+ } = {
136143 request : diagnosticsChannel . channel ( 'urllib:request' ) ,
137144 response : diagnosticsChannel . channel ( 'urllib:response' ) ,
138145 fetchRequest : diagnosticsChannel . channel ( 'urllib:fetch:request' ) ,
@@ -205,15 +212,15 @@ export class HttpClient extends EventEmitter {
205212 initDiagnosticsChannel ( ) ;
206213 }
207214
208- getDispatcher ( ) {
215+ getDispatcher ( ) : Dispatcher {
209216 return this . #dispatcher ?? getGlobalDispatcher ( ) ;
210217 }
211218
212- setDispatcher ( dispatcher : Dispatcher ) {
219+ setDispatcher ( dispatcher : Dispatcher ) : void {
213220 this . #dispatcher = dispatcher ;
214221 }
215222
216- getDispatcherPoolStats ( ) {
223+ getDispatcherPoolStats ( ) : Record < string , PoolStat > {
217224 const agent = this . getDispatcher ( ) ;
218225 // origin => Pool Instance
219226 const clients : Map < string , WeakRef < Pool > > | undefined = Reflect . get ( agent , undiciSymbols . kClients ) ;
@@ -239,12 +246,12 @@ export class HttpClient extends EventEmitter {
239246 return poolStatsMap ;
240247 }
241248
242- async request < T = any > ( url : RequestURL , options ?: RequestOptions ) {
249+ async request < T = any > ( url : RequestURL , options ?: RequestOptions ) : Promise < HttpClientResponse < T > > {
243250 return await this . #requestInternal< T > ( url , options ) ;
244251 }
245252
246253 // alias to request, keep compatible with urllib@2 HttpClient.curl
247- async curl < T = any > ( url : RequestURL , options ?: RequestOptions ) {
254+ async curl < T = any > ( url : RequestURL , options ?: RequestOptions ) : Promise < HttpClientResponse < T > > {
248255 return await this . request < T > ( url , options ) ;
249256 }
250257
0 commit comments