@sdeck/plugin 0.0.0-stage → 0.2.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/dist/bridge.d.ts +48 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/connection.d.ts +44 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +774 -0
- package/dist/index.js.map +17 -0
- package/dist/manifest.d.ts +45 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/permissions.d.ts +31 -0
- package/dist/permissions.d.ts.map +1 -0
- package/dist/plugin.d.ts +332 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/settings.d.ts +135 -0
- package/dist/settings.d.ts.map +1 -0
- package/dist/shared.d.ts +9 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/ui.d.ts +4 -0
- package/dist/ui.d.ts.map +1 -0
- package/package.json +41 -4
- package/src/bridge.ts +134 -0
- package/src/connection.ts +206 -0
- package/src/index.ts +93 -0
- package/src/manifest.ts +85 -0
- package/src/permissions.ts +119 -0
- package/src/plugin.ts +571 -0
- package/src/settings.ts +293 -0
- package/src/shared.ts +34 -0
- package/src/ui.ts +3 -0
- package/README.md +0 -3
package/src/manifest.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { PLUGIN_API_VERSION } from "./plugin.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What makes an npm package an sdeck plugin. In its `package.json`:
|
|
5
|
+
*
|
|
6
|
+
* ```json
|
|
7
|
+
* {
|
|
8
|
+
* "name": "sdeck-plugin-weather",
|
|
9
|
+
* "version": "1.0.0",
|
|
10
|
+
* "type": "module",
|
|
11
|
+
* "keywords": ["sdeck-plugin"],
|
|
12
|
+
* "sdeck": { "apiVersion": 2, "entry": "./dist/index.js" }
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* The entry's default export is the `definePlugin(...)` result. `react`, `@sdeck/plugin`,
|
|
17
|
+
* `@sdeck/app`, `@sdeck/core`, `@sdeck/engine` and `@sdeck/schema` are provided by the host: list them as peer dependencies and
|
|
18
|
+
* keep them out of the bundle.
|
|
19
|
+
*/
|
|
20
|
+
export interface PluginManifest {
|
|
21
|
+
/** Package name; also the plugin id. */
|
|
22
|
+
name: string;
|
|
23
|
+
version: string;
|
|
24
|
+
description?: string;
|
|
25
|
+
/** Path of the module to import, relative to the package (default: `main`, else `index.js`). */
|
|
26
|
+
entry: string;
|
|
27
|
+
apiVersion: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The npm keyword that lists a package in the plugin registry. */
|
|
31
|
+
export const PLUGIN_KEYWORD = "sdeck-plugin";
|
|
32
|
+
|
|
33
|
+
/** Modules the host provides to plugins; plugins must not bundle their own copy. */
|
|
34
|
+
export const HOST_MODULES = [
|
|
35
|
+
"react",
|
|
36
|
+
"react/jsx-runtime",
|
|
37
|
+
"react/jsx-dev-runtime",
|
|
38
|
+
"@sdeck/plugin",
|
|
39
|
+
"@sdeck/app",
|
|
40
|
+
"@sdeck/core",
|
|
41
|
+
"@sdeck/engine",
|
|
42
|
+
"@sdeck/schema",
|
|
43
|
+
] as const;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Plugin APIs this host loads. Version 1 plugins (actions that draw themselves) are a subset of
|
|
47
|
+
* version 2 and run unchanged.
|
|
48
|
+
*/
|
|
49
|
+
export const SUPPORTED_API_VERSIONS: readonly number[] = [1, PLUGIN_API_VERSION];
|
|
50
|
+
|
|
51
|
+
export type ManifestResult = { ok: true; manifest: PluginManifest } | { ok: false; error: string };
|
|
52
|
+
|
|
53
|
+
/** Reads the plugin manifest out of a parsed `package.json`. */
|
|
54
|
+
export function parseManifest(pkg: unknown): ManifestResult {
|
|
55
|
+
if (!pkg || typeof pkg !== "object") return { ok: false, error: "package.json is not an object" };
|
|
56
|
+
const p = pkg as Record<string, unknown>;
|
|
57
|
+
const sdeck = p.sdeck as Record<string, unknown> | undefined;
|
|
58
|
+
if (typeof p.name !== "string") return { ok: false, error: "package.json has no name" };
|
|
59
|
+
if (!sdeck || typeof sdeck !== "object") {
|
|
60
|
+
return { ok: false, error: `${p.name} is not an sdeck plugin (no "sdeck" field)` };
|
|
61
|
+
}
|
|
62
|
+
const apiVersion = sdeck.apiVersion;
|
|
63
|
+
if (typeof apiVersion !== "number" || !SUPPORTED_API_VERSIONS.includes(apiVersion)) {
|
|
64
|
+
return {
|
|
65
|
+
ok: false,
|
|
66
|
+
error: `${p.name} targets plugin API ${String(apiVersion)}, this host supports ${SUPPORTED_API_VERSIONS.join(" and ")}`,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
const entry =
|
|
70
|
+
typeof sdeck.entry === "string"
|
|
71
|
+
? sdeck.entry
|
|
72
|
+
: typeof p.main === "string"
|
|
73
|
+
? p.main
|
|
74
|
+
: "index.js";
|
|
75
|
+
return {
|
|
76
|
+
ok: true,
|
|
77
|
+
manifest: {
|
|
78
|
+
name: p.name,
|
|
79
|
+
version: typeof p.version === "string" ? p.version : "0.0.0",
|
|
80
|
+
...(typeof p.description === "string" && { description: p.description }),
|
|
81
|
+
entry,
|
|
82
|
+
apiVersion,
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a plugin can ask to do. The user decides about each permission separately, and a plugin
|
|
3
|
+
* gets nothing it did not ask for and nothing the user did not allow.
|
|
4
|
+
*/
|
|
5
|
+
export type Permission =
|
|
6
|
+
| "network"
|
|
7
|
+
| "filesystem"
|
|
8
|
+
| "process"
|
|
9
|
+
| "shell"
|
|
10
|
+
| "input"
|
|
11
|
+
| "clipboard"
|
|
12
|
+
| "audio"
|
|
13
|
+
| "environment"
|
|
14
|
+
| "variables"
|
|
15
|
+
| "state"
|
|
16
|
+
| "actions"
|
|
17
|
+
| "native";
|
|
18
|
+
|
|
19
|
+
/** A permission as the user reads it. */
|
|
20
|
+
export interface PermissionText {
|
|
21
|
+
name: string;
|
|
22
|
+
/** What allowing it makes possible. */
|
|
23
|
+
description: string;
|
|
24
|
+
/** Finishes "the plugin is not allowed to …". */
|
|
25
|
+
action: string;
|
|
26
|
+
/** Allowing it lets the plugin do nearly anything the user can. */
|
|
27
|
+
powerful?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Every permission, in the order the UI lists them. */
|
|
31
|
+
export const PERMISSION_TEXTS: Readonly<Record<Permission, PermissionText>> = {
|
|
32
|
+
network: {
|
|
33
|
+
name: "Network",
|
|
34
|
+
description: "Connect to the internet and to devices on your network.",
|
|
35
|
+
action: "use the network",
|
|
36
|
+
},
|
|
37
|
+
filesystem: {
|
|
38
|
+
name: "Files",
|
|
39
|
+
description: "Read and change your files. Its own data folder is always allowed.",
|
|
40
|
+
action: "use files outside its own folder",
|
|
41
|
+
},
|
|
42
|
+
process: {
|
|
43
|
+
name: "Start programs",
|
|
44
|
+
description: "Open apps and run programs that are installed on this computer.",
|
|
45
|
+
action: "start programs",
|
|
46
|
+
powerful: true,
|
|
47
|
+
},
|
|
48
|
+
shell: {
|
|
49
|
+
name: "Run commands",
|
|
50
|
+
description: "Run any command line. Commands can do everything you can.",
|
|
51
|
+
action: "run commands",
|
|
52
|
+
powerful: true,
|
|
53
|
+
},
|
|
54
|
+
input: {
|
|
55
|
+
name: "Keyboard and mouse",
|
|
56
|
+
description: "Press keys, type text and control media playback.",
|
|
57
|
+
action: "press keys",
|
|
58
|
+
},
|
|
59
|
+
clipboard: {
|
|
60
|
+
name: "Clipboard",
|
|
61
|
+
description: "Read and change what you copied.",
|
|
62
|
+
action: "use the clipboard",
|
|
63
|
+
},
|
|
64
|
+
audio: {
|
|
65
|
+
name: "Sound",
|
|
66
|
+
description: "Change volumes, mute apps and switch audio devices.",
|
|
67
|
+
action: "control sound",
|
|
68
|
+
},
|
|
69
|
+
environment: {
|
|
70
|
+
name: "Environment variables",
|
|
71
|
+
description: "Read environment variables, which can contain keys and tokens.",
|
|
72
|
+
action: "read environment variables",
|
|
73
|
+
},
|
|
74
|
+
variables: {
|
|
75
|
+
name: "Your variables",
|
|
76
|
+
description: "Read and change the variables your workflows use.",
|
|
77
|
+
action: "use your variables",
|
|
78
|
+
},
|
|
79
|
+
state: {
|
|
80
|
+
name: "Other plugins' values",
|
|
81
|
+
description: "Read live values from other plugins, such as the app in front.",
|
|
82
|
+
action: "read other plugins' values",
|
|
83
|
+
},
|
|
84
|
+
actions: {
|
|
85
|
+
name: "Other plugins' actions",
|
|
86
|
+
description: "Run actions of other plugins, with the permissions those have.",
|
|
87
|
+
action: "run other plugins' actions",
|
|
88
|
+
powerful: true,
|
|
89
|
+
},
|
|
90
|
+
native: {
|
|
91
|
+
name: "Native code",
|
|
92
|
+
description: "Load compiled code, which sdeck cannot limit in any way.",
|
|
93
|
+
action: "load native code",
|
|
94
|
+
powerful: true,
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export const PERMISSIONS = Object.keys(PERMISSION_TEXTS) as readonly Permission[];
|
|
99
|
+
|
|
100
|
+
/** Thrown when a plugin does something the user has not allowed. The message is for the user. */
|
|
101
|
+
export class PermissionError extends Error {
|
|
102
|
+
override readonly name = "PermissionError";
|
|
103
|
+
/** As Node's file system reports a refused path, so libraries treat it the same way. */
|
|
104
|
+
readonly code = "EACCES";
|
|
105
|
+
|
|
106
|
+
constructor(
|
|
107
|
+
/** The plugin's name or id. */
|
|
108
|
+
plugin: string,
|
|
109
|
+
readonly permission: Permission,
|
|
110
|
+
/** What was refused, when that helps (a path, an address). */
|
|
111
|
+
detail?: string,
|
|
112
|
+
) {
|
|
113
|
+
const { name, action } = PERMISSION_TEXTS[permission];
|
|
114
|
+
super(
|
|
115
|
+
`${plugin} is not allowed to ${action}${detail ? ` (${detail})` : ""}. ` +
|
|
116
|
+
`Allow "${name}" in the plugin's permissions.`,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
}
|