blun-king-cli 9.1.476 → 9.1.477
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
3
|
+
|
|
4
|
+
import { startAgentApiServiceFromEnvironment } from './agent-api-service-environment.mjs';
|
|
5
|
+
|
|
6
|
+
function requireProcessLike(value) {
|
|
7
|
+
if (value === null
|
|
8
|
+
|| typeof value !== 'object'
|
|
9
|
+
|| typeof value.on !== 'function'
|
|
10
|
+
|| typeof value.removeListener !== 'function') {
|
|
11
|
+
throw new TypeError('processLike must provide on and removeListener');
|
|
12
|
+
}
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function requireFunction(value, label) {
|
|
17
|
+
if (typeof value !== 'function') {
|
|
18
|
+
throw new TypeError(`${label} must be a function`);
|
|
19
|
+
}
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function copyAddress(value) {
|
|
24
|
+
if (value === null
|
|
25
|
+
|| typeof value !== 'object'
|
|
26
|
+
|| typeof value.address !== 'string'
|
|
27
|
+
|| typeof value.family !== 'string'
|
|
28
|
+
|| !Number.isInteger(value.port)) {
|
|
29
|
+
throw new TypeError('Agent API service must provide a network address');
|
|
30
|
+
}
|
|
31
|
+
return Object.freeze({
|
|
32
|
+
address: value.address,
|
|
33
|
+
family: value.family,
|
|
34
|
+
port: value.port,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function runAgentApiServiceProcess(options = {}) {
|
|
39
|
+
const processLike = requireProcessLike(
|
|
40
|
+
options.processLike === undefined ? process : options.processLike,
|
|
41
|
+
);
|
|
42
|
+
const startService = requireFunction(
|
|
43
|
+
options.startService === undefined
|
|
44
|
+
? startAgentApiServiceFromEnvironment
|
|
45
|
+
: options.startService,
|
|
46
|
+
'startService',
|
|
47
|
+
);
|
|
48
|
+
const writeStdout = requireFunction(
|
|
49
|
+
options.writeStdout === undefined
|
|
50
|
+
? (value) => process.stdout.write(value)
|
|
51
|
+
: options.writeStdout,
|
|
52
|
+
'writeStdout',
|
|
53
|
+
);
|
|
54
|
+
const writeStderr = requireFunction(
|
|
55
|
+
options.writeStderr === undefined
|
|
56
|
+
? (value) => process.stderr.write(value)
|
|
57
|
+
: options.writeStderr,
|
|
58
|
+
'writeStderr',
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
let running;
|
|
62
|
+
let requestedSignal;
|
|
63
|
+
let shutdownPromise;
|
|
64
|
+
let resolveClosed;
|
|
65
|
+
let rejectClosed;
|
|
66
|
+
const closed = new Promise((resolve, reject) => {
|
|
67
|
+
resolveClosed = resolve;
|
|
68
|
+
rejectClosed = reject;
|
|
69
|
+
});
|
|
70
|
+
closed.catch(() => {});
|
|
71
|
+
|
|
72
|
+
function removeSignalHandlers() {
|
|
73
|
+
processLike.removeListener('SIGINT', onSigint);
|
|
74
|
+
processLike.removeListener('SIGTERM', onSigterm);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function beginShutdown(signal) {
|
|
78
|
+
if (shutdownPromise !== undefined) return shutdownPromise;
|
|
79
|
+
removeSignalHandlers();
|
|
80
|
+
shutdownPromise = (async () => {
|
|
81
|
+
try {
|
|
82
|
+
await running.close();
|
|
83
|
+
if (signal !== undefined) {
|
|
84
|
+
processLike.exitCode = 0;
|
|
85
|
+
writeStdout(`Agent API service stopped after ${signal}.\n`);
|
|
86
|
+
}
|
|
87
|
+
resolveClosed();
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (signal !== undefined) {
|
|
90
|
+
processLike.exitCode = 1;
|
|
91
|
+
writeStderr('Agent API service shutdown failed.\n');
|
|
92
|
+
}
|
|
93
|
+
rejectClosed(error);
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
})();
|
|
97
|
+
return shutdownPromise;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function requestShutdown(signal) {
|
|
101
|
+
if (requestedSignal !== undefined) return;
|
|
102
|
+
requestedSignal = signal;
|
|
103
|
+
if (running !== undefined) {
|
|
104
|
+
void beginShutdown(signal).catch(() => {});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function onSigint() {
|
|
109
|
+
requestShutdown('SIGINT');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function onSigterm() {
|
|
113
|
+
requestShutdown('SIGTERM');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
processLike.on('SIGINT', onSigint);
|
|
117
|
+
processLike.on('SIGTERM', onSigterm);
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
running = await startService({
|
|
121
|
+
env: options.env === undefined ? process.env : options.env,
|
|
122
|
+
});
|
|
123
|
+
if (running === null
|
|
124
|
+
|| typeof running !== 'object'
|
|
125
|
+
|| typeof running.close !== 'function') {
|
|
126
|
+
throw new TypeError('Agent API service must provide close');
|
|
127
|
+
}
|
|
128
|
+
const address = copyAddress(running.address);
|
|
129
|
+
if (requestedSignal !== undefined) {
|
|
130
|
+
void beginShutdown(requestedSignal).catch(() => {});
|
|
131
|
+
} else {
|
|
132
|
+
writeStdout(`Agent API service listening on ${address.address}:${address.port}.\n`);
|
|
133
|
+
}
|
|
134
|
+
return Object.freeze({
|
|
135
|
+
address,
|
|
136
|
+
close() {
|
|
137
|
+
return beginShutdown();
|
|
138
|
+
},
|
|
139
|
+
closed,
|
|
140
|
+
});
|
|
141
|
+
} catch (error) {
|
|
142
|
+
removeSignalHandlers();
|
|
143
|
+
if (running !== undefined
|
|
144
|
+
&& typeof running?.close === 'function'
|
|
145
|
+
&& shutdownPromise === undefined) {
|
|
146
|
+
try {
|
|
147
|
+
await running.close();
|
|
148
|
+
} catch {
|
|
149
|
+
// The startup error remains the actionable failure.
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
rejectClosed(error);
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function isDirectExecution() {
|
|
158
|
+
if (typeof process.argv[1] !== 'string' || process.argv[1].length === 0) return false;
|
|
159
|
+
return pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (isDirectExecution()) {
|
|
163
|
+
runAgentApiServiceProcess().catch(() => {
|
|
164
|
+
process.exitCode = 1;
|
|
165
|
+
process.stderr.write('Agent API service failed to start.\n');
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export {
|
|
170
|
+
runAgentApiServiceProcess,
|
|
171
|
+
};
|