@foxford/den 4.1.0 → 4.2.1

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/serve/index.cjs CHANGED
@@ -6,7 +6,7 @@ var _chunkKQAB6C6Qcjs = require('../chunk-KQAB6C6Q.cjs');
6
6
 
7
7
 
8
8
 
9
- var _chunkQTWJTYZWcjs = require('../chunk-QTWJTYZW.cjs');
9
+ var _chunkI42ZL2FScjs = require('../chunk-I42ZL2FS.cjs');
10
10
  require('../chunk-XOJ2VWX4.cjs');
11
11
  require('../chunk-6QV4L6R2.cjs');
12
12
 
@@ -18,12 +18,7 @@ var _chunkA6ZPAM6Zcjs = require('../chunk-A6ZPAM6Z.cjs');
18
18
 
19
19
 
20
20
 
21
- var _chunkHJFBEJT6cjs = require('../chunk-HJFBEJT6.cjs');
22
-
23
-
24
-
25
-
26
- var _chunkT5QVCXVBcjs = require('../chunk-T5QVCXVB.cjs');
21
+ var _chunkWS6QCYBRcjs = require('../chunk-WS6QCYBR.cjs');
27
22
 
28
23
  // src/serve/index.ts
29
24
  var _fastify = require('fastify'); var _fastify2 = _interopRequireDefault(_fastify);
