Skip to content

Commit 415e94f

Browse files
authored
feat(rpc): server-side validator (#3150)
1 parent 1455cae commit 415e94f

File tree

7 files changed

+946
-1463
lines changed

7 files changed

+946
-1463
lines changed

src/rpc/channels.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ export type RouteContinueParams = {
14401440
name: string,
14411441
value: string,
14421442
}[],
1443-
postData?: string,
1443+
postData?: Binary,
14441444
};
14451445
export type RouteContinueResult = void;
14461446
export type RouteFulfillParams = {

src/rpc/client/connection.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { ChromiumBrowser } from './chromiumBrowser';
3838
import { ChromiumBrowserContext } from './chromiumBrowserContext';
3939
import { Selectors } from './selectors';
4040
import { Stream } from './stream';
41-
import { validateParams } from './validator';
41+
import { createScheme, Validator, ValidationError } from '../validator';
4242

4343
class Root extends ChannelOwner<Channel, {}> {
4444
constructor(connection: Connection) {
@@ -214,3 +214,20 @@ export class Connection {
214214
return result;
215215
}
216216
}
217+
218+
const tChannel = (name: string): Validator => {
219+
return (arg: any, path: string) => {
220+
if (arg._object instanceof ChannelOwner && (name === '*' || arg._object._type === name))
221+
return { guid: arg._object._guid };
222+
throw new ValidationError(`${path}: expected ${name}`);
223+
};
224+
};
225+
226+
const scheme = createScheme(tChannel);
227+
228+
function validateParams(type: string, method: string, params: any): any {
229+
const name = type + method[0].toUpperCase() + method.substring(1) + 'Params';
230+
if (!scheme[name])
231+
throw new ValidationError(`Uknown scheme for ${type}.${method}`);
232+
return scheme[name](params, '');
233+
}

0 commit comments

Comments
 (0)