@silverbulletmd/silverbullet 2.5.3 → 2.6.1
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 -5
- package/client/asset_bundle/bundle.ts +3 -9
- package/client/data/datastore.ts +4 -5
- package/client/markdown_parser/constants.ts +3 -2
- package/client/plugos/hooks/code_widget.ts +3 -5
- package/client/plugos/hooks/command.ts +8 -8
- 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 +12 -27
- package/client/plugos/hooks/syscall.ts +3 -3
- package/client/plugos/manifest_cache.ts +22 -15
- package/client/plugos/plug.ts +2 -5
- package/client/plugos/plug_compile.ts +67 -65
- package/client/plugos/protocol.ts +28 -28
- package/client/plugos/proxy_fetch.ts +7 -6
- package/client/plugos/sandboxes/worker_sandbox.ts +16 -15
- package/client/plugos/syscalls/asset.ts +1 -3
- package/client/plugos/syscalls/code_widget.ts +1 -3
- package/client/plugos/syscalls/config.ts +1 -5
- package/client/plugos/syscalls/datastore.ts +1 -1
- package/client/plugos/syscalls/editor.ts +63 -60
- package/client/plugos/syscalls/event.ts +9 -12
- package/client/plugos/syscalls/fetch.ts +30 -22
- package/client/plugos/syscalls/index.ts +10 -1
- package/client/plugos/syscalls/jsonschema.ts +72 -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 +2 -3
- package/client/plugos/system.ts +4 -10
- package/client/plugos/worker_runtime.ts +4 -3
- package/client/space_lua/aggregates.ts +632 -59
- 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/liq_null.ts +6 -0
- 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/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 +44 -41
- package/package.json +24 -22
- 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 +18 -18
- package/plug-api/lib/resolve.ts +7 -11
- package/plug-api/lib/tags.ts +13 -4
- package/plug-api/lib/transclusion.ts +6 -17
- package/plug-api/lib/tree.ts +115 -43
- package/plug-api/lib/yaml.ts +25 -15
- package/plug-api/syscalls/asset.ts +1 -1
- package/plug-api/syscalls/config.ts +1 -4
- package/plug-api/syscalls/editor.ts +14 -14
- 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 +1 -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 +8 -9
- package/plugs/builtin_plugs.ts +2 -2
- package/dist/worker_runtime_bundle.js +0 -233
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { syscall } from "../syscall.ts";
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
FilterOption,
|
|
4
|
+
NotificationType,
|
|
5
|
+
UploadFile,
|
|
6
|
+
} from "../../plug-api/types/client.ts";
|
|
3
7
|
import type { Path, Ref } from "../lib/ref.ts";
|
|
4
8
|
import type { PageMeta } from "../../plug-api/types/index.ts";
|
|
5
9
|
|
|
@@ -77,9 +81,11 @@ export function getCursor(): Promise<number> {
|
|
|
77
81
|
/**
|
|
78
82
|
* Returns the line number and column number of the cursor in the editor
|
|
79
83
|
*/
|
|
80
|
-
export function getSelection(): Promise<
|
|
81
|
-
|
|
82
|
-
|
|
84
|
+
export function getSelection(): Promise<{
|
|
85
|
+
from: number;
|
|
86
|
+
to: number;
|
|
87
|
+
text: string;
|
|
88
|
+
}> {
|
|
83
89
|
return syscall("editor.getSelection");
|
|
84
90
|
}
|
|
85
91
|
|
|
@@ -218,7 +224,7 @@ export function uploadFile(
|
|
|
218
224
|
*/
|
|
219
225
|
export function flashNotification(
|
|
220
226
|
message: string,
|
|
221
|
-
type:
|
|
227
|
+
type: NotificationType = "info",
|
|
222
228
|
): Promise<void> {
|
|
223
229
|
return syscall("editor.flashNotification", message, type);
|
|
224
230
|
}
|
|
@@ -259,9 +265,7 @@ export function showPanel(
|
|
|
259
265
|
* Hides a panel in the editor
|
|
260
266
|
* @param id the location of the panel to hide
|
|
261
267
|
*/
|
|
262
|
-
export function hidePanel(
|
|
263
|
-
id: "lhs" | "rhs" | "bhs" | "modal",
|
|
264
|
-
): Promise<void> {
|
|
268
|
+
export function hidePanel(id: "lhs" | "rhs" | "bhs" | "modal"): Promise<void> {
|
|
265
269
|
return syscall("editor.hidePanel", id);
|
|
266
270
|
}
|
|
267
271
|
|
|
@@ -360,9 +364,7 @@ export function prompt(
|
|
|
360
364
|
* @param message the message to show in the confirmation dialog
|
|
361
365
|
* @returns
|
|
362
366
|
*/
|
|
363
|
-
export function confirm(
|
|
364
|
-
message: string,
|
|
365
|
-
): Promise<boolean> {
|
|
367
|
+
export function confirm(message: string): Promise<boolean> {
|
|
366
368
|
return syscall("editor.confirm", message);
|
|
367
369
|
}
|
|
368
370
|
|
|
@@ -371,9 +373,7 @@ export function confirm(
|
|
|
371
373
|
* @param message the message to show in the confirmation dialog
|
|
372
374
|
* @returns
|
|
373
375
|
*/
|
|
374
|
-
export function alert(
|
|
375
|
-
message: string,
|
|
376
|
-
): Promise<boolean> {
|
|
376
|
+
export function alert(message: string): Promise<boolean> {
|
|
377
377
|
return syscall("editor.alert", message);
|
|
378
378
|
}
|
|
379
379
|
|
|
@@ -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
|
|
|
@@ -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;
|
|
@@ -81,15 +81,14 @@ export type DocumentEditorT = {
|
|
|
81
81
|
*
|
|
82
82
|
* related: plugos/ui_types.ts#FunctionDef
|
|
83
83
|
*/
|
|
84
|
-
export type SilverBulletHooks =
|
|
85
|
-
&
|
|
86
|
-
&
|
|
87
|
-
&
|
|
88
|
-
&
|
|
89
|
-
&
|
|
90
|
-
&
|
|
91
|
-
|
|
92
|
-
& SyscallHookT;
|
|
84
|
+
export type SilverBulletHooks = CommandHookT &
|
|
85
|
+
SlashCommandHookT &
|
|
86
|
+
MQHookT &
|
|
87
|
+
EventHookT &
|
|
88
|
+
CodeWidgetT &
|
|
89
|
+
PlugNamespaceHookT &
|
|
90
|
+
DocumentEditorT &
|
|
91
|
+
SyscallHookT;
|
|
93
92
|
|
|
94
93
|
/** A plug manifest configures hooks, declares syntax extensions, and describes plug metadata.
|
|
95
94
|
*
|
package/plugs/builtin_plugs.ts
CHANGED
|
@@ -9,6 +9,6 @@ export const builtinPlugNames = [
|
|
|
9
9
|
"image-viewer",
|
|
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,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
|
-
};
|