Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 21, 2023
1 parent 196a3b6 commit ba4bd43
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
getGlobalDispatcher,
Pool,
} from 'undici';
import { kClients } from 'undici/lib/core/symbols.js';
import undiciSymbols from 'undici/lib/core/symbols.js';
import { FormData as FormDataNode } from 'formdata-node';
import { FormDataEncoder } from 'form-data-encoder';
import createUserAgent from 'default-user-agent';
Expand Down Expand Up @@ -206,7 +206,7 @@ export class HttpClient extends EventEmitter {
getDispatcherPoolStats() {
const agent = this.getDispatcher();
// origin => Pool Instance
const clients: Map<string, WeakRef<Pool>> = agent[kClients];
const clients: Map<string, WeakRef<Pool>> = agent[undiciSymbols.kClients];
const poolStatsMap: Record<string, PoolStat> = {};
for (const [ key, ref ] of clients) {
const pool = ref.deref();
Expand All @@ -228,7 +228,7 @@ export class HttpClient extends EventEmitter {
return await this.#requestInternal<T>(url, options);
}

// alias to request, keep compatible with urlib@2 HttpClient.curl
// alias to request, keep compatible with urllib@2 HttpClient.curl
async curl<T = any>(url: RequestURL, options?: RequestOptions) {
return await this.request<T>(url, options);
}
Expand Down
22 changes: 13 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@ import { HttpClient, HEADER_USER_AGENT } from './HttpClient.js';
import { RequestOptions, RequestURL } from './Request.js';

let httpClient: HttpClient;
const domainSocketHttpclients = new LRU(50);
const domainSocketHttpClients = new LRU(50);

export function getDefaultHttpClient(): HttpClient {
if (!httpClient) {
httpClient = new HttpClient();
}
return httpClient;
}

export async function request<T = any>(url: RequestURL, options?: RequestOptions) {
if (options?.socketPath) {
let domainSocketHttpclient = domainSocketHttpclients.get<HttpClient>(options.socketPath);
let domainSocketHttpclient = domainSocketHttpClients.get<HttpClient>(options.socketPath);

Check warning on line 17 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L17

Added line #L17 was not covered by tests
if (!domainSocketHttpclient) {
domainSocketHttpclient = new HttpClient({
connect: { socketPath: options.socketPath },
});
domainSocketHttpclients.set(options.socketPath, domainSocketHttpclient);
domainSocketHttpClients.set(options.socketPath, domainSocketHttpclient);

Check warning on line 22 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L22

Added line #L22 was not covered by tests
}
return await domainSocketHttpclient.request<T>(url, options);
}

if (!httpClient) {
httpClient = new HttpClient({});
}
return await httpClient.request<T>(url, options);
return await getDefaultHttpClient().request<T>(url, options);
}

// export curl method is keep compatible with urllib.curl()
Expand All @@ -36,12 +40,12 @@ export {
MockAgent, ProxyAgent, Agent, Dispatcher,
setGlobalDispatcher, getGlobalDispatcher,
} from 'undici';
// HttpClient2 is keep compatible with urlib@2 HttpClient2
// HttpClient2 is keep compatible with urllib@2 HttpClient2
export {
HttpClient, HttpClient as HttpClient2, HEADER_USER_AGENT as USER_AGENT,
RequestDiagnosticsMessage, ResponseDiagnosticsMessage,
} from './HttpClient.js';
// RequestOptions2 is keep compatible with urlib@2 RequestOptions2
// RequestOptions2 is keep compatible with urllib@2 RequestOptions2
export {
RequestOptions, RequestOptions as RequestOptions2, RequestURL, HttpMethod,
FixJSONCtlCharsHandler, FixJSONCtlChars,
Expand Down
3 changes: 2 additions & 1 deletion test/esm/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { strict as assert } from 'assert';
import * as urllibStar from 'urllib';
import urllib from 'urllib';
import { request, HttpClient, USER_AGENT } from 'urllib';
import { request, HttpClient, USER_AGENT, getDefaultHttpClient } from 'urllib';

console.log(urllibStar);
console.log(urllibStar.request, urllibStar.HttpClient);
console.log(urllibStar.request, urllibStar.HttpClient);
console.log(urllibStar.USER_AGENT, urllib.USER_AGENT, USER_AGENT);
console.log(request, HttpClient);
console.log('stats %o', getDefaultHttpClient().getDispatcherPoolStats());

assert(urllibStar);
assert.equal(typeof urllibStar.request, 'function');
Expand Down
15 changes: 14 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { strict as assert } from 'node:assert';
import { parse as urlparse } from 'node:url';
import { readFileSync } from 'node:fs';
import { describe, it, beforeAll, afterAll, afterEach, beforeEach } from 'vitest';
import urllib, { HttpClient } from '../src';
import urllib, { HttpClient, getDefaultHttpClient } from '../src';
import { MockAgent, setGlobalDispatcher, getGlobalDispatcher } from '../src';
import { startServer } from './fixtures/server';
import { readableToBytes } from './utils';
Expand All @@ -20,6 +20,19 @@ describe('index.test.ts', () => {
await close();
});

describe('getDefaultHttpClient()', () => {
it('should work', async () => {
const response = await getDefaultHttpClient().request(`${_url}html`);
assert.equal(response.status, 200);
assert.equal(response.headers['content-type'], 'text/html');
assert(response.headers.date);
assert.equal(response.url, `${_url}html`);
assert(!response.redirected);
assert.equal(getDefaultHttpClient(), getDefaultHttpClient());
console.log('stats %o', getDefaultHttpClient().getDispatcherPoolStats());
});
});

describe('urllib.request()', () => {
it('should work', async () => {
const response = await urllib.request(`${_url}html`);
Expand Down

0 comments on commit ba4bd43

Please sign in to comment.