@dooc-dev/dooc 0.0.3 → 0.0.5
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/cli.js +98 -56
- package/dist/compiler/asset-references.d.ts +4 -0
- package/dist/compiler/asset-references.js +117 -0
- package/dist/compiler/bundle-urls.d.ts +1 -1
- package/dist/compiler/bundle-urls.js +15 -5
- package/dist/compiler/check.js +8 -5
- package/dist/compiler/code-languages.js +7 -2
- package/dist/compiler/compile.d.ts +5 -0
- package/dist/compiler/compile.js +105 -10
- package/dist/compiler/config.js +2 -1
- package/dist/compiler/export.d.ts +4 -0
- package/dist/compiler/export.js +140 -0
- package/dist/compiler/figures.js +4 -0
- package/dist/compiler/files.d.ts +1 -0
- package/dist/compiler/files.js +96 -38
- package/dist/compiler/fonts.js +4 -1
- package/dist/compiler/icons.d.ts +1 -1
- package/dist/compiler/icons.js +11 -2
- package/dist/compiler/ignored-files.d.ts +3 -0
- package/dist/compiler/ignored-files.js +68 -0
- package/dist/compiler/locales.js +13 -8
- package/dist/compiler/manifest.d.ts +2 -0
- package/dist/compiler/manifest.js +9 -2
- package/dist/compiler/ops.d.ts +6 -0
- package/dist/compiler/ops.js +98 -7
- package/dist/compiler/subformats.d.ts +24 -0
- package/dist/compiler/subformats.js +299 -0
- package/dist/compiler/textfile.js +3 -0
- package/dist/compiler/theme-variants.js +5 -2
- package/dist/core/content-metadata.d.ts +15 -6
- package/dist/core/content-metadata.js +278 -17
- package/dist/core/content-types.d.ts +3 -1
- package/dist/core/figure-frame.js +19 -1
- package/dist/core/files.d.ts +2 -1
- package/dist/core/files.js +6 -8
- package/dist/core/flow-base-css.generated.js +1 -1
- package/dist/core/html.js +3 -3
- package/dist/core/icons.d.ts +3 -0
- package/dist/core/icons.js +3 -0
- package/dist/core/json-tools.d.ts +1 -0
- package/dist/core/json-tools.js +12 -0
- package/dist/core/routing.d.ts +5 -2
- package/dist/core/routing.js +17 -5
- package/dist/core/subformat-record.d.ts +3 -0
- package/dist/core/subformat-record.js +131 -0
- package/dist/core/subformats.d.ts +43 -0
- package/dist/core/subformats.js +81 -0
- package/dist/core/toc-ui.js +25 -23
- package/dist/edit.global.js +322 -95
- package/dist/mcp.js +50 -9
- package/dist/plugins/index.d.ts +1 -0
- package/dist/plugins/local-storage.js +3 -0
- package/dist/plugins/oauth.d.ts +1 -1
- package/dist/plugins/oauth.js +8 -2
- package/dist/plugins/runtime.d.ts +6 -0
- package/dist/plugins/runtime.js +166 -34
- package/dist/plugins/transport.d.ts +22 -0
- package/dist/plugins/transport.js +1 -0
- package/dist/plugins/types.d.ts +3 -1
- package/dist/plugins.global.js +128 -0
- package/dist/subformats.global.js +127 -0
- package/dist/ui.global.js +91 -38
- package/dist/viewer/build-plugins.d.ts +7 -0
- package/dist/viewer/build-plugins.js +70 -0
- package/dist/viewer/build.d.ts +9 -0
- package/dist/viewer/build.js +32 -5
- package/dist/viewer/editor-tools.d.ts +7 -7
- package/dist/viewer/editor-tools.js +36 -5
- package/dist/viewer/font-picker.js +5 -1
- package/dist/viewer/git-diff.d.ts +3 -0
- package/dist/viewer/git-diff.js +47 -0
- package/dist/viewer/hmr.js +4 -1
- package/dist/viewer/i18n.js +7 -5
- package/dist/viewer/language-picker.d.ts +2 -2
- package/dist/viewer/language-picker.js +26 -9
- package/dist/viewer/lazy-graph.d.ts +2 -0
- package/dist/viewer/lazy-graph.js +30 -0
- package/dist/viewer/locales/eng.json +74 -10
- package/dist/viewer/locales/jpn.json +75 -11
- package/dist/viewer/locales/zho.json +75 -11
- package/dist/viewer/nav-focus.js +8 -1
- package/dist/viewer/nav-update.d.ts +2 -0
- package/dist/viewer/nav-update.js +117 -0
- package/dist/viewer/plugin-style.d.ts +1 -0
- package/dist/viewer/plugin-style.js +82 -0
- package/dist/viewer/plugins.js +46 -3
- package/dist/viewer/resize.d.ts +0 -2
- package/dist/viewer/resize.js +2 -98
- package/dist/viewer/resource-library.d.ts +10 -8
- package/dist/viewer/resource-library.js +179 -30
- package/dist/viewer/search.d.ts +4 -2
- package/dist/viewer/search.js +252 -53
- package/dist/viewer/serve.d.ts +10 -5
- package/dist/viewer/serve.js +882 -287
- package/dist/viewer/site-icon.js +13 -6
- package/dist/viewer/site-title.d.ts +13 -0
- package/dist/viewer/site-title.js +50 -0
- package/dist/viewer/textfile.d.ts +3 -0
- package/dist/viewer/textfile.js +19 -2
- package/dist/viewer/theme-picker.d.ts +2 -0
- package/dist/viewer/theme-picker.js +24 -8
- package/dist/viewer/workspace.d.ts +2 -0
- package/dist/viewer/workspace.js +46 -1
- package/package.json +24 -9
- package/dist/compiler/bundle.d.ts +0 -2
- package/dist/compiler/bundle.js +0 -100
package/dist/mcp.js
CHANGED
|
@@ -8,10 +8,13 @@ import { createInterface } from "node:readline";
|
|
|
8
8
|
import { checkSite } from "./compiler/check.js";
|
|
9
9
|
import { CHECK_KINDS } from "./compiler/check-types.js";
|
|
10
10
|
import { createCompiler } from "./compiler/compile.js";
|
|
11
|
+
import { exportBundle } from "./compiler/export.js";
|
|
11
12
|
import { filesSnapshot, updateFiles, uploadFile } from "./compiler/files.js";
|
|
12
13
|
import { addProjectFont, listProjectFonts, removeProjectFont } from "./compiler/fonts.js";
|
|
13
14
|
import { createDocOps, isOpError } from "./compiler/ops.js";
|
|
15
|
+
import { validateSubformatFile } from "./compiler/subformats.js";
|
|
14
16
|
import { readTextfile, textfilePath, writeTextfile } from "./compiler/textfile.js";
|
|
17
|
+
import { MANIFEST_FILE } from "./core/content-metadata.js";
|
|
15
18
|
import { localFile } from "./core/files.js";
|
|
16
19
|
import { docDisplayName } from "./core/taxonomy.js";
|
|
17
20
|
import { buildBundle } from "./viewer/build.js";
|
|
@@ -40,11 +43,37 @@ export const createTools = (root, options = {}) => {
|
|
|
40
43
|
return file ? (compiled.sources.get(file)?.errors ?? []) : [];
|
|
41
44
|
};
|
|
42
45
|
return {
|
|
46
|
+
export: {
|
|
47
|
+
description: "Collect dooc.yaml and its source dependencies into dist/bundle.zip, without executing plugins or fetching remote data.",
|
|
48
|
+
props: {},
|
|
49
|
+
required: [],
|
|
50
|
+
run: () => {
|
|
51
|
+
const file = "dist/bundle.zip";
|
|
52
|
+
const data = exportBundle(root);
|
|
53
|
+
const path = textfilePath(root, file);
|
|
54
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
55
|
+
const temp = join(dirname(path), `.export-${randomUUID()}.tmp`);
|
|
56
|
+
try {
|
|
57
|
+
writeFileSync(temp, data, { flag: "wx" });
|
|
58
|
+
renameSync(temp, path);
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
rmSync(temp, { force: true });
|
|
62
|
+
}
|
|
63
|
+
return { file, path, bytes: data.length };
|
|
64
|
+
},
|
|
65
|
+
},
|
|
43
66
|
build: {
|
|
44
67
|
description: "Build dist/bundle.html or dist/bundle.zip. url_preview=embed snapshots embedded URL content; network preserves URL references. Defaults to embed. Returns the output path, byte count and document count.",
|
|
45
68
|
props: {
|
|
46
69
|
format: { ...str, enum: ["html", "zip"] },
|
|
47
70
|
url_preview: { ...str, enum: ["embed", "network"] },
|
|
71
|
+
mode: { ...str, enum: ["static", "live"] },
|
|
72
|
+
auth: { ...str, enum: ["external"] },
|
|
73
|
+
source_url: {
|
|
74
|
+
...str,
|
|
75
|
+
description: "Public source directory URL for card copy links in HTML, relative to dooc.yaml.",
|
|
76
|
+
},
|
|
48
77
|
},
|
|
49
78
|
required: ["format"],
|
|
50
79
|
run: async (a) => {
|
|
@@ -52,7 +81,10 @@ export const createTools = (root, options = {}) => {
|
|
|
52
81
|
textfilePath(root, file);
|
|
53
82
|
const bundle = await buildBundle(root, {
|
|
54
83
|
format: a.format,
|
|
55
|
-
urlPreview: (a.url_preview ?? "embed"),
|
|
84
|
+
urlPreview: (a.url_preview ?? (a.mode === "live" ? "network" : "embed")),
|
|
85
|
+
mode: a.mode,
|
|
86
|
+
auth: a.auth,
|
|
87
|
+
sourceUrl: a.source_url,
|
|
56
88
|
});
|
|
57
89
|
const path = textfilePath(root, file);
|
|
58
90
|
mkdirSync(dirname(path), { recursive: true });
|
|
@@ -69,7 +101,7 @@ export const createTools = (root, options = {}) => {
|
|
|
69
101
|
path,
|
|
70
102
|
bytes: Buffer.byteLength(bundle.data),
|
|
71
103
|
docs: bundle.docs,
|
|
72
|
-
url_preview: a.url_preview ?? "embed",
|
|
104
|
+
url_preview: a.url_preview ?? (a.mode === "live" ? "network" : "embed"),
|
|
73
105
|
};
|
|
74
106
|
},
|
|
75
107
|
},
|
|
@@ -88,9 +120,12 @@ export const createTools = (root, options = {}) => {
|
|
|
88
120
|
description: "Create or update a local UTF-8 bundle file. base must equal the content from read_file, or null to create a new file. Conflicting edits are never overwritten.",
|
|
89
121
|
props: { file: str, content: str, base: { type: ["string", "null"] } },
|
|
90
122
|
required: ["file", "content", "base"],
|
|
91
|
-
run: (a) =>
|
|
92
|
-
|
|
93
|
-
|
|
123
|
+
run: (a) => {
|
|
124
|
+
validateSubformatFile(root, fileArg(a.file), a.content);
|
|
125
|
+
return writeTextfile(root, fileArg(a.file), a.content, a.base)
|
|
126
|
+
? { file: a.file, ok: true }
|
|
127
|
+
: { error: "File changed. Read it again before writing.", status: 409 };
|
|
128
|
+
},
|
|
94
129
|
},
|
|
95
130
|
upload_file: {
|
|
96
131
|
description: "Import a binary or text file from base64 data using the bundle file layout. Existing files are preserved with a unique name. Maximum 64 MiB.",
|
|
@@ -99,7 +134,7 @@ export const createTools = (root, options = {}) => {
|
|
|
99
134
|
run: (a) => uploadFile(root, a),
|
|
100
135
|
},
|
|
101
136
|
update_files: {
|
|
102
|
-
description: "Rename a file (file, name), move files/folders (paths, directory
|
|
137
|
+
description: "Rename a file (file, name), move files/folders (paths, directory), change automatic layout (enabled), or restructure by extension. Updates document references and variants. Requires revision from list_files.",
|
|
103
138
|
props: {
|
|
104
139
|
action: { ...str, enum: ["rename", "move", "layout", "restructure"] },
|
|
105
140
|
revision: str,
|
|
@@ -107,14 +142,13 @@ export const createTools = (root, options = {}) => {
|
|
|
107
142
|
name: str,
|
|
108
143
|
paths: strings,
|
|
109
144
|
directory: str,
|
|
110
|
-
before: str,
|
|
111
145
|
enabled: bool,
|
|
112
146
|
},
|
|
113
147
|
required: ["action", "revision"],
|
|
114
148
|
run: (a) => updateFiles(root, a),
|
|
115
149
|
},
|
|
116
150
|
list_resources: {
|
|
117
|
-
description: "List Statics:
|
|
151
|
+
description: "List Statics: codeblocks (including term definitions and local text-file embeds, with normalized format and reference locations), URLs from Markdown links, and URL previews from Markdown images, with source files and line numbers.",
|
|
118
152
|
props: {},
|
|
119
153
|
run: () => resourceIndex(root, filesSnapshot(root).files.map((file) => file.path)),
|
|
120
154
|
},
|
|
@@ -190,7 +224,8 @@ export const createTools = (root, options = {}) => {
|
|
|
190
224
|
depth.set(d.id, dep);
|
|
191
225
|
return {
|
|
192
226
|
id: d.id,
|
|
193
|
-
file: d.file,
|
|
227
|
+
file: d.subformat ? MANIFEST_FILE : d.file,
|
|
228
|
+
...(d.subformat ? { subformat: d.subformat.ext } : {}),
|
|
194
229
|
title: docDisplayName(d, d.id),
|
|
195
230
|
parent: d.parent,
|
|
196
231
|
depth: dep,
|
|
@@ -229,6 +264,12 @@ export const createTools = (root, options = {}) => {
|
|
|
229
264
|
const id = String(a.id);
|
|
230
265
|
const compiled = compiler.compile();
|
|
231
266
|
const doc = compiled.docs.find((d) => d.id === id);
|
|
267
|
+
if (doc?.subformat)
|
|
268
|
+
return {
|
|
269
|
+
file: MANIFEST_FILE,
|
|
270
|
+
subformat: doc.subformat,
|
|
271
|
+
errors: compiled.sources.get(doc.file)?.errors ?? [],
|
|
272
|
+
};
|
|
232
273
|
return doc
|
|
233
274
|
? {
|
|
234
275
|
file: doc.file,
|
package/dist/plugins/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export type { PluginAction, PluginActionContext, PluginActionField, PluginActionRequest, PluginActionResult, } from "./actions.js";
|
|
2
2
|
export type { PluginBlock, PluginBlockContent, PluginCell, PluginEdit, PluginIcon, PluginInline, PluginTone, } from "./blocks.js";
|
|
3
3
|
export { PluginError, parseOAuthToken } from "./oauth.js";
|
|
4
|
+
export type { PluginRequest, PluginResponse, PluginTransport } from "./transport.js";
|
|
4
5
|
export type { DataPlugin, OAuthToken, PluginContent, PluginContext, PluginOAuth, PluginUser, PluginView, } from "./types.js";
|
|
@@ -56,6 +56,9 @@ export const createLocalPluginStorage = (userDir = process.env.DOOC_USER_DIR ??
|
|
|
56
56
|
[...plugin.oauth.scopes].sort(),
|
|
57
57
|
[...plugin.apiOrigins].sort(),
|
|
58
58
|
plugin.oauth.authorizationParams ?? {},
|
|
59
|
+
...(plugin.oauth.deviceAuthorizationUrl
|
|
60
|
+
? [plugin.oauth.deviceAuthorizationUrl]
|
|
61
|
+
: []),
|
|
59
62
|
]))
|
|
60
63
|
.digest("hex")}.json`;
|
|
61
64
|
const credentials = {
|
package/dist/plugins/oauth.d.ts
CHANGED
|
@@ -10,5 +10,5 @@ export declare const httpsUrl: (source: string) => URL;
|
|
|
10
10
|
/** Use after selecting a provider's user-token object in parseToken. */
|
|
11
11
|
export declare const parseOAuthToken: (input: unknown) => OAuthToken;
|
|
12
12
|
/** Bound both transfer time and decoded size; never include remote error bodies in errors. */
|
|
13
|
-
export declare const fetchPluginJson: (url: string, init: RequestInit, tokenExchange?: boolean, transport?: typeof fetch) => Promise<unknown>;
|
|
13
|
+
export declare const fetchPluginJson: (url: string, init: RequestInit, tokenExchange?: boolean | "device", transport?: typeof fetch) => Promise<unknown>;
|
|
14
14
|
export declare const validatePlugins: (input: unknown) => DataPlugin[];
|
package/dist/plugins/oauth.js
CHANGED
|
@@ -59,7 +59,7 @@ export const fetchPluginJson = async (url, init, tokenExchange = false, transpor
|
|
|
59
59
|
cache: "no-store",
|
|
60
60
|
signal: AbortSignal.timeout(15_000),
|
|
61
61
|
});
|
|
62
|
-
if (!response.ok) {
|
|
62
|
+
if (!response.ok && !(tokenExchange === "device" && response.status === 400)) {
|
|
63
63
|
await response.body?.cancel();
|
|
64
64
|
throw new PluginError(response.status === 400 && tokenExchange
|
|
65
65
|
? 401
|
|
@@ -121,6 +121,8 @@ export const validatePlugins = (input) => {
|
|
|
121
121
|
typeof oauth.clientId !== "string" ||
|
|
122
122
|
!oauth.clientId.trim() ||
|
|
123
123
|
(oauth.clientSecret !== undefined && typeof oauth.clientSecret !== "string") ||
|
|
124
|
+
(oauth.deviceAuthorizationUrl !== undefined &&
|
|
125
|
+
(typeof oauth.deviceAuthorizationUrl !== "string" || !!oauth.clientSecret)) ||
|
|
124
126
|
!Array.isArray(oauth.scopes) ||
|
|
125
127
|
oauth.scopes.some((s) => typeof s !== "string" || !s.trim()) ||
|
|
126
128
|
typeof plugin.identifyUser !== "function" ||
|
|
@@ -133,7 +135,11 @@ export const validatePlugins = (input) => {
|
|
|
133
135
|
!plugin.apiOrigins.length ||
|
|
134
136
|
(oauth.parseToken !== undefined && typeof oauth.parseToken !== "function"))
|
|
135
137
|
throw new Error(`Viewer plugins: invalid plugin ${plugin.id}`);
|
|
136
|
-
for (const endpoint of [
|
|
138
|
+
for (const endpoint of [
|
|
139
|
+
oauth.authorizationUrl,
|
|
140
|
+
oauth.tokenUrl,
|
|
141
|
+
...(oauth.deviceAuthorizationUrl === undefined ? [] : [oauth.deviceAuthorizationUrl]),
|
|
142
|
+
]) {
|
|
137
143
|
if (typeof endpoint !== "string" || httpsUrl(endpoint).search)
|
|
138
144
|
throw new Error("OAuth endpoints must not contain query parameters");
|
|
139
145
|
}
|
|
@@ -8,6 +8,12 @@ export declare const createPluginRuntime: (definitions: readonly DataPlugin[], t
|
|
|
8
8
|
createSession(): string;
|
|
9
9
|
hasSession(id: string): boolean;
|
|
10
10
|
connect(id: string, pluginId: string, redirectUri: string): string;
|
|
11
|
+
connectDevice(id: string, pluginId: string): Promise<string>;
|
|
12
|
+
pollDevice(id: string, pluginId: string, state: string): Promise<{
|
|
13
|
+
userCode: string;
|
|
14
|
+
verificationUri: string;
|
|
15
|
+
retryAfter: number;
|
|
16
|
+
} | undefined>;
|
|
11
17
|
callback(id: string, pluginId: string, redirectUri: string, params: URLSearchParams): Promise<void>;
|
|
12
18
|
disconnect(id: string, pluginId: string): void;
|
|
13
19
|
status(id: string, pluginId: string): PluginUser | undefined;
|
package/dist/plugins/runtime.js
CHANGED
|
@@ -23,16 +23,7 @@ export const createPluginRuntime = (definitions, transport, credentials) => {
|
|
|
23
23
|
throw new PluginError(404, "Plugin is not configured");
|
|
24
24
|
return plugin;
|
|
25
25
|
};
|
|
26
|
-
const
|
|
27
|
-
const { oauth } = plugin;
|
|
28
|
-
const body = new URLSearchParams({ ...fields, client_id: oauth.clientId });
|
|
29
|
-
if (oauth.clientSecret)
|
|
30
|
-
body.set("client_secret", oauth.clientSecret);
|
|
31
|
-
const result = await fetchPluginJson(oauth.tokenUrl, {
|
|
32
|
-
method: "POST",
|
|
33
|
-
body,
|
|
34
|
-
headers: { accept: "application/json" },
|
|
35
|
-
}, true, transport);
|
|
26
|
+
const tokenFrom = (plugin, result) => {
|
|
36
27
|
if (!result ||
|
|
37
28
|
typeof result !== "object" ||
|
|
38
29
|
"error" in result ||
|
|
@@ -45,12 +36,24 @@ export const createPluginRuntime = (definitions, transport, credentials) => {
|
|
|
45
36
|
throw new PluginError(502, "OAuth exchange failed");
|
|
46
37
|
}
|
|
47
38
|
try {
|
|
48
|
-
return parseOAuthToken(oauth.parseToken ? oauth.parseToken(result) : result);
|
|
39
|
+
return parseOAuthToken(plugin.oauth.parseToken ? plugin.oauth.parseToken(result) : result);
|
|
49
40
|
}
|
|
50
41
|
catch {
|
|
51
42
|
throw new PluginError(502, "Invalid OAuth token response");
|
|
52
43
|
}
|
|
53
44
|
};
|
|
45
|
+
const exchange = async (plugin, fields) => {
|
|
46
|
+
const { oauth } = plugin;
|
|
47
|
+
const body = new URLSearchParams({ ...fields, client_id: oauth.clientId });
|
|
48
|
+
if (oauth.clientSecret)
|
|
49
|
+
body.set("client_secret", oauth.clientSecret);
|
|
50
|
+
const result = await fetchPluginJson(oauth.tokenUrl, {
|
|
51
|
+
method: "POST",
|
|
52
|
+
body,
|
|
53
|
+
headers: { accept: "application/json" },
|
|
54
|
+
}, true, transport);
|
|
55
|
+
return tokenFrom(plugin, result);
|
|
56
|
+
};
|
|
54
57
|
const context = (plugin, accessToken, write = false, check = () => { }) => ({
|
|
55
58
|
request: async (source, init) => {
|
|
56
59
|
check();
|
|
@@ -77,6 +80,34 @@ export const createPluginRuntime = (definitions, transport, credentials) => {
|
|
|
77
80
|
if (sessionFor(id).connections.get(pluginId) !== connection)
|
|
78
81
|
throw expired();
|
|
79
82
|
};
|
|
83
|
+
const begin = (id, plugin, attempt) => {
|
|
84
|
+
const session = sessionFor(id);
|
|
85
|
+
session.connections.delete(plugin.id);
|
|
86
|
+
credentials?.set(plugin);
|
|
87
|
+
if (credentials)
|
|
88
|
+
for (const other of sessions.values())
|
|
89
|
+
other.connections.delete(plugin.id);
|
|
90
|
+
session.attempts.set(plugin.id, attempt);
|
|
91
|
+
};
|
|
92
|
+
const complete = async (id, plugin, attempt, token) => {
|
|
93
|
+
const expires = expiry(token);
|
|
94
|
+
const user = await plugin.identifyUser(context(plugin, token.access_token));
|
|
95
|
+
if (!user ||
|
|
96
|
+
typeof user.id !== "string" ||
|
|
97
|
+
!user.id ||
|
|
98
|
+
typeof user.name !== "string" ||
|
|
99
|
+
!user.name ||
|
|
100
|
+
user.id.length > 512 ||
|
|
101
|
+
user.name.length > 512)
|
|
102
|
+
throw new PluginError(502, "Plugin could not identify the authorized user");
|
|
103
|
+
const session = sessionFor(id);
|
|
104
|
+
if (session.attempts.get(plugin.id) !== attempt || attempt.expires <= Date.now())
|
|
105
|
+
throw expired();
|
|
106
|
+
session.attempts.delete(plugin.id);
|
|
107
|
+
const connection = { token, expires, user: { id: user.id, name: user.name } };
|
|
108
|
+
credentials?.set(plugin, connection);
|
|
109
|
+
session.connections.set(plugin.id, connection);
|
|
110
|
+
};
|
|
80
111
|
const unregister = (pluginId) => {
|
|
81
112
|
plugins.delete(pluginId);
|
|
82
113
|
for (const session of sessions.values()) {
|
|
@@ -217,8 +248,9 @@ export const createPluginRuntime = (definitions, transport, credentials) => {
|
|
|
217
248
|
}
|
|
218
249
|
},
|
|
219
250
|
connect(id, pluginId, redirectUri) {
|
|
220
|
-
const session = sessionFor(id);
|
|
221
251
|
const plugin = pluginFor(pluginId);
|
|
252
|
+
if (plugin.oauth.deviceAuthorizationUrl)
|
|
253
|
+
throw new PluginError(400, "Use device authorization for this plugin");
|
|
222
254
|
const redirect = new URL(redirectUri);
|
|
223
255
|
if (redirect.protocol !== "http:" ||
|
|
224
256
|
!["localhost", "127.0.0.1", "[::1]"].includes(redirect.hostname) ||
|
|
@@ -234,12 +266,7 @@ export const createPluginRuntime = (definitions, transport, credentials) => {
|
|
|
234
266
|
redirectUri,
|
|
235
267
|
expires: Date.now() + 10 * 60 * 1000,
|
|
236
268
|
};
|
|
237
|
-
|
|
238
|
-
credentials?.set(plugin);
|
|
239
|
-
if (credentials)
|
|
240
|
-
for (const other of sessions.values())
|
|
241
|
-
other.connections.delete(pluginId);
|
|
242
|
-
session.attempts.set(pluginId, attempt);
|
|
269
|
+
begin(id, plugin, attempt);
|
|
243
270
|
const url = new URL(plugin.oauth.authorizationUrl);
|
|
244
271
|
for (const [key, value] of Object.entries(plugin.oauth.authorizationParams ?? {}))
|
|
245
272
|
url.searchParams.set(key, value);
|
|
@@ -255,11 +282,131 @@ export const createPluginRuntime = (definitions, transport, credentials) => {
|
|
|
255
282
|
url.searchParams.set(key, value);
|
|
256
283
|
return url.href;
|
|
257
284
|
},
|
|
285
|
+
async connectDevice(id, pluginId) {
|
|
286
|
+
const plugin = pluginFor(pluginId);
|
|
287
|
+
if (!plugin.oauth.deviceAuthorizationUrl)
|
|
288
|
+
throw new PluginError(400, "Device authorization is not configured");
|
|
289
|
+
const attempt = {
|
|
290
|
+
state: nonce(),
|
|
291
|
+
expires: Date.now() + 15 * 60_000,
|
|
292
|
+
deviceCode: "",
|
|
293
|
+
userCode: "",
|
|
294
|
+
verificationUri: "",
|
|
295
|
+
interval: 5,
|
|
296
|
+
nextPoll: Infinity,
|
|
297
|
+
polling: false,
|
|
298
|
+
};
|
|
299
|
+
begin(id, plugin, attempt);
|
|
300
|
+
try {
|
|
301
|
+
const result = (await fetchPluginJson(plugin.oauth.deviceAuthorizationUrl, {
|
|
302
|
+
method: "POST",
|
|
303
|
+
headers: { accept: "application/json" },
|
|
304
|
+
body: new URLSearchParams({
|
|
305
|
+
client_id: plugin.oauth.clientId,
|
|
306
|
+
[plugin.oauth.scopeParameter ?? "scope"]: plugin.oauth.scopes.join(plugin.oauth.scopeSeparator ?? " "),
|
|
307
|
+
}),
|
|
308
|
+
}, true, transport));
|
|
309
|
+
if (!result ||
|
|
310
|
+
typeof result.device_code !== "string" ||
|
|
311
|
+
!result.device_code ||
|
|
312
|
+
result.device_code.length > 4096 ||
|
|
313
|
+
typeof result.user_code !== "string" ||
|
|
314
|
+
!result.user_code ||
|
|
315
|
+
result.user_code.length > 128 ||
|
|
316
|
+
typeof result.verification_uri !== "string" ||
|
|
317
|
+
result.verification_uri.length > 2048 ||
|
|
318
|
+
typeof result.expires_in !== "number" ||
|
|
319
|
+
!Number.isFinite(result.expires_in) ||
|
|
320
|
+
result.expires_in <= 0 ||
|
|
321
|
+
result.expires_in > 3600 ||
|
|
322
|
+
(result.interval !== undefined &&
|
|
323
|
+
(typeof result.interval !== "number" ||
|
|
324
|
+
!Number.isFinite(result.interval) ||
|
|
325
|
+
result.interval <= 0 ||
|
|
326
|
+
result.interval > 60)))
|
|
327
|
+
throw new PluginError(502, "Invalid device authorization response");
|
|
328
|
+
const verificationUri = httpsUrl(result.verification_uri).href;
|
|
329
|
+
if (sessionFor(id).attempts.get(pluginId) !== attempt)
|
|
330
|
+
throw expired();
|
|
331
|
+
Object.assign(attempt, {
|
|
332
|
+
deviceCode: result.device_code,
|
|
333
|
+
userCode: result.user_code,
|
|
334
|
+
verificationUri,
|
|
335
|
+
expires: Date.now() + result.expires_in * 1000,
|
|
336
|
+
interval: result.interval ?? 5,
|
|
337
|
+
nextPoll: Date.now() + Number(result.interval ?? 5) * 1000,
|
|
338
|
+
});
|
|
339
|
+
return attempt.state;
|
|
340
|
+
}
|
|
341
|
+
catch (error) {
|
|
342
|
+
if (sessionFor(id).attempts.get(pluginId) === attempt)
|
|
343
|
+
sessionFor(id).attempts.delete(pluginId);
|
|
344
|
+
throw error;
|
|
345
|
+
}
|
|
346
|
+
},
|
|
347
|
+
async pollDevice(id, pluginId, state) {
|
|
348
|
+
const session = sessionFor(id);
|
|
349
|
+
const plugin = pluginFor(pluginId);
|
|
350
|
+
const attempt = session.attempts.get(pluginId);
|
|
351
|
+
if (!state ||
|
|
352
|
+
!attempt ||
|
|
353
|
+
!("deviceCode" in attempt) ||
|
|
354
|
+
!attempt.deviceCode ||
|
|
355
|
+
attempt.state !== state ||
|
|
356
|
+
attempt.expires <= Date.now())
|
|
357
|
+
throw new PluginError(400, "Invalid or expired device authorization");
|
|
358
|
+
if (!attempt.polling && Date.now() >= attempt.nextPoll) {
|
|
359
|
+
attempt.polling = true;
|
|
360
|
+
attempt.nextPoll = Date.now() + attempt.interval * 1000;
|
|
361
|
+
try {
|
|
362
|
+
const result = await fetchPluginJson(plugin.oauth.tokenUrl, {
|
|
363
|
+
method: "POST",
|
|
364
|
+
headers: { accept: "application/json" },
|
|
365
|
+
body: new URLSearchParams({
|
|
366
|
+
client_id: plugin.oauth.clientId,
|
|
367
|
+
device_code: attempt.deviceCode,
|
|
368
|
+
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
|
369
|
+
}),
|
|
370
|
+
}, "device", transport);
|
|
371
|
+
if (sessionFor(id).attempts.get(pluginId) !== attempt ||
|
|
372
|
+
attempt.expires <= Date.now())
|
|
373
|
+
throw expired();
|
|
374
|
+
if (result && typeof result === "object" && "error" in result) {
|
|
375
|
+
if (result.error === "slow_down") {
|
|
376
|
+
attempt.interval += 5;
|
|
377
|
+
if ("interval" in result &&
|
|
378
|
+
typeof result.interval === "number" &&
|
|
379
|
+
Number.isFinite(result.interval) &&
|
|
380
|
+
result.interval > attempt.interval)
|
|
381
|
+
attempt.interval = result.interval;
|
|
382
|
+
}
|
|
383
|
+
else if (result.error !== "authorization_pending") {
|
|
384
|
+
session.attempts.delete(pluginId);
|
|
385
|
+
throw new PluginError(400, "Device sign-in was not completed. Connect this plugin again.");
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
await complete(id, plugin, attempt, tokenFrom(plugin, result));
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
finally {
|
|
394
|
+
attempt.polling = false;
|
|
395
|
+
attempt.nextPoll = Date.now() + attempt.interval * 1000;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
userCode: attempt.userCode,
|
|
400
|
+
verificationUri: attempt.verificationUri,
|
|
401
|
+
retryAfter: Math.max(1, Math.ceil((Math.min(attempt.nextPoll, attempt.expires) - Date.now()) / 1000)),
|
|
402
|
+
};
|
|
403
|
+
},
|
|
258
404
|
async callback(id, pluginId, redirectUri, params) {
|
|
259
405
|
const session = sessionFor(id);
|
|
260
406
|
const plugin = pluginFor(pluginId);
|
|
261
407
|
const attempt = session.attempts.get(pluginId);
|
|
262
408
|
if (!attempt?.state ||
|
|
409
|
+
!("verifier" in attempt) ||
|
|
263
410
|
attempt.expires <= Date.now() ||
|
|
264
411
|
attempt.redirectUri !== redirectUri ||
|
|
265
412
|
params.getAll("state").length !== 1 ||
|
|
@@ -276,22 +423,7 @@ export const createPluginRuntime = (definitions, transport, credentials) => {
|
|
|
276
423
|
redirect_uri: redirectUri,
|
|
277
424
|
code_verifier: attempt.verifier,
|
|
278
425
|
});
|
|
279
|
-
|
|
280
|
-
const user = await plugin.identifyUser(context(plugin, token.access_token));
|
|
281
|
-
if (!user ||
|
|
282
|
-
typeof user.id !== "string" ||
|
|
283
|
-
!user.id ||
|
|
284
|
-
typeof user.name !== "string" ||
|
|
285
|
-
!user.name ||
|
|
286
|
-
user.id.length > 512 ||
|
|
287
|
-
user.name.length > 512)
|
|
288
|
-
throw new PluginError(502, "Plugin could not identify the authorized user");
|
|
289
|
-
if (sessionFor(id).attempts.get(pluginId) !== attempt)
|
|
290
|
-
throw expired();
|
|
291
|
-
session.attempts.delete(pluginId);
|
|
292
|
-
const connection = { token, expires, user: { id: user.id, name: user.name } };
|
|
293
|
-
credentials?.set(plugin, connection);
|
|
294
|
-
session.connections.set(pluginId, connection);
|
|
426
|
+
await complete(id, plugin, attempt, token);
|
|
295
427
|
},
|
|
296
428
|
disconnect(id, pluginId) {
|
|
297
429
|
const session = sessionFor(id);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { PluginAction, PluginActionRequest, PluginActionResult } from "./actions.js";
|
|
2
|
+
import type { PluginUser } from "./types.js";
|
|
3
|
+
import type { PluginView } from "./view.js";
|
|
4
|
+
export interface PluginRequest {
|
|
5
|
+
action: string;
|
|
6
|
+
plugin?: string;
|
|
7
|
+
url?: string;
|
|
8
|
+
operation?: PluginActionRequest;
|
|
9
|
+
requestId?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface PluginResponse {
|
|
12
|
+
plugin?: string;
|
|
13
|
+
connected?: boolean;
|
|
14
|
+
autoConnect?: boolean;
|
|
15
|
+
user?: PluginUser | null;
|
|
16
|
+
view?: PluginView;
|
|
17
|
+
actions?: PluginAction[];
|
|
18
|
+
result?: PluginActionResult;
|
|
19
|
+
url?: string;
|
|
20
|
+
}
|
|
21
|
+
/** The host authenticates requests; rejecting with status 401/403 clears the plugin's views. */
|
|
22
|
+
export type PluginTransport = (request: PluginRequest) => Promise<PluginResponse>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/plugins/types.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/** Distributors bundle public client IDs; secrets stay on trusted servers, never in plugin ZIPs. */
|
|
2
2
|
export interface PluginOAuth {
|
|
3
3
|
authorizationUrl: string;
|
|
4
4
|
tokenUrl: string;
|
|
5
5
|
clientId: string;
|
|
6
6
|
clientSecret?: string;
|
|
7
|
+
/** Public clients may use the device grant instead of a browser callback. */
|
|
8
|
+
deviceAuthorizationUrl?: string;
|
|
7
9
|
scopes: readonly string[];
|
|
8
10
|
/** Provider-specific user authorization parameters, such as actor=user. */
|
|
9
11
|
authorizationParams?: Readonly<Record<string, string>>;
|