dev: begin using Invoker for driver calls

This commit is contained in:
KernelDeimos 2024-07-25 18:00:05 -04:00 committed by Eric Dubé
parent 20b58ddcb5
commit ea409d1578

View File

@ -24,6 +24,7 @@ const BaseService = require("../BaseService");
const { Driver } = require("../../definitions/Driver"); const { Driver } = require("../../definitions/Driver");
const { PermissionUtil } = require("../auth/PermissionService"); const { PermissionUtil } = require("../auth/PermissionService");
const { PolicyEnforcer } = require("./PolicyEnforcer"); const { PolicyEnforcer } = require("./PolicyEnforcer");
const { Invoker } = require("@heyputer/puter-js-common/src/libs/invoker");
/** /**
* DriverService provides the functionality of Puter drivers. * DriverService provides the functionality of Puter drivers.
@ -185,32 +186,16 @@ class DriverService extends BaseService {
driver, method, driver, method,
}); });
try { await policy_enforcer.check();
await policy_enforcer.check(); const result = await this.call_new_({
const result = await this.call_new_({ service_name: driver,
service_name: driver, service,
service, method,
method, args: processed_args,
args: processed_args, iface,
iface, });
}); await policy_enforcer.on_success();
await policy_enforcer.on_success(); return result;
return result;
} catch (e) {
policy_enforcer.on_fail();
console.error(e);
let for_user = (e instanceof APIError) || (e instanceof DriverError);
if ( ! for_user ) this.errors.report(`driver:${iface}:${method}`, {
source: e,
trace: true,
// TODO: alarm will not be suitable for all errors.
alarm: true,
extra: {
args,
}
});
return this._driver_response_from_error(e, meta);
}
} }
} }
@ -298,28 +283,44 @@ class DriverService extends BaseService {
service, method, args, service, method, args,
iface, iface,
}) { }) {
const svc_registry = this.services.get('registry'); const invoker = Invoker.create({
const c_interfaces = svc_registry.get('interfaces'); decorators: [
let result = await service.as(iface)[method](args); {
if ( result instanceof TypedValue ) { name: 'add metadata',
const interface_ = c_interfaces.get(iface); on_return: async result => {
let desired_type = interface_.methods[method] const service_meta = {};
.result_choices[0].type; if ( service.list_traits().includes('version') ) {
const svc_coercion = services.get('coercion'); service_meta.version = service.as('version').get_version();
result = await svc_coercion.coerce(desired_type, result); }
} return {
const service_meta = {}; success: true,
if ( service.list_traits().includes('version') ) { service: {
service_meta.version = service.as('version').get_version(); ...service_meta,
} name: service_name,
return { },
success: true, result,
service: { };
...service_meta, },
name: service_name, },
{
name: 'result coercion',
on_return: async (result) => {
if ( result instanceof TypedValue ) {
const interface_ = c_interfaces.get(iface);
let desired_type = interface_.methods[method]
.result_choices[0].type;
const svc_coercion = services.get('coercion');
result = await svc_coercion.coerce(desired_type, result);
}
return result;
},
},
],
delegate: async (args) => {
return await service.as(iface)[method](args);
}, },
result });
}; return await invoker.run(args);
} }
async _driver_response_from_error (e, meta) { async _driver_response_from_error (e, meta) {