@dsh-workbench/adapter-runtime 0.0.0-stage → 1.0.0-beta.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/LICENSE +21 -0
- package/README.md +36 -2
- package/channel/core.js +237 -0
- package/channel/fetch.js +14 -0
- package/channel/opencli-plugin.json +6 -0
- package/channel/package.json +10 -0
- package/channel/send.js +27 -0
- package/dist/browser/browser-find.d.ts +38 -0
- package/dist/browser/browser-find.js +242 -0
- package/dist/browser/browser-read.d.ts +27 -0
- package/dist/browser/browser-read.js +242 -0
- package/dist/browser/index.d.ts +2 -0
- package/dist/browser/index.js +2 -0
- package/dist/cli/command.d.ts +16 -0
- package/dist/cli/command.js +30 -0
- package/dist/cli/index.d.ts +5 -0
- package/dist/cli/index.js +5 -0
- package/dist/cli/opencli-runtime.d.ts +17 -0
- package/dist/cli/opencli-runtime.js +44 -0
- package/dist/cli/opencli-types.d.ts +12 -0
- package/dist/cli/opencli-types.js +1 -0
- package/dist/cli/opencli-window.d.ts +2 -0
- package/dist/cli/opencli-window.js +2 -0
- package/dist/cli/opencli.d.ts +13 -0
- package/dist/cli/opencli.js +136 -0
- package/dist/command/index.d.ts +7 -0
- package/dist/command/index.js +71 -0
- package/dist/data/channel.d.ts +19 -0
- package/dist/data/channel.js +121 -0
- package/dist/data/index.d.ts +10 -0
- package/dist/data/index.js +359 -0
- package/dist/extensions/index.d.ts +10 -0
- package/dist/extensions/index.js +36 -0
- package/dist/git/index.d.ts +9 -0
- package/dist/git/index.js +60 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/libs/index.d.ts +6 -0
- package/dist/libs/index.js +46 -0
- package/package.json +32 -3
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
import { channelInstalledVersion, channelPluginPlan, channelSiteInstalled, channelVersionOf, DEFAULT_RECIPE_MAX_BYTES, quoteShellArg, runShell, } from '@dsh-workbench/shared/host';
|
|
3
|
+
export const CHANNEL_DIR = fileURLToPath(new URL('../../channel/', import.meta.url));
|
|
4
|
+
function authOf(recipe) {
|
|
5
|
+
return recipe.auth ?? { source: { kind: 'cookie' }, place: { in: 'cookie' } };
|
|
6
|
+
}
|
|
7
|
+
function eligible(recipe) {
|
|
8
|
+
const auth = authOf(recipe);
|
|
9
|
+
if (auth.kind === 'none')
|
|
10
|
+
return undefined;
|
|
11
|
+
if (auth.source.kind === 'cookie')
|
|
12
|
+
return auth.place.in === 'cookie' ? { kind: 'cookie' } : undefined;
|
|
13
|
+
if (auth.source.kind === 'cookieValue')
|
|
14
|
+
return { kind: 'cookieValue', name: auth.source.name };
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
function queryOf(recipe, overrides) {
|
|
18
|
+
const query = {};
|
|
19
|
+
for (const [name, value] of Object.entries(recipe.request.query ?? {}))
|
|
20
|
+
query[name] = String(value);
|
|
21
|
+
for (const [name, value] of Object.entries(overrides.query ?? {}))
|
|
22
|
+
query[name] = String(value);
|
|
23
|
+
return query;
|
|
24
|
+
}
|
|
25
|
+
export function channelUrlOf(recipe, overrides) {
|
|
26
|
+
const path = overrides.path ?? recipe.request.path;
|
|
27
|
+
const url = new URL(path.startsWith('/') ? `${recipe.origin}${path}` : path);
|
|
28
|
+
for (const [name, value] of Object.entries(queryOf(recipe, overrides)))
|
|
29
|
+
url.searchParams.set(name, value);
|
|
30
|
+
return url.toString();
|
|
31
|
+
}
|
|
32
|
+
export function wireBodyOf(recipe, overrides) {
|
|
33
|
+
const contentType = recipe.request.contentType ?? 'json';
|
|
34
|
+
const value = overrides.body !== undefined ? overrides.body : recipe.request.body;
|
|
35
|
+
if (value === undefined)
|
|
36
|
+
return { contentType, body: undefined };
|
|
37
|
+
if (contentType === 'form') {
|
|
38
|
+
const params = new URLSearchParams();
|
|
39
|
+
for (const [name, entry] of Object.entries(value))
|
|
40
|
+
params.set(name, String(entry));
|
|
41
|
+
return { contentType, body: params.toString() };
|
|
42
|
+
}
|
|
43
|
+
return { contentType, body: typeof value === 'string' ? value : JSON.stringify(value) };
|
|
44
|
+
}
|
|
45
|
+
function placeArgsOf(recipe) {
|
|
46
|
+
const auth = authOf(recipe);
|
|
47
|
+
if (auth.kind === 'none' || auth.place.in === 'cookie')
|
|
48
|
+
return [];
|
|
49
|
+
if (auth.place.in === 'header') {
|
|
50
|
+
const prefix = auth.place.prefix;
|
|
51
|
+
return [
|
|
52
|
+
'--place',
|
|
53
|
+
`header:${auth.place.name}`,
|
|
54
|
+
...(prefix === undefined ? [] : ['--prefix', prefix]),
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
if (auth.place.in === 'query')
|
|
58
|
+
return ['--place', `query:${auth.place.name}`];
|
|
59
|
+
return ['--place', `body:${auth.place.field}`];
|
|
60
|
+
}
|
|
61
|
+
export function channelCallOf(recipe, overrides, command) {
|
|
62
|
+
const source = eligible(recipe);
|
|
63
|
+
if (source === undefined)
|
|
64
|
+
return undefined;
|
|
65
|
+
const write = recipe.request.method !== 'GET';
|
|
66
|
+
const { contentType, body } = wireBodyOf(recipe, overrides);
|
|
67
|
+
const argv = [
|
|
68
|
+
command,
|
|
69
|
+
'workbench',
|
|
70
|
+
write ? 'send' : 'fetch',
|
|
71
|
+
'--api',
|
|
72
|
+
'1',
|
|
73
|
+
'--url',
|
|
74
|
+
channelUrlOf(recipe, overrides),
|
|
75
|
+
'--jar',
|
|
76
|
+
'cookie',
|
|
77
|
+
...(source.kind === 'cookieValue' ? ['--cookie', source.name] : []),
|
|
78
|
+
...placeArgsOf(recipe),
|
|
79
|
+
...(Object.keys(recipe.request.headers ?? {}).length === 0 && Object.keys(overrides.headers ?? {}).length === 0
|
|
80
|
+
? []
|
|
81
|
+
: [
|
|
82
|
+
'--headers',
|
|
83
|
+
JSON.stringify({ ...(recipe.request.headers ?? {}), ...(overrides.headers ?? {}) }),
|
|
84
|
+
]),
|
|
85
|
+
'--kind',
|
|
86
|
+
recipe.response?.kind ?? 'json',
|
|
87
|
+
'--maxBytes',
|
|
88
|
+
String(recipe.response?.maxBytes ?? DEFAULT_RECIPE_MAX_BYTES),
|
|
89
|
+
'-f',
|
|
90
|
+
'json',
|
|
91
|
+
...(write ? ['--method', recipe.request.method] : []),
|
|
92
|
+
...(write ? ['--content-type', contentType] : []),
|
|
93
|
+
...(write && body !== undefined ? ['--body', body] : []),
|
|
94
|
+
];
|
|
95
|
+
return { argv, timeoutMs: recipe.timeoutMs ?? 25_000 };
|
|
96
|
+
}
|
|
97
|
+
export function channelAlreadyInstalled() {
|
|
98
|
+
const shipped = channelVersionOf(CHANNEL_DIR);
|
|
99
|
+
return shipped !== undefined && channelInstalledVersion() === shipped && channelSiteInstalled();
|
|
100
|
+
}
|
|
101
|
+
export async function ensureChannel(shell, command, state, ready = channelAlreadyInstalled) {
|
|
102
|
+
if (state.value === 'ready')
|
|
103
|
+
return true;
|
|
104
|
+
if (state.value === 'unavailable')
|
|
105
|
+
return false;
|
|
106
|
+
if (ready()) {
|
|
107
|
+
state.value = 'ready';
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
const plan = channelPluginPlan({ channelDir: CHANNEL_DIR, command });
|
|
111
|
+
if (plan === undefined) {
|
|
112
|
+
state.value = 'unavailable';
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
const result = await runShell(shell, plan.line, { timeoutMs: 120_000 });
|
|
116
|
+
state.value = result.exitCode === 0 && !result.timedOut ? 'ready' : 'unavailable';
|
|
117
|
+
return state.value === 'ready';
|
|
118
|
+
}
|
|
119
|
+
export function channelCommandLine(call) {
|
|
120
|
+
return call.argv.map((one) => quoteShellArg(one)).join(' ');
|
|
121
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DshShell } from '@dsh-workbench/shared';
|
|
2
|
+
import type { DataReader } from '@dsh-workbench/shared/host';
|
|
3
|
+
export interface DataReaderOptions {
|
|
4
|
+
readonly shell?: DshShell | undefined;
|
|
5
|
+
readonly command?: string | undefined;
|
|
6
|
+
readonly fetch?: typeof fetch | undefined;
|
|
7
|
+
readonly channel?: boolean | undefined;
|
|
8
|
+
readonly channelInstalled?: (() => boolean) | undefined;
|
|
9
|
+
}
|
|
10
|
+
export declare function createDataReader(options?: DataReaderOptions): DataReader;
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import { callsProblemOf, recipeProblemOf, runShell, scriptOf, scriptOfMany } from '@dsh-workbench/shared/host';
|
|
2
|
+
import { browserRead } from '../browser/index.js';
|
|
3
|
+
import { createOpencli } from '../cli/index.js';
|
|
4
|
+
import { WorkbenchDataError } from '@dsh-workbench/shared/host';
|
|
5
|
+
import { channelCallOf, channelCommandLine, ensureChannel, } from './channel.js';
|
|
6
|
+
const DEFAULT_TIMEOUT_MS = 20_000;
|
|
7
|
+
function failure(code, message, extra = {}) {
|
|
8
|
+
return { ok: false, error: { code, message, ...extra } };
|
|
9
|
+
}
|
|
10
|
+
function parse(text) {
|
|
11
|
+
try {
|
|
12
|
+
return { ok: true, data: JSON.parse(text) };
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return failure('parse', '返回的不是 JSON', { detail: text.slice(0, 2000) });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function asRecord(value) {
|
|
19
|
+
return typeof value === 'object' && value !== null ? value : undefined;
|
|
20
|
+
}
|
|
21
|
+
function dataCodeOf(pageCode) {
|
|
22
|
+
const code = pageCode.trim().toUpperCase().replace(/-/g, '_');
|
|
23
|
+
if (code === 'NOT_LOGGED_IN' ||
|
|
24
|
+
code === 'AUTH_FAILED' ||
|
|
25
|
+
code === 'AUTH_REQUIRED' ||
|
|
26
|
+
code === 'UNAUTHORIZED' ||
|
|
27
|
+
code === 'UNAUTHENTICATED' ||
|
|
28
|
+
code === 'PERMISSION_DENIED' ||
|
|
29
|
+
code === 'FORBIDDEN' ||
|
|
30
|
+
code.includes('UNAUTH')) {
|
|
31
|
+
return 'auth';
|
|
32
|
+
}
|
|
33
|
+
if (code === 'TIMEOUT' || code === 'CLI_TIMEOUT')
|
|
34
|
+
return 'timeout';
|
|
35
|
+
if (/^HTTP_\d{3}$/.test(code))
|
|
36
|
+
return 'http';
|
|
37
|
+
if (code === 'PARSE' || code === 'SHAPE')
|
|
38
|
+
return 'parse';
|
|
39
|
+
return 'browser';
|
|
40
|
+
}
|
|
41
|
+
function statusOf(pageCode) {
|
|
42
|
+
const match = /^http-(\d{3})$/i.exec(pageCode.trim());
|
|
43
|
+
return match === null ? undefined : Number(match[1]);
|
|
44
|
+
}
|
|
45
|
+
function browserFailure(result, loginUrl) {
|
|
46
|
+
const pageCode = result.pageCode ?? result.code;
|
|
47
|
+
const mapped = dataCodeOf(result.code);
|
|
48
|
+
const code = mapped === 'browser' ? dataCodeOf(pageCode) : mapped;
|
|
49
|
+
const status = statusOf(result.code) ?? statusOf(pageCode);
|
|
50
|
+
const helpUrl = result.helpUrl ?? (code === 'auth' ? loginUrl : undefined);
|
|
51
|
+
return {
|
|
52
|
+
ok: false,
|
|
53
|
+
error: {
|
|
54
|
+
code,
|
|
55
|
+
message: result.message,
|
|
56
|
+
...(status === undefined ? {} : { status }),
|
|
57
|
+
...(result.detail === undefined ? {} : { detail: result.detail }),
|
|
58
|
+
...(result.hint === undefined ? {} : { hint: result.hint }),
|
|
59
|
+
...(helpUrl === undefined ? {} : { helpUrl }),
|
|
60
|
+
...(pageCode === '' ? {} : { pageCode }),
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function pageFailure(pageCode, message, loginUrl) {
|
|
65
|
+
const code = dataCodeOf(pageCode);
|
|
66
|
+
const status = statusOf(pageCode);
|
|
67
|
+
const helpUrl = code === 'auth' ? loginUrl : undefined;
|
|
68
|
+
return {
|
|
69
|
+
ok: false,
|
|
70
|
+
error: {
|
|
71
|
+
code,
|
|
72
|
+
message,
|
|
73
|
+
...(status === undefined ? {} : { status }),
|
|
74
|
+
...(helpUrl === undefined ? {} : { helpUrl }),
|
|
75
|
+
...(pageCode === '' ? {} : { pageCode }),
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function pickOf(value, path) {
|
|
80
|
+
let at = value;
|
|
81
|
+
for (const key of path.split('.')) {
|
|
82
|
+
const box = asRecord(at);
|
|
83
|
+
if (box === undefined)
|
|
84
|
+
return undefined;
|
|
85
|
+
at = box[key];
|
|
86
|
+
}
|
|
87
|
+
return at;
|
|
88
|
+
}
|
|
89
|
+
function allFailure(names, code, message, extra = {}) {
|
|
90
|
+
const out = {};
|
|
91
|
+
for (const name of names)
|
|
92
|
+
out[name] = { ok: false, error: { code, message, ...extra } };
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
export function createDataReader(options = {}) {
|
|
96
|
+
const fetchOf = options.fetch ?? globalThis.fetch;
|
|
97
|
+
const command = options.command ?? 'opencli';
|
|
98
|
+
async function readHttp(request) {
|
|
99
|
+
if (fetchOf === undefined)
|
|
100
|
+
return failure('invalid-request', '这个运行时没有 fetch');
|
|
101
|
+
const timeoutMs = request.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
102
|
+
const controller = new AbortController();
|
|
103
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
104
|
+
try {
|
|
105
|
+
const response = await fetchOf(request.url, {
|
|
106
|
+
method: 'GET',
|
|
107
|
+
...(request.headers === undefined ? {} : { headers: request.headers }),
|
|
108
|
+
signal: controller.signal,
|
|
109
|
+
});
|
|
110
|
+
const text = await response.text();
|
|
111
|
+
if (!response.ok) {
|
|
112
|
+
return failure(response.status === 401 || response.status === 403 ? 'auth' : 'http', `HTTP ${response.status}`, {
|
|
113
|
+
status: response.status,
|
|
114
|
+
detail: text.slice(0, 2000),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return parse(text);
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
if (controller.signal.aborted)
|
|
121
|
+
return failure('timeout', `读超时(${timeoutMs}ms)`);
|
|
122
|
+
return failure('http', error instanceof Error ? error.message : String(error));
|
|
123
|
+
}
|
|
124
|
+
finally {
|
|
125
|
+
clearTimeout(timer);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async function readBrowser(request) {
|
|
129
|
+
if (options.shell === undefined)
|
|
130
|
+
return failure('invalid-request', '没有 shell,读不了浏览器');
|
|
131
|
+
const result = await browserRead({
|
|
132
|
+
shell: options.shell,
|
|
133
|
+
script: request.script,
|
|
134
|
+
command,
|
|
135
|
+
...(request.url === undefined ? {} : { url: request.url }),
|
|
136
|
+
...(request.loginUrl === undefined ? {} : { loginUrl: request.loginUrl }),
|
|
137
|
+
...(request.session === undefined ? {} : { session: request.session }),
|
|
138
|
+
...(request.open === undefined ? {} : { open: request.open }),
|
|
139
|
+
...(request.timeoutMs === undefined ? {} : { timeoutMs: request.timeoutMs }),
|
|
140
|
+
});
|
|
141
|
+
if (!result.ok)
|
|
142
|
+
return browserFailure(result, request.loginUrl);
|
|
143
|
+
return { ok: true, data: result.value };
|
|
144
|
+
}
|
|
145
|
+
const channelState = { value: 'unknown' };
|
|
146
|
+
async function readChannel(shell, call, recipe) {
|
|
147
|
+
const result = await runShell(shell, channelCommandLine(call), { timeoutMs: call.timeoutMs + 5000 });
|
|
148
|
+
if (result.timedOut)
|
|
149
|
+
return failure('timeout', `通道读超时(${call.timeoutMs}ms)`);
|
|
150
|
+
if (result.exitCode !== 0)
|
|
151
|
+
return undefined;
|
|
152
|
+
let parsed;
|
|
153
|
+
try {
|
|
154
|
+
parsed = JSON.parse(result.stdout);
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
const box = asRecord(parsed);
|
|
160
|
+
if (box === undefined)
|
|
161
|
+
return undefined;
|
|
162
|
+
if (box['ok'] !== true) {
|
|
163
|
+
const pageCode = typeof box['code'] === 'string' ? box['code'] : '';
|
|
164
|
+
const message = typeof box['message'] === 'string' && box['message'] !== ''
|
|
165
|
+
? box['message']
|
|
166
|
+
: `读不到(${pageCode === '' ? 'unknown' : pageCode})`;
|
|
167
|
+
return pageFailure(pageCode, message, recipe.loginUrl);
|
|
168
|
+
}
|
|
169
|
+
if (recipe.response?.kind === 'text') {
|
|
170
|
+
return typeof box['text'] === 'string'
|
|
171
|
+
? { ok: true, data: box['text'] }
|
|
172
|
+
: failure('parse', '通道没有回 text', { detail: JSON.stringify(parsed).slice(0, 2000) });
|
|
173
|
+
}
|
|
174
|
+
const path = recipe.response?.path;
|
|
175
|
+
if (path === undefined)
|
|
176
|
+
return { ok: true, data: box['data'] };
|
|
177
|
+
const value = pickOf(box['data'], path);
|
|
178
|
+
return value === undefined
|
|
179
|
+
? failure('parse', `返回里没有 ${path} 这一格`, { detail: JSON.stringify(parsed).slice(0, 2000) })
|
|
180
|
+
: { ok: true, data: value };
|
|
181
|
+
}
|
|
182
|
+
async function readRecipe(recipe, overrides, mode) {
|
|
183
|
+
const problem = recipeProblemOf(recipe);
|
|
184
|
+
if (problem !== undefined)
|
|
185
|
+
return failure('invalid-request', problem);
|
|
186
|
+
if (mode === 'request' && recipe.request.method !== 'GET') {
|
|
187
|
+
return failure('invalid-request', 'request 只读 GET;写走 submit');
|
|
188
|
+
}
|
|
189
|
+
if (options.shell === undefined)
|
|
190
|
+
return failure('invalid-request', '没有 shell,读不了浏览器');
|
|
191
|
+
const call = options.channel === false ? undefined : channelCallOf(recipe, overrides ?? {}, command);
|
|
192
|
+
if (call !== undefined &&
|
|
193
|
+
(await ensureChannel(options.shell, command, channelState, options.channelInstalled))) {
|
|
194
|
+
const viaChannel = await readChannel(options.shell, call, recipe);
|
|
195
|
+
if (viaChannel !== undefined)
|
|
196
|
+
return viaChannel;
|
|
197
|
+
channelState.value = 'unavailable';
|
|
198
|
+
}
|
|
199
|
+
let script;
|
|
200
|
+
try {
|
|
201
|
+
script = scriptOf(recipe, overrides);
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
return failure('invalid-request', error instanceof Error ? error.message : String(error));
|
|
205
|
+
}
|
|
206
|
+
const result = await browserRead({
|
|
207
|
+
shell: options.shell,
|
|
208
|
+
script,
|
|
209
|
+
command,
|
|
210
|
+
url: recipe.origin,
|
|
211
|
+
...(recipe.loginUrl === undefined ? {} : { loginUrl: recipe.loginUrl }),
|
|
212
|
+
...(recipe.session === undefined ? {} : { session: recipe.session }),
|
|
213
|
+
...(recipe.timeoutMs === undefined ? {} : { timeoutMs: recipe.timeoutMs }),
|
|
214
|
+
});
|
|
215
|
+
if (!result.ok)
|
|
216
|
+
return browserFailure(result, recipe.loginUrl);
|
|
217
|
+
const box = asRecord(result.value);
|
|
218
|
+
if (box !== undefined && box['ok'] === true) {
|
|
219
|
+
if ('data' in box)
|
|
220
|
+
return { ok: true, data: box['data'] };
|
|
221
|
+
if ('text' in box)
|
|
222
|
+
return { ok: true, data: box['text'] };
|
|
223
|
+
}
|
|
224
|
+
return failure('parse', '页面脚本没有回 data / text', {
|
|
225
|
+
detail: JSON.stringify(result.value).slice(0, 2000),
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
async function readMany(recipe, calls) {
|
|
229
|
+
const names = Object.keys(calls);
|
|
230
|
+
const problem = callsProblemOf(recipe, calls);
|
|
231
|
+
if (problem !== undefined)
|
|
232
|
+
return allFailure(names, 'invalid-request', problem);
|
|
233
|
+
if (recipe.request.method !== 'GET') {
|
|
234
|
+
return allFailure(names, 'invalid-request', 'requestMany 只读 GET;写走 submit');
|
|
235
|
+
}
|
|
236
|
+
if (options.shell === undefined)
|
|
237
|
+
return allFailure(names, 'invalid-request', '没有 shell,读不了浏览器');
|
|
238
|
+
let script;
|
|
239
|
+
try {
|
|
240
|
+
script = scriptOfMany(recipe, calls);
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
return allFailure(names, 'invalid-request', error instanceof Error ? error.message : String(error));
|
|
244
|
+
}
|
|
245
|
+
const result = await browserRead({
|
|
246
|
+
shell: options.shell,
|
|
247
|
+
script,
|
|
248
|
+
command,
|
|
249
|
+
url: recipe.origin,
|
|
250
|
+
...(recipe.loginUrl === undefined ? {} : { loginUrl: recipe.loginUrl }),
|
|
251
|
+
...(recipe.session === undefined ? {} : { session: recipe.session }),
|
|
252
|
+
...(recipe.timeoutMs === undefined ? {} : { timeoutMs: recipe.timeoutMs }),
|
|
253
|
+
});
|
|
254
|
+
if (!result.ok) {
|
|
255
|
+
const one = browserFailure(result, recipe.loginUrl);
|
|
256
|
+
const failed = {};
|
|
257
|
+
for (const name of names)
|
|
258
|
+
failed[name] = one;
|
|
259
|
+
return failed;
|
|
260
|
+
}
|
|
261
|
+
const box = asRecord(result.value);
|
|
262
|
+
const results = box === undefined ? undefined : asRecord(box['results']);
|
|
263
|
+
if (box === undefined || box['ok'] !== true || results === undefined) {
|
|
264
|
+
return allFailure(names, 'parse', '页面脚本没有回 results', {
|
|
265
|
+
detail: JSON.stringify(result.value).slice(0, 2000),
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
const out = {};
|
|
269
|
+
for (const name of names) {
|
|
270
|
+
const entry = asRecord(results[name]);
|
|
271
|
+
if (entry === undefined) {
|
|
272
|
+
out[name] = failure('parse', `页面脚本没有回 ${name} 这条`);
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
if (entry['ok'] === true) {
|
|
276
|
+
if ('data' in entry)
|
|
277
|
+
out[name] = { ok: true, data: entry['data'] };
|
|
278
|
+
else if ('text' in entry)
|
|
279
|
+
out[name] = { ok: true, data: entry['text'] };
|
|
280
|
+
else
|
|
281
|
+
out[name] = failure('parse', `页面脚本 ${name} 这条没有 data / text`);
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
const pageCode = typeof entry['code'] === 'string' ? entry['code'] : '';
|
|
285
|
+
const message = typeof entry['message'] === 'string' && entry['message'] !== ''
|
|
286
|
+
? entry['message']
|
|
287
|
+
: `读不到(${pageCode === '' ? 'unknown' : pageCode})`;
|
|
288
|
+
out[name] = pageFailure(pageCode, message, recipe.loginUrl);
|
|
289
|
+
}
|
|
290
|
+
return out;
|
|
291
|
+
}
|
|
292
|
+
const openclis = new Map();
|
|
293
|
+
function opencliOf(plugin, timeoutMs) {
|
|
294
|
+
const key = `${plugin}:${String(timeoutMs)}`;
|
|
295
|
+
const cached = openclis.get(key);
|
|
296
|
+
if (cached !== undefined)
|
|
297
|
+
return cached;
|
|
298
|
+
const created = createOpencli({
|
|
299
|
+
shell: options.shell,
|
|
300
|
+
plugin,
|
|
301
|
+
command,
|
|
302
|
+
timeoutMs,
|
|
303
|
+
});
|
|
304
|
+
openclis.set(key, created);
|
|
305
|
+
return created;
|
|
306
|
+
}
|
|
307
|
+
async function readCommand(request) {
|
|
308
|
+
if (options.shell === undefined)
|
|
309
|
+
return failure('invalid-request', '没有 shell,跑不了命令');
|
|
310
|
+
if (request.adapter !== 'opencli') {
|
|
311
|
+
return failure('invalid-request', `不认识的命令适配器:${request.adapter}`);
|
|
312
|
+
}
|
|
313
|
+
const plugin = request.argv[0];
|
|
314
|
+
if (plugin === undefined)
|
|
315
|
+
return failure('invalid-request', '命令缺少 argv[0](插件名)');
|
|
316
|
+
const timeoutMs = request.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
317
|
+
try {
|
|
318
|
+
const { value } = await opencliOf(plugin, timeoutMs).json(request.argv.slice(1));
|
|
319
|
+
return { ok: true, data: value };
|
|
320
|
+
}
|
|
321
|
+
catch (error) {
|
|
322
|
+
if (!(error instanceof WorkbenchDataError))
|
|
323
|
+
return failure('command', String(error));
|
|
324
|
+
const code = error.code === 'auth-required' || error.code === 'permission-denied'
|
|
325
|
+
? 'auth'
|
|
326
|
+
: error.code === 'cli-timeout'
|
|
327
|
+
? 'timeout'
|
|
328
|
+
: error.code === 'cli-bad-output'
|
|
329
|
+
? 'parse'
|
|
330
|
+
: 'command';
|
|
331
|
+
return failure(code, error.message, {
|
|
332
|
+
...(error.detail === undefined ? {} : { detail: error.detail }),
|
|
333
|
+
...(error.hint === undefined ? {} : { hint: error.hint }),
|
|
334
|
+
...(error.helpUrl === undefined ? {} : { helpUrl: error.helpUrl }),
|
|
335
|
+
pageCode: error.code,
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return {
|
|
340
|
+
async read(request) {
|
|
341
|
+
if (request.kind === 'http')
|
|
342
|
+
return (await readHttp(request));
|
|
343
|
+
if (request.kind === 'browser')
|
|
344
|
+
return (await readBrowser(request));
|
|
345
|
+
if (request.kind === 'command')
|
|
346
|
+
return (await readCommand(request));
|
|
347
|
+
return failure('invalid-request', '不认识的读类型');
|
|
348
|
+
},
|
|
349
|
+
async request(recipe, overrides) {
|
|
350
|
+
return (await readRecipe(recipe, overrides, 'request'));
|
|
351
|
+
},
|
|
352
|
+
async requestMany(recipe, calls) {
|
|
353
|
+
return (await readMany(recipe, calls));
|
|
354
|
+
},
|
|
355
|
+
async submit(recipe, overrides) {
|
|
356
|
+
return (await readRecipe(recipe, overrides, 'submit'));
|
|
357
|
+
},
|
|
358
|
+
};
|
|
359
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DshShell } from '@dsh-workbench/shared';
|
|
2
|
+
import type { ExtensionReader } from '@dsh-workbench/shared/host';
|
|
3
|
+
export interface ExtensionReaderOptions {
|
|
4
|
+
readonly shell?: DshShell | undefined;
|
|
5
|
+
readonly command?: string | undefined;
|
|
6
|
+
readonly logger?: {
|
|
7
|
+
warn?(message: string): void;
|
|
8
|
+
} | undefined;
|
|
9
|
+
}
|
|
10
|
+
export declare function createExtensionReader(options: ExtensionReaderOptions): ExtensionReader;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { WorkbenchDataError } from '@dsh-workbench/shared/host';
|
|
2
|
+
import { createOpencli } from '../cli/index.js';
|
|
3
|
+
export function createExtensionReader(options) {
|
|
4
|
+
const readers = new Map();
|
|
5
|
+
function readerOf(need) {
|
|
6
|
+
if (need.runtime !== 'opencli') {
|
|
7
|
+
throw new WorkbenchDataError('unsupported-runtime', `没有能跑 ${need.runtime} 扩展的适配器:这个模块要的是 ${need.name}`, { hint: '换一个带这种运行时的适配器集合,或把这条需要改成 opencli 扩展。' });
|
|
8
|
+
}
|
|
9
|
+
if (options.shell === undefined) {
|
|
10
|
+
throw new WorkbenchDataError('cli-exec-failed', `读不了:这台机器上没有 shell 服务(${need.name})`, {
|
|
11
|
+
hint: '工作台的 shell 服务没起来,重启一下工作台再试',
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
const cached = readers.get(need.name);
|
|
15
|
+
if (cached !== undefined)
|
|
16
|
+
return cached;
|
|
17
|
+
const created = createOpencli({
|
|
18
|
+
shell: options.shell,
|
|
19
|
+
plugin: need.name,
|
|
20
|
+
...(options.command === undefined ? {} : { command: options.command }),
|
|
21
|
+
...(options.logger === undefined ? {} : { logger: options.logger }),
|
|
22
|
+
});
|
|
23
|
+
readers.set(need.name, created);
|
|
24
|
+
return created;
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
async json(need, args, callOptions = {}) {
|
|
28
|
+
const result = await readerOf(need).json(args, {
|
|
29
|
+
...(callOptions.key === undefined ? {} : { key: callOptions.key }),
|
|
30
|
+
...(callOptions.workdir === undefined ? {} : { workdir: callOptions.workdir }),
|
|
31
|
+
...(callOptions.session === undefined ? {} : { session: callOptions.session }),
|
|
32
|
+
});
|
|
33
|
+
return result;
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { DshShell } from '@dsh-workbench/shared';
|
|
2
|
+
import type { GitReader } from '@dsh-workbench/shared';
|
|
3
|
+
export interface GitReaderOptions {
|
|
4
|
+
readonly shell: DshShell;
|
|
5
|
+
readonly logger?: {
|
|
6
|
+
warn?(message: string): void;
|
|
7
|
+
} | undefined;
|
|
8
|
+
}
|
|
9
|
+
export declare function createGitReader(options: GitReaderOptions): GitReader;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { quoteShellArg, runShell, tailLines } from '@dsh-workbench/shared/host';
|
|
2
|
+
function keyOf(path) {
|
|
3
|
+
return String(path ?? '').trim();
|
|
4
|
+
}
|
|
5
|
+
export function createGitReader(options) {
|
|
6
|
+
const { shell, logger } = options;
|
|
7
|
+
return {
|
|
8
|
+
async origin(path) {
|
|
9
|
+
const key = keyOf(path);
|
|
10
|
+
if (!key)
|
|
11
|
+
return { kind: 'unknown', reason: 'no-path' };
|
|
12
|
+
let result;
|
|
13
|
+
try {
|
|
14
|
+
result = await runShell(shell, `git -C ${quoteShellArg(key)} remote get-url origin`);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
logger?.warn?.(`[dsh-workbench] git remote 执行失败:${String(error)}`);
|
|
18
|
+
return { kind: 'unknown', reason: 'exec-failed', detail: String(error) };
|
|
19
|
+
}
|
|
20
|
+
if (result.exitCode !== 0)
|
|
21
|
+
return { kind: 'no-origin', detail: tailLines(result.stderr, 3) };
|
|
22
|
+
return { kind: 'ok', remote: result.stdout.trim() };
|
|
23
|
+
},
|
|
24
|
+
async branch(path) {
|
|
25
|
+
const key = keyOf(path);
|
|
26
|
+
if (!key)
|
|
27
|
+
return { kind: 'unknown', reason: 'no-path' };
|
|
28
|
+
let result;
|
|
29
|
+
try {
|
|
30
|
+
result = await runShell(shell, `git -C ${quoteShellArg(key)} rev-parse --abbrev-ref HEAD`);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
logger?.warn?.(`[dsh-workbench] git rev-parse --abbrev-ref 执行失败:${String(error)}`);
|
|
34
|
+
return { kind: 'unknown', reason: 'exec-failed', detail: String(error) };
|
|
35
|
+
}
|
|
36
|
+
const name = result.stdout.trim();
|
|
37
|
+
if (result.exitCode !== 0)
|
|
38
|
+
return { kind: 'no-branch', detail: tailLines(result.stderr, 3) };
|
|
39
|
+
if (name === '' || name === 'HEAD')
|
|
40
|
+
return { kind: 'no-branch', detail: name };
|
|
41
|
+
return { kind: 'ok', branch: name };
|
|
42
|
+
},
|
|
43
|
+
async head(path) {
|
|
44
|
+
const key = keyOf(path);
|
|
45
|
+
if (!key)
|
|
46
|
+
return { kind: 'unknown', reason: 'no-path' };
|
|
47
|
+
let result;
|
|
48
|
+
try {
|
|
49
|
+
result = await runShell(shell, `git -C ${quoteShellArg(key)} rev-parse HEAD`);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
logger?.warn?.(`[dsh-workbench] git rev-parse 执行失败:${String(error)}`);
|
|
53
|
+
return { kind: 'unknown', reason: 'exec-failed', detail: String(error) };
|
|
54
|
+
}
|
|
55
|
+
if (result.exitCode !== 0)
|
|
56
|
+
return { kind: 'no-head', detail: tailLines(result.stderr, 3) };
|
|
57
|
+
return { kind: 'ok', commit: result.stdout.trim() };
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { LibReader } from '@dsh-workbench/shared/host';
|
|
2
|
+
export interface LibReaderOptions {
|
|
3
|
+
readonly roots?: readonly string[] | undefined;
|
|
4
|
+
readonly env?: Readonly<Record<string, string | undefined>> | undefined;
|
|
5
|
+
}
|
|
6
|
+
export declare function createLibReader(options?: LibReaderOptions): LibReader;
|