import net from 'node:net';
import type { IncomingHttpHeaders } from 'node:http2';
import { type Readable } from 'node:stream';
import { type ValueOf } from './prototype.ts';
import type { Encoding } from './file.ts';
import { type Deferred2, type OnTimeout } from './utils.ts';
import { type BasicAuth, type BearerAuth } from './net.common.ts';
export * from './net.common.ts';
export declare const MyProxy: {
readonly socks5: 'socks5://127.0.0.1:10080';
readonly http: 'http://127.0.0.1:10080';
readonly work: 'socks5://127.0.0.1:9443';
};
export declare const byproxy: {
readonly proxy: true;
};
export declare const bywork: {
readonly proxy: "socks5://127.0.0.1:9443";
};
export interface RawResponse {
status: number;
/** 只有 set-cookie 头可能是 string[], 其他都是 string | undefined (用 `, ` (逗号空格) 分割) */
headers: IncomingHttpHeaders;
body: Readable;
}
export interface FullResponse
{
status: number;
/** 只有 set-cookie 头可能是 string[], 其他都是 string | undefined (用 `, ` (逗号空格) 分割) */
headers: IncomingHttpHeaders;
body: TBody;
}
export interface RequestOptions {
method?: 'GET' | 'POST' | 'PUT' | 'HEAD' | 'DELETE' | 'PATCH';
queries?: Record;
headers?: Record;
body?: string | Record | Uint8Array | URLSearchParams | Readable;
type?: 'application/json' | 'application/x-www-form-urlencoded' | 'multipart/form-data' | 'application/jose+json' | 'text/plain';
proxy?: boolean | ValueOf | string;
encoding?: Encoding | 'binary';
retries?: true | number;
timeout?: number;
auth?: BasicAuth | BearerAuth;
redirect?: 'follow' | 'manual';
decode?: boolean;
dryrun?: boolean;
browser?: boolean;
ua?: string;
signal?: AbortSignal;
h2?: boolean;
milo?: boolean;
print?: {
retry: boolean;
timeout: boolean;
};
}
export interface RequestFullOptions extends RequestOptions {
full: true;
}
export interface RequestRawOptions extends RequestOptions {
raw: true;
}
export interface RequestError extends Error {
url: URL;
options: RequestOptions | RequestFullOptions | RequestRawOptions;
response?: FullResponse;
}
export declare class StatusCodeError extends Error {
name: 'StatusCodeError';
status: number;
constructor(status: number, url: string);
}
/** 发起 http 请求,返回响应
- url: 必须是完整 url
- options?:
- method?: `有 body 时为 POST, 否则为 GET` 'GET' | 'POST' | ··
- queries?: 添加到 url 上的参数,是 Record,true/false 会被转换为 0/1
- headers?: http 请求头,是 Record,其中 key 必须是小写的
- body?: http 请求体,可以是:
- string
- Uint8Array
- Record: 会自动 JSON.stringify
- URLSearchParams: 仅限 type 为 x-www-form-urlencoded
- Record 自动转为 FormData: 仅限 type 为 form-data, 可以传 File 对象设置文件名
- type?: `'application/json'` 有 body 时设置 http 请求头中的 content-type 头
- proxy?: `false` 通过代理发送请求
- 为 true 时使用 MyProxy.socks5
- 为非空 string 作为代理地址
- 为 falsy 值时设为 false
- encoding?: `根据网页 content-type: charset=gb18030 提取 || 'utf-8'` 传入 'binary' 时返回 Buffer
(只对非 raw 的请求起作用)
- retries?: `false` 可以传入 true (默认 2 次) 或 重试次数
- timeout?: `5 * 1000` 超时后会抛出 error.name 为 'TimeoutError' 的 error (但类型不是 utils.ts 中的 TimeoutError), 可以传入 0 来禁用超时
- auth?: BasicAuth | BearerAuth
- raw?: `false` 传入后返回整个 response (RawResponse),body 为 Readable
- full?: `false` 传入后返回整个 response (FullResponse),body 根据 encoding 对应为 string 或 Buffer
- decode?: `true` 根据 content-encoding: br 等解码压缩后的 response.body
- browser?: `false` 使用 chrome 浏览器的 ua
- ua?: `无 user-agent 头` 快捷设置 user-agent 头,默认不发送
- dryrun?: `false` 仅打印 undici options, 并不实际发送请求,返回 undefined
- signal?: AbortSignal
- print?:
- retry: `true` 是否打印 "等待 1 秒后重试 request" 提示信息
- timeout: `true` 是否打印最后一次超时重试失败时的错误 */
export declare function request(url: string | URL): Promise;
export declare function request(url: string | URL, options: RequestRawOptions): Promise;
export declare function request(url: string | URL, options: RequestFullOptions & {
encoding: 'binary';
}): Promise>;
export declare function request(url: string | URL, options: RequestFullOptions): Promise>;
export declare function request(url: string | URL, options: RequestOptions & {
encoding: 'binary';
}): Promise;
export declare function request(url: string | URL, options: RequestOptions): Promise;
/** 发起 http 请求并将响应体作为 json 解析 */
export declare function request_json(url: string | URL, options?: RequestOptions): Promise;
export interface ConnectOptions {
local_port?: number;
timeout?: number;
on_timeout?: OnTimeout;
keep_alive_duration?: number;
print?: {
error: boolean;
connect: boolean;
};
}
/** 建立 tcp 连接,超时时间为 2 秒 (timeout 选项),返回已连接的 socket
连接中遇到的错误会 reject,之后取消监听 error 事件,由调用者 attach 处理
连接超时则自动 socket.resetAndDestroy
- hostname: 要连接到的 ip 或域名
- port: 要连接的端口
- options?:
- local_port?: `随机端口` 使用的本地端口
- timeout?: `2000` 超时时间
- keep_alive_duration?: `undefined` 毫秒数,设置后启用操作系统级的 keep alive
- print?: 打印连接日志,错误日志
- error?: `true`
- connect: `false` */
export declare function connect(hostname: string, port: number, { local_port, timeout: _timeout, on_timeout, keep_alive_duration, print }?: ConnectOptions): Deferred2;
export declare function get_socket_ip(socket: net.Socket): string;