@idlebox/common 1.4.0 → 1.4.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.
Files changed (217) hide show
  1. package/lib/cjs/__create_index.generated.cjs +12 -4
  2. package/lib/cjs/__create_index.generated.cjs.map +1 -1
  3. package/lib/cjs/date/timeString.cjs +4 -4
  4. package/lib/cjs/date/timeString.cjs.map +1 -1
  5. package/lib/cjs/error/convertUnknown.cjs +2 -2
  6. package/lib/cjs/error/convertUnknown.cjs.map +1 -1
  7. package/lib/cjs/error/known.cjs +2 -2
  8. package/lib/cjs/error/known.cjs.map +1 -1
  9. package/lib/cjs/error/pretty.cjs +225 -154
  10. package/lib/cjs/error/pretty.cjs.map +1 -1
  11. package/lib/cjs/function/asyncCallbackList.cjs +2 -2
  12. package/lib/cjs/function/asyncCallbackList.cjs.map +1 -1
  13. package/lib/cjs/function/callbackList.cjs +2 -2
  14. package/lib/cjs/function/callbackList.cjs.map +1 -1
  15. package/lib/cjs/function/delayCallbackList.cjs +2 -2
  16. package/lib/cjs/function/delayCallbackList.cjs.map +1 -1
  17. package/lib/cjs/lifecycle/dispose/disposedError.cjs +7 -7
  18. package/lib/cjs/lifecycle/dispose/disposedError.cjs.map +1 -1
  19. package/lib/cjs/lifecycle/dispose/lifecycle.async.cjs +8 -8
  20. package/lib/cjs/lifecycle/dispose/lifecycle.async.cjs.map +1 -1
  21. package/lib/cjs/lifecycle/dispose/lifecycle.global.cjs +9 -9
  22. package/lib/cjs/lifecycle/dispose/lifecycle.global.cjs.map +1 -1
  23. package/lib/cjs/lifecycle/dispose/lifecycle.sync.cjs +7 -7
  24. package/lib/cjs/lifecycle/dispose/lifecycle.sync.cjs.map +1 -1
  25. package/lib/cjs/lifecycle/event/event.cjs +61 -6
  26. package/lib/cjs/lifecycle/event/event.cjs.map +1 -1
  27. package/lib/cjs/lifecycle/event/memorized.cjs +2 -2
  28. package/lib/cjs/lifecycle/event/memorized.cjs.map +1 -1
  29. package/lib/cjs/lifecycle/promise/cancellationToken/driver.common.cjs +2 -2
  30. package/lib/cjs/lifecycle/promise/cancellationToken/driver.common.cjs.map +1 -1
  31. package/lib/cjs/lifecycle/promise/cancellationToken/source.cjs +8 -8
  32. package/lib/cjs/lifecycle/promise/cancellationToken/source.cjs.map +1 -1
  33. package/lib/cjs/lifecycle/promise/deferredPromise.cjs +2 -2
  34. package/lib/cjs/lifecycle/promise/deferredPromise.cjs.map +1 -1
  35. package/lib/cjs/lifecycle/timeout/timeout.cjs +4 -4
  36. package/lib/cjs/lifecycle/timeout/timeout.cjs.map +1 -1
  37. package/lib/cjs/lifecycle/timeout/timeoutError.cjs +2 -2
  38. package/lib/cjs/lifecycle/timeout/timeoutError.cjs.map +1 -1
  39. package/lib/cjs/log/logger.cjs +6 -6
  40. package/lib/cjs/log/logger.cjs.map +1 -1
  41. package/lib/cjs/misc/package.json.cjs +22 -0
  42. package/lib/cjs/misc/package.json.cjs.map +1 -1
  43. package/lib/cjs/object/definePublicConstant.cjs +9 -3
  44. package/lib/cjs/object/definePublicConstant.cjs.map +1 -1
  45. package/lib/cjs/object/objectSame.cjs +4 -4
  46. package/lib/cjs/object/objectSame.cjs.map +1 -1
  47. package/lib/cjs/path/pathArray.cjs +9 -9
  48. package/lib/cjs/path/pathArray.cjs.map +1 -1
  49. package/lib/cjs/platform/globalSingleton.cjs +5 -5
  50. package/lib/cjs/platform/globalSingleton.cjs.map +1 -1
  51. package/lib/cjs/platform/globalSymbol.cjs +2 -2
  52. package/lib/cjs/platform/globalSymbol.cjs.map +1 -1
  53. package/lib/cjs/promise/promiseCollection.cjs +4 -4
  54. package/lib/cjs/promise/promiseCollection.cjs.map +1 -1
  55. package/lib/cjs/promise/timeoutPromiseCollection.cjs +4 -4
  56. package/lib/cjs/promise/timeoutPromiseCollection.cjs.map +1 -1
  57. package/lib/cjs/reflection/classes/singleton.cjs +2 -2
  58. package/lib/cjs/reflection/classes/singleton.cjs.map +1 -1
  59. package/lib/cjs/reflection/methods/initOnRead.cjs +2 -2
  60. package/lib/cjs/reflection/methods/initOnRead.cjs.map +1 -1
  61. package/lib/cjs/state/StateMachine.cjs +2 -2
  62. package/lib/cjs/state/StateMachine.cjs.map +1 -1
  63. package/lib/cjs/string/concatType.generated.cjs +0 -1
  64. package/lib/cjs/string/concatType.generated.cjs.map +1 -1
  65. package/lib/esm/__create_index.generated.d.ts +9 -0
  66. package/lib/esm/__create_index.generated.d.ts.map +1 -1
  67. package/lib/esm/__create_index.generated.js +4 -0
  68. package/lib/esm/__create_index.generated.js.map +1 -1
  69. package/lib/esm/__virtual_tsconfig.tsbuildinfo +1 -1
  70. package/lib/esm/array/arrayUnique.d.ts.map +1 -1
  71. package/lib/esm/date/timeString.js +1 -1
  72. package/lib/esm/date/timeString.js.map +1 -1
  73. package/lib/esm/error/convertUnknown.js +1 -1
  74. package/lib/esm/error/convertUnknown.js.map +1 -1
  75. package/lib/esm/error/known.js +1 -1
  76. package/lib/esm/error/known.js.map +1 -1
  77. package/lib/esm/error/pretty.d.ts +34 -1
  78. package/lib/esm/error/pretty.d.ts.map +1 -1
  79. package/lib/esm/error/pretty.js +219 -150
  80. package/lib/esm/error/pretty.js.map +1 -1
  81. package/lib/esm/function/asyncCallbackList.js +1 -1
  82. package/lib/esm/function/asyncCallbackList.js.map +1 -1
  83. package/lib/esm/function/callbackList.js +1 -1
  84. package/lib/esm/function/callbackList.js.map +1 -1
  85. package/lib/esm/function/delayCallbackList.js +1 -1
  86. package/lib/esm/function/delayCallbackList.js.map +1 -1
  87. package/lib/esm/lifecycle/dispose/disposableEvent.d.ts +1 -1
  88. package/lib/esm/lifecycle/dispose/disposableEvent.d.ts.map +1 -1
  89. package/lib/esm/lifecycle/dispose/disposedError.js +3 -3
  90. package/lib/esm/lifecycle/dispose/disposedError.js.map +1 -1
  91. package/lib/esm/lifecycle/dispose/lifecycle.async.d.ts +2 -2
  92. package/lib/esm/lifecycle/dispose/lifecycle.async.d.ts.map +1 -1
  93. package/lib/esm/lifecycle/dispose/lifecycle.async.js +3 -3
  94. package/lib/esm/lifecycle/dispose/lifecycle.async.js.map +1 -1
  95. package/lib/esm/lifecycle/dispose/lifecycle.d.ts +1 -1
  96. package/lib/esm/lifecycle/dispose/lifecycle.d.ts.map +1 -1
  97. package/lib/esm/lifecycle/dispose/lifecycle.global.d.ts +2 -2
  98. package/lib/esm/lifecycle/dispose/lifecycle.global.d.ts.map +1 -1
  99. package/lib/esm/lifecycle/dispose/lifecycle.global.js +3 -3
  100. package/lib/esm/lifecycle/dispose/lifecycle.global.js.map +1 -1
  101. package/lib/esm/lifecycle/dispose/lifecycle.sync.d.ts +2 -2
  102. package/lib/esm/lifecycle/dispose/lifecycle.sync.d.ts.map +1 -1
  103. package/lib/esm/lifecycle/dispose/lifecycle.sync.js +2 -2
  104. package/lib/esm/lifecycle/dispose/lifecycle.sync.js.map +1 -1
  105. package/lib/esm/lifecycle/event/event.d.ts +13 -1
  106. package/lib/esm/lifecycle/event/event.d.ts.map +1 -1
  107. package/lib/esm/lifecycle/event/event.js +61 -6
  108. package/lib/esm/lifecycle/event/event.js.map +1 -1
  109. package/lib/esm/lifecycle/event/memorized.d.ts +2 -2
  110. package/lib/esm/lifecycle/event/memorized.d.ts.map +1 -1
  111. package/lib/esm/lifecycle/event/memorized.js +1 -1
  112. package/lib/esm/lifecycle/event/memorized.js.map +1 -1
  113. package/lib/esm/lifecycle/promise/cancellationToken/driver.browser.d.ts +1 -1
  114. package/lib/esm/lifecycle/promise/cancellationToken/driver.browser.d.ts.map +1 -1
  115. package/lib/esm/lifecycle/promise/cancellationToken/driver.common.d.ts +1 -1
  116. package/lib/esm/lifecycle/promise/cancellationToken/driver.common.d.ts.map +1 -1
  117. package/lib/esm/lifecycle/promise/cancellationToken/driver.common.js +1 -1
  118. package/lib/esm/lifecycle/promise/cancellationToken/driver.common.js.map +1 -1
  119. package/lib/esm/lifecycle/promise/cancellationToken/source.d.ts +3 -3
  120. package/lib/esm/lifecycle/promise/cancellationToken/source.d.ts.map +1 -1
  121. package/lib/esm/lifecycle/promise/cancellationToken/source.js +4 -4
  122. package/lib/esm/lifecycle/promise/cancellationToken/source.js.map +1 -1
  123. package/lib/esm/lifecycle/promise/deferredPromise.js +1 -1
  124. package/lib/esm/lifecycle/promise/deferredPromise.js.map +1 -1
  125. package/lib/esm/lifecycle/timeout/timeout.d.ts +1 -1
  126. package/lib/esm/lifecycle/timeout/timeout.d.ts.map +1 -1
  127. package/lib/esm/lifecycle/timeout/timeout.js +2 -2
  128. package/lib/esm/lifecycle/timeout/timeout.js.map +1 -1
  129. package/lib/esm/lifecycle/timeout/timeoutError.js +1 -1
  130. package/lib/esm/lifecycle/timeout/timeoutError.js.map +1 -1
  131. package/lib/esm/log/logger.js +2 -2
  132. package/lib/esm/log/logger.js.map +1 -1
  133. package/lib/esm/misc/package.json.d.ts +19 -1
  134. package/lib/esm/misc/package.json.d.ts.map +1 -1
  135. package/lib/esm/misc/package.json.js +21 -1
  136. package/lib/esm/misc/package.json.js.map +1 -1
  137. package/lib/esm/object/definePublicConstant.d.ts +1 -0
  138. package/lib/esm/object/definePublicConstant.d.ts.map +1 -1
  139. package/lib/esm/object/definePublicConstant.js +8 -3
  140. package/lib/esm/object/definePublicConstant.js.map +1 -1
  141. package/lib/esm/object/objectSame.js +1 -1
  142. package/lib/esm/object/objectSame.js.map +1 -1
  143. package/lib/esm/path/pathArray.js +3 -3
  144. package/lib/esm/path/pathArray.js.map +1 -1
  145. package/lib/esm/platform/globalSingleton.js +2 -2
  146. package/lib/esm/platform/globalSingleton.js.map +1 -1
  147. package/lib/esm/platform/globalSymbol.js +1 -1
  148. package/lib/esm/platform/globalSymbol.js.map +1 -1
  149. package/lib/esm/promise/promiseCollection.d.ts +2 -2
  150. package/lib/esm/promise/promiseCollection.d.ts.map +1 -1
  151. package/lib/esm/promise/promiseCollection.js +2 -2
  152. package/lib/esm/promise/promiseCollection.js.map +1 -1
  153. package/lib/esm/promise/timeoutPromiseCollection.d.ts +2 -2
  154. package/lib/esm/promise/timeoutPromiseCollection.d.ts.map +1 -1
  155. package/lib/esm/promise/timeoutPromiseCollection.js +2 -2
  156. package/lib/esm/promise/timeoutPromiseCollection.js.map +1 -1
  157. package/lib/esm/reflection/classes/singleton.js +1 -1
  158. package/lib/esm/reflection/classes/singleton.js.map +1 -1
  159. package/lib/esm/reflection/methods/initOnRead.d.ts +1 -1
  160. package/lib/esm/reflection/methods/initOnRead.d.ts.map +1 -1
  161. package/lib/esm/reflection/methods/initOnRead.js +1 -1
  162. package/lib/esm/reflection/methods/initOnRead.js.map +1 -1
  163. package/lib/esm/state/StateMachine.d.ts +1 -1
  164. package/lib/esm/state/StateMachine.d.ts.map +1 -1
  165. package/lib/esm/state/StateMachine.js +1 -1
  166. package/lib/esm/state/StateMachine.js.map +1 -1
  167. package/lib/esm/string/concatType.generated.d.ts.map +1 -1
  168. package/lib/esm/string/concatType.generated.js +0 -1
  169. package/lib/esm/string/concatType.generated.js.map +1 -1
  170. package/lib/esm/typingHelper/deep.partial.d.ts +1 -1
  171. package/lib/esm/typingHelper/deep.partial.d.ts.map +1 -1
  172. package/lib/esm/typingHelper/deep.readonly.d.ts +1 -1
  173. package/lib/esm/typingHelper/deep.readonly.d.ts.map +1 -1
  174. package/lib/esm/typingHelper/deep.required.d.ts +1 -1
  175. package/lib/esm/typingHelper/deep.required.d.ts.map +1 -1
  176. package/lib/esm/typingHelper/deep.writable.d.ts +1 -1
  177. package/lib/esm/typingHelper/deep.writable.d.ts.map +1 -1
  178. package/package.json +2 -2
  179. package/src/date/timeString.ts +1 -1
  180. package/src/error/convertUnknown.ts +1 -1
  181. package/src/error/known.ts +1 -1
  182. package/src/error/pretty.ts +269 -153
  183. package/src/function/asyncCallbackList.ts +1 -1
  184. package/src/function/callbackList.ts +1 -1
  185. package/src/function/delayCallbackList.ts +1 -1
  186. package/src/lifecycle/dispose/disposableEvent.ts +1 -1
  187. package/src/lifecycle/dispose/disposedError.ts +3 -3
  188. package/src/lifecycle/dispose/lifecycle.async.ts +4 -4
  189. package/src/lifecycle/dispose/lifecycle.global.ts +4 -4
  190. package/src/lifecycle/dispose/lifecycle.sync.ts +3 -3
  191. package/src/lifecycle/dispose/lifecycle.ts +1 -1
  192. package/src/lifecycle/event/event.ts +64 -7
  193. package/src/lifecycle/event/memorized.ts +2 -2
  194. package/src/lifecycle/promise/cancellationToken/driver.browser.ts +2 -2
  195. package/src/lifecycle/promise/cancellationToken/driver.common.ts +3 -3
  196. package/src/lifecycle/promise/cancellationToken/source.ts +6 -6
  197. package/src/lifecycle/promise/deferredPromise.ts +1 -1
  198. package/src/lifecycle/timeout/timeout.ts +2 -2
  199. package/src/lifecycle/timeout/timeoutError.ts +1 -1
  200. package/src/log/logger.ts +2 -2
  201. package/src/misc/package.json.ts +45 -1
  202. package/src/object/definePublicConstant.ts +9 -2
  203. package/src/object/objectSame.ts +1 -1
  204. package/src/path/pathArray.ts +3 -3
  205. package/src/platform/globalSingleton.ts +2 -2
  206. package/src/platform/globalSymbol.ts +1 -1
  207. package/src/promise/promiseCollection.ts +2 -2
  208. package/src/promise/timeoutPromiseCollection.ts +2 -2
  209. package/src/reflection/classes/singleton.ts +1 -1
  210. package/src/reflection/methods/initOnRead.ts +1 -1
  211. package/src/state/StateMachine.ts +1 -1
  212. package/src/string/concatType.generated.ts +0 -3
  213. package/src/string/concatType.generator.ts +3 -13
  214. package/src/typingHelper/deep.partial.ts +1 -1
  215. package/src/typingHelper/deep.readonly.ts +1 -1
  216. package/src/typingHelper/deep.required.ts +1 -1
  217. package/src/typingHelper/deep.writable.ts +1 -1
