Skip to content

Commit

Permalink
extract symbols (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Jun 25, 2023
1 parent 5a83d50 commit 1a15d14
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
9 changes: 6 additions & 3 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/get-plugin-name.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 26 additions & 0 deletions lib/symbols.js
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 7 additions & 5 deletions lib/time-tree.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions test/lib/get-plugin-name.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
})

Expand All @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion test/plugin-name.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 1a15d14

Please sign in to comment.