Registry / http-networking / git-http-backend

git-http-backend

JSON →
library1.1.2jsnpmunverified

`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.

npm install git-http-backend
INSTALL
IMPORT
SIG · GIT-HTTP-BACKEND
G
git-http-backend
http-networkingjavascriptv1.1.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

backend
const backend = require('git-http-backend')
import backend from 'git-http-backend'
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.

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.

var http = require('http'); var spawn = require('child_process').spawn; var path = require('path'); var backend = require('git-http-backend'); var zlib = require('zlib'); var server = http.createServer(function (req, res) { var repo = req.url.split('/')[1]; var dir = path.join(__dirname, 'repos', repo); // Handle gzipped content for incoming requests var reqStream = req.headers['content-encoding'] == 'gzip' ? req.pipe(zlib.createGunzip()) : req; reqStream.pipe(backend(req.url, function (err, service) { if (err) return res.end(err + '\n'); res.setHeader('content-type', service.type); console.log(service.action, repo, service.fields); // Spawn system git command based on service detected var ps = spawn(service.cmd, service.args.concat(dir)); // Pipe the service stream (request data) into git's stdin // Pipe git's stdout into the service's response stream ps.stdout.pipe(service.createStream()).pipe(ps.stdin); })).pipe(res); }); server.listen(5000); // To set up and use: // 1. Create a directory for repositories: `mkdir repos` // 2. Initialize a bare Git repository: `git init repos/myrepo.git --bare -q` // 3. Run the server: `node server.js &` // 4. Push to it: `cd my/local/project && git push http://localhost:5000/myrepo.git master` // 5. Clone from it: `git clone http://localhost:5000/myrepo.git`
Debug
Known issues
gotchaThis 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.
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.
affects: >=1.0.0
gotchaThe 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.
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.
affects: >=1.0.0
gotchaThe 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.
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.
affects: >=1.0.0
gotchaThe 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.
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).
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'git-http-backend'
The 'git-http-backend' package has not been installed in your project's `node_modules`.
fix
Run `npm install git-http-backend` or `yarn add git-http-backend` in your project directory.
fatal: repository 'http://localhost:5000/myrepo.git/' not found
The requested Git repository path either does not exist on the server's filesystem, or the server's `repos` directory mapping is incorrect.
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.
git remote: error: unpack failed: unpack-objects abnormal exit
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.
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.
http.createServer(function (req, res) { ... }): service.type is undefined
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.
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.
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
git-http-backend — npm install git-http-backend · libregistry