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

[TEX-537] Refactor compression #555

Merged
merged 12 commits into from
Dec 10, 2023
Merged

Conversation

timdawborn
Copy link
Contributor

@timdawborn timdawborn commented Dec 8, 2023

This PR continues to cleanup this codebase ahead of supporting richer, content-type-aware, request body matching.

This PR refactors all of the logic that deals with compression into a singular location, and removes an old, outdated third-party library in favour of the builtin NodeJS implementation.

@@ -37,6 +37,7 @@ export function setupServers({
defaultTapeName,
host: TEST_SERVER_HOST,
timeout: 100,
enableLogging: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated change: enable logging on the proxay instance running in the integration tests.

data: string;
};

export type CompressionAlgorithm = "br" | "gzip" | "none";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved into compression.ts.

@@ -37,8 +38,6 @@ export type PersistedBuffer =
}
| {
encoding: "utf8";
compression: CompressionAlgorithm;
compression: CompressionAlgorithm | undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Older tapes don't have the compression attribute serialized.

Comment on lines -130 to -138
let compression: CompressionAlgorithm = "none";
if (contentEncoding === "br") {
buffer = Buffer.from(brotli.decompress(buffer));
compression = "br";
}
if (contentEncoding === "gzip") {
buffer = gunzipSync(buffer);
compression = "gzip";
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this common logic into compression.ts.

Comment on lines -173 to -187
if (persisted.compression === "br") {
// TODO: Find a workaround for the new compressed message not necessarily
// being identical to what was originally sent (update Content-Length?).
const compressed = brotli.compress(buffer);
if (compressed) {
buffer = Buffer.from(compressed);
} else {
throw new Error(`Brotli compression failed!`);
}
}
if (persisted.compression === "gzip") {
// TODO: Find a workaround for the new compressed message not necessarily
// being identical to what was originally sent (update Content-Length?).
buffer = gzipSync(buffer);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this common logic into compression.ts.

Comment on lines -65 to -73
switch (contentEncoding) {
case "":
return request.body;
case "br":
return Buffer.from(brotli.decompress(request.body));
case "gzip":
return zlib.gunzipSync(request.body);
default:
throw Error(`Unhandled content-encoding value "${contentEncoding}"`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this common logic into compression.ts.

@@ -51,7 +51,6 @@
},
"dependencies": {
"assert-never": "^1.2.1",
"brotli": "^1.3.3",
Copy link
Contributor Author

@timdawborn timdawborn Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replacing this third-party library with the brotli implementation provided by the NodeJS builtin zlib module.

@@ -221,7 +222,7 @@ describe("Persistence", () => {
},
body: {
encoding: "base64",
data: "GxcAAI6UrMm1WkAERl0HoDFuCn3CIekc",
data: "GxcA+I+UrMm1WkAERl0HoDFuCn3CAZLOAQ==",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The brotli encoding default compression configuration differs between the old library and the new NodeJS builtin implementation. This is why hard-coded expected encoding here differs.

@@ -233,7 +234,7 @@ describe("Persistence", () => {
},
body: {
encoding: "base64",
data: "GxcAAI6UrPmFmgFmOV+HoM3+C33CIe4U",
data: "GxcA+I+UrPmFmgFmOV+HoM3+C33CAeJOAQ==",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The brotli encoding default compression configuration differs between the old library and the new NodeJS builtin implementation. This is why hard-coded expected encoding here differs.

@timdawborn timdawborn requested a review from njday December 8, 2023 05:15
@timdawborn timdawborn marked this pull request as ready for review December 8, 2023 05:15
@timdawborn timdawborn requested a review from a team as a code owner December 8, 2023 05:15
njday
njday previously approved these changes Dec 9, 2023
Copy link

@njday njday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Nice cleanups in this repo

Base automatically changed from TEX-537-refactor-http-request-response to master December 10, 2023 21:52
@timdawborn timdawborn dismissed njday’s stale review December 10, 2023 21:52

The base branch was changed.

@timdawborn timdawborn merged commit 3ef704e into master Dec 10, 2023
6 checks passed
@timdawborn timdawborn deleted the TEX-537-refactor-compression branch December 10, 2023 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants