befly 3.11.1 → 3.12.2

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/befly.js CHANGED
@@ -232,44 +232,6 @@ import { isAbsolute as nodePathIsAbsolute, join as nodePathJoin, resolve as node
232
232
  function escapeRegExp(input) {
233
233
  return String(input).replace(REGEXP_SPECIAL, "\\$&");
234
234
  }
235
- function installGracefulExitHooks() {
236
- if (didInstallGracefulExitHooks)
237
- return;
238
- didInstallGracefulExitHooks = true;
239
- const install = (signal, exitCode) => {
240
- try {
241
- if (typeof process.listenerCount === "function" && process.listenerCount(signal) > 0) {
242
- return;
243
- }
244
- } catch {}
245
- try {
246
- process.once(signal, () => {
247
- let exited = false;
248
- const timer = setTimeout(() => {
249
- if (exited)
250
- return;
251
- exited = true;
252
- try {
253
- process.exit(exitCode);
254
- } catch {}
255
- }, 2000);
256
- shutdown().catch(() => {
257
- return;
258
- }).finally(() => {
259
- if (exited)
260
- return;
261
- exited = true;
262
- clearTimeout(timer);
263
- try {
264
- process.exit(exitCode);
265
- } catch {}
266
- });
267
- });
268
- } catch {}
269
- };
270
- install("SIGINT", 130);
271
- install("SIGTERM", 143);
272
- }
273
235
  function formatLocalDate(date) {
274
236
  const y = date.getFullYear();
275
237
  const m = date.getMonth() + 1;
@@ -598,6 +560,7 @@ async function shutdown() {
598
560
  appConsoleSink = null;
599
561
  instance = null;
600
562
  errorInstance = null;
563
+ didEnsureLogDir = false;
601
564
  }
602
565
  function normalizePositiveInt(value, fallback, min, max) {
603
566
  if (typeof value !== "number")
@@ -619,9 +582,6 @@ function resolveLogDir() {
619
582
  return nodePathResolve(INITIAL_CWD, rawDir);
620
583
  }
621
584
  function ensureLogDirExists() {
622
- if (didEnsureLogDir)
623
- return;
624
- didEnsureLogDir = true;
625
585
  const dir = resolveLogDir();
626
586
  try {
627
587
  if (!existsSync2(dir)) {
@@ -630,6 +590,7 @@ function ensureLogDirExists() {
630
590
  } catch (error) {
631
591
  throw new Error(`\u521B\u5EFA logs \u76EE\u5F55\u5931\u8D25: ${dir}. ${error?.message || error}`);
632
592
  }
593
+ didEnsureLogDir = true;
633
594
  }
634
595
  async function pruneOldLogFiles() {
635
596
  if (didPruneOldLogFiles)
@@ -726,7 +687,6 @@ function getLogger() {
726
687
  if (instance)
727
688
  return instance;
728
689
  ensureLogDirExists();
729
- installGracefulExitHooks();
730
690
  pruneOldLogFiles();
731
691
  const minLevel = normalizeLogLevelName();
732
692
  const maxFileBytes = (typeof config.maxSize === "number" && config.maxSize > 0 ? config.maxSize : 10) * 1024 * 1024;
@@ -745,7 +705,6 @@ function getErrorLogger() {
745
705
  if (errorInstance)
746
706
  return errorInstance;
747
707
  ensureLogDirExists();
748
- installGracefulExitHooks();
749
708
  pruneOldLogFiles();
750
709
  const maxFileBytes = (typeof config.maxSize === "number" && config.maxSize > 0 ? config.maxSize : 10) * 1024 * 1024;
751
710
  if (!errorFileSink) {
@@ -825,7 +784,7 @@ function buildLogLine(level, args) {
825
784
  `;
826
785
  }
827
786
  if (first instanceof Error) {
828
- base.err = sanitizeErrorValue(first, {});
787
+ base.err = sanitizeErrorValue(first);
829
788
  if (typeof second === "string") {
830
789
  const extraMsg = formatExtrasToMsg(args.slice(2));
831
790
  base.msg = extraMsg ? `${second} ${extraMsg}` : second;
@@ -882,7 +841,7 @@ function createSinkLogger(options) {
882
841
  }
883
842
  };
884
843
  }
885
- function truncateString(val, stats) {
844
+ function truncateString(val) {
886
845
  if (val.length <= maxLogStringLen)
887
846
  return val;
888
847
  return val.slice(0, maxLogStringLen);
@@ -900,7 +859,7 @@ function isSensitiveKey(key) {
900
859
  }
901
860
  return false;
902
861
  }
903
- function safeToStringMasked(val, visited, stats) {
862
+ function safeToStringMasked(val, visited) {
904
863
  if (typeof val === "string")
905
864
  return val;
906
865
  if (val instanceof Error) {
@@ -946,25 +905,25 @@ function safeToStringMasked(val, visited, stats) {
946
905
  }
947
906
  }
948
907
  }
949
- function sanitizeErrorValue(err, stats) {
908
+ function sanitizeErrorValue(err) {
950
909
  const errObj = {
951
910
  name: err.name || "Error",
952
- message: truncateString(err.message || "", stats)
911
+ message: truncateString(err.message || "")
953
912
  };
954
913
  if (typeof err.stack === "string") {
955
- errObj.stack = truncateString(err.stack, stats);
914
+ errObj.stack = truncateString(err.stack);
956
915
  }
957
916
  return errObj;
958
917
  }
959
- function stringifyPreview(val, visited, stats) {
960
- const str = safeToStringMasked(val, visited, stats);
961
- return truncateString(str, stats);
918
+ function stringifyPreview(val, visited) {
919
+ const str = safeToStringMasked(val, visited);
920
+ return truncateString(str);
962
921
  }
963
- function sanitizeValueLimited(val, visited, stats) {
922
+ function sanitizeValueLimited(val, visited) {
964
923
  if (val === null || val === undefined)
965
924
  return val;
966
925
  if (typeof val === "string")
967
- return truncateString(val, stats);
926
+ return truncateString(val);
968
927
  if (typeof val === "number")
969
928
  return val;
970
929
  if (typeof val === "boolean")
@@ -972,12 +931,12 @@ function sanitizeValueLimited(val, visited, stats) {
972
931
  if (typeof val === "bigint")
973
932
  return val;
974
933
  if (val instanceof Error) {
975
- return sanitizeErrorValue(val, stats);
934
+ return sanitizeErrorValue(val);
976
935
  }
977
936
  const isArr = Array.isArray(val);
978
937
  const isObj = isPlainObject(val);
979
938
  if (!isArr && !isObj) {
980
- return stringifyPreview(val, visited, stats);
939
+ return stringifyPreview(val, visited);
981
940
  }
982
941
  if (visited.has(val)) {
983
942
  return "[Circular]";
@@ -992,7 +951,7 @@ function sanitizeValueLimited(val, visited, stats) {
992
951
  return;
993
952
  }
994
953
  if (typeof child === "string") {
995
- dst[key] = truncateString(child, stats);
954
+ dst[key] = truncateString(child);
996
955
  return;
997
956
  }
998
957
  if (typeof child === "number" || typeof child === "boolean" || typeof child === "bigint") {
@@ -1000,21 +959,21 @@ function sanitizeValueLimited(val, visited, stats) {
1000
959
  return;
1001
960
  }
1002
961
  if (child instanceof Error) {
1003
- dst[key] = sanitizeErrorValue(child, stats);
962
+ dst[key] = sanitizeErrorValue(child);
1004
963
  return;
1005
964
  }
1006
965
  const childIsArr = Array.isArray(child);
1007
966
  const childIsObj = isPlainObject(child);
1008
967
  if (!childIsArr && !childIsObj) {
1009
- dst[key] = stringifyPreview(child, visited, stats);
968
+ dst[key] = stringifyPreview(child, visited);
1010
969
  return;
1011
970
  }
1012
971
  if (depth >= sanitizeDepthLimit) {
1013
- dst[key] = stringifyPreview(child, visited, stats);
972
+ dst[key] = stringifyPreview(child, visited);
1014
973
  return;
1015
974
  }
1016
975
  if (nodes >= sanitizeNodesLimit) {
1017
- dst[key] = stringifyPreview(child, visited, stats);
976
+ dst[key] = stringifyPreview(child, visited);
1018
977
  return;
1019
978
  }
1020
979
  if (visited.has(child)) {
@@ -1062,7 +1021,7 @@ function sanitizeValueLimited(val, visited, stats) {
1062
1021
  return rootOut;
1063
1022
  }
1064
1023
  function sanitizeTopValue(val, visited) {
1065
- return sanitizeValueLimited(val, visited, {});
1024
+ return sanitizeValueLimited(val, visited);
1066
1025
  }
1067
1026
  function sanitizeLogObject(obj) {
1068
1027
  const visited = new WeakSet;
@@ -1144,7 +1103,7 @@ function withRequestMeta(args) {
1144
1103
  function withRequestMetaTyped(args) {
1145
1104
  return withRequestMeta(args);
1146
1105
  }
1147
- var REGEXP_SPECIAL, INITIAL_CWD, DEFAULT_LOG_STRING_LEN = 100, DEFAULT_LOG_ARRAY_ITEMS = 100, maxLogStringLen, maxLogArrayItems, DEFAULT_LOG_SANITIZE_DEPTH = 3, DEFAULT_LOG_OBJECT_KEYS = 100, DEFAULT_LOG_SANITIZE_NODES = 500, sanitizeDepthLimit, sanitizeObjectKeysLimit, sanitizeNodesLimit, ONE_YEAR_MS, BUILTIN_SENSITIVE_KEYS, sensitiveKeySet, sensitiveContainsMatchers, sensitiveContainsRegex = null, HOSTNAME, LOG_LEVEL_NUM, DEFAULT_MAX_BUFFER_BYTES, DEFAULT_FLUSH_DELAY_MS = 10, DEFAULT_MAX_BATCH_BYTES, instance = null, errorInstance = null, mockInstance = null, appFileSink = null, errorFileSink = null, appConsoleSink = null, didWarnIoError = false, didPruneOldLogFiles = false, didEnsureLogDir = false, didInstallGracefulExitHooks = false, config, Logger;
1106
+ var REGEXP_SPECIAL, INITIAL_CWD, DEFAULT_LOG_STRING_LEN = 100, DEFAULT_LOG_ARRAY_ITEMS = 100, maxLogStringLen, maxLogArrayItems, DEFAULT_LOG_SANITIZE_DEPTH = 3, DEFAULT_LOG_OBJECT_KEYS = 100, DEFAULT_LOG_SANITIZE_NODES = 500, sanitizeDepthLimit, sanitizeObjectKeysLimit, sanitizeNodesLimit, ONE_YEAR_MS, BUILTIN_SENSITIVE_KEYS, sensitiveKeySet, sensitiveContainsMatchers, sensitiveContainsRegex = null, HOSTNAME, LOG_LEVEL_NUM, DEFAULT_MAX_BUFFER_BYTES, DEFAULT_FLUSH_DELAY_MS = 10, DEFAULT_MAX_BATCH_BYTES, instance = null, errorInstance = null, mockInstance = null, appFileSink = null, errorFileSink = null, appConsoleSink = null, didWarnIoError = false, didPruneOldLogFiles = false, didEnsureLogDir = false, config, Logger;
1148
1107
  var init_logger = __esm(() => {
1149
1108
  init_asyncContext();
1150
1109
  REGEXP_SPECIAL = /[\\^$.*+?()[\]{}|]/g;
@@ -15590,29 +15549,19 @@ class Befly {
15590
15549
  Logger.info(`${this.config.appName} \u542F\u52A8\u6210\u529F! (${roleLabel}${envLabel})`);
15591
15550
  Logger.info(`\u670D\u52A1\u5668\u542F\u52A8\u8017\u65F6: ${finalStartupTime}`);
15592
15551
  Logger.info(`\u670D\u52A1\u5668\u76D1\u542C\u5730\u5740: ${server.url}`);
15593
- const gracefulShutdown = async (signal) => {
15594
- Logger.info(`\u6536\u5230 ${signal} \u4FE1\u53F7\uFF0C\u5F00\u59CB\u4F18\u96C5\u5173\u95ED...`);
15595
- try {
15596
- await server.stop();
15597
- Logger.info("HTTP \u670D\u52A1\u5668\u5DF2\u505C\u6B62");
15598
- } catch (error) {
15599
- Logger.error({ err: error }, "\u505C\u6B62 HTTP \u670D\u52A1\u5668\u65F6\u51FA\u9519");
15600
- }
15601
- try {
15602
- await Connect.disconnect();
15603
- Logger.info("\u6570\u636E\u5E93\u8FDE\u63A5\u5DF2\u5173\u95ED");
15604
- } catch (error) {
15605
- Logger.error({ err: error }, "\u5173\u95ED\u6570\u636E\u5E93\u8FDE\u63A5\u65F6\u51FA\u9519");
15606
- }
15607
- Logger.info("\u670D\u52A1\u5668\u5DF2\u4F18\u96C5\u5173\u95ED");
15608
- process.exit(0);
15609
- };
15610
- process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
15611
- process.on("SIGINT", () => gracefulShutdown("SIGINT"));
15612
15552
  return server;
15613
15553
  } catch (error) {
15614
15554
  Logger.error({ err: error }, "\u9879\u76EE\u542F\u52A8\u5931\u8D25");
15615
- process.exit(1);
15555
+ try {
15556
+ await Logger.flush();
15557
+ } catch {}
15558
+ try {
15559
+ await Logger.shutdown();
15560
+ } catch {}
15561
+ if (error instanceof Error) {
15562
+ throw error;
15563
+ }
15564
+ throw new Error(String(error));
15616
15565
  }
15617
15566
  }
15618
15567
  }