@@ -25,12 +25,9 @@ interface GitServerOptions extends ServerOptions {
25
25
type : 'http' | 'https' ;
26
26
}
27
27
28
- export interface GitOptions {
28
+ export interface GitOptions < T = undefined > {
29
29
autoCreate ?: boolean ;
30
- authenticate ?: (
31
- options : GitAuthenticateOptions ,
32
- callback : ( error ?: Error ) => void | undefined
33
- ) => void | Promise < Error | undefined | void > | undefined ;
30
+ authenticate ?: ( options : GitAuthenticateOptions ) => Promise < T > | T ;
34
31
checkout ?: boolean ;
35
32
}
36
33
@@ -50,44 +47,49 @@ export interface GitAuthenticateOptions {
50
47
/**
51
48
* An http duplex object (see below) with these extra properties:
52
49
*/
53
- export interface TagData extends HttpDuplex {
50
+ export interface TagData < T = any > extends HttpDuplex {
54
51
repo : string ; // The string that defines the repo
55
52
commit : string ; // The string that defines the commit sha
56
53
version : string ; // The string that defines the tag being pushed
54
+ context ?: T ;
57
55
}
58
56
59
57
/**
60
58
* Is a http duplex object (see below) with these extra properties
61
59
*/
62
- export interface PushData extends HttpDuplex {
60
+ export interface PushData < T = any > extends HttpDuplex {
63
61
repo : string ; // The string that defines the repo
64
62
commit : string ; // The string that defines the commit sha
65
63
branch : string ; // The string that defines the branch
64
+ context ?: T ;
66
65
}
67
66
68
67
/**
69
68
* an http duplex object (see below) with these extra properties
70
69
*/
71
- export interface FetchData extends HttpDuplex {
70
+ export interface FetchData < T = any > extends HttpDuplex {
72
71
repo : string ; // The string that defines the repo
73
72
commit : string ; // The string that defines the commit sha
73
+ context ?: T ;
74
74
}
75
75
76
76
/**
77
77
* an http duplex object (see below) with these extra properties
78
78
*/
79
- export interface InfoData extends HttpDuplex {
79
+ export interface InfoData < T = any > extends HttpDuplex {
80
80
repo : string ; // The string that defines the repo
81
+ context ?: T ;
81
82
}
82
83
83
84
/**
84
85
* an http duplex object (see below) with these extra properties
85
86
*/
86
- export interface HeadData extends HttpDuplex {
87
+ export interface HeadData < T = any > extends HttpDuplex {
87
88
repo : string ; // The string that defines the repo
89
+ context ?: T ;
88
90
}
89
91
90
- export interface GitEvents {
92
+ export interface GitEvents < T = any > {
91
93
/**
92
94
* @example
93
95
* repos.on('push', function (push) { ... }
@@ -97,7 +99,7 @@ export interface GitEvents {
97
99
* Exactly one listener must call `push.accept()` or `push.reject()`. If there are
98
100
* no listeners, `push.accept()` is called automatically.
99
101
**/
100
- on ( event : 'push' , listener : ( push : PushData ) => void ) : this;
102
+ on ( event : 'push' , listener : ( push : PushData < T > ) => void ) : this;
101
103
102
104
/**
103
105
* @example
@@ -107,7 +109,7 @@ export interface GitEvents {
107
109
* Exactly one listener must call `tag.accept()` or `tag.reject()`. If there are
108
110
* No listeners, `tag.accept()` is called automatically.
109
111
**/
110
- on ( event : 'tag' , listener : ( tag : TagData ) => void ) : this;
112
+ on ( event : 'tag' , listener : ( tag : TagData < T > ) => void ) : this;
111
113
112
114
/**
113
115
* @example
@@ -119,7 +121,7 @@ export interface GitEvents {
119
121
* Exactly one listener must call `fetch.accept()` or `fetch.reject()`. If there are
120
122
* no listeners, `fetch.accept()` is called automatically.
121
123
**/
122
- on ( event : 'fetch' , listener : ( fetch : FetchData ) => void ) : this;
124
+ on ( event : 'fetch' , listener : ( fetch : FetchData < T > ) => void ) : this;
123
125
124
126
/**
125
127
* @example
@@ -130,7 +132,7 @@ export interface GitEvents {
130
132
* Exactly one listener must call `info.accept()` or `info.reject()`. If there are
131
133
* no listeners, `info.accept()` is called automatically.
132
134
**/
133
- on ( event : 'info' , listener : ( info : InfoData ) => void ) : this;
135
+ on ( event : 'info' , listener : ( info : InfoData < T > ) => void ) : this;
134
136
135
137
/**
136
138
* @example
@@ -142,17 +144,14 @@ export interface GitEvents {
142
144
* no listeners, `head.accept()` is called automatically.
143
145
*
144
146
**/
145
- on ( event : 'head' , listener : ( head : HeadData ) => void ) : this;
147
+ on ( event : 'head' , listener : ( head : HeadData < T > ) => void ) : this;
146
148
}
147
- export class Git extends EventEmitter implements GitEvents {
149
+ export class Git < T = any > extends EventEmitter implements GitEvents {
148
150
dirMap : ( dir ?: string ) => string ;
149
151
150
152
authenticate :
151
- | ( (
152
- options : GitAuthenticateOptions ,
153
- callback : ( error ?: Error ) => void | undefined
154
- ) => void | Promise < Error | undefined | void > | undefined )
155
- | undefined ;
153
+ | ( ( options : GitAuthenticateOptions ) => Promise < T > | T )
154
+ | undefined = undefined ;
156
155
157
156
autoCreate : boolean ;
158
157
checkout : boolean | undefined ;
@@ -185,7 +184,7 @@ export class Git extends EventEmitter implements GitEvents {
185
184
*/
186
185
constructor (
187
186
repoDir : string | ( ( dir ?: string ) => string ) ,
188
- options : GitOptions = { }
187
+ options : GitOptions < T > = { }
189
188
) {
190
189
super ( ) ;
191
190
@@ -199,9 +198,7 @@ export class Git extends EventEmitter implements GitEvents {
199
198
} ;
200
199
}
201
200
202
- if ( options . authenticate ) {
203
- this . authenticate = options . authenticate ;
204
- }
201
+ this . authenticate = options . authenticate ;
205
202
206
203
this . autoCreate = options . autoCreate === false ? false : true ;
207
204
this . checkout = options . checkout ;
@@ -253,13 +250,13 @@ export class Git extends EventEmitter implements GitEvents {
253
250
* @param callback - Optionally get a callback `cb(err)` to be notified when the repository was created.
254
251
*/
255
252
create ( repo : string , callback : ( error ?: Error ) => void ) {
256
- function next ( self : Git ) {
253
+ const next = ( ) => {
257
254
let ps ;
258
255
let _error = '' ;
259
256
260
- const dir = self . dirMap ( repo ) ;
257
+ const dir = this . dirMap ( repo ) ;
261
258
262
- if ( self . checkout ) {
259
+ if ( this . checkout ) {
263
260
ps = spawn ( 'git' , [ 'init' , dir ] ) ;
264
261
} else {
265
262
ps = spawn ( 'git' , [ 'init' , '--bare' , dir ] ) ;
@@ -278,7 +275,7 @@ export class Git extends EventEmitter implements GitEvents {
278
275
callback ( ) ;
279
276
}
280
277
} ) ;
281
- }
278
+ } ;
282
279
283
280
if ( typeof callback !== 'function' )
284
281
callback = ( ) => {
@@ -293,7 +290,7 @@ export class Git extends EventEmitter implements GitEvents {
293
290
this . mkdir ( repo ) ;
294
291
}
295
292
296
- next ( this ) ;
293
+ next ( ) ;
297
294
}
298
295
/**
299
296
* returns the typeof service being process. This will respond with either fetch or push.
@@ -319,6 +316,8 @@ export class Git extends EventEmitter implements GitEvents {
319
316
// eslint-disable-next-line @typescript-eslint/no-this-alias
320
317
const self = this ;
321
318
319
+ let context : T | undefined = undefined ;
320
+
322
321
const handlers = [
323
322
( req : http . IncomingMessage , res : http . ServerResponse ) => {
324
323
if ( req . method !== 'GET' ) return false ;
@@ -367,27 +366,40 @@ export class Git extends EventEmitter implements GitEvents {
367
366
const headers = req . headers ;
368
367
const user = (
369
368
callback ?: ( username ?: string , password ?: string ) => void
370
- ) =>
371
- callback
372
- ? basicAuth ( req , res , callback )
373
- : new Promise < [ string | undefined , string | undefined ] > (
374
- ( resolve ) => basicAuth ( req , res , ( u , p ) => resolve ( [ u , p ] ) )
375
- ) ;
376
-
377
- const promise = this . authenticate (
378
- {
379
- type,
380
- repo : repoName ,
381
- user : user as unknown as GitAuthenticateOptions [ 'user' ] ,
382
- headers,
383
- } ,
384
- ( error ?: Error ) => {
385
- return next ( error ) ;
369
+ ) => {
370
+ const basicAuthResult = basicAuth ( req , res ) ;
371
+ if ( basicAuthResult && callback ) {
372
+ callback ( ...basicAuthResult ) ;
373
+ }
374
+ if ( basicAuthResult ) {
375
+ return new Promise < [ string | undefined , string | undefined ] > (
376
+ ( resolve ) => resolve ( basicAuthResult )
377
+ ) ;
386
378
}
387
- ) ;
379
+ // return new Promise<[string | undefined, string | undefined]>(
380
+ // (_, reject) => reject(new Error("Basic auth failed"))
381
+ // );
382
+ return new Promise < [ string | undefined , string | undefined ] > (
383
+ ( ) => { }
384
+ ) ;
385
+ } ;
386
+
387
+ const promise = this . authenticate ( {
388
+ type,
389
+ repo : repoName ,
390
+ user : user ,
391
+ headers,
392
+ } ) ;
388
393
389
394
if ( promise instanceof Promise ) {
390
- return promise . then ( next ) . catch ( next ) ;
395
+ return promise
396
+ . then ( ( ctx ) => {
397
+ context = ctx ;
398
+ next ( ) ;
399
+ } )
400
+ . catch ( next ) ;
401
+ } else {
402
+ context = promise ;
391
403
}
392
404
} else {
393
405
return next ( ) ;
@@ -467,14 +479,15 @@ export class Git extends EventEmitter implements GitEvents {
467
479
) ;
468
480
noCache ( res ) ;
469
481
470
- const action = createAction (
482
+ const action = createAction < T > (
471
483
{
472
484
repo : repo ,
473
485
service : service as ServiceString ,
474
486
cwd : self . dirMap ( repo ) ,
475
487
} ,
476
488
req ,
477
- res
489
+ res ,
490
+ context
478
491
) ;
479
492
480
493
action . on ( 'header' , ( ) => {
@@ -515,7 +528,11 @@ export class Git extends EventEmitter implements GitEvents {
515
528
* @param options.cert - the cert file for the https server
516
529
* @param callback - the function to call when server is started or error has occurred
517
530
*/
518
- listen ( port : number , options ?: GitServerOptions , callback ?: ( ) => void ) : Git {
531
+ listen (
532
+ port : number ,
533
+ options ?: GitServerOptions ,
534
+ callback ?: ( ) => void
535
+ ) : this {
519
536
if ( ! options ) {
520
537
options = { type : 'http' } ;
521
538
}
0 commit comments