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
37 changes: 24 additions & 13 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,37 @@ function resolvePkgPath (mainFilename) {
return join(dirname(require.resolve('fastify', { paths: [mainFilename] })), 'package.json')
}

function tryGetPath (p) {
var pkgPath
try {
pkgPath = resolvePkgPath(p)
} catch (_) {
}
return pkgPath
}

function checkVersion (version, pluginName) {
if (typeof version !== 'string') {
throw new TypeError(`fastify-plugin expects a version string, instead got '${typeof version}'`)
}

// TODO refactor this check and move it inside Fastify itself.
// TODO refactor this check and move it inside Fastify itself. https://github.com/fastify/fastify/issues/2507
var fastifyVersion
try {
var pkgPath
if (require.main && require.main.filename) {
// We need to dynamically compute this to support yarn pnp
pkgPath = resolvePkgPath(require.main.filename)
} else if (process.argv[1]) {
// We need this to support native ESM context
pkgPath = resolvePkgPath(process.argv[1])
} else {
// In bundlers, there is no require.main.filename so we go ahead and require directly
pkgPath = 'fastify/package.json'
}
var pkgPath
if (require.main && require.main.filename) {
// We need to dynamically compute this to support yarn pnp
pkgPath = tryGetPath(require.main.filename)
}
if (!pkgPath && process.argv[1]) {
// We need this to support native ESM context
pkgPath = tryGetPath(process.argv[1])
}
if (!pkgPath) {
// In bundlers, there is no require.main.filename so we go ahead and require directly
pkgPath = 'fastify/package.json'
}

try {
fastifyVersion = semver.coerce(require(pkgPath).version)
} catch (_) {
console.info('fastify not found, proceeding anyway')
Expand Down
73 changes: 73 additions & 0 deletions test/checkVersion.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'

const { test } = require('tap')
const fp = require('../plugin')

test('checkVersion having require.main.filename', (t) => {
const info = console.info
t.ok(require.main.filename)
t.tearDown(() => {
console.info = info
})

console.info = function (msg) {
t.fail('logged: ' + msg)
}

fp((fastify, opts, next) => {
next()
}, {
fastify: '^3.0.0'
})

t.end()
})

test('checkVersion having no require.main.filename but process.argv[1]', (t) => {
const filename = require.main.filename
const info = console.info
t.tearDown(() => {
require.main.filename = filename
console.info = info
})

require.main.filename = null

console.info = function (msg) {
t.fail('logged: ' + msg)
}

fp((fastify, opts, next) => {
next()
}, {
fastify: '^3.0.0'
})

t.end()
})

test('checkVersion having no require.main.filename and no process.argv[1]', (t) => {
const filename = require.main.filename
const argv = process.argv
const info = console.info
t.tearDown(() => {
require.main.filename = filename
process.argv = argv
console.info = info
})

require.main.filename = null
process.argv[1] = null

console.info = function (msg) {
t.fail('logged: ' + msg)
}

fp((fastify, opts, next) => {
next()
}, {
fastify: '^3.0.0'
})

t.end()
})