@lark-apaas/devtool-kits 1.0.5-alpha.4 → 1.0.5-alpha.6

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/index.js CHANGED
@@ -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
- return match[1];
28470
+ const content = match[1].trim();
28471
+ return content || null;
28473
28472
  }
28474
- return trimmed;
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.access(logFilePath);
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 allLines = [];
28488
- const stream = createReadStream(logFilePath, {
28489
- encoding: "utf8"
28490
- });
28491
- const rl = createInterface({
28492
- input: stream,
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
- for await (const line of rl) {
28497
- const parsed = parseLogLine(line);
28498
- if (parsed !== null) {
28499
- allLines.push(parsed);
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
- rl.close();
28504
- stream.destroy();
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
- if (allLines[i].includes("dev:server")) {
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: false
28532
+ logs: fallbackLogs,
28533
+ hasCompileError: hasCompileError2
28517
28534
  };
28518
28535
  }
28519
28536
  let endIndex = allLines.length;
28520
- let hasCompileError = false;
28521
- for (let i = startIndex; i < allLines.length; i++) {
28522
- if (/Found \d+ errors?\./.test(allLines[i])) {
28523
- endIndex = i + 1;
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,6 +28550,20 @@ async function readRecentErrorLogs(logDir, maxLogs, fileName) {
28533
28550
  };
28534
28551
  }
28535
28552
  __name(readRecentErrorLogs, "readRecentErrorLogs");
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");
28536
28567
  function injectErrorData(template, errorLogs, clientBasePath) {
28537
28568
  let logsText = "";
28538
28569
  if (errorLogs.length > 0) {
@@ -28545,9 +28576,9 @@ function injectErrorData(template, errorLogs, 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, clientBasePath = process.env.CLIENT_BASE_PATH || "/" } = options || {};
28579
+ const { logDir = path2.join(process.cwd(), "logs"), maxErrorLogs = 100, logFileName = "server.log", retryTimeout = 5e3, retryInterval = 500, target = `http://localhost:${process.env.SERVER_PORT || 3e3}`, clientBasePath = process.env.CLIENT_BASE_PATH || "/" } = options || {};
28549
28580
  const clientBasePathWithoutSlash = normalizeBasePath(clientBasePath);
28550
- console.error("[Proxy Error]:", err.message);
28581
+ console.error("[Proxy Error]:", err.message, clientBasePathWithoutSlash);
28551
28582
  if (res.headersSent) {
28552
28583
  console.error("[Proxy Error]: Headers already sent, cannot send error page");
28553
28584
  return;
@@ -28556,7 +28587,7 @@ function handleDevProxyError(err, req, res, options) {
28556
28587
  try {
28557
28588
  const isConnError = isConnectionError(err);
28558
28589
  const { logs: errorLogs, hasCompileError } = await readRecentErrorLogs(logDir, maxErrorLogs, logFileName);
28559
- if (isConnError && !hasCompileError && target) {
28590
+ if (isConnError && !hasCompileError) {
28560
28591
  console.log("[Proxy Error]: Connection error without compile errors, possibly server restarting...");
28561
28592
  try {
28562
28593
  new URL(target);
@@ -29124,18 +29155,18 @@ __name(serializeError, "serializeError");
29124
29155
  import { join as join2 } from "path";
29125
29156
 
29126
29157
  // src/middlewares/dev-logs/services.ts
29127
- import { createReadStream as createReadStream2, promises as fs7 } from "fs";
29128
- import { createInterface as createInterface2 } from "readline";
29158
+ import { createReadStream, promises as fs7 } from "fs";
29159
+ import { createInterface } from "readline";
29129
29160
  async function readLogEntriesByTrace(filePath, traceId, limit) {
29130
29161
  const exists = await fileExists(filePath);
29131
29162
  if (!exists) {
29132
29163
  return void 0;
29133
29164
  }
29134
29165
  const matches = [];
29135
- const stream = createReadStream2(filePath, {
29166
+ const stream = createReadStream(filePath, {
29136
29167
  encoding: "utf8"
29137
29168
  });
29138
- const rl = createInterface2({
29169
+ const rl = createInterface({
29139
29170
  input: stream,
29140
29171
  crlfDelay: Infinity
29141
29172
  });
@@ -29286,10 +29317,10 @@ async function readLogFilePage(filePath, page, pageSize) {
29286
29317
  const capacity = page * pageSize;
29287
29318
  const buffer = [];
29288
29319
  let totalLines = 0;
29289
- const stream = createReadStream2(filePath, {
29320
+ const stream = createReadStream(filePath, {
29290
29321
  encoding: "utf8"
29291
29322
  });
29292
- const rl = createInterface2({
29323
+ const rl = createInterface({
29293
29324
  input: stream,
29294
29325
  crlfDelay: Infinity
29295
29326
  });