Skip to content

Commit

Permalink
Refine internal TS export/imports to be more robust (#1769)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Nov 13, 2022
1 parent 93fd794 commit a3ce745
Show file tree
Hide file tree
Showing 29 changed files with 118 additions and 114 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -78,3 +78,4 @@ fuzz-results-*.json

# Bundle output
undici-fetch.js
/test/imports/undici-import.js
46 changes: 23 additions & 23 deletions index.d.ts
@@ -1,19 +1,19 @@
import Dispatcher = require('./types/dispatcher')
import Dispatcher from'./types/dispatcher'
import { setGlobalDispatcher, getGlobalDispatcher } from './types/global-dispatcher'
import { setGlobalOrigin, getGlobalOrigin } from './types/global-origin'
import Pool = require('./types/pool')
import Pool from'./types/pool'
import { RedirectHandler, DecoratorHandler } from './types/handlers'

import BalancedPool = require('./types/balanced-pool')
import Client = require('./types/client')
import buildConnector = require('./types/connector')
import errors = require('./types/errors')
import Agent = require('./types/agent')
import MockClient = require('./types/mock-client')
import MockPool = require('./types/mock-pool')
import MockAgent = require('./types/mock-agent')
import mockErrors = require('./types/mock-errors')
import ProxyAgent = require('./types/proxy-agent')
import BalancedPool from './types/balanced-pool'
import Client from'./types/client'
import buildConnector from'./types/connector'
import errors from'./types/errors'
import Agent from'./types/agent'
import MockClient from'./types/mock-client'
import MockPool from'./types/mock-pool'
import MockAgent from'./types/mock-agent'
import mockErrors from'./types/mock-errors'
import ProxyAgent from'./types/proxy-agent'
import { request, pipeline, stream, connect, upgrade } from './types/api'

export * from './types/fetch'
Expand All @@ -27,26 +27,26 @@ export { Dispatcher, BalancedPool, Pool, Client, buildConnector, errors, Agent,
export default Undici

declare namespace Undici {
var Dispatcher: typeof import('./types/dispatcher')
var Pool: typeof import('./types/pool');
var Dispatcher: typeof import('./types/dispatcher').default
var Pool: typeof import('./types/pool').default;
var RedirectHandler: typeof import ('./types/handlers').RedirectHandler
var DecoratorHandler: typeof import ('./types/handlers').DecoratorHandler
var createRedirectInterceptor: typeof import ('./types/interceptors').createRedirectInterceptor
var BalancedPool: typeof import('./types/balanced-pool');
var Client: typeof import('./types/client');
var buildConnector: typeof import('./types/connector');
var errors: typeof import('./types/errors');
var Agent: typeof import('./types/agent');
var BalancedPool: typeof import('./types/balanced-pool').default;
var Client: typeof import('./types/client').default;
var buildConnector: typeof import('./types/connector').default;
var errors: typeof import('./types/errors').default;
var Agent: typeof import('./types/agent').default;
var setGlobalDispatcher: typeof import('./types/global-dispatcher').setGlobalDispatcher;
var getGlobalDispatcher: typeof import('./types/global-dispatcher').getGlobalDispatcher;
var request: typeof import('./types/api').request;
var stream: typeof import('./types/api').stream;
var pipeline: typeof import('./types/api').pipeline;
var connect: typeof import('./types/api').connect;
var upgrade: typeof import('./types/api').upgrade;
var MockClient: typeof import('./types/mock-client');
var MockPool: typeof import('./types/mock-pool');
var MockAgent: typeof import('./types/mock-agent');
var mockErrors: typeof import('./types/mock-errors');
var MockClient: typeof import('./types/mock-client').default;
var MockPool: typeof import('./types/mock-pool').default;
var MockAgent: typeof import('./types/mock-agent').default;
var mockErrors: typeof import('./types/mock-errors').default;
var fetch: typeof import('./types/fetch').fetch;
}
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -52,7 +52,7 @@
"test:jest": "node scripts/verifyVersion.js 14 || jest",
"test:tap": "tap test/*.js test/diagnostics-channel/*.js",
"test:tdd": "tap test/*.js test/diagnostics-channel/*.js -w",
"test:typescript": "tsd",
"test:typescript": "tsd && tsc test/imports/undici-import.ts",
"test:wpt": "node scripts/verifyVersion 18 || (node test/wpt/start-fetch.mjs && node test/wpt/start-FileAPI.mjs && node test/wpt/start-mimesniff.mjs && node test/wpt/start-xhr.mjs)",
"coverage": "nyc --reporter=text --reporter=html npm run test",
"coverage:ci": "nyc --reporter=lcov npm run test",
Expand Down Expand Up @@ -95,6 +95,7 @@
"table": "^6.8.0",
"tap": "^16.1.0",
"tsd": "^0.24.1",
"typescript": "^4.8.4",
"wait-on": "^6.0.0"
},
"engines": {
Expand Down
5 changes: 5 additions & 0 deletions test/imports/undici-import.ts
@@ -0,0 +1,5 @@
import { request } from '../../'

async function exampleCode() {
await request('http://localhost:3000/foo')
}
8 changes: 4 additions & 4 deletions test/types/dispatcher.events.test-d.ts
@@ -1,12 +1,12 @@
import { Dispatcher } from '../..'
import {expectAssignable} from "tsd";
import {URL} from "url";
import {UndiciError} from "../../types/errors";
import Errors from "../../types/errors";

interface EventHandler {
connect(origin: URL, targets: readonly Dispatcher[]): void
disconnect(origin: URL, targets: readonly Dispatcher[], error: UndiciError): void
connectionError(origin: URL, targets: readonly Dispatcher[], error: UndiciError): void
disconnect(origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError): void
connectionError(origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError): void
drain(origin: URL): void
}

Expand Down Expand Up @@ -36,7 +36,7 @@ interface EventHandler {

const origin = new URL('')
const targets = new Array<Dispatcher>()
const error = new UndiciError()
const error = new Errors.UndiciError()
expectAssignable<boolean>(dispatcher.emit('connect', origin, targets))
expectAssignable<boolean>(dispatcher.emit('disconnect', origin, targets, error))
expectAssignable<boolean>(dispatcher.emit('connectionError', origin, targets, error))
Expand Down
4 changes: 2 additions & 2 deletions test/types/errors.test-d.ts
@@ -1,6 +1,6 @@
import { expectAssignable } from 'tsd'
import { errors } from '../..'
import { SocketInfo } from '../../types/client'
import Client from '../../types/client'

expectAssignable<errors.UndiciError>(new errors.UndiciError())

Expand Down Expand Up @@ -53,7 +53,7 @@ expectAssignable<errors.UndiciError>(new errors.SocketError())
expectAssignable<errors.SocketError>(new errors.SocketError())
expectAssignable<'SocketError'>(new errors.SocketError().name)
expectAssignable<'UND_ERR_SOCKET'>(new errors.SocketError().code)
expectAssignable<SocketInfo | null>(new errors.SocketError().socket)
expectAssignable<Client.SocketInfo | null>(new errors.SocketError().socket)

expectAssignable<errors.UndiciError>(new errors.NotSupportedError())
expectAssignable<errors.NotSupportedError>(new errors.NotSupportedError())
Expand Down
4 changes: 2 additions & 2 deletions test/types/formdata.test-d.ts
Expand Up @@ -2,9 +2,9 @@ import { Blob } from 'buffer'
import { Readable } from 'stream'
import { expectAssignable, expectType } from 'tsd'
import { File, FormData, SpecIterableIterator } from '../..'
import { DispatchOptions } from '../../types/dispatcher'
import Dispatcher from '../../types/dispatcher'

declare const dispatcherOptions: DispatchOptions
declare const dispatcherOptions: Dispatcher.DispatchOptions

declare const blob: Blob
const formData = new FormData()
Expand Down
4 changes: 2 additions & 2 deletions test/types/interceptor.test-d.ts
@@ -1,5 +1,5 @@
import {expectAssignable} from "tsd";
import Undici from "../..";
import Dispatcher, {DispatchInterceptor} from "../../types/dispatcher";
import Dispatcher from "../../types/dispatcher";

expectAssignable<DispatchInterceptor>(Undici.createRedirectInterceptor({ maxRedirections: 3 }))
expectAssignable<Dispatcher.DispatchInterceptor>(Undici.createRedirectInterceptor({ maxRedirections: 3 }))
2 changes: 1 addition & 1 deletion test/types/readable.test-d.ts
@@ -1,5 +1,5 @@
import { expectAssignable } from 'tsd'
import BodyReadable = require('../../types/readable')
import BodyReadable from '../../types/readable'
import { Blob } from 'buffer'

expectAssignable<BodyReadable>(new BodyReadable())
Expand Down
9 changes: 4 additions & 5 deletions types/agent.d.ts
@@ -1,9 +1,8 @@
import { URL } from 'url'
import Dispatcher = require('./dispatcher')
import Pool = require('./pool')
import {DispatchInterceptor} from "./dispatcher";
import Pool from './pool'
import Dispatcher from "./dispatcher";

export = Agent
export default Agent

declare class Agent extends Dispatcher{
constructor(opts?: Agent.Options)
Expand All @@ -22,7 +21,7 @@ declare namespace Agent {
/** Integer. Default: `0` */
maxRedirections?: number;

interceptors?: { Agent?: readonly DispatchInterceptor[] } & Pool.Options["interceptors"]
interceptors?: { Agent?: readonly Dispatcher.DispatchInterceptor[] } & Pool.Options["interceptors"]
}

export interface DispatchOptions extends Dispatcher.DispatchOptions {
Expand Down
2 changes: 1 addition & 1 deletion types/api.d.ts
@@ -1,6 +1,6 @@
import { URL, UrlObject } from 'url'
import { Duplex } from 'stream'
import Dispatcher = require('./dispatcher')
import Dispatcher from './dispatcher'

export {
request,
Expand Down
7 changes: 3 additions & 4 deletions types/balanced-pool.d.ts
@@ -1,9 +1,8 @@
import Client = require('./client')
import Pool = require('./pool')
import Dispatcher = require('./dispatcher')
import Pool from './pool'
import Dispatcher from './dispatcher'
import { URL } from 'url'

export = BalancedPool
export default BalancedPool

declare class BalancedPool extends Dispatcher {
constructor(url: string | URL | string[], options?: Pool.Options);
Expand Down
10 changes: 5 additions & 5 deletions types/client.d.ts
@@ -1,10 +1,10 @@
import { URL } from 'url'
import { TlsOptions } from 'tls'
import Dispatcher = require('./dispatcher')
import {DispatchInterceptor} from './dispatcher'
import buildConnector, {connector} from "./connector";
import Dispatcher from './dispatcher'
import DispatchInterceptor from './dispatcher'
import buildConnector from "./connector";

export = Client
export default Client

/** A basic HTTP/1.1 client, mapped on top a single TCP/TLS connection. Pipelining is disabled by default. */
declare class Client extends Dispatcher {
Expand All @@ -28,7 +28,7 @@ declare namespace Client {
/** The amount of concurrent requests to be sent over the single TCP/TLS connection according to [RFC7230](https://tools.ietf.org/html/rfc7230#section-6.3.2). Default: `1`. */
pipelining?: number | null;
/** **/
connect?: buildConnector.BuildOptions | connector | null;
connect?: buildConnector.BuildOptions | buildConnector.connector | null;
/** The maximum length of request headers in bytes. Default: `16384` (16KiB). */
maxHeaderSize?: number | null;
/** The timeout after which a request will time out, in milliseconds. Monitors time between receiving body data. Use `0` to disable it entirely. Default: `30e3` milliseconds (30s). */
Expand Down
6 changes: 3 additions & 3 deletions types/connector.d.ts
@@ -1,7 +1,7 @@
import {TLSSocket, ConnectionOptions} from 'tls'
import {IpcNetConnectOpts, Socket, TcpNetConnectOpts} from 'net'
import { TLSSocket, ConnectionOptions } from 'tls'
import { IpcNetConnectOpts, Socket, TcpNetConnectOpts } from 'net'

export = buildConnector
export default buildConnector
declare function buildConnector (options?: buildConnector.BuildOptions): buildConnector.connector

declare namespace buildConnector {
Expand Down
8 changes: 4 additions & 4 deletions types/diagnostics-channel.d.ts
@@ -1,13 +1,13 @@
import { Socket } from "net";
import { URL } from "url";
import { connector } from "./connector";
import { HttpMethod } from "./dispatcher";
import Connector from "./connector";
import Dispatcher from "./dispatcher";

declare namespace DiagnosticsChannel {
interface Request {
origin?: string | URL;
completed: boolean;
method?: HttpMethod;
method?: Dispatcher.HttpMethod;
path: string;
headers: string;
addHeader(key: string, value: string): Request;
Expand All @@ -25,7 +25,7 @@ declare namespace DiagnosticsChannel {
port: URL["port"];
servername: string | null;
}
type Connector = connector;
type Connector = Connector.connector;
export interface RequestCreateMessage {
request: Request;
}
Expand Down
46 changes: 23 additions & 23 deletions types/dispatcher.d.ts
Expand Up @@ -3,13 +3,13 @@ import { Duplex, Readable, Writable } from 'stream'
import { EventEmitter } from 'events'
import { IncomingHttpHeaders } from 'http'
import { Blob } from 'buffer'
import type BodyReadable from './readable'
import BodyReadable from './readable'
import { FormData } from './formdata'
import { UndiciError } from './errors'
import Errors from './errors'

type AbortSignal = unknown;

export = Dispatcher;
export default Dispatcher

/** Dispatcher is the core API used to dispatch requests. */
declare class Dispatcher extends EventEmitter {
Expand Down Expand Up @@ -39,56 +39,56 @@ declare class Dispatcher extends EventEmitter {
destroy(err: Error | null, callback: () => void): void;

on(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
on(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
on(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
on(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
on(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
on(eventName: 'drain', callback: (origin: URL) => void): this;


once(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
once(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
once(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
once(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
once(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
once(eventName: 'drain', callback: (origin: URL) => void): this;


off(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
off(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
off(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
off(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
off(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
off(eventName: 'drain', callback: (origin: URL) => void): this;


addListener(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
addListener(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
addListener(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
addListener(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
addListener(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
addListener(eventName: 'drain', callback: (origin: URL) => void): this;

removeListener(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
removeListener(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
removeListener(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
removeListener(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
removeListener(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
removeListener(eventName: 'drain', callback: (origin: URL) => void): this;

prependListener(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
prependListener(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
prependListener(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
prependListener(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
prependListener(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
prependListener(eventName: 'drain', callback: (origin: URL) => void): this;

prependOnceListener(eventName: 'connect', callback: (origin: URL, targets: readonly Dispatcher[]) => void): this;
prependOnceListener(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
prependOnceListener(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void): this;
prependOnceListener(eventName: 'disconnect', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
prependOnceListener(eventName: 'connectionError', callback: (origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void): this;
prependOnceListener(eventName: 'drain', callback: (origin: URL) => void): this;

listeners(eventName: 'connect'): ((origin: URL, targets: readonly Dispatcher[]) => void)[]
listeners(eventName: 'disconnect'): ((origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void)[];
listeners(eventName: 'connectionError'): ((origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void)[];
listeners(eventName: 'disconnect'): ((origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void)[];
listeners(eventName: 'connectionError'): ((origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void)[];
listeners(eventName: 'drain'): ((origin: URL) => void)[];

rawListeners(eventName: 'connect'): ((origin: URL, targets: readonly Dispatcher[]) => void)[]
rawListeners(eventName: 'disconnect'): ((origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void)[];
rawListeners(eventName: 'connectionError'): ((origin: URL, targets: readonly Dispatcher[], error: UndiciError) => void)[];
rawListeners(eventName: 'disconnect'): ((origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void)[];
rawListeners(eventName: 'connectionError'): ((origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError) => void)[];
rawListeners(eventName: 'drain'): ((origin: URL) => void)[];

emit(eventName: 'connect', origin: URL, targets: readonly Dispatcher[]): boolean;
emit(eventName: 'disconnect', origin: URL, targets: readonly Dispatcher[], error: UndiciError): boolean;
emit(eventName: 'connectionError', origin: URL, targets: readonly Dispatcher[], error: UndiciError): boolean;
emit(eventName: 'disconnect', origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError): boolean;
emit(eventName: 'connectionError', origin: URL, targets: readonly Dispatcher[], error: Errors.UndiciError): boolean;
emit(eventName: 'drain', origin: URL): boolean;
}

Expand Down
6 changes: 3 additions & 3 deletions types/errors.d.ts
@@ -1,7 +1,7 @@
import {IncomingHttpHeaders} from "http";
import Client from './client'

export = Errors
import { SocketInfo } from './client'
export default Errors

declare namespace Errors {
export class UndiciError extends Error { }
Expand Down Expand Up @@ -79,7 +79,7 @@ declare namespace Errors {
export class SocketError extends UndiciError {
name: 'SocketError';
code: 'UND_ERR_SOCKET';
socket: SocketInfo | null
socket: Client.SocketInfo | null
}

/** Encountered unsupported functionality. */
Expand Down
2 changes: 1 addition & 1 deletion types/fetch.d.ts
Expand Up @@ -7,7 +7,7 @@ import { URL, URLSearchParams } from 'url'
import { ReadableStream } from 'stream/web'
import { FormData } from './formdata'

import Dispatcher = require('./dispatcher')
import Dispatcher from './dispatcher'

export type RequestInfo = string | URL | Request

Expand Down
2 changes: 1 addition & 1 deletion types/global-dispatcher.d.ts
@@ -1,4 +1,4 @@
import Dispatcher = require("./dispatcher");
import Dispatcher from "./dispatcher";

export {
getGlobalDispatcher,
Expand Down

0 comments on commit a3ce745

Please sign in to comment.