diff --git a/boot.js b/boot.js index 23baf2b..201a94a 100644 --- a/boot.js +++ b/boot.js @@ -10,12 +10,15 @@ const { AVV_ERR_ROOT_PLG_BOOTED, AVV_ERR_READY_TIMEOUT } = require('./lib/errors') +const { + kAvvio, + kIsOnCloseHandler, + kThenifyDoNotWrap +} = require('./lib/symbols') const { TimeTree } = require('./lib/time-tree') const { Plugin } = require('./plugin') const { debug } = require('./lib/debug') const { loadPlugin } = require('./lib/load-plugin') -const kAvvio = Symbol('kAvvio') -const kThenifyDoNotWrap = Symbol('kThenifyDoNotWrap') function wrap (server, opts, instance) { const expose = opts.expose || {} @@ -117,7 +120,7 @@ function Boot (server, opts, done) { this._server = server this._current = [] this._error = null - this._isOnCloseHandlerKey = Symbol('isOnCloseHandler') + this._isOnCloseHandlerKey = kIsOnCloseHandler this._lastUsed = null this.setMaxListeners(0) diff --git a/lib/get-plugin-name.js b/lib/get-plugin-name.js index 8624479..79bf79a 100644 --- a/lib/get-plugin-name.js +++ b/lib/get-plugin-name.js @@ -1,7 +1,7 @@ 'use strict' // this symbol is assigned by fastify-plugin -const kPluginMeta = Symbol.for('plugin-meta') +const { kPluginMeta } = require('./symbols') /** * @param {function} plugin diff --git a/lib/symbols.js b/lib/symbols.js new file mode 100644 index 0000000..62cdf38 --- /dev/null +++ b/lib/symbols.js @@ -0,0 +1,26 @@ +'use strict' + +// Internal Symbols +const kAvvio = Symbol('avvio.Boot') +const kIsOnCloseHandler = Symbol('isOnCloseHandler') +const kThenifyDoNotWrap = Symbol('avvio.ThenifyDoNotWrap') +const kUntrackNode = Symbol('avvio.TimeTree.untrackNode') +const kTrackNode = Symbol('avvio.TimeTree.trackNode') +const kGetParent = Symbol('avvio.TimeTree.getParent') +const kGetNode = Symbol('avvio.TimeTree.getNode') +const kAddNode = Symbol('avvio.TimeTree.addNode') + +// Public Symbols +const kPluginMeta = Symbol.for('plugin-meta') + +module.exports = { + kAvvio, + kIsOnCloseHandler, + kThenifyDoNotWrap, + kUntrackNode, + kTrackNode, + kGetParent, + kGetNode, + kAddNode, + kPluginMeta +} diff --git a/lib/time-tree.js b/lib/time-tree.js index 6a7c304..61e3ca3 100644 --- a/lib/time-tree.js +++ b/lib/time-tree.js @@ -1,10 +1,12 @@ 'use strict' -const kUntrackNode = Symbol('avvio.TimeTree.untrackNode') -const kTrackNode = Symbol('avvio.TimeTree.trackNode') -const kGetParent = Symbol('avvio.TimeTree.getParent') -const kGetNode = Symbol('avvio.TimeTree.getNode') -const kAddNode = Symbol('avvio.TimeTree.addNode') +const { + kUntrackNode, + kTrackNode, + kGetParent, + kGetNode, + kAddNode +} = require('./symbols') /** * Node of the TimeTree diff --git a/test/lib/get-plugin-name.test.js b/test/lib/get-plugin-name.test.js index 28e7e5e..2cb3853 100644 --- a/test/lib/get-plugin-name.test.js +++ b/test/lib/get-plugin-name.test.js @@ -2,6 +2,7 @@ const { test } = require('tap') const { getPluginName } = require('../../lib/get-plugin-name') +const { kPluginMeta } = require('../../lib/symbols') test('getPluginName of function', (t) => { t.plan(1) @@ -36,7 +37,7 @@ test("getPluginName based on Symbol 'plugin-meta' /1", (t) => { } - plugin[Symbol.for('plugin-meta')] = {} + plugin[kPluginMeta] = {} t.equal(getPluginName(plugin), 'plugin') }) @@ -47,7 +48,7 @@ test("getPluginName based on Symbol 'plugin-meta' /2", (t) => { } - plugin[Symbol.for('plugin-meta')] = { + plugin[kPluginMeta] = { name: 'fastify-non-existent' } t.equal(getPluginName(plugin), 'fastify-non-existent') diff --git a/test/plugin-name.test.js b/test/plugin-name.test.js index 4c81643..1008396 100644 --- a/test/plugin-name.test.js +++ b/test/plugin-name.test.js @@ -2,13 +2,14 @@ const { test } = require('tap') const boot = require('..') +const { kPluginMeta } = require('../lib/symbols') test('plugins get a name from the plugin metadata if it is set', async (t) => { t.plan(1) const app = boot() const func = (app, opts, next) => next() - func[Symbol.for('plugin-meta')] = { name: 'a-test-plugin' } + func[kPluginMeta] = { name: 'a-test-plugin' } app.use(func) await app.ready()