1
- import { ChildProcess , spawn , SpawnOptions } from 'child_process' ;
1
+ import { ChildProcess , spawn } from 'child_process' ;
2
2
import EventEmitter from 'events' ;
3
3
import { readFileSync } from 'fs' ;
4
4
5
5
import {
6
- SERVER_PATHS ,
6
+ DEFAULT_JOB_PROGRESS ,
7
+ DEBUG_ACCESS ,
7
8
PGID ,
8
9
PUID ,
9
- DEFAULT_JOB_PROGRESS ,
10
10
REP_UUID ,
11
+ SERVER_PATHS ,
11
12
} from './consts' ;
12
13
13
14
import { formatSql } from './formatSql' ;
15
+ import { repeat } from './repeat' ;
14
16
import {
15
17
date ,
16
18
stderr as sherr ,
@@ -24,9 +26,13 @@ import {
24
26
* * This daemon's lifecycle events should follow the naming from systemd.
25
27
*/
26
28
class Access extends EventEmitter {
29
+ private static readonly VERBOSE : string = repeat ( 'v' , DEBUG_ACCESS , {
30
+ prefix : '-' ,
31
+ } ) ;
32
+
27
33
private ps : ChildProcess ;
28
34
29
- private readonly mapToExternalEventHandler : Record <
35
+ private readonly MAP_TO_EVT_HDL : Record <
30
36
string ,
31
37
( args : { options : AccessStartOptions ; ps : ChildProcess } ) => void
32
38
> = {
@@ -42,22 +48,27 @@ class Access extends EventEmitter {
42
48
43
49
constructor ( {
44
50
eventEmitterOptions = { } ,
45
- spawnOptions = { } ,
51
+ startOptions = { } ,
46
52
} : {
47
53
eventEmitterOptions ?: ConstructorParameters < typeof EventEmitter > [ 0 ] ;
48
- spawnOptions ?: SpawnOptions ;
54
+ startOptions ?: AccessStartOptions ;
49
55
} = { } ) {
50
56
super ( eventEmitterOptions ) ;
51
57
52
- this . ps = this . start ( spawnOptions ) ;
58
+ const { args : initial = [ ] , ...rest } = startOptions ;
59
+
60
+ const args = [ ...initial , '--emit-events' , Access . VERBOSE ] . filter (
61
+ ( value ) => value !== '' ,
62
+ ) ;
63
+
64
+ this . ps = this . start ( { args, ...rest } ) ;
53
65
}
54
66
55
67
private start ( {
56
- args = [ '--emit-events' ] ,
68
+ args = [ ] ,
57
69
gid = PGID ,
58
70
restartInterval = 10000 ,
59
71
stdio = 'pipe' ,
60
- timeout = 10000 ,
61
72
uid = PUID ,
62
73
...restSpawnOptions
63
74
} : AccessStartOptions = { } ) {
@@ -66,7 +77,6 @@ class Access extends EventEmitter {
66
77
gid,
67
78
restartInterval,
68
79
stdio,
69
- timeout,
70
80
uid,
71
81
...restSpawnOptions ,
72
82
} ;
@@ -76,7 +86,6 @@ class Access extends EventEmitter {
76
86
const ps = spawn ( SERVER_PATHS . usr . sbin [ 'anvil-access-module' ] . self , args , {
77
87
gid,
78
88
stdio,
79
- timeout,
80
89
uid,
81
90
...restSpawnOptions ,
82
91
} ) ;
@@ -115,7 +124,7 @@ class Access extends EventEmitter {
115
124
116
125
const { 1 : n = '' , 2 : event } = parts ;
117
126
118
- this . mapToExternalEventHandler [ event ] ?. call ( null , { options, ps } ) ;
127
+ this . MAP_TO_EVT_HDL [ event ] ?. call ( null , { options, ps } ) ;
119
128
120
129
return n ;
121
130
} ) ;
@@ -190,21 +199,17 @@ class Access extends EventEmitter {
190
199
}
191
200
192
201
const access = new Access ( ) ;
193
- const rootAccess = new Access ( { spawnOptions : { gid : 0 , uid : 0 } } ) ;
194
202
195
203
const subroutine = async < T extends unknown [ ] > (
196
204
subroutine : string ,
197
205
{
198
206
params = [ ] ,
199
207
pre = [ 'Database' ] ,
200
- root,
201
208
} : {
202
209
params ?: unknown [ ] ;
203
210
pre ?: string [ ] ;
204
- root ?: boolean ;
205
211
} = { } ,
206
212
) => {
207
- const selectedAccess = root ? rootAccess : access ;
208
213
const chain = `${ pre . join ( '->' ) } ->${ subroutine } ` ;
209
214
210
215
const subParams : string [ ] = params . map < string > ( ( p ) => {
@@ -219,7 +224,7 @@ const subroutine = async <T extends unknown[]>(
219
224
return `"${ result . replaceAll ( '"' , '\\"' ) } "` ;
220
225
} ) ;
221
226
222
- const { sub_results : results } = await selectedAccess . interact < {
227
+ const { sub_results : results } = await access . interact < {
223
228
sub_results : T ;
224
229
} > ( 'x' , chain , ...subParams ) ;
225
230
0 commit comments