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

feat: Add plugin version check #2549

Merged
merged 8 commits into from
Sep 15, 2020
Merged

feat: Add plugin version check #2549

merged 8 commits into from
Sep 15, 2020

Conversation

zekth
Copy link
Member

@zekth zekth commented Sep 9, 2020

Following #2507
Added the check of version in pluginOverride

Checklist

@@ -48,6 +53,7 @@ module.exports = function override (old, fn, opts) {
instance.getSchemas = instance[kSchemas].getSchemas.bind(instance[kSchemas])
instance[pluginUtils.registeredPlugins] = Object.create(instance[pluginUtils.registeredPlugins])
instance[kPluginNameChain] = [pluginUtils.getPluginName(fn) || pluginUtils.getFuncPreview(fn)]
instance[kVersion] = version
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to make this private? It might be useful for others if fastifyInstance.version returned the correct value.

Copy link
Member Author

Choose a reason for hiding this comment

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

If so, you want to mutate old ?

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand the question.

Copy link
Member

Choose a reason for hiding this comment

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

we do not need this stored per-instance. You can store it as a top level value.

const requiredVersion = meta.fastify
if (!requiredVersion) return

assert(semver.lte(this[kVersion], requiredVersion), `fastify-plugin: ${meta.name} - expected '${requiredVersion}' fastify version, '${this[kVersion]}' is installed`)
Copy link
Member

Choose a reason for hiding this comment

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

Would you mind to use an error with code (see lib/errors file).


function loadVersion () {
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json')))
return pkg.version
Copy link
Member

Choose a reason for hiding this comment

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

this should be cached.

As a suggestion I would put it in a getter on the top level fastify function (the factory).

@@ -48,6 +53,7 @@ module.exports = function override (old, fn, opts) {
instance.getSchemas = instance[kSchemas].getSchemas.bind(instance[kSchemas])
instance[pluginUtils.registeredPlugins] = Object.create(instance[pluginUtils.registeredPlugins])
instance[kPluginNameChain] = [pluginUtils.getPluginName(fn) || pluginUtils.getFuncPreview(fn)]
instance[kVersion] = version
Copy link
Member

Choose a reason for hiding this comment

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

we do not need this stored per-instance. You can store it as a top level value.

fastify.js Outdated Show resolved Hide resolved
@@ -581,6 +590,11 @@ function wrapRouting (httpHandler, { rewriteUrl, logger }) {
}
}

function loadVersion () {
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json')))
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why it makes more sense to read and parse manually rather than require it? Do we want to avoid caching it for some reason?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. To avoid keeping the "large" object in memory.

Copy link
Member Author

Choose a reason for hiding this comment

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

require involves caching the required file

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.

This also needs some docs

fastify.js Outdated
get () { return this[kSerializerCompiler] }
},
version: {
get () { return this[kVersion] }
Copy link
Member

Choose a reason for hiding this comment

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

I would load the version number dynamically on-demand and then cache it at the top of the file, similar to lightMyRequest.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed good point

Copy link
Member Author

Choose a reason for hiding this comment

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

As we use fs and path only for this, it might be interesting to only require them in the loadVersion method no?

Copy link
Member

Choose a reason for hiding this comment

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

As we use fs and path only for this, it might be interesting to only require them in the loadVersion method no?

I think so, we did it per light-my-request

const requiredVersion = meta.fastify
if (!requiredVersion) return

if (semver.lte(this[kVersion], requiredVersion)) throw new FST_ERR_PLUGIN_VERSION_MISMATCH(meta.name, requiredVersion, this[kVersion])
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (semver.lte(this[kVersion], requiredVersion)) throw new FST_ERR_PLUGIN_VERSION_MISMATCH(meta.name, requiredVersion, this[kVersion])
if (semver.lte(this.version, requiredVersion)) throw new FST_ERR_PLUGIN_VERSION_MISMATCH(meta.name, requiredVersion, this[kVersion])

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

const requiredVersion = meta.fastify
if (!requiredVersion) return

if (semver.lte(this.version, requiredVersion)) throw new FST_ERR_PLUGIN_VERSION_MISMATCH(meta.name, requiredVersion, this.version)
Copy link
Member

Choose a reason for hiding this comment

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

In our plugins we have patterns like '>=3.x'

In this case semver.lte('3.3.1', '>=3.x') it will throw or Am I missing something?

https://github.com/fastify/fastify-oauth2/blob/master/index.js#L144

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed will have to use validerange of semver. Will fix and add tests for this. Good catch

docs/Server.md Outdated Show resolved Hide resolved
Copy link
Member

@jsumners jsumners 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
Member

@Eomm Eomm left a comment

Choose a reason for hiding this comment

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

Do we have some canary project to test this feature across plugins?
cc @fastify/plugins

@Eomm Eomm added semver-minor Issue or PR that should land as semver minor v3.x Issue or pr related to Fastify v3 labels Sep 10, 2020
@mcollina
Copy link
Member

Do we have some canary project to test this feature across plugins?

we don't - we should rely on manually checking.

Copy link
Member

@RafaelGSS RafaelGSS left a comment

Choose a reason for hiding this comment

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

LGTM.

@Eomm
Copy link
Member

Eomm commented Sep 14, 2020

Do we have some canary project to test this feature across plugins?

we don't - we should rely on manually checking.

Could we arrange a repo for that?
I never did it, so I would like to try 👍🏼

Copy link
Member

@fox1t fox1t 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
Member

@Ethan-Arrowood Ethan-Arrowood 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 work

@mcollina mcollina merged commit 797a9db into fastify:master Sep 15, 2020
@mcollina
Copy link
Member

May I get a matching PR to fastify-plugin?

@zekth
Copy link
Member Author

zekth commented Sep 15, 2020

May I get a matching PR to fastify-plugin?

Will do.

@zekth zekth deleted the plugin_version_chk branch September 15, 2020 07:17
@github-actions
Copy link

github-actions bot commented Feb 7, 2022

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 7, 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 v3.x Issue or pr related to Fastify v3
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants