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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![CI](https://github.com/fastify/fastify-autoload/workflows/CI/badge.svg)
[![NPM version](https://img.shields.io/npm/v/@fastify/autoload.svg?style=flat)](https://www.npmjs.com/package/@fastify/autoload)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

Convenience plugin for Fastify that loads all plugins found in a directory and automatically configures routes matching the folder structure.

Expand Down
11 changes: 11 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

module.exports = require('neostandard')({
ignores: [
...require('neostandard').resolveIgnoresFromGitignore(),
'test/commonjs/syntax-error/lib/a.js',
'test/issues/369/invalid-autohooks',
'test/issues/369/non-SyntaxError'
],
ts: true
})
12 changes: 3 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"type": "commonjs",
"types": "types/index.d.ts",
"scripts": {
"lint": "standard | snazzy",
"lint:fix": "standard --fix | snazzy",
"lint": "eslint",
"lint:fix": "eslint --fix",
"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",
Expand Down Expand Up @@ -56,8 +56,7 @@
"fastify": "^5.0.0",
"fastify-plugin": "^5.0.0",
"jest": "^29.7.0",
"snazzy": "^9.0.0",
"standard": "^17.1.0",
"neostandard": "^0.11.9",
"tap": "^19.0.2",
"ts-jest": "^29.1.4",
"ts-node": "^10.9.2",
Expand All @@ -70,11 +69,6 @@
"vite": "^5.2.12",
"vitest": "^2.0.3"
},
"standard": {
"ignore": [
"test/*/*-error/lib/a.js"
]
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
Expand Down
6 changes: 3 additions & 3 deletions test/commonjs/babel-node/routes/foo/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = async (fastify) => {
fastify.get("/", function () {
return { foo: "bar" };
fastify.get('/', function () {
return { foo: 'bar' }
})
};
}
6 changes: 3 additions & 3 deletions test/commonjs/babel-node/routes/root.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = async (fastify) => {
fastify.get("/", function () {
return { hello: "world" };
fastify.get('/', function () {
return { hello: 'world' }
})
};
}
6 changes: 3 additions & 3 deletions test/commonjs/ts-node/routes/bar/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports.default = async (fastify: any) => {
fastify.get("/", function () {
return { bar: "bar" };
fastify.get('/', function () {
return { bar: 'bar' }
})
};
}
6 changes: 3 additions & 3 deletions test/commonjs/ts-node/routes/foo/baz/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports.default = async (fastify: any) => {
fastify.get("/customPath", function () {
return { baz: "baz" };
fastify.get('/customPath', function () {
return { baz: 'baz' }
})
};
}
6 changes: 3 additions & 3 deletions test/commonjs/ts-node/routes/foo/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports.default = async (fastify: any) => {
fastify.get("/", function () {
return { foo: "foo" };
fastify.get('/', function () {
return { foo: 'foo' }
})
};
}
6 changes: 3 additions & 3 deletions test/commonjs/ts-node/routes/root.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports.default = async (fastify: any) => {
fastify.get("/", function () {
return { hello: "world" };
fastify.get('/', function () {
return { hello: 'world' }
})
};
}
6 changes: 3 additions & 3 deletions test/issues/369/routes/.autohooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
req.hooked = req.hooked || []
req.hooked.push('root')
})
req.hooked = req.hooked || []
req.hooked.push('root')
})
}
8 changes: 4 additions & 4 deletions test/issues/369/routes/child/.autohooks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
req.hooked = req.hooked || []
req.hooked.push('child')
})
app.addHook('onRequest', async (req, reply) => {
req.hooked = req.hooked || []
req.hooked.push('child')
})
}
12 changes: 6 additions & 6 deletions test/issues/369/routes/promisified/.autohooks.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

module.exports = new Promise((res) => {
res(async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
req.hooked = req.hooked || []
req.hooked.push('promisified')
})
module.exports = new Promise((resolve) => {
resolve(async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
req.hooked = req.hooked || []
req.hooked.push('promisified')
})
})
})
6 changes: 3 additions & 3 deletions test/issues/374/routes/entity/.autohooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
req.hooked = req.hooked || []
req.hooked.push('root')
})
req.hooked = req.hooked || []
req.hooked.push('root')
})
}
6 changes: 3 additions & 3 deletions test/issues/376/routes/entity/.autohooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
req.hooked = req.hooked || []
req.hooked.push('root')
})
req.hooked = req.hooked || []
req.hooked.push('root')
})
}
10 changes: 5 additions & 5 deletions test/typescript-jest/babel-node/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import fastify from 'fastify'
import { join } from 'path'
import AutoLoad from '../../../'

