mirror of
https://github.com/cypress-io/cypress.git
synced 2026-04-21 22:49:16 -05:00
ab401ecd35
* chore: use import type across repo * chore: use import type across repo * chore: use import type across repo * chore: use import type across repo * update exports * update test * update import type * update types * use import type in driver * correctly export function * revert test * remove unrelated code * revert code * improve type imports * override for reporter
66 lines
1.3 KiB
TypeScript
66 lines
1.3 KiB
TypeScript
import _ from 'lodash'
|
|
import debugModule from 'debug'
|
|
import { uri } from '@packages/network'
|
|
import type { Readable } from 'stream'
|
|
import type { IncomingMessage } from 'http'
|
|
|
|
const debug = debugModule('cypress:proxy:http:util:buffers')
|
|
|
|
export type HttpBuffer = {
|
|
details: object
|
|
originalUrl: string
|
|
response: IncomingMessage
|
|
stream: Readable
|
|
url: string
|
|
}
|
|
|
|
const stripPort = (url) => {
|
|
try {
|
|
return uri.removeDefaultPort(url).format()
|
|
} catch (e) {
|
|
return url
|
|
}
|
|
}
|
|
|
|
export class HttpBuffers {
|
|
buffer: Optional<HttpBuffer> | undefined = undefined
|
|
|
|
reset (): void {
|
|
debug('resetting buffers')
|
|
|
|
delete this.buffer
|
|
}
|
|
|
|
set (obj) {
|
|
obj = _.cloneDeep(obj)
|
|
obj.url = stripPort(obj.url)
|
|
obj.originalUrl = stripPort(obj.originalUrl)
|
|
|
|
if (this.buffer) {
|
|
debug('warning: overwriting existing buffer...', { buffer: _.pick(this.buffer, 'url') })
|
|
}
|
|
|
|
debug('setting buffer %o', _.pick(obj, 'url'))
|
|
|
|
this.buffer = obj
|
|
}
|
|
|
|
get (str): Optional<HttpBuffer> {
|
|
if (this.buffer && this.buffer.url === stripPort(str)) {
|
|
return this.buffer
|
|
}
|
|
}
|
|
|
|
take (str): Optional<HttpBuffer> {
|
|
const foundBuffer = this.get(str)
|
|
|
|
if (foundBuffer) {
|
|
delete this.buffer
|
|
|
|
debug('found request buffer %o', { buffer: _.pick(foundBuffer, 'url') })
|
|
|
|
return foundBuffer
|
|
}
|
|
}
|
|
}
|