@lark-apaas/devtool-kits 1.0.5-alpha.3 → 1.0.5-alpha.5
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/dist/error.html +34 -44
- package/dist/index.cjs +205 -72
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +34 -1
- package/dist/index.d.ts +34 -1
- package/dist/index.js +192 -60
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -30,6 +30,8 @@ interface ProxyErrorOptions {
|
|
|
30
30
|
retryInterval?: number;
|
|
31
31
|
/** 目标服务器地址,用于检查服务是否恢复,格式:http://localhost:3000 */
|
|
32
32
|
target?: string;
|
|
33
|
+
/** 客户端基础路径,默认 '/' */
|
|
34
|
+
clientBasePath?: string;
|
|
33
35
|
}
|
|
34
36
|
/**
|
|
35
37
|
* HTTP Proxy 错误处理器
|
|
@@ -197,6 +199,37 @@ interface DevLogsMiddlewareOptions {
|
|
|
197
199
|
*/
|
|
198
200
|
declare function createCollectLogsMiddleware(options?: DevLogsMiddlewareOptions): RouteMiddleware;
|
|
199
201
|
|
|
202
|
+
interface HealthCheckMiddlewareOptions {
|
|
203
|
+
/** 目标服务端口,默认 3000 */
|
|
204
|
+
targetPort?: number;
|
|
205
|
+
/** 目标服务主机,默认 localhost */
|
|
206
|
+
targetHost?: string;
|
|
207
|
+
/** 健康检查超时时间(毫秒),默认 2000ms */
|
|
208
|
+
timeout?: number;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Creates health check middleware for checking service availability
|
|
212
|
+
* Supports both rspack/webpack and Vite dev servers
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```typescript
|
|
216
|
+
* // In rspack/webpack setupMiddlewares
|
|
217
|
+
* registerMiddlewares(devServer.app, [
|
|
218
|
+
* createHealthCheckMiddleware({ targetPort: 3000 })
|
|
219
|
+
* ]);
|
|
220
|
+
*
|
|
221
|
+
* // Check health at: GET /dev/health
|
|
222
|
+
* // Response:
|
|
223
|
+
* // {
|
|
224
|
+
* // "status": "healthy",
|
|
225
|
+
* // "service": "localhost:3000",
|
|
226
|
+
* // "responseTime": 15,
|
|
227
|
+
* // "timestamp": "2025-01-10T12:00:00.000Z"
|
|
228
|
+
* // }
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
declare function createHealthCheckMiddleware(options?: HealthCheckMiddlewareOptions): RouteMiddleware;
|
|
232
|
+
|
|
200
233
|
/**
|
|
201
234
|
* Register middlewares for Express-compatible servers or Vite
|
|
202
235
|
* @param server - Express app or Vite middleware instance
|
|
@@ -236,4 +269,4 @@ declare function createCollectLogsMiddleware(options?: DevLogsMiddlewareOptions)
|
|
|
236
269
|
*/
|
|
237
270
|
declare function registerMiddlewares(server: ExpressApp | ViteMiddleware, middlewares: Middleware[], options?: Partial<MiddlewareContext>): Promise<void>;
|
|
238
271
|
|
|
239
|
-
export { type GlobalMiddleware, type Middleware, type MiddlewareContext, type RouteInfo, type RouteMiddleware, createCollectLogsMiddleware, createDevLogsMiddleware, createOpenapiMiddleware, handleDevProxyError, normalizeBasePath, postprocessDrizzleSchema, registerMiddlewares };
|
|
272
|
+
export { type GlobalMiddleware, type Middleware, type MiddlewareContext, type RouteInfo, type RouteMiddleware, createCollectLogsMiddleware, createDevLogsMiddleware, createHealthCheckMiddleware, createOpenapiMiddleware, handleDevProxyError, normalizeBasePath, postprocessDrizzleSchema, registerMiddlewares };
|
package/dist/index.d.ts
CHANGED
|
@@ -30,6 +30,8 @@ interface ProxyErrorOptions {
|
|
|
30
30
|
retryInterval?: number;
|
|
31
31
|
/** 目标服务器地址,用于检查服务是否恢复,格式:http://localhost:3000 */
|
|
32
32
|
target?: string;
|
|
33
|
+
/** 客户端基础路径,默认 '/' */
|
|
34
|
+
clientBasePath?: string;
|
|
33
35
|
}
|
|
34
36
|
/**
|
|
35
37
|
* HTTP Proxy 错误处理器
|
|
@@ -197,6 +199,37 @@ interface DevLogsMiddlewareOptions {
|
|
|
197
199
|
*/
|
|
198
200
|
declare function createCollectLogsMiddleware(options?: DevLogsMiddlewareOptions): RouteMiddleware;
|
|
199
201
|
|
|
202
|
+
interface HealthCheckMiddlewareOptions {
|
|
203
|
+
/** 目标服务端口,默认 3000 */
|
|
204
|
+
targetPort?: number;
|
|
205
|
+
/** 目标服务主机,默认 localhost */
|
|
206
|
+
targetHost?: string;
|
|
207
|
+
/** 健康检查超时时间(毫秒),默认 2000ms */
|
|
208
|
+
timeout?: number;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Creates health check middleware for checking service availability
|
|
212
|
+
* Supports both rspack/webpack and Vite dev servers
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```typescript
|
|
216
|
+
* // In rspack/webpack setupMiddlewares
|
|
217
|
+
* registerMiddlewares(devServer.app, [
|
|
218
|
+
* createHealthCheckMiddleware({ targetPort: 3000 })
|
|
219
|
+
* ]);
|
|
220
|
+
*
|
|
221
|
+
* // Check health at: GET /dev/health
|
|
222
|
+
* // Response:
|
|
223
|
+
* // {
|
|
224
|
+
* // "status": "healthy",
|
|
225
|
+
* // "service": "localhost:3000",
|
|
226
|
+
* // "responseTime": 15,
|
|
227
|
+
* // "timestamp": "2025-01-10T12:00:00.000Z"
|
|
228
|
+
* // }
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
declare function createHealthCheckMiddleware(options?: HealthCheckMiddlewareOptions): RouteMiddleware;
|
|
232
|
+
|
|
200
233
|
/**
|
|
201
234
|
* Register middlewares for Express-compatible servers or Vite
|
|
202
235
|
* @param server - Express app or Vite middleware instance
|
|
@@ -236,4 +269,4 @@ declare function createCollectLogsMiddleware(options?: DevLogsMiddlewareOptions)
|
|
|
236
269
|
*/
|
|
237
270
|
declare function registerMiddlewares(server: ExpressApp | ViteMiddleware, middlewares: Middleware[], options?: Partial<MiddlewareContext>): Promise<void>;
|
|
238
271
|
|
|
239
|
-
export { type GlobalMiddleware, type Middleware, type MiddlewareContext, type RouteInfo, type RouteMiddleware, createCollectLogsMiddleware, createDevLogsMiddleware, createOpenapiMiddleware, handleDevProxyError, normalizeBasePath, postprocessDrizzleSchema, registerMiddlewares };
|
|
272
|
+
export { type GlobalMiddleware, type Middleware, type MiddlewareContext, type RouteInfo, type RouteMiddleware, createCollectLogsMiddleware, createDevLogsMiddleware, createHealthCheckMiddleware, createOpenapiMiddleware, handleDevProxyError, normalizeBasePath, postprocessDrizzleSchema, registerMiddlewares };
|
package/dist/index.js
CHANGED
|
@@ -22320,10 +22320,10 @@ var require_layer = __commonJS({
|
|
|
22320
22320
|
var require_methods = __commonJS({
|
|
22321
22321
|
"../../../node_modules/methods/index.js"(exports, module) {
|
|
22322
22322
|
"use strict";
|
|
22323
|
-
var
|
|
22323
|
+
var http3 = __require("http");
|
|
22324
22324
|
module.exports = getCurrentNodeMethods() || getBasicNodeMethods();
|
|
22325
22325
|
function getCurrentNodeMethods() {
|
|
22326
|
-
return
|
|
22326
|
+
return http3.METHODS && http3.METHODS.map(/* @__PURE__ */ __name(function lowerCaseMethod(method) {
|
|
22327
22327
|
return method.toLowerCase();
|
|
22328
22328
|
}, "lowerCaseMethod"));
|
|
22329
22329
|
}
|
|
@@ -25995,13 +25995,13 @@ var require_application = __commonJS({
|
|
|
25995
25995
|
"../../../node_modules/express/lib/application.js"(exports, module) {
|
|
25996
25996
|
"use strict";
|
|
25997
25997
|
var finalhandler = require_finalhandler();
|
|
25998
|
-
var
|
|
25998
|
+
var Router2 = require_router();
|
|
25999
25999
|
var methods = require_methods();
|
|
26000
26000
|
var middleware = require_init();
|
|
26001
26001
|
var query = require_query();
|
|
26002
26002
|
var debug = require_src3()("express:application");
|
|
26003
26003
|
var View = require_view();
|
|
26004
|
-
var
|
|
26004
|
+
var http3 = __require("http");
|
|
26005
26005
|
var compileETag = require_utils2().compileETag;
|
|
26006
26006
|
var compileQueryParser = require_utils2().compileQueryParser;
|
|
26007
26007
|
var compileTrust = require_utils2().compileTrust;
|
|
@@ -26060,7 +26060,7 @@ var require_application = __commonJS({
|
|
|
26060
26060
|
}, "defaultConfiguration");
|
|
26061
26061
|
app.lazyrouter = /* @__PURE__ */ __name(function lazyrouter() {
|
|
26062
26062
|
if (!this._router) {
|
|
26063
|
-
this._router = new
|
|
26063
|
+
this._router = new Router2({
|
|
26064
26064
|
caseSensitive: this.enabled("case sensitive routing"),
|
|
26065
26065
|
strict: this.enabled("strict routing")
|
|
26066
26066
|
});
|
|
@@ -26250,7 +26250,7 @@ var require_application = __commonJS({
|
|
|
26250
26250
|
tryRender(view, renderOptions, done);
|
|
26251
26251
|
}, "render");
|
|
26252
26252
|
app.listen = /* @__PURE__ */ __name(function listen() {
|
|
26253
|
-
var server =
|
|
26253
|
+
var server = http3.createServer(this);
|
|
26254
26254
|
return server.listen.apply(server, arguments);
|
|
26255
26255
|
}, "listen");
|
|
26256
26256
|
function logerror(err) {
|
|
@@ -26913,12 +26913,12 @@ var require_request = __commonJS({
|
|
|
26913
26913
|
var deprecate = require_depd()("express");
|
|
26914
26914
|
var isIP = __require("net").isIP;
|
|
26915
26915
|
var typeis = require_type_is();
|
|
26916
|
-
var
|
|
26916
|
+
var http3 = __require("http");
|
|
26917
26917
|
var fresh = require_fresh();
|
|
26918
26918
|
var parseRange = require_range_parser();
|
|
26919
26919
|
var parse = require_parseurl();
|
|
26920
26920
|
var proxyaddr = require_proxy_addr();
|
|
26921
|
-
var req = Object.create(
|
|
26921
|
+
var req = Object.create(http3.IncomingMessage.prototype);
|
|
26922
26922
|
module.exports = req;
|
|
26923
26923
|
req.get = req.header = /* @__PURE__ */ __name(function header(name) {
|
|
26924
26924
|
if (!name) {
|
|
@@ -27344,7 +27344,7 @@ var require_response = __commonJS({
|
|
|
27344
27344
|
var deprecate = require_depd()("express");
|
|
27345
27345
|
var encodeUrl = require_encodeurl();
|
|
27346
27346
|
var escapeHtml = require_escape_html();
|
|
27347
|
-
var
|
|
27347
|
+
var http3 = __require("http");
|
|
27348
27348
|
var isAbsolute3 = require_utils2().isAbsolute;
|
|
27349
27349
|
var onFinished = require_on_finished();
|
|
27350
27350
|
var path6 = __require("path");
|
|
@@ -27360,7 +27360,7 @@ var require_response = __commonJS({
|
|
|
27360
27360
|
var mime = send.mime;
|
|
27361
27361
|
var resolve = path6.resolve;
|
|
27362
27362
|
var vary = require_vary();
|
|
27363
|
-
var res = Object.create(
|
|
27363
|
+
var res = Object.create(http3.ServerResponse.prototype);
|
|
27364
27364
|
module.exports = res;
|
|
27365
27365
|
var charsetRegExp = /;\s*charset\s*=/;
|
|
27366
27366
|
res.status = /* @__PURE__ */ __name(function status(code) {
|
|
@@ -28009,7 +28009,7 @@ var require_express = __commonJS({
|
|
|
28009
28009
|
var mixin = require_merge_descriptors();
|
|
28010
28010
|
var proto = require_application();
|
|
28011
28011
|
var Route = require_route();
|
|
28012
|
-
var
|
|
28012
|
+
var Router2 = require_router();
|
|
28013
28013
|
var req = require_request();
|
|
28014
28014
|
var res = require_response();
|
|
28015
28015
|
exports = module.exports = createApplication;
|
|
@@ -28043,7 +28043,7 @@ var require_express = __commonJS({
|
|
|
28043
28043
|
exports.request = req;
|
|
28044
28044
|
exports.response = res;
|
|
28045
28045
|
exports.Route = Route;
|
|
28046
|
-
exports.Router =
|
|
28046
|
+
exports.Router = Router2;
|
|
28047
28047
|
exports.json = bodyParser.json;
|
|
28048
28048
|
exports.query = require_query();
|
|
28049
28049
|
exports.raw = bodyParser.raw;
|
|
@@ -28392,8 +28392,6 @@ __name(collapseExtraBlankLines, "collapseExtraBlankLines");
|
|
|
28392
28392
|
// src/helpers/proxy-error/index.ts
|
|
28393
28393
|
import fs2 from "fs";
|
|
28394
28394
|
import path2 from "path";
|
|
28395
|
-
import { createReadStream } from "fs";
|
|
28396
|
-
import { createInterface } from "readline";
|
|
28397
28395
|
import http from "http";
|
|
28398
28396
|
import https from "https";
|
|
28399
28397
|
var errorHtmlTemplate = null;
|
|
@@ -28469,63 +28467,82 @@ function parseLogLine(line) {
|
|
|
28469
28467
|
if (!trimmed) return null;
|
|
28470
28468
|
const match = trimmed.match(/^\[\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}\]\s+\[server\]\s+(.*)$/);
|
|
28471
28469
|
if (match) {
|
|
28472
|
-
|
|
28470
|
+
const content = match[1].trim();
|
|
28471
|
+
return content || null;
|
|
28473
28472
|
}
|
|
28474
|
-
return
|
|
28473
|
+
return null;
|
|
28475
28474
|
}
|
|
28476
28475
|
__name(parseLogLine, "parseLogLine");
|
|
28477
28476
|
async function readRecentErrorLogs(logDir, maxLogs, fileName) {
|
|
28478
28477
|
const logFilePath = path2.join(logDir, fileName);
|
|
28478
|
+
let fileStats;
|
|
28479
28479
|
try {
|
|
28480
|
-
await fs2.promises.
|
|
28480
|
+
fileStats = await fs2.promises.stat(logFilePath);
|
|
28481
28481
|
} catch {
|
|
28482
28482
|
return {
|
|
28483
28483
|
logs: [],
|
|
28484
28484
|
hasCompileError: false
|
|
28485
28485
|
};
|
|
28486
28486
|
}
|
|
28487
|
-
const
|
|
28488
|
-
const
|
|
28489
|
-
|
|
28490
|
-
|
|
28491
|
-
const
|
|
28492
|
-
|
|
28493
|
-
crlfDelay: Infinity
|
|
28494
|
-
});
|
|
28487
|
+
const fileSize = fileStats.size;
|
|
28488
|
+
const maxReadSize = 1024 * 1024;
|
|
28489
|
+
const readSize = Math.min(fileSize, maxReadSize);
|
|
28490
|
+
const startPosition = Math.max(0, fileSize - readSize);
|
|
28491
|
+
const buffer = Buffer.allocUnsafe(readSize);
|
|
28492
|
+
let fileHandle;
|
|
28495
28493
|
try {
|
|
28496
|
-
|
|
28497
|
-
|
|
28498
|
-
|
|
28499
|
-
|
|
28500
|
-
|
|
28501
|
-
|
|
28494
|
+
fileHandle = await fs2.promises.open(logFilePath, "r");
|
|
28495
|
+
await fileHandle.read(buffer, 0, readSize, startPosition);
|
|
28496
|
+
} catch (error) {
|
|
28497
|
+
console.error("[Proxy Error]: Failed to read log file:", error);
|
|
28498
|
+
return {
|
|
28499
|
+
logs: [],
|
|
28500
|
+
hasCompileError: false
|
|
28501
|
+
};
|
|
28502
28502
|
} finally {
|
|
28503
|
-
|
|
28504
|
-
|
|
28503
|
+
if (fileHandle) {
|
|
28504
|
+
await fileHandle.close();
|
|
28505
|
+
}
|
|
28506
|
+
}
|
|
28507
|
+
const content = buffer.toString("utf8");
|
|
28508
|
+
const lines = content.split("\n");
|
|
28509
|
+
if (startPosition > 0 && lines.length > 0) {
|
|
28510
|
+
lines.shift();
|
|
28511
|
+
}
|
|
28512
|
+
const allLines = [];
|
|
28513
|
+
for (const line of lines) {
|
|
28514
|
+
const parsed = parseLogLine(line);
|
|
28515
|
+
if (parsed !== null) {
|
|
28516
|
+
allLines.push(parsed);
|
|
28517
|
+
}
|
|
28505
28518
|
}
|
|
28506
28519
|
let startIndex = -1;
|
|
28507
28520
|
for (let i = allLines.length - 1; i >= 0; i--) {
|
|
28508
|
-
|
|
28521
|
+
const line = allLines[i];
|
|
28522
|
+
if (line.includes("Starting compilation in watch mode") || line.includes("File change detected. Starting incremental compilation")) {
|
|
28509
28523
|
startIndex = i;
|
|
28510
28524
|
break;
|
|
28511
28525
|
}
|
|
28512
28526
|
}
|
|
28513
28527
|
if (startIndex === -1) {
|
|
28528
|
+
console.log("[Proxy Error]: No compilation start marker found, returning last logs");
|
|
28529
|
+
const fallbackLogs = allLines.slice(-maxLogs);
|
|
28530
|
+
const hasCompileError2 = checkForErrors(fallbackLogs);
|
|
28514
28531
|
return {
|
|
28515
|
-
logs:
|
|
28516
|
-
hasCompileError:
|
|
28532
|
+
logs: fallbackLogs,
|
|
28533
|
+
hasCompileError: hasCompileError2
|
|
28517
28534
|
};
|
|
28518
28535
|
}
|
|
28519
28536
|
let endIndex = allLines.length;
|
|
28520
|
-
let
|
|
28521
|
-
|
|
28522
|
-
if (
|
|
28523
|
-
endIndex = i
|
|
28524
|
-
hasCompileError = true;
|
|
28537
|
+
for (let i = startIndex + 1; i < allLines.length; i++) {
|
|
28538
|
+
const line = allLines[i];
|
|
28539
|
+
if (line.includes("Starting compilation in watch mode") || line.includes("File change detected. Starting incremental compilation")) {
|
|
28540
|
+
endIndex = i;
|
|
28525
28541
|
break;
|
|
28526
28542
|
}
|
|
28527
28543
|
}
|
|
28528
28544
|
const errorSection = allLines.slice(startIndex, endIndex);
|
|
28545
|
+
const hasCompileError = checkForErrors(errorSection);
|
|
28529
28546
|
const logs = errorSection.length > maxLogs ? errorSection.slice(-maxLogs) : errorSection;
|
|
28530
28547
|
return {
|
|
28531
28548
|
logs,
|
|
@@ -28533,7 +28550,21 @@ async function readRecentErrorLogs(logDir, maxLogs, fileName) {
|
|
|
28533
28550
|
};
|
|
28534
28551
|
}
|
|
28535
28552
|
__name(readRecentErrorLogs, "readRecentErrorLogs");
|
|
28536
|
-
function
|
|
28553
|
+
function checkForErrors(logs) {
|
|
28554
|
+
for (const line of logs) {
|
|
28555
|
+
const compileErrorMatch = line.match(/Found (\d+) errors?\. Watching for file changes/);
|
|
28556
|
+
if (compileErrorMatch) {
|
|
28557
|
+
const errorCount = parseInt(compileErrorMatch[1], 10);
|
|
28558
|
+
if (errorCount > 0) {
|
|
28559
|
+
console.log(`[Proxy Error]: Found ${errorCount} compilation error(s)`);
|
|
28560
|
+
return true;
|
|
28561
|
+
}
|
|
28562
|
+
}
|
|
28563
|
+
}
|
|
28564
|
+
return false;
|
|
28565
|
+
}
|
|
28566
|
+
__name(checkForErrors, "checkForErrors");
|
|
28567
|
+
function injectErrorData(template, errorLogs, clientBasePath) {
|
|
28537
28568
|
let logsText = "";
|
|
28538
28569
|
if (errorLogs.length > 0) {
|
|
28539
28570
|
logsText = errorLogs.join("\n");
|
|
@@ -28541,11 +28572,12 @@ function injectErrorData(template, errorLogs) {
|
|
|
28541
28572
|
logsText = "\u672A\u627E\u5230\u76F8\u5173\u9519\u8BEF\u65E5\u5FD7";
|
|
28542
28573
|
}
|
|
28543
28574
|
return template.replace("{{.errorData.message}}", `\u670D\u52A1\u542F\u52A8\u5F02\u5E38\uFF0C\u8BF7\u6839\u636E\u65E5\u5FD7\u4FEE\u590D\u76F8\u5173\u95EE\u9898
|
|
28544
|
-
${JSON.stringify(logsText)}`);
|
|
28575
|
+
${JSON.stringify(logsText)}`).replace("{{.clientBasePath}}", clientBasePath);
|
|
28545
28576
|
}
|
|
28546
28577
|
__name(injectErrorData, "injectErrorData");
|
|
28547
28578
|
function handleDevProxyError(err, req, res, options) {
|
|
28548
|
-
const { logDir = path2.join(process.cwd(), "logs"), maxErrorLogs = 100, logFileName = "server.log", retryTimeout = 5e3, retryInterval = 500, target } = options || {};
|
|
28579
|
+
const { logDir = path2.join(process.cwd(), "logs"), maxErrorLogs = 100, logFileName = "server.log", retryTimeout = 5e3, retryInterval = 500, target, clientBasePath = process.env.CLIENT_BASE_PATH || "/" } = options || {};
|
|
28580
|
+
const clientBasePathWithoutSlash = normalizeBasePath(clientBasePath);
|
|
28549
28581
|
console.error("[Proxy Error]:", err.message);
|
|
28550
28582
|
if (res.headersSent) {
|
|
28551
28583
|
console.error("[Proxy Error]: Headers already sent, cannot send error page");
|
|
@@ -28578,7 +28610,7 @@ function handleDevProxyError(err, req, res, options) {
|
|
|
28578
28610
|
console.log("[Proxy Error]: Compile error or non-connection error, showing error page");
|
|
28579
28611
|
}
|
|
28580
28612
|
const template = getErrorHtmlTemplate();
|
|
28581
|
-
const html = injectErrorData(template, errorLogs);
|
|
28613
|
+
const html = injectErrorData(template, errorLogs, clientBasePathWithoutSlash);
|
|
28582
28614
|
res.writeHead(502, {
|
|
28583
28615
|
"Content-Type": "text/html; charset=utf-8",
|
|
28584
28616
|
"Cache-Control": "no-cache, no-store, must-revalidate",
|
|
@@ -28612,8 +28644,99 @@ __name(sendSimpleRedirect, "sendSimpleRedirect");
|
|
|
28612
28644
|
// src/middlewares/index.ts
|
|
28613
28645
|
import path5 from "path";
|
|
28614
28646
|
|
|
28615
|
-
// src/middlewares/
|
|
28647
|
+
// src/middlewares/health-check/router.ts
|
|
28616
28648
|
var import_express = __toESM(require_express2(), 1);
|
|
28649
|
+
import http2 from "http";
|
|
28650
|
+
function checkServiceHealth(host, port, timeout) {
|
|
28651
|
+
return new Promise((resolve) => {
|
|
28652
|
+
const startTime = Date.now();
|
|
28653
|
+
const req = http2.request({
|
|
28654
|
+
hostname: host,
|
|
28655
|
+
port,
|
|
28656
|
+
path: "/",
|
|
28657
|
+
method: "HEAD",
|
|
28658
|
+
timeout
|
|
28659
|
+
}, (_res) => {
|
|
28660
|
+
const responseTime = Date.now() - startTime;
|
|
28661
|
+
resolve({
|
|
28662
|
+
available: true,
|
|
28663
|
+
responseTime
|
|
28664
|
+
});
|
|
28665
|
+
});
|
|
28666
|
+
req.on("timeout", () => {
|
|
28667
|
+
req.destroy();
|
|
28668
|
+
resolve({
|
|
28669
|
+
available: false,
|
|
28670
|
+
error: "Request timeout"
|
|
28671
|
+
});
|
|
28672
|
+
});
|
|
28673
|
+
req.on("error", (err) => {
|
|
28674
|
+
resolve({
|
|
28675
|
+
available: false,
|
|
28676
|
+
error: err.message
|
|
28677
|
+
});
|
|
28678
|
+
});
|
|
28679
|
+
req.end();
|
|
28680
|
+
});
|
|
28681
|
+
}
|
|
28682
|
+
__name(checkServiceHealth, "checkServiceHealth");
|
|
28683
|
+
function createHealthCheckRouter(options = {}) {
|
|
28684
|
+
const { targetPort = Number(process.env.SERVER_PORT) || 3e3, targetHost = "localhost", timeout = 2e3 } = options;
|
|
28685
|
+
const router = (0, import_express.Router)();
|
|
28686
|
+
router.get("/", async (_req, res) => {
|
|
28687
|
+
try {
|
|
28688
|
+
const result = await checkServiceHealth(targetHost, targetPort, timeout);
|
|
28689
|
+
if (result.available) {
|
|
28690
|
+
res.status(200).json({
|
|
28691
|
+
status: "healthy",
|
|
28692
|
+
service: `${targetHost}:${targetPort}`,
|
|
28693
|
+
responseTime: result.responseTime,
|
|
28694
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
28695
|
+
});
|
|
28696
|
+
} else {
|
|
28697
|
+
res.status(503).json({
|
|
28698
|
+
status: "unhealthy",
|
|
28699
|
+
service: `${targetHost}:${targetPort}`,
|
|
28700
|
+
error: result.error,
|
|
28701
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
28702
|
+
});
|
|
28703
|
+
}
|
|
28704
|
+
} catch (error) {
|
|
28705
|
+
res.status(500).json({
|
|
28706
|
+
status: "error",
|
|
28707
|
+
service: `${targetHost}:${targetPort}`,
|
|
28708
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
28709
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
28710
|
+
});
|
|
28711
|
+
}
|
|
28712
|
+
});
|
|
28713
|
+
return router;
|
|
28714
|
+
}
|
|
28715
|
+
__name(createHealthCheckRouter, "createHealthCheckRouter");
|
|
28716
|
+
|
|
28717
|
+
// src/middlewares/health-check/index.ts
|
|
28718
|
+
var HEALTH_CHECK_ROUTES = [
|
|
28719
|
+
{
|
|
28720
|
+
method: "GET",
|
|
28721
|
+
path: "/",
|
|
28722
|
+
description: "Check if target service is healthy and available"
|
|
28723
|
+
}
|
|
28724
|
+
];
|
|
28725
|
+
function createHealthCheckMiddleware(options = {}) {
|
|
28726
|
+
return {
|
|
28727
|
+
name: "health-check",
|
|
28728
|
+
mountPath: "/dev/health",
|
|
28729
|
+
routes: HEALTH_CHECK_ROUTES,
|
|
28730
|
+
enabled: /* @__PURE__ */ __name((context) => context.isDev, "enabled"),
|
|
28731
|
+
createRouter: /* @__PURE__ */ __name((_context) => {
|
|
28732
|
+
return createHealthCheckRouter(options);
|
|
28733
|
+
}, "createRouter")
|
|
28734
|
+
};
|
|
28735
|
+
}
|
|
28736
|
+
__name(createHealthCheckMiddleware, "createHealthCheckMiddleware");
|
|
28737
|
+
|
|
28738
|
+
// src/middlewares/openapi/router.ts
|
|
28739
|
+
var import_express2 = __toESM(require_express2(), 1);
|
|
28617
28740
|
|
|
28618
28741
|
// src/middlewares/openapi/controller.ts
|
|
28619
28742
|
import fs5 from "fs/promises";
|
|
@@ -28860,7 +28983,7 @@ __name(createOpenapiHandler, "createOpenapiHandler");
|
|
|
28860
28983
|
// src/middlewares/openapi/router.ts
|
|
28861
28984
|
function createOpenapiRouter(options, context) {
|
|
28862
28985
|
const { openapiFilePath, enableEnhancement, serverDir } = options;
|
|
28863
|
-
const router =
|
|
28986
|
+
const router = import_express2.default.Router();
|
|
28864
28987
|
const handler = createOpenapiHandler(openapiFilePath, enableEnhancement, serverDir);
|
|
28865
28988
|
router.get("/openapi.json", (req, res) => handler(req, res, context));
|
|
28866
28989
|
return router;
|
|
@@ -28894,7 +29017,7 @@ function createOpenapiMiddleware(options) {
|
|
|
28894
29017
|
__name(createOpenapiMiddleware, "createOpenapiMiddleware");
|
|
28895
29018
|
|
|
28896
29019
|
// src/middlewares/dev-logs/router.ts
|
|
28897
|
-
var
|
|
29020
|
+
var import_express3 = __toESM(require_express2(), 1);
|
|
28898
29021
|
|
|
28899
29022
|
// src/middlewares/dev-logs/utils.ts
|
|
28900
29023
|
import { promises as fs6 } from "fs";
|
|
@@ -29032,18 +29155,18 @@ __name(serializeError, "serializeError");
|
|
|
29032
29155
|
import { join as join2 } from "path";
|
|
29033
29156
|
|
|
29034
29157
|
// src/middlewares/dev-logs/services.ts
|
|
29035
|
-
import { createReadStream
|
|
29036
|
-
import { createInterface
|
|
29158
|
+
import { createReadStream, promises as fs7 } from "fs";
|
|
29159
|
+
import { createInterface } from "readline";
|
|
29037
29160
|
async function readLogEntriesByTrace(filePath, traceId, limit) {
|
|
29038
29161
|
const exists = await fileExists(filePath);
|
|
29039
29162
|
if (!exists) {
|
|
29040
29163
|
return void 0;
|
|
29041
29164
|
}
|
|
29042
29165
|
const matches = [];
|
|
29043
|
-
const stream =
|
|
29166
|
+
const stream = createReadStream(filePath, {
|
|
29044
29167
|
encoding: "utf8"
|
|
29045
29168
|
});
|
|
29046
|
-
const rl =
|
|
29169
|
+
const rl = createInterface({
|
|
29047
29170
|
input: stream,
|
|
29048
29171
|
crlfDelay: Infinity
|
|
29049
29172
|
});
|
|
@@ -29194,10 +29317,10 @@ async function readLogFilePage(filePath, page, pageSize) {
|
|
|
29194
29317
|
const capacity = page * pageSize;
|
|
29195
29318
|
const buffer = [];
|
|
29196
29319
|
let totalLines = 0;
|
|
29197
|
-
const stream =
|
|
29320
|
+
const stream = createReadStream(filePath, {
|
|
29198
29321
|
encoding: "utf8"
|
|
29199
29322
|
});
|
|
29200
|
-
const rl =
|
|
29323
|
+
const rl = createInterface({
|
|
29201
29324
|
input: stream,
|
|
29202
29325
|
crlfDelay: Infinity
|
|
29203
29326
|
});
|
|
@@ -29339,7 +29462,7 @@ __name(createGetLogFileHandler, "createGetLogFileHandler");
|
|
|
29339
29462
|
// src/middlewares/dev-logs/router.ts
|
|
29340
29463
|
function createDevLogRouter(options = {}) {
|
|
29341
29464
|
const logDir = resolveLogDir(options.logDir);
|
|
29342
|
-
const router =
|
|
29465
|
+
const router = import_express3.default.Router();
|
|
29343
29466
|
router.get("/app/trace/:traceId", createGetTraceEntriesHandler(logDir));
|
|
29344
29467
|
router.get("/trace/recent", createGetRecentTracesHandler(logDir));
|
|
29345
29468
|
router.get("/files/:fileName", createGetLogFileHandler(logDir));
|
|
@@ -29382,7 +29505,7 @@ function createDevLogsMiddleware(options = {}) {
|
|
|
29382
29505
|
__name(createDevLogsMiddleware, "createDevLogsMiddleware");
|
|
29383
29506
|
|
|
29384
29507
|
// src/middlewares/collect-logs/router.ts
|
|
29385
|
-
var
|
|
29508
|
+
var import_express4 = __toESM(require_express2(), 1);
|
|
29386
29509
|
|
|
29387
29510
|
// src/middlewares/collect-logs/controller.ts
|
|
29388
29511
|
import { join as join4 } from "path";
|
|
@@ -29481,9 +29604,9 @@ __name(handleError2, "handleError");
|
|
|
29481
29604
|
// src/middlewares/collect-logs/router.ts
|
|
29482
29605
|
function createDevLogRouter2(options = {}) {
|
|
29483
29606
|
const logDir = resolveLogDir2(options.logDir);
|
|
29484
|
-
const router =
|
|
29485
|
-
router.post("/collect",
|
|
29486
|
-
router.post("/collect-batch",
|
|
29607
|
+
const router = import_express4.default.Router();
|
|
29608
|
+
router.post("/collect", import_express4.default.json(), collectLogsHandler(logDir, options.fileName || "client.log"));
|
|
29609
|
+
router.post("/collect-batch", import_express4.default.json(), collectLogsBatchHandler(logDir, options.fileName || "client.log"));
|
|
29487
29610
|
return router;
|
|
29488
29611
|
}
|
|
29489
29612
|
__name(createDevLogRouter2, "createDevLogRouter");
|
|
@@ -29566,7 +29689,15 @@ async function registerMiddlewares(server, middlewares, options) {
|
|
|
29566
29689
|
rootDir: process.cwd(),
|
|
29567
29690
|
...options
|
|
29568
29691
|
};
|
|
29569
|
-
|
|
29692
|
+
const defaultMiddlewares = [];
|
|
29693
|
+
if (context.isDev) {
|
|
29694
|
+
defaultMiddlewares.push(createHealthCheckMiddleware());
|
|
29695
|
+
}
|
|
29696
|
+
const allMiddlewares = [
|
|
29697
|
+
...defaultMiddlewares,
|
|
29698
|
+
...middlewares
|
|
29699
|
+
];
|
|
29700
|
+
for (const middleware of allMiddlewares) {
|
|
29570
29701
|
if (middleware.enabled && !middleware.enabled(context)) {
|
|
29571
29702
|
continue;
|
|
29572
29703
|
}
|
|
@@ -29592,6 +29723,7 @@ __name(registerMiddlewares, "registerMiddlewares");
|
|
|
29592
29723
|
export {
|
|
29593
29724
|
createCollectLogsMiddleware,
|
|
29594
29725
|
createDevLogsMiddleware,
|
|
29726
|
+
createHealthCheckMiddleware,
|
|
29595
29727
|
createOpenapiMiddleware,
|
|
29596
29728
|
handleDevProxyError,
|
|
29597
29729
|
normalizeBasePath,
|