diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7b002766..5395d92e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ on: jobs: test: - uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3 + uses: fastify/workflows/.github/workflows/plugins-ci.yml@v4.1.0 with: license-check: true lint: true diff --git a/.swcrc b/.swcrc deleted file mode 100644 index a2d5b04f..00000000 --- a/.swcrc +++ /dev/null @@ -1,8 +0,0 @@ -{ - "jsc": { - "parser": { - "syntax": "typescript" - }, - "target": "es2016" - } -} diff --git a/.taprc b/.taprc index 03d6c227..e2dd1e4d 100644 --- a/.taprc +++ b/.taprc @@ -1,3 +1,4 @@ -ts: true -jsx: false -flow: false +# vim: set filetype=yaml : +timeout: 60 +plugin: + - "!@tapjs/typescript" diff --git a/index.js b/index.js index 2fe75848..3dd39db3 100644 --- a/index.js +++ b/index.js @@ -108,9 +108,11 @@ const fastifyAutoload = async function autoload (fastify, options) { async function getPackageType (cwd) { const directories = cwd.split(sep) + /* c8 ignore start */ // required for paths that begin with the sep, such as linux root - directories[0] = /* istanbul ignore next - OS specific */ directories[0] !== '' ? directories[0] : sep - + // ignore because OS specific evaluation + directories[0] = directories[0] !== '' ? directories[0] : sep + /* c8 ignore stop */ while (directories.length > 0) { const filePath = join(...directories, 'package.json') diff --git a/package.json b/package.json index 1f3b0d20..843414a1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fastify/autoload", - "version": "5.8.2", + "version": "5.8.1", "description": "Require all plugins in a directory", "main": "index.js", "type": "commonjs", @@ -8,11 +8,10 @@ "scripts": { "lint": "standard | snazzy", "lint:fix": "standard --fix | snazzy", - "test": "npm run unit && npm run typescript && npm run typescript:jest && npm run typescript:esm && npm run typescript:swc && npm run typescript:swc-node-register && npm run typescript:tsm && npm run typescript:tsx && npm run typescript:vitest && npm run typescript:esbuild", + "test": "npm run typescript && npm run typescript:jest && npm run typescript:swc-node-register && npm run typescript:tsm && npm run typescript:tsx && npm run typescript:vitest && npm run typescript:esbuild && npm run unit", "typescript": "tsd", "typescript:jest": "jest", "typescript:esm": "node scripts/unit-typescript-esm.js", - "typescript:swc": "node scripts/unit-typescript-swc.js", "typescript:swc-node-register": "node scripts/unit-typescript-swc-node-register.js", "typescript:tsm": "node scripts/unit-typescript-tsm.js", "typescript:tsx": "node scripts/unit-typescript-tsx.js", @@ -20,7 +19,7 @@ "typescript:vitest": "vitest run", "typescript:vitest:dev": "vitest", "unit": "node scripts/unit.js", - "unit:with-modules": "tap test/issues/*/test.js test/commonjs/*.js test/module/*.js test/typescript/*.ts" + "unit:with-modules": " tap plugin rm @tapjs/typescript && tap test/issues/*/test.js test/commonjs/*.js test/module/*.js" }, "repository": { "type": "git", @@ -43,33 +42,31 @@ "url": "https://github.com/fastify/fastify-autoload/issues" }, "homepage": "https://github.com/fastify/fastify-autoload#readme", - "dependencies": {}, "devDependencies": { - "@fastify/pre-commit": "^2.0.2", - "@fastify/url-data": "^5.0.0", - "@swc-node/register": "^1.5.1", - "@swc/core": "^1.2.129", - "@swc/register": "^0.1.9", - "@types/jest": "^29.0.0", - "@types/node": "^20.1.0", - "@types/tap": "^15.0.5", - "esbuild": "^0.19.2", - "esbuild-register": "^3.4.1", - "fastify": "^4.0.0-rc.2", - "fastify-plugin": "^4.0.0", - "jest": "^28.1.3", + "@fastify/pre-commit": "^2.1.0", + "@fastify/url-data": "^5.4.0", + "@swc-node/register": "^1.9.1", + "@swc/core": "^1.4.13", + "@types/jest": "^29.5.12", + "@types/node": "^20.12.7", + "@types/tap": "^15.0.11", + "esbuild": "^0.20.2", + "esbuild-register": "^3.5.0", + "fastify": "^4.26.2", + "fastify-plugin": "^4.5.1", + "jest": "^29.7.0", "snazzy": "^9.0.0", - "standard": "^17.0.0", - "tap": "^16.0.0", - "ts-jest": "^28.0.7", - "ts-node": "^10.4.0", + "standard": "^17.1.0", + "tap": "^18.7.2", + "ts-jest": "^29.1.2", + "ts-node": "^10.9.2", "ts-node-dev": "^2.0.0", - "tsd": "^0.29.0", - "tsm": "^2.2.1", - "tsx": "^3.7.1", - "typescript": "^5.0.2", - "vite": "^5.0.0", - "vitest": "^0.34.1" + "tsd": "^0.31.0", + "tsm": "^2.3.0", + "tsx": "^4.7.2", + "typescript": "^5.4.5", + "vite": "^5.2.8", + "vitest": "^1.5.0" }, "standard": { "ignore": [ diff --git a/runtime.js b/runtime.js index c5ef5d65..ed3623f2 100644 --- a/runtime.js +++ b/runtime.js @@ -1,26 +1,29 @@ 'use strict' -// TODO: change x ?? (x = y) to x ??= y when next major // runtime cache const cache = {} let processArgv function checkProcessArgv (moduleName) { - /* istanbul ignore next - nullish needed for non Node.js runtime */ - processArgv ?? (processArgv = (process.execArgv ?? []).concat(process.argv ?? [])) + /* c8 ignore start */ + // nullish needed for non Node.js runtime + processArgv ??= (process.execArgv ?? []).concat(process.argv ?? []) + /* c8 ignore stop */ return processArgv.some((arg) => arg.indexOf(moduleName) >= 0) } let preloadModules function checkPreloadModules (moduleName) { - /* istanbul ignore next - nullish needed for non Node.js runtime */ - preloadModules ?? (preloadModules = (process._preload_modules ?? [])) + /* c8 ignore start */ + // nullish needed for non Node.js runtime + preloadModules ??= (process._preload_modules ?? []) + /* c8 ignore stop */ return preloadModules.includes(moduleName) } let preloadModulesString function checkPreloadModulesString (moduleName) { - preloadModulesString ?? (preloadModulesString = preloadModules.toString()) + preloadModulesString ??= preloadModules.toString() return preloadModulesString.includes(moduleName) } @@ -35,69 +38,69 @@ const runtime = {} Object.defineProperties(runtime, { tsNode: { get () { - cache.tsNode ?? (cache.tsNode = ( + cache.tsNode ??= ( // --require tsnode/register (Symbol.for('ts-node.register.instance') in process) || // --loader ts-node/esm checkProcessArgv('ts-node/esm') || // ts-node-dev !!process.env.TS_NODE_DEV - )) + ) return cache.tsNode } }, babelNode: { get () { - cache.babelNode ?? (cache.babelNode = checkProcessArgv('babel-node')) + cache.babelNode ??= checkProcessArgv('babel-node') return cache.babelNode } }, vitest: { get () { - cache.vitest ?? (cache.vitest = ( + cache.vitest ??= ( checkEnvVariable('VITEST', 'true') || checkEnvVariable('VITEST_WORKER_ID') - )) + ) return cache.vitest } }, jest: { get () { - cache.jest ?? (cache.jest = checkEnvVariable('JEST_WORKER_ID')) + cache.jest ??= checkEnvVariable('JEST_WORKER_ID') return cache.jest } }, swc: { get () { - cache.swc ?? (cache.swc = ( + cache.swc ??= ( checkPreloadModules('@swc/register') || checkPreloadModules('@swc-node/register') || checkProcessArgv('.bin/swc-node') - )) + ) return cache.swc } }, tsm: { get () { - cache.tsm ?? (cache.tsm = checkPreloadModules('tsm')) + cache.tsm ??= checkPreloadModules('tsm') return cache.tsm } }, esbuild: { get () { - cache.esbuild ?? (cache.esbuild = checkPreloadModules('esbuild-register')) + cache.esbuild ??= checkPreloadModules('esbuild-register') return cache.esbuild } }, tsx: { get () { - cache.tsx ?? (cache.tsx = checkPreloadModulesString('tsx')) + cache.tsx ??= checkPreloadModulesString('tsx') return cache.tsx } }, supportTypeScript: { get () { - cache.supportTypeScript ?? (cache.supportTypeScript = ( + cache.supportTypeScript ??= ( checkEnvVariable('FASTIFY_AUTOLOAD_TYPESCRIPT') || runtime.tsNode || runtime.vitest || @@ -107,17 +110,17 @@ Object.defineProperties(runtime, { runtime.tsm || runtime.tsx || runtime.esbuild - )) + ) return cache.supportTypeScript } }, forceESM: { get () { - cache.forceESM ?? (cache.forceESM = ( + cache.forceESM ??= ( checkProcessArgv('ts-node/esm') || runtime.vitest || false - )) + ) return cache.forceESM } } diff --git a/scripts/unit-typescript-esm.js b/scripts/unit-typescript-esm.js index d9d6caec..5337f77e 100644 --- a/scripts/unit-typescript-esm.js +++ b/scripts/unit-typescript-esm.js @@ -3,10 +3,8 @@ const { exec } = require('node:child_process') const args = [ - 'tap', - '--node-arg=--loader=ts-node/esm', - '--node-arg=--experimental-specifier-resolution=node', - '--no-coverage', + 'node', + '--loader=ts-node/esm', 'test/typescript-esm/*.ts' ] diff --git a/scripts/unit-typescript-swc-node-register.js b/scripts/unit-typescript-swc-node-register.js index 2fbf7738..c73440f3 100644 --- a/scripts/unit-typescript-swc-node-register.js +++ b/scripts/unit-typescript-swc-node-register.js @@ -3,9 +3,8 @@ const { exec } = require('node:child_process') const args = [ - 'tap', - '--node-arg=--require=@swc-node/register', - '--no-coverage', + 'node', + '--require=@swc-node/register', 'test/typescript/*.ts' ] diff --git a/scripts/unit-typescript-swc.js b/scripts/unit-typescript-swc.js deleted file mode 100644 index 4648e021..00000000 --- a/scripts/unit-typescript-swc.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict' - -const { exec } = require('node:child_process') - -const args = [ - 'tap', - '--node-arg=--require=@swc/register', - '--no-coverage', - 'test/typescript/*.ts' -] - -const child = exec(args.join(' '), { - shell: true -}) - -child.stdout.pipe(process.stdout) -child.stderr.pipe(process.stderr) -child.once('close', process.exit) diff --git a/scripts/unit-typescript-tsm.js b/scripts/unit-typescript-tsm.js index f2eb48bc..bc819103 100644 --- a/scripts/unit-typescript-tsm.js +++ b/scripts/unit-typescript-tsm.js @@ -3,10 +3,8 @@ const { exec } = require('node:child_process') const args = [ - 'tap', - '--no-ts', - '--node-arg=--require=tsm', - '--no-coverage', + 'node', + '--require=tsm', 'test/typescript/*.ts' ] diff --git a/test/commonjs/autohooks/routes/.autohooks.js b/test/commonjs/autohooks/routes/.autohooks.js index 1c0b192d..1e551aef 100644 --- a/test/commonjs/autohooks/routes/.autohooks.js +++ b/test/commonjs/autohooks/routes/.autohooks.js @@ -1,7 +1,6 @@ -module.exports = async function (app, opts, next) { +module.exports = async function (app, opts) { app.addHook('onRequest', async (req, reply) => { req.hooked = req.hooked || [] req.hooked.push('root') - next() }) } diff --git a/test/commonjs/autohooks/routes/child/.autohooks.js b/test/commonjs/autohooks/routes/child/.autohooks.js index 79bcd565..b90b1884 100644 --- a/test/commonjs/autohooks/routes/child/.autohooks.js +++ b/test/commonjs/autohooks/routes/child/.autohooks.js @@ -1,7 +1,6 @@ -module.exports = async function (app, opts, next) { +module.exports = async function (app, opts) { app.addHook('onRequest', async (req, reply) => { req.hooked = req.hooked || [] req.hooked.push('child') - next() }) } diff --git a/test/commonjs/autohooks/routes/child/grandchild/.autohooks.js b/test/commonjs/autohooks/routes/child/grandchild/.autohooks.js index 59eaf44b..9e466fe8 100644 --- a/test/commonjs/autohooks/routes/child/grandchild/.autohooks.js +++ b/test/commonjs/autohooks/routes/child/grandchild/.autohooks.js @@ -1,7 +1,6 @@ -module.exports = async function (app, opts, next) { +module.exports = async function (app, opts) { app.addHook('onRequest', async (req, reply) => { req.hooked = req.hooked || [] req.hooked.push('grandchild') - next() }) } diff --git a/test/commonjs/autohooks/routes/child/grandchild/routes.js b/test/commonjs/autohooks/routes/child/grandchild/routes.js index 33d10213..521b6527 100644 --- a/test/commonjs/autohooks/routes/child/grandchild/routes.js +++ b/test/commonjs/autohooks/routes/child/grandchild/routes.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async function (app, opts, next) { +module.exports = async function (app, opts) { app.get('/', async function (req, reply) { reply.status(200).send({ hooked: req.hooked }) }) diff --git a/test/commonjs/autohooks/routes/child/routes.js b/test/commonjs/autohooks/routes/child/routes.js index 33d10213..521b6527 100644 --- a/test/commonjs/autohooks/routes/child/routes.js +++ b/test/commonjs/autohooks/routes/child/routes.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async function (app, opts, next) { +module.exports = async function (app, opts) { app.get('/', async function (req, reply) { reply.status(200).send({ hooked: req.hooked }) }) diff --git a/test/commonjs/autohooks/routes/routes.js b/test/commonjs/autohooks/routes/routes.js index 33d10213..521b6527 100644 --- a/test/commonjs/autohooks/routes/routes.js +++ b/test/commonjs/autohooks/routes/routes.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async function (app, opts, next) { +module.exports = async function (app, opts) { app.get('/', async function (req, reply) { reply.status(200).send({ hooked: req.hooked }) }) diff --git a/test/commonjs/autohooks/routes/sibling/routes.js b/test/commonjs/autohooks/routes/sibling/routes.js index 33d10213..521b6527 100644 --- a/test/commonjs/autohooks/routes/sibling/routes.js +++ b/test/commonjs/autohooks/routes/sibling/routes.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async function (app, opts, next) { +module.exports = async function (app, opts) { app.get('/', async function (req, reply) { reply.status(200).send({ hooked: req.hooked }) }) diff --git a/test/commonjs/basic/index/bar/index.js b/test/commonjs/basic/index/bar/index.js index a1bb94af..a0fbcb2a 100644 --- a/test/commonjs/basic/index/bar/index.js +++ b/test/commonjs/basic/index/bar/index.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async (server, opts, next) => { +module.exports = async (server, opts) => { server.get('/', async (request, reply) => { reply.status(200).send({ works: true }) }) diff --git a/test/commonjs/basic/index/ignored.js b/test/commonjs/basic/index/ignored.js index 92894bb4..7351acb5 100644 --- a/test/commonjs/basic/index/ignored.js +++ b/test/commonjs/basic/index/ignored.js @@ -2,7 +2,7 @@ // This module should be ignored because an index.js file is present in the same directory -module.exports = async (server, opts, next) => { +module.exports = async (server, opts) => { server.get('/ignored', async (request, reply) => { reply.status(200).send({ works: true }) }) diff --git a/test/commonjs/basic/index/index.js b/test/commonjs/basic/index/index.js index a1bb94af..a0fbcb2a 100644 --- a/test/commonjs/basic/index/index.js +++ b/test/commonjs/basic/index/index.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async (server, opts, next) => { +module.exports = async (server, opts) => { server.get('/', async (request, reply) => { reply.status(200).send({ works: true }) }) diff --git a/test/commonjs/basic/routeParams/foo/_id1/bar/_id2/index.js b/test/commonjs/basic/routeParams/foo/_id1/bar/_id2/index.js index 995f54b4..8fd7611c 100644 --- a/test/commonjs/basic/routeParams/foo/_id1/bar/_id2/index.js +++ b/test/commonjs/basic/routeParams/foo/_id1/bar/_id2/index.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async (server, opts, next) => { +module.exports = async (server, opts) => { server.get('/', async (request, reply) => { const params = request.params reply.status(200).send({ works: true, ...params }) diff --git a/test/commonjs/basic/routeParams/foo/_id1/index.js b/test/commonjs/basic/routeParams/foo/_id1/index.js index 995f54b4..8fd7611c 100644 --- a/test/commonjs/basic/routeParams/foo/_id1/index.js +++ b/test/commonjs/basic/routeParams/foo/_id1/index.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async (server, opts, next) => { +module.exports = async (server, opts) => { server.get('/', async (request, reply) => { const params = request.params reply.status(200).send({ works: true, ...params }) diff --git a/test/commonjs/basic/routeParams/foo/index.js b/test/commonjs/basic/routeParams/foo/index.js index a1bb94af..a0fbcb2a 100644 --- a/test/commonjs/basic/routeParams/foo/index.js +++ b/test/commonjs/basic/routeParams/foo/index.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async (server, opts, next) => { +module.exports = async (server, opts) => { server.get('/', async (request, reply) => { reply.status(200).send({ works: true }) }) diff --git a/test/commonjs/basic/routeParams/index.js b/test/commonjs/basic/routeParams/index.js index a1bb94af..a0fbcb2a 100644 --- a/test/commonjs/basic/routeParams/index.js +++ b/test/commonjs/basic/routeParams/index.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async (server, opts, next) => { +module.exports = async (server, opts) => { server.get('/', async (request, reply) => { reply.status(200).send({ works: true }) }) diff --git a/test/commonjs/route-parameters/routes/__country-__language/actions.js b/test/commonjs/route-parameters/routes/__country-__language/actions.js index ceec4507..a9037773 100644 --- a/test/commonjs/route-parameters/routes/__country-__language/actions.js +++ b/test/commonjs/route-parameters/routes/__country-__language/actions.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async function (app, opts, next) { +module.exports = async function (app, opts) { app.get('/', async function (req, reply) { reply.status(200).send(req.params) }) diff --git a/test/commonjs/route-parameters/routes/users/_id/actions.js b/test/commonjs/route-parameters/routes/users/_id/actions.js index ccea4884..783425d7 100644 --- a/test/commonjs/route-parameters/routes/users/_id/actions.js +++ b/test/commonjs/route-parameters/routes/users/_id/actions.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async function (app, opts, next) { +module.exports = async function (app, opts) { app.get('/', async function (req, reply) { reply.status(200).send({ user: { id: req.params.id || 'null', username: 'example' } }) // explicit null reply to preserve response body }) diff --git a/test/commonjs/route-parameters/routes/users/index.js b/test/commonjs/route-parameters/routes/users/index.js index 1997dd52..853af697 100644 --- a/test/commonjs/route-parameters/routes/users/index.js +++ b/test/commonjs/route-parameters/routes/users/index.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async function (app, opts, next) { +module.exports = async function (app, opts) { app.get('/', async function (req, reply) { reply.status(200).send({ users: [{ id: 7, username: 'example' }] }) }) diff --git a/test/issues/369/routes/child/routes.js b/test/issues/369/routes/child/routes.js index 33d10213..521b6527 100644 --- a/test/issues/369/routes/child/routes.js +++ b/test/issues/369/routes/child/routes.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async function (app, opts, next) { +module.exports = async function (app, opts) { app.get('/', async function (req, reply) { reply.status(200).send({ hooked: req.hooked }) }) diff --git a/test/issues/369/routes/promisified/routes.js b/test/issues/369/routes/promisified/routes.js index 33d10213..521b6527 100644 --- a/test/issues/369/routes/promisified/routes.js +++ b/test/issues/369/routes/promisified/routes.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async function (app, opts, next) { +module.exports = async function (app, opts) { app.get('/', async function (req, reply) { reply.status(200).send({ hooked: req.hooked }) }) diff --git a/test/issues/369/routes/routes.js b/test/issues/369/routes/routes.js index 33d10213..521b6527 100644 --- a/test/issues/369/routes/routes.js +++ b/test/issues/369/routes/routes.js @@ -1,6 +1,6 @@ 'use strict' -module.exports = async function (app, opts, next) { +module.exports = async function (app, opts) { app.get('/', async function (req, reply) { reply.status(200).send({ hooked: req.hooked }) })