git-service
Serve git over http(s).
Prerequirements
This package requires that git is installed and available either locally or in the path.
Install
From npm:
$ npm install --save git-service
From GitHub:
$ npm install --save https://github.com/revam/node-git-monorepo/releases/download/git-service/v$VERSION/package.tgz
From git.lan (internet-people can ignore this):
$ npm install --save https://git.lan/mist@node/git@git-service/v$VERSION/package.tgz
What is this?
It is meant as an substitude for server-side git hooks implemented in typescript. This allows for more dynamic controll of requests as opposed to a static hook, in my opinion, as it allows an application to controll any and all syncronization between the itself and its clients.
It exports some high-level and some low-level functions, some interfaces and some classes used as part of the logic. It is reccomended to use the high-level functions and interfaces, unless your requirements require some low-level changes than cannot be implemented at a high-level.
It is adviced to see the source-code for a full list of exports, or the usage examples below, as the documentation is not available yet.
Motivation for this library
As I was not familiar with git hooks, and wanted control over what goes in and out of my local git server, I set out to create a library to handle the git- functionality of a http git server.
I am not a fan of events used in a middleware-driven workflow, as it breaks the pattern, so I made this library which, in my eyes, is better suited in a middleware-driven workflow. I also wanted it to be framework independent, so it can adapt to any framework you want to use.
It's a side-project, so expect irregular updates, if you plan to use it in any of your projects. Below you can find some simular projects with different approches to this , and in which helped me greatly when creating this library.
Also a great help was the technical documentation for git, which can be found here, among other places (such at github).
Simular projects
Usage
Bare http server.
;;; // Load variables from environmentconst origin = ;const port = || 3000; // Create serverconst server = ; // Start serverserver;
Some snippets for controller.
// Some simple logginglet int = 0;controlleronUsable;controlleronComplete; // Restrict HTTP methods to known methods for serverconst METHODS = "HEAD" "GET" "POST";// LogicController#use gives us a context for request, with bound methods for// controller.controller; // Reject request for resource "/favicon.ico"controller; // Reject all requests for invalid service types.controller; // Reject all requests for repositories not ending with ".git" or ".git/".const GIT_REGEX = /\.git\/?$/;controller; // Add user to state if users credentials are valid.; // Example model from database.controlleronUsable; // Redirect from mapconst redirects = "test-1.git" "test-2.git" // Example mapping ;controller; // Map public path to internal path or reject with 404.const pathToInternalMap = "root/git.git" "5b/9d/ee4cc4e8af2864e2f34c" // Example mapping;controller;
Http server connected to a database for repository metadata (e.g. web hooks, public/internal paths, etc.) and user authorization. The database/models part is omitted here.
Note: For simplicity's sake do all functions run in sync.
;;; // Example models for database;; // Load variables from environmentconst origin = ;const port = || 3000; const server = ; { // A user can have special access rights if user // Hard-code repository owner access rights. if repositoryownerId === userid return true; // Check for an entry for user const entry = ; if entry if service === ServiceTypeReceivePack return entrycanPush; return entrycanFetchOrView; // Check if service is upload-pack and repository is public. return service === ServiceTypeUploadPack && repositoryisPublic;} // For simplicity's sake are the levels a part of the repository model. { return repositoryaccessLevelEntries;} // Start serverserver;
Manual use of controller. (Only onError
signal is used) It is recommended to use the
LogicController#serve()
method, but still possible to use the controller manually.
;;; // Load variables from environmentconst origin = ;const port = || 3000; // Create controller and serverconst controller = ;const server = ; // Log errors thrown in controllercontrolleronError; // Start serverserver;
Documentation
The documentation is not available as of yet, but if you use TypeScript, the definitions are available with some (short) descriptions. There are also some examples below for how you could use this library.
Typescript
This module includes a TypeScript
declaration file to enable auto complete in compatible editors and type
information for TypeScript projects. This module depends on the Node.js
types, so install @types/node
:
npm install --save-dev @types/node
Changelog and versioning
All notable changes to this project will be documented in CHANGELOG.md.
The format is based on Keep a Changelog and this project adheres to Semantic Versioning.
License
This project is licensed under the MIT license. See LICENSE for the full terms.