-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.d.ts
More file actions
157 lines (146 loc) · 5.19 KB
/
Copy pathindex.d.ts
File metadata and controls
157 lines (146 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* tslint:disable */
/* eslint-disable */
import type { Node } from 'estree';
/**
* Create a new instrumentation matcher from an array of instrumentation configs.
*/
export function create(configs: InstrumentationConfig[], dc_module?: string | null): InstrumentationMatcher;
/**
* Output of a transformation operation
*/
export interface TransformOutput {
/**
* The transformed JavaScript code
*/
code: string;
/**
* The sourcemap for the transformation (if generated)
*/
map: string | undefined;
}
/**
* The kind of function
*/
export type FunctionKind = "Sync" | "Async" | "Callback" | "Auto";
/**
* Describes which function to instrument
*/
export type FunctionQuery = { className: string; methodName: string; kind: FunctionKind; index?: number | null; isExportAlias?: boolean } | { className: string; privateMethodName: string; kind: FunctionKind; index?: number | null } | { className: string; index?: number | null; isExportAlias?: boolean } | { methodName: string; kind: FunctionKind; index?: number | null } | { functionName: string; kind: FunctionKind; index?: number | null; isExportAlias?: boolean } | { expressionName: string; kind: FunctionKind; index?: number | null; isExportAlias?: boolean };
/**
* A custom transform function registered via `addTransform`.
* Receives the instrumentation state and the matched AST node.
*/
export type CustomTransform = (state: unknown, node: Node, parent: Node, ancestry: Node[]) => void;
/**
* The behaviour-only fields of a `FunctionQuery`. Used together with `astQuery`,
* where the raw selector chooses the node and these fields drive how it is
* wrapped (the name-based matching fields are ignored).
*/
export interface FunctionBehavior {
kind?: FunctionKind;
index?: number | null;
callbackIndex?: number;
mutableResult?: boolean;
}
/**
* Configuration for injecting instrumentation code.
*
* Either `functionQuery` (name-based matching) or `astQuery` (a raw esquery
* selector) must identify the node(s) to instrument. When `astQuery` is set it
* takes precedence over `functionQuery`'s matching fields, and `functionQuery`
* becomes an optional bag of behaviour options ({@link FunctionBehavior}).
*/
export type InstrumentationConfig =
| {
/** The name of the diagnostics channel to publish to */
channelName: string;
/** The module matcher to identify the module and file to instrument */
module: ModuleMatcher;
/** The function query to identify the function to instrument */
functionQuery: FunctionQuery;
/**
* A raw esquery selector that chooses the node(s) to instrument. When
* set, it takes precedence over `functionQuery`'s matching fields.
*/
astQuery?: string;
/**
* The name of a custom transform registered via `addTransform`.
* When set, takes precedence over `functionQuery.kind`.
*/
transform?: string;
}
| {
channelName: string;
module: ModuleMatcher;
/**
* A raw esquery selector that chooses the node(s) to instrument. This is
* the escape hatch for shapes the name-based `functionQuery` can't
* express, e.g. an anonymous arrow returned by a factory function.
*/
astQuery: string;
/** Behaviour options for the matched node(s); matching fields are ignored. */
functionQuery?: FunctionBehavior;
transform?: string;
};
/**
* Describes the module and file path you would like to match
*/
export interface ModuleMatcher {
/**
* The name of the module you want to match
*/
name: string;
/**
* The semver range that you want to match
*/
versionRange: string;
/**
* The path of the file you want to match from the module root
*/
filePath: string | RegExp;
}
/**
* The type of module being passed - ESM, CJS or unknown
*/
export type ModuleType = "esm" | "cjs" | "unknown";
/**
* The InstrumentationMatcher is responsible for matching specific modules
*/
export class InstrumentationMatcher {
private constructor();
free(): void;
/**
* Get a transformer for the given module name, version and file path.
* Returns `undefined` if no matching instrumentations are found.
*/
getTransformer(moduleName: string, version: string, filePath: string): Transformer | undefined;
/**
* Register a custom transform function under the given name.
* The name can then be referenced via the `transform` option in an `InstrumentationConfig`.
*/
addTransform(name: string, fn: CustomTransform): void;
}
/**
* The Transformer is responsible for transforming JavaScript code.
*/
export class Transformer {
private constructor();
free(): void;
/**
* The name of the module to transform.
*/
get moduleName(): string;
/**
* The relative file path within the npm package being instrumented.
*
* @returns {string}
*/
get filePath(): string;
/**
* Transform JavaScript code and optionally sourcemap.
*
* # Errors
* Returns an error if the transformation fails to find injection points.
*/
transform(code: string | Buffer, moduleType: ModuleType, sourcemap?: string | object | null): TransformOutput;
}