Skip to content

Commit f247fd8

Browse files
echopointgabrielcsapo
authored andcommitted
Removes dependency on http-duplex package replacing w/ internal replacement lib (#24)
* Removes dependency on http-duplex package by replacing with internal replacement lib * forgot to remove the dep from package.json * add contributors to package.json * enclose field keys in quotes..
1 parent e3978c5 commit f247fd8

File tree

5 files changed

+139
-6
lines changed

5 files changed

+139
-6
lines changed

lib/git.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const path = require('path');
33
const http = require('http');
44
const url = require('url');
55
const qs = require('querystring');
6-
const httpDuplex = require('http-duplex');
6+
const httpDuplex = require('./http-duplex');
77

88
const { spawn } = require('child_process');
99
const { EventEmitter } = require('events');
@@ -324,7 +324,7 @@ class Git extends EventEmitter {
324324

325325
self.exists(repo, (ex) => {
326326
const anyListeners = self.listeners('head').length > 0;
327-
const dup = httpDuplex(req, res);
327+
const dup = new httpDuplex(req, res);
328328
dup.exists = ex;
329329
dup.repo = repo;
330330
dup.cwd = self.dirMap(repo);

lib/http-duplex.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
const EventEmitter = require('events');
2+
3+
/** Updated/Simplified http-duplex replacement.
4+
* Creates a unified stream from the passed in streams input and output.
5+
* Generally meant to combine http.req (input) and http.res (output) streams
6+
* @class HttpDuplex
7+
* @extends EventEmitter
8+
*/
9+
10+
class HttpDuplex extends EventEmitter {
11+
/**
12+
* Constructs a proxy object over input and output.
13+
* @constructor
14+
* @param {stream.readable} input - http request object
15+
* @param {stream.writeable} output - http response object
16+
*/
17+
constructor(input, output) {
18+
super();
19+
20+
this.req = input;
21+
this.res = output;
22+
23+
var self = this;
24+
25+
// request / input proxy events
26+
['data', 'end', 'error', 'close'].forEach(function (name) {
27+
self.req.on(name, self.emit.bind(self, name));
28+
});
29+
30+
// respone / output proxy events
31+
['error', 'drain'].forEach(function (name) {
32+
self.res.on(name, self.emit.bind(self, name));
33+
});
34+
}
35+
36+
// input / request wrapping
37+
get client() {
38+
return this.req.client;
39+
}
40+
41+
get complete() {
42+
return this.req.complete;
43+
}
44+
45+
get connection() {
46+
return this.req.connection;
47+
}
48+
49+
get headers() {
50+
return this.req.headers;
51+
}
52+
53+
get httpVersion() {
54+
return this.req.httpVersion;
55+
}
56+
57+
get httpVersionMajor() {
58+
return this.req.httpVersionMajor;
59+
}
60+
61+
get httpVersionMinor() {
62+
return this.req.httpVersionMinor;
63+
}
64+
65+
get method() {
66+
return this.req.method;
67+
}
68+
69+
get readable() {
70+
return this.req.readable;
71+
}
72+
73+
get socket() {
74+
return this.req.socket;
75+
}
76+
77+
get trailers() {
78+
return this.req.trailers;
79+
}
80+
81+
get upgrade() {
82+
return this.req.upgrade;
83+
}
84+
85+
get url() {
86+
return this.req.url;
87+
}
88+
89+
// output / response wrapping
90+
get writable() {
91+
return this.res.writable;
92+
}
93+
94+
get statusCode() {
95+
return this.res.statusCode;
96+
}
97+
98+
set statusCode(val) {
99+
this.res.statusCode = val;
100+
}
101+
102+
}
103+
104+
// proxy request methods
105+
['pause', 'resume', 'setEncoding'].forEach(function (name) {
106+
HttpDuplex.prototype[name] = function () {
107+
return this.req[name].apply(this.req, Array.from(arguments));
108+
};
109+
});
110+
111+
// proxy respone methods
112+
[
113+
'cork', 'uncork', 'setDefaultEncoding', 'write', 'end', 'flush', 'writeHeader', 'writeHead', 'writeContinue',
114+
'setHeader', 'getHeader', 'removeHeader', 'addTrailers'
115+
].forEach(function (name) {
116+
HttpDuplex.prototype[name] = function () {
117+
return this.res[name].apply(this.res, Array.from(arguments));
118+
};
119+
});
120+
121+
/**
122+
* destroys stream object and it's bound streams
123+
* @method destroy
124+
*/
125+
HttpDuplex.prototype.destroy = function () {
126+
this.req.destroy();
127+
this.res.destroy();
128+
};
129+
130+
module.exports = HttpDuplex;

lib/service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const through = require('through');
2-
const HttpDuplex = require('http-duplex');
2+
const HttpDuplex = require('./http-duplex');
33
const zlib = require('zlib');
44

55
const { spawn } = require('child_process');

lib/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @module lib/util
33
*/
44

5-
const httpDuplex = require('http-duplex');
5+
const httpDuplex = require('./http-duplex');
66
const { spawn } = require('child_process');
77

88
const Service = require('./service');
@@ -86,7 +86,7 @@ const Util = {
8686
},
8787
infoResponse: function infoResponse(opts, req, res) {
8888
var self = opts.repos;
89-
var dup = httpDuplex(req, res);
89+
var dup = new httpDuplex(req, res);
9090
dup.cwd = self.dirMap(opts.repo);
9191
dup.repo = opts.repo;
9292

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
"version": "0.3.2",
44
"description": "🎡 A configurable git server written in Node.js",
55
"author": "Gabriel J. Csapo <[email protected]>",
6+
"contributors": [{
7+
"name": "echopoint",
8+
"email": "echopoint <[email protected]>"
9+
}],
610
"license": "MIT",
711
"bugs": {
812
"url": "https://github.com/gabrielcsapo/node-git-server/issues"
@@ -23,7 +27,6 @@
2327
"generate-docs": "tryitout --template=landing --output=./docs && jsdoc -c jsdoc.json"
2428
},
2529
"dependencies": {
26-
"http-duplex": "~0.0.2",
2730
"through": "^2.3.8"
2831
},
2932
"devDependencies": {

0 commit comments

Comments
 (0)