Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mangling responses #25

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/interceptor/NodeClientRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IsomorphicRequest } from './utils/IsomorphicRequest';
import { getArrayBuffer } from './utils/bufferUtils';
import { isInterceptable } from './utils/isInterceptable';
import { IsomorphicResponse } from './utils/IsomorphicResponse';
import { cloneIncomingMessage } from './utils/cloneIncomingMessage';

export type NodeClientOptions = {
emitter: EventEmitter;
Expand Down Expand Up @@ -106,7 +107,12 @@ export class NodeClientRequest extends ClientRequest {
emit(event: 'pipe', src: Readable): boolean;
emit(event: 'unpipe', src: Readable): boolean;
emit(event: string | symbol, ...args: any[]): boolean {

if (event === 'response') {
const response = args[0] as IncomingMessage;
const firstClone = cloneIncomingMessage(response);
const secondClone = cloneIncomingMessage(response);

async function emitResponse(
requestId: string,
message: IncomingMessage,
Expand All @@ -119,12 +125,10 @@ export class NodeClientRequest extends ClientRequest {
}

if (this.isInterceptable) {
emitResponse(this.requestId as string, args[0], this.emitter);
emitResponse(this.requestId as string, secondClone, this.emitter);
}

if (this.isInterceptable) {
emitResponse(this.requestId as string, args[0], this.emitter);
}
return super.emit(event as string, firstClone, ...args.slice(1));
}

return super.emit(event as string, ...args);
Expand Down
74 changes: 74 additions & 0 deletions src/interceptor/utils/cloneIncomingMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { IncomingMessage } from 'http'
import { PassThrough } from 'stream'

export const IS_CLONE = Symbol('isClone')

export interface ClonedIncomingMessage extends IncomingMessage {
[IS_CLONE]: boolean
}

/**
* Clones a given `http.IncomingMessage` instance.
*/
export function cloneIncomingMessage(
message: IncomingMessage
): ClonedIncomingMessage {
const clone = message.pipe(new PassThrough())

// Inherit all direct "IncomingMessage" properties.
inheritProperties(message, clone)

// Deeply inherit the message prototypes (Readable, Stream, EventEmitter, etc.).
const clonedPrototype = Object.create(IncomingMessage.prototype)
getPrototypes(clone).forEach((prototype) => {
inheritProperties(prototype, clonedPrototype)
})
Object.setPrototypeOf(clone, clonedPrototype)

Object.defineProperty(clone, IS_CLONE, {
enumerable: true,
value: true,
})

return clone as unknown as ClonedIncomingMessage
}

/**
* Returns a list of all prototypes the given object extends.
*/
function getPrototypes(source: object): object[] {
const prototypes: object[] = []
let current = source

while ((current = Object.getPrototypeOf(current))) {
prototypes.push(current)
}

return prototypes
}

/**
* Inherits a given target object properties and symbols
* onto the given source object.
* @param source Object which should acquire properties.
* @param target Object to inherit the properties from.
*/
function inheritProperties(source: object, target: object): void {
const properties = [
...Object.getOwnPropertyNames(source),
...Object.getOwnPropertySymbols(source),
]

for (const property of properties) {
if (target.hasOwnProperty(property)) {
continue
}

const descriptor = Object.getOwnPropertyDescriptor(source, property)
if (!descriptor) {
continue
}

Object.defineProperty(target, property, descriptor)
}
}
Loading