PlugOS refactor and other tweaks (#631)
* Prep for in-process plug loading (e.g. for CF workers, Deno Deploy) * Prototype of fixed in-process loading plugs * Fix: buttons not to scroll with content * Better positioning of modal especially on mobile * Move query caching outside query * Fix annoying mouse behavior when filter box appears * Page navigator search tweaks
This commit is contained in:
@@ -2,11 +2,8 @@ import { SysCallMapping, System } from "../system.ts";
|
||||
|
||||
export default function assetSyscalls(system: System<any>): SysCallMapping {
|
||||
return {
|
||||
"asset.readAsset": (
|
||||
ctx,
|
||||
name: string,
|
||||
): string => {
|
||||
return system.loadedPlugs.get(ctx.plug.name!)!.assets!.readFileAsDataUrl(
|
||||
"asset.readAsset": (_ctx, plugName: string, name: string): string => {
|
||||
return system.loadedPlugs.get(plugName)!.assets!.readFileAsDataUrl(
|
||||
name,
|
||||
);
|
||||
},
|
||||
|
||||
@@ -1,75 +1,47 @@
|
||||
import { KV, KvKey, KvQuery } from "$sb/types.ts";
|
||||
import type { DataStore } from "../lib/datastore.ts";
|
||||
import type { SyscallContext, SysCallMapping } from "../system.ts";
|
||||
import type { SysCallMapping } from "../system.ts";
|
||||
|
||||
/**
|
||||
* Exposes the datastore API to plugs, but scoping everything to a prefix based on the plug's name
|
||||
* @param ds the datastore to wrap
|
||||
* @param prefix prefix to scope all keys to to which the plug name will be appended
|
||||
*/
|
||||
export function dataStoreSyscalls(
|
||||
ds: DataStore,
|
||||
prefix: KvKey = ["ds"],
|
||||
): SysCallMapping {
|
||||
export function dataStoreSyscalls(ds: DataStore): SysCallMapping {
|
||||
return {
|
||||
"datastore.delete": (ctx, key: KvKey) => {
|
||||
return ds.delete(applyPrefix(ctx, key));
|
||||
"datastore.delete": (_ctx, key: KvKey) => {
|
||||
return ds.delete(key);
|
||||
},
|
||||
|
||||
"datastore.set": (ctx, key: KvKey, value: any) => {
|
||||
return ds.set(applyPrefix(ctx, key), value);
|
||||
"datastore.set": (_ctx, key: KvKey, value: any) => {
|
||||
return ds.set(key, value);
|
||||
},
|
||||
|
||||
"datastore.batchSet": (ctx, kvs: KV[]) => {
|
||||
return ds.batchSet(
|
||||
kvs.map((kv) => ({ key: applyPrefix(ctx, kv.key), value: kv.value })),
|
||||
);
|
||||
"datastore.batchSet": (_ctx, kvs: KV[]) => {
|
||||
return ds.batchSet(kvs);
|
||||
},
|
||||
|
||||
"datastore.batchDelete": (ctx, keys: KvKey[]) => {
|
||||
return ds.batchDelete(keys.map((k) => applyPrefix(ctx, k)));
|
||||
"datastore.batchDelete": (_ctx, keys: KvKey[]) => {
|
||||
return ds.batchDelete(keys);
|
||||
},
|
||||
|
||||
"datastore.batchGet": (
|
||||
ctx,
|
||||
_ctx,
|
||||
keys: KvKey[],
|
||||
): Promise<(any | undefined)[]> => {
|
||||
return ds.batchGet(keys.map((k) => applyPrefix(ctx, k)));
|
||||
return ds.batchGet(keys);
|
||||
},
|
||||
|
||||
"datastore.get": (ctx, key: KvKey): Promise<any | null> => {
|
||||
return ds.get(applyPrefix(ctx, key));
|
||||
"datastore.get": (_ctx, key: KvKey): Promise<any | null> => {
|
||||
return ds.get(key);
|
||||
},
|
||||
|
||||
"datastore.query": async (
|
||||
ctx,
|
||||
query: KvQuery,
|
||||
): Promise<KV[]> => {
|
||||
return (await ds.query({
|
||||
...query,
|
||||
prefix: applyPrefix(ctx, query.prefix),
|
||||
})).map((kv) => ({
|
||||
key: stripPrefix(kv.key),
|
||||
value: kv.value,
|
||||
}));
|
||||
"datastore.query": async (_ctx, query: KvQuery): Promise<KV[]> => {
|
||||
return (await ds.query(query));
|
||||
},
|
||||
|
||||
"datastore.queryDelete": (
|
||||
ctx,
|
||||
query: KvQuery,
|
||||
): Promise<void> => {
|
||||
return ds.queryDelete({
|
||||
...query,
|
||||
prefix: applyPrefix(ctx, query.prefix),
|
||||
});
|
||||
"datastore.queryDelete": (_ctx, query: KvQuery): Promise<void> => {
|
||||
return ds.queryDelete(query);
|
||||
},
|
||||
};
|
||||
|
||||
function applyPrefix(ctx: SyscallContext, key?: KvKey): KvKey {
|
||||
return [...prefix, ctx.plug.name!, ...(key ? key : [])];
|
||||
}
|
||||
|
||||
function stripPrefix(key: KvKey): KvKey {
|
||||
return key.slice(prefix.length + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,15 +3,13 @@ import { assert } from "../../test_deps.ts";
|
||||
import { path } from "../deps.ts";
|
||||
import fileSystemSyscalls from "./fs.deno.ts";
|
||||
|
||||
const fakeCtx = {} as any;
|
||||
|
||||
Deno.test("Test FS operations", async () => {
|
||||
const thisFolder = path.resolve(
|
||||
path.dirname(new URL(import.meta.url).pathname),
|
||||
);
|
||||
const syscalls = fileSystemSyscalls(thisFolder);
|
||||
const allFiles: FileMeta[] = await syscalls["fs.listFiles"](
|
||||
fakeCtx,
|
||||
{},
|
||||
thisFolder,
|
||||
true,
|
||||
);
|
||||
|
||||
+10
-11
@@ -1,25 +1,24 @@
|
||||
import { SysCallMapping } from "../system.ts";
|
||||
import { fullQueueName } from "../lib/mq_util.ts";
|
||||
import { MessageQueue } from "../lib/mq.ts";
|
||||
|
||||
export function mqSyscalls(
|
||||
mq: MessageQueue,
|
||||
): SysCallMapping {
|
||||
return {
|
||||
"mq.send": (ctx, queue: string, body: any) => {
|
||||
return mq.send(fullQueueName(ctx.plug.name!, queue), body);
|
||||
"mq.send": (_ctx, queue: string, body: any) => {
|
||||
return mq.send(queue, body);
|
||||
},
|
||||
"mq.batchSend": (ctx, queue: string, bodies: any[]) => {
|
||||
return mq.batchSend(fullQueueName(ctx.plug.name!, queue), bodies);
|
||||
"mq.batchSend": (_ctx, queue: string, bodies: any[]) => {
|
||||
return mq.batchSend(queue, bodies);
|
||||
},
|
||||
"mq.ack": (ctx, queue: string, id: string) => {
|
||||
return mq.ack(fullQueueName(ctx.plug.name!, queue), id);
|
||||
"mq.ack": (_ctx, queue: string, id: string) => {
|
||||
return mq.ack(queue, id);
|
||||
},
|
||||
"mq.batchAck": (ctx, queue: string, ids: string[]) => {
|
||||
return mq.batchAck(fullQueueName(ctx.plug.name!, queue), ids);
|
||||
"mq.batchAck": (_ctx, queue: string, ids: string[]) => {
|
||||
return mq.batchAck(queue, ids);
|
||||
},
|
||||
"mq.getQueueStats": (ctx, queue: string) => {
|
||||
return mq.getQueueStats(fullQueueName(ctx.plug.name!, queue));
|
||||
"mq.getQueueStats": (_ctx, queue: string) => {
|
||||
return mq.getQueueStats(queue);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
import { SyscallContext, SysCallMapping } from "../system.ts";
|
||||
|
||||
export function proxySyscalls(
|
||||
names: string[],
|
||||
transportCall: (
|
||||
ctx: SyscallContext,
|
||||
name: string,
|
||||
...args: any[]
|
||||
) => Promise<any>,
|
||||
): SysCallMapping {
|
||||
const syscalls: SysCallMapping = {};
|
||||
|
||||
for (const name of names) {
|
||||
syscalls[name] = (ctx, ...args: any[]) => {
|
||||
return transportCall(ctx, name, ...args);
|
||||
};
|
||||
}
|
||||
|
||||
return syscalls;
|
||||
}
|
||||
Reference in New Issue
Block a user