Skip to content

Commit

Permalink
fix(client): 2dn attempt to defer undici
Browse files Browse the repository at this point in the history
  • Loading branch information
millsp committed Mar 22, 2022
1 parent f1bacfb commit 4a1e395
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions packages/engine-core/src/binary/Connection.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import getStream from 'get-stream'
import type { IncomingHttpHeaders } from 'http'
import type { Pool } from 'undici'
import type { Dispatcher, Pool } from 'undici'
import type { URL } from 'url'

export type Result<R> = {
statusCode: number
headers: IncomingHttpHeaders
statusCode: Dispatcher.ResponseData['statusCode']
headers: Dispatcher.ResponseData['headers']
data: R
}

Expand All @@ -23,7 +22,6 @@ function assertHasPool<A>(pool: A): asserts pool is NonNullable<A> {
* Open an HTTP connection pool
*/
export class Connection {
private _undici = import('undici')
private _pool: Pool | undefined

constructor() {}
Expand Down Expand Up @@ -54,11 +52,13 @@ export class Connection {
async open(url: string | URL, options?: Pool.Options) {
if (this._pool) return

const undici = await this._undici
const undici = await import('undici')

this._pool = new undici.Pool(url, {
connections: 1000,
keepAliveMaxTimeout: 600e3,
headersTimeout: 0,
bodyTimeout: 0,
...options,
})
}
Expand All @@ -71,7 +71,12 @@ export class Connection {
* @param body
* @returns
*/
async raw<R>(method: 'POST' | 'GET', endpoint: string, headers?: IncomingHttpHeaders, body?: string) {
async raw<R>(
method: 'POST' | 'GET',
endpoint: string,
headers?: Dispatcher.DispatchOptions['headers'],
body?: Dispatcher.DispatchOptions['body'],
) {
assertHasPool(this._pool)

const response = await this._pool.request({
Expand Down Expand Up @@ -100,7 +105,11 @@ export class Connection {
* @param headers
* @returns
*/
post<R>(endpoint: string, body?: string, headers?: IncomingHttpHeaders) {
post<R>(
endpoint: string,
body?: Dispatcher.DispatchOptions['body'],
headers?: Dispatcher.DispatchOptions['headers'],
) {
return this.raw<R>('POST', endpoint, headers, body)
}

Expand All @@ -111,7 +120,7 @@ export class Connection {
* @param headers
* @returns
*/
get<R>(path: string, headers?: IncomingHttpHeaders) {
get<R>(path: string, headers?: Dispatcher.DispatchOptions['headers']) {
return this.raw<R>('GET', path, headers)
}

Expand Down

0 comments on commit 4a1e395

Please sign in to comment.