1
1
import http from 'http' ;
2
2
import zlib from 'zlib' ;
3
- import through , { ThroughStream } from 'through' ;
3
+ import through from 'through' ;
4
4
import util from 'util' ;
5
5
import os from 'os' ;
6
6
import { spawn } from 'child_process' ;
@@ -49,8 +49,6 @@ export class Service extends HttpDuplex {
49
49
super ( req , res ) ;
50
50
51
51
let data = '' ;
52
- // eslint-disable-next-line @typescript-eslint/no-this-alias
53
- const self = this ;
54
52
55
53
this . status = 'pending' ;
56
54
this . repo = opts . repo ;
@@ -83,93 +81,95 @@ export class Service extends HttpDuplex {
83
81
}
84
82
}
85
83
86
- ts . once ( 'data' , function onData ( chunk : string ) {
84
+ ts . once ( 'data' , ( chunk : string ) => {
87
85
data += chunk ;
88
86
89
- const ops = data . match ( new RegExp ( headerRegex [ self . service ] , 'gi' ) ) ;
87
+ const ops = data . match ( new RegExp ( headerRegex [ this . service ] , 'gi' ) ) ;
90
88
if ( ! ops ) return ;
91
89
data = '' ;
92
90
93
- ops . forEach ( function ( op ) {
91
+ ops . forEach ( ( op ) => {
94
92
let type ;
95
- const m = op . match ( new RegExp ( headerRegex [ self . service ] ) ) ;
93
+ const m = op . match ( new RegExp ( headerRegex [ this . service ] ) ) ;
96
94
97
95
if ( ! m ) return ;
98
96
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 ] ;
102
100
103
101
if ( m [ 3 ] == 'heads' ) {
104
102
type = 'branch' ;
105
- self . evName = 'push' ;
103
+ this . evName = 'push' ;
106
104
} else {
107
105
type = 'version' ;
108
- self . evName = 'tag' ;
106
+ this . evName = 'tag' ;
109
107
}
110
108
111
109
const headers : { [ key : string ] : string } = {
112
- last : self . last ,
113
- commit : self . commit ,
110
+ last : this . last ,
111
+ commit : this . commit ,
114
112
} ;
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 ,
122
120
} ) ;
123
121
}
124
122
} ) ;
125
123
} ) ;
126
124
127
- self . once ( 'accept' , function onAccept ( ) {
128
- process . nextTick ( function ( ) {
125
+ this . once ( 'accept' , ( ) => {
126
+ process . nextTick ( ( ) => {
129
127
const cmd =
130
128
os . platform ( ) == 'win32'
131
129
? [ 'git' , opts . service , '--stateless-rpc' , opts . cwd ]
132
130
: [ 'git-' + opts . service , '--stateless-rpc' , opts . cwd ] ;
133
131
134
132
const ps = spawn ( cmd [ 0 ] , cmd . slice ( 1 ) ) ;
135
133
136
- ps . on ( 'error' , function ( error : Error ) {
137
- self . emit (
134
+ ps . on ( 'error' , ( error : Error ) => {
135
+ this . emit (
138
136
'error' ,
139
137
new Error ( `${ error . message } running command ${ cmd . join ( ' ' ) } ` )
140
138
) ;
141
139
} ) ;
142
140
143
- self . emit ( 'service' , ps ) ;
141
+ this . emit ( 'service' , ps ) ;
144
142
145
143
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 ( ) ) ;
151
150
}
152
151
}
153
152
154
- return this . queue ( c ) ;
153
+ return respStream . queue ( c ) ;
155
154
}
156
155
// prevent git from sending the close signal
157
156
if ( c . length === 4 && c . toString ( ) === '0000' ) return ;
158
- this . queue ( c ) ;
157
+ respStream . queue ( c ) ;
159
158
} ,
160
- function end ( this : ThroughStream ) {
161
- if ( self . listeners ( 'response' ) . length > 0 ) return ;
159
+ // read
160
+ ( ) => {
161
+ if ( this . listeners ( 'response' ) . length > 0 ) return ;
162
162
163
- this . queue ( null ) ;
163
+ respStream . queue ( null ) ;
164
164
}
165
165
) ;
166
166
167
- ( respStream as any ) . log = function ( ) {
167
+ ( respStream as any ) . log = ( ) => {
168
168
// eslint-disable-next-line prefer-rest-params
169
- ( self as any ) . log ( ...arguments ) ;
169
+ ( this as any ) . log ( ...arguments ) ;
170
170
} ;
171
171
172
- self . emit ( 'response' , respStream , function endResponse ( ) {
172
+ this . emit ( 'response' , respStream , function endResponse ( ) {
173
173
( res as any ) . queue ( Buffer . from ( '0000' ) ) ;
174
174
( res as any ) . queue ( null ) ;
175
175
} ) ;
@@ -180,20 +180,20 @@ export class Service extends HttpDuplex {
180
180
buffered . resume ( ) ;
181
181
182
182
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 ( ) ) ;
186
186
}
187
187
respStream . queue ( Buffer . from ( '0000' ) ) ;
188
188
respStream . queue ( null ) ;
189
189
}
190
190
191
- self . emit . bind ( self , 'exit' ) ;
191
+ this . emit ( 'exit' ) ;
192
192
} ) ;
193
193
} ) ;
194
194
} ) ;
195
195
196
- self . once ( 'reject' , function onReject ( code : number , msg : string ) {
196
+ this . once ( 'reject' , function onReject ( code : number , msg : string ) {
197
197
res . statusCode = code ;
198
198
res . end ( msg ) ;
199
199
} ) ;
0 commit comments