describe("load typescript using babel-node", () => {
describe('load typescript using babel-node', () => {
const app = fastify()

beforeAll( done => {
beforeAll(done => {
app.register(AutoLoad, {
dir: join(__dirname, '../../commonjs/babel-node/routes')
})
app.ready(done)
})

it("tests the root route", async function () {
it('tests the root route', async function () {
const response = await app.inject({
method: 'GET',
url: '/'
})
expect(response.statusCode).toEqual(200)
expect(JSON.parse(response.payload)).toEqual({ hello: 'world' })
})
it("tests /foo route", async function () {
it('tests /foo route', async function () {
const response = await app.inject({
method: 'GET',
url: '/foo'
})
expect(response.statusCode).toBe(200)
expect(JSON.parse(response.payload)).toEqual({ foo: 'bar' })
})
})
})
2 changes: 1 addition & 1 deletion test/typescript-jest/basic/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ const app: FastifyPluginCallback = function (fastify, opts, next): void {
next()
}

export default app;
export default app
6 changes: 3 additions & 3 deletions test/typescript-jest/basic/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const app = fastify()

app.register(basicApp)

describe("load typescript plugin in jest environment", () => {
beforeAll( done => {
describe('load typescript plugin in jest environment', () => {
beforeAll(done => {
app.ready(done)
})

it("should response code 200 OK", async () => {
it('should response code 200 OK', async () => {
const { payload, statusCode } = await app.inject({
url: '/typescript'
})
Expand Down
16 changes: 8 additions & 8 deletions test/typescript-jest/integration/instance.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import fastify from "fastify";
import basicApp from "../../typescript/basic/app";
import fastify from 'fastify'
import basicApp from '../../typescript/basic/app'

const app = fastify();
app.register(basicApp);
const app = fastify()
app.register(basicApp)

app.listen({
port: Math.floor(Math.random() * 3000 + 3000)
}, function (err) {
if (err) process.stderr.write("failed");
process.stdout.write("success");
app.close();
});
if (err) process.stderr.write('failed')
process.stdout.write('success')
app.close()
})
42 changes: 21 additions & 21 deletions test/typescript-jest/integration/integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { exec } from "child_process";
import { exec } from 'child_process'

describe("integration test", function () {
test.concurrent.each(["ts-node", "ts-node-dev"])(
"integration with %s",
describe('integration test', function () {
test.concurrent.each(['ts-node', 'ts-node-dev'])(
'integration with %s',
async function (instance) {
await new Promise(function (resolve, reject) {
const child = exec(`${instance} "${process.cwd()}/test/typescript-jest/integration/instance.ts"`);
let stderr = "";
child.stderr?.on("data", function (b) {
stderr = stderr + b.toString();
});
let stdout = "";
child.stdout?.on("data", function (b) {
stdout = stdout + b.toString();
});
child.once("close", function () {
expect(stderr.includes("failed")).toStrictEqual(false);
expect(stdout.includes("success")).toStrictEqual(true);
resolve("");
});
});
const child = exec(`${instance} "${process.cwd()}/test/typescript-jest/integration/instance.ts"`)

Check warning

Code scanning / CodeQL

Shell command built from environment values

This shell command depends on an uncontrolled [absolute path](1).

Copilot Autofix

AI about 1 year ago

To fix the problem, we should avoid constructing the shell command using template literals and instead use the execFile method from the child_process module. This method allows us to pass the command and its arguments separately, which prevents the shell from interpreting special characters in the arguments.

We need to:

  1. Replace the exec method with execFile.
  2. Pass the command and its arguments as separate parameters to execFile.
Suggested changeset 1
test/typescript-jest/integration/integration.test.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/test/typescript-jest/integration/integration.test.ts b/test/typescript-jest/integration/integration.test.ts
--- a/test/typescript-jest/integration/integration.test.ts
+++ b/test/typescript-jest/integration/integration.test.ts
@@ -1,2 +1,2 @@
-import { exec } from 'child_process'
+import { execFile } from 'child_process'
 
@@ -7,3 +7,3 @@
       await new Promise(function (resolve, reject) {
-        const child = exec(`${instance} "${process.cwd()}/test/typescript-jest/integration/instance.ts"`)
+        const child = execFile(instance, [`${process.cwd()}/test/typescript-jest/integration/instance.ts`])
         let stderr = ''
EOF
@@ -1,2 +1,2 @@
import { exec } from 'child_process'
import { execFile } from 'child_process'

@@ -7,3 +7,3 @@
await new Promise(function (resolve, reject) {
const child = exec(`${instance} "${process.cwd()}/test/typescript-jest/integration/instance.ts"`)
const child = execFile(instance, [`${process.cwd()}/test/typescript-jest/integration/instance.ts`])
let stderr = ''
Copilot is powered by AI and may make mistakes. Always verify output.
let stderr = ''
child.stderr?.on('data', function (b) {
stderr = stderr + b.toString()
})
let stdout = ''
child.stdout?.on('data', function (b) {
stdout = stdout + b.toString()
})
child.once('close', function () {
expect(stderr.includes('failed')).toStrictEqual(false)
expect(stdout.includes('success')).toStrictEqual(true)
resolve('')
})
})
},
30000
);
});
)
})
2 changes: 1 addition & 1 deletion test/typescript/basic/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ const app: FastifyPluginCallback = function (fastify, opts, next): void {
next()
}

export default app;
export default app
12 changes: 6 additions & 6 deletions test/vitest/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import Fastify from 'fastify'
import AutoLoad from '../../index'
import { join } from 'path'

describe.concurrent("Vitest test suite", function () {
describe.concurrent('Vitest test suite', function () {
const app = Fastify()
app.register(AutoLoad, {
dir: join(__dirname, '../commonjs/ts-node/routes')
})
app.register(AutoLoad, {
dir: join(__dirname, '../commonjs/ts-node/routes')
})

test("Test the root route", async function () {
test('Test the root route', async function () {
const response = await app.inject({
method: 'GET',
url: '/'
Expand All @@ -20,7 +20,7 @@ describe.concurrent("Vitest test suite", function () {
expect(JSON.parse(response.payload)).toEqual({ hello: 'world' })
})

test("Test /foo route", async function () {
test('Test /foo route', async function () {
const response = await app.inject({
method: 'GET',
url: '/foo'
Expand Down
Loading