@remotex-labs/xmap 2.0.3 → 2.1.0

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.
@@ -1,11 +1,218 @@
1
1
  /**
2
2
  * Import will remove at compile time
3
3
  */
4
- import type { StackEntryInterface } from "./interfaces/parse.interface";
4
+ import type { ParsedStackTrace, StackFrame } from "./interfaces/parse.interface";
5
5
  /**
6
- * Parses an error stack trace and returns an object with a message and an array of stack entries.
6
+ * Enumeration of JavaScript engines that can be detected from stack traces
7
7
  *
8
- * @param stackString - The error stack trace.
9
- * @returns The parsed stack trace object.
8
+ * @since 2.1.0
10
9
  */
11
- export declare function parseErrorStack(stackString: string): Array<StackEntryInterface>;
10
+ export declare const enum JSEngines {
11
+ V8 = 0,
12
+ SPIDERMONKEY = 1,
13
+ JAVASCRIPT_CORE = 2,
14
+ UNKNOWN = 3
15
+ }
16
+ /**
17
+ * Detects the JavaScript engine based on the format of a stack trace line
18
+ *
19
+ * @param stack - The stack trace to analyze
20
+ * @returns The identified JavaScript engine type
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const engine = detectJSEngine("at functionName (/path/to/file.js:10:15)");
25
+ * if (engine === JSEngines.V8) {
26
+ * // Handle V8 specific logic
27
+ * }
28
+ * ```
29
+ *
30
+ * @since 2.1.0
31
+ */
32
+ export declare function detectJSEngine(stack: string): JSEngines;
33
+ /**
34
+ * Normalizes file paths from various formats to a standard format
35
+ *
36
+ * @param filePath - The file path to normalize, which may include protocol prefixes
37
+ * @returns A normalized file path with consistent separators and without protocol prefixes
38
+ *
39
+ * @remarks
40
+ * Handles both Windows and Unix-style paths, as well as file:// protocol URLs.
41
+ * Converts all backslashes to forward slashes for consistency.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * // Windows file URL to a normal path
46
+ * normalizePath("file:///C:/path/to/file.js"); // "C:/path/to/file.js"
47
+ *
48
+ * // Unix file URL to a normal path
49
+ * normalizePath("file:///path/to/file.js"); // "/path/to/file.js"
50
+ *
51
+ * // Windows backslashes to forward slashes
52
+ * normalizePath("C:\\path\\to\\file.js"); // "C:/path/to/file.js"
53
+ * ```
54
+ *
55
+ * @since 2.1.0
56
+ */
57
+ export declare function normalizePath(filePath: string): string;
58
+ /**
59
+ * Creates a default stack frame object with initial values
60
+ *
61
+ * @param source - The original source line from the stack trace
62
+ * @returns A new StackFrame object with default null values
63
+ *
64
+ * @see ParsedStackTrace
65
+ * @see StackFrame
66
+ *
67
+ * @since 2.1.0
68
+ */
69
+ export declare function createDefaultFrame(source: string): StackFrame;
70
+ /**
71
+ * Safely parses a string value to an integer, handling undefined and null cases
72
+ *
73
+ * @param value - The string value to parse
74
+ * @returns The parsed integer or null if the input is undefined/null
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * safeParseInt("42"); // 42
79
+ * safeParseInt(undefined); // null
80
+ * safeParseInt(null); // null
81
+ * ```
82
+ *
83
+ * @since 2.1.0
84
+ */
85
+ export declare function safeParseInt(value: string | undefined | null): number | null;
86
+ /**
87
+ * Parses a V8 JavaScript engine stack trace line into a structured StackFrame object
88
+ *
89
+ * @param line - The stack trace line to parse
90
+ * @returns A StackFrame object containing the parsed information
91
+ *
92
+ * @remarks
93
+ * Handles both standard V8 stack frames and eval-generated stack frames which
94
+ * have a more complex structure with nested origin information.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * // Standard frame
99
+ * parseV8StackLine("at functionName (/path/to/file.js:10:15)");
100
+ *
101
+ * // Eval frame
102
+ * parseV8StackLine("at eval (eval at evalFn (/source.js:5:10), <anonymous>:1:5)");
103
+ * ```
104
+ *
105
+ * @throws Error - If the line format doesn't match any known V8 pattern
106
+ *
107
+ * @see StackFrame
108
+ * @see createDefaultFrame
109
+ *
110
+ * @since 2.1.0
111
+ */
112
+ export declare function parseV8StackLine(line: string): StackFrame;
113
+ /**
114
+ * Parses a SpiderMonkey JavaScript engine stack trace line into a structured StackFrame object
115
+ *
116
+ * @param line - The stack trace line to parse
117
+ * @returns A StackFrame object containing the parsed information
118
+ *
119
+ * @remarks
120
+ * Handles both standard SpiderMonkey stack frames and eval/Function-generated stack frames
121
+ * which contain additional evaluation context information.
122
+ *
123
+ * @example
124
+ * ```ts
125
+ * // Standard frame
126
+ * parseSpiderMonkeyStackLine("functionName@/path/to/file.js:10:15");
127
+ *
128
+ * // Eval frame
129
+ * parseSpiderMonkeyStackLine("evalFn@/source.js line 5 > eval:1:5");
130
+ * ```
131
+ *
132
+ * @see StackFrame
133
+ * @see createDefaultFrame
134
+ *
135
+ * @since 2.1.0
136
+ */
137
+ export declare function parseSpiderMonkeyStackLine(line: string): StackFrame;
138
+ /**
139
+ * Parses a JavaScriptCore engine stack trace line into a structured StackFrame object
140
+ *
141
+ * @param line - The stack trace line to parse
142
+ * @returns A StackFrame object containing the parsed information
143
+ *
144
+ * @remarks
145
+ * Handles both standard JavaScriptCore stack frames and eval-generated stack frames.
146
+ * Special handling is provided for "global code" references and native code.
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * // Standard frame
151
+ * parseJavaScriptCoreStackLine("functionName@/path/to/file.js:10:15");
152
+ *
153
+ * // Eval frame
154
+ * parseJavaScriptCoreStackLine("eval code@");
155
+ * ```
156
+ *
157
+ * @see StackFrame
158
+ * @see createDefaultFrame
159
+ *
160
+ * @since 2.1.0
161
+ */
162
+ export declare function parseJavaScriptCoreStackLine(line: string): StackFrame;
163
+ /**
164
+ * Parses a stack trace line based on the detected JavaScript engine
165
+ *
166
+ * @param line - The stack trace line to parse
167
+ * @param engine - The JavaScript engine type that generated the stack trace
168
+ * @returns A StackFrame object containing the parsed information
169
+ *
170
+ * @remarks
171
+ * Delegates to the appropriate parsing function based on the JavaScript engine.
172
+ * Defaults to V8 parsing if the engine is unknown.
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * const engine = detectJSEngine(stackLine);
177
+ * const frame = parseStackLine(stackLine, engine);
178
+ * ```
179
+ *
180
+ * @see JSEngines
181
+ * @see parseV8StackLine
182
+ * @see parseSpiderMonkeyStackLine
183
+ * @see parseJavaScriptCoreStackLine
184
+ *
185
+ * @since 2.1.0
186
+ */
187
+ export declare function parseStackLine(line: string, engine: JSEngines): StackFrame;
188
+ /**
189
+ * Parses a complete error stack trace into a structured format
190
+ *
191
+ * @param error - Error object or error message string to parse
192
+ * @returns A ParsedStackTrace object containing structured stack trace information
193
+ *
194
+ * @remarks
195
+ * Automatically detects the JavaScript engine from the stack format.
196
+ * Filters out redundant information like the error name/message line.
197
+ * Handles both Error objects and string error messages.
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * try {
202
+ * throw new Error ("Something went wrong");
203
+ * } catch (error) {
204
+ * const parsedStack = parseErrorStack(error);
205
+ * console.log(parsedStack.name); // "Error"
206
+ * console.log(parsedStack.message); // "Something went wrong"
207
+ * console.log(parsedStack.stack); // Array of StackFrame objects
208
+ * }
209
+ * ```
210
+ *
211
+ * @see ParsedStackTrace
212
+ * @see StackFrame
213
+ * @see parseStackLine
214
+ * @see detectJSEngine
215
+ *
216
+ * @since 2.1.0
217
+ */
218
+ export declare function parseErrorStack(error: Error | string): ParsedStackTrace;
package/dist/esm/index.js CHANGED
@@ -1,8 +1,8 @@
1
- function $(u){let e=u.split(`
2
- `).slice(1),t=/^\s*at\s+(.*?)\s+\((.*?):(\d+):(\d+)\)$|^\s*at\s+(.*?):(\d+):(\d+)$/,n=/eval\s+at\s+([^\s(]+).+\((.+):(\d+):(\d+)\),\s(.+)/,i=[];return e.forEach(r=>{let o=r.match(t);if(!o)return;let a=o.slice(1);o[2]||(a=o.slice(4));let[c,l,d,g]=a,h=parseInt(d,10),m=parseInt(g,10);if(r.includes("eval")){let y=l.match(n)?.slice(1);if(y){let[N,w,E,A,M]=y;i.push({at:c,file:M,line:h,column:m,executor:{at:N,file:w,line:parseInt(E,10),column:parseInt(A,10)}});return}}i.push({at:c||"<anonymous>",file:l,line:h,column:m,executor:null})}),i}var x={},I="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");I.forEach((u,e)=>{x[u]=e});function T(u){let e=u<0,t="",n=e?(-u<<1)+1:u<<1;do{let i=n&31;n>>>=5,t+=I[i|(n>0?32:0)]}while(n>0);return t}function C(u){return u.map(T).join("")}function b(u){let e=[],t=0,n=0;for(let i=0;i<u.length;i++){let r=x[u[i]];if(r===void 0)throw new Error(`Invalid Base64 character: ${u[i]}`);let o=r&32;if(n+=(r&31)<<t,o)t+=5;else{let a=(n&1)===1,c=n>>1;e.push(a?-c:c),n=t=0}}return e}function K(u,e={}){let t=u.split(`
3
- `),n=e.padding??10,i=e.startLine??0;return t.map((r,o)=>{let a=o+i+1,l=`${`${a} | `.padStart(n)}${r}`;return e.action&&a===e.action.triggerLine?e.action.callback(l,n,a):l}).join(`
4
- `)}function F(u,e){let{code:t,line:n,column:i,startLine:r}=u;if(n<r||i<1)throw new Error("Invalid line or column number.");return K(t,{startLine:r,action:{triggerLine:n,callback:(o,a,c)=>{let l="^",d=a-1,g=">";e&&(l=`${e.color}${l}${e.reset}`,d+=e.color.length+e.reset.length,g=`${e.color}>${e.reset}`);let h=" | ".padStart(a)+" ".repeat(i-1)+`${l}`;return o=`${g} ${c} |`.padStart(d)+o.split("|")[1],o+`
5
- ${h}`}}})}import*as s from"typescript";import{SyntaxKind as p}from"typescript";var O=(m=>(m.reset="\x1B[0m",m.gray="\x1B[38;5;243m",m.darkGray="\x1B[38;5;238m",m.lightCoral="\x1B[38;5;203m",m.lightOrange="\x1B[38;5;215m",m.oliveGreen="\x1B[38;5;149m",m.burntOrange="\x1B[38;5;208m",m.lightGoldenrodYellow="\x1B[38;5;221m",m.lightYellow="\x1B[38;5;230m",m.canaryYellow="\x1B[38;5;227m",m.deepOrange="\x1B[38;5;166m",m.lightGray="\x1B[38;5;252m",m.brightPink="\x1B[38;5;197m",m))(O||{}),L={enumColor:"\x1B[38;5;208m",typeColor:"\x1B[38;5;221m",classColor:"\x1B[38;5;215m",stringColor:"\x1B[38;5;149m",keywordColor:"\x1B[38;5;203m",commentColor:"\x1B[38;5;238m",functionColor:"\x1B[38;5;215m",variableColor:"\x1B[38;5;208m",interfaceColor:"\x1B[38;5;221m",parameterColor:"\x1B[38;5;166m",getAccessorColor:"\x1B[38;5;230m",numericLiteralColor:"\x1B[38;5;252m",methodSignatureColor:"\x1B[38;5;208m",regularExpressionColor:"\x1B[38;5;149m",propertyAssignmentColor:"\x1B[38;5;227m",propertyAccessExpressionColor:"\x1B[38;5;230m",expressionWithTypeArgumentsColor:"\x1B[38;5;215m"},S=class{constructor(e,t,n){this.sourceFile=e;this.code=t;this.schema=n}segments=new Map;parseNode(e){this.processComments(e),this.processKeywords(e),this.processNode(e)}highlight(){let e=0,t,n=[];return Array.from(this.segments.values()).sort((r,o)=>r.start-o.start||r.end-o.end).forEach(r=>{if(t&&r.start<t.end){let o=n.pop();if(!o)return;let a=this.getSegmentSource(r.start,r.end),c=`${r.color}${a}${t.color}`;n.push(o.replace(a,c));return}n.push(this.getSegmentSource(e,r.start)),n.push(`${r.color}${this.getSegmentSource(r.start,r.end)}${r.reset}`),e=r.end,t=r}),n.join("")+this.getSegmentSource(e)}getSegmentSource(e,t){return this.code.slice(e,t)}addSegment(e,t,n,i="\x1B[0m"){let r=`${e}-${t}`;this.segments.set(r,{start:e,end:t,color:n,reset:i})}processComments(e){[...s.getTrailingCommentRanges(this.sourceFile.getFullText(),e.getFullStart())||[],...s.getLeadingCommentRanges(this.sourceFile.getFullText(),e.getFullStart())||[]].forEach(n=>this.addSegment(n.pos,n.end,this.schema.commentColor))}processKeywords(e){if([p.NullKeyword,p.VoidKeyword,p.StringKeyword,p.NumberKeyword,p.BooleanKeyword,p.UndefinedKeyword].includes(e.kind))return this.addSegment(e.getStart(),e.getEnd(),this.schema.typeColor);e&&e.kind>=s.SyntaxKind.FirstKeyword&&e.kind<=s.SyntaxKind.LastKeyword&&this.addSegment(e.getStart(),e.getEnd(),this.schema.keywordColor)}processIdentifier(e){let t=e.getEnd(),n=e.getStart();switch(e.parent.kind){case s.SyntaxKind.EnumMember:return this.addSegment(n,t,this.schema.enumColor);case s.SyntaxKind.CallExpression:case s.SyntaxKind.EnumDeclaration:case s.SyntaxKind.PropertySignature:case s.SyntaxKind.ModuleDeclaration:return this.addSegment(n,t,this.schema.variableColor);case s.SyntaxKind.InterfaceDeclaration:return this.addSegment(n,t,this.schema.interfaceColor);case s.SyntaxKind.GetAccessor:return this.addSegment(n,t,this.schema.getAccessorColor);case s.SyntaxKind.PropertyAssignment:return this.addSegment(n,t,this.schema.propertyAssignmentColor);case s.SyntaxKind.MethodSignature:return this.addSegment(n,t,this.schema.methodSignatureColor);case s.SyntaxKind.MethodDeclaration:case s.SyntaxKind.FunctionDeclaration:return this.addSegment(n,t,this.schema.functionColor);case s.SyntaxKind.ClassDeclaration:return this.addSegment(n,t,this.schema.classColor);case s.SyntaxKind.Parameter:return this.addSegment(n,t,this.schema.parameterColor);case s.SyntaxKind.VariableDeclaration:return this.addSegment(n,t,this.schema.variableColor);case s.SyntaxKind.PropertyDeclaration:return this.addSegment(n,t,this.schema.variableColor);case s.SyntaxKind.PropertyAccessExpression:return e.parent.getChildAt(0).getText()===e.getText()?this.addSegment(n,t,this.schema.variableColor):this.addSegment(n,t,this.schema.propertyAccessExpressionColor);case s.SyntaxKind.ExpressionWithTypeArguments:return this.addSegment(n,t,this.schema.expressionWithTypeArgumentsColor);case s.SyntaxKind.BreakStatement:case s.SyntaxKind.ShorthandPropertyAssignment:case s.SyntaxKind.BindingElement:return this.addSegment(n,t,this.schema.variableColor);case s.SyntaxKind.BinaryExpression:case s.SyntaxKind.SwitchStatement:case s.SyntaxKind.TemplateSpan:return this.addSegment(n,t,this.schema.variableColor);case s.SyntaxKind.TypeReference:case s.SyntaxKind.TypeAliasDeclaration:return this.addSegment(n,t,this.schema.typeColor);case s.SyntaxKind.NewExpression:return this.addSegment(n,t,this.schema.variableColor)}}processTemplateExpression(e){let t=e.head.getStart(),n=e.head.getEnd();this.addSegment(t,n,this.schema.stringColor),e.templateSpans.forEach(i=>{let r=i.literal.getStart(),o=i.literal.getEnd();this.addSegment(r,o,this.schema.stringColor)})}processNode(e){let t=e.getStart(),n=e.getEnd();switch(e.kind){case s.SyntaxKind.TypeParameter:return this.addSegment(t,t+e.name.text.length,this.schema.typeColor);case s.SyntaxKind.TypeReference:return this.addSegment(t,n,this.schema.typeColor);case s.SyntaxKind.StringLiteral:case s.SyntaxKind.NoSubstitutionTemplateLiteral:return this.addSegment(t,n,this.schema.stringColor);case s.SyntaxKind.RegularExpressionLiteral:return this.addSegment(t,n,this.schema.regularExpressionColor);case s.SyntaxKind.TemplateExpression:return this.processTemplateExpression(e);case s.SyntaxKind.Identifier:return this.processIdentifier(e);case s.SyntaxKind.BigIntLiteral:case s.SyntaxKind.NumericLiteral:return this.addSegment(t,n,this.schema.numericLiteralColor)}}};function D(u,e={}){let t=s.createSourceFile("temp.ts",u,s.ScriptTarget.Latest,!0,s.ScriptKind.TS),n=new S(t,u,Object.assign(L,e));function i(r){n.parseNode(r);for(let o=0;o<r.getChildCount();o++)i(r.getChildAt(o))}return s.forEachChild(t,i),n.highlight()}var f=class u{mapping=[];constructor(e,t=0,n=0){e=e instanceof u?e.mapping:e,Array.isArray(e)?this.decodeMappingArray(e,t,n):this.decodeMappingString(e,t,n)}encode(){return this.encodeMappings(this.mapping)}decode(e,t=0,n=0){e=e instanceof u?e.mapping:e,Array.isArray(e)?this.decodeMappingArray(e,t,n):this.decodeMappingString(e,t,n)}getSegment(e,t,n=0){let i=this.mapping[e-1];if(!i||i.length===0)return null;let r=0,o=i.length-1,a=null;for(;r<=o;){let c=Math.floor((r+o)/2),l=i[c];if(l.generatedColumn<t)r=c+1,a=n===1?l:a;else if(l.generatedColumn>t)o=c-1,a=n===2?l:a;else return l}return a}getOriginalSegment(e,t,n,i=0){let r=null;for(let o of this.mapping){if(!o)continue;let a=0,c=o.length-1;for(;a<=c;){let l=Math.floor((a+c)/2),d=o[l];if(d.sourceIndex<n||d.line<e)a=l+1;else if(d.sourceIndex>n||d.line>e)c=l-1;else if(d.column<t)a=l+1,r=i===1?d:r;else if(d.column>t)c=l-1,r=i===2?d:r;else return d}}return r}initPositionOffsets(e=0,t=0){return{line:0,column:0,nameIndex:e,sourceIndex:t,generatedLine:0,generatedColumn:0}}validateMappingString(e){return/^[a-zA-Z0-9+/,;]+$/.test(e)}validateSegment(e){if(!Number.isFinite(e.line))throw new Error(`Invalid segment: line must be a finite number, received ${e.line}`);if(!Number.isFinite(e.column))throw new Error(`Invalid segment: column must be a finite number, received ${e.column}`);if(e.nameIndex!==null&&!Number.isFinite(e.nameIndex))throw new Error(`Invalid segment: nameIndex must be a number or null, received ${e.nameIndex}`);if(!Number.isFinite(e.sourceIndex))throw new Error(`Invalid segment: sourceIndex must be a finite number, received ${e.sourceIndex}`);if(!Number.isFinite(e.generatedLine))throw new Error(`Invalid segment: generatedLine must be a finite number, received ${e.generatedLine}`);if(!Number.isFinite(e.generatedColumn))throw new Error(`Invalid segment: generatedColumn must be a finite number, received ${e.generatedColumn}`)}encodeSegment(e,t){let{line:n,column:i,generatedColumn:r,nameIndex:o,sourceIndex:a}=t,c=n-1,l=i-1,d=r-1,g=[d-e.generatedColumn,a!==e.sourceIndex?a-e.sourceIndex:0,c-e.line,l-e.column];return o!=null&&(g[4]=o-e.nameIndex,e.nameIndex=o),e.line=c,e.column=l,e.generatedColumn=d,e.sourceIndex=a,C(g)}encodeMappings(e){let t=this.initPositionOffsets();return e.map(n=>n?(t.generatedColumn=0,n.map(r=>this.encodeSegment(t,r)).join(",")):"").join(";")}decodedSegment(e,t){let[n,i,r,o,a]=t;return e.line+=r,e.column+=o,e.nameIndex+=a??0,e.sourceIndex+=i,e.generatedColumn+=n,{line:e.line+1,column:e.column+1,nameIndex:a!==void 0?e.nameIndex:null,sourceIndex:e.sourceIndex,generatedLine:e.generatedLine+1,generatedColumn:e.generatedColumn+1}}decodeMappingString(e,t,n){if(!this.validateMappingString(e))throw new Error("Invalid Mappings string format: the provided string does not conform to expected VLQ format.");let i=e.split(";"),r=this.mapping.length,o=this.initPositionOffsets(t,n);try{i.forEach((a,c)=>{if(!a){this.mapping.push(null);return}o.generatedColumn=0,o.generatedLine=r+c;let l=a.split(",").map(d=>this.decodedSegment(o,b(d)));this.mapping.push(l)})}catch(a){throw new Error(`Error decoding mappings at frame index ${i.length}: ${a.message}`)}}decodeMappingArray(e,t,n){let i=this.mapping.length;if(!Array.isArray(e))throw new Error("Invalid encoded map: expected an array of frames.");try{e.forEach((r,o)=>{if(!r){this.mapping.push(r);return}if(!Array.isArray(r))throw new Error(`Invalid Mappings array format at frame index ${o}: expected an array, received ${typeof r}.`);let a=r.map(c=>(this.validateSegment(c),{...c,nameIndex:typeof c.nameIndex=="number"?c.nameIndex+t:null,sourceIndex:c.sourceIndex+n,generatedLine:c.generatedLine+i}));this.mapping.push(a)})}catch(r){let o=r instanceof Error?r.message:"Unknown error";throw new Error(`Error decoding mappings: ${o}`)}}};var v=class u{file;mappings;sourceRoot;names;sources;sourcesContent;constructor(e,t=null){typeof e=="string"&&(e=JSON.parse(e)),e=e,this.validateSourceMap(e),this.file=e.file??t,this.names=[...e.names??[]],this.sources=[...e.sources??[]],this.sourceRoot=e.sourceRoot??null,this.sourcesContent=e.sourcesContent?[...e.sourcesContent]:[],this.mappings=new f(e.mappings)}getMapObject(){let e={version:3,names:this.names,sources:this.sources,mappings:this.mappings.encode(),sourcesContent:this.sourcesContent};return this.file&&(e.file=this.file),this.sourceRoot&&(e.sourceRoot=this.sourceRoot),e}concat(...e){if(e.length<1)throw new Error("At least one map must be provided for concatenation.");for(let t of e)this.mappings.decode(t.mappings,this.names.length,this.sources.length),this.names.push(...t.names),this.sources.push(...t.sources),this.sourcesContent.push(...t.sourcesContent??[])}concatNewMap(...e){if(e.length<1)throw new Error("At least one map must be provided for concatenation.");let t=new u(this);for(let n of e)t.mappings.decode(n.mappings,t.names.length,t.sources.length),t.names.push(...n.names),t.sources.push(...n.sources),t.sourcesContent.push(...n.sourcesContent??[]);return t}getPositionByOriginal(e,t,n,i=0){let r=n;if(typeof n=="string"&&(r=this.sources.findIndex(a=>a.includes(n))),r<0)return null;let o=this.mappings.getOriginalSegment(e,t,r,i);return o?{name:this.names[o.nameIndex??-1]??null,line:o.line,column:o.column,source:this.sources[o.sourceIndex],sourceRoot:this.sourceRoot,sourceIndex:o.sourceIndex,generatedLine:o.generatedLine,generatedColumn:o.generatedColumn}:null}getPosition(e,t,n=0){let i=this.mappings.getSegment(e,t,n);return i?{name:this.names[i.nameIndex??-1]??null,line:i.line,column:i.column,source:this.sources[i.sourceIndex],sourceRoot:this.sourceRoot,sourceIndex:i.sourceIndex,generatedLine:i.generatedLine,generatedColumn:i.generatedColumn}:null}getPositionWithContent(e,t,n=0){let i=this.getPosition(e,t,n);return i?{...i,sourcesContent:this.sourcesContent[i.sourceIndex]}:null}getPositionWithCode(e,t,n=0,i){let r=this.getPosition(e,t,n);if(!r||!this.sourcesContent[r.sourceIndex])return null;let o=Object.assign({linesAfter:4,linesBefore:3},i),a=this.sourcesContent[r.sourceIndex].split(`
6
- `),c=Math.min((r.line??1)+o.linesAfter,a.length),l=Math.max((r.line??1)-o.linesBefore,0),d=a.slice(l,Math.min(c+1,a.length)).join(`
7
- `);return{...r,code:d,endLine:c,startLine:l}}toString(){return JSON.stringify(this.getMapObject())}validateSourceMap(e){if(!["sources","mappings","names"].every(n=>n in e))throw new Error("Missing required keys in SourceMap.")}};export{S as CodeHighlighter,O as Colors,v as SourceService,b as decodeVLQ,C as encodeArrayVLQ,T as encodeVLQ,K as formatCode,F as formatErrorCode,D as highlightCode,$ as parseErrorStack};
1
+ var S={V8:{STANDARD:/at\s+(?:([^(]+?)\s+)?\(?(?:(.+?):(\d+):(\d+)|(native))\)?/,EVAL:/^at\s(.+?)\s\(eval\sat\s(.+?)\s?\((.*):(\d+):(\d+)\),\s(.+?):(\d+):(\d+)\)$/},SPIDERMONKEY:{EVAL:/^(.*)@(.+?):(\d+):(\d+),\s(.+?)@(.+?):(\d+):(\d+)$/,STANDARD:/^(.*)@(.*?)(?:(\[native code\])|:(\d+):(\d+))$/},JAVASCRIPT_CORE:{STANDARD:/^(?:(global|eval)\s)?(.*)@(.*?)(?::(\d+)(?::(\d+))?)?$/}},w=(i=>(i[i.V8=0]="V8",i[i.SPIDERMONKEY=1]="SPIDERMONKEY",i[i.JAVASCRIPT_CORE=2]="JAVASCRIPT_CORE",i[i.UNKNOWN=3]="UNKNOWN",i))(w||{});function T(o){return o.startsWith(" at ")||o.startsWith("at ")?0:o.includes("@")?/(?:global|eval) code@/.test(o)?2:1:3}function p(o){return o.startsWith("file://")?o.startsWith("file:///")&&/^file:\/\/\/[A-Za-z]:/.test(o)?o.substring(8):o.substring(7):(o=o.replace(/\\/g,"/"),o)}function y(o){return{source:o,isEval:!1,fileName:null,lineNumber:null,columnNumber:null,functionName:null}}function d(o){return o&&o.trim()!==""?parseInt(o,10):null}function M(o){let e=y(o),t=o.match(S.V8.EVAL);if(t)return e.isEval=!0,e.functionName=t[1]||"<anonymous>",e.evalOrigin={fileName:p(t[3])||null,lineNumber:d(t[4]),columnNumber:d(t[5]),functionName:t[2]||"<anonymous>"},e.fileName=p(t[6])||"<anonymous>",e.lineNumber=d(t[7]),e.columnNumber=d(t[8]),e;let n=o.match(S.V8.STANDARD);return n&&(e.functionName=n[1]?n[1].trim():null,n[5]==="native"?e.fileName="[native code]":(e.fileName=p(n[2])||null,e.lineNumber=d(n[3]),e.columnNumber=d(n[4]))),e}function O(o){let e=y(o),t=o.match(S.SPIDERMONKEY.EVAL);if(t)return e.isEval=!0,e.functionName=t[1]||null,e.evalOrigin={fileName:p(t[6])||null,lineNumber:d(t[7]),columnNumber:d(t[8]),functionName:t[5]},e.lineNumber=d(t[3]),e.columnNumber=d(t[4]),e;let n=o.match(S.SPIDERMONKEY.STANDARD);return n&&(e.functionName=n[1]||null,e.fileName=p(n[2])||null,n[3]==="[native code]"?e.fileName="[native code]":(e.lineNumber=d(n[4]),e.columnNumber=d(n[5]))),e}function K(o){let e=y(o),t=o.match(S.JAVASCRIPT_CORE.STANDARD);if(t){let n=t[2];e.functionName=n&&n!=="global code"?n:null,e.isEval=t[1]==="eval"||t[3]==="eval",t[3]==="[native code]"?e.fileName="[native code]":(e.fileName=p(t[3])||null,e.lineNumber=d(t[4]),e.columnNumber=d(t[5]))}return e}function L(o,e){switch(e){case 1:return O(o);case 2:return K(o);case 0:default:return M(o)}}function F(o){let e=typeof o=="string"?new Error(o):o,t=e.stack||"",n=e.message||"",i=e.name||"Error",r=T(t),s=t.split(`
2
+ `).map(a=>a.trim()).filter(a=>!a.includes(`${i}: ${n}`)).filter(a=>a.trim()!=="");return{name:i,message:n,stack:s.map(a=>L(a,r)),rawStack:t}}var C={},b="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");b.forEach((o,e)=>{C[o]=e});function P(o){let e=o<0,t="",n=e?(-o<<1)+1:o<<1;do{let i=n&31;n>>>=5,t+=b[i|(n>0?32:0)]}while(n>0);return t}function N(o){return o.map(P).join("")}function v(o){let e=[],t=0,n=0;for(let i=0;i<o.length;i++){let r=C[o[i]];if(r===void 0)throw new Error(`Invalid Base64 character: ${o[i]}`);let s=r&32;if(n+=(r&31)<<t,s)t+=5;else{let a=(n&1)===1,l=n>>1;e.push(a?-l:l),n=t=0}}return e}function R(o,e={}){let t=o.split(`
3
+ `),n=e.padding??10,i=e.startLine??0;return t.map((r,s)=>{let a=s+i+1,u=`${`${a} | `.padStart(n)}${r}`;return e.action&&a===e.action.triggerLine?e.action.callback(u,n,a):u}).join(`
4
+ `)}function U(o,e){let{code:t,line:n,column:i,startLine:r}=o;if(n<r||i<1)throw new Error("Invalid line or column number.");return R(t,{startLine:r,action:{triggerLine:n,callback:(s,a,l)=>{let u="^",m=a-1,f=">";e&&(u=`${e.color}${u}${e.reset}`,m+=e.color.length+e.reset.length,f=`${e.color}>${e.reset}`);let A=" | ".padStart(a)+" ".repeat(i-1)+`${u}`;return s=`${f} ${l} |`.padStart(m)+s.split("|")[1],s+`
5
+ ${A}`}}})}import*as c from"typescript";import{SyntaxKind as h}from"typescript";var D=(g=>(g.reset="\x1B[0m",g.gray="\x1B[38;5;243m",g.darkGray="\x1B[38;5;238m",g.lightCoral="\x1B[38;5;203m",g.lightOrange="\x1B[38;5;215m",g.oliveGreen="\x1B[38;5;149m",g.burntOrange="\x1B[38;5;208m",g.lightGoldenrodYellow="\x1B[38;5;221m",g.lightYellow="\x1B[38;5;230m",g.canaryYellow="\x1B[38;5;227m",g.deepOrange="\x1B[38;5;166m",g.lightGray="\x1B[38;5;252m",g.brightPink="\x1B[38;5;197m",g))(D||{}),$={enumColor:"\x1B[38;5;208m",typeColor:"\x1B[38;5;221m",classColor:"\x1B[38;5;215m",stringColor:"\x1B[38;5;149m",keywordColor:"\x1B[38;5;203m",commentColor:"\x1B[38;5;238m",functionColor:"\x1B[38;5;215m",variableColor:"\x1B[38;5;208m",interfaceColor:"\x1B[38;5;221m",parameterColor:"\x1B[38;5;166m",getAccessorColor:"\x1B[38;5;230m",numericLiteralColor:"\x1B[38;5;252m",methodSignatureColor:"\x1B[38;5;208m",regularExpressionColor:"\x1B[38;5;149m",propertyAssignmentColor:"\x1B[38;5;227m",propertyAccessExpressionColor:"\x1B[38;5;230m",expressionWithTypeArgumentsColor:"\x1B[38;5;215m"},I=class{constructor(e,t,n){this.sourceFile=e;this.code=t;this.schema=n}segments=new Map;parseNode(e){this.processComments(e),this.processKeywords(e),this.processNode(e)}highlight(){let e=0,t,n=[];return Array.from(this.segments.values()).sort((r,s)=>r.start-s.start||r.end-s.end).forEach(r=>{if(t&&r.start<t.end){let s=n.pop();if(!s)return;let a=this.getSegmentSource(r.start,r.end),l=`${r.color}${a}${t.color}`;n.push(s.replace(a,l));return}n.push(this.getSegmentSource(e,r.start)),n.push(`${r.color}${this.getSegmentSource(r.start,r.end)}${r.reset}`),e=r.end,t=r}),n.join("")+this.getSegmentSource(e)}getSegmentSource(e,t){return this.code.slice(e,t)}addSegment(e,t,n,i="\x1B[0m"){let r=`${e}-${t}`;this.segments.set(r,{start:e,end:t,color:n,reset:i})}processComments(e){[...c.getTrailingCommentRanges(this.sourceFile.getFullText(),e.getFullStart())||[],...c.getLeadingCommentRanges(this.sourceFile.getFullText(),e.getFullStart())||[]].forEach(n=>this.addSegment(n.pos,n.end,this.schema.commentColor))}processKeywords(e){if([h.NullKeyword,h.VoidKeyword,h.StringKeyword,h.NumberKeyword,h.BooleanKeyword,h.UndefinedKeyword].includes(e.kind))return this.addSegment(e.getStart(),e.getEnd(),this.schema.typeColor);e&&e.kind>=c.SyntaxKind.FirstKeyword&&e.kind<=c.SyntaxKind.LastKeyword&&this.addSegment(e.getStart(),e.getEnd(),this.schema.keywordColor)}processIdentifier(e){let t=e.getEnd(),n=e.getStart();switch(e.parent.kind){case c.SyntaxKind.EnumMember:return this.addSegment(n,t,this.schema.enumColor);case c.SyntaxKind.CallExpression:case c.SyntaxKind.EnumDeclaration:case c.SyntaxKind.PropertySignature:case c.SyntaxKind.ModuleDeclaration:return this.addSegment(n,t,this.schema.variableColor);case c.SyntaxKind.InterfaceDeclaration:return this.addSegment(n,t,this.schema.interfaceColor);case c.SyntaxKind.GetAccessor:return this.addSegment(n,t,this.schema.getAccessorColor);case c.SyntaxKind.PropertyAssignment:return this.addSegment(n,t,this.schema.propertyAssignmentColor);case c.SyntaxKind.MethodSignature:return this.addSegment(n,t,this.schema.methodSignatureColor);case c.SyntaxKind.MethodDeclaration:case c.SyntaxKind.FunctionDeclaration:return this.addSegment(n,t,this.schema.functionColor);case c.SyntaxKind.ClassDeclaration:return this.addSegment(n,t,this.schema.classColor);case c.SyntaxKind.Parameter:return this.addSegment(n,t,this.schema.parameterColor);case c.SyntaxKind.VariableDeclaration:return this.addSegment(n,t,this.schema.variableColor);case c.SyntaxKind.PropertyDeclaration:return this.addSegment(n,t,this.schema.variableColor);case c.SyntaxKind.PropertyAccessExpression:return e.parent.getChildAt(0).getText()===e.getText()?this.addSegment(n,t,this.schema.variableColor):this.addSegment(n,t,this.schema.propertyAccessExpressionColor);case c.SyntaxKind.ExpressionWithTypeArguments:return this.addSegment(n,t,this.schema.expressionWithTypeArgumentsColor);case c.SyntaxKind.BreakStatement:case c.SyntaxKind.ShorthandPropertyAssignment:case c.SyntaxKind.BindingElement:return this.addSegment(n,t,this.schema.variableColor);case c.SyntaxKind.BinaryExpression:case c.SyntaxKind.SwitchStatement:case c.SyntaxKind.TemplateSpan:return this.addSegment(n,t,this.schema.variableColor);case c.SyntaxKind.TypeReference:case c.SyntaxKind.TypeAliasDeclaration:return this.addSegment(n,t,this.schema.typeColor);case c.SyntaxKind.NewExpression:return this.addSegment(n,t,this.schema.variableColor)}}processTemplateExpression(e){let t=e.head.getStart(),n=e.head.getEnd();this.addSegment(t,n,this.schema.stringColor),e.templateSpans.forEach(i=>{let r=i.literal.getStart(),s=i.literal.getEnd();this.addSegment(r,s,this.schema.stringColor)})}processNode(e){let t=e.getStart(),n=e.getEnd();switch(e.kind){case c.SyntaxKind.TypeParameter:return this.addSegment(t,t+e.name.text.length,this.schema.typeColor);case c.SyntaxKind.TypeReference:return this.addSegment(t,n,this.schema.typeColor);case c.SyntaxKind.StringLiteral:case c.SyntaxKind.NoSubstitutionTemplateLiteral:return this.addSegment(t,n,this.schema.stringColor);case c.SyntaxKind.RegularExpressionLiteral:return this.addSegment(t,n,this.schema.regularExpressionColor);case c.SyntaxKind.TemplateExpression:return this.processTemplateExpression(e);case c.SyntaxKind.Identifier:return this.processIdentifier(e);case c.SyntaxKind.BigIntLiteral:case c.SyntaxKind.NumericLiteral:return this.addSegment(t,n,this.schema.numericLiteralColor)}}};function j(o,e={}){let t=c.createSourceFile("temp.ts",o,c.ScriptTarget.Latest,!0,c.ScriptKind.TS),n=new I(t,o,Object.assign($,e));function i(r){n.parseNode(r);for(let s=0;s<r.getChildCount();s++)i(r.getChildAt(s))}return c.forEachChild(t,i),n.highlight()}var x=class o{mapping=[];constructor(e,t=0,n=0){e=e instanceof o?e.mapping:e,Array.isArray(e)?this.decodeMappingArray(e,t,n):this.decodeMappingString(e,t,n)}encode(){return this.encodeMappings(this.mapping)}decode(e,t=0,n=0){e=e instanceof o?e.mapping:e,Array.isArray(e)?this.decodeMappingArray(e,t,n):this.decodeMappingString(e,t,n)}getSegment(e,t,n=0){let i=this.mapping[e-1];if(!i||i.length===0)return null;let r=0,s=i.length-1,a=null;for(;r<=s;){let l=Math.floor((r+s)/2),u=i[l];if(u.generatedColumn<t)r=l+1,a=n===1?u:a;else if(u.generatedColumn>t)s=l-1,a=n===2?u:a;else return u}return a}getOriginalSegment(e,t,n,i=0){let r=null;for(let s of this.mapping){if(!s)continue;let a=0,l=s.length-1;for(;a<=l;){let u=Math.floor((a+l)/2),m=s[u];if(m.sourceIndex<n||m.line<e)a=u+1;else if(m.sourceIndex>n||m.line>e)l=u-1;else if(m.column<t)a=u+1,r=i===1?m:r;else if(m.column>t)l=u-1,r=i===2?m:r;else return m}}return r}initPositionOffsets(e=0,t=0){return{line:0,column:0,nameIndex:e,sourceIndex:t,generatedLine:0,generatedColumn:0}}validateMappingString(e){return/^[a-zA-Z0-9+/,;]+$/.test(e)}validateSegment(e){if(!Number.isFinite(e.line))throw new Error(`Invalid segment: line must be a finite number, received ${e.line}`);if(!Number.isFinite(e.column))throw new Error(`Invalid segment: column must be a finite number, received ${e.column}`);if(e.nameIndex!==null&&!Number.isFinite(e.nameIndex))throw new Error(`Invalid segment: nameIndex must be a number or null, received ${e.nameIndex}`);if(!Number.isFinite(e.sourceIndex))throw new Error(`Invalid segment: sourceIndex must be a finite number, received ${e.sourceIndex}`);if(!Number.isFinite(e.generatedLine))throw new Error(`Invalid segment: generatedLine must be a finite number, received ${e.generatedLine}`);if(!Number.isFinite(e.generatedColumn))throw new Error(`Invalid segment: generatedColumn must be a finite number, received ${e.generatedColumn}`)}encodeSegment(e,t){let{line:n,column:i,generatedColumn:r,nameIndex:s,sourceIndex:a}=t,l=n-1,u=i-1,m=r-1,f=[m-e.generatedColumn,a!==e.sourceIndex?a-e.sourceIndex:0,l-e.line,u-e.column];return s!=null&&(f[4]=s-e.nameIndex,e.nameIndex=s),e.line=l,e.column=u,e.generatedColumn=m,e.sourceIndex=a,N(f)}encodeMappings(e){let t=this.initPositionOffsets();return e.map(n=>n?(t.generatedColumn=0,n.map(r=>this.encodeSegment(t,r)).join(",")):"").join(";")}decodedSegment(e,t){let[n,i,r,s,a]=t;return e.line+=r,e.column+=s,e.nameIndex+=a??0,e.sourceIndex+=i,e.generatedColumn+=n,{line:e.line+1,column:e.column+1,nameIndex:a!==void 0?e.nameIndex:null,sourceIndex:e.sourceIndex,generatedLine:e.generatedLine+1,generatedColumn:e.generatedColumn+1}}decodeMappingString(e,t,n){if(!this.validateMappingString(e))throw new Error("Invalid Mappings string format: the provided string does not conform to expected VLQ format.");let i=e.split(";"),r=this.mapping.length,s=this.initPositionOffsets(t,n);try{i.forEach((a,l)=>{if(!a){this.mapping.push(null);return}s.generatedColumn=0,s.generatedLine=r+l;let u=a.split(",").map(m=>this.decodedSegment(s,v(m)));this.mapping.push(u)})}catch(a){throw new Error(`Error decoding mappings at frame index ${i.length}: ${a.message}`)}}decodeMappingArray(e,t,n){let i=this.mapping.length;if(!Array.isArray(e))throw new Error("Invalid encoded map: expected an array of frames.");try{e.forEach((r,s)=>{if(!r){this.mapping.push(r);return}if(!Array.isArray(r))throw new Error(`Invalid Mappings array format at frame index ${s}: expected an array, received ${typeof r}.`);let a=r.map(l=>(this.validateSegment(l),{...l,nameIndex:typeof l.nameIndex=="number"?l.nameIndex+t:null,sourceIndex:l.sourceIndex+n,generatedLine:l.generatedLine+i}));this.mapping.push(a)})}catch(r){let s=r instanceof Error?r.message:"Unknown error";throw new Error(`Error decoding mappings: ${s}`)}}};var E=class o{file;mappings;sourceRoot;names;sources;sourcesContent;constructor(e,t=null){typeof e=="string"&&(e=JSON.parse(e)),e=e,this.validateSourceMap(e),this.file=e.file??t,this.names=[...e.names??[]],this.sources=[...e.sources??[]],this.sourceRoot=e.sourceRoot??null,this.sourcesContent=e.sourcesContent?[...e.sourcesContent]:[],this.mappings=new x(e.mappings)}getMapObject(){let e={version:3,names:this.names,sources:this.sources,mappings:this.mappings.encode(),sourcesContent:this.sourcesContent};return this.file&&(e.file=this.file),this.sourceRoot&&(e.sourceRoot=this.sourceRoot),e}concat(...e){if(e.length<1)throw new Error("At least one map must be provided for concatenation.");for(let t of e)this.mappings.decode(t.mappings,this.names.length,this.sources.length),this.names.push(...t.names),this.sources.push(...t.sources),this.sourcesContent.push(...t.sourcesContent??[])}concatNewMap(...e){if(e.length<1)throw new Error("At least one map must be provided for concatenation.");let t=new o(this);for(let n of e)t.mappings.decode(n.mappings,t.names.length,t.sources.length),t.names.push(...n.names),t.sources.push(...n.sources),t.sourcesContent.push(...n.sourcesContent??[]);return t}getPositionByOriginal(e,t,n,i=0){let r=n;if(typeof n=="string"&&(r=this.sources.findIndex(a=>a.includes(n))),r<0)return null;let s=this.mappings.getOriginalSegment(e,t,r,i);return s?{name:this.names[s.nameIndex??-1]??null,line:s.line,column:s.column,source:this.sources[s.sourceIndex],sourceRoot:this.sourceRoot,sourceIndex:s.sourceIndex,generatedLine:s.generatedLine,generatedColumn:s.generatedColumn}:null}getPosition(e,t,n=0){let i=this.mappings.getSegment(e,t,n);return i?{name:this.names[i.nameIndex??-1]??null,line:i.line,column:i.column,source:this.sources[i.sourceIndex],sourceRoot:this.sourceRoot,sourceIndex:i.sourceIndex,generatedLine:i.generatedLine,generatedColumn:i.generatedColumn}:null}getPositionWithContent(e,t,n=0){let i=this.getPosition(e,t,n);return i?{...i,sourcesContent:this.sourcesContent[i.sourceIndex]}:null}getPositionWithCode(e,t,n=0,i){let r=this.getPosition(e,t,n);if(!r||!this.sourcesContent[r.sourceIndex])return null;let s=Object.assign({linesAfter:4,linesBefore:3},i),a=this.sourcesContent[r.sourceIndex].split(`
6
+ `),l=Math.min((r.line??1)+s.linesAfter,a.length),u=Math.max((r.line??1)-s.linesBefore,0),m=a.slice(u,Math.min(l+1,a.length)).join(`
7
+ `);return{...r,code:m,endLine:l,startLine:u}}toString(){return JSON.stringify(this.getMapObject())}validateSourceMap(e){if(!["sources","mappings","names"].every(n=>n in e))throw new Error("Missing required keys in SourceMap.")}};export{I as CodeHighlighter,D as Colors,w as JSEngines,E as SourceService,y as createDefaultFrame,v as decodeVLQ,T as detectJSEngine,N as encodeArrayVLQ,P as encodeVLQ,R as formatCode,U as formatErrorCode,j as highlightCode,p as normalizePath,F as parseErrorStack,K as parseJavaScriptCoreStackLine,O as parseSpiderMonkeyStackLine,L as parseStackLine,M as parseV8StackLine,d as safeParseInt};
8
8
  //# sourceMappingURL=index.js.map