@@ -1,57 +1,146 @@
1
- import { isAbsolute } from '../path/isAbsolute';
2
- import { relativePath } from '../path/normalizePath';
3
- import { globalObject } from '../platform/globalObject';
4
- import { isNative } from '../platform/os';
1
+ import { isAbsolute } from '../path/isAbsolute.js';
2
+ import { relativePath } from '../path/normalizePath.js';
3
+ import { globalObject } from '../platform/globalObject.js';
4
+ import { isNative } from '../platform/os.js';
5
5
 
6
- const regNormal = /^\s+at ([^\/\s]+)(?: \[as ([^\]]+)])? \(((?:node|file):\/*)?([^:]+)(:\d+)?(:\d+)\)$/;
7
6
  const process = globalObject.process;
8
7
  const window = globalObject.window;
9
8
 
10
- const enum regNormalMatch {
11
- fn = 1,
12
- fnAlias,
13
- fileSchema,
14
- file,
15
- row,
16
- column,
17
- }
9
+ const padding = /^(?<padding>\s*)at /.source;
10
+ const func_call = /(?<func_name>(?:async )?[^\/\\\s]+) (?:\[as (?<func_alias>[^\]]+)] )?/.source;
11
+ // [xxxx.yyyyy] [as eval]
12
+ const line_column = /(?::(?<line>\d+))?(?::(?<column>\d+))?/.source;
13
+ const locationEsm = /(?<schema>node:|file:\/\/|https?:\/\/)?(?<path2>[^:]+)/.source;
14
+ // node:internal/modules/cjs/loader.js:883:14
15
+ const locationCjs = /(?<path1>(?:\/|[a-zA-Z]:)[^:]+)/.source;
16
+ // /data/to/file.js
17
+ const location = `(?:${locationCjs}|${locationEsm})${line_column}`;
18
18
 
19
- const regFunctionOnly = /^\s+at ([^\/\\\s]+(?: \[as ([^\]]+)])?)$/;
19
+ const regNormal = new RegExp(`${padding}${func_call}\\(${location}\\)$`);
20
+ type TypeMatchNormal = 'padding' | TypeMatchNoFile | TypeMatchFileOnly;
20
21
 
21
- const enum regFunctionMatch {
22
- fn = 1,
23
- fnAlias,
24
- }
22
+ const regNoFile = new RegExp(`${padding}${func_call}$`);
23
+ type TypeMatchNoFile = 'padding' | 'func_name' | 'func_alias';
24
+
25
+ const regFileOnly = new RegExp(`${padding}${location}$`);
26
+ type TypeMatchFileOnly = 'padding' | 'schema' | 'path1' | 'path2' | 'line' | 'column';
25
27
 
26
- const regFileOnly = /^\s+at ([^:]+)(:\d+)?(:\d+)$/;
28
+ const regEvalItem = new RegExp(`\\(eval at ${func_call}`, 'g');
29
+ const eval_source = /, (?<eval_func>[\S]+):(?<eval_line>\d+):(?<eval_column>\d+)/.source;
30
+ const regEval = new RegExp(`${padding}${func_call}.*?\\(${location}\\)+${eval_source}`);
31
+ type TypeMatchEval = 'padding' | TypeMatchNoFile | TypeMatchFileOnly | 'eval_func' | 'eval_line' | 'eval_column';
27
32
 
28
- const enum regFileOnlyMatch {
29
- file = 1,
30
- row,
31
- column,
33
+ let root = process?.cwd?.() ?? window?.location?.domain ?? '/';
34
+ export function setErrorLogRoot(_root: string) {
35
+ root = _root;
32
36
  }
33
37
 
34
- const regSimpleFrame = /^\s+at ([^(]+) \(<anonymous>\)$/;
38
+ interface IFunction {
39
+ name: string;
40
+ alias?: string;
41
+ }
42
+ interface IFileLocation {
43
+ path: string;
44
+ schema: string; // '' | 'node:' | 'file:' | 'http:' | 'https:';
45
+ line: number;
46
+ column: number;
47
+ isAbsolute: boolean;
48
+ }
49
+ interface IEvalDef {
50
+ eval_func: string;
51
+ eval_line: number;
52
+ eval_column: number;
53
+ funcs: string[];
54
+ }
35
55
 
36
- const enum regSimpleFrameMatch {
37
- fn = 1,
56
+ export interface IStructreStackLine {
57
+ invalid?: boolean;
58
+ toString(): string;
59
+ padding?: string;
60
+ func?: IFunction;
61
+ location?: IFileLocation;
62
+ eval?: IEvalDef;
38
63
  }
39
64
 
40
- let root = process?.cwd?.() ?? window?.location?.domain ?? '/';
65
+ function matchLine<T extends string>(line: string, reg: RegExp): null | Record<T, string> {
66
+ const m = reg.exec(line);
67
+ if (!m) {
68
+ return null;
69
+ }
70
+ return m.groups as any;
71
+ }
72
+ function addLoc(ret: IStructreStackLine, m: Record<TypeMatchFileOnly, string>) {
73
+ const path = m.path1 || m.path2;
74
+ ret.location = {
75
+ schema: m.schema?.replace(/\/+$/, '') ?? '',
76
+ path: path,
77
+ line: parseInt(m.line),
78
+ column: parseInt(m.column),
79
+ isAbsolute: isAbsolute(path),
80
+ };
81
+ }
41
82
 
42
- export function setErrorLogRoot(_root: string) {
43
- root = _root;
83
+ function addFunc(ret: IStructreStackLine, m: Record<TypeMatchNoFile, string>) {
84
+ ret.func = {
85
+ name: m.func_name,
86
+ alias: m.func_alias,
87
+ };
44
88
  }
45
89
 
46
- interface IInternalData {
47
- raw?: string;
48
- fn?: string;
49
- as?: string;
50
- file?: string;
51
- fileSchema?: string; // 'node' | 'file';
52
- line?: number;
53
- col?: number;
54
- abs?: boolean;
90
+ export function parseStackLine(line: string): IStructreStackLine {
91
+ const __raw = line;
92
+ const ret: IStructreStackLine = {
93
+ invalid: false,
94
+ toString() {
95
+ return __raw;
96
+ },
97
+ };
98
+ Object.assign(ret, { __raw });
99
+ const mNormal = matchLine<TypeMatchNormal>(line, regNormal);
100
+
101
+ if (mNormal) {
102
+ ret.padding = mNormal.padding;
103
+ addFunc(ret, mNormal);
104
+ addLoc(ret, mNormal);
105
+ return ret;
106
+ }
107
+
108
+ const mFile = matchLine<TypeMatchFileOnly>(line, regFileOnly);
109
+ if (mFile) {
110
+ ret.padding = mFile.padding;
111
+ addLoc(ret, mFile);
112
+ return ret;
113
+ }
114
+
115
+ const mNoFile = matchLine<TypeMatchNoFile>(line, regNoFile);
116
+ if (mNoFile) {
117
+ ret.padding = mNoFile.padding;
118
+ addFunc(ret, mNoFile);
119
+ return ret;
120
+ }
121
+
122
+ const mEval = matchLine<TypeMatchEval>(line.replaceAll(regEvalItem, ''), regEval);
123
+ if (mEval) {
124
+ ret.padding = mEval.padding;
125
+ addFunc(ret, mEval);
126
+ addLoc(ret, mEval);
127
+
128
+ ret.eval = {
129
+ eval_column: parseInt(mEval.eval_column),
130
+ eval_func: mEval.eval_func,
131
+ eval_line: parseInt(mEval.eval_line),
132
+ funcs: [],
133
+ };
134
+ for (const item of line.matchAll(regEvalItem)) {
135
+ ret.eval.funcs.push(item.groups!.func_name);
136
+ }
137
+ ret.eval.funcs.push(mEval.eval_func);
138
+
139
+ return ret;
140
+ }
141
+
142
+ ret.invalid = true;
143
+ return ret;
55
144
  }
56
145
 
57
146
  let notify_printed = false;
@@ -60,7 +149,7 @@ export function prettyPrintError(type: string, e: Error) {
60
149
  return console.error(e.message);
61
150
  }
62
151
 
63
- if (globalObject.DISABLE_PRETTY_ERROR || process?.env?.DISABLE_PRETTY_ERROR) {
152
+ if (globalObject.DISABLE_PRETTY_ERROR || globalObject.process?.env?.DISABLE_PRETTY_ERROR) {
64
153
  console.error('[%s] %s', type, e.stack || e.message);
65
154
  return;
66
155
  }
@@ -81,120 +170,125 @@ function red(s: string) {
81
170
  return isNative ? `\x1B[38;5;9m${s}\x1B[0m` : s;
82
171
  }
83
172
 
84
- export function prettyFormatError(e: Error, withMessage = true) {
85
- if (!e || typeof e.message !== 'string') {
86
- console.error(e);
87
- return red('Unknown Error') + '\n' + new Error().stack?.split('\n').slice(3).join('\n');
88
- }
89
- if (!e.stack) {
90
- return red(e.message + '\n No stack trace');
91
- }
92
- const stackStr = e.stack.replace(/(file|http|https):\/\//gi, '').split(/\n/g);
93
-
94
- let first = stackStr.shift()!;
95
- const stack: IInternalData[] = stackStr.map((line) => {
96
- if (regNormal.test(line)) {
97
- const m = regNormal.exec(line)!;
98
- return {
99
- fn: m[regNormalMatch.fn],
100
- as: m[regNormalMatch.fnAlias],
101
- fileSchema: m[regNormalMatch.fileSchema],
102
- file: m[regNormalMatch.file],
103
- line: parseInt(m[regNormalMatch.row].slice(1)),
104
- col: parseInt(m[regNormalMatch.column].slice(1)),
105
- abs: isAbsolute(m[regNormalMatch.file]),
106
- };
107
- } else if (regFunctionOnly.test(line)) {
108
- const m = regFunctionOnly.exec(line)!;
109
- return {
110
- fn: m[regFunctionMatch.fn],
111
- as: m[regFunctionMatch.fnAlias],
112
- file: undefined,
113
- line: undefined,
114
- col: undefined,
115
- abs: false,
116
- };
117
- } else if (regFileOnly.test(line)) {
118
- const m = regFileOnly.exec(line)!;
119
- return {
120
- fn: undefined,
121
- as: undefined,
122
- file: m[regFileOnlyMatch.file],
123
- line: parseInt(m[regFileOnlyMatch.row].slice(1)),
124
- col: parseInt(m[regFileOnlyMatch.column].slice(1)),
125
- abs: isAbsolute(m[regFileOnlyMatch.file]),
126
- };
127
- } else if (regSimpleFrame.test(line)) {
128
- const m = regSimpleFrame.exec(line)!;
129
- return {
130
- fn: m[regSimpleFrameMatch.fn],
131
- as: undefined,
132
- file: '<anonymous>',
133
- line: undefined,
134
- col: undefined,
135
- abs: false,
136
- };
137
- } else {
138
- return { raw: line.replace(/^ /, '') };
139
- }
140
- });
141
- const stackOutput = stack
142
- .filter(ignoreSomeFiles)
173
+ export function prettyFormatStack(stackLines: readonly string[]) {
174
+ const structured: IStructreStackLine[] = stackLines.map(parseStackLine);
175
+ return structured
176
+ .filter(skipSomeFrame)
143
177
  .map(translateFunction)
144
- .map(({ raw, fn, file, as, abs, line, col, fileSchema }) => {
178
+ .map((line) => {
145
179
  let ret;
146
- if (raw) {
147
- return raw;
180
+ if (line.invalid) {
181
+ return line.toString();
148
182
  }
183
+ const fn_name = line.func?.name;
149
184
 
150
- if (abs) {
151
- const isNodeModule = file?.includes('/node_modules/');
152
- const color = fn ? (isNodeModule ? '4' : '14') : '8';
153
- ret = ` at \x1b[38;5;${color}m`;
154
- if (as && fn && fn.startsWith('Object.')) {
155
- ret += as + ' [export]';
156
- } else {
157
- ret += formatFunctionName(fn, as);
158
- }
159
- ret += '\x1b[0m';
160
- if (file) {
161
- ret += ' (';
162
- if (!isNodeModule) {
163
- ret += '\x1b[38;5;2m';
185
+ if (!line.location?.isAbsolute || line.location.schema === 'node:') {
186
+ // no-path, relative path, node:
187
+ ret = ' at \x1B[2m';
188
+ if (line.func) {
189
+ ret += fn_name;
190
+ if (line.func.alias) {
191
+ ret += ` [${line.func.alias}]`;
164
192
  }
165
- ret += formatFileLine(fileSchema, file, line, col);
166
- ret += '\x1B[0m)';
167
- }
168
- } else {
169
- ret = '\x1B[2m at ';
170
- if (fn) {
171
- ret += fn;
172
193
  }
173
- if (file === '<anonymous>') {
174
- } else if (file) {
175
- if (fn) {
194
+
195
+ if (line.location?.path === '<anonymous>') {
196
+ } else if (line.location?.path) {
197
+ if (fn_name) {
176
198
  ret += ' (';
177
199
  }
178
- ret += formatFileLine(fileSchema, file, line, col);
179
- if (fn) {
200
+ ret += formatFileLine(
201
+ line.location.schema,
202
+ line.location.path,
203
+ line.location.line,
204
+ line.location.column,
205
+ );
206
+ if (fn_name) {
180
207
  ret += ')';
181
208
  }
182
209
  }
183
210
  ret += '\x1B[0m';
211
+ } else {
212
+ const path = line.location.path;
213
+ const isNodeModule = path.includes('/node_modules/');
214
+ const fn_alias = line.func?.alias;
215
+
216
+ const color = fn_name ? (isNodeModule ? '14;2' : '14') : '8';
217
+ ret = `\x1b[0m at \x1b[38;5;${color}m`;
218
+ if (fn_alias && fn_name && fn_name.startsWith('Object.')) {
219
+ ret += fn_alias + ' [export]';
220
+ } else {
221
+ ret += formatFunctionName(fn_name, fn_alias);
222
+ }
223
+ ret += '\x1b[0m';
224
+ if (line.eval) {
225
+ ret += `\x1b[2m(${line.eval.funcs.filter((e) => e !== '<anonymous>').join('->')})\x1b[0m`;
226
+ }
227
+
228
+ if (path) {
229
+ let color;
230
+ if (isNodeModule) {
231
+ color = '\x1b[38;5;237m';
232
+ } else {
233
+ color = '\x1b[38;5;2m';
234
+ }
235
+ ret += ' (' + color;
236
+ const file = formatFileLine(line.location.schema, path, line.location.line, line.location.column);
237
+ const lastNmPos = file.lastIndexOf('/node_modules/');
238
+ if (lastNmPos >= 0) {
239
+ ret += file.slice(0, lastNmPos + 14);
240
+
241
+ let nextSlash = file.indexOf('/', lastNmPos + 15);
242
+ const scopePkg = file[lastNmPos + 14] === '@';
243
+ if (scopePkg) {
244
+ nextSlash = file.indexOf('/', nextSlash + 1);
245
+ }
246
+
247
+ ret += '\x1B[0m';
248
+ ret += file.slice(lastNmPos + 14, nextSlash);
249
+ ret += color;
250
+ ret += file.slice(nextSlash);
251
+ } else {
252
+ ret += file;
253
+ }
254
+ ret += '\x1B[0m)';
255
+ }
184
256
  }
185
257
  return ret;
186
258
  });
259
+ }
260
+
261
+ interface IError {
262
+ readonly code?: any;
263
+ readonly message?: any;
264
+ readonly stack?: string;
265
+ }
266
+ export function prettyFormatError(e: IError, withMessage = true) {
267
+ if (!e || !e.stack) {
268
+ if (withMessage) {
269
+ let msg = e?.message || 'Unknown Error';
270
+
271
+ return red(msg + '\n No stack trace');
272
+ } else {
273
+ return red(' No stack trace');
274
+ }
275
+ }
276
+
277
+ let stackLines = e.stack.split(/\n/g);
278
+
279
+ let first = 'Unknown Error';
280
+ if (/^\S/.test(stackLines[0])) {
281
+ first = stackLines.shift()!;
282
+ }
187
283
 
188
284
  if (withMessage) {
189
- if ((e as any).code) {
190
- first = first.replace(/^(\S+):/, (_, name) => {
191
- return `${name}(code ${(e as any).code}):`;
192
- });
285
+ if (e.code) {
286
+ first = first.replace(/^(\S+):/, `$1(code ${e.code}):`);
193
287
  }
194
288
 
195
- return first + '\n' + stackOutput.join('\n');
289
+ return first + '\n' + prettyFormatStack(stackLines).join('\n');
196
290
  } else {
197
- return stackOutput.join('\n');
291
+ return prettyFormatStack(stackLines).join('\n');
198
292
  }
199
293
  }
200
294
 
@@ -205,10 +299,15 @@ function formatFileLine(schema: string | undefined, file: string, row?: number,
205
299
  if (schema !== 'node:') {
206
300
  try {
207
301
  rel = relativePath(root, file);
208
- if (rel.startsWith('..')) {
209
- rel = file;
210
- } else if (!rel.startsWith('.')) {
302
+ if (rel[0] !== '.') {
211
303
  rel = './' + rel;
304
+ } else if (rel[1] === '.') {
305
+ if (file.length < rel.length) {
306
+ rel = file;
307
+ }
308
+ }
309
+ if (rel[0] === '.') {
310
+ schema = '';
212
311
  }
213
312
  } catch (e: any) {
214
313
  if (!alert) {
@@ -236,36 +335,53 @@ function formatFunctionName(fn?: string, as?: string) {
236
335
  return fn;
237
336
  }
238
337
  } else {
239
- return '[anonymous]';
338
+ return '<anonymous>';
240
339
  }
241
340
  }
242
341
 
243
- function translateFunction(data: IInternalData): IInternalData {
244
- if (!data.fn) {
342
+ function translateFunction(data: IStructreStackLine): IStructreStackLine {
343
+ if (!data.func?.name) {
245
344
  return data;
246
345
  }
247
- if (data.fn === 'Timeout._onTimeout') {
248
- data.fn = 'setTimeout';
346
+ if (data.func.name === 'Timeout._onTimeout') {
347
+ data.func.name = 'setTimeout';
348
+ }
349
+ if (data.func.name === 'Immediate._onImmediate') {
350
+ data.func.name = 'setImmediate';
249
351
  }
250
- if (data.fn.startsWith('Timeout.') && data.as === '_onTimeout') {
251
- data.fn = 'setTimeout->' + data.fn.slice(8);
252
- delete data.as;
352
+ if (data.func.name === 'process.processTicksAndRejections') {
353
+ data.func.name = 'process.nextTick';
354
+ }
355
+ if (data.func.name.startsWith('Timeout.') && data.func.alias === '_onTimeout') {
356
+ data.func.name = 'setTimeout->' + data.func.name.slice(8);
357
+ delete data.func.alias;
253
358
  }
254
359
  return data;
255
360
  }
256
361
 
257
- function ignoreSomeFiles({ file }: IInternalData): boolean {
258
- if (!file) {
362
+ /**
363
+ *
364
+ */
365
+ function skipSomeFrame({ location }: IStructreStackLine): boolean {
366
+ if (!location?.path) {
259
367
  return true;
260
368
  }
261
- if (file === 'internal/timers.js') {
262
- return false;
263
- }
264
- if (file.startsWith('internal/modules/cjs/loader')) {
265
- return false;
369
+
370
+ if (location.schema === 'node:') {
371
+ if (location.path.startsWith('internal/timers')) {
372
+ return false;
373
+ } else if (location.path === 'internal/modules/cjs/loader') {
374
+ return false;
375
+ } else if (location.path === 'internal/modules/esm/loader') {
376
+ return false;
377
+ } else if (location.path === 'internal/modules/run_main') {
378
+ return false;
379
+ } else if (location.path === 'internal/modules/esm/module_job') {
380
+ return false;
381
+ }
266
382
  }
267
- if (!file.includes('/')) {
268
- if (file.startsWith('_stream_')) {
383
+ if (!location.path.includes('/')) {
384
+ if (location.path.startsWith('_stream_')) {
269
385
  return false;
270
386
  }
271
387
  }
@@ -1,4 +1,4 @@
1
- import { nameFunction } from './functionName';
1
+ import { nameFunction } from './functionName.js';
2
2
 
3
3
  export interface MyAsyncCallback<Argument extends unknown[]> {
4
4
  displayName?: string;
@@ -1,4 +1,4 @@
1
- import { nameFunction } from './functionName';
1
+ import { nameFunction } from './functionName.js';
2
2
 
3
3
  export interface MyCallback<Argument extends unknown[]> {
4
4
  displayName?: string;
@@ -1,4 +1,4 @@
1
- import { nameFunction } from './functionName';
1
+ import { nameFunction } from './functionName.js';
2
2
 
3
3
  export interface MyDelayCallback<Argument extends unknown[]> {
4
4
  displayName?: string;
@@ -1,4 +1,4 @@
1
- import { IDisposable } from './lifecycle';
1
+ import { IDisposable } from './lifecycle.js';
2
2
 
3
3
  declare const AbortController: new () => any;
4
4
 
@@ -1,6 +1,6 @@
1
- import { tryInspect } from '../../debugging/tryInspect';
2
- import { getErrorFrame } from '../../error/getFrame';
3
- import { prettyFormatError } from '../../error/pretty';
1
+ import { tryInspect } from '../../debugging/tryInspect.js';
2
+ import { getErrorFrame } from '../../error/getFrame.js';
3
+ import { prettyFormatError } from '../../error/pretty.js';
4
4
 
5
5
  /**
6
6
  * Error when call dispose() twice
@@ -1,7 +1,7 @@
1
- import { convertCatchedError } from '../../error/convertUnknown';
2
- import { Emitter, EventRegister } from '../event/event';
3
- import { DisposedError } from './disposedError';
4
- import { IAsyncDisposable, IDisposableEvents } from './lifecycle';
1
+ import { convertCatchedError } from '../../error/convertUnknown.js';
2
+ import { Emitter, EventRegister } from '../event/event.js';
3
+ import { DisposedError } from './disposedError.js';
4
+ import { IAsyncDisposable, IDisposableEvents } from './lifecycle.js';
5
5
 
6
6
  /**
7
7
  * Async version of Disposable
@@ -1,7 +1,7 @@
1
- import { globalSingletonStrong } from '../../platform/globalSingleton';
2
- import { createSymbol } from '../../platform/globalSymbol';
3
- import { IDisposable } from './lifecycle';
4
- import { AsyncDisposable } from './lifecycle.async';
1
+ import { globalSingletonStrong } from '../../platform/globalSingleton.js';
2
+ import { createSymbol } from '../../platform/globalSymbol.js';
3
+ import { IDisposable } from './lifecycle.js';
4
+ import { AsyncDisposable } from './lifecycle.async.js';
5
5
 
6
6
  const symbol = createSymbol('lifecycle', 'application');
7
7
 
@@ -1,6 +1,6 @@
1
- import { Emitter, EventRegister } from '../event/event';
2
- import { DisposedError } from './disposedError';
3
- import { IDisposable, IDisposableEvents } from './lifecycle';
1
+ import { Emitter, EventRegister } from '../event/event.js';
2
+ import { DisposedError } from './disposedError.js';
3
+ import { IDisposable, IDisposableEvents } from './lifecycle.js';
4
4
 
5
5
  export abstract class DisposableOnce implements IDisposable {
6
6
  private _disposed?: Error;
@@ -1,4 +1,4 @@
1
- import { EventRegister } from '../event/event';
1
+ import { EventRegister } from '../event/event.js';
2
2
 
3
3
  /**
4
4
  * @private