fastify-git-server
A Fastify plugin to easily make one/many Git repositories available for clone/fetch/push.
Installation
$ yarn add @ethicdevs/fastify-git-server
# or
$ npm i @ethicdevs/fastify-git-server
Usage
import { resolve } from "node:path";
import fastify from "fastify";
import fastifyGitServer, { GitServer } from "@ethicdevs/fastify-git-server";
const HOST = process.env.HOST || "localhost";
const PORT = process.env.PORT != null ? parseInt(process.env.PORT, 10) : 4200;
async function main() {
const server = fastify();
server.register(fastifyGitServer, {
gitExecutablePath: undefined,
withSideBandMessages: true,
async authorizationResolver(repoSlug, credentials) {
if (repoSlug.toLowerCase() === "testorg/test-repo") {
return (
credentials.username === "test" && credentials.password === "test"
);
}
return false;
},
async repositoryResolver(repoSlug) {
if (repoSlug !== "testorg/test-repo") {
throw new Error("Cannot find such repository.");
}
return {
authMode: GitServer.AuthMode.ALWAYS,
gitRepositoryDir: resolve(
"/home/git-server-user/repos/testorg/test-repo",
),
};
},
onPush: (push) => {
push.write("\n");
push.write("Hey from the server 🖖\n\n");
},
});
server.listen(PORT, HOST, (err, listeningOnUrl) => {
if (err != null) {
console.error(`❌ Could not start server. Error: ${err.message}`);
} else {
console.log(`🚀 Server is up and running at: ${listeningOnUrl}`);
}
});
return server;
}
main();
Run the server like so (or build/bundle it first), and enjoy!
$ ts-node server.ts
Now you can easily git clone/fetch/push to the repository assuming you pass the
right credentials set for the right repository.
$ git clone http://localhost:4200/testorg/test-repo.git
$ cd test-repo/
$ git fetch
$ git pull --rebase
$ echo "Today is: $(date)" >> ReadMe.md
$ git commit -am 'docs(readme): add the date of today'
$ git push
Contributing
Contributions to this repository are welcome to everyone, please feel free to
send a pull request for further review/discussion/merging/resolution. 👌
Run the tests
$ yarn test
$ yarn test --coverage
$ yarn test --coverage --watchAll
Build the lib
$ yarn build
$ yarn typecheck
Credits
This library is a port of git-express,
aimed at Fastify v3.x+. Original lib' has not received any update for more than
2 years now and has been released without any license...
It was a great source of inspiration for this library to be born, so thanks to
the author for that piece of code! 😅
License
The MIT license.