Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register ecmascript modules #105

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
49 changes: 49 additions & 0 deletions test/ecmascript-modules.test.mjs
Original file line number Diff line number Diff line change
@@ -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/)
})
3 changes: 3 additions & 0 deletions test/sources/module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function(app, opts, done) {
done()
}
Loading