autotel 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.
- package/LICENSE +21 -0
- package/README.md +1946 -0
- package/dist/chunk-2LNRY4QK.js +273 -0
- package/dist/chunk-2LNRY4QK.js.map +1 -0
- package/dist/chunk-3HENGDW2.js +587 -0
- package/dist/chunk-3HENGDW2.js.map +1 -0
- package/dist/chunk-4OAT42CA.cjs +73 -0
- package/dist/chunk-4OAT42CA.cjs.map +1 -0
- package/dist/chunk-5GWX5LFW.js +70 -0
- package/dist/chunk-5GWX5LFW.js.map +1 -0
- package/dist/chunk-5R2M36QB.js +195 -0
- package/dist/chunk-5R2M36QB.js.map +1 -0
- package/dist/chunk-5ZN622AO.js +73 -0
- package/dist/chunk-5ZN622AO.js.map +1 -0
- package/dist/chunk-77MSMAUQ.cjs +498 -0
- package/dist/chunk-77MSMAUQ.cjs.map +1 -0
- package/dist/chunk-ABPEQ6RK.cjs +596 -0
- package/dist/chunk-ABPEQ6RK.cjs.map +1 -0
- package/dist/chunk-BWYGJKRB.js +95 -0
- package/dist/chunk-BWYGJKRB.js.map +1 -0
- package/dist/chunk-BZHG5IZ4.js +73 -0
- package/dist/chunk-BZHG5IZ4.js.map +1 -0
- package/dist/chunk-G7VZBCD6.cjs +35 -0
- package/dist/chunk-G7VZBCD6.cjs.map +1 -0
- package/dist/chunk-GVLK7YUU.cjs +30 -0
- package/dist/chunk-GVLK7YUU.cjs.map +1 -0
- package/dist/chunk-HCCXC7XG.js +205 -0
- package/dist/chunk-HCCXC7XG.js.map +1 -0
- package/dist/chunk-HE6T6FIX.cjs +203 -0
- package/dist/chunk-HE6T6FIX.cjs.map +1 -0
- package/dist/chunk-KIXWPOCO.cjs +100 -0
- package/dist/chunk-KIXWPOCO.cjs.map +1 -0
- package/dist/chunk-KVGNW3FC.js +87 -0
- package/dist/chunk-KVGNW3FC.js.map +1 -0
- package/dist/chunk-LITNXTTT.js +3 -0
- package/dist/chunk-LITNXTTT.js.map +1 -0
- package/dist/chunk-M4ANN7RL.js +114 -0
- package/dist/chunk-M4ANN7RL.js.map +1 -0
- package/dist/chunk-NC52UBR2.cjs +32 -0
- package/dist/chunk-NC52UBR2.cjs.map +1 -0
- package/dist/chunk-NHCNRQD3.cjs +212 -0
- package/dist/chunk-NHCNRQD3.cjs.map +1 -0
- package/dist/chunk-NZ72VDNY.cjs +4 -0
- package/dist/chunk-NZ72VDNY.cjs.map +1 -0
- package/dist/chunk-P6JUDYNO.js +57 -0
- package/dist/chunk-P6JUDYNO.js.map +1 -0
- package/dist/chunk-RJYY7BWX.js +1349 -0
- package/dist/chunk-RJYY7BWX.js.map +1 -0
- package/dist/chunk-TRI4V5BF.cjs +126 -0
- package/dist/chunk-TRI4V5BF.cjs.map +1 -0
- package/dist/chunk-UL33I6IS.js +139 -0
- package/dist/chunk-UL33I6IS.js.map +1 -0
- package/dist/chunk-URRW6M2C.cjs +61 -0
- package/dist/chunk-URRW6M2C.cjs.map +1 -0
- package/dist/chunk-UY3UYPBZ.cjs +77 -0
- package/dist/chunk-UY3UYPBZ.cjs.map +1 -0
- package/dist/chunk-W3253FGB.cjs +277 -0
- package/dist/chunk-W3253FGB.cjs.map +1 -0
- package/dist/chunk-W7LHZVQF.js +26 -0
- package/dist/chunk-W7LHZVQF.js.map +1 -0
- package/dist/chunk-WBWNM6LB.cjs +1360 -0
- package/dist/chunk-WBWNM6LB.cjs.map +1 -0
- package/dist/chunk-WFJ7L2RV.js +494 -0
- package/dist/chunk-WFJ7L2RV.js.map +1 -0
- package/dist/chunk-X4RMFFMR.js +28 -0
- package/dist/chunk-X4RMFFMR.js.map +1 -0
- package/dist/chunk-Y4Y2S7BM.cjs +92 -0
- package/dist/chunk-Y4Y2S7BM.cjs.map +1 -0
- package/dist/chunk-YLPNXZFI.cjs +143 -0
- package/dist/chunk-YLPNXZFI.cjs.map +1 -0
- package/dist/chunk-YTXEZ4SD.cjs +77 -0
- package/dist/chunk-YTXEZ4SD.cjs.map +1 -0
- package/dist/chunk-Z6ZWNWWR.js +30 -0
- package/dist/chunk-Z6ZWNWWR.js.map +1 -0
- package/dist/config.cjs +26 -0
- package/dist/config.cjs.map +1 -0
- package/dist/config.d.cts +75 -0
- package/dist/config.d.ts +75 -0
- package/dist/config.js +5 -0
- package/dist/config.js.map +1 -0
- package/dist/db.cjs +233 -0
- package/dist/db.cjs.map +1 -0
- package/dist/db.d.cts +123 -0
- package/dist/db.d.ts +123 -0
- package/dist/db.js +228 -0
- package/dist/db.js.map +1 -0
- package/dist/decorators.cjs +67 -0
- package/dist/decorators.cjs.map +1 -0
- package/dist/decorators.d.cts +91 -0
- package/dist/decorators.d.ts +91 -0
- package/dist/decorators.js +65 -0
- package/dist/decorators.js.map +1 -0
- package/dist/event-subscriber.cjs +6 -0
- package/dist/event-subscriber.cjs.map +1 -0
- package/dist/event-subscriber.d.cts +116 -0
- package/dist/event-subscriber.d.ts +116 -0
- package/dist/event-subscriber.js +3 -0
- package/dist/event-subscriber.js.map +1 -0
- package/dist/event-testing.cjs +21 -0
- package/dist/event-testing.cjs.map +1 -0
- package/dist/event-testing.d.cts +110 -0
- package/dist/event-testing.d.ts +110 -0
- package/dist/event-testing.js +4 -0
- package/dist/event-testing.js.map +1 -0
- package/dist/event.cjs +30 -0
- package/dist/event.cjs.map +1 -0
- package/dist/event.d.cts +282 -0
- package/dist/event.d.ts +282 -0
- package/dist/event.js +13 -0
- package/dist/event.js.map +1 -0
- package/dist/exporters.cjs +17 -0
- package/dist/exporters.cjs.map +1 -0
- package/dist/exporters.d.cts +1 -0
- package/dist/exporters.d.ts +1 -0
- package/dist/exporters.js +4 -0
- package/dist/exporters.js.map +1 -0
- package/dist/functional.cjs +46 -0
- package/dist/functional.cjs.map +1 -0
- package/dist/functional.d.cts +478 -0
- package/dist/functional.d.ts +478 -0
- package/dist/functional.js +13 -0
- package/dist/functional.js.map +1 -0
- package/dist/http.cjs +189 -0
- package/dist/http.cjs.map +1 -0
- package/dist/http.d.cts +169 -0
- package/dist/http.d.ts +169 -0
- package/dist/http.js +184 -0
- package/dist/http.js.map +1 -0
- package/dist/index.cjs +333 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +758 -0
- package/dist/index.d.ts +758 -0
- package/dist/index.js +143 -0
- package/dist/index.js.map +1 -0
- package/dist/instrumentation.cjs +182 -0
- package/dist/instrumentation.cjs.map +1 -0
- package/dist/instrumentation.d.cts +49 -0
- package/dist/instrumentation.d.ts +49 -0
- package/dist/instrumentation.js +179 -0
- package/dist/instrumentation.js.map +1 -0
- package/dist/logger.cjs +19 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +146 -0
- package/dist/logger.d.ts +146 -0
- package/dist/logger.js +6 -0
- package/dist/logger.js.map +1 -0
- package/dist/metric-helpers.cjs +31 -0
- package/dist/metric-helpers.cjs.map +1 -0
- package/dist/metric-helpers.d.cts +13 -0
- package/dist/metric-helpers.d.ts +13 -0
- package/dist/metric-helpers.js +6 -0
- package/dist/metric-helpers.js.map +1 -0
- package/dist/metric-testing.cjs +21 -0
- package/dist/metric-testing.cjs.map +1 -0
- package/dist/metric-testing.d.cts +110 -0
- package/dist/metric-testing.d.ts +110 -0
- package/dist/metric-testing.js +4 -0
- package/dist/metric-testing.js.map +1 -0
- package/dist/metric.cjs +26 -0
- package/dist/metric.cjs.map +1 -0
- package/dist/metric.d.cts +240 -0
- package/dist/metric.d.ts +240 -0
- package/dist/metric.js +9 -0
- package/dist/metric.js.map +1 -0
- package/dist/processors.cjs +17 -0
- package/dist/processors.cjs.map +1 -0
- package/dist/processors.d.cts +1 -0
- package/dist/processors.d.ts +1 -0
- package/dist/processors.js +4 -0
- package/dist/processors.js.map +1 -0
- package/dist/sampling.cjs +40 -0
- package/dist/sampling.cjs.map +1 -0
- package/dist/sampling.d.cts +260 -0
- package/dist/sampling.d.ts +260 -0
- package/dist/sampling.js +7 -0
- package/dist/sampling.js.map +1 -0
- package/dist/semantic-helpers.cjs +35 -0
- package/dist/semantic-helpers.cjs.map +1 -0
- package/dist/semantic-helpers.d.cts +442 -0
- package/dist/semantic-helpers.d.ts +442 -0
- package/dist/semantic-helpers.js +14 -0
- package/dist/semantic-helpers.js.map +1 -0
- package/dist/tail-sampling-processor.cjs +13 -0
- package/dist/tail-sampling-processor.cjs.map +1 -0
- package/dist/tail-sampling-processor.d.cts +27 -0
- package/dist/tail-sampling-processor.d.ts +27 -0
- package/dist/tail-sampling-processor.js +4 -0
- package/dist/tail-sampling-processor.js.map +1 -0
- package/dist/testing.cjs +286 -0
- package/dist/testing.cjs.map +1 -0
- package/dist/testing.d.cts +291 -0
- package/dist/testing.d.ts +291 -0
- package/dist/testing.js +263 -0
- package/dist/testing.js.map +1 -0
- package/dist/trace-context-DRZdUvVY.d.cts +181 -0
- package/dist/trace-context-DRZdUvVY.d.ts +181 -0
- package/dist/trace-helpers.cjs +54 -0
- package/dist/trace-helpers.cjs.map +1 -0
- package/dist/trace-helpers.d.cts +524 -0
- package/dist/trace-helpers.d.ts +524 -0
- package/dist/trace-helpers.js +5 -0
- package/dist/trace-helpers.js.map +1 -0
- package/dist/tracer-provider.cjs +21 -0
- package/dist/tracer-provider.cjs.map +1 -0
- package/dist/tracer-provider.d.cts +169 -0
- package/dist/tracer-provider.d.ts +169 -0
- package/dist/tracer-provider.js +4 -0
- package/dist/tracer-provider.js.map +1 -0
- package/package.json +280 -0
- package/src/baggage-span-processor.test.ts +202 -0
- package/src/baggage-span-processor.ts +98 -0
- package/src/circuit-breaker.test.ts +341 -0
- package/src/circuit-breaker.ts +184 -0
- package/src/config.test.ts +94 -0
- package/src/config.ts +169 -0
- package/src/db.test.ts +252 -0
- package/src/db.ts +447 -0
- package/src/decorators.test.ts +203 -0
- package/src/decorators.ts +188 -0
- package/src/env-config.test.ts +246 -0
- package/src/env-config.ts +158 -0
- package/src/event-queue.test.ts +222 -0
- package/src/event-queue.ts +203 -0
- package/src/event-subscriber.ts +136 -0
- package/src/event-testing.ts +197 -0
- package/src/event.test.ts +718 -0
- package/src/event.ts +556 -0
- package/src/exporters.ts +96 -0
- package/src/functional.test.ts +1059 -0
- package/src/functional.ts +2295 -0
- package/src/http.test.ts +487 -0
- package/src/http.ts +424 -0
- package/src/index.ts +158 -0
- package/src/init.customization.test.ts +210 -0
- package/src/init.integrations.test.ts +366 -0
- package/src/init.openllmetry.test.ts +282 -0
- package/src/init.protocol.test.ts +215 -0
- package/src/init.ts +1426 -0
- package/src/instrumentation.test.ts +108 -0
- package/src/instrumentation.ts +308 -0
- package/src/logger.test.ts +117 -0
- package/src/logger.ts +246 -0
- package/src/metric-helpers.ts +47 -0
- package/src/metric-testing.ts +197 -0
- package/src/metric.ts +434 -0
- package/src/metrics.test.ts +205 -0
- package/src/operation-context.ts +93 -0
- package/src/processors.ts +106 -0
- package/src/rate-limiter.test.ts +199 -0
- package/src/rate-limiter.ts +98 -0
- package/src/sampling.test.ts +513 -0
- package/src/sampling.ts +428 -0
- package/src/semantic-helpers.test.ts +311 -0
- package/src/semantic-helpers.ts +584 -0
- package/src/shutdown.test.ts +311 -0
- package/src/shutdown.ts +222 -0
- package/src/stub.integration.test.ts +361 -0
- package/src/tail-sampling-processor.test.ts +226 -0
- package/src/tail-sampling-processor.ts +51 -0
- package/src/testing.ts +670 -0
- package/src/trace-context.ts +470 -0
- package/src/trace-helpers.new.test.ts +278 -0
- package/src/trace-helpers.test.ts +242 -0
- package/src/trace-helpers.ts +690 -0
- package/src/tracer-provider.test.ts +183 -0
- package/src/tracer-provider.ts +266 -0
- package/src/track.test.ts +153 -0
- package/src/track.ts +120 -0
- package/src/validation.test.ts +306 -0
- package/src/validation.ts +239 -0
- package/src/variable-name-inference.test.ts +178 -0
- package/src/variable-name-inference.ts +242 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
inferVariableNameFromCallStack,
|
|
4
|
+
clearInferenceCache,
|
|
5
|
+
} from './variable-name-inference';
|
|
6
|
+
|
|
7
|
+
// Helper function that simulates trace() for testing
|
|
8
|
+
function trace<T>(fn: T): T {
|
|
9
|
+
const varName = inferVariableNameFromCallStack();
|
|
10
|
+
// Attach the inferred name to the function for verification
|
|
11
|
+
(fn as { __inferredName?: string }).__inferredName = varName;
|
|
12
|
+
return fn;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe('variable-name-inference', () => {
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
// Clear cache before each test to ensure isolation
|
|
18
|
+
clearInferenceCache();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('inferVariableNameFromCallStack', () => {
|
|
22
|
+
it('should infer variable name from const assignment', () => {
|
|
23
|
+
const testFunction = trace(() => 'test');
|
|
24
|
+
|
|
25
|
+
expect((testFunction as { __inferredName?: string }).__inferredName).toBe(
|
|
26
|
+
'testFunction',
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should cache inference results', () => {
|
|
31
|
+
// Helper to test caching from same location
|
|
32
|
+
function callFromSameLocation() {
|
|
33
|
+
const cachedResult = trace(() => 'test');
|
|
34
|
+
return (cachedResult as { __inferredName?: string }).__inferredName;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const result1 = callFromSameLocation();
|
|
38
|
+
const result2 = callFromSameLocation();
|
|
39
|
+
|
|
40
|
+
// Both calls from same location should get same cached result
|
|
41
|
+
expect(result1).toBe('cachedResult');
|
|
42
|
+
expect(result2).toBe('cachedResult');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should gracefully handle errors', () => {
|
|
46
|
+
// This test verifies that even if something goes wrong,
|
|
47
|
+
// the function returns undefined rather than throwing
|
|
48
|
+
expect(() => trace(() => 'test')).not.toThrow();
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('pattern matching', () => {
|
|
53
|
+
// These tests verify that the regex patterns work correctly
|
|
54
|
+
// by creating actual assignments and checking inference
|
|
55
|
+
|
|
56
|
+
it('should match const pattern', () => {
|
|
57
|
+
const myConstVariable = trace(() => 'test');
|
|
58
|
+
expect(
|
|
59
|
+
(myConstVariable as { __inferredName?: string }).__inferredName,
|
|
60
|
+
).toBe('myConstVariable');
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('should handle camelCase names', () => {
|
|
64
|
+
const myCamelCaseVariableName = trace(() => 'test');
|
|
65
|
+
expect(
|
|
66
|
+
(myCamelCaseVariableName as { __inferredName?: string }).__inferredName,
|
|
67
|
+
).toBe('myCamelCaseVariableName');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should handle PascalCase names', () => {
|
|
71
|
+
const MyPascalCaseName = trace(() => 'test');
|
|
72
|
+
expect(
|
|
73
|
+
(MyPascalCaseName as { __inferredName?: string }).__inferredName,
|
|
74
|
+
).toBe('MyPascalCaseName');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should handle snake_case names', () => {
|
|
78
|
+
const my_snake_case_name = trace(() => 'test');
|
|
79
|
+
expect(
|
|
80
|
+
(my_snake_case_name as { __inferredName?: string }).__inferredName,
|
|
81
|
+
).toBe('my_snake_case_name');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should handle names with numbers', () => {
|
|
85
|
+
const myVariable123 = trace(() => 'test');
|
|
86
|
+
expect(
|
|
87
|
+
(myVariable123 as { __inferredName?: string }).__inferredName,
|
|
88
|
+
).toBe('myVariable123');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('should handle names with dollar signs', () => {
|
|
92
|
+
const $myVariable = trace(() => 'test');
|
|
93
|
+
expect(($myVariable as { __inferredName?: string }).__inferredName).toBe(
|
|
94
|
+
'$myVariable',
|
|
95
|
+
);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should handle names with underscores', () => {
|
|
99
|
+
const _privateVariable = trace(() => 'test');
|
|
100
|
+
expect(
|
|
101
|
+
(_privateVariable as { __inferredName?: string }).__inferredName,
|
|
102
|
+
).toBe('_privateVariable');
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe('edge cases', () => {
|
|
107
|
+
it('should handle multiple spaces in assignment', () => {
|
|
108
|
+
const spacedVariable = trace(() => 'test');
|
|
109
|
+
expect(
|
|
110
|
+
(spacedVariable as { __inferredName?: string }).__inferredName,
|
|
111
|
+
).toBe('spacedVariable');
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should handle assignment with no spaces', () => {
|
|
115
|
+
const noSpaces = trace(() => 'test');
|
|
116
|
+
expect((noSpaces as { __inferredName?: string }).__inferredName).toBe(
|
|
117
|
+
'noSpaces',
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('caching behavior', () => {
|
|
123
|
+
it('should cache results for same call location', () => {
|
|
124
|
+
// Helper function to call from same location multiple times
|
|
125
|
+
function callFromSameLocation() {
|
|
126
|
+
const cachedResult = trace(() => 'test');
|
|
127
|
+
return (cachedResult as { __inferredName?: string }).__inferredName;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const result1 = callFromSameLocation();
|
|
131
|
+
const result2 = callFromSameLocation();
|
|
132
|
+
|
|
133
|
+
// Both calls from same source location should return same result
|
|
134
|
+
expect(result1).toBe('cachedResult');
|
|
135
|
+
expect(result2).toBe('cachedResult');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('should clear cache when requested', () => {
|
|
139
|
+
const beforeClear = trace(() => 'test');
|
|
140
|
+
const result1 = (beforeClear as { __inferredName?: string })
|
|
141
|
+
.__inferredName;
|
|
142
|
+
|
|
143
|
+
clearInferenceCache();
|
|
144
|
+
|
|
145
|
+
const afterClear = trace(() => 'test');
|
|
146
|
+
const result2 = (afterClear as { __inferredName?: string })
|
|
147
|
+
.__inferredName;
|
|
148
|
+
|
|
149
|
+
// Both should infer correctly regardless of cache
|
|
150
|
+
expect(result1).toBe('beforeClear');
|
|
151
|
+
expect(result2).toBe('afterClear');
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe('integration with trace pattern', () => {
|
|
156
|
+
it('should work with factory pattern simulation', () => {
|
|
157
|
+
// Simulate the factory pattern: trace((ctx) => async () => {})
|
|
158
|
+
const myFactoryFunction = trace((_ctx: unknown) => async () => {
|
|
159
|
+
return 'test';
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
expect(
|
|
163
|
+
(myFactoryFunction as { __inferredName?: string }).__inferredName,
|
|
164
|
+
).toBe('myFactoryFunction');
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('should work with direct pattern simulation', () => {
|
|
168
|
+
// Simulate direct pattern: trace(async () => {})
|
|
169
|
+
const myDirectFunction = trace(async () => {
|
|
170
|
+
return 'test';
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
expect(
|
|
174
|
+
(myDirectFunction as { __inferredName?: string }).__inferredName,
|
|
175
|
+
).toBe('myDirectFunction');
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
});
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Variable Name Inference Utility
|
|
3
|
+
*
|
|
4
|
+
* Attempts to infer variable names from const/export const assignments
|
|
5
|
+
* by analyzing the call stack and parsing source code.
|
|
6
|
+
*
|
|
7
|
+
* This is a best-effort approach with graceful degradation - if inference
|
|
8
|
+
* fails for any reason, it returns undefined without breaking the application.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { readFileSync } from 'node:fs';
|
|
12
|
+
import { fileURLToPath } from 'node:url';
|
|
13
|
+
|
|
14
|
+
interface CallLocation {
|
|
15
|
+
file: string;
|
|
16
|
+
line: number;
|
|
17
|
+
column: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* LRU Cache for inferred variable names
|
|
22
|
+
* Key: "file:line" (e.g., "/path/to/file.ts:42")
|
|
23
|
+
* Value: inferred variable name or undefined
|
|
24
|
+
*/
|
|
25
|
+
const inferenceCache = new Map<string, string | undefined>();
|
|
26
|
+
const MAX_CACHE_SIZE = 50;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Captures the current call stack
|
|
30
|
+
*/
|
|
31
|
+
function captureStackTrace(): string {
|
|
32
|
+
const originalStackTraceLimit = Error.stackTraceLimit;
|
|
33
|
+
Error.stackTraceLimit = 10; // Only need first few frames
|
|
34
|
+
|
|
35
|
+
const err = new Error('Stack trace capture');
|
|
36
|
+
const stack = err.stack || '';
|
|
37
|
+
|
|
38
|
+
Error.stackTraceLimit = originalStackTraceLimit;
|
|
39
|
+
return stack;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Parses the stack trace to find where trace() was called
|
|
44
|
+
*
|
|
45
|
+
* Stack trace format (Node.js):
|
|
46
|
+
* at functionName (file:line:column)
|
|
47
|
+
* at file:line:column
|
|
48
|
+
*
|
|
49
|
+
* We skip frames until we find one that's NOT in functional.ts or this file.
|
|
50
|
+
* We also need to skip one additional frame (the trace/span/instrument function itself)
|
|
51
|
+
* to get to the actual user code.
|
|
52
|
+
*/
|
|
53
|
+
function parseCallLocation(stack: string): CallLocation | undefined {
|
|
54
|
+
const lines = stack.split('\n');
|
|
55
|
+
let skippedExternalFrame = false;
|
|
56
|
+
|
|
57
|
+
for (const line of lines) {
|
|
58
|
+
// Skip if line contains this file or functional.ts (internal frames)
|
|
59
|
+
// Be specific about the filename to avoid matching test files
|
|
60
|
+
if (
|
|
61
|
+
line.includes('variable-name-inference.ts') ||
|
|
62
|
+
line.includes('variable-name-inference.js') ||
|
|
63
|
+
line.includes('functional.ts') ||
|
|
64
|
+
line.includes('functional.js')
|
|
65
|
+
) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Match various stack trace formats
|
|
70
|
+
// Format 1: at functionName (file:line:column)
|
|
71
|
+
// Format 2: at file:line:column
|
|
72
|
+
const match =
|
|
73
|
+
line.match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/) ||
|
|
74
|
+
line.match(/^.*?([^:]+):(\d+):(\d+)/);
|
|
75
|
+
|
|
76
|
+
if (match) {
|
|
77
|
+
let filePath = match[1]!.trim();
|
|
78
|
+
|
|
79
|
+
// Handle file:// URLs (convert to paths)
|
|
80
|
+
if (filePath.startsWith('file://')) {
|
|
81
|
+
try {
|
|
82
|
+
filePath = fileURLToPath(filePath);
|
|
83
|
+
} catch {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Skip the first external frame (the trace/span function itself)
|
|
89
|
+
// We want the frame where the user CALLS trace(), not inside trace()
|
|
90
|
+
if (!skippedExternalFrame) {
|
|
91
|
+
skippedExternalFrame = true;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
file: filePath,
|
|
97
|
+
line: Number.parseInt(match[2]!, 10),
|
|
98
|
+
column: Number.parseInt(match[3]!, 10),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Reads a specific line from a source file
|
|
108
|
+
*/
|
|
109
|
+
function readSourceLine(
|
|
110
|
+
filePath: string,
|
|
111
|
+
lineNumber: number,
|
|
112
|
+
): string | undefined {
|
|
113
|
+
try {
|
|
114
|
+
// Check if we can access the file system (not available in edge runtimes)
|
|
115
|
+
if (typeof readFileSync !== 'function') {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const content = readFileSync(filePath, 'utf8');
|
|
120
|
+
const lines = content.split('\n');
|
|
121
|
+
|
|
122
|
+
// Line numbers are 1-based
|
|
123
|
+
return lines[lineNumber - 1];
|
|
124
|
+
} catch {
|
|
125
|
+
// File doesn't exist, permission denied, or other error
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Extracts variable name from source code line using regex patterns
|
|
132
|
+
*
|
|
133
|
+
* Supported patterns:
|
|
134
|
+
* - const varName = anyFunction(
|
|
135
|
+
* - export const varName = anyFunction(
|
|
136
|
+
* - let varName = anyFunction(
|
|
137
|
+
* - var varName = anyFunction(
|
|
138
|
+
*
|
|
139
|
+
* Note: This won't work with destructuring assignments or complex patterns
|
|
140
|
+
*/
|
|
141
|
+
function extractVariableName(sourceLine: string): string | undefined {
|
|
142
|
+
// Remove leading/trailing whitespace
|
|
143
|
+
const trimmed = sourceLine.trim();
|
|
144
|
+
|
|
145
|
+
// Pattern: (export)? (const|let|var) varName = anyFunctionCall(
|
|
146
|
+
// We match any function call, not just trace(), to support wrapper functions
|
|
147
|
+
const patterns = [
|
|
148
|
+
// export const varName = anyFunction(
|
|
149
|
+
/export\s+const\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*=\s*[a-zA-Z_$][a-zA-Z0-9_$]*\s*\(/,
|
|
150
|
+
// const varName = anyFunction(
|
|
151
|
+
/const\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*=\s*[a-zA-Z_$][a-zA-Z0-9_$]*\s*\(/,
|
|
152
|
+
// export let varName = anyFunction(
|
|
153
|
+
/export\s+let\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*=\s*[a-zA-Z_$][a-zA-Z0-9_$]*\s*\(/,
|
|
154
|
+
// let varName = anyFunction(
|
|
155
|
+
/let\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*=\s*[a-zA-Z_$][a-zA-Z0-9_$]*\s*\(/,
|
|
156
|
+
// export var varName = anyFunction(
|
|
157
|
+
/export\s+var\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*=\s*[a-zA-Z_$][a-zA-Z0-9_$]*\s*\(/,
|
|
158
|
+
// var varName = anyFunction(
|
|
159
|
+
/var\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\s*=\s*[a-zA-Z_$][a-zA-Z0-9_$]*\s*\(/,
|
|
160
|
+
];
|
|
161
|
+
|
|
162
|
+
for (const pattern of patterns) {
|
|
163
|
+
const match = trimmed.match(pattern);
|
|
164
|
+
if (match && match[1]) {
|
|
165
|
+
return match[1];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Adds an entry to the cache with LRU eviction
|
|
174
|
+
*/
|
|
175
|
+
function cacheInference(key: string, value: string | undefined): void {
|
|
176
|
+
// If cache is full, remove oldest entry (first entry in Map)
|
|
177
|
+
if (inferenceCache.size >= MAX_CACHE_SIZE) {
|
|
178
|
+
const firstKey = inferenceCache.keys().next().value;
|
|
179
|
+
if (firstKey) {
|
|
180
|
+
inferenceCache.delete(firstKey);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
inferenceCache.set(key, value);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Main entry point: Attempts to infer the variable name from the call stack
|
|
189
|
+
*
|
|
190
|
+
* This function:
|
|
191
|
+
* 1. Captures the call stack
|
|
192
|
+
* 2. Parses it to find where trace() was called (file + line)
|
|
193
|
+
* 3. Reads that line from the source file
|
|
194
|
+
* 4. Extracts the variable name using regex
|
|
195
|
+
*
|
|
196
|
+
* Returns undefined if inference fails at any step (graceful degradation).
|
|
197
|
+
* Results are cached to avoid repeated file I/O.
|
|
198
|
+
*
|
|
199
|
+
* @returns The inferred variable name, or undefined if inference failed
|
|
200
|
+
*/
|
|
201
|
+
export function inferVariableNameFromCallStack(): string | undefined {
|
|
202
|
+
try {
|
|
203
|
+
// Capture stack trace
|
|
204
|
+
const stack = captureStackTrace();
|
|
205
|
+
|
|
206
|
+
// Parse stack to find trace() call location
|
|
207
|
+
const callLocation = parseCallLocation(stack);
|
|
208
|
+
if (!callLocation) {
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Check cache
|
|
213
|
+
const cacheKey = `${callLocation.file}:${callLocation.line}`;
|
|
214
|
+
if (inferenceCache.has(cacheKey)) {
|
|
215
|
+
return inferenceCache.get(cacheKey);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Read source line
|
|
219
|
+
const sourceLine = readSourceLine(callLocation.file, callLocation.line);
|
|
220
|
+
if (!sourceLine) {
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Extract variable name
|
|
225
|
+
const variableName = extractVariableName(sourceLine);
|
|
226
|
+
|
|
227
|
+
// Cache result (even if undefined, to avoid repeated failed attempts)
|
|
228
|
+
cacheInference(cacheKey, variableName);
|
|
229
|
+
|
|
230
|
+
return variableName;
|
|
231
|
+
} catch {
|
|
232
|
+
// Graceful degradation - don't break the app if inference fails
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Clears the inference cache (useful for testing)
|
|
239
|
+
*/
|
|
240
|
+
export function clearInferenceCache(): void {
|
|
241
|
+
inferenceCache.clear();
|
|
242
|
+
}
|