{"library":"node-git-server","title":"Node.js Git Server","description":"node-git-server is a highly configurable Git server implemented in Node.js, designed for embedding or standalone deployment within a Node.js environment. The project adheres to a 'zero dependency footprint' philosophy, aiming to keep its core lean and efficient. Its current stable version is 1.0.0, released recently after an extensive beta phase. Key differentiators include its programmatic control over Git operations, extensive configurability for authentication and authorization, and its origin as a hard fork of the well-established `pushover` library. The library migrated to TypeScript starting with version 1.0.0-beta.1, enhancing type safety and developer experience. It now requires Node.js version 16 or newer, aligning with modern Node.js LTS releases. The project shows an active development and release cadence, having frequently updated through its beta cycle to reach a stable 1.0.0 release.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-git-server"],"cli":null},"imports":["import { GitServer } from 'node-git-server';","import type { GitServerOptions } from 'node-git-server';","import type { PushEvent } from 'node-git-server';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { GitServer } from 'node-git-server';\nimport path from 'path';\nimport fs from 'fs';\n\nconst REPOS_DIR = path.resolve(process.env.GIT_REPOS_DIR ?? './tmp/repos');\n\n// Ensure the repository directory exists\nif (!fs.existsSync(REPOS_DIR)) {\n  fs.mkdirSync(REPOS_DIR, { recursive: true });\n}\n\nconst git = new GitServer(REPOS_DIR, {\n  autoCreate: true, // Automatically create repositories on first push\n  authenticate: (type, repository, username, password, callback) => {\n    console.log(`Authentication attempt: ${type} for ${repository} by ${username}`);\n    // In a real application, validate username and password against a database\n    if (username === 'gituser' && password === 'gitpass') {\n      console.log('Authentication successful.');\n      return callback(null, true); // Authentication successful\n    }\n    console.log('Authentication failed.');\n    return callback(new Error('Authentication failed'), false); // Authentication failed\n  },\n  authorize: (type, repository, username, callback) => {\n    console.log(`Authorization attempt: ${type} for ${repository} by ${username}`);\n    // In a real application, check permissions based on user and repository\n    if (username === 'gituser') {\n      console.log('Authorization successful.');\n      return callback(null, true); // Authorization successful\n    }\n    console.log('Authorization failed.');\n    return callback(new Error('Authorization failed'), false); // Authorization failed\n  },\n});\n\ngit.on('push', (push) => {\n  console.log(`Push event: ${push.repo}/${push.commit} by ${push.branch}`);\n  push.accept(); // Accept the push\n});\n\ngit.on('fetch', (fetch) => {\n  console.log(`Fetch event: ${fetch.repo} by ${fetch.branch}`);\n  fetch.accept(); // Accept the fetch\n});\n\nconst PORT = parseInt(process.env.GIT_SERVER_PORT ?? '7000', 10);\n\ngit.listen(PORT, () => {\n  console.log(`node-git-server running on port ${PORT}`);\n  console.log(`Serving repositories from: ${REPOS_DIR}`);\n  console.log(`Try cloning: git clone http://localhost:${PORT}/my-repo.git`);\n});\n\n// Handle process exit to close server gracefully\nprocess.on('SIGINT', () => {\n  console.log('Shutting down git server...');\n  git.close(() => {\n    console.log('Git server closed.');\n    process.exit(0);\n  });\n});","lang":"typescript","description":"This quickstart code demonstrates how to set up a basic configurable Git server using node-git-server. It initializes a Git server instance, defines a directory for repositories, implements a simple username/password authentication mechanism, and handles push/fetch events. The server listens on a specified port, allowing users to clone and push to repositories managed by this instance.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}