From cfa2b4e4ec52b6f0b5c6ee231e35ee60bac2789f Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Fri, 23 Feb 2024 11:53:31 +0100 Subject: [PATCH] refactor: move files into logical folders (#2813) --- index.js | 14 ++++++------ lib/{ => dispatcher}/agent.js | 8 +++---- lib/{ => dispatcher}/balanced-pool.js | 6 ++--- lib/{ => dispatcher}/client.js | 30 ++++++++++++------------- lib/{ => dispatcher}/dispatcher-base.js | 4 ++-- lib/{ => dispatcher}/dispatcher.js | 0 lib/{ => dispatcher}/pool-base.js | 4 ++-- lib/{ => dispatcher}/pool-stats.js | 2 +- lib/{ => dispatcher}/pool.js | 8 +++---- lib/{ => dispatcher}/proxy-agent.js | 6 ++--- lib/{ => dispatcher}/retry-agent.js | 2 +- lib/global.js | 2 +- lib/mock/mock-agent.js | 4 ++-- lib/mock/mock-client.js | 2 +- lib/mock/mock-pool.js | 2 +- lib/{ => util}/timers.js | 0 test/client-keep-alive.js | 2 +- test/client-reconnect.js | 2 +- test/client-timeout.js | 2 +- test/dispatcher.js | 2 +- test/fetch/fetch-timeouts.js | 2 +- test/mock-agent.js | 2 +- test/mock-client.js | 2 +- test/mock-pool.js | 2 +- test/pool.js | 2 +- test/proxy-agent.js | 4 ++-- test/request-timeout.js | 2 +- test/socket-timeout.js | 2 +- 28 files changed, 60 insertions(+), 60 deletions(-) rename lib/{ => dispatcher}/agent.js (94%) rename lib/{ => dispatcher}/balanced-pool.js (97%) rename lib/{ => dispatcher}/client.js (98%) rename lib/{ => dispatcher}/dispatcher-base.js (98%) rename lib/{ => dispatcher}/dispatcher.js (100%) rename lib/{ => dispatcher}/pool-base.js (97%) rename lib/{ => dispatcher}/pool-stats.js (94%) rename lib/{ => dispatcher}/pool.js (92%) rename lib/{ => dispatcher}/proxy-agent.js (97%) rename lib/{ => dispatcher}/retry-agent.js (91%) rename lib/{ => util}/timers.js (100%) diff --git a/index.js b/index.js index dd52f2e46a1..4c7a979f001 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,13 @@ 'use strict' -const Client = require('./lib/client') -const Dispatcher = require('./lib/dispatcher') +const Client = require('./lib/dispatcher/client') +const Dispatcher = require('./lib/dispatcher/dispatcher') +const Pool = require('./lib/dispatcher/pool') +const BalancedPool = require('./lib/dispatcher/balanced-pool') +const Agent = require('./lib/dispatcher/agent') +const ProxyAgent = require('./lib/dispatcher/proxy-agent') +const RetryAgent = require('./lib/dispatcher/retry-agent') const errors = require('./lib/core/errors') -const Pool = require('./lib/pool') -const BalancedPool = require('./lib/balanced-pool') -const Agent = require('./lib/agent') const util = require('./lib/core/util') const { InvalidArgumentError } = errors const api = require('./lib/api') @@ -14,8 +16,6 @@ const MockClient = require('./lib/mock/mock-client') const MockAgent = require('./lib/mock/mock-agent') const MockPool = require('./lib/mock/mock-pool') const mockErrors = require('./lib/mock/mock-errors') -const ProxyAgent = require('./lib/proxy-agent') -const RetryAgent = require('./lib/retry-agent') const RetryHandler = require('./lib/handler/RetryHandler') const { getGlobalDispatcher, setGlobalDispatcher } = require('./lib/global') const DecoratorHandler = require('./lib/handler/DecoratorHandler') diff --git a/lib/agent.js b/lib/dispatcher/agent.js similarity index 94% rename from lib/agent.js rename to lib/dispatcher/agent.js index 9a07708654a..d91c412382f 100644 --- a/lib/agent.js +++ b/lib/dispatcher/agent.js @@ -1,12 +1,12 @@ 'use strict' -const { InvalidArgumentError } = require('./core/errors') -const { kClients, kRunning, kClose, kDestroy, kDispatch, kInterceptors } = require('./core/symbols') +const { InvalidArgumentError } = require('../core/errors') +const { kClients, kRunning, kClose, kDestroy, kDispatch, kInterceptors } = require('../core/symbols') const DispatcherBase = require('./dispatcher-base') const Pool = require('./pool') const Client = require('./client') -const util = require('./core/util') -const createRedirectInterceptor = require('./interceptor/redirectInterceptor') +const util = require('../core/util') +const createRedirectInterceptor = require('../interceptor/redirectInterceptor') const kOnConnect = Symbol('onConnect') const kOnDisconnect = Symbol('onDisconnect') diff --git a/lib/balanced-pool.js b/lib/dispatcher/balanced-pool.js similarity index 97% rename from lib/balanced-pool.js rename to lib/dispatcher/balanced-pool.js index cf06fcacedc..15a7e7b5879 100644 --- a/lib/balanced-pool.js +++ b/lib/dispatcher/balanced-pool.js @@ -3,7 +3,7 @@ const { BalancedPoolMissingUpstreamError, InvalidArgumentError -} = require('./core/errors') +} = require('../core/errors') const { PoolBase, kClients, @@ -13,8 +13,8 @@ const { kGetDispatcher } = require('./pool-base') const Pool = require('./pool') -const { kUrl, kInterceptors } = require('./core/symbols') -const { parseOrigin } = require('./core/util') +const { kUrl, kInterceptors } = require('../core/symbols') +const { parseOrigin } = require('../core/util') const kFactory = Symbol('factory') const kOptions = Symbol('options') diff --git a/lib/client.js b/lib/dispatcher/client.js similarity index 98% rename from lib/client.js rename to lib/dispatcher/client.js index 7a7260406ef..cb3feabb37b 100644 --- a/lib/client.js +++ b/lib/dispatcher/client.js @@ -8,10 +8,10 @@ const assert = require('node:assert') const net = require('node:net') const http = require('node:http') const { pipeline } = require('node:stream') -const util = require('./core/util') -const { channels } = require('./core/diagnostics') -const timers = require('./timers') -const Request = require('./core/request') +const util = require('../core/util.js') +const { channels } = require('../core/diagnostics.js') +const timers = require('../util/timers.js') +const Request = require('../core/request.js') const DispatcherBase = require('./dispatcher-base') const { RequestContentLengthMismatchError, @@ -26,8 +26,8 @@ const { HTTPParserError, ResponseExceededMaxSizeError, ClientDestroyedError -} = require('./core/errors') -const buildConnector = require('./core/connect') +} = require('../core/errors.js') +const buildConnector = require('../core/connect.js') const { kUrl, kReset, @@ -79,7 +79,7 @@ const { kHTTP2BuildRequest, kHTTP2CopyHeaders, kHTTP1BuildRequest -} = require('./core/symbols') +} = require('../core/symbols.js') /** @type {import('http2')} */ let http2 @@ -112,13 +112,13 @@ const FastBuffer = Buffer[Symbol.species] const kClosedResolve = Symbol('kClosedResolve') /** - * @type {import('../types/client').default} + * @type {import('../../types/client.js').default} */ class Client extends DispatcherBase { /** * * @param {string|URL} url - * @param {import('../types/client').Client.Options} options + * @param {import('../../types/client.js').Client.Options} options */ constructor (url, { interceptors, @@ -472,16 +472,16 @@ function onHTTP2GoAway (code) { resume(client) } -const constants = require('./llhttp/constants') -const createRedirectInterceptor = require('./interceptor/redirectInterceptor') +const constants = require('../llhttp/constants.js') +const createRedirectInterceptor = require('../interceptor/redirectInterceptor.js') const EMPTY_BUF = Buffer.alloc(0) async function lazyllhttp () { - const llhttpWasmData = process.env.JEST_WORKER_ID ? require('./llhttp/llhttp-wasm.js') : undefined + const llhttpWasmData = process.env.JEST_WORKER_ID ? require('../llhttp/llhttp-wasm.js') : undefined let mod try { - mod = await WebAssembly.compile(require('./llhttp/llhttp_simd-wasm.js')) + mod = await WebAssembly.compile(require('../llhttp/llhttp_simd-wasm.js')) } catch (e) { /* istanbul ignore next */ @@ -489,7 +489,7 @@ async function lazyllhttp () { // being enabled, but the occurring of this other error // * https://github.com/emscripten-core/emscripten/issues/11495 // got me to remove that check to avoid breaking Node 12. - mod = await WebAssembly.compile(llhttpWasmData || require('./llhttp/llhttp-wasm.js')) + mod = await WebAssembly.compile(llhttpWasmData || require('../llhttp/llhttp-wasm.js')) } return await WebAssembly.instantiate(mod, { @@ -1500,7 +1500,7 @@ function write (client, request) { if (util.isFormDataLike(body)) { if (!extractBody) { - extractBody = require('./web/fetch/body.js').extractBody + extractBody = require('../web/fetch/body.js').extractBody } const [bodyStream, contentType] = extractBody(body) diff --git a/lib/dispatcher-base.js b/lib/dispatcher/dispatcher-base.js similarity index 98% rename from lib/dispatcher-base.js rename to lib/dispatcher/dispatcher-base.js index 5c0220b5b33..88e1ca48db7 100644 --- a/lib/dispatcher-base.js +++ b/lib/dispatcher/dispatcher-base.js @@ -5,8 +5,8 @@ const { ClientDestroyedError, ClientClosedError, InvalidArgumentError -} = require('./core/errors') -const { kDestroy, kClose, kDispatch, kInterceptors } = require('./core/symbols') +} = require('../core/errors') +const { kDestroy, kClose, kDispatch, kInterceptors } = require('../core/symbols') const kDestroyed = Symbol('destroyed') const kClosed = Symbol('closed') diff --git a/lib/dispatcher.js b/lib/dispatcher/dispatcher.js similarity index 100% rename from lib/dispatcher.js rename to lib/dispatcher/dispatcher.js diff --git a/lib/pool-base.js b/lib/dispatcher/pool-base.js similarity index 97% rename from lib/pool-base.js rename to lib/dispatcher/pool-base.js index 2a909eee083..6c524e2a437 100644 --- a/lib/pool-base.js +++ b/lib/dispatcher/pool-base.js @@ -1,8 +1,8 @@ 'use strict' const DispatcherBase = require('./dispatcher-base') -const FixedQueue = require('./node/fixed-queue') -const { kConnected, kSize, kRunning, kPending, kQueued, kBusy, kFree, kUrl, kClose, kDestroy, kDispatch } = require('./core/symbols') +const FixedQueue = require('../node/fixed-queue') +const { kConnected, kSize, kRunning, kPending, kQueued, kBusy, kFree, kUrl, kClose, kDestroy, kDispatch } = require('../core/symbols') const PoolStats = require('./pool-stats') const kClients = Symbol('clients') diff --git a/lib/pool-stats.js b/lib/dispatcher/pool-stats.js similarity index 94% rename from lib/pool-stats.js rename to lib/dispatcher/pool-stats.js index b4af8aeed5f..8c7e8c9572e 100644 --- a/lib/pool-stats.js +++ b/lib/dispatcher/pool-stats.js @@ -1,4 +1,4 @@ -const { kFree, kConnected, kPending, kQueued, kRunning, kSize } = require('./core/symbols') +const { kFree, kConnected, kPending, kQueued, kRunning, kSize } = require('../core/symbols') const kPool = Symbol('pool') class PoolStats { diff --git a/lib/pool.js b/lib/dispatcher/pool.js similarity index 92% rename from lib/pool.js rename to lib/dispatcher/pool.js index d74dcca5604..0ba3a2b5f3e 100644 --- a/lib/pool.js +++ b/lib/dispatcher/pool.js @@ -10,10 +10,10 @@ const { const Client = require('./client') const { InvalidArgumentError -} = require('./core/errors') -const util = require('./core/util') -const { kUrl, kInterceptors } = require('./core/symbols') -const buildConnector = require('./core/connect') +} = require('../core/errors') +const util = require('../core/util') +const { kUrl, kInterceptors } = require('../core/symbols') +const buildConnector = require('../core/connect') const kOptions = Symbol('options') const kConnections = Symbol('connections') diff --git a/lib/proxy-agent.js b/lib/dispatcher/proxy-agent.js similarity index 97% rename from lib/proxy-agent.js rename to lib/dispatcher/proxy-agent.js index c80134b742d..9df39edb1aa 100644 --- a/lib/proxy-agent.js +++ b/lib/dispatcher/proxy-agent.js @@ -1,12 +1,12 @@ 'use strict' -const { kProxy, kClose, kDestroy, kInterceptors } = require('./core/symbols') +const { kProxy, kClose, kDestroy, kInterceptors } = require('../core/symbols') const { URL } = require('node:url') const Agent = require('./agent') const Pool = require('./pool') const DispatcherBase = require('./dispatcher-base') -const { InvalidArgumentError, RequestAbortedError } = require('./core/errors') -const buildConnector = require('./core/connect') +const { InvalidArgumentError, RequestAbortedError } = require('../core/errors') +const buildConnector = require('../core/connect') const kAgent = Symbol('proxy agent') const kClient = Symbol('proxy client') diff --git a/lib/retry-agent.js b/lib/dispatcher/retry-agent.js similarity index 91% rename from lib/retry-agent.js rename to lib/dispatcher/retry-agent.js index 9edb2aa529f..2ca82b0d02c 100644 --- a/lib/retry-agent.js +++ b/lib/dispatcher/retry-agent.js @@ -1,7 +1,7 @@ 'use strict' const Dispatcher = require('./dispatcher') -const RetryHandler = require('./handler/RetryHandler') +const RetryHandler = require('../handler/RetryHandler') class RetryAgent extends Dispatcher { #agent = null diff --git a/lib/global.js b/lib/global.js index 18bfd73cc92..0c7528fa653 100644 --- a/lib/global.js +++ b/lib/global.js @@ -4,7 +4,7 @@ // this version number must be increased to avoid conflicts. const globalDispatcher = Symbol.for('undici.globalDispatcher.1') const { InvalidArgumentError } = require('./core/errors') -const Agent = require('./agent') +const Agent = require('./dispatcher/agent') if (getGlobalDispatcher() === undefined) { setGlobalDispatcher(new Agent()) diff --git a/lib/mock/mock-agent.js b/lib/mock/mock-agent.js index 3d26a6a65ba..c02ee375e25 100644 --- a/lib/mock/mock-agent.js +++ b/lib/mock/mock-agent.js @@ -1,7 +1,7 @@ 'use strict' const { kClients } = require('../core/symbols') -const Agent = require('../agent') +const Agent = require('../dispatcher/agent') const { kAgent, kMockAgentSet, @@ -17,7 +17,7 @@ const MockClient = require('./mock-client') const MockPool = require('./mock-pool') const { matchValue, buildMockOptions } = require('./mock-utils') const { InvalidArgumentError, UndiciError } = require('../core/errors') -const Dispatcher = require('../dispatcher') +const Dispatcher = require('../dispatcher/dispatcher') const Pluralizer = require('./pluralizer') const PendingInterceptorsFormatter = require('./pending-interceptors-formatter') diff --git a/lib/mock/mock-client.js b/lib/mock/mock-client.js index 193f217501d..c375dbd455b 100644 --- a/lib/mock/mock-client.js +++ b/lib/mock/mock-client.js @@ -1,7 +1,7 @@ 'use strict' const { promisify } = require('node:util') -const Client = require('../client') +const Client = require('../dispatcher/client') const { buildMockDispatch } = require('./mock-utils') const { kDispatches, diff --git a/lib/mock/mock-pool.js b/lib/mock/mock-pool.js index 17695e72eba..8b005d72ead 100644 --- a/lib/mock/mock-pool.js +++ b/lib/mock/mock-pool.js @@ -1,7 +1,7 @@ 'use strict' const { promisify } = require('node:util') -const Pool = require('../pool') +const Pool = require('../dispatcher/pool') const { buildMockDispatch } = require('./mock-utils') const { kDispatches, diff --git a/lib/timers.js b/lib/util/timers.js similarity index 100% rename from lib/timers.js rename to lib/util/timers.js diff --git a/test/client-keep-alive.js b/test/client-keep-alive.js index b521624caec..f00e4d8a128 100644 --- a/test/client-keep-alive.js +++ b/test/client-keep-alive.js @@ -4,7 +4,7 @@ const { tspl } = require('@matteo.collina/tspl') const { test, after } = require('node:test') const { once } = require('node:events') const { Client } = require('..') -const timers = require('../lib/timers') +const timers = require('../lib/util/timers') const { kConnect } = require('../lib/core/symbols') const { createServer } = require('node:net') const http = require('node:http') diff --git a/test/client-reconnect.js b/test/client-reconnect.js index 34355ce40ac..222922a5ea0 100644 --- a/test/client-reconnect.js +++ b/test/client-reconnect.js @@ -6,7 +6,7 @@ const { once } = require('node:events') const { Client } = require('..') const { createServer } = require('node:http') const FakeTimers = require('@sinonjs/fake-timers') -const timers = require('../lib/timers') +const timers = require('../lib/util/timers') test('multiple reconnect', async (t) => { t = tspl(t, { plan: 5 }) diff --git a/test/client-timeout.js b/test/client-timeout.js index 4964774d0bc..c4ff9c2a59c 100644 --- a/test/client-timeout.js +++ b/test/client-timeout.js @@ -6,7 +6,7 @@ const { Client, errors } = require('..') const { createServer } = require('node:http') const { Readable } = require('node:stream') const FakeTimers = require('@sinonjs/fake-timers') -const timers = require('../lib/timers') +const timers = require('../lib/util/timers') test('refresh timeout on pause', async (t) => { t = tspl(t, { plan: 1 }) diff --git a/test/dispatcher.js b/test/dispatcher.js index d004c5e5e27..95a9bc59a83 100644 --- a/test/dispatcher.js +++ b/test/dispatcher.js @@ -3,7 +3,7 @@ const { tspl } = require('@matteo.collina/tspl') const { test } = require('node:test') -const Dispatcher = require('../lib/dispatcher') +const Dispatcher = require('../lib/dispatcher/dispatcher') class PoorImplementation extends Dispatcher {} diff --git a/test/fetch/fetch-timeouts.js b/test/fetch/fetch-timeouts.js index 93792871cb1..038c23b5af9 100644 --- a/test/fetch/fetch-timeouts.js +++ b/test/fetch/fetch-timeouts.js @@ -4,7 +4,7 @@ const { test } = require('node:test') const { tspl } = require('@matteo.collina/tspl') const { fetch, Agent } = require('../..') -const timers = require('../../lib/timers') +const timers = require('../../lib/util/timers') const { createServer } = require('node:http') const FakeTimers = require('@sinonjs/fake-timers') const { closeServerAsPromise } = require('../utils/node-http') diff --git a/test/mock-agent.js b/test/mock-agent.js index 58066229f58..ab852896daf 100644 --- a/test/mock-agent.js +++ b/test/mock-agent.js @@ -11,7 +11,7 @@ const { InvalidArgumentError, ClientDestroyedError } = require('../lib/core/erro const MockClient = require('../lib/mock/mock-client') const MockPool = require('../lib/mock/mock-pool') const { kAgent } = require('../lib/mock/mock-symbols') -const Dispatcher = require('../lib/dispatcher') +const Dispatcher = require('../lib/dispatcher/dispatcher') const { MockNotMatchedError } = require('../lib/mock/mock-errors') const { fetch } = require('..') diff --git a/test/mock-client.js b/test/mock-client.js index ef6721b42be..9b8f416ec34 100644 --- a/test/mock-client.js +++ b/test/mock-client.js @@ -10,7 +10,7 @@ const { kDispatches } = require('../lib/mock/mock-symbols') const { InvalidArgumentError } = require('../lib/core/errors') const { MockInterceptor } = require('../lib/mock/mock-interceptor') const { getResponse } = require('../lib/mock/mock-utils') -const Dispatcher = require('../lib/dispatcher') +const Dispatcher = require('../lib/dispatcher/dispatcher') describe('MockClient - constructor', () => { test('fails if opts.agent does not implement `get` method', t => { diff --git a/test/mock-pool.js b/test/mock-pool.js index 0b690fe6d48..b52d3406cce 100644 --- a/test/mock-pool.js +++ b/test/mock-pool.js @@ -10,7 +10,7 @@ const { kDispatches } = require('../lib/mock/mock-symbols') const { InvalidArgumentError } = require('../lib/core/errors') const { MockInterceptor } = require('../lib/mock/mock-interceptor') const { getResponse } = require('../lib/mock/mock-utils') -const Dispatcher = require('../lib/dispatcher') +const Dispatcher = require('../lib/dispatcher/dispatcher') const { fetch } = require('..') describe('MockPool - constructor', () => { diff --git a/test/pool.js b/test/pool.js index 4a22aedc84e..5e84f4402bb 100644 --- a/test/pool.js +++ b/test/pool.js @@ -366,7 +366,7 @@ test('backpressure algorithm', async (t) => { } } - const Pool = proxyquire('../lib/pool', { + const Pool = proxyquire('../lib/dispatcher/pool', { './client': FakeClient }) diff --git a/test/proxy-agent.js b/test/proxy-agent.js index 8c6634ff1fb..0e33f9600ee 100644 --- a/test/proxy-agent.js +++ b/test/proxy-agent.js @@ -6,8 +6,8 @@ const { request, fetch, setGlobalDispatcher, getGlobalDispatcher } = require('.. const { InvalidArgumentError } = require('../lib/core/errors') const { readFileSync } = require('node:fs') const { join } = require('node:path') -const ProxyAgent = require('../lib/proxy-agent') -const Pool = require('../lib/pool') +const ProxyAgent = require('../lib/dispatcher/proxy-agent') +const Pool = require('../lib/dispatcher/pool') const { createServer } = require('node:http') const https = require('node:https') const proxy = require('proxy') diff --git a/test/request-timeout.js b/test/request-timeout.js index f49f6c5bcf3..71bff4fb9cc 100644 --- a/test/request-timeout.js +++ b/test/request-timeout.js @@ -5,7 +5,7 @@ const { test, after } = require('node:test') const { createReadStream, writeFileSync, unlinkSync } = require('node:fs') const { Client, errors } = require('..') const { kConnect } = require('../lib/core/symbols') -const timers = require('../lib/timers') +const timers = require('../lib/util/timers') const { createServer } = require('node:http') const EventEmitter = require('node:events') const FakeTimers = require('@sinonjs/fake-timers') diff --git a/test/socket-timeout.js b/test/socket-timeout.js index 83617e94f7e..5be62fe4119 100644 --- a/test/socket-timeout.js +++ b/test/socket-timeout.js @@ -3,7 +3,7 @@ const { tspl } = require('@matteo.collina/tspl') const { test, after } = require('node:test') const { Client, errors } = require('..') -const timers = require('../lib/timers') +const timers = require('../lib/util/timers') const { createServer } = require('node:http') const FakeTimers = require('@sinonjs/fake-timers')