Skip to content

Conversation

@darkgl0w
Copy link
Member

@darkgl0w darkgl0w commented Feb 22, 2019

Address #1444

@mcollina > I followed your advices and now initialConfig uses a schema to validate what must be exposed, I also documented the functionality.

Let me know if you want that I make some rework.

PS: sorry again for the burden of re-opening a PR, I currently have a poor cellular connection and it was easier for me to do like this in order to avoid commit pollution 😸

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Good work! I’ve just left a couple of nits.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

@cemremengu
Copy link
Contributor

Maybe we should put this in a separate file per #1475 ?

@mcollina
Copy link
Member

@cemremengu it mostly is already, that was the whole of point of some of the latest comments.

@darkgl0w
Copy link
Member Author

darkgl0w commented Feb 24, 2019

@mcollina I was looking at this while doing the backport for the 1.x branch.
I can litteraly remove the function from fastify.js, add this to the initialConfigValidation.js file

function validateInitialConfig (options) { // options instead of opts
  // We clone the initial configuration options object
  let opts = Object.assign({}, options) // add this here

  const isOptionValid = validate(opts)

and we require initialConfigValidation like this in fastify.js:

const getSecuredInitialConfig = require('./lib/initialConfigValidation')

is it what you had in mind?

@mcollina
Copy link
Member

wait for this to land before backporting.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Still LGTM ;).

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@nwoltman nwoltman left a comment

Choose a reason for hiding this comment

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

Just 2 small things to clean up and then this will be good!

Copy link
Contributor

@nwoltman nwoltman left a comment

Choose a reason for hiding this comment

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

LGTM. Great job @darkgl0w!

Copy link
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

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

I feel that this solution is overkill. Why do we need to clone and freeze the object?

@delvedor delvedor added the semver-minor Issue or PR that should land as semver minor label Mar 1, 2019
@nwoltman
Copy link
Contributor

nwoltman commented Mar 7, 2019

Still LGTM 👍

@mcollina
Copy link
Member

mcollina commented Mar 8, 2019

@delvedor do you still strongly object to the deep-freeze?

@alex-ppg
Copy link
Contributor

alex-ppg commented Mar 8, 2019

A minor comment, it appears that the schema defined for the options parameter does not contain complex data structures. As such, JSON.parse(JSON.stringify(obj)) will create a deep-clone of the object fast without needing the addition of an external dependency. I understand the flexibility that the rfdc package allows but would a configuration for the fastify server possess any complex data structures that should be exposed? Just a thought, I understand this has passed numerous reviews.

P.S. I think line 82 in lib/initialConfigValidation.js has a reduntant check, typeof value === 'object', which is covered by the preceding if clause.

This will avoid any TypeError and cover all the cases.

*Previously it only covered `Uint8Array` because `Buffer` are instances of `Uint8Array` since Node.js 4*
ref.: https://nodejs.org/docs/latest-v4.x/api/buffer.html#buffer_buffer
@mcollina
Copy link
Member

mcollina commented Mar 9, 2019

@alex-ppg unfortunately out opts could contain some complex data structures (streams, buffers) that would not like being stringified.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Can you please add a test passing a stream to pino? So we can verify this mechanism is not causing issues with the stream.

Copy link
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

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

Hello! Sorry for the delay, I left a couple of comments.
I still think that deep clone and deep freeze is overkill, but I'll not block this.

@darkgl0w
Copy link
Member Author

darkgl0w commented Mar 9, 2019

As suggested by @delvedor I refactored the default behavior when Fastify is initialized without options.

Possible enhancements :
Currently internal defaults are passed directly to the different functions that require them like this const requestIdHeader = options.requestIdHeader || 'request-id'.

What we can do, is define internal defaults inside constants declared at the top of fastify.js file like it is done for the bodyLimit that defaults to theconst DEFAULT_BODY_LIMIT = 1024 * 1024 (this can be done according to this on going work here #1475) .

Doing so we can expand the current function signature to something like this:

getSecuredInitialConfig(options, defaults)

and thus we could implement an automatic update of the schema defaults every time they are internally changed.

@delvedor delvedor added the v2.x Issue or pr related to Fastify v2 label Mar 9, 2019
@darkgl0w
Copy link
Member Author

darkgl0w commented Mar 9, 2019

WDYT if I export the deepFreezeObject() as an utils like this

module.exports.utils = { deepFreezeObject }

or like this:

module.exports.deepFreezeObject = deepFreezeObject

It actually bothers me a lot to not be able to achieve 100% code coverage on this file because due to the current filtering schema a path is actually not able to be taken (it's like an unpleasant pinch x)). Exporting the function I would be able to effectively completely test the way it should work and in addition it can be reusable if by chance a future object deep freeze should be done in another part of fastify internals.

Are you ok with this ? can I go for it ?

I already have a commit ready with all the changes needed and 100% code coverage achieved 😁 .

@mcollina
Copy link
Member

mcollina commented Mar 9, 2019 via email

@mcollina
Copy link
Member

Would you like to add getSecuredInitialConfig(options, defaults) support as well, or do that in another PR?

@darkgl0w
Copy link
Member Author

I think it's better to do that in another PR, this way it will be cleaner and easier to review.

@mcollina mcollina merged commit 0a934fc into fastify:master Mar 10, 2019
@darkgl0w darkgl0w deleted the initial-config-draft branch March 10, 2019 10:48
@mcollina
Copy link
Member

@darkgl0w please send over the follow up PR!

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

semver-minor Issue or PR that should land as semver minor v2.x Issue or pr related to Fastify v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants