@foxford/den 4.1.0 → 4.2.0
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.
- package/README.mdx +13 -0
- package/adapter.cjs +2 -3
- package/adapter.js +1 -2
- package/bin/den.cjs +31 -51
- package/bin/den.js +31 -51
- package/builder/index.cjs +2 -3
- package/builder/index.d.cts +3 -2
- package/builder/index.d.ts +3 -2
- package/builder/index.js +1 -2
- package/{chunk-E56BKBQZ.js → chunk-AS6ZNCUY.js} +7 -7
- package/{chunk-H5DKRHHQ.cjs → chunk-BFRLBMH2.cjs} +8 -8
- package/chunk-ETO2T2TA.cjs +503 -0
- package/{chunk-E23E2VUC.js → chunk-HWDDAFGV.js} +1 -1
- package/{chunk-XQ7EHXHG.js → chunk-JW24N6O4.js} +47 -55
- package/{chunk-HJFBEJT6.cjs → chunk-MH2WIOTQ.cjs} +50 -58
- package/{chunk-XOJ2VWX4.cjs → chunk-MWDNN7Q4.cjs} +1 -1
- package/{chunk-QTWJTYZW.cjs → chunk-NOI7FSBX.cjs} +47 -55
- package/{chunk-A2CYL5LY.js → chunk-VCIMVMHG.js} +49 -57
- package/{chunk-WBHHHICS.js → chunk-WKCIOQOY.js} +1 -1
- package/{chunk-C7BM4DGX.cjs → chunk-Y7DGFJFA.cjs} +1 -1
- package/chunk-YWWRJMWN.js +503 -0
- package/define.cjs +3 -4
- package/define.js +2 -3
- package/index.cjs +54 -71
- package/index.js +53 -70
- package/island/index.cjs +5 -8
- package/island/index.js +4 -7
- package/ops/index.cjs +2 -3
- package/ops/index.js +1 -2
- package/package.json +17 -18
- package/serve/index.cjs +260 -273
- package/serve/index.js +257 -270
- package/chunk-6FNC3XMI.js +0 -45
- package/chunk-7FOG2MJV.js +0 -559
- package/chunk-IATA5QZ7.cjs +0 -559
- package/chunk-T5QVCXVB.cjs +0 -45
package/serve/index.cjs
CHANGED
|
@@ -6,8 +6,8 @@ var _chunkKQAB6C6Qcjs = require('../chunk-KQAB6C6Q.cjs');
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
var
|
|
10
|
-
require('../chunk-
|
|
9
|
+
var _chunkNOI7FSBXcjs = require('../chunk-NOI7FSBX.cjs');
|
|
10
|
+
require('../chunk-MWDNN7Q4.cjs');
|
|
11
11
|
require('../chunk-6QV4L6R2.cjs');
|
|
12
12
|
|
|
13
13
|
|
|
@@ -18,12 +18,7 @@ var _chunkA6ZPAM6Zcjs = require('../chunk-A6ZPAM6Z.cjs');
|
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
|
|
21
|
-
var
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
var _chunkT5QVCXVBcjs = require('../chunk-T5QVCXVB.cjs');
|
|
21
|
+
var _chunkMH2WIOTQcjs = require('../chunk-MH2WIOTQ.cjs');
|
|
27
22
|
|
|
28
23
|
// src/serve/index.ts
|
|
29
24
|
var _fastify = require('fastify'); var _fastify2 = _interopRequireDefault(_fastify);
|
|
@@ -51,7 +46,7 @@ var MIME = {
|
|
|
51
46
|
var HASHED_DIR = "assets/";
|
|
52
47
|
function assetsPrefix(base) {
|
|
53
48
|
const { pathname } = new URL(base, "http://assets.invalid");
|
|
54
|
-
return pathname.endsWith("/") ? pathname :
|
|
49
|
+
return pathname.endsWith("/") ? pathname : "".concat(pathname, "/");
|
|
55
50
|
}
|
|
56
51
|
function assetFile(dir, relative) {
|
|
57
52
|
const file = _path2.default.normalize(_path2.default.join(dir, relative));
|
|
@@ -60,7 +55,7 @@ function assetFile(dir, relative) {
|
|
|
60
55
|
function registerAssets(instance, source) {
|
|
61
56
|
const dir = _path2.default.resolve(source.dir);
|
|
62
57
|
const prefix = assetsPrefix(source.base);
|
|
63
|
-
instance.get(
|
|
58
|
+
instance.get("".concat(prefix, "*"), (request, reply) => {
|
|
64
59
|
var _a;
|
|
65
60
|
const relative = request.params["*"];
|
|
66
61
|
const file = assetFile(dir, relative);
|
|
@@ -79,14 +74,14 @@ function registerAssets(instance, source) {
|
|
|
79
74
|
|
|
80
75
|
var DEFAULT_SLOW_RENDER_MS = 1e3;
|
|
81
76
|
function claim(app) {
|
|
82
|
-
return
|
|
77
|
+
return _chunkNOI7FSBXcjs.normalizeRoutes.call(void 0, app.routes).map((route) => route.source);
|
|
83
78
|
}
|
|
84
79
|
function matchClaim(app, pathname) {
|
|
85
80
|
var _a, _b;
|
|
86
81
|
if (claim(app).length === 0) {
|
|
87
82
|
return {};
|
|
88
83
|
}
|
|
89
|
-
return (_b = (_a =
|
|
84
|
+
return (_b = (_a = _chunkNOI7FSBXcjs.matchRoute.call(void 0, app.routes, pathname)) == null ? void 0 : _a.params) != null ? _b : null;
|
|
90
85
|
}
|
|
91
86
|
function requestContextOf(req, fromBody) {
|
|
92
87
|
const headers = {};
|
|
@@ -100,8 +95,11 @@ function requestContextOf(req, fromBody) {
|
|
|
100
95
|
}
|
|
101
96
|
}
|
|
102
97
|
const cookie = req.headers.cookie;
|
|
103
|
-
const backendHeaders =
|
|
104
|
-
|
|
98
|
+
const backendHeaders = {
|
|
99
|
+
...typeof cookie === "string" && cookie !== "" ? { cookie } : {},
|
|
100
|
+
...fromBody
|
|
101
|
+
};
|
|
102
|
+
return { backendHeaders, cookies: _chunkNOI7FSBXcjs.parseCookieHeader.call(void 0, cookie), headers };
|
|
105
103
|
}
|
|
106
104
|
function backendHeadersOf(value) {
|
|
107
105
|
if (value === void 0) {
|
|
@@ -125,43 +123,41 @@ var appPlugin = (instance, options) => {
|
|
|
125
123
|
void reply.header("cache-control", "no-store");
|
|
126
124
|
return claim(app);
|
|
127
125
|
});
|
|
128
|
-
function handleRender(req, reply, target, inputs, rawBackendHeaders) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
serveLog.warn(`\u043C\u0435\u0434\u043B\u0435\u043D\u043D\u044B\u0439 \u0440\u0435\u043D\u0434\u0435\u0440 \xAB${pathname}\xBB: ${String(elapsed)}\u043C\u0441`);
|
|
158
|
-
}
|
|
159
|
-
return result;
|
|
160
|
-
} catch (error) {
|
|
161
|
-
serveLog.error(`\u0440\u0435\u043D\u0434\u0435\u0440 \xAB${pathname}\xBB \u0443\u043F\u0430\u043B \u0437\u0430 ${String(Date.now() - startedAt)}\u043C\u0441`, error);
|
|
162
|
-
return reply.send(error instanceof Error ? error : new Error(String(error)));
|
|
126
|
+
async function handleRender(req, reply, target, inputs, rawBackendHeaders) {
|
|
127
|
+
if (typeof target !== "string" || !target.startsWith("/")) {
|
|
128
|
+
return reply.code(400).send({ error: "\u043D\u0435 \u043F\u0435\u0440\u0435\u0434\u0430\u043D \u0438\u043B\u0438 \u043D\u0435\u0432\u0430\u043B\u0438\u0434\u0435\u043D \u043F\u0430\u0440\u0430\u043C\u0435\u0442\u0440 url" });
|
|
129
|
+
}
|
|
130
|
+
if (inputs !== void 0 && (typeof inputs !== "object" || inputs === null || Array.isArray(inputs))) {
|
|
131
|
+
return reply.code(400).send({ error: "inputs \u0434\u043E\u043B\u0436\u043D\u044B \u0431\u044B\u0442\u044C \u043E\u0431\u044A\u0435\u043A\u0442\u043E\u043C \u043F\u043E \u043A\u043B\u044E\u0447\u0443 token.description" });
|
|
132
|
+
}
|
|
133
|
+
const backendHeaders = backendHeadersOf(rawBackendHeaders);
|
|
134
|
+
if (backendHeaders === "invalid") {
|
|
135
|
+
return reply.code(400).send({ error: "backendHeaders \u0434\u043E\u043B\u0436\u043D\u044B \u0431\u044B\u0442\u044C \u043E\u0431\u044A\u0435\u043A\u0442\u043E\u043C \xAB\u0438\u043C\u044F \u0437\u0430\u0433\u043E\u043B\u043E\u0432\u043A\u0430 \u2192 \u0441\u0442\u0440\u043E\u043A\u0430\xBB" });
|
|
136
|
+
}
|
|
137
|
+
const { pathname, search } = new URL(target, "http://localhost");
|
|
138
|
+
const params = matchClaim(app, pathname);
|
|
139
|
+
if (params === null) {
|
|
140
|
+
serveLog.error("\u043F\u0443\u0442\u044C \xAB".concat(pathname, "\xBB \u0432\u043D\u0435 \u0437\u0430\u044F\u0432\u043A\u0438 \u043F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u044F"));
|
|
141
|
+
return reply.code(404).send({ error: "\u043F\u0443\u0442\u044C \xAB".concat(pathname, "\xBB \u0432\u043D\u0435 \u0437\u0430\u044F\u0432\u043A\u0438 \u043F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u044F") });
|
|
142
|
+
}
|
|
143
|
+
const startedAt = Date.now();
|
|
144
|
+
try {
|
|
145
|
+
const result = await _chunkNOI7FSBXcjs.renderApp.call(void 0, app, {
|
|
146
|
+
context: requestContextOf(req, backendHeaders),
|
|
147
|
+
inputs,
|
|
148
|
+
params,
|
|
149
|
+
pathname,
|
|
150
|
+
search
|
|
151
|
+
});
|
|
152
|
+
const elapsed = Date.now() - startedAt;
|
|
153
|
+
if (elapsed >= slowRenderMs) {
|
|
154
|
+
serveLog.warn("\u043C\u0435\u0434\u043B\u0435\u043D\u043D\u044B\u0439 \u0440\u0435\u043D\u0434\u0435\u0440 \xAB".concat(pathname, "\xBB: ").concat(String(elapsed), "\u043C\u0441"));
|
|
163
155
|
}
|
|
164
|
-
|
|
156
|
+
return result;
|
|
157
|
+
} catch (error) {
|
|
158
|
+
serveLog.error("\u0440\u0435\u043D\u0434\u0435\u0440 \xAB".concat(pathname, "\xBB \u0443\u043F\u0430\u043B \u0437\u0430 ").concat(String(Date.now() - startedAt), "\u043C\u0441"), error);
|
|
159
|
+
return reply.send(error instanceof Error ? error : new Error(String(error)));
|
|
160
|
+
}
|
|
165
161
|
}
|
|
166
162
|
instance.get(
|
|
167
163
|
"/_render",
|
|
@@ -188,187 +184,180 @@ var _cluster = require('cluster'); var _cluster2 = _interopRequireDefault(_clust
|
|
|
188
184
|
var ALIVE = "den:app-worker-alive";
|
|
189
185
|
var SIGNALS = ["SIGTERM", "SIGINT"];
|
|
190
186
|
var SHUTDOWN_MS = 1e4;
|
|
191
|
-
function startWorker(appName, start) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
187
|
+
async function startWorker(appName, start) {
|
|
188
|
+
const aggregation = _chunkMH2WIOTQcjs.clusterMetrics.call(void 0, );
|
|
189
|
+
const logger2 = _logger.log.getLogger(appName).getLogger("cluster");
|
|
190
|
+
let timer;
|
|
191
|
+
let closing;
|
|
192
|
+
const starting = start();
|
|
193
|
+
const close = () => {
|
|
194
|
+
closing != null ? closing : closing = (async () => {
|
|
195
|
+
clearInterval(timer);
|
|
196
|
+
try {
|
|
197
|
+
const instance = await starting;
|
|
198
|
+
await instance.close();
|
|
199
|
+
} finally {
|
|
201
200
|
try {
|
|
202
|
-
|
|
203
|
-
yield instance.close();
|
|
201
|
+
if (process.connected) await aggregation.shutdown();
|
|
204
202
|
} finally {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
for (const signal of SIGNALS) process.removeListener(signal, shutdown);
|
|
209
|
-
process.removeListener("disconnect", shutdown);
|
|
210
|
-
if (process.connected) process.disconnect();
|
|
211
|
-
}
|
|
203
|
+
for (const signal of SIGNALS) process.removeListener(signal, shutdown);
|
|
204
|
+
process.removeListener("disconnect", shutdown);
|
|
205
|
+
if (process.connected) process.disconnect();
|
|
212
206
|
}
|
|
213
|
-
}))();
|
|
214
|
-
return closing;
|
|
215
|
-
};
|
|
216
|
-
const shutdown = () => {
|
|
217
|
-
const deadline = setTimeout(() => process.exit(1), SHUTDOWN_MS);
|
|
218
|
-
deadline.unref();
|
|
219
|
-
void close().then(() => process.exit(0)).catch((error) => {
|
|
220
|
-
logger2.error("\u043E\u0441\u0442\u0430\u043D\u043E\u0432\u043A\u0430 \u0432\u043E\u0440\u043A\u0435\u0440\u0430 \u043D\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043B\u0430\u0441\u044C:", error);
|
|
221
|
-
process.exit(1);
|
|
222
|
-
});
|
|
223
|
-
};
|
|
224
|
-
for (const signal of SIGNALS) process.on(signal, shutdown);
|
|
225
|
-
process.once("disconnect", shutdown);
|
|
226
|
-
try {
|
|
227
|
-
yield starting;
|
|
228
|
-
if (!closing) {
|
|
229
|
-
const notify = () => {
|
|
230
|
-
var _a;
|
|
231
|
-
if (process.connected) (_a = process.send) == null ? void 0 : _a.call(process, { type: ALIVE });
|
|
232
|
-
};
|
|
233
|
-
timer = setInterval(notify, 2e3);
|
|
234
|
-
timer.unref();
|
|
235
|
-
notify();
|
|
236
207
|
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
|
|
208
|
+
})();
|
|
209
|
+
return closing;
|
|
210
|
+
};
|
|
211
|
+
const shutdown = () => {
|
|
212
|
+
const deadline = setTimeout(() => process.exit(1), SHUTDOWN_MS);
|
|
213
|
+
deadline.unref();
|
|
214
|
+
void close().then(() => process.exit(0)).catch((error) => {
|
|
215
|
+
logger2.error("\u043E\u0441\u0442\u0430\u043D\u043E\u0432\u043A\u0430 \u0432\u043E\u0440\u043A\u0435\u0440\u0430 \u043D\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043B\u0430\u0441\u044C:", error);
|
|
216
|
+
process.exit(1);
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
for (const signal of SIGNALS) process.on(signal, shutdown);
|
|
220
|
+
process.once("disconnect", shutdown);
|
|
221
|
+
try {
|
|
222
|
+
await starting;
|
|
223
|
+
if (!closing) {
|
|
224
|
+
const notify = () => {
|
|
225
|
+
var _a;
|
|
226
|
+
if (process.connected) (_a = process.send) == null ? void 0 : _a.call(process, { type: ALIVE });
|
|
227
|
+
};
|
|
228
|
+
timer = setInterval(notify, 2e3);
|
|
229
|
+
timer.unref();
|
|
230
|
+
notify();
|
|
240
231
|
}
|
|
241
|
-
|
|
242
|
-
|
|
232
|
+
} catch (error) {
|
|
233
|
+
await close().catch(() => void 0);
|
|
234
|
+
throw error;
|
|
235
|
+
}
|
|
236
|
+
return { close };
|
|
243
237
|
}
|
|
244
238
|
|
|
245
239
|
// src/serve/process.ts
|
|
246
240
|
var STARTUP_MS = 6e4;
|
|
247
|
-
function startAppProcess(appName, options, start) {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
return startPrimary(appName, options, workers);
|
|
264
|
-
});
|
|
241
|
+
async function startAppProcess(appName, options, start) {
|
|
242
|
+
var _a;
|
|
243
|
+
const workers = (_a = options.workers) != null ? _a : 1;
|
|
244
|
+
if (!Number.isSafeInteger(workers) || workers < 1) {
|
|
245
|
+
throw new Error("WORKERS must be a positive integer");
|
|
246
|
+
}
|
|
247
|
+
if (workers === 1) {
|
|
248
|
+
const instance = await start(options);
|
|
249
|
+
return { close: () => instance.close() };
|
|
250
|
+
}
|
|
251
|
+
const metrics = _chunkMH2WIOTQcjs.getProcessMetrics.call(void 0, );
|
|
252
|
+
metrics.startRuntime(appName);
|
|
253
|
+
if (_cluster2.default.isWorker) {
|
|
254
|
+
return startWorker(appName, () => start({ ...options, gracefulShutdown: false, ops: false }));
|
|
255
|
+
}
|
|
256
|
+
return startPrimary(appName, options, workers);
|
|
265
257
|
}
|
|
266
|
-
function startPrimary(appName, options, workers) {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
258
|
+
async function startPrimary(appName, options, workers) {
|
|
259
|
+
const aggregation = _chunkMH2WIOTQcjs.clusterMetrics.call(void 0, );
|
|
260
|
+
const logger2 = _logger.log.getLogger(appName).getLogger("cluster");
|
|
261
|
+
const children = /* @__PURE__ */ new Set();
|
|
262
|
+
const alive = /* @__PURE__ */ new Map();
|
|
263
|
+
let ops;
|
|
264
|
+
let closing;
|
|
265
|
+
let started = false;
|
|
266
|
+
let resolveReady = () => void 0;
|
|
267
|
+
let rejectReady = () => void 0;
|
|
268
|
+
const ready = new Promise((resolve, reject) => {
|
|
269
|
+
resolveReady = resolve;
|
|
270
|
+
rejectReady = reject;
|
|
271
|
+
});
|
|
272
|
+
void ready.catch(() => void 0);
|
|
273
|
+
const timeout = setTimeout(() => rejectReady(new Error("Den workers startup timeout")), STARTUP_MS);
|
|
274
|
+
const onMessage = (worker, message) => {
|
|
275
|
+
if (children.has(worker) && typeof message === "object" && message !== null && "type" in message && message.type === ALIVE) {
|
|
276
|
+
alive.set(worker.id, Date.now());
|
|
277
|
+
if (alive.size === workers) resolveReady();
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
const close = () => {
|
|
281
|
+
closing != null ? closing : closing = (async () => {
|
|
282
|
+
clearTimeout(timeout);
|
|
283
|
+
resolveReady();
|
|
284
|
+
_cluster2.default.removeListener("message", onMessage);
|
|
285
|
+
await Promise.all([...children].map(stopWorker));
|
|
286
|
+
if (ops) await _chunkMH2WIOTQcjs.closeHttpServer.call(void 0, ops);
|
|
287
|
+
await aggregation.shutdown();
|
|
288
|
+
for (const signal of SIGNALS) process.removeListener(signal, shutdown);
|
|
289
|
+
})();
|
|
290
|
+
return closing;
|
|
291
|
+
};
|
|
292
|
+
const shutdown = () => {
|
|
293
|
+
void close().then(() => process.exit()).catch((error) => {
|
|
294
|
+
logger2.error("\u043E\u0441\u0442\u0430\u043D\u043E\u0432\u043A\u0430 \u043A\u043B\u0430\u0441\u0442\u0435\u0440\u0430 \u043D\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043B\u0430\u0441\u044C:", error);
|
|
295
|
+
process.exit(1);
|
|
280
296
|
});
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
297
|
+
};
|
|
298
|
+
const failed = (error) => {
|
|
299
|
+
if (closing) return;
|
|
300
|
+
if (!started) {
|
|
301
|
+
rejectReady(error);
|
|
302
|
+
} else {
|
|
303
|
+
logger2.error(error);
|
|
304
|
+
process.exitCode = 1;
|
|
305
|
+
shutdown();
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
_cluster2.default.on("message", onMessage);
|
|
309
|
+
if (options.gracefulShutdown !== false) {
|
|
310
|
+
for (const signal of SIGNALS) process.on(signal, shutdown);
|
|
311
|
+
}
|
|
312
|
+
try {
|
|
313
|
+
if (options.ops !== false) {
|
|
314
|
+
const supplied = options.ops;
|
|
315
|
+
ops = await _chunkMH2WIOTQcjs.startOpsServer.call(void 0, {
|
|
316
|
+
...supplied,
|
|
317
|
+
collect: aggregation.collect,
|
|
318
|
+
contentType: aggregation.contentType,
|
|
319
|
+
isLive: () => {
|
|
320
|
+
var _a;
|
|
321
|
+
return [...children].some((worker) => {
|
|
322
|
+
var _a2;
|
|
323
|
+
return worker.isConnected() && Date.now() - ((_a2 = alive.get(worker.id)) != null ? _a2 : 0) < 1e4;
|
|
324
|
+
}) && ((_a = supplied == null ? void 0 : supplied.isLive) == null ? void 0 : _a.call(supplied)) !== false;
|
|
325
|
+
}
|
|
305
326
|
});
|
|
306
|
-
};
|
|
307
|
-
const failed = (error) => {
|
|
308
|
-
if (closing) return;
|
|
309
|
-
if (!started) {
|
|
310
|
-
rejectReady(error);
|
|
311
|
-
} else {
|
|
312
|
-
logger2.error(error);
|
|
313
|
-
process.exitCode = 1;
|
|
314
|
-
shutdown();
|
|
315
|
-
}
|
|
316
|
-
};
|
|
317
|
-
_cluster2.default.on("message", onMessage);
|
|
318
|
-
if (options.gracefulShutdown !== false) {
|
|
319
|
-
for (const signal of SIGNALS) process.on(signal, shutdown);
|
|
320
327
|
}
|
|
321
|
-
|
|
322
|
-
if (
|
|
323
|
-
|
|
324
|
-
ops = yield _chunkHJFBEJT6cjs.startOpsServer.call(void 0, _chunkT5QVCXVBcjs.__spreadProps.call(void 0, _chunkT5QVCXVBcjs.__spreadValues.call(void 0, {}, supplied), {
|
|
325
|
-
collect: aggregation.collect,
|
|
326
|
-
contentType: aggregation.contentType,
|
|
327
|
-
isLive: () => {
|
|
328
|
-
var _a;
|
|
329
|
-
return [...children].some((worker) => {
|
|
330
|
-
var _a2;
|
|
331
|
-
return worker.isConnected() && Date.now() - ((_a2 = alive.get(worker.id)) != null ? _a2 : 0) < 1e4;
|
|
332
|
-
}) && ((_a = supplied == null ? void 0 : supplied.isLive) == null ? void 0 : _a.call(supplied)) !== false;
|
|
333
|
-
}
|
|
334
|
-
}));
|
|
335
|
-
}
|
|
336
|
-
if (closing) {
|
|
337
|
-
if (ops) yield _chunkHJFBEJT6cjs.closeHttpServer.call(void 0, ops);
|
|
338
|
-
yield closing;
|
|
339
|
-
return { close };
|
|
340
|
-
}
|
|
341
|
-
for (let i = 0; i < workers; i++) {
|
|
342
|
-
const worker = _cluster2.default.fork();
|
|
343
|
-
children.add(worker);
|
|
344
|
-
worker.once("error", failed);
|
|
345
|
-
worker.once("exit", (code, signal) => {
|
|
346
|
-
alive.delete(worker.id);
|
|
347
|
-
failed(new Error(`Den worker ${String(worker.process.pid)} exited (${String(code != null ? code : signal)})`));
|
|
348
|
-
});
|
|
349
|
-
}
|
|
350
|
-
yield ready;
|
|
351
|
-
started = true;
|
|
352
|
-
clearTimeout(timeout);
|
|
353
|
-
if (!closing) logger2.info(`\u0437\u0430\u043F\u0443\u0449\u0435\u043D\u043E \u0432\u043E\u0440\u043A\u0435\u0440\u043E\u0432: ${String(workers)}`);
|
|
328
|
+
if (closing) {
|
|
329
|
+
if (ops) await _chunkMH2WIOTQcjs.closeHttpServer.call(void 0, ops);
|
|
330
|
+
await closing;
|
|
354
331
|
return { close };
|
|
355
|
-
} catch (error) {
|
|
356
|
-
yield close();
|
|
357
|
-
throw error;
|
|
358
332
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
worker.once("exit", () => {
|
|
367
|
-
clearTimeout(timeout);
|
|
368
|
-
resolve();
|
|
333
|
+
for (let i = 0; i < workers; i++) {
|
|
334
|
+
const worker = _cluster2.default.fork();
|
|
335
|
+
children.add(worker);
|
|
336
|
+
worker.once("error", failed);
|
|
337
|
+
worker.once("exit", (code, signal) => {
|
|
338
|
+
alive.delete(worker.id);
|
|
339
|
+
failed(new Error("Den worker ".concat(String(worker.process.pid), " exited (").concat(String(code != null ? code : signal), ")")));
|
|
369
340
|
});
|
|
370
|
-
|
|
341
|
+
}
|
|
342
|
+
await ready;
|
|
343
|
+
started = true;
|
|
344
|
+
clearTimeout(timeout);
|
|
345
|
+
if (!closing) logger2.info("\u0437\u0430\u043F\u0443\u0449\u0435\u043D\u043E \u0432\u043E\u0440\u043A\u0435\u0440\u043E\u0432: ".concat(String(workers)));
|
|
346
|
+
return { close };
|
|
347
|
+
} catch (error) {
|
|
348
|
+
await close();
|
|
349
|
+
throw error;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
async function stopWorker(worker) {
|
|
353
|
+
if (worker.isDead()) return;
|
|
354
|
+
await new Promise((resolve) => {
|
|
355
|
+
const timeout = setTimeout(() => worker.kill("SIGKILL"), SHUTDOWN_MS);
|
|
356
|
+
worker.once("exit", () => {
|
|
357
|
+
clearTimeout(timeout);
|
|
358
|
+
resolve();
|
|
371
359
|
});
|
|
360
|
+
worker.kill("SIGTERM");
|
|
372
361
|
});
|
|
373
362
|
}
|
|
374
363
|
|
|
@@ -377,70 +366,68 @@ function serveAppProcess(app, options = {}) {
|
|
|
377
366
|
return startAppProcess(app.name, options, (serveOptions) => serveApp(app, serveOptions));
|
|
378
367
|
}
|
|
379
368
|
var SHUTDOWN_SIGNALS = ["SIGTERM", "SIGINT"];
|
|
380
|
-
function serveApp(
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
return;
|
|
396
|
-
}
|
|
397
|
-
metrics.trackHttp(req.raw, reply.raw, () => {
|
|
398
|
-
var _a2;
|
|
399
|
-
return (_a2 = req.routeOptions.url) != null ? _a2 : "unmatched";
|
|
400
|
-
});
|
|
401
|
-
done();
|
|
402
|
-
});
|
|
403
|
-
instance.addHook("onClose", () => _chunkT5QVCXVBcjs.__async.call(void 0, null, null, function* () {
|
|
404
|
-
for (const signal of SHUTDOWN_SIGNALS) {
|
|
405
|
-
process.removeListener(signal, shutdown);
|
|
406
|
-
}
|
|
407
|
-
if (ops) {
|
|
408
|
-
yield _chunkHJFBEJT6cjs.closeHttpServer.call(void 0, ops);
|
|
409
|
-
}
|
|
410
|
-
}));
|
|
411
|
-
yield instance.register(appPlugin, { app, slowRenderMs: options.slowRenderMs });
|
|
412
|
-
if (options.assets) {
|
|
413
|
-
registerAssets(instance, options.assets);
|
|
369
|
+
async function serveApp(app, options = {}) {
|
|
370
|
+
var _a, _b, _c, _d, _e;
|
|
371
|
+
const appLog = (_a = _logger.log.getLogger(app.name)) != null ? _a : _chunkA6ZPAM6Zcjs.logger;
|
|
372
|
+
const serveLog = (_b = appLog.getLogger("serve")) != null ? _b : appLog;
|
|
373
|
+
const instance = _fastify2.default.call(void 0, { logger: false });
|
|
374
|
+
const metrics = _chunkMH2WIOTQcjs.getProcessMetrics.call(void 0, );
|
|
375
|
+
metrics.startRuntime(app.name);
|
|
376
|
+
let ops;
|
|
377
|
+
const shutdown = () => {
|
|
378
|
+
void instance.close();
|
|
379
|
+
};
|
|
380
|
+
instance.addHook("onRequest", (req, reply, done) => {
|
|
381
|
+
if (_chunkMH2WIOTQcjs.isOpsPath.call(void 0, req.url)) {
|
|
382
|
+
void reply.code(404).send();
|
|
383
|
+
return;
|
|
414
384
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
});
|
|
419
|
-
instance.addHook("onResponse", (req, reply, done) => {
|
|
420
|
-
const elapsed = reply.elapsedTime.toFixed(1);
|
|
421
|
-
serveLog.info(`\u2190 ${String(reply.statusCode)} ${req.method} ${req.url} ${elapsed}\u043C\u0441`);
|
|
422
|
-
done();
|
|
385
|
+
metrics.trackHttp(req.raw, reply.raw, () => {
|
|
386
|
+
var _a2;
|
|
387
|
+
return (_a2 = req.routeOptions.url) != null ? _a2 : "unmatched";
|
|
423
388
|
});
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
yield instance.listen({ host: (_d = options.host) != null ? _d : "0.0.0.0", port: (_e = options.port) != null ? _e : 8080 });
|
|
430
|
-
} catch (error) {
|
|
431
|
-
yield instance.close();
|
|
432
|
-
throw error;
|
|
389
|
+
done();
|
|
390
|
+
});
|
|
391
|
+
instance.addHook("onClose", async () => {
|
|
392
|
+
for (const signal of SHUTDOWN_SIGNALS) {
|
|
393
|
+
process.removeListener(signal, shutdown);
|
|
433
394
|
}
|
|
434
|
-
if (
|
|
435
|
-
|
|
436
|
-
process.once(signal, shutdown);
|
|
437
|
-
}
|
|
395
|
+
if (ops) {
|
|
396
|
+
await _chunkMH2WIOTQcjs.closeHttpServer.call(void 0, ops);
|
|
438
397
|
}
|
|
439
|
-
serveLog.info(
|
|
440
|
-
`\u043F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u0435 \xAB${app.name}\xBB \u0441\u043B\u0443\u0448\u0430\u0435\u0442 ${instance.addresses().map((a) => String(a.port)).join(", ")}`
|
|
441
|
-
);
|
|
442
|
-
return instance;
|
|
443
398
|
});
|
|
399
|
+
await instance.register(appPlugin, { app, slowRenderMs: options.slowRenderMs });
|
|
400
|
+
if (options.assets) {
|
|
401
|
+
registerAssets(instance, options.assets);
|
|
402
|
+
}
|
|
403
|
+
instance.addHook("onRequest", (req, _reply, done) => {
|
|
404
|
+
serveLog.debug("\u2192 ".concat(req.method, " ").concat(req.url));
|
|
405
|
+
done();
|
|
406
|
+
});
|
|
407
|
+
instance.addHook("onResponse", (req, reply, done) => {
|
|
408
|
+
const elapsed = reply.elapsedTime.toFixed(1);
|
|
409
|
+
serveLog.info("\u2190 ".concat(String(reply.statusCode), " ").concat(req.method, " ").concat(req.url, " ").concat(elapsed, "\u043C\u0441"));
|
|
410
|
+
done();
|
|
411
|
+
});
|
|
412
|
+
try {
|
|
413
|
+
await ((_c = options.configure) == null ? void 0 : _c.call(options, instance));
|
|
414
|
+
if (options.ops !== false) {
|
|
415
|
+
ops = await _chunkMH2WIOTQcjs.startOpsServer.call(void 0, options.ops);
|
|
416
|
+
}
|
|
417
|
+
await instance.listen({ host: (_d = options.host) != null ? _d : "0.0.0.0", port: (_e = options.port) != null ? _e : 8080 });
|
|
418
|
+
} catch (error) {
|
|
419
|
+
await instance.close();
|
|
420
|
+
throw error;
|
|
421
|
+
}
|
|
422
|
+
if (options.gracefulShutdown !== false) {
|
|
423
|
+
for (const signal of SHUTDOWN_SIGNALS) {
|
|
424
|
+
process.once(signal, shutdown);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
serveLog.info(
|
|
428
|
+
"\u043F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u0435 \xAB".concat(app.name, "\xBB \u0441\u043B\u0443\u0448\u0430\u0435\u0442 ").concat(instance.addresses().map((a) => String(a.port)).join(", "))
|
|
429
|
+
);
|
|
430
|
+
return instance;
|
|
444
431
|
}
|
|
445
432
|
|
|
446
433
|
|