Migrate to Deno (#86)

Big bang migration to Deno 🤯
This commit is contained in:
Zef Hemel
2022-10-10 14:50:21 +02:00
committed by GitHub
parent 78f83c70d8
commit 561aa6891f
287 changed files with 4577 additions and 25087 deletions
+22
View File
@@ -0,0 +1,22 @@
import { base64Decode } from "../../plugos/base64.ts";
import type { FileMeta } from "./fs.ts";
import { syscall } from "./syscall.ts";
export async function readAsset(
name: string,
encoding: "utf8" | "dataurl" = "utf8",
): Promise<{ text: string; meta: FileMeta }> {
const { data, meta } = await syscall("asset.readAsset", name);
switch (encoding) {
case "utf8":
return {
text: new TextDecoder().decode(base64Decode(data)),
meta,
};
case "dataurl":
return {
text: "data:application/octet-stream," + data,
meta,
};
}
}
+29
View File
@@ -0,0 +1,29 @@
import { syscall } from "./syscall.ts";
export function dispatch(
eventName: string,
data: any,
timeout?: number,
): Promise<any[]> {
return new Promise((resolve, reject) => {
let timeouter: any = -1;
if (timeout) {
timeouter = setTimeout(() => {
console.log("Timeout!");
reject("timeout");
}, timeout);
}
syscall("event.dispatch", eventName, data)
.then((r) => {
if (timeouter !== -1) {
clearTimeout(timeouter);
}
resolve(r);
})
.catch(reject);
});
}
export function listEvents(): Promise<string[]> {
return syscall("event.list");
}
+36
View File
@@ -0,0 +1,36 @@
import { syscall } from "./syscall.ts";
export type FileMeta = {
name: string;
lastModified: number;
};
export function readFile(
path: string,
encoding: "utf8" | "dataurl" = "utf8",
): Promise<{ text: string; meta: FileMeta }> {
return syscall("fs.readFile", path, encoding);
}
export function getFileMeta(path: string): Promise<FileMeta> {
return syscall("fs.getFileMeta", path);
}
export function writeFile(
path: string,
text: string,
encoding: "utf8" | "dataurl" = "utf8",
): Promise<FileMeta> {
return syscall("fs.writeFile", path, text, encoding);
}
export function deleteFile(path: string): Promise<void> {
return syscall("fs.deleteFile", path);
}
export function listFiles(
dirName: string,
recursive = false,
): Promise<FileMeta[]> {
return syscall("fs.listFiles", dirName, recursive);
}
+13
View File
@@ -0,0 +1,13 @@
import { syscall } from "./syscall.ts";
export function fullTextIndex(key: string, value: string) {
return syscall("fulltext.index", key, value);
}
export function fullTextDelete(key: string) {
return syscall("fulltext.delete", key);
}
export function fullTextSearch(phrase: string, limit = 100) {
return syscall("fulltext.search", phrase, limit);
}
+5
View File
@@ -0,0 +1,5 @@
import type { LogEntry } from "../../plugos/sandbox.ts";
export function getLogs(): Promise<LogEntry[]> {
return syscall("sandbox.getLogs");
}
+8
View File
@@ -0,0 +1,8 @@
import { syscall } from "./syscall.ts";
export function run(
cmd: string,
args: string[],
): Promise<{ stdout: string; stderr: string }> {
return syscall("shell.run", cmd, args);
}
+54
View File
@@ -0,0 +1,54 @@
import { syscall } from "./syscall.ts";
export type KV = {
key: string;
value: any;
};
export type Query = {
filter?: Filter[];
orderBy?: string;
orderDesc?: boolean;
limit?: number;
select?: string[];
};
export type Filter = {
op: string;
prop: string;
value: any;
};
export function set(key: string, value: any): Promise<void> {
return syscall("store.set", key, value);
}
export function batchSet(kvs: KV[]): Promise<void> {
return syscall("store.batchSet", kvs);
}
export function get(key: string): Promise<any> {
return syscall("store.get", key);
}
export function del(key: string): Promise<void> {
return syscall("store.delete", key);
}
export function batchDel(keys: string[]): Promise<void> {
return syscall("store.batchDelete", keys);
}
export function queryPrefix(
prefix: string,
): Promise<{ key: string; value: any }[]> {
return syscall("store.queryPrefix", prefix);
}
export function deletePrefix(prefix: string): Promise<void> {
return syscall("store.deletePrefix", prefix);
}
export function deleteAll(): Promise<void> {
return syscall("store.deleteAll");
}
+5
View File
@@ -0,0 +1,5 @@
declare global {
function syscall(name: string, ...args: any[]): Promise<any>;
}
export const syscall = self.syscall;