trickle-observe 0.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/auto-env.js +13 -0
- package/auto-esm.mjs +128 -0
- package/auto.js +3 -0
- package/dist/auto-codegen.d.ts +29 -0
- package/dist/auto-codegen.js +999 -0
- package/dist/auto-register.d.ts +16 -0
- package/dist/auto-register.js +99 -0
- package/dist/cache.d.ts +27 -0
- package/dist/cache.js +52 -0
- package/dist/env-detect.d.ts +5 -0
- package/dist/env-detect.js +35 -0
- package/dist/express.d.ts +44 -0
- package/dist/express.js +342 -0
- package/dist/fetch-observer.d.ts +24 -0
- package/dist/fetch-observer.js +217 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +172 -0
- package/dist/observe-register.d.ts +29 -0
- package/dist/observe-register.js +455 -0
- package/dist/observe.d.ts +44 -0
- package/dist/observe.js +109 -0
- package/dist/proxy-tracker.d.ts +15 -0
- package/dist/proxy-tracker.js +172 -0
- package/dist/register.d.ts +21 -0
- package/dist/register.js +105 -0
- package/dist/transport.d.ts +22 -0
- package/dist/transport.js +228 -0
- package/dist/type-hash.d.ts +5 -0
- package/dist/type-hash.js +60 -0
- package/dist/type-inference.d.ts +14 -0
- package/dist/type-inference.js +259 -0
- package/dist/types.d.ts +78 -0
- package/dist/types.js +2 -0
- package/dist/wrap.d.ts +10 -0
- package/dist/wrap.js +247 -0
- package/observe-esm-hooks.mjs +367 -0
- package/observe-esm.mjs +40 -0
- package/observe.js +2 -0
- package/package.json +26 -0
- package/register.js +2 -0
- package/src/auto-codegen.ts +1058 -0
- package/src/auto-register.ts +102 -0
- package/src/cache.ts +53 -0
- package/src/env-detect.ts +22 -0
- package/src/express.ts +386 -0
- package/src/fetch-observer.ts +226 -0
- package/src/index.ts +199 -0
- package/src/observe-register.ts +453 -0
- package/src/observe.ts +127 -0
- package/src/proxy-tracker.ts +208 -0
- package/src/register.ts +110 -0
- package/src/transport.ts +207 -0
- package/src/type-hash.ts +71 -0
- package/src/type-inference.ts +285 -0
- package/src/types.ts +61 -0
- package/src/wrap.ts +289 -0
- package/tsconfig.json +8 -0
package/auto-env.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Conditional auto-instrumentation loader.
|
|
2
|
+
//
|
|
3
|
+
// Use with NODE_OPTIONS or -r flag:
|
|
4
|
+
// TRICKLE_AUTO=1 node -r trickle/auto-env app.js
|
|
5
|
+
// NODE_OPTIONS="--require trickle/auto-env" node app.js
|
|
6
|
+
//
|
|
7
|
+
// When TRICKLE_AUTO=1 is set, this loads trickle/auto which installs
|
|
8
|
+
// all hooks and starts background type generation.
|
|
9
|
+
// When TRICKLE_AUTO is not set, this is a no-op.
|
|
10
|
+
|
|
11
|
+
if (process.env.TRICKLE_AUTO === '1') {
|
|
12
|
+
require('./dist/auto-register');
|
|
13
|
+
}
|
package/auto-esm.mjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* trickle/auto-esm — Zero-config type generation for ESM modules.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* node --import trickle/auto-esm app.mjs
|
|
6
|
+
*
|
|
7
|
+
* This module:
|
|
8
|
+
* 1. Forces local mode (no backend needed)
|
|
9
|
+
* 2. Registers ESM loader hooks that wrap exported functions
|
|
10
|
+
* 3. Runs a background timer that generates .d.ts files from observations
|
|
11
|
+
* 4. On process exit, does a final type generation
|
|
12
|
+
*
|
|
13
|
+
* No CLI. No backend. No configuration. Just types.
|
|
14
|
+
* Works for ALL ESM modules (import/export syntax).
|
|
15
|
+
*/
|
|
16
|
+
import { register } from 'node:module';
|
|
17
|
+
import { pathToFileURL } from 'node:url';
|
|
18
|
+
import { dirname, join } from 'node:path';
|
|
19
|
+
import { fileURLToPath } from 'node:url';
|
|
20
|
+
import { createRequire } from 'node:module';
|
|
21
|
+
|
|
22
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
23
|
+
const require = createRequire(import.meta.url);
|
|
24
|
+
|
|
25
|
+
// Force local mode BEFORE anything else
|
|
26
|
+
process.env.TRICKLE_LOCAL = '1';
|
|
27
|
+
|
|
28
|
+
const hooksPath = join(__dirname, 'observe-esm-hooks.mjs');
|
|
29
|
+
const debug = process.env.TRICKLE_DEBUG === '1' || process.env.TRICKLE_DEBUG === 'true';
|
|
30
|
+
|
|
31
|
+
if (debug) {
|
|
32
|
+
console.log(`[trickle/auto-esm] Registering ESM observation hooks (local mode)`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Register the ESM loader hooks
|
|
36
|
+
register(pathToFileURL(hooksPath).href, {
|
|
37
|
+
parentURL: import.meta.url,
|
|
38
|
+
data: {
|
|
39
|
+
wrapperPath: join(__dirname, 'dist', 'wrap.js'),
|
|
40
|
+
transportPath: join(__dirname, 'dist', 'transport.js'),
|
|
41
|
+
envDetectPath: join(__dirname, 'dist', 'env-detect.js'),
|
|
42
|
+
backendUrl: 'http://localhost:4888', // unused in local mode but configure() needs it
|
|
43
|
+
debug,
|
|
44
|
+
includePatterns: process.env.TRICKLE_OBSERVE_INCLUDE
|
|
45
|
+
? process.env.TRICKLE_OBSERVE_INCLUDE.split(',').map(s => s.trim())
|
|
46
|
+
: [],
|
|
47
|
+
excludePatterns: process.env.TRICKLE_OBSERVE_EXCLUDE
|
|
48
|
+
? process.env.TRICKLE_OBSERVE_EXCLUDE.split(',').map(s => s.trim())
|
|
49
|
+
: [],
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Also configure transport in the main thread for local mode
|
|
54
|
+
const { configure } = require(join(__dirname, 'dist', 'transport.js'));
|
|
55
|
+
configure({
|
|
56
|
+
backendUrl: 'http://localhost:4888',
|
|
57
|
+
batchIntervalMs: 2000,
|
|
58
|
+
debug,
|
|
59
|
+
enabled: true,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Start background codegen
|
|
63
|
+
const { generateTypes, injectTypes, generateCoverageReport, generateTypeSummary } = require(join(__dirname, 'dist', 'auto-codegen.js'));
|
|
64
|
+
|
|
65
|
+
let lastFunctionCount = 0;
|
|
66
|
+
let generationCount = 0;
|
|
67
|
+
|
|
68
|
+
function runGeneration(isFinal) {
|
|
69
|
+
try {
|
|
70
|
+
const count = generateTypes();
|
|
71
|
+
if (count === -1) return;
|
|
72
|
+
|
|
73
|
+
if (count > 0) {
|
|
74
|
+
generationCount++;
|
|
75
|
+
if (debug || (count > lastFunctionCount)) {
|
|
76
|
+
const newTypes = count - lastFunctionCount;
|
|
77
|
+
if (newTypes > 0 && generationCount > 1) {
|
|
78
|
+
console.log(`[trickle/auto-esm] +${newTypes} type(s) generated (${count} total)`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
lastFunctionCount = count;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (isFinal && lastFunctionCount > 0) {
|
|
85
|
+
console.log(`[trickle/auto-esm] ${lastFunctionCount} function type(s) written to .d.ts`);
|
|
86
|
+
try {
|
|
87
|
+
const injected = injectTypes();
|
|
88
|
+
if (injected > 0) {
|
|
89
|
+
console.log(`[trickle/auto-esm] ${injected} function(s) annotated with JSDoc in source`);
|
|
90
|
+
}
|
|
91
|
+
} catch { /* don't crash */ }
|
|
92
|
+
try {
|
|
93
|
+
const report = generateCoverageReport();
|
|
94
|
+
if (report) console.log(report);
|
|
95
|
+
} catch { /* don't crash */ }
|
|
96
|
+
try {
|
|
97
|
+
const summary = generateTypeSummary();
|
|
98
|
+
if (summary) console.log(summary);
|
|
99
|
+
} catch { /* don't crash */ }
|
|
100
|
+
}
|
|
101
|
+
} catch {
|
|
102
|
+
// Never crash user's app
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Background timer — regenerate types every 3 seconds
|
|
107
|
+
const timer = setInterval(() => runGeneration(false), 3000);
|
|
108
|
+
if (timer && typeof timer === 'object' && 'unref' in timer) {
|
|
109
|
+
timer.unref();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// First check after 1 second
|
|
113
|
+
const initialTimer = setTimeout(() => runGeneration(false), 1000);
|
|
114
|
+
if (initialTimer && typeof initialTimer === 'object' && 'unref' in initialTimer) {
|
|
115
|
+
initialTimer.unref();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Final generation on exit
|
|
119
|
+
process.on('beforeExit', () => {
|
|
120
|
+
runGeneration(true);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const exitHandler = () => {
|
|
124
|
+
clearInterval(timer);
|
|
125
|
+
runGeneration(true);
|
|
126
|
+
};
|
|
127
|
+
process.on('SIGTERM', exitHandler);
|
|
128
|
+
process.on('SIGINT', exitHandler);
|
package/auto.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightweight inline codegen for `trickle/auto`.
|
|
3
|
+
*
|
|
4
|
+
* Reads .trickle/observations.jsonl and generates .d.ts sidecar files
|
|
5
|
+
* next to source files. Runs entirely inside the user's process —
|
|
6
|
+
* no CLI or backend required.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Inject JSDoc comments into JS source files based on observations.
|
|
10
|
+
* Only runs when TRICKLE_INJECT=1.
|
|
11
|
+
*/
|
|
12
|
+
export declare function injectTypes(): number;
|
|
13
|
+
/**
|
|
14
|
+
* Read observations and generate .d.ts files next to source files.
|
|
15
|
+
* Returns the number of functions typed.
|
|
16
|
+
*/
|
|
17
|
+
export declare function generateTypes(): number;
|
|
18
|
+
/**
|
|
19
|
+
* Generate a type coverage report comparing observed types against
|
|
20
|
+
* all function declarations found in source files.
|
|
21
|
+
* Only runs when TRICKLE_COVERAGE=1.
|
|
22
|
+
*/
|
|
23
|
+
export declare function generateCoverageReport(): string | null;
|
|
24
|
+
/**
|
|
25
|
+
* Generate a type summary showing discovered function signatures.
|
|
26
|
+
* When a previous snapshot exists, highlights new and changed functions.
|
|
27
|
+
* Only runs when TRICKLE_SUMMARY=1.
|
|
28
|
+
*/
|
|
29
|
+
export declare function generateTypeSummary(): string | null;
|