@convo-lang/convo-lang
Version:
The language of AI
129 lines • 6 kB
JavaScript
import { applyIdentityToConvoDbCommand, applyIdentityToConvoDbCommands, applyIdentityToConvoDbQuery, applyIdentityToPermission } from "./convo-db-lib.js";
/**
* The ConvoDbPermissionBoundary class wraps an existing ConvoDb and ensures all calls to the wrapped
* database use the provided `identityPath`. Permissions boundaries allow save access from external
* callers ensuring that their access to the wrapped database is restricted to their identityPath.
* The provided `identityPath` will be used to set the `permissionFrom` of all calls to the wrapped
* database. The permissionFrom value supplied by external callers will always be overwritten.
* External callers are not allowed to issue driver commands and will receive a 401 response
* if they try.
*/
export class ConvoDbPermissionBoundary {
constructor({ proxiedDb, identityPath, auth, }) {
this.lastUsed = Date.now();
this.dbName = proxiedDb.dbName;
this._driver = proxiedDb._driver;
this.auth = proxiedDb.auth;
this.proxiedDb = proxiedDb;
this.identityPath = identityPath;
this.parentAuth = auth;
}
initAsync() {
return Promise.resolve({
success: true,
});
}
get isDisposed() { return this.proxiedDb.isDisposed; }
dispose() {
// do nothing
}
readBlobStringAsync(path, permissionFrom) {
return this.proxiedDb.readBlobStringAsync(path, this.identityPath);
}
openBlobAsync(path) {
return this.proxiedDb.openBlobAsync(path, this.identityPath);
}
writeBlobAsync(path, blob) {
return this.proxiedDb.writeBlobAsync(path, blob, this.identityPath);
}
hasBlobAsync(path) {
return this.proxiedDb.hasBlobAsync(path, this.identityPath);
}
callFunctionAsync(path, args, _permissionFrom) {
return this.proxiedDb.callFunctionAsync(path, args, this.identityPath);
}
callFunctionWithSchemaAsync(inputSchema, outputSchema, path, args, _permissionFrom) {
return this.proxiedDb.callFunctionWithSchemaAsync(inputSchema, outputSchema, path, args, this.identityPath);
}
callFunctionReturnValueAsync(path, args, _permissionFrom) {
return this.proxiedDb.callFunctionReturnValueAsync(path, args, this.identityPath);
}
callFunctionReturnNodeAsync(path, args, _permissionFrom) {
return this.proxiedDb.callFunctionReturnNodeAsync(path, args, this.identityPath);
}
queryNodesAsync(query) {
return this.proxiedDb.queryNodesAsync(applyIdentityToConvoDbQuery(this.identityPath, query));
}
streamNodesAsync(query, cancel) {
return this.proxiedDb.streamNodesAsync(applyIdentityToConvoDbQuery(this.identityPath, query), cancel);
}
getNodesByPathAsync(path, _permissionFrom) {
return this.proxiedDb.getNodesByPathAsync(path, this.identityPath);
}
getNodeByPathAsync(path, _permissionFrom) {
return this.proxiedDb.getNodeByPathAsync(path, this.identityPath);
}
requireNodeByPathAsync(path, permissionFrom) {
return this.proxiedDb.requireNodeByPathAsync(path, this.identityPath);
}
getNodePermissionAsync(_fromPath, toPath) {
return this.proxiedDb.getNodePermissionAsync(this.identityPath, toPath);
}
checkNodePermissionAsync(_fromPath, toPath, type, matchAny) {
return this.proxiedDb.checkNodePermissionAsync(this.identityPath, toPath, type, matchAny);
}
insertNodeAsync(node, options) {
return this.proxiedDb.insertNodeAsync(node, applyIdentityToPermission(this.identityPath, options ?? {}));
}
updateNodeAsync(node, options) {
return this.proxiedDb.updateNodeAsync(node, applyIdentityToPermission(this.identityPath, options ?? {}));
}
deleteNodeAsync(path, options) {
return this.proxiedDb.deleteNodeAsync(path, applyIdentityToPermission(this.identityPath, options ?? {}));
}
queryEdgesAsync(query) {
return this.proxiedDb.queryEdgesAsync(applyIdentityToPermission(this.identityPath, query));
}
getEdgeByIdAsync(id, _permissionFrom) {
return this.proxiedDb.getEdgeByIdAsync(id, this.identityPath);
}
insertEdgeAsync(edge, options) {
return this.proxiedDb.insertEdgeAsync(edge, applyIdentityToPermission(this.identityPath, options ?? {}));
}
updateEdgeAsync(update, options) {
return this.proxiedDb.updateEdgeAsync(update, applyIdentityToPermission(this.identityPath, options ?? {}));
}
deleteEdgeAsync(id, options) {
return this.proxiedDb.deleteEdgeAsync(id, applyIdentityToPermission(this.identityPath, options ?? {}));
}
queryEmbeddingsAsync(query) {
return this.proxiedDb.queryEmbeddingsAsync(applyIdentityToPermission(this.identityPath, query));
}
getEmbeddingByIdAsync(id, _permissionFrom) {
return this.proxiedDb.getEmbeddingByIdAsync(id, this.identityPath);
}
insertEmbeddingAsync(embedding, options) {
return this.proxiedDb.insertEmbeddingAsync(embedding, applyIdentityToPermission(this.identityPath, options ?? {}));
}
updateEmbeddingAsync(update, options) {
return this.proxiedDb.updateEmbeddingAsync(update, applyIdentityToPermission(this.identityPath, options ?? {}));
}
deleteEmbeddingAsync(id, options) {
return this.proxiedDb.deleteEmbeddingAsync(id, applyIdentityToPermission(this.identityPath, options ?? {}));
}
executeCommandAsync(command) {
const r = applyIdentityToConvoDbCommand(this.identityPath, command);
if (!r.success) {
return Promise.resolve(r);
}
return this.proxiedDb.executeCommandAsync(command);
}
executeCommandsAsync(commands) {
const r = applyIdentityToConvoDbCommands(this.identityPath, commands);
if (!r.success) {
return Promise.resolve(r);
}
return this.proxiedDb.executeCommandsAsync(commands);
}
}
//# sourceMappingURL=ConvoDbPermissionBoundary.js.map