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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@types/qs": "^6.9.7",
"@types/selfsigned": "^2.0.1",
"@types/tar-stream": "^2.2.2",
"@vitest/coverage-v8": "^1.3.1",
"@vitest/coverage-v8": "^3.0.0",
"busboy": "^1.6.0",
"cross-env": "^7.0.3",
"eslint": "^8.25.0",
Expand All @@ -83,7 +83,7 @@
"tshy": "^1.0.0",
"tshy-after": "^1.0.0",
"typescript": "^5.0.4",
"vitest": "^1.3.1"
"vitest": "^3.0.0"
},
"engines": {
"node": ">= 14.19.3"
Expand Down
22 changes: 18 additions & 4 deletions src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import {
request as undiciRequest,
Dispatcher,
Agent,
getGlobalDispatcher,
Pool,
getGlobalDispatcher,
MockAgent,
} from 'undici';
import undiciSymbols from 'undici/lib/core/symbols.js';
import { FormData as FormDataNode } from 'formdata-node';
Expand Down Expand Up @@ -207,10 +208,23 @@ export class HttpClient extends EventEmitter {
}

getDispatcher() {
return this.#dispatcher ?? getGlobalDispatcher();
if (this.#dispatcher) {
return this.#dispatcher;
}
// In a multi-version undici environment
// the global dispatcher is the highest version of undici
// which will conflict with the maxRedirects field and report an error
// so we need to create it that use 5.x version
const globalDispatcher = getGlobalDispatcher();
if (!(globalDispatcher instanceof Agent) && !(globalDispatcher instanceof MockAgent)) {
const dispatcher = globalDispatcher.constructor.name === 'MockAgent' ? new MockAgent() : new Agent();
this.setDispatcher(dispatcher);
return dispatcher;
}
return globalDispatcher;
}

setDispatcher(dispatcher: Dispatcher) {
setDispatcher(dispatcher?: Dispatcher) {
this.#dispatcher = dispatcher;
}

Expand Down Expand Up @@ -415,7 +429,7 @@ export class HttpClient extends EventEmitter {
headers,
bodyTimeout,
opaque: internalOpaque,
dispatcher: args.dispatcher ?? this.#dispatcher,
dispatcher: args.dispatcher ?? this.getDispatcher(),
signal: args.signal,
};
if (typeof args.highWaterMark === 'number') {
Expand Down
13 changes: 11 additions & 2 deletions test/diagnostics_channel.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { strict as assert } from 'node:assert';
import diagnosticsChannel from 'node:diagnostics_channel';
import { describe, it, beforeEach, afterEach } from 'vitest';
import urllib from '../src';
import type {
import urllib, { getGlobalDispatcher, setGlobalDispatcher } from '../src';
import {
MockAgent,
RequestDiagnosticsMessage,
ResponseDiagnosticsMessage,
} from '../src';
Expand All @@ -13,14 +14,22 @@ import { sleep } from './utils';
describe('diagnostics_channel.test.ts', () => {
let close: any;
let _url: string;

let mockAgent: MockAgent;
const globalAgent = getGlobalDispatcher();

beforeEach(async () => {
const { closeServer, url } = await startServer();
close = closeServer;
_url = url;
mockAgent = new MockAgent();
setGlobalDispatcher(mockAgent);
});

afterEach(async () => {
await close();
setGlobalDispatcher(globalAgent);
await mockAgent.close();
});

it('should support trace socket info by undici:client:sendHeaders and undici:request:trailers', async () => {
Expand Down
4 changes: 4 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,15 @@ describe('index.test.ts', () => {
const globalAgent = getGlobalDispatcher();
beforeEach(() => {
mockAgent = new MockAgent();
const httpClient = getDefaultHttpClient();
httpClient.setDispatcher(mockAgent);
setGlobalDispatcher(mockAgent);
});

afterEach(async () => {
setGlobalDispatcher(globalAgent);
const httpClient = getDefaultHttpClient();
httpClient.setDispatcher();
await mockAgent.close();
});

Expand Down
9 changes: 8 additions & 1 deletion test/options.retry.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { strict as assert } from 'node:assert';
import { createWriteStream, createReadStream } from 'node:fs';
import { describe, it, beforeAll, afterAll, beforeEach, afterEach } from 'vitest';
import urllib from '../src';
import urllib, { getGlobalDispatcher, MockAgent, setGlobalDispatcher } from '../src';
import { startServer } from './fixtures/server';
import { readableToString, createTempfile } from './utils';

Expand All @@ -11,6 +11,9 @@ describe('options.retry.test.ts', () => {
let tmpfile: string;
let cleanup: any;

let mockAgent: MockAgent;
const globalAgent = getGlobalDispatcher();

beforeAll(async () => {
const { closeServer, url } = await startServer();
close = closeServer;
Expand All @@ -24,9 +27,13 @@ describe('options.retry.test.ts', () => {
const item = await createTempfile();
tmpfile = item.tmpfile;
cleanup = item.cleanup;
mockAgent = new MockAgent();
setGlobalDispatcher(mockAgent);
});
afterEach(async () => {
await cleanup();
setGlobalDispatcher(globalAgent);
await mockAgent.close();
});

it('should not retry on 400', async () => {
Expand Down
9 changes: 8 additions & 1 deletion test/options.timing.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { strict as assert } from 'node:assert';
import { describe, it, beforeAll, afterAll } from 'vitest';
import urllib from '../src';
import urllib, { getGlobalDispatcher, MockAgent, setGlobalDispatcher } from '../src';
import { RawResponseWithMeta } from '../src/Response';
import { startServer } from './fixtures/server';
import { sleep } from './utils';

describe('options.timing.test.ts', () => {
let close: any;
let _url: string;

let mockAgent: MockAgent;
const globalAgent = getGlobalDispatcher();
beforeAll(async () => {
const { closeServer, url } = await startServer();
close = closeServer;
_url = url;
mockAgent = new MockAgent();
setGlobalDispatcher(mockAgent);
});

afterAll(async () => {
await close();
setGlobalDispatcher(globalAgent);
await mockAgent.close();
});

it('should timing = true work', async () => {
Expand Down
Loading