From 0771c819e7d63515fce64d90769ab42f25238bf2 Mon Sep 17 00:00:00 2001 From: matthyk <53833818+matthyk@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:18:23 +0100 Subject: [PATCH 1/2] first implementation --- package.json | 2 +- test/ecmascript-modules.test.mjs | 49 ++++++++++++++++++++++++++++++++ test/sources/module.mjs | 3 ++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 test/ecmascript-modules.test.mjs create mode 100644 test/sources/module.mjs diff --git a/package.json b/package.json index 769865a..905b431 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "lint": "standard && npm run lint:typescript", "lint:fix": "standard --fix && npm run lint:typescript -- --fix", "lint:typescript": "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin *.ts test/**/*.ts", - "test": "tap test/**/*.test.js && npm run test:typescript", + "test": "tap test/**/*.test.{js,mjs} && npm run test:typescript", "test:typescript": "tsd" }, "repository": { diff --git a/test/ecmascript-modules.test.mjs b/test/ecmascript-modules.test.mjs new file mode 100644 index 0000000..05832cb --- /dev/null +++ b/test/ecmascript-modules.test.mjs @@ -0,0 +1,49 @@ +import t from 'tap' +import Fastify from 'fastify' +import fastifyOverview from '../index.js' + +// See https://github.com/Eomm/fastify-overview/issues/98#issuecomment-1943830833 for more details + +t.test('register module namespace object', async t => { + const app = Fastify() + await app.register(fastifyOverview, { addSource: true }) + + app.register(await import('./sources/module.mjs')) + + try { + await app.ready() + } catch (e) { + t.fail() + } + + const overview = app.overview() + + t.equal(overview.children.length, 1) + + const child0 = overview.children[0] + t.ok(child0.source) + t.match(child0.source.fileName, /test\/sources\/module\.mjs$/) +}) + +t.test('register promise of module namespace object', async t => { + let out + const logger = { + debug: (msg) => {}, + info: (msg) => {}, + error: (msg) => {}, + fatal: (msg) => {}, + warn: (msg) => { out += msg }, + trace: (msg) => {}, + child: () => logger + } + + const app = Fastify({ logger }) + + await app.register(fastifyOverview) + + app.register(import('./sources/module.mjs')) + + await app.ready() + + t.match(out, /Promise like plugin/) +}) diff --git a/test/sources/module.mjs b/test/sources/module.mjs new file mode 100644 index 0000000..ff612b6 --- /dev/null +++ b/test/sources/module.mjs @@ -0,0 +1,3 @@ +export default function(app, opts, done) { + done() +} From 4de5f5f73496120fc9bd3b77d0b06a5ab8e52820 Mon Sep 17 00:00:00 2001 From: matthyk <53833818+matthyk@users.noreply.github.com> Date: Thu, 29 Feb 2024 23:19:08 +0100 Subject: [PATCH 2/2] Update index.js --- index.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/index.js b/index.js index c1c4596..8b2dfc2 100644 --- a/index.js +++ b/index.js @@ -118,6 +118,15 @@ function wrapFastify (instance, pluginOpts) { // *** register const originalRegister = instance.register instance.register = function wrapRegister (pluginFn, opts) { + if (isPromiseLike(pluginFn)) { + instance.log.warn('Promise like plugin functions are not supported by fastify-overview.') + return originalRegister.call(this, pluginFn, opts) + } + + if (isBundledOrTypescriptPlugin(pluginFn)) { + pluginFn = pluginFn.default + } + if (pluginOpts.addSource) { // this Symbol is processed by the `onRegister` hook if necessary pluginFn[kSourceRegister] = getSource()[0] @@ -167,6 +176,24 @@ function wrapFastify (instance, pluginOpts) { } } +// From https://github.com/fastify/avvio/blob/a153be8358ece6a1ed970d0bee2c28a8230175b9/lib/is-bundled-or-typescript-plugin.js#L13-L19 +function isBundledOrTypescriptPlugin (maybeBundledOrTypescriptPlugin) { + return ( + maybeBundledOrTypescriptPlugin !== null && + typeof maybeBundledOrTypescriptPlugin === 'object' && + typeof maybeBundledOrTypescriptPlugin.default === 'function' + ) +} + +// From https://github.com/fastify/avvio/blob/a153be8358ece6a1ed970d0bee2c28a8230175b9/lib/is-promise-like.js#L7-L13 +function isPromiseLike (maybePromiseLike) { + return ( + maybePromiseLike !== null && + typeof maybePromiseLike === 'object' && + typeof maybePromiseLike.then === 'function' + ) +} + function wrapDecorator (instance, type, { addSource, onDecorateDefinition }) { const originalDecorate = instance[type] instance[type] = function wrapDecorate (name, value) {