{"id":17661,"library":"git-http-backend","title":"Git HTTP Backend","description":"`git-http-backend` is a Node.js library designed to facilitate serving Git repositories over HTTP, providing a programmatic interface to the underlying Git protocol. It enables developers to construct custom HTTP Git servers, allowing them to either integrate their own `git-receive-pack` and `git-upload-pack` implementations or to delegate these operations to the system's installed Git executables. The package abstracts the complexities of the Git HTTP protocol, primarily by streaming request and response data. As of the provided metadata, the current stable version is 1.1.2. Its release cadence appears to be infrequent, reflecting its role as a focused utility. A key differentiator is its minimalist design, which offers core protocol parsing and streaming capabilities without imposing a complete server framework, thus allowing for flexible integration into existing Node.js HTTP server applications.","status":"maintenance","version":"1.1.2","language":"javascript","source_language":"en","source_url":"git://github.com/substack/git-http-backend","tags":["javascript","git","http","backend","web","server","git-receive-pack","git-update-pack"],"install":[{"cmd":"npm install git-http-backend","lang":"bash","label":"npm"},{"cmd":"yarn add git-http-backend","lang":"bash","label":"yarn"},{"cmd":"pnpm add git-http-backend","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only. Direct ESM 'import' syntax will not work without a CommonJS bridge or bundler configuration. The primary export is a function that returns a duplex stream.","wrong":"import backend from 'git-http-backend'","symbol":"backend","correct":"const backend = require('git-http-backend')"}],"quickstart":{"code":"var http = require('http');\nvar spawn = require('child_process').spawn;\nvar path = require('path');\nvar backend = require('git-http-backend');\nvar zlib = require('zlib');\n\nvar server = http.createServer(function (req, res) {\n    var repo = req.url.split('/')[1];\n    var dir = path.join(__dirname, 'repos', repo);\n    // Handle gzipped content for incoming requests\n    var reqStream = req.headers['content-encoding'] == 'gzip' ? req.pipe(zlib.createGunzip()) : req;\n    \n    reqStream.pipe(backend(req.url, function (err, service) {\n        if (err) return res.end(err + '\\n');\n        \n        res.setHeader('content-type', service.type);\n        console.log(service.action, repo, service.fields);\n        \n        // Spawn system git command based on service detected\n        var ps = spawn(service.cmd, service.args.concat(dir));\n        // Pipe the service stream (request data) into git's stdin\n        // Pipe git's stdout into the service's response stream\n        ps.stdout.pipe(service.createStream()).pipe(ps.stdin);\n        \n    })).pipe(res);\n});\nserver.listen(5000);\n\n// To set up and use:\n// 1. Create a directory for repositories: `mkdir repos`\n// 2. Initialize a bare Git repository: `git init repos/myrepo.git --bare -q`\n// 3. Run the server: `node server.js &`\n// 4. Push to it: `cd my/local/project && git push http://localhost:5000/myrepo.git master`\n// 5. Clone from it: `git clone http://localhost:5000/myrepo.git`\n","lang":"javascript","description":"This example demonstrates how to set up a basic HTTP server using `git-http-backend` to serve Git repositories, including handling gzipped requests and spawning system Git commands for push and pull operations."},"warnings":[{"fix":"Ensure robust input validation and sanitization for all request parameters, especially if `git` command arguments are derived from user input. Consider implementing a whitelist for allowed commands and arguments.","message":"This package relies on shelling out to system `git-{receive,upload}-pack` executables. Improper input validation or misconfiguration could lead to command injection vulnerabilities if `service.cmd` or `service.args` are manipulated by malicious clients.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Implement authentication and authorization middleware in your HTTP server *before* piping requests to `git-http-backend` to secure access to repositories based on user permissions.","message":"The library itself does not provide any authentication or authorization mechanisms. Deploying a `git-http-backend` server without implementing custom security layers (e.g., HTTP Basic Auth, token-based auth) will expose all hosted repositories publicly.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For ESM projects, use dynamic `import('git-http-backend')` or ensure your build setup correctly handles CommonJS modules. The simplest fix for hybrid projects is to stick to `require()` or use a transpiler.","message":"The package is designed for CommonJS (`require`). Attempting to use `import` statements directly in a pure ESM Node.js project will result in a `TypeError: require is not a function` or similar module resolution errors.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Extend the request stream handling to detect and decompress other `Content-Encoding` types using appropriate Node.js `zlib` methods (e.g., `createInflateRaw()` for deflate, `createBrotliDecompress()` for brotli).","message":"The provided example only explicitly handles `gzip` for incoming `Content-Encoding`. Other compression types (e.g., `deflate`, `br`) will not be automatically decompressed, potentially leading to issues with clients using those encodings.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Run `npm install git-http-backend` or `yarn add git-http-backend` in your project directory.","cause":"The 'git-http-backend' package has not been installed in your project's `node_modules`.","error":"Error: Cannot find module 'git-http-backend'"},{"fix":"Verify that the bare Git repository (`myrepo.git`) exists at the specified `dir` path on the server. Ensure the `req.url` parsing correctly extracts the repository name and maps it to a valid physical location.","cause":"The requested Git repository path either does not exist on the server's filesystem, or the server's `repos` directory mapping is incorrect.","error":"fatal: repository 'http://localhost:5000/myrepo.git/' not found"},{"fix":"Check server logs for more detailed `git-receive-pack` errors. Verify that the user running the Node.js server has write permissions to the repository directory and that there is sufficient disk space.","cause":"This error typically indicates an issue during the `git-receive-pack` process on the server, often due to corrupted data, disk space issues, or incorrect permissions on the repository directory.","error":"git remote: error: unpack failed: unpack-objects abnormal exit"},{"fix":"Ensure `req.url` is correctly structured for Git HTTP requests (e.g., `/repo.git/info/refs?service=git-upload-pack`) and that the incoming request body (if any) is valid. Debug the `backend(req.url, cb)` call to inspect the `err` object.","cause":"The `service` object's `type` property is crucial for setting the HTTP `Content-Type` header, and if it's `undefined`, it suggests the `git-http-backend` failed to parse the Git service request correctly.","error":"http.createServer(function (req, res) { ... }): service.type is undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}