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;
@@ -0,0 +1,13 @@
import { syscall } from "./syscall.ts";
export function set(key: string, value: any): Promise<void> {
return syscall("clientStore.set", key, value);
}
export function get(key: string): Promise<any> {
return syscall("clientStore.get", key);
}
export function del(key: string): Promise<void> {
return syscall("clientStore.delete", key);
}
+156
View File
@@ -0,0 +1,156 @@
import { syscall } from "./syscall.ts";
import { FilterOption } from "../../common/types.ts";
export function getCurrentPage(): Promise<string> {
return syscall("editor.getCurrentPage");
}
export function setPage(newName: string): Promise<void> {
return syscall("editor.setPage", newName);
}
export function getText(): Promise<string> {
return syscall("editor.getText");
}
export function getCursor(): Promise<number> {
return syscall("editor.getCursor");
}
export function getSelection(): Promise<{ from: number; to: number }> {
return syscall("editor.getSelection");
}
export function setSelection(from: number, to: number): Promise<void> {
return syscall("editor.setSelection", from, to);
}
export function save(): Promise<void> {
return syscall("editor.save");
}
export function navigate(
name: string,
pos?: string | number,
replaceState = false,
): Promise<void> {
return syscall("editor.navigate", name, pos, replaceState);
}
export function reloadPage(): Promise<void> {
return syscall("editor.reloadPage");
}
export function openUrl(url: string): Promise<void> {
return syscall("editor.openUrl", url);
}
export function flashNotification(
message: string,
type: "info" | "error" = "info",
): Promise<void> {
return syscall("editor.flashNotification", message, type);
}
export function filterBox(
label: string,
options: FilterOption[],
helpText = "",
placeHolder = "",
): Promise<FilterOption | undefined> {
return syscall("editor.filterBox", label, options, helpText, placeHolder);
}
export function showPanel(
id: "lhs" | "rhs" | "bhs" | "modal",
mode: number,
html: string,
script = "",
): Promise<void> {
return syscall("editor.showPanel", id, mode, html, script);
}
export function hidePanel(id: "lhs" | "rhs" | "bhs" | "modal"): Promise<void> {
return syscall("editor.hidePanel", id);
}
export function insertAtPos(text: string, pos: number): Promise<void> {
return syscall("editor.insertAtPos", text, pos);
}
export function replaceRange(
from: number,
to: number,
text: string,
): Promise<void> {
return syscall("editor.replaceRange", from, to, text);
}
export function moveCursor(pos: number): Promise<void> {
return syscall("editor.moveCursor", pos);
}
export function insertAtCursor(text: string): Promise<void> {
return syscall("editor.insertAtCursor", text);
}
export function matchBefore(
re: string,
): Promise<{ from: number; to: number; text: string } | null> {
return syscall("editor.matchBefore", re);
}
export function dispatch(change: any): Promise<void> {
return syscall("editor.dispatch", change);
}
export function prompt(
message: string,
defaultValue = "",
): Promise<string | undefined> {
return syscall("editor.prompt", message, defaultValue);
}
export function enableReadOnlyMode(enabled: boolean) {
return syscall("editor.enableReadOnlyMode", enabled);
}
// DEPRECATED in favor of showPanel and hidePanel
export function showRhs(
html: string,
script?: string,
flex = 1,
): Promise<void> {
return syscall("editor.showRhs", html, script, flex);
}
export function hideRhs(): Promise<void> {
return syscall("editor.hideRhs");
}
export function showLhs(
html: string,
script?: string,
flex = 1,
): Promise<void> {
return syscall("editor.showLhs", html, script, flex);
}
export function hideLhs(): Promise<void> {
return syscall("editor.hideLhs");
}
export function showBhs(
html: string,
script?: string,
flex = 1,
): Promise<void> {
return syscall("editor.showBhs", html, script, flex);
}
export function hideBhs(): Promise<void> {
return syscall("editor.hideBhs");
}
// End deprecation
+54
View File
@@ -0,0 +1,54 @@
import type { Query } from "../plugos-syscall/store.ts";
import { syscall } from "./syscall.ts";
export type KV = {
key: string;
value: any;
};
export function set(
page: string,
key: string,
value: any,
): Promise<void> {
return syscall("index.set", page, key, value);
}
export function batchSet(page: string, kvs: KV[]): Promise<void> {
return syscall("index.batchSet", page, kvs);
}
export function get(page: string, key: string): Promise<any> {
return syscall("index.get", page, key);
}
export function del(page: string, key: string): Promise<void> {
return syscall("index.delete", page, key);
}
export function queryPrefix(
prefix: string,
): Promise<{ key: string; page: string; value: any }[]> {
return syscall("index.queryPrefix", prefix);
}
export function query(
query: Query,
): Promise<{ key: string; page: string; value: any }[]> {
return syscall("index.query", query);
}
export function clearPageIndexForPage(page: string): Promise<void> {
return syscall("index.clearPageIndexForPage", page);
}
export function deletePrefixForPage(
page: string,
prefix: string,
): Promise<void> {
return syscall("index.deletePrefixForPage", page, prefix);
}
export function clearPageIndex(): Promise<void> {
return syscall("index.clearPageIndex");
}
+7
View File
@@ -0,0 +1,7 @@
import { syscall } from "./syscall.ts";
import type { ParseTree } from "../../common/tree.ts";
export function parseMarkdown(text: string): Promise<ParseTree> {
return syscall("markdown.parseMarkdown", text);
}
+5
View File
@@ -0,0 +1,5 @@
import type { LogEntry } from "../../plugos/sandbox.ts";
export function getServerLogs(): Promise<LogEntry[]> {
return syscall("sandbox.getServerLogs");
}
+54
View File
@@ -0,0 +1,54 @@
import { syscall } from "./syscall.ts";
import { AttachmentMeta, PageMeta } from "../../common/types.ts";
export function listPages(unfiltered = false): Promise<PageMeta[]> {
return syscall("space.listPages", unfiltered);
}
export function getPageMeta(name: string): Promise<PageMeta> {
return syscall("space.getPageMeta", name);
}
export function readPage(
name: string,
): Promise<{ text: string; meta: PageMeta }> {
return syscall("space.readPage", name);
}
export function writePage(name: string, text: string): Promise<PageMeta> {
return syscall("space.writePage", name, text);
}
export function deletePage(name: string): Promise<void> {
return syscall("space.deletePage", name);
}
export function listPlugs(): Promise<string[]> {
return syscall("space.listPlugs");
}
export function listAttachments(): Promise<PageMeta[]> {
return syscall("space.listAttachments");
}
export function getAttachmentMeta(name: string): Promise<AttachmentMeta> {
return syscall("space.getAttachmentMeta", name);
}
export function readAttachment(
name: string,
): Promise<{ data: string; meta: AttachmentMeta }> {
return syscall("space.readAttachment", name);
}
export function writeAttachment(
name: string,
encoding: "string" | "dataurl",
data: string,
): Promise<AttachmentMeta> {
return syscall("space.writeAttachment", name, encoding, data);
}
export function deleteAttachment(name: string): Promise<void> {
return syscall("space.deleteAttachment", name);
}
+16
View File
@@ -0,0 +1,16 @@
declare global {
function syscall(name: string, ...args: any[]): Promise<any>;
}
// This is the case when running tests only, so giving it a dummy syscall function
if (typeof self === "undefined") {
// @ts-ignore: test
// deno-lint-ignore no-global-assign
self = {
syscall: () => {
throw new Error("Not implemented here");
},
};
}
export const syscall = self.syscall;
+24
View File
@@ -0,0 +1,24 @@
import type { CommandDef } from "../../web/hooks/command.ts";
import { syscall } from "./syscall.ts";
export function invokeFunction(
env: string,
name: string,
...args: any[]
): Promise<any> {
return syscall("system.invokeFunction", env, name, ...args);
}
// Only available on the client
export function invokeCommand(name: string): Promise<any> {
return syscall("system.invokeCommand", name);
}
// Only available on the client
export function listCommands(): Promise<{ [key: string]: CommandDef }> {
return syscall("system.listCommands");
}
export function reloadPlugs() {
syscall("system.reloadPlugs");
}