Skip to content

Commit 73ec48e

Browse files
authored
Avoid using self in service.ts - to avoid issue with through (#95)
1 parent a2ead41 commit 73ec48e

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

src/service.ts

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import http from 'http';
22
import zlib from 'zlib';
3-
import through, { ThroughStream } from 'through';
3+
import through from 'through';
44
import util from 'util';
55
import os from 'os';
66
import { spawn } from 'child_process';
@@ -49,8 +49,6 @@ export class Service extends HttpDuplex {
4949
super(req, res);
5050

5151
let data = '';
52-
// eslint-disable-next-line @typescript-eslint/no-this-alias
53-
const self = this;
5452

5553
this.status = 'pending';
5654
this.repo = opts.repo;
@@ -83,93 +81,95 @@ export class Service extends HttpDuplex {
8381
}
8482
}
8583

86-
ts.once('data', function onData(chunk: string) {
84+
ts.once('data', (chunk: string) => {
8785
data += chunk;
8886

89-
const ops = data.match(new RegExp(headerRegex[self.service], 'gi'));
87+
const ops = data.match(new RegExp(headerRegex[this.service], 'gi'));
9088
if (!ops) return;
9189
data = '';
9290

93-
ops.forEach(function (op) {
91+
ops.forEach((op) => {
9492
let type;
95-
const m = op.match(new RegExp(headerRegex[self.service]));
93+
const m = op.match(new RegExp(headerRegex[this.service]));
9694

9795
if (!m) return;
9896

99-
if (self.service === 'receive-pack') {
100-
self.last = m[1];
101-
self.commit = m[2];
97+
if (this.service === 'receive-pack') {
98+
this.last = m[1];
99+
this.commit = m[2];
102100

103101
if (m[3] == 'heads') {
104102
type = 'branch';
105-
self.evName = 'push';
103+
this.evName = 'push';
106104
} else {
107105
type = 'version';
108-
self.evName = 'tag';
106+
this.evName = 'tag';
109107
}
110108

111109
const headers: { [key: string]: string } = {
112-
last: self.last,
113-
commit: self.commit,
110+
last: this.last,
111+
commit: this.commit,
114112
};
115-
headers[type] = (self as any)[type] = m[4];
116-
self.emit('header', headers);
117-
} else if (self.service === 'upload-pack') {
118-
self.commit = m[1];
119-
self.evName = 'fetch';
120-
self.emit('header', {
121-
commit: self.commit,
113+
headers[type] = (this as any)[type] = m[4];
114+
this.emit('header', headers);
115+
} else if (this.service === 'upload-pack') {
116+
this.commit = m[1];
117+
this.evName = 'fetch';
118+
this.emit('header', {
119+
commit: this.commit,
122120
});
123121
}
124122
});
125123
});
126124

127-
self.once('accept', function onAccept() {
128-
process.nextTick(function () {
125+
this.once('accept', () => {
126+
process.nextTick(() => {
129127
const cmd =
130128
os.platform() == 'win32'
131129
? ['git', opts.service, '--stateless-rpc', opts.cwd]
132130
: ['git-' + opts.service, '--stateless-rpc', opts.cwd];
133131

134132
const ps = spawn(cmd[0], cmd.slice(1));
135133

136-
ps.on('error', function (error: Error) {
137-
self.emit(
134+
ps.on('error', (error: Error) => {
135+
this.emit(
138136
'error',
139137
new Error(`${error.message} running command ${cmd.join(' ')}`)
140138
);
141139
});
142140

143-
self.emit('service', ps);
141+
this.emit('service', ps);
144142

145143
const respStream = through(
146-
function write(this: ThroughStream, c: any) {
147-
if (self.listeners('response').length === 0) {
148-
if (self.logs.length > 0) {
149-
while (self.logs.length > 0) {
150-
this.queue(self.logs.pop());
144+
// write
145+
(c: any) => {
146+
if (this.listeners('response').length === 0) {
147+
if (this.logs.length > 0) {
148+
while (this.logs.length > 0) {
149+
respStream.queue(this.logs.pop());
151150
}
152151
}
153152

154-
return this.queue(c);
153+
return respStream.queue(c);
155154
}
156155
// prevent git from sending the close signal
157156
if (c.length === 4 && c.toString() === '0000') return;
158-
this.queue(c);
157+
respStream.queue(c);
159158
},
160-
function end(this: ThroughStream) {
161-
if (self.listeners('response').length > 0) return;
159+
// read
160+
() => {
161+
if (this.listeners('response').length > 0) return;
162162

163-
this.queue(null);
163+
respStream.queue(null);
164164
}
165165
);
166166

167-
(respStream as any).log = function () {
167+
(respStream as any).log = () => {
168168
// eslint-disable-next-line prefer-rest-params
169-
(self as any).log(...arguments);
169+
(this as any).log(...arguments);
170170
};
171171

172-
self.emit('response', respStream, function endResponse() {
172+
this.emit('response', respStream, function endResponse() {
173173
(res as any).queue(Buffer.from('0000'));
174174
(res as any).queue(null);
175175
});
@@ -180,20 +180,20 @@ export class Service extends HttpDuplex {
180180
buffered.resume();
181181

182182
ps.on('exit', () => {
183-
if (self.logs.length > 0) {
184-
while (self.logs.length > 0) {
185-
respStream.queue(self.logs.pop());
183+
if (this.logs.length > 0) {
184+
while (this.logs.length > 0) {
185+
respStream.queue(this.logs.pop());
186186
}
187187
respStream.queue(Buffer.from('0000'));
188188
respStream.queue(null);
189189
}
190190

191-
self.emit.bind(self, 'exit');
191+
this.emit('exit');
192192
});
193193
});
194194
});
195195

196-
self.once('reject', function onReject(code: number, msg: string) {
196+
this.once('reject', function onReject(code: number, msg: string) {
197197
res.statusCode = code;
198198
res.end(msg);
199199
});

0 commit comments

Comments
 (0)