Skip to content

Commit

Permalink
feat: add hasPlugin method
Browse files Browse the repository at this point in the history
  • Loading branch information
is2ei committed Jun 1, 2022
1 parent 4e95233 commit 03b8f46
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
10 changes: 10 additions & 0 deletions docs/Reference/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describes the properties available in that options object.
- [addHook](#addhook)
- [prefix](#prefix)
- [pluginName](#pluginname)
- [hasPlugin](#hasplugin)
- [log](#log)
- [version](#version)
- [inject](#inject)
Expand Down Expand Up @@ -1105,6 +1106,15 @@ no new scope is created and therefore we have no place to attach contextual
data. In that case, the plugin name will represent the boot order of all
involved plugins in the format of `fastify -> plugin-A -> plugin-B`.
#### hasPlugin
<a id="hasPlugin"></a>
Method to check if the plugin is registered.
```js
fastify.hasPlugin('your-plugin-name')
```
#### log
<a id="log"></a>
Expand Down
4 changes: 4 additions & 0 deletions fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ function fastify (options) {
onClose: null,
close: null,
printPlugins: null,
// check existence of the plugin
hasPlugin: function (name) {
return this[kPluginNameChain].includes(name)
},
// http server
listen,
server,
Expand Down
10 changes: 8 additions & 2 deletions test/hooks.on-ready.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ const Fastify = require('../fastify')
const immediate = require('util').promisify(setImmediate)

t.test('onReady should be called in order', t => {
t.plan(7)
t.plan(10)
const fastify = Fastify()

let order = 0

fastify.addHook('onReady', function (done) {
t.equal(order++, 0, 'called in root')
t.equal(this.pluginName, fastify.pluginName, 'the this binding is the right instance')
t.ok(this.hasPlugin, fastify.pluginName)
done()
})

fastify.register(async (childOne, o) => {
childOne.addHook('onReady', function (done) {
t.equal(order++, 1, 'called in childOne')
t.equal(this.pluginName, childOne.pluginName, 'the this binding is the right instance')
t.ok(this.hasPlugin, childOne.pluginName)
done()
})

Expand All @@ -28,6 +30,7 @@ t.test('onReady should be called in order', t => {
await immediate()
t.equal(order++, 2, 'called in childTwo')
t.equal(this.pluginName, childTwo.pluginName, 'the this binding is the right instance')
t.ok(this.pluginName, childTwo.pluginName)
})
})
})
Expand All @@ -36,7 +39,7 @@ t.test('onReady should be called in order', t => {
})

t.test('async onReady should be called in order', async t => {
t.plan(7)
t.plan(10)
const fastify = Fastify()

let order = 0
Expand All @@ -45,20 +48,23 @@ t.test('async onReady should be called in order', async t => {
await immediate()
t.equal(order++, 0, 'called in root')
t.equal(this.pluginName, fastify.pluginName, 'the this binding is the right instance')
t.ok(this.hasPlugin, fastify.pluginName)
})

fastify.register(async (childOne, o) => {
childOne.addHook('onReady', async function () {
await immediate()
t.equal(order++, 1, 'called in childOne')
t.equal(this.pluginName, childOne.pluginName, 'the this binding is the right instance')
t.ok(this.hasPlugin, childOne.pluginName)
})

childOne.register(async (childTwo, o) => {
childTwo.addHook('onReady', async function () {
await immediate()
t.equal(order++, 2, 'called in childTwo')
t.equal(this.pluginName, childTwo.pluginName, 'the this binding is the right instance')
t.ok(this.hasPlugin, childTwo.pluginName)
})
})
})
Expand Down
1 change: 1 addition & 0 deletions test/plugin.name.display.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require('assert')

module.exports = function (fastify, opts, done) {
assert.strictEqual(fastify.pluginName, 'test-plugin')
assert.strictEqual(fastify.hasPlugin('test-plugin'), true)
done()
}

Expand Down
32 changes: 28 additions & 4 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@ test('plugin metadata - ignore prefix', t => {
})

test('plugin metadata - naming plugins', async t => {
t.plan(2)
t.plan(4)
const fastify = Fastify()

fastify.register(require('./plugin.name.display'))
fastify.register(function (fastify, opts, done) {
// one line
t.equal(fastify.pluginName, 'function (fastify, opts, done) { -- // one line')
t.ok(fastify.hasPlugin('function (fastify, opts, done) { -- // one line'))
done()
})
fastify.register(function fooBar (fastify, opts, done) {
t.equal(fastify.pluginName, 'fooBar')
t.ok(fastify.hasPlugin('fooBar'))
done()
})

Expand Down Expand Up @@ -354,24 +356,34 @@ test('check dependencies - should throw', t => {
})

test('set the plugin name based on the plugin displayName symbol', t => {
t.plan(6)
t.plan(16)
const fastify = Fastify()

fastify.register(fp((fastify, opts, done) => {
t.equal(fastify.pluginName, 'fastify -> plugin-A')
t.ok(fastify.hasPlugin('plugin-A'))
fastify.register(fp((fastify, opts, done) => {
t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')
t.ok(fastify.hasPlugin('plugin-A'))
t.ok(fastify.hasPlugin('plugin-AB'))
done()
}, { name: 'plugin-AB' }))
fastify.register(fp((fastify, opts, done) => {
t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
t.ok(fastify.hasPlugin('plugin-A'))
t.ok(fastify.hasPlugin('plugin-AB'))
t.ok(fastify.hasPlugin('plugin-AC'))
done()
}, { name: 'plugin-AC' }))
done()
}, { name: 'plugin-A' }))

fastify.register(fp((fastify, opts, done) => {
t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC -> plugin-B')
t.ok(fastify.hasPlugin('plugin-A'))
t.ok(fastify.hasPlugin('plugin-AB'))
t.ok(fastify.hasPlugin('plugin-AC'))
t.ok(fastify.hasPlugin('plugin-B'))
done()
}, { name: 'plugin-B' }))

Expand All @@ -384,24 +396,32 @@ test('set the plugin name based on the plugin displayName symbol', t => {
})

test('plugin name will change when using no encapsulation', t => {
t.plan(6)
t.plan(14)
const fastify = Fastify()

fastify.register(fp((fastify, opts, done) => {
// store it in a different variable will hold the correct name
const pluginName = fastify.pluginName
fastify.register(fp((fastify, opts, done) => {
t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB')
t.ok(fastify.hasPlugin('plugin-A'))
t.ok(fastify.hasPlugin('plugin-AB'))
done()
}, { name: 'plugin-AB' }))
fastify.register(fp((fastify, opts, done) => {
t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
t.ok(fastify.hasPlugin('plugin-A'))
t.ok(fastify.hasPlugin('plugin-AB'))
t.ok(fastify.hasPlugin('plugin-AC'))
done()
}, { name: 'plugin-AC' }))
setImmediate(() => {
// normally we would expect the name plugin-A
// but we operate on the same instance in each plugin
t.equal(fastify.pluginName, 'fastify -> plugin-A -> plugin-AB -> plugin-AC')
t.ok(fastify.hasPlugin('plugin-A'))
t.ok(fastify.hasPlugin('plugin-AB'))
t.ok(fastify.hasPlugin('plugin-AC'))
t.equal(pluginName, 'fastify -> plugin-A')
})
done()
Expand All @@ -428,24 +448,28 @@ test('plugin name is undefined when accessing in no plugin context', t => {
})

test('set the plugin name based on the plugin function name', t => {
t.plan(5)
t.plan(9)
const fastify = Fastify()

fastify.register(function myPluginA (fastify, opts, done) {
t.equal(fastify.pluginName, 'myPluginA')
t.ok(fastify.hasPlugin('myPluginA'))
fastify.register(function myPluginAB (fastify, opts, done) {
t.equal(fastify.pluginName, 'myPluginAB')
t.ok(fastify.hasPlugin('myPluginAB'))
done()
})
setImmediate(() => {
// exact name due to encapsulation
t.equal(fastify.pluginName, 'myPluginA')
t.ok(fastify.hasPlugin('myPluginA'))
})
done()
})

fastify.register(function myPluginB (fastify, opts, done) {
t.equal(fastify.pluginName, 'myPluginB')
t.ok(fastify.hasPlugin('myPluginB'))
done()
})

Expand Down

0 comments on commit 03b8f46

Please sign in to comment.