Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

refactor(lumberjack): replace with util fn #1213

Merged
merged 9 commits into from
Jan 11, 2024
Merged

Conversation

JAdshead
Copy link
Contributor

Description

This is almost a direct lift and shift from https://github.com/americanexpress/lumberjack/blob/main/src/monkeypatches/attachHttpsRequestSpy.js.
For simplicity i combined the attach http and https request spies into single function.

Motivation and Context

One App is now only using small part of Lumberjack. This will allow lumberjack to be deprecated.

How Has This Been Tested?

Test suite.

Types of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (adding or updating documentation)
  • Dependency update
  • Security update

Checklist:

  • My change requires a change to the documentation and I have updated the documentation accordingly.
  • These changes should be applied to a maintenance branch.
  • This change requires cross browser checks.
  • Performance tests should be ran against the server prior to merging.
  • This change impacts caching for client browsers.
  • This change impacts HTTP headers.
  • This change adds additional environment variable requirements for One App users.
  • I have added the Apache 2.0 license header to any new files created.

What is the Impact to Developers Using One App?

Comment on lines -72 to -80
// In Node.js v8 and earlier https.request internally called http.request, but this is changed in
// later versions
// https://github.com/nodejs/node/blob/v6.x/lib/https.js#L206
// https://github.com/nodejs/node/blob/v8.x/lib/https.js#L239
// https://github.com/nodejs/node/blob/v10.x/lib/https.js#L271
if (Number.parseInt(/^v(\d+)/.exec(process.version)[1], 10) > 8) {
monkeypatches.attachHttpsRequestSpy(outgoingRequestSpy, outgoingRequestEndSpy);
}
monkeypatches.attachHttpRequestSpy(outgoingRequestSpy, outgoingRequestEndSpy);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this has been simplified as One App has long since dropped support for node 8

@@ -0,0 +1,47 @@
/*
Copy link
Contributor Author

Choose a reason for hiding this comment

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

};
}

export default function attachRequestSpies(requestSpy, socketCloseSpy) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

github-actions bot commented Dec 14, 2023

Size Change: 0 B

Total Size: 712 kB

ℹ️ View Unchanged
Filename Size
./build/app/app.js 164 kB
./build/app/app~vendors.js 411 kB
./build/app/runtime.js 7.07 kB
./build/app/service-worker-client.js 7.25 kB
./build/app/vendors.js 123 kB

compressed-size-action

Comment on lines +81 to +82
attachSpy(https, 'request', httpSpy('https', requestSpy, socketCloseSpy));
attachSpy(http, 'request', httpSpy('http', requestSpy, socketCloseSpy));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the only change. instead of having a function for each attachment we have a single function for attaching both.

return url.parse(url.format(buildUrlObject(options, defaultProtocol)));
}

function httpSpy(defaultProtocol, requestSpy, socketCloseSpy) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

package.json Outdated Show resolved Hide resolved
import attachSpy from './attachSpy';

function buildUrlObject(options, defaultProtocol) {
// TODO: url.parse is deprecated, use new URL() instead
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this change is beyond the scope of this PR

Comment on lines +33 to +51
const urlObject = {
auth: options.auth,
hostname: options.hostname || options.host || 'localhost',
protocol,
port: options.port || (protocol === 'http:' ? 80 : 443),
hash: parsedPath.hash,
path: parsedPath.path,
pathname: parsedPath.pathname,
query: parsedPath.query,
search: parsedPath.search,
};
if (
(protocol === 'http:' && urlObject.port === 80)
|| (protocol === 'https:' && urlObject.port === 443)
) {
urlObject.port = undefined;
}
return urlObject;
}
Copy link
Member

Choose a reason for hiding this comment

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

(ref #1213 (comment)), knowing that we can use undefined for port, then there's no need for the if statement + mutation, we could simplify it like:

Suggested change
const urlObject = {
auth: options.auth,
hostname: options.hostname || options.host || 'localhost',
protocol,
port: options.port || (protocol === 'http:' ? 80 : 443),
hash: parsedPath.hash,
path: parsedPath.path,
pathname: parsedPath.pathname,
query: parsedPath.query,
search: parsedPath.search,
};
if (
(protocol === 'http:' && urlObject.port === 80)
|| (protocol === 'https:' && urlObject.port === 443)
) {
urlObject.port = undefined;
}
return urlObject;
}
const urlObject = {
auth: options.auth,
hostname: options.hostname || options.host || 'localhost',
protocol,
portx: options.port || (protocol === 'http:' ? 80 : 443),
port: options.port && (
(protocol === 'http:' && options.port !== 80) ||
(protocol === 'https:' && options.port !== 443)
) ? options.port : undefined,
hash: parsedPath.hash,
path: parsedPath.path,
pathname: parsedPath.pathname,
query: parsedPath.query,
search: parsedPath.search,
};
return urlObject;
}

The idea is that, if the options.port is provided then we validate and make sure it's a different port (different than 80 for http and different than 443 for https), otherwise we just set it to undefined.

Not, this leads me to the following question, why do we need to unset the port for those cases? can't just use 80 for http since that's the provided port? looks like that we need it's just port: options.port

Copy link
Contributor Author

@JAdshead JAdshead Jan 3, 2024

Choose a reason for hiding this comment

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

This is not quite the same result. if the protocol is undefined and no options.port is set the current result would be 443 not undefined. Im not convinced that this is the intended result but for this PR im trying to keep the functionality identical.

I think having the removal as a separate conditional is clearer. I suspect this logic is based around old Node edge cases.
This PR is intended as a simple lift and shift. This will need a rewrite as part of the removal url.parse

https://github.com/americanexpress/one-app/pull/1213/files#diff-8bfac20050e8adb94d26610e32b8af417528273ceb2d8e282e06484e02825299R25-R30

@JAdshead JAdshead requested review from giulianok and a team January 3, 2024 19:29
@10xLaCroixDrinker
Copy link
Member

commit type should be refactor

@JAdshead
Copy link
Contributor Author

JAdshead commented Jan 3, 2024

commit type should be refactor

will change on merge

@JAdshead JAdshead enabled auto-merge (squash) January 3, 2024 20:17
@10xLaCroixDrinker 10xLaCroixDrinker changed the title chore(lumberjack): replace with util fn refactor(lumberjack): replace with util fn Jan 3, 2024

const callOriginal = jest.fn(() => 'client request object');

httpsSpy(['http://example.tld'], callOriginal);
Copy link
Member

Choose a reason for hiding this comment

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

Can we call from http directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we could, these tests rely on attachSpy being mocked so they will all need a rewrite.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Matthew-Mallimo
Matthew-Mallimo previously approved these changes Jan 5, 2024
@JAdshead JAdshead merged commit 6f8c847 into main Jan 11, 2024
9 checks passed
@JAdshead JAdshead deleted the feat/replace-lumberjac branch January 11, 2024 19:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants