Files
cypress/packages/proxy/lib/http/util/buffers.ts
T
Lachlan Miller ab401ecd35 chore: use import type syntax (#17864)
* 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
2021-08-25 09:11:56 +10:00

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
}
}
}