Skip to content

Commit

Permalink
Update httpolyglot to drop __httpPeekedData entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Dec 6, 2023
1 parent f4f0da9 commit 3cb5e66
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 22 deletions.
4 changes: 0 additions & 4 deletions custom-typings/node-type-extensions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ declare module "net" {
// into a single response & error event.
clientErrorInProgress?: { rawPacket?: Buffer; }

// Data that was peeked by httpolyglot, and thereby probably lost from the
// HTTP parser errors, but which might be useful for debugging later
__httpPeekedData?: Buffer;

// Our recordings of various timestamps, used for monitoring &
// performance analysis later on
__timingInfo?: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
"dependencies": {
"@graphql-tools/schema": "^8.5.0",
"@graphql-tools/utils": "^8.8.0",
"@httptoolkit/httpolyglot": "^2.1.1",
"@httptoolkit/httpolyglot": "^2.2.0",
"@httptoolkit/subscriptions-transport-ws": "^0.11.2",
"@httptoolkit/websocket-stream": "^6.0.1",
"@types/cors": "^2.8.6",
Expand Down
20 changes: 3 additions & 17 deletions src/server/mockttp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,9 +919,7 @@ ${await this.suggestRule(request)}`
// (e.g. Q as first char) then this packet data does get thrown! Eugh. In that case,
// we need to avoid using both by accident, so we use just the non-peeked data instead
// if the initial data is _exactly_ identical.
rawPacket: !error.rawPacket || socket.__httpPeekedData?.equals(error.rawPacket)
? undefined
: error.rawPacket
rawPacket: error.rawPacket
};

setImmediate(async () => {
Expand All @@ -934,20 +932,8 @@ ${await this.suggestRule(request)}`
timingEvents: { startTime: Date.now(), startTimestamp: now() } as TimingEvents
};

// Initially _httpMessage is undefined, until at least one request has been parsed.
// Later it's set to the current ServerResponse, and then null when the socket is
// detached, but never back to undefined. Avoids issues with using old peeked data
// on subsequent requests within keep-alive connections.
const isFirstRequest = (socket as any)._httpMessage === undefined;

// HTTPolyglot's byte-peeking can sometimes lose the initial byte from the parser's
// exposed buffer. If that's happened, we need to get it back:
const rawPacket = Buffer.concat(
[
isFirstRequest && socket.__httpPeekedData,
socket.clientErrorInProgress?.rawPacket
].filter((data) => !!data) as Buffer[]
);
const rawPacket = socket.clientErrorInProgress?.rawPacket
?? Buffer.from([]);

// For packets where we get more than just httpolyglot-peeked data, guess-parse them:
const parsedRequest = rawPacket.byteLength > 1
Expand Down

0 comments on commit 3cb5e66

Please sign in to comment.