Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ Boot.prototype._loadPlugin = function (plugin, callback) {
if (typeof fn.default === 'function') {
fn = fn.default
}
plugin.func = fn
plugin.setFunc(fn)
this._loadPlugin(plugin, callback)
}, callback)
return
Expand Down
13 changes: 13 additions & 0 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ function Plugin (queue, func, options, isAfter, timeout) {

inherits(Plugin, EventEmitter)

/**
* Replaces the plugin function, keeping the derived name in sync.
*
* Used when a thenable plugin resolves to the actual plugin function, as the
* name derived from the thenable itself is meaningless.
*
* @param {function} func
*/
Plugin.prototype.setFunc = function (func) {
this.func = func
this.name = getPluginName(func, this.options)
}

/**
* @callback ExecCallback
* @param {Error|null} execErr
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/esm-named.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default async function esmNamedPlugin (app) {
app.loaded = true
}
3 changes: 3 additions & 0 deletions test/fixtures/esm-no-next.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function esmPluginNoNext (app, opts, next) {
// do not call next on purpose
}
8 changes: 8 additions & 0 deletions test/fixtures/esm-plugin-meta.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
async function plugin (app) {
app.loaded = true
}

// this symbol is assigned by fastify-plugin
plugin[Symbol.for('plugin-meta')] = { name: 'esm-plugin-meta' }

export default plugin
36 changes: 36 additions & 0 deletions test/plugin-name.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,39 @@ test('plugins get a name from the function source if theres no other option', as
t.assert.strictEqual(jsonToCompare.label, 'root')
t.assert.strictEqual(jsonToCompare.nodes[0].label, '(app, opts, next) => next()')
})

test('thenable plugins get a name from the resolved plugin metadata if it is set', async (t) => {
t.plan(2)
const app = boot()

app.use(import('./fixtures/esm-plugin-meta.mjs'))
await app.ready()

const jsonToCompare = app.toJSON()
t.assert.strictEqual(jsonToCompare.label, 'root')
t.assert.strictEqual(jsonToCompare.nodes[0].label, 'esm-plugin-meta')
})

test('thenable plugins get a name from the options if theres no metadata', async (t) => {
t.plan(2)
const app = boot()

app.use(import('./fixtures/esm-named.mjs'), { name: 'test registration options name' })
await app.ready()

const jsonToCompare = app.toJSON()
t.assert.strictEqual(jsonToCompare.label, 'root')
t.assert.strictEqual(jsonToCompare.nodes[0].label, 'test registration options name')
})

test('thenable plugins get a name from the resolved function name if theres no name in the options and no metadata', async (t) => {
t.plan(2)
const app = boot()

app.use(import('./fixtures/esm-named.mjs'))
await app.ready()

const jsonToCompare = app.toJSON()
t.assert.strictEqual(jsonToCompare.label, 'root')
t.assert.strictEqual(jsonToCompare.nodes[0].label, 'esmNamedPlugin')
})
15 changes: 15 additions & 0 deletions test/plugin-timeout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ test('timeout without calling next - use code as name', (t, done) => {
})
})

test('timeout without calling next - use the name of the resolved thenable', (t, done) => {
t.plan(3)
const app = boot({}, {
timeout: 10 // 10 ms
})
app.use(import('./fixtures/esm-no-next.mjs'))

app.ready((err) => {
t.assert.ok(err)
t.assert.strictEqual(err.message, message('esmPluginNoNext'))
t.assert.strictEqual(err.code, 'AVV_ERR_PLUGIN_EXEC_TIMEOUT')
done()
})
})

test('does not keep going', (t, done) => {
t.plan(2)
const app = boot({}, {
Expand Down
Loading