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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ console.log('status: %s, body size: %d, headers: %j', res.status, data.length, r
- **_streaming_** Boolean - lets you get the `res` object when request connected, default `false`. alias `customResponse`
- **_compressed_** Boolean - Accept `gzip, br` response content and auto decode it, default is `false`.
- **_timing_** Boolean - Enable timing or not, default is `true`.
- **_rejectUnauthorized_** Boolean - Verify the server certificate. Default is `true`. Set `false` to allow self-signed certificates. Applies to top-level `request()` and `curl()` when neither `dispatcher` nor `socketPath` is set. For `HttpClient` instances, use `connect.rejectUnauthorized`; custom dispatchers control their own TLS settings.
- **_socketPath_** String | null - request a unix socket service, default is `null`.
- **_highWaterMark_** Number - default is `67108864`, 64 KiB.

Expand Down
8 changes: 8 additions & 0 deletions src/Request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ export type RequestOptions = {
ctx?: unknown;
/** Request dispatcher, default is getGlobalDispatcher() */
dispatcher?: Dispatcher;
/**
* Verify the server certificate for top-level `request()` and `curl()`. Default: `true`. Set `false` to allow
* self-signed certificates.
*
* Applies when neither `dispatcher` nor `socketPath` is set. For `HttpClient` instances, configure
* `connect.rejectUnauthorized` instead. Custom dispatchers control their own TLS settings.
*/
rejectUnauthorized?: boolean;
/**
* Negotiate HTTP/2 with capable servers via ALPN. Enabled by default since undici@8; set `false` to force HTTP/1.1
* for this request without bypassing the active dispatcher.
Expand Down
16 changes: 2 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,7 @@ export function getDefaultHttpClient(rejectUnauthorized?: boolean, allowH2?: boo
return client;
}

interface UrllibRequestOptions extends RequestOptions {
/**
* If `true`, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if
* verification fails. Default: `true`
*/
rejectUnauthorized?: boolean;
// `allowH2` is inherited from RequestOptions.
}

export async function request<T = any>(
url: RequestURL,
options?: UrllibRequestOptions,
): Promise<HttpClientResponse<T>> {
export async function request<T = any>(url: RequestURL, options?: RequestOptions): Promise<HttpClientResponse<T>> {
if (options?.socketPath) {
let domainSocketHttpclient = domainSocketHttpClients.get<HttpClient>(options.socketPath);
if (!domainSocketHttpclient) {
Expand All @@ -63,7 +51,7 @@ export async function request<T = any>(
// import * as urllib from 'urllib';
// urllib.curl(url);
// ```
export async function curl<T = any>(url: RequestURL, options?: UrllibRequestOptions): Promise<HttpClientResponse<T>> {
export async function curl<T = any>(url: RequestURL, options?: RequestOptions): Promise<HttpClientResponse<T>> {
return await request<T>(url, options);
}

Expand Down
10 changes: 8 additions & 2 deletions test/fixtures/ts-esm/hello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import type { HttpClientResponse } from 'urllib';
import urllib from 'urllib';
import * as urllib2 from 'urllib';

async function request(url: RequestURL, options: RequestOptions): Promise<HttpClientResponse> {
async function request(
url: RequestURL,
options: RequestOptions = { rejectUnauthorized: false },
): Promise<HttpClientResponse> {
return await urllib.request(url, options);
}

async function request2(url: RequestURL, options: RequestOptions2): Promise<HttpClientResponse> {
async function request2(
url: RequestURL,
options: RequestOptions2 = { rejectUnauthorized: true },
): Promise<HttpClientResponse> {
return await urllib2.curl(url, options);
}

Expand Down
16 changes: 13 additions & 3 deletions test/urllib.options.rejectUnauthorized-false.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import selfsigned from 'selfsigned';
import { describe, it, beforeAll, afterAll } from 'vite-plus/test';

import urllib, { HttpClient } from '../src/index.js';
import type { RequestOptions } from '../src/index.js';
import { startServer } from './fixtures/server.js';

describe('urllib.options.rejectUnauthorized-false.test.ts', () => {
Expand All @@ -22,13 +23,22 @@ describe('urllib.options.rejectUnauthorized-false.test.ts', () => {
await close();
});

it('should 200 on options.rejectUnauthorized = false', async () => {
const response = await urllib.request(_url, {
it.each(['request', 'curl'] as const)('should honor typed rejectUnauthorized on urllib.%s', async (method) => {
const options: RequestOptions = {
rejectUnauthorized: false,
dataType: 'json',
});
};
const response = await urllib[method](_url, options);
assert.equal(response.status, 200);
assert.equal(response.data.method, 'GET');

// An unverified connection must not be reused by requests that verify certificates.
for (const rejectUnauthorized of [true, undefined]) {
const secureOptions: RequestOptions = { rejectUnauthorized };
await assert.rejects(urllib[method](_url, secureOptions), {
code: 'DEPTH_ZERO_SELF_SIGNED_CERT',
});
}
});

it('should 200 with H2 on options.rejectUnauthorized = false', async () => {
Expand Down
Loading