@silverbulletmd/silverbullet 2.5.3 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -6
- package/client/asset_bundle/bundle.ts +3 -9
- package/client/data/datastore.ts +4 -5
- package/client/markdown_parser/constants.ts +4 -2
- package/client/plugos/hooks/code_widget.ts +3 -5
- package/client/plugos/hooks/command.ts +54 -24
- package/client/plugos/hooks/document_editor.ts +10 -12
- package/client/plugos/hooks/event.ts +33 -36
- package/client/plugos/hooks/mq.ts +17 -17
- package/client/plugos/hooks/plug_namespace.ts +3 -5
- package/client/plugos/hooks/slash_command.ts +15 -28
- package/client/plugos/hooks/syscall.ts +3 -3
- package/client/plugos/manifest_cache.ts +22 -15
- package/client/plugos/plug.ts +7 -7
- package/client/plugos/plug_compile.ts +119 -67
- package/client/plugos/protocol.ts +28 -28
- package/client/plugos/proxy_fetch.ts +7 -6
- package/client/plugos/sandboxes/worker_sandbox.ts +26 -15
- package/client/plugos/syscalls/asset.ts +1 -3
- package/client/plugos/syscalls/code_widget.ts +1 -3
- package/client/plugos/syscalls/config.ts +10 -7
- package/client/plugos/syscalls/datastore.ts +1 -1
- package/client/plugos/syscalls/editor.ts +199 -60
- package/client/plugos/syscalls/event.ts +9 -12
- package/client/plugos/syscalls/fetch.ts +30 -22
- package/client/plugos/syscalls/index.ts +11 -2
- package/client/plugos/syscalls/jsonschema.ts +54 -32
- package/client/plugos/syscalls/language.ts +9 -5
- package/client/plugos/syscalls/markdown.ts +29 -7
- package/client/plugos/syscalls/mq.ts +3 -11
- package/client/plugos/syscalls/service_registry.ts +1 -4
- package/client/plugos/syscalls/shell.ts +2 -5
- package/client/plugos/syscalls/sync.ts +69 -60
- package/client/plugos/syscalls/system.ts +15 -3
- package/client/plugos/system.ts +18 -20
- package/client/plugos/types.ts +25 -0
- package/client/plugos/util.ts +20 -0
- package/client/plugos/worker_runtime.ts +4 -3
- package/client/space_lua/aggregates.ts +633 -60
- package/client/space_lua/ast.ts +21 -9
- package/client/space_lua/ast_narrow.ts +4 -2
- package/client/space_lua/eval.ts +842 -536
- package/client/space_lua/labels.ts +6 -11
- package/client/space_lua/numeric.ts +5 -8
- package/client/space_lua/parse.ts +290 -169
- package/client/space_lua/query_collection.ts +213 -149
- package/client/space_lua/render_lua_markdown.ts +369 -0
- package/client/space_lua/rp.ts +5 -4
- package/client/space_lua/runtime.ts +245 -142
- package/client/space_lua/sliq_null.ts +6 -0
- package/client/space_lua/stdlib/format.ts +34 -20
- package/client/space_lua/stdlib/js.ts +3 -7
- package/client/space_lua/stdlib/load.ts +1 -3
- package/client/space_lua/stdlib/math.ts +15 -14
- package/client/space_lua/stdlib/net.ts +25 -15
- package/client/space_lua/stdlib/os.ts +76 -85
- package/client/space_lua/stdlib/pattern.ts +28 -35
- package/client/space_lua/stdlib/prng.ts +15 -12
- package/client/space_lua/stdlib/space_lua.ts +16 -17
- package/client/space_lua/stdlib/string.ts +7 -17
- package/client/space_lua/stdlib/string_pack.ts +23 -19
- package/client/space_lua/stdlib/table.ts +5 -9
- package/client/space_lua/stdlib.ts +20 -30
- package/client/space_lua/tonumber.ts +79 -40
- package/client/space_lua/util.ts +14 -10
- package/dist/plug-compile.js +87 -42
- package/package.json +24 -22
- package/plug-api/constants.ts +20 -0
- package/plug-api/lib/async.ts +19 -6
- package/plug-api/lib/crypto.ts +5 -6
- package/plug-api/lib/dates.ts +15 -7
- package/plug-api/lib/json.ts +10 -4
- package/plug-api/lib/ref.ts +27 -20
- package/plug-api/lib/resolve.ts +7 -11
- package/plug-api/lib/shortcut.ts +16 -0
- package/plug-api/lib/tags.ts +13 -4
- package/plug-api/lib/transclusion.ts +6 -17
- package/plug-api/lib/tree.ts +146 -43
- package/plug-api/lib/yaml.ts +25 -15
- package/plug-api/syscalls/asset.ts +1 -1
- package/plug-api/syscalls/config.ts +38 -4
- package/plug-api/syscalls/editor.ts +169 -14
- package/plug-api/syscalls/index.ts +18 -0
- package/plug-api/syscalls/jsonschema.ts +1 -3
- package/plug-api/syscalls/lua.ts +3 -9
- package/plug-api/syscalls/mq.ts +1 -4
- package/plug-api/syscalls/shell.ts +4 -1
- package/plug-api/syscalls/space.ts +3 -10
- package/plug-api/syscalls/system.ts +16 -4
- package/plug-api/syscalls/yaml.ts +2 -6
- package/plug-api/types/client.ts +16 -1
- package/plug-api/types/event.ts +6 -4
- package/plug-api/types/manifest.ts +71 -9
- package/plugs/builtin_plugs.ts +3 -3
- package/client/plugos/sandboxes/web_worker_sandbox.ts +0 -17
- package/dist/worker_runtime_bundle.js +0 -233
|
@@ -18,8 +18,6 @@ export function validateObject(
|
|
|
18
18
|
* @param schema the JSON schema to validate
|
|
19
19
|
* @returns an error message if the schema is invalid, or undefined if it is valid
|
|
20
20
|
*/
|
|
21
|
-
export function validateSchema(
|
|
22
|
-
schema: any,
|
|
23
|
-
): Promise<string | undefined> {
|
|
21
|
+
export function validateSchema(schema: any): Promise<string | undefined> {
|
|
24
22
|
return syscall("jsonschema.validateSchema", schema);
|
|
25
23
|
}
|
package/plug-api/syscalls/lua.ts
CHANGED
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
import { syscall } from "../syscall.ts";
|
|
2
2
|
import type { LuaBlock, LuaExpression } from "../../client/space_lua/ast.ts";
|
|
3
3
|
|
|
4
|
-
export function parse(
|
|
5
|
-
code: string,
|
|
6
|
-
): Promise<LuaBlock> {
|
|
4
|
+
export function parse(code: string): Promise<LuaBlock> {
|
|
7
5
|
return syscall("lua.parse", code);
|
|
8
6
|
}
|
|
9
7
|
|
|
10
|
-
export function parseExpression(
|
|
11
|
-
expression: string,
|
|
12
|
-
): Promise<LuaExpression> {
|
|
8
|
+
export function parseExpression(expression: string): Promise<LuaExpression> {
|
|
13
9
|
return syscall("lua.parseExpression", expression);
|
|
14
10
|
}
|
|
15
11
|
|
|
16
|
-
export function evalExpression(
|
|
17
|
-
expression: string,
|
|
18
|
-
): Promise<any> {
|
|
12
|
+
export function evalExpression(expression: string): Promise<any> {
|
|
19
13
|
return syscall("lua.evalExpression", expression);
|
|
20
14
|
}
|
package/plug-api/syscalls/mq.ts
CHANGED
|
@@ -21,10 +21,7 @@ export function send(queue: string, body: any): Promise<void> {
|
|
|
21
21
|
* @param queue the name of the queue
|
|
22
22
|
* @param bodies the bodies of the messages to send
|
|
23
23
|
*/
|
|
24
|
-
export function batchSend(
|
|
25
|
-
queue: string,
|
|
26
|
-
bodies: any[],
|
|
27
|
-
): Promise<void> {
|
|
24
|
+
export function batchSend(queue: string, bodies: any[]): Promise<void> {
|
|
28
25
|
return syscall("mq.batchSend", queue, bodies);
|
|
29
26
|
}
|
|
30
27
|
|
|
@@ -4,11 +4,14 @@ import { syscall } from "../syscall.ts";
|
|
|
4
4
|
* Runs a shell command.
|
|
5
5
|
* @param cmd the command to run
|
|
6
6
|
* @param args the arguments to pass to the command
|
|
7
|
+
* @param stdin optional string to pass as stdin to the command
|
|
7
8
|
* @returns the stdout, stderr, and exit code of the command
|
|
8
9
|
*/
|
|
9
10
|
export function run(
|
|
10
11
|
cmd: string,
|
|
11
12
|
args: string[],
|
|
13
|
+
stdin?: string,
|
|
12
14
|
): Promise<{ stdout: string; stderr: string; code: number }> {
|
|
13
|
-
return syscall("shell.run", cmd, args);
|
|
15
|
+
return syscall("shell.run", cmd, args, stdin);
|
|
14
16
|
}
|
|
17
|
+
|
|
@@ -44,9 +44,7 @@ export function pageExists(name: string): Promise<boolean> {
|
|
|
44
44
|
* @param name the name of the page to read
|
|
45
45
|
* @returns the text of the page
|
|
46
46
|
*/
|
|
47
|
-
export function readPage(
|
|
48
|
-
name: string,
|
|
49
|
-
): Promise<string> {
|
|
47
|
+
export function readPage(name: string): Promise<string> {
|
|
50
48
|
return syscall("space.readPage", name);
|
|
51
49
|
}
|
|
52
50
|
|
|
@@ -111,9 +109,7 @@ export function getDocumentMeta(name: string): Promise<DocumentMeta> {
|
|
|
111
109
|
* @param name path of the document to read
|
|
112
110
|
* @returns the document data as a UInt8Array
|
|
113
111
|
*/
|
|
114
|
-
export function readDocument(
|
|
115
|
-
name: string,
|
|
116
|
-
): Promise<Uint8Array> {
|
|
112
|
+
export function readDocument(name: string): Promise<Uint8Array> {
|
|
117
113
|
return syscall("space.readDocument", name);
|
|
118
114
|
}
|
|
119
115
|
|
|
@@ -192,10 +188,7 @@ export function getFileMeta(name: string): Promise<FileMeta> {
|
|
|
192
188
|
* @param data the data of the file to write
|
|
193
189
|
* @returns the metadata for the written file
|
|
194
190
|
*/
|
|
195
|
-
export function writeFile(
|
|
196
|
-
name: string,
|
|
197
|
-
data: Uint8Array,
|
|
198
|
-
): Promise<FileMeta> {
|
|
191
|
+
export function writeFile(name: string, data: Uint8Array): Promise<FileMeta> {
|
|
199
192
|
return syscall("space.writeFile", name, data);
|
|
200
193
|
}
|
|
201
194
|
|
|
@@ -13,10 +13,7 @@ import type { SyscallMeta } from "@silverbulletmd/silverbullet/type/index";
|
|
|
13
13
|
* @param args arguments to pass to the function
|
|
14
14
|
* @returns
|
|
15
15
|
*/
|
|
16
|
-
export function invokeFunction(
|
|
17
|
-
name: string,
|
|
18
|
-
...args: any[]
|
|
19
|
-
): Promise<any> {
|
|
16
|
+
export function invokeFunction(name: string, ...args: any[]): Promise<any> {
|
|
20
17
|
return syscall("system.invokeFunction", name, ...args);
|
|
21
18
|
}
|
|
22
19
|
|
|
@@ -54,6 +51,21 @@ export function reloadPlugs(): Promise<void> {
|
|
|
54
51
|
return syscall("system.reloadPlugs");
|
|
55
52
|
}
|
|
56
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Load (or reload) a single plug from a space file path.
|
|
56
|
+
*/
|
|
57
|
+
export function loadPlug(path: string): Promise<void> {
|
|
58
|
+
return syscall("system.loadPlug", path);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Unload a plug previously loaded from the given space file path.
|
|
63
|
+
* Returns true if a plug was unloaded.
|
|
64
|
+
*/
|
|
65
|
+
export function unloadPlug(path: string): Promise<boolean> {
|
|
66
|
+
return syscall("system.unloadPlug", path);
|
|
67
|
+
}
|
|
68
|
+
|
|
57
69
|
/**
|
|
58
70
|
* Returns the current mode of the system, either "ro" (read-only) or "rw" (read-write)
|
|
59
71
|
*/
|
|
@@ -10,9 +10,7 @@ import { syscall } from "../syscall.ts";
|
|
|
10
10
|
* @param text the YAML text to parse
|
|
11
11
|
* @returns a JavaScript object representation of the YAML text
|
|
12
12
|
*/
|
|
13
|
-
export function parse(
|
|
14
|
-
text: string,
|
|
15
|
-
): Promise<any> {
|
|
13
|
+
export function parse(text: string): Promise<any> {
|
|
16
14
|
return syscall("yaml.parse", text);
|
|
17
15
|
}
|
|
18
16
|
|
|
@@ -21,8 +19,6 @@ export function parse(
|
|
|
21
19
|
* @param obj the object to stringify
|
|
22
20
|
* @returns a YAML string representation of the object
|
|
23
21
|
*/
|
|
24
|
-
export function stringify(
|
|
25
|
-
obj: any,
|
|
26
|
-
): Promise<string> {
|
|
22
|
+
export function stringify(obj: any): Promise<string> {
|
|
27
23
|
return syscall("yaml.stringify", obj);
|
|
28
24
|
}
|
package/plug-api/types/client.ts
CHANGED
|
@@ -11,11 +11,26 @@ export type FilterOption = {
|
|
|
11
11
|
prefix?: string;
|
|
12
12
|
} & Record<string, any>;
|
|
13
13
|
|
|
14
|
+
export type NotificationType = "info" | "error" | "warning";
|
|
15
|
+
|
|
16
|
+
export const notificationDismissTimeouts: Record<NotificationType, number> = {
|
|
17
|
+
info: 4000,
|
|
18
|
+
error: 5000,
|
|
19
|
+
warning: 8000,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type NotificationAction = {
|
|
23
|
+
name: string;
|
|
24
|
+
run: () => void;
|
|
25
|
+
};
|
|
26
|
+
|
|
14
27
|
export type Notification = {
|
|
15
28
|
id: number;
|
|
16
29
|
message: string;
|
|
17
|
-
type:
|
|
30
|
+
type: NotificationType;
|
|
18
31
|
date: Date;
|
|
32
|
+
actions?: NotificationAction[];
|
|
33
|
+
persistent?: boolean;
|
|
19
34
|
};
|
|
20
35
|
|
|
21
36
|
export type PanelMode = number;
|
package/plug-api/types/event.ts
CHANGED
|
@@ -21,7 +21,9 @@ export type EventSubscription = EventSubscriptionDef & {
|
|
|
21
21
|
run: (...args: any[]) => Promise<any>;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
export type ResolvedPlug =
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
export type ResolvedPlug =
|
|
25
|
+
| {
|
|
26
|
+
code: string;
|
|
27
|
+
name?: string; // This will only dictate the filename
|
|
28
|
+
}
|
|
29
|
+
| string;
|
|
@@ -6,6 +6,48 @@ export type CodeWidgetT = {
|
|
|
6
6
|
renderMode?: "iframe";
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Predefined native menu items. Ignored in PWA builds; used by the
|
|
11
|
+
* SilverBullet desktop app to emit OS-native menu items (Cut/Copy/Paste,
|
|
12
|
+
* Quit, About, etc.) instead of a command-dispatching item.
|
|
13
|
+
*/
|
|
14
|
+
export type PredefinedMenuKind =
|
|
15
|
+
| "quit" | "about" | "services" | "hide" | "hideOthers" | "showAll"
|
|
16
|
+
| "closeWindow" | "minimize" | "maximize" | "fullscreen"
|
|
17
|
+
| "separator"
|
|
18
|
+
| "undo" | "redo" | "cut" | "copy" | "paste" | "selectAll";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A single native-menu placement for a command or submenu.
|
|
22
|
+
* Desktop-app only; ignored in PWA builds.
|
|
23
|
+
*
|
|
24
|
+
* - `location`: id of the target menu or submenu (e.g. "file",
|
|
25
|
+
* "edit.format").
|
|
26
|
+
* - `group`: sort-prefix convention "1_new", "2_dashboard", "9_close". Items
|
|
27
|
+
* are partitioned by group within a location; groups sort alphabetically
|
|
28
|
+
* with separators auto-inserted between them.
|
|
29
|
+
* - `order`: ascending within a group; tiebreak by label.
|
|
30
|
+
* - `label`: overrides the command's display label in the menu only.
|
|
31
|
+
* - `predefined`: emit an OS-native predefined item instead of a
|
|
32
|
+
* command-dispatching item.
|
|
33
|
+
* - `icon`: optional icon identifier; platform support varies.
|
|
34
|
+
*/
|
|
35
|
+
export type MenuContribution = {
|
|
36
|
+
location: string;
|
|
37
|
+
group?: string;
|
|
38
|
+
order?: number;
|
|
39
|
+
label?: string;
|
|
40
|
+
predefined?: PredefinedMenuKind;
|
|
41
|
+
icon?: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A menu placement value: a single contribution, an array (multiple
|
|
46
|
+
* placements), or `null` (hide this entry on the matching platform).
|
|
47
|
+
* Desktop-app only.
|
|
48
|
+
*/
|
|
49
|
+
export type MenuPlacement = MenuContribution | MenuContribution[] | null;
|
|
50
|
+
|
|
9
51
|
export type CommandDef = {
|
|
10
52
|
name: string;
|
|
11
53
|
|
|
@@ -21,6 +63,27 @@ export type CommandDef = {
|
|
|
21
63
|
hide?: boolean;
|
|
22
64
|
requireMode?: "rw" | "ro";
|
|
23
65
|
requireEditor?: "any" | "page" | "notpage" | string;
|
|
66
|
+
|
|
67
|
+
// When true, this binding is NOT registered while vim mode is active,
|
|
68
|
+
// so the key falls through to vim.
|
|
69
|
+
disableInVim?: boolean;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Desktop-app only. Native menu placement for this command.
|
|
73
|
+
*
|
|
74
|
+
* Naming note: the existing `key`/`mac` pair uses a bare platform name
|
|
75
|
+
* for the override. That pattern doesn't extend cleanly here — bare
|
|
76
|
+
* `mac` is already taken by the keybinding override, and `windows` /
|
|
77
|
+
* `linux` as top-level fields would be ambiguous. The `menu` + capital
|
|
78
|
+
* platform suffix pattern keeps overrides unambiguous.
|
|
79
|
+
*/
|
|
80
|
+
menu?: MenuPlacement;
|
|
81
|
+
/** Platform-specific override: replaces `menu` on macOS. */
|
|
82
|
+
menuMac?: MenuPlacement;
|
|
83
|
+
/** Platform-specific override: replaces `menu` on Windows. */
|
|
84
|
+
menuWindows?: MenuPlacement;
|
|
85
|
+
/** Platform-specific override: replaces `menu` on Linux. */
|
|
86
|
+
menuLinux?: MenuPlacement;
|
|
24
87
|
};
|
|
25
88
|
export type CommandHookT = {
|
|
26
89
|
command?: CommandDef;
|
|
@@ -81,15 +144,14 @@ export type DocumentEditorT = {
|
|
|
81
144
|
*
|
|
82
145
|
* related: plugos/ui_types.ts#FunctionDef
|
|
83
146
|
*/
|
|
84
|
-
export type SilverBulletHooks =
|
|
85
|
-
&
|
|
86
|
-
&
|
|
87
|
-
&
|
|
88
|
-
&
|
|
89
|
-
&
|
|
90
|
-
&
|
|
91
|
-
|
|
92
|
-
& SyscallHookT;
|
|
147
|
+
export type SilverBulletHooks = CommandHookT &
|
|
148
|
+
SlashCommandHookT &
|
|
149
|
+
MQHookT &
|
|
150
|
+
EventHookT &
|
|
151
|
+
CodeWidgetT &
|
|
152
|
+
PlugNamespaceHookT &
|
|
153
|
+
DocumentEditorT &
|
|
154
|
+
SyscallHookT;
|
|
93
155
|
|
|
94
156
|
/** A plug manifest configures hooks, declares syntax extensions, and describes plug metadata.
|
|
95
157
|
*
|
package/plugs/builtin_plugs.ts
CHANGED
|
@@ -4,11 +4,11 @@ export const builtinPlugNames = [
|
|
|
4
4
|
"editor",
|
|
5
5
|
"index",
|
|
6
6
|
"sync",
|
|
7
|
-
"plug-manager",
|
|
8
7
|
"emoji",
|
|
9
8
|
"image-viewer",
|
|
9
|
+
"configuration-manager",
|
|
10
10
|
];
|
|
11
11
|
|
|
12
|
-
export const builtinPlugPaths = builtinPlugNames.map(
|
|
13
|
-
`Library/Std/Plugs/${name}.plug.js
|
|
12
|
+
export const builtinPlugPaths = builtinPlugNames.map(
|
|
13
|
+
(name) => `Library/Std/Plugs/${name}.plug.js`,
|
|
14
14
|
);
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { WorkerSandbox } from "./worker_sandbox.ts";
|
|
2
|
-
import type { Plug } from "../plug.ts";
|
|
3
|
-
import type { SandboxFactory } from "./sandbox.ts";
|
|
4
|
-
import { fsEndpoint } from "../../spaces/constants.ts";
|
|
5
|
-
|
|
6
|
-
export function createWorkerSandboxFromLocalPath<HookT>(
|
|
7
|
-
name: string,
|
|
8
|
-
): SandboxFactory<HookT> {
|
|
9
|
-
return (plug: Plug<HookT>) =>
|
|
10
|
-
new WorkerSandbox(
|
|
11
|
-
plug,
|
|
12
|
-
new URL(
|
|
13
|
-
name,
|
|
14
|
-
`${document.baseURI.slice(0, -1) + fsEndpoint}/`, // We're NOT striping trailing '/', this used to be `location.origin`
|
|
15
|
-
),
|
|
16
|
-
);
|
|
17
|
-
}
|
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
// plug-api/lib/crypto.ts
|
|
2
|
-
function base64Decode(s) {
|
|
3
|
-
const binString = atob(s);
|
|
4
|
-
const len = binString.length;
|
|
5
|
-
const bytes = new Uint8Array(len);
|
|
6
|
-
for (let i = 0; i < len; i++) {
|
|
7
|
-
bytes[i] = binString.charCodeAt(i);
|
|
8
|
-
}
|
|
9
|
-
return bytes;
|
|
10
|
-
}
|
|
11
|
-
function base64Encode(buffer) {
|
|
12
|
-
if (typeof buffer === "string") {
|
|
13
|
-
buffer = new TextEncoder().encode(buffer);
|
|
14
|
-
}
|
|
15
|
-
let binary = "";
|
|
16
|
-
const len = buffer.byteLength;
|
|
17
|
-
for (let i = 0; i < len; i++) {
|
|
18
|
-
binary += String.fromCharCode(buffer[i]);
|
|
19
|
-
}
|
|
20
|
-
return btoa(binary);
|
|
21
|
-
}
|
|
22
|
-
var fixedCounter = new Uint8Array(16);
|
|
23
|
-
|
|
24
|
-
// client/lib/logger.ts
|
|
25
|
-
var Logger = class {
|
|
26
|
-
constructor(prefix = "", maxCaptureSize = 1e3) {
|
|
27
|
-
this.prefix = prefix;
|
|
28
|
-
this.maxCaptureSize = maxCaptureSize;
|
|
29
|
-
this.prefix = prefix;
|
|
30
|
-
this.originalConsole = {
|
|
31
|
-
log: console.log.bind(console),
|
|
32
|
-
info: console.info.bind(console),
|
|
33
|
-
warn: console.warn.bind(console),
|
|
34
|
-
error: console.error.bind(console),
|
|
35
|
-
debug: console.debug.bind(console)
|
|
36
|
-
};
|
|
37
|
-
this.patchConsole();
|
|
38
|
-
}
|
|
39
|
-
originalConsole;
|
|
40
|
-
logBuffer = [];
|
|
41
|
-
patchConsole() {
|
|
42
|
-
const createPatchedMethod = (level) => {
|
|
43
|
-
return (...args) => {
|
|
44
|
-
const prefixedArgs = this.prefix ? [this.prefix, ...args] : args;
|
|
45
|
-
this.originalConsole[level](...prefixedArgs);
|
|
46
|
-
this.captureLog(level, args);
|
|
47
|
-
};
|
|
48
|
-
};
|
|
49
|
-
console.log = createPatchedMethod("log");
|
|
50
|
-
console.info = createPatchedMethod("info");
|
|
51
|
-
console.warn = createPatchedMethod("warn");
|
|
52
|
-
console.error = createPatchedMethod("error");
|
|
53
|
-
console.debug = createPatchedMethod("debug");
|
|
54
|
-
}
|
|
55
|
-
captureLog(level, args) {
|
|
56
|
-
const entry = {
|
|
57
|
-
level,
|
|
58
|
-
timestamp: Date.now(),
|
|
59
|
-
message: args.map((arg) => {
|
|
60
|
-
if (typeof arg === "string") {
|
|
61
|
-
return arg;
|
|
62
|
-
}
|
|
63
|
-
try {
|
|
64
|
-
return JSON.stringify(arg);
|
|
65
|
-
} catch {
|
|
66
|
-
return String(arg);
|
|
67
|
-
}
|
|
68
|
-
}).join(" ")
|
|
69
|
-
};
|
|
70
|
-
this.logBuffer.push(entry);
|
|
71
|
-
if (this.logBuffer.length > this.maxCaptureSize) {
|
|
72
|
-
this.logBuffer.shift();
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Posts all buffered logs to a server endpoint
|
|
77
|
-
*/
|
|
78
|
-
async postToServer(logEndpoint, source) {
|
|
79
|
-
const logs = this.logBuffer;
|
|
80
|
-
if (logs.length > 0) {
|
|
81
|
-
const logCopy = [...this.logBuffer];
|
|
82
|
-
this.logBuffer = [];
|
|
83
|
-
try {
|
|
84
|
-
const resp = await fetch(logEndpoint, {
|
|
85
|
-
method: "POST",
|
|
86
|
-
headers: {
|
|
87
|
-
"Content-Type": "application/json"
|
|
88
|
-
},
|
|
89
|
-
body: JSON.stringify(logCopy.map((entry) => ({ ...entry, source })))
|
|
90
|
-
});
|
|
91
|
-
if (!resp.ok) {
|
|
92
|
-
throw new Error("Failed to post logs to server");
|
|
93
|
-
}
|
|
94
|
-
} catch (e) {
|
|
95
|
-
console.warn("Could not post logs to server", e.message);
|
|
96
|
-
this.logBuffer.unshift(...logCopy);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
var globalLogger;
|
|
102
|
-
function initLogger(prefix = "") {
|
|
103
|
-
globalLogger = new Logger(prefix);
|
|
104
|
-
return globalLogger;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// client/plugos/worker_runtime.ts
|
|
108
|
-
var workerPostMessage = (_msg) => {
|
|
109
|
-
throw new Error("Not initialized yet");
|
|
110
|
-
};
|
|
111
|
-
var runningAsWebWorker = typeof window === "undefined" && // @ts-expect-error: globalThis
|
|
112
|
-
typeof globalThis.WebSocketPair === "undefined";
|
|
113
|
-
var pendingRequests = /* @__PURE__ */ new Map();
|
|
114
|
-
var syscallReqId = 0;
|
|
115
|
-
if (runningAsWebWorker) {
|
|
116
|
-
globalThis.syscall = async (name, ...args) => {
|
|
117
|
-
return await new Promise((resolve, reject) => {
|
|
118
|
-
syscallReqId++;
|
|
119
|
-
pendingRequests.set(syscallReqId, { resolve, reject });
|
|
120
|
-
workerPostMessage({
|
|
121
|
-
type: "sys",
|
|
122
|
-
id: syscallReqId,
|
|
123
|
-
name,
|
|
124
|
-
args
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
function setupMessageListener(functionMapping, manifest, postMessageFn) {
|
|
130
|
-
if (!runningAsWebWorker) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
workerPostMessage = postMessageFn;
|
|
134
|
-
self.addEventListener("message", (event) => {
|
|
135
|
-
(async () => {
|
|
136
|
-
const data = event.data;
|
|
137
|
-
switch (data.type) {
|
|
138
|
-
case "inv":
|
|
139
|
-
{
|
|
140
|
-
const fn = functionMapping[data.name];
|
|
141
|
-
if (!fn) {
|
|
142
|
-
throw new Error(`Function not loaded: ${data.name}`);
|
|
143
|
-
}
|
|
144
|
-
try {
|
|
145
|
-
const result = await Promise.resolve(fn(...data.args || []));
|
|
146
|
-
workerPostMessage({
|
|
147
|
-
type: "invr",
|
|
148
|
-
id: data.id,
|
|
149
|
-
result
|
|
150
|
-
});
|
|
151
|
-
} catch (e) {
|
|
152
|
-
console.error(
|
|
153
|
-
"An exception was thrown as a result of invoking function",
|
|
154
|
-
data.name,
|
|
155
|
-
"error:",
|
|
156
|
-
e.message
|
|
157
|
-
);
|
|
158
|
-
workerPostMessage({
|
|
159
|
-
type: "invr",
|
|
160
|
-
id: data.id,
|
|
161
|
-
error: e.message
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
break;
|
|
166
|
-
case "sysr":
|
|
167
|
-
{
|
|
168
|
-
const syscallId = data.id;
|
|
169
|
-
const lookup = pendingRequests.get(syscallId);
|
|
170
|
-
if (!lookup) {
|
|
171
|
-
throw Error("Invalid request id");
|
|
172
|
-
}
|
|
173
|
-
pendingRequests.delete(syscallId);
|
|
174
|
-
if (data.error) {
|
|
175
|
-
lookup.reject(new Error(data.error));
|
|
176
|
-
} else {
|
|
177
|
-
lookup.resolve(data.result);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
break;
|
|
181
|
-
}
|
|
182
|
-
})().catch(console.error);
|
|
183
|
-
});
|
|
184
|
-
workerPostMessage({
|
|
185
|
-
type: "manifest",
|
|
186
|
-
manifest
|
|
187
|
-
});
|
|
188
|
-
initLogger(`[${manifest.name} plug]`);
|
|
189
|
-
}
|
|
190
|
-
async function sandboxFetch(reqInfo, options) {
|
|
191
|
-
if (typeof reqInfo !== "string") {
|
|
192
|
-
const body = new Uint8Array(await reqInfo.arrayBuffer());
|
|
193
|
-
const encodedBody = body.length > 0 ? base64Encode(body) : void 0;
|
|
194
|
-
options = {
|
|
195
|
-
method: reqInfo.method,
|
|
196
|
-
headers: Object.fromEntries(reqInfo.headers.entries()),
|
|
197
|
-
base64Body: encodedBody
|
|
198
|
-
};
|
|
199
|
-
reqInfo = reqInfo.url;
|
|
200
|
-
}
|
|
201
|
-
return syscall("sandboxFetch.fetch", reqInfo, options);
|
|
202
|
-
}
|
|
203
|
-
globalThis.nativeFetch = globalThis.fetch;
|
|
204
|
-
function monkeyPatchFetch() {
|
|
205
|
-
globalThis.fetch = async (reqInfo, init) => {
|
|
206
|
-
const encodedBody = init?.body ? base64Encode(
|
|
207
|
-
new Uint8Array(await new Response(init.body).arrayBuffer())
|
|
208
|
-
) : void 0;
|
|
209
|
-
const r = await sandboxFetch(
|
|
210
|
-
reqInfo,
|
|
211
|
-
init && {
|
|
212
|
-
method: init.method,
|
|
213
|
-
headers: init.headers,
|
|
214
|
-
base64Body: encodedBody
|
|
215
|
-
}
|
|
216
|
-
);
|
|
217
|
-
return new Response(
|
|
218
|
-
r.base64Body ? base64Decode(r.base64Body) : null,
|
|
219
|
-
{
|
|
220
|
-
status: r.status,
|
|
221
|
-
headers: r.headers
|
|
222
|
-
}
|
|
223
|
-
);
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
if (runningAsWebWorker) {
|
|
227
|
-
monkeyPatchFetch();
|
|
228
|
-
}
|
|
229
|
-
export {
|
|
230
|
-
monkeyPatchFetch,
|
|
231
|
-
sandboxFetch,
|
|
232
|
-
setupMessageListener
|
|
233
|
-
};
|