dev: add system kv access for extensions
Docker Image CI / build-and-push-image (push) Has been cancelled
Maintain Release Merge PR / update-release-pr (push) Has been cancelled
release-please / release-please (push) Has been cancelled
test / test (18.x) (push) Has been cancelled
test / test (20.x) (push) Has been cancelled
test / test (22.x) (push) Has been cancelled

This commit is contained in:
KernelDeimos
2025-01-20 13:33:02 -05:00
parent e8f5450cb0
commit 44c6f57c7a
+34
View File
@@ -23,6 +23,7 @@ const { Endpoint } = require("./util/expressutil");
const configurable_auth = require("./middleware/configurable_auth");
const { Context } = require("./util/context");
const { DB_READ, DB_WRITE } = require("./services/database/consts");
const { Actor } = require("./services/auth/Actor");
/**
* State shared with the default service and the `extension` global so that
@@ -96,6 +97,39 @@ class ExtensionService extends BaseService {
svc_event.emit(key, data, meta);
});
this.state.extension.kv = (() => {
const impls = this.services.get_implementors('puter-kvstore');
const impl_kv = impls[0].impl;
return new Proxy(impl_kv, {
get: (target, prop) => {
if ( typeof target[prop] !== 'function' ) {
return target[prop];
}
return (...args) => {
if ( typeof args[0] !== 'object' ) {
// Luckily named parameters don't have positional
// overlaps between the different kv methods, so
// we can just set them all.
args[0] = {
key: args[0],
as: args[0],
value: args[1],
amount: args[2],
timestamp: args[2],
ttl: args[2],
};
}
return Context.sub({
actor: Actor.get_system_actor(),
}).arun(() => target[prop](...args));
};
},
});
})();
console.log('set kv on', this.state.extension);
}
['__on_install.routes'] (_, { app }) {