Skip to content

Commit

Permalink
fix(rpc): move controllerAccess to handleAction to not call it twice.
Browse files Browse the repository at this point in the history
This allows everyone to read the types of a rpc action.

this also makes sure stopwatch context is provided.

fix(stopwatch): treat unassigned stopwatchContextId as no context
  • Loading branch information
marcj committed Oct 11, 2023
1 parent ba3d506 commit c68308d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
5 changes: 2 additions & 3 deletions packages/example-app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import { RpcController } from './src/controller/rpc.controller.js';
import { ApiConsoleModule } from '@deepkit/api-console-module';
import { OrmBrowserModule } from '@deepkit/orm-browser-module';
import { OpenAPIModule } from 'deepkit-openapi';
import { provideStorage } from '@deepkit/filesystem/dist/cjs/src/storage.js';
import { StorageLocalAdapter } from '@deepkit/filesystem/dist/cjs/src/local-adapter.js';
import { FilesystemLocalAdapter, provideFilesystem } from '@deepkit/filesystem';

const bookStoreCrud = createCrudRoutes([Author, Book]);

new App({
config: Config,
providers: [
SQLiteDatabase, MainController,
provideStorage(() => new StorageLocalAdapter({root: 'public'})),
provideFilesystem(() => new FilesystemLocalAdapter({ root: __dirname + '/public' })),
],
controllers: [MainController, UsersCommand, RpcController],
listeners: [
Expand Down
36 changes: 18 additions & 18 deletions packages/rpc/src/server/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,7 @@ export class RpcServerAction {
throw new Error(`No controller registered for id ${controller}`);
}
const action = getActions(classType.controller).get(methodName);

if (!action) {
throw new Error(`Action unknown ${methodName}`);
}

const controllerAccess: RpcControllerAccess = {
controllerName: controller, actionName: methodName, controllerClassType: classType.controller,
actionGroups: action.groups, actionData: action.data
};

if (!await this.hasControllerAccess(controllerAccess)) {
throw new Error(`Access denied to action ${methodName}`);
}
if (!action) throw new Error(`Action unknown ${methodName}`);

const methodReflection = ReflectionClass.from(classType.controller).getMethod(methodName);
const method = methodReflection.type;
Expand Down Expand Up @@ -386,8 +374,20 @@ export class RpcServerAction {
public async handleAction(message: RpcMessage, response: RpcMessageBuilder) {
const body = message.parseBody<rpcActionType>();

const controller = this.controllers.get(body.controller);
if (!controller) throw new Error(`No controller registered for id ${body.controller}`);
const classType = this.controllers.get(body.controller);
if (!classType) throw new Error(`No controller registered for id ${body.controller}`);

const action = getActions(classType.controller).get(body.method);
if (!action) throw new Error(`Action unknown ${body.method}`);

const controllerAccess: RpcControllerAccess = {
controllerName: body.controller, actionName: body.method, controllerClassType: classType.controller,
actionGroups: action.groups, actionData: action.data
};

if (!await this.hasControllerAccess(controllerAccess)) {
throw new Error(`Access denied to action ${body.method}`);
}

const types = await this.loadTypes(body.controller, body.method);
let value: { args: any[] } = { args: [] };
Expand All @@ -402,9 +402,9 @@ export class RpcServerAction {
return response.error(error);
}

const controllerClassType = this.injector.get(controller.controller, controller.module);
const controllerClassType = this.injector.get(classType.controller, classType.module);
if (!controllerClassType) {
response.error(new Error(`No instance of ${getClassName(controller.controller)} found.`));
response.error(new Error(`No instance of ${getClassName(classType.controller)} found.`));
}
// const converted = types.parametersDeserialize(value.args);
const errors: ValidationErrorItem[] = [];
Expand Down Expand Up @@ -474,7 +474,7 @@ export class RpcServerAction {
}
};
} else if (isObservable(result)) {
this.observables[message.id] = { observable: result, subscriptions: {}, types, classType: controller.controller, method: body.method };
this.observables[message.id] = { observable: result, subscriptions: {}, types, classType: classType.controller, method: body.method };

let type: ActionObservableTypes = ActionObservableTypes.observable;
if (isSubject(result)) {
Expand Down
3 changes: 2 additions & 1 deletion packages/stopwatch/src/stopwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ export class Stopwatch {
} else {
if (!zone) return new NoopStopwatchFrame();
context = zone.stopwatchContextId;
if (!context) throw new Error('No Stopwatch context given');
// might be getting an empty object on some platforms, which we treat as no context (as we start new context only with stopwatchContextId)
if (!context) return new NoopStopwatchFrame();
}

const id = ++frameId;
Expand Down

0 comments on commit c68308d

Please sign in to comment.