Skip to content

Commit 23a4875

Browse files
authored
chore: refactor server request to TypeScript (#33610)
* chore: convert stream_buffer to TypeScript * chore: convert request to TypeScript and to ES6 class * fix: actually use rp in the deferredsourcemapcache and pass in the correct options to the request
1 parent c0b3929 commit 23a4875

12 files changed

Lines changed: 917 additions & 863 deletions

File tree

packages/proxy/lib/http/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type { Readable } from 'stream'
2626
import type { Request, Response } from 'express'
2727
import type { RemoteStates } from '@packages/server/lib/remote_states'
2828
import type { CookieJar, SerializableAutomationCookie } from '@packages/server/lib/util/cookies'
29+
import type { Request as ServerRequest } from '@packages/server/lib/request'
2930
import type { FoundBrowser, ProtocolManagerShape } from '@packages/types'
3031
import type Protocol from 'devtools-protocol'
3132
import type { ServiceWorkerClientEvent } from './util/service-worker-manager'
@@ -98,7 +99,7 @@ export type ServerCtx = Readonly<{
9899
netStubbingState: NetStubbingState
99100
middleware: HttpMiddlewareStacks
100101
socket: SocketBroadcaster
101-
request: any
102+
request: ServerRequest
102103
serverBus: EventEmitter
103104
getCurrentBrowser: () => FoundBrowser
104105
}>
@@ -273,7 +274,7 @@ export class Http {
273274
netStubbingState: NetStubbingState
274275
preRequests: PreRequests = new PreRequests()
275276
getCurrentBrowser: () => FoundBrowser
276-
request: any
277+
request: ServerRequest
277278
socket: SocketBroadcaster
278279
serverBus: EventEmitter
279280
renderedHTMLOrigins: {[key: string]: boolean} = {}
@@ -284,7 +285,7 @@ export class Http {
284285

285286
constructor (opts: ServerCtx & { middleware?: HttpMiddlewareStacks }) {
286287
this.buffers = new HttpBuffers()
287-
this.deferredSourceMapCache = new DeferredSourceMapCache(opts.request)
288+
this.deferredSourceMapCache = new DeferredSourceMapCache(opts.request.rp)
288289
this.config = opts.config
289290
this.shouldCorrelatePreRequests = opts.shouldCorrelatePreRequests || (() => false)
290291
this.getFileServerToken = opts.getFileServerToken

packages/proxy/test/integration/net-stubbing.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { DocumentDomainInjection } from '@packages/network-tools'
1313
import { EventEmitter } from 'events'
1414
import { RemoteStates } from '@packages/server/lib/remote_states'
1515
import { CookieJar } from '@packages/server/lib/util/cookies'
16-
const Request = require('@packages/server/lib/request')
16+
import { Request as ServerRequest } from '@packages/server/lib/request'
1717
const getFixture = async () => {}
1818

1919
describe('network stubbing', () => {
@@ -59,7 +59,7 @@ describe('network stubbing', () => {
5959
getCookieJar: () => new CookieJar(),
6060
remoteStates,
6161
getFileServerToken: () => 'fake-token',
62-
request: new Request(),
62+
request: new ServerRequest(),
6363
getRenderedHTMLOrigins: () => ({}),
6464
serverBus: new EventEmitter(),
6565
getCurrentBrowser: vi.fn(),

packages/proxy/test/unit/http/index.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('http', function () {
2828
[HttpStages.Error]: { error },
2929
}
3030

31-
httpOpts = { config, middleware } as ServerCtx & { middleware?: HttpMiddlewareStacks }
31+
httpOpts = { config, middleware, request: { rp: vi.fn() } } as ServerCtx & { middleware?: HttpMiddlewareStacks } & { request: { rp: Mock } }
3232
})
3333

3434
it('calls IncomingRequest stack, then IncomingResponse stack', async function () {
@@ -277,7 +277,7 @@ describe('http', function () {
277277
let httpOpts
278278

279279
beforeEach(function () {
280-
httpOpts = { config: {}, middleware: {} }
280+
httpOpts = { config: {}, middleware: {}, request: { rp: vi.fn() } }
281281
})
282282

283283
it('resets preRequests when resetBetweenSpecs is true', function () {
@@ -321,7 +321,7 @@ describe('http', function () {
321321
[HttpStages.Error]: { error },
322322
}
323323

324-
httpOpts = { config, middleware } as ServerCtx & { middleware?: HttpMiddlewareStacks }
324+
httpOpts = { config, middleware, request: { rp: vi.fn() } } as ServerCtx & { middleware?: HttpMiddlewareStacks } & { request: { rp: Mock } }
325325
})
326326

327327
it('properly ignores requests that are controlled by a service worker', () => {

packages/rewriter/lib/deferred-source-map-cache.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Debug from 'debug'
33
import { rewriteJsSourceMapAsync } from './async-rewriters'
44
import * as sourceMaps from './util/source-maps'
55
import url from 'url'
6+
import type cypressRequestPromise from '@cypress/request-promise'
67

78
const debug = Debug('cypress:rewriter:deferred-source-map-cache')
89

@@ -39,10 +40,10 @@ const getSourceMapHeader = (headers) => {
3940
export class DeferredSourceMapCache {
4041
_idCounter = 0
4142
requests: DeferredSourceMapRequest[] = []
42-
requestLib: any
43+
requestPromise: typeof cypressRequestPromise
4344

44-
constructor (requestLib) {
45-
this.requestLib = requestLib
45+
constructor (requestPromiseInstance: typeof cypressRequestPromise) {
46+
this.requestPromise = requestPromiseInstance
4647
}
4748

4849
defer = (request: DeferredSourceMapRequest) => {
@@ -82,16 +83,17 @@ export class DeferredSourceMapCache {
8283

8384
// try to load it from the web
8485
const req = {
85-
url: url.resolve(request.url, sourceMapUrl),
86+
uri: url.resolve(request.url, sourceMapUrl),
8687
// TODO: this assumes that the sourcemap is on the same base domain, so it's safe to send the same headers
8788
// the browser sent for this sourcemap request - but if sourcemap is on a different domain, this will not
8889
// be true. need to use browser's cookiejar instead.
8990
headers,
9091
timeout: 5000,
92+
resolveWithFullResponse: true,
9193
}
9294

9395
try {
94-
const { body } = await this.requestLib(req, true)
96+
const { body } = await this.requestPromise(req)
9597

9698
return body
9799
} catch (error) {

packages/rewriter/test/unit/deferred-source-map-cache.spec.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, it, vi } from 'vitest'
2+
import cypressRequestPromise from '@cypress/request-promise'
23
import { DeferredSourceMapCache } from '../../lib/deferred-source-map-cache'
34
import {
45
testSourceWithExternalSourceMap,
@@ -11,7 +12,7 @@ describe('DeferredSourceMapCache', function () {
1112
let cache: DeferredSourceMapCache
1213

1314
beforeEach(() => {
14-
cache = new DeferredSourceMapCache(vi.fn())
15+
cache = new DeferredSourceMapCache(vi.fn() as unknown as typeof cypressRequestPromise)
1516
})
1617

1718
describe('#defer', () => {
@@ -109,16 +110,18 @@ describe('DeferredSourceMapCache', function () {
109110
return
110111
}
111112

112-
expect(cache.requestLib).toHaveBeenCalledWith({
113-
url: 'http://somedomain.net/dir/test.js.map',
113+
expect(cache.requestPromise).toHaveBeenCalledWith({
114+
uri: 'http://somedomain.net/dir/test.js.map',
114115
headers: {},
115116
timeout: 5000,
116-
}, true)
117+
resolveWithFullResponse: true,
118+
})
117119
}
118120
}
119121

120122
beforeEach(() => {
121-
cache.requestLib.mockResolvedValue({ body: testSourceMap })
123+
// @ts-expect-error: @cypress/request-promise not typed
124+
cache.requestPromise.mockResolvedValue({ body: testSourceMap })
122125
})
123126

124127
it('with inlined base64 sourceMappingURL', testExternalSourceMap(testSourceWithInlineSourceMap, {}, false))
File renamed without changes.

0 commit comments

Comments
 (0)