Skip to content

Commit aa31655

Browse files
committed
Revert "fix(api): preserve http apiFetch support"
This reverts commit 7b3e4ac.
1 parent d4e17c7 commit aa31655

2 files changed

Lines changed: 6 additions & 55 deletions

File tree

src/utils/api.mts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
* - Falls back to configured apiBaseUrl or default API_V0_URL
2020
*/
2121

22-
import { request as httpRequest } from 'node:http'
2322
import { Agent as HttpsAgent, request as httpsRequest } from 'node:https'
2423
import { ReadableStream } from 'node:stream/web'
2524

@@ -85,27 +84,25 @@ export type ApiFetchInit = {
8584
method?: string | undefined
8685
}
8786

88-
// Internal node request-based fetch with redirect support.
89-
function _nodeRequestFetch(
87+
// Internal httpsRequest-based fetch with redirect support.
88+
function _httpsRequestFetch(
9089
url: string,
9190
init: ApiFetchInit,
9291
agent: HttpsAgent | undefined,
9392
redirectCount: number,
9493
): Promise<Response> {
9594
return new Promise((resolve, reject) => {
96-
const parsedUrl = new URL(url)
9795
const headers: Record<string, string> = { ...init.headers }
9896
// Set Content-Length for request bodies to avoid chunked transfer encoding.
9997
if (init.body) {
10098
headers['content-length'] = String(Buffer.byteLength(init.body))
10199
}
102-
const request = parsedUrl.protocol === 'http:' ? httpRequest : httpsRequest
103-
const req = request(
100+
const req = httpsRequest(
104101
url,
105102
{
106103
method: init.method || 'GET',
107104
headers,
108-
agent: parsedUrl.protocol === 'https:' ? agent : undefined,
105+
agent,
109106
},
110107
res => {
111108
const { statusCode } = res
@@ -144,7 +141,7 @@ function _nodeRequestFetch(
144141
// 307 and 308 preserve the original method and body.
145142
const preserveMethod = statusCode === 307 || statusCode === 308
146143
resolve(
147-
_nodeRequestFetch(
144+
_httpsRequestFetch(
148145
redirectUrl,
149146
preserveMethod
150147
? { ...init, headers: redirectHeaders }
@@ -207,7 +204,7 @@ export async function apiFetch(
207204
url: string,
208205
init: ApiFetchInit = {},
209206
): Promise<Response> {
210-
return await _nodeRequestFetch(url, init, getHttpsAgent(), 0)
207+
return await _httpsRequestFetch(url, init, getHttpsAgent(), 0)
211208
}
212209

213210
export type CommandRequirements = {

src/utils/api.test.mts

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,9 @@ type RequestCallback = (
4545
},
4646
) => void
4747
const mockHttpsRequest = vi.hoisted(() => vi.fn())
48-
const mockHttpRequest = vi.hoisted(() => vi.fn())
4948
const MockHttpsAgent = vi.hoisted(() =>
5049
vi.fn().mockImplementation(opts => ({ ...opts, _isHttpsAgent: true })),
5150
)
52-
vi.mock('node:http', () => ({
53-
request: mockHttpRequest,
54-
}))
5551
vi.mock('node:https', () => ({
5652
Agent: MockHttpsAgent,
5753
request: mockHttpsRequest,
@@ -160,48 +156,6 @@ describe('apiFetch with extra CA certificates', () => {
160156
expect(result.ok).toBe(true)
161157
})
162158

163-
it('should use http.request for plain HTTP API URLs', async () => {
164-
const mockReq = {
165-
end: vi.fn(),
166-
on: vi.fn(),
167-
write: vi.fn(),
168-
}
169-
170-
mockHttpRequest.mockImplementation(
171-
(_url: string, _opts: unknown, callback: RequestCallback) => {
172-
setTimeout(() => {
173-
const mockRes = {
174-
headers: { 'content-type': 'text/plain' },
175-
on: vi.fn(),
176-
statusCode: 200,
177-
statusMessage: 'OK',
178-
}
179-
const handlers: Record<string, Function> = {}
180-
mockRes.on.mockImplementation((event: string, handler: Function) => {
181-
handlers[event] = handler
182-
return mockRes
183-
})
184-
callback(mockRes)
185-
handlers['data']?.(Buffer.from('local response'))
186-
handlers['end']?.()
187-
}, 0)
188-
return mockReq
189-
},
190-
)
191-
192-
const { apiFetch } = await import('./api.mts')
193-
const response = await apiFetch('http://localhost:3000/v0/report')
194-
195-
expect(response.status).toBe(200)
196-
expect(await response.text()).toBe('local response')
197-
expect(mockHttpRequest).toHaveBeenCalledWith(
198-
'http://localhost:3000/v0/report',
199-
expect.objectContaining({ agent: undefined, method: 'GET' }),
200-
expect.any(Function),
201-
)
202-
expect(mockHttpsRequest).not.toHaveBeenCalled()
203-
})
204-
205159
it('should use https.request when extra CA certs are available', async () => {
206160
const caCerts = ['ROOT_CERT', 'EXTRA_CERT']
207161
mockGetExtraCaCerts.mockReturnValue(caCerts)

0 commit comments

Comments
 (0)