@@ -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 _chunkQTWJTYZWcjs.normalizeRoutes.call(void 0, app.routes).map((route) => route.source);
77
+ return _chunkI42ZL2FScjs.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 = _chunkQTWJTYZWcjs.matchRoute.call(void 0, app.routes, pathname)) == null ? void 0 : _a.params) != null ? _b : null;
84
+ return (_b = (_a = _chunkI42ZL2FScjs.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 = _chunkT5QVCXVBcjs.__spreadValues.call(void 0, _chunkT5QVCXVBcjs.__spreadValues.call(void 0, {}, typeof cookie === "string" && cookie !== "" ? { cookie } : {}), fromBody);
104
- return { backendHeaders, cookies: _chunkQTWJTYZWcjs.parseCookieHeader.call(void 0, cookie), headers };
98
+ const backendHeaders = {
99
+ ...typeof cookie === "string" && cookie !== "" ? { cookie } : {},
100
+ ...fromBody
101
+ };
102
+ return { backendHeaders, cookies: _chunkI42ZL2FScjs.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
- return _chunkT5QVCXVBcjs.__async.call(void 0, this, null, function* () {
130
- if (typeof target !== "string" || !target.startsWith("/")) {
131
- 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" });
132
- }
133
- if (inputs !== void 0 && (typeof inputs !== "object" || inputs === null || Array.isArray(inputs))) {
134
- 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" });
135
- }
136
- const backendHeaders = backendHeadersOf(rawBackendHeaders);
137
- if (backendHeaders === "invalid") {
138
- 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" });
139
- }
140
- const { pathname, search } = new URL(target, "http://localhost");
141
- const params = matchClaim(app, pathname);
142
- if (params === null) {
143
- serveLog.error(`\u043F\u0443\u0442\u044C \xAB${pathname}\xBB \u0432\u043D\u0435 \u0437\u0430\u044F\u0432\u043A\u0438 \u043F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u044F`);
144
- return reply.code(404).send({ error: `\u043F\u0443\u0442\u044C \xAB${pathname}\xBB \u0432\u043D\u0435 \u0437\u0430\u044F\u0432\u043A\u0438 \u043F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u044F` });
145
- }
146
- const startedAt = Date.now();
147
- try {
148
- const result = yield _chunkQTWJTYZWcjs.renderApp.call(void 0, app, {
149
- context: requestContextOf(req, backendHeaders),
150
- inputs,
151
- params,
152
- pathname,
153
- search
154
- });
155
- const elapsed = Date.now() - startedAt;
156
- if (elapsed >= slowRenderMs) {
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${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${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 _chunkI42ZL2FScjs.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${pathname}\xBB: ${String(elapsed)}\u043C\u0441`);
163
155
  }
164
- });
156
+ return result;
157
+ } catch (error) {
158
+ serveLog.error(`\u0440\u0435\u043D\u0434\u0435\u0440 \xAB${pathname}\xBB \u0443\u043F\u0430\u043B \u0437\u0430 ${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
- return _chunkT5QVCXVBcjs.__async.call(void 0, this, null, function* () {
193
- const aggregation = _chunkHJFBEJT6cjs.clusterMetrics.call(void 0, );
194
- const logger2 = _logger.log.getLogger(appName).getLogger("cluster");
195
- let timer;
196
- let closing;
197
- const starting = start();
198
- const close = () => {
199
- closing != null ? closing : closing = (() => _chunkT5QVCXVBcjs.__async.call(void 0, null, null, function* () {
200
- clearInterval(timer);
187
+ async function startWorker(appName, start) {
188
+ const aggregation = _chunkWS6QCYBRcjs.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
- const instance = yield starting;
203
- yield instance.close();
201
+ if (process.connected) await aggregation.shutdown();
204
202
  } finally {
205
- try {
206
- if (process.connected) yield aggregation.shutdown();
207
- } finally {
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
- } catch (error) {
238
- yield close().catch(() => void 0);
239
- throw error;
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
- return { close };
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
- return _chunkT5QVCXVBcjs.__async.call(void 0, this, null, function* () {
249
- var _a;
250
- const workers = (_a = options.workers) != null ? _a : 1;
251
- if (!Number.isSafeInteger(workers) || workers < 1) {
252
- throw new Error("WORKERS must be a positive integer");
253
- }
254
- if (workers === 1) {
255
- const instance = yield start(options);
256
- return { close: () => instance.close() };
257
- }
258
- const metrics = _chunkHJFBEJT6cjs.getProcessMetrics.call(void 0, );
259
- metrics.startRuntime(appName);
260
- if (_cluster2.default.isWorker) {
261
- return startWorker(appName, () => start(_chunkT5QVCXVBcjs.__spreadProps.call(void 0, _chunkT5QVCXVBcjs.__spreadValues.call(void 0, {}, options), { gracefulShutdown: false, ops: false })));
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 = _chunkWS6QCYBRcjs.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
- return _chunkT5QVCXVBcjs.__async.call(void 0, this, null, function* () {
268
- const aggregation = _chunkHJFBEJT6cjs.clusterMetrics.call(void 0, );
269
- const logger2 = _logger.log.getLogger(appName).getLogger("cluster");
270
- const children = /* @__PURE__ */ new Set();
271
- const alive = /* @__PURE__ */ new Map();
272
- let ops;
273
- let closing;
274
- let started = false;
275
- let resolveReady = () => void 0;
276
- let rejectReady = () => void 0;
277
- const ready = new Promise((resolve, reject) => {
278
- resolveReady = resolve;
279
- rejectReady = reject;
258
+ async function startPrimary(appName, options, workers) {
259
+ const aggregation = _chunkWS6QCYBRcjs.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 _chunkWS6QCYBRcjs.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
- void ready.catch(() => void 0);
282
- const timeout = setTimeout(() => rejectReady(new Error("Den workers startup timeout")), STARTUP_MS);
283
- const onMessage = (worker, message) => {
284
- if (children.has(worker) && typeof message === "object" && message !== null && "type" in message && message.type === ALIVE) {
285
- alive.set(worker.id, Date.now());
286
- if (alive.size === workers) resolveReady();
287
- }
288
- };
289
- const close = () => {
290
- closing != null ? closing : closing = (() => _chunkT5QVCXVBcjs.__async.call(void 0, null, null, function* () {
291
- clearTimeout(timeout);
292
- resolveReady();
293
- _cluster2.default.removeListener("message", onMessage);
294
- yield Promise.all([...children].map(stopWorker));
295
- if (ops) yield _chunkHJFBEJT6cjs.closeHttpServer.call(void 0, ops);
296
- yield aggregation.shutdown();
297
- for (const signal of SIGNALS) process.removeListener(signal, shutdown);
298
- }))();
299
- return closing;
300
- };
301
- const shutdown = () => {
302
- void close().then(() => process.exit()).catch((error) => {
303
- 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);
304
- process.exit(1);
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 _chunkWS6QCYBRcjs.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
- try {
322
- if (options.ops !== false) {
323
- const supplied = options.ops;
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 _chunkWS6QCYBRcjs.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
- function stopWorker(worker) {
362
- return _chunkT5QVCXVBcjs.__async.call(void 0, this, null, function* () {
363
- if (worker.isDead()) return;
364
- yield new Promise((resolve) => {
365
- const timeout = setTimeout(() => worker.kill("SIGKILL"), SHUTDOWN_MS);
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 ${String(worker.process.pid)} exited (${String(code != null ? code : signal)})`));
369
340
  });
370
- worker.kill("SIGTERM");
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: ${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(_0) {
381
- return _chunkT5QVCXVBcjs.__async.call(void 0, this, arguments, function* (app, options = {}) {
382
- var _a, _b, _c, _d, _e;
383
- const appLog = (_a = _logger.log.getLogger(app.name)) != null ? _a : _chunkA6ZPAM6Zcjs.logger;
384
- const serveLog = (_b = appLog.getLogger("serve")) != null ? _b : appLog;
385
- const instance = _fastify2.default.call(void 0, { logger: false });
386
- const metrics = _chunkHJFBEJT6cjs.getProcessMetrics.call(void 0, );
387
- metrics.startRuntime(app.name);
388
- let ops;
389
- const shutdown = () => {
390
- void instance.close();
391
- };
392
- instance.addHook("onRequest", (req, reply, done) => {
393
- if (_chunkHJFBEJT6cjs.isOpsPath.call(void 0, req.url)) {
394
- void reply.code(404).send();
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 = _chunkWS6QCYBRcjs.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 (_chunkWS6QCYBRcjs.isOpsPath.call(void 0, req.url)) {
382
+ void reply.code(404).send();
383
+ return;
414
384
  }
415
- instance.addHook("onRequest", (req, _reply, done) => {
416
- serveLog.debug(`\u2192 ${req.method} ${req.url}`);
417
- done();
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
- try {
425
- yield (_c = options.configure) == null ? void 0 : _c.call(options, instance);
426
- if (options.ops !== false) {
427
- ops = yield _chunkHJFBEJT6cjs.startOpsServer.call(void 0, options.ops);
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 (options.gracefulShutdown !== false) {
435
- for (const signal of SHUTDOWN_SIGNALS) {
436
- process.once(signal, shutdown);
437
- }
395
+ if (ops) {
396
+ await _chunkWS6QCYBRcjs.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 ${req.method} ${req.url}`);
405
+ done();
406
+ });
407
+ instance.addHook("onResponse", (req, reply, done) => {
408
+ const elapsed = reply.elapsedTime.toFixed(1);
409
+ serveLog.info(`\u2190 ${String(reply.statusCode)} ${req.method} ${req.url} ${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 _chunkWS6QCYBRcjs.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${app.name}\xBB \u0441\u043B\u0443\u0448\u0430\u0435\u0442 ${instance.addresses().map((a) => String(a.port)).join(", ")}`
429
+ );
430
+ return instance;
444
431
  }
445
432
 
446
433