Skip to content

Commit af0fb51

Browse files
committed
Allow querying individual interceptors & requesting detailed metadata
This is going to allow the upcoming JVM interceptor to expose the list of JVM processes only when required, which is useful because it's a bit expensive to do all the time.
1 parent 7a8c074 commit af0fb51

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/api-server.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const typeDefs = `
4242
version: String!
4343
config: InterceptionConfig!
4444
interceptors: [Interceptor!]!
45+
interceptor(id: ID!): Interceptor!
4546
networkInterfaces: Json
4647
}
4748
@@ -67,12 +68,17 @@ const typeDefs = `
6768
type Interceptor {
6869
id: ID!
6970
version: String!
70-
metadata: Json
71+
metadata(type: MetadataType): Json
7172
7273
isActivable: Boolean!
7374
isActive(proxyPort: Int!): Boolean!
7475
}
7576
77+
enum MetadataType {
78+
SUMMARY,
79+
DETAILED
80+
}
81+
7682
scalar Json
7783
scalar Error
7884
scalar Void
@@ -101,6 +107,7 @@ const buildResolvers = (
101107
Query: {
102108
version: () => packageJson.version,
103109
interceptors: () => _.values(interceptors),
110+
interceptor: (_: any, { id } : { id: string }) => interceptors[id],
104111
config: () => ({
105112
certificatePath: config.https.certPath,
106113
certificateContent: config.https.certContent,
@@ -113,9 +120,11 @@ const buildResolvers = (
113120
},
114121

115122
Mutation: {
116-
activateInterceptor: async (__: void, args: _.Dictionary<any>) => {
117-
const { id, proxyPort, options } = args;
118-
123+
activateInterceptor: async (__: void, { id, proxyPort, options }: {
124+
id: string,
125+
proxyPort: number,
126+
options: unknown
127+
}) => {
119128
addBreadcrumb(`Activating ${id}`, { category: 'interceptor', data: { id, options } });
120129

121130
const interceptor = interceptors[id];
@@ -138,9 +147,11 @@ const buildResolvers = (
138147
return { success: true, metadata: result };
139148
}
140149
},
141-
deactivateInterceptor: async (__: void, args: _.Dictionary<any>) => {
142-
const { id, proxyPort, options } = args;
143-
150+
deactivateInterceptor: async (__: void, { id, proxyPort, options }: {
151+
id: string,
152+
proxyPort: number,
153+
options: unknown
154+
}) => {
144155
const interceptor = interceptors[id];
145156
if (!interceptor) throw new Error(`Unknown interceptor ${id}`);
146157

@@ -160,21 +171,29 @@ const buildResolvers = (
160171
false
161172
);
162173
},
163-
isActive: (interceptor: Interceptor, args: _.Dictionary<any>) => {
174+
isActive: (interceptor: Interceptor, { proxyPort }: { proxyPort: number }) => {
164175
try {
165-
return interceptor.isActive(args.proxyPort);
176+
return interceptor.isActive(proxyPort);
166177
} catch (e) {
167178
reportError(e);
168179
return false;
169180
}
170181
},
171-
metadata: async (interceptor: Interceptor) => {
182+
metadata: async function (interceptor: Interceptor, { type }: { type?: 'DETAILED' | 'SUMMARY' }) {
172183
if (!interceptor.getMetadata) return undefined;
173184

185+
const metadataType = type
186+
? type.toLowerCase() as 'summary' | 'detailed'
187+
: 'summary';
188+
189+
const timeout = metadataType === 'summary'
190+
? INTERCEPTOR_TIMEOUT
191+
: INTERCEPTOR_TIMEOUT * 10; // Longer timeout for detailed metadata
192+
174193
try {
175194
return await withFallback(
176-
interceptor.getMetadata(),
177-
INTERCEPTOR_TIMEOUT,
195+
interceptor.getMetadata(metadataType),
196+
timeout,
178197
undefined
179198
);
180199
} catch (e) {

src/interceptors/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface Interceptor {
2828
id: string;
2929
version: string;
3030

31-
getMetadata?(): any;
31+
getMetadata?(type: 'summary' | 'detailed'): any;
3232

3333
isActivable(): Promise<boolean>;
3434
isActive(proxyPort: number): boolean;

0 commit comments

Comments
 (0)