@dsh-workbench/adapter-runtime 0.0.0-stage → 1.0.0-beta.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.
@@ -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,322 @@
1
+ import { callsProblemOf, recipeProblemOf, runShell, scriptOf, scriptOfMany } from '@dsh-workbench/shared/host';
2
+ import { browserRead } from '../browser/index.js';
3
+ import { channelCallOf, channelCommandLine, ensureChannel, } from './channel.js';
4
+ const DEFAULT_TIMEOUT_MS = 20_000;
5
+ function failure(code, message, extra = {}) {
6
+ return { ok: false, error: { code, message, ...extra } };
7
+ }
8
+ function parse(text) {
9
+ try {
10
+ return { ok: true, data: JSON.parse(text) };
11
+ }
12
+ catch {
13
+ return failure('parse', '返回的不是 JSON', { detail: text.slice(0, 2000) });
14
+ }
15
+ }
16
+ function asRecord(value) {
17
+ return typeof value === 'object' && value !== null ? value : undefined;
18
+ }
19
+ function dataCodeOf(pageCode) {
20
+ const code = pageCode.trim().toUpperCase().replace(/-/g, '_');
21
+ if (code === 'NOT_LOGGED_IN' ||
22
+ code === 'AUTH_FAILED' ||
23
+ code === 'AUTH_REQUIRED' ||
24
+ code === 'UNAUTHORIZED' ||
25
+ code === 'UNAUTHENTICATED' ||
26
+ code === 'PERMISSION_DENIED' ||
27
+ code === 'FORBIDDEN' ||
28
+ code.includes('UNAUTH')) {
29
+ return 'auth';
30
+ }
31
+ if (code === 'TIMEOUT' || code === 'CLI_TIMEOUT')
32
+ return 'timeout';
33
+ if (/^HTTP_\d{3}$/.test(code))
34
+ return 'http';
35
+ if (code === 'PARSE' || code === 'SHAPE')
36
+ return 'parse';
37
+ return 'browser';
38
+ }
39
+ function statusOf(pageCode) {
40
+ const match = /^http-(\d{3})$/i.exec(pageCode.trim());
41
+ return match === null ? undefined : Number(match[1]);
42
+ }
43
+ function browserFailure(result, loginUrl) {
44
+ const pageCode = result.pageCode ?? result.code;
45
+ const mapped = dataCodeOf(result.code);
46
+ const code = mapped === 'browser' ? dataCodeOf(pageCode) : mapped;
47
+ const status = statusOf(result.code) ?? statusOf(pageCode);
48
+ const helpUrl = result.helpUrl ?? (code === 'auth' ? loginUrl : undefined);
49
+ return {
50
+ ok: false,
51
+ error: {
52
+ code,
53
+ message: result.message,
54
+ ...(status === undefined ? {} : { status }),
55
+ ...(result.detail === undefined ? {} : { detail: result.detail }),
56
+ ...(result.hint === undefined ? {} : { hint: result.hint }),
57
+ ...(helpUrl === undefined ? {} : { helpUrl }),
58
+ ...(pageCode === '' ? {} : { pageCode }),
59
+ },
60
+ };
61
+ }
62
+ function pageFailure(pageCode, message, loginUrl) {
63
+ const code = dataCodeOf(pageCode);
64
+ const status = statusOf(pageCode);
65
+ const helpUrl = code === 'auth' ? loginUrl : undefined;
66
+ return {
67
+ ok: false,
68
+ error: {
69
+ code,
70
+ message,
71
+ ...(status === undefined ? {} : { status }),
72
+ ...(helpUrl === undefined ? {} : { helpUrl }),
73
+ ...(pageCode === '' ? {} : { pageCode }),
74
+ },
75
+ };
76
+ }
77
+ function pickOf(value, path) {
78
+ let at = value;
79
+ for (const key of path.split('.')) {
80
+ const box = asRecord(at);
81
+ if (box === undefined)
82
+ return undefined;
83
+ at = box[key];
84
+ }
85
+ return at;
86
+ }
87
+ function allFailure(names, code, message, extra = {}) {
88
+ const out = {};
89
+ for (const name of names)
90
+ out[name] = { ok: false, error: { code, message, ...extra } };
91
+ return out;
92
+ }
93
+ export function createDataReader(options = {}) {
94
+ const fetchOf = options.fetch ?? globalThis.fetch;
95
+ const command = options.command ?? 'opencli';
96
+ async function readHttp(request) {
97
+ if (fetchOf === undefined)
98
+ return failure('invalid-request', '这个运行时没有 fetch');
99
+ const timeoutMs = request.timeoutMs ?? DEFAULT_TIMEOUT_MS;
100
+ const controller = new AbortController();
101
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
102
+ try {
103
+ const response = await fetchOf(request.url, {
104
+ method: 'GET',
105
+ ...(request.headers === undefined ? {} : { headers: request.headers }),
106
+ signal: controller.signal,
107
+ });
108
+ const text = await response.text();
109
+ if (!response.ok) {
110
+ return failure(response.status === 401 || response.status === 403 ? 'auth' : 'http', `HTTP ${response.status}`, {
111
+ status: response.status,
112
+ detail: text.slice(0, 2000),
113
+ });
114
+ }
115
+ return parse(text);
116
+ }
117
+ catch (error) {
118
+ if (controller.signal.aborted)
119
+ return failure('timeout', `读超时(${timeoutMs}ms)`);
120
+ return failure('http', error instanceof Error ? error.message : String(error));
121
+ }
122
+ finally {
123
+ clearTimeout(timer);
124
+ }
125
+ }
126
+ async function readBrowser(request) {
127
+ if (options.shell === undefined)
128
+ return failure('invalid-request', '没有 shell,读不了浏览器');
129
+ const result = await browserRead({
130
+ shell: options.shell,
131
+ script: request.script,
132
+ command,
133
+ ...(request.url === undefined ? {} : { url: request.url }),
134
+ ...(request.loginUrl === undefined ? {} : { loginUrl: request.loginUrl }),
135
+ ...(request.session === undefined ? {} : { session: request.session }),
136
+ ...(request.open === undefined ? {} : { open: request.open }),
137
+ ...(request.timeoutMs === undefined ? {} : { timeoutMs: request.timeoutMs }),
138
+ });
139
+ if (!result.ok)
140
+ return browserFailure(result, request.loginUrl);
141
+ return { ok: true, data: result.value };
142
+ }
143
+ const channelState = { value: 'unknown' };
144
+ async function readChannel(shell, call, recipe) {
145
+ const result = await runShell(shell, channelCommandLine(call), { timeoutMs: call.timeoutMs + 5000 });
146
+ if (result.timedOut)
147
+ return failure('timeout', `通道读超时(${call.timeoutMs}ms)`);
148
+ if (result.exitCode !== 0)
149
+ return undefined;
150
+ let parsed;
151
+ try {
152
+ parsed = JSON.parse(result.stdout);
153
+ }
154
+ catch {
155
+ return undefined;
156
+ }
157
+ const box = asRecord(parsed);
158
+ if (box === undefined)
159
+ return undefined;
160
+ if (box['ok'] !== true) {
161
+ const pageCode = typeof box['code'] === 'string' ? box['code'] : '';
162
+ const message = typeof box['message'] === 'string' && box['message'] !== ''
163
+ ? box['message']
164
+ : `读不到(${pageCode === '' ? 'unknown' : pageCode})`;
165
+ return pageFailure(pageCode, message, recipe.loginUrl);
166
+ }
167
+ if (recipe.response?.kind === 'text') {
168
+ return typeof box['text'] === 'string'
169
+ ? { ok: true, data: box['text'] }
170
+ : failure('parse', '通道没有回 text', { detail: JSON.stringify(parsed).slice(0, 2000) });
171
+ }
172
+ const path = recipe.response?.path;
173
+ if (path === undefined)
174
+ return { ok: true, data: box['data'] };
175
+ const value = pickOf(box['data'], path);
176
+ return value === undefined
177
+ ? failure('parse', `返回里没有 ${path} 这一格`, { detail: JSON.stringify(parsed).slice(0, 2000) })
178
+ : { ok: true, data: value };
179
+ }
180
+ async function readRecipe(recipe, overrides, mode) {
181
+ const problem = recipeProblemOf(recipe);
182
+ if (problem !== undefined)
183
+ return failure('invalid-request', problem);
184
+ if (mode === 'request' && recipe.request.method !== 'GET') {
185
+ return failure('invalid-request', 'request 只读 GET;写走 submit');
186
+ }
187
+ if (options.shell === undefined)
188
+ return failure('invalid-request', '没有 shell,读不了浏览器');
189
+ const call = options.channel === false ? undefined : channelCallOf(recipe, overrides ?? {}, command);
190
+ if (call !== undefined &&
191
+ (await ensureChannel(options.shell, command, channelState, options.channelInstalled))) {
192
+ const viaChannel = await readChannel(options.shell, call, recipe);
193
+ if (viaChannel !== undefined)
194
+ return viaChannel;
195
+ channelState.value = 'unavailable';
196
+ }
197
+ let script;
198
+ try {
199
+ script = scriptOf(recipe, overrides);
200
+ }
201
+ catch (error) {
202
+ return failure('invalid-request', error instanceof Error ? error.message : String(error));
203
+ }
204
+ const result = await browserRead({
205
+ shell: options.shell,
206
+ script,
207
+ command,
208
+ url: recipe.origin,
209
+ ...(recipe.loginUrl === undefined ? {} : { loginUrl: recipe.loginUrl }),
210
+ ...(recipe.session === undefined ? {} : { session: recipe.session }),
211
+ ...(recipe.timeoutMs === undefined ? {} : { timeoutMs: recipe.timeoutMs }),
212
+ });
213
+ if (!result.ok)
214
+ return browserFailure(result, recipe.loginUrl);
215
+ const box = asRecord(result.value);
216
+ if (box !== undefined && box['ok'] === true) {
217
+ if ('data' in box)
218
+ return { ok: true, data: box['data'] };
219
+ if ('text' in box)
220
+ return { ok: true, data: box['text'] };
221
+ }
222
+ return failure('parse', '页面脚本没有回 data / text', {
223
+ detail: JSON.stringify(result.value).slice(0, 2000),
224
+ });
225
+ }
226
+ async function readMany(recipe, calls) {
227
+ const names = Object.keys(calls);
228
+ const problem = callsProblemOf(recipe, calls);
229
+ if (problem !== undefined)
230
+ return allFailure(names, 'invalid-request', problem);
231
+ if (recipe.request.method !== 'GET') {
232
+ return allFailure(names, 'invalid-request', 'requestMany 只读 GET;写走 submit');
233
+ }
234
+ if (options.shell === undefined)
235
+ return allFailure(names, 'invalid-request', '没有 shell,读不了浏览器');
236
+ let script;
237
+ try {
238
+ script = scriptOfMany(recipe, calls);
239
+ }
240
+ catch (error) {
241
+ return allFailure(names, 'invalid-request', error instanceof Error ? error.message : String(error));
242
+ }
243
+ const result = await browserRead({
244
+ shell: options.shell,
245
+ script,
246
+ command,
247
+ url: recipe.origin,
248
+ ...(recipe.loginUrl === undefined ? {} : { loginUrl: recipe.loginUrl }),
249
+ ...(recipe.session === undefined ? {} : { session: recipe.session }),
250
+ ...(recipe.timeoutMs === undefined ? {} : { timeoutMs: recipe.timeoutMs }),
251
+ });
252
+ if (!result.ok) {
253
+ const one = browserFailure(result, recipe.loginUrl);
254
+ const failed = {};
255
+ for (const name of names)
256
+ failed[name] = one;
257
+ return failed;
258
+ }
259
+ const box = asRecord(result.value);
260
+ const results = box === undefined ? undefined : asRecord(box['results']);
261
+ if (box === undefined || box['ok'] !== true || results === undefined) {
262
+ return allFailure(names, 'parse', '页面脚本没有回 results', {
263
+ detail: JSON.stringify(result.value).slice(0, 2000),
264
+ });
265
+ }
266
+ const out = {};
267
+ for (const name of names) {
268
+ const entry = asRecord(results[name]);
269
+ if (entry === undefined) {
270
+ out[name] = failure('parse', `页面脚本没有回 ${name} 这条`);
271
+ continue;
272
+ }
273
+ if (entry['ok'] === true) {
274
+ if ('data' in entry)
275
+ out[name] = { ok: true, data: entry['data'] };
276
+ else if ('text' in entry)
277
+ out[name] = { ok: true, data: entry['text'] };
278
+ else
279
+ out[name] = failure('parse', `页面脚本 ${name} 这条没有 data / text`);
280
+ continue;
281
+ }
282
+ const pageCode = typeof entry['code'] === 'string' ? entry['code'] : '';
283
+ const message = typeof entry['message'] === 'string' && entry['message'] !== ''
284
+ ? entry['message']
285
+ : `读不到(${pageCode === '' ? 'unknown' : pageCode})`;
286
+ out[name] = pageFailure(pageCode, message, recipe.loginUrl);
287
+ }
288
+ return out;
289
+ }
290
+ async function readCommand(request) {
291
+ if (options.shell === undefined)
292
+ return failure('invalid-request', '没有 shell,跑不了命令');
293
+ const timeoutMs = request.timeoutMs ?? DEFAULT_TIMEOUT_MS;
294
+ const result = await runShell(options.shell, request.command, { timeoutMs });
295
+ if (result.timedOut)
296
+ return failure('timeout', `命令超时(${timeoutMs}ms)`);
297
+ if (result.exitCode !== 0) {
298
+ return failure('command', `命令退出码 ${String(result.exitCode)}`, { detail: result.stderr.slice(0, 2000) });
299
+ }
300
+ return parse(result.stdout);
301
+ }
302
+ return {
303
+ async read(request) {
304
+ if (request.kind === 'http')
305
+ return (await readHttp(request));
306
+ if (request.kind === 'browser')
307
+ return (await readBrowser(request));
308
+ if (request.kind === 'command')
309
+ return (await readCommand(request));
310
+ return failure('invalid-request', '不认识的读类型');
311
+ },
312
+ async request(recipe, overrides) {
313
+ return (await readRecipe(recipe, overrides, 'request'));
314
+ },
315
+ async requestMany(recipe, calls) {
316
+ return (await readMany(recipe, calls));
317
+ },
318
+ async submit(recipe, overrides) {
319
+ return (await readRecipe(recipe, overrides, 'submit'));
320
+ },
321
+ };
322
+ }
@@ -0,0 +1,3 @@
1
+ export * from './cli/index.js';
2
+ export * from './browser/index.js';
3
+ export * from './data/index.js';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './cli/index.js';
2
+ export * from './browser/index.js';
3
+ export * from './data/index.js';
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
2
  "name": "@dsh-workbench/adapter-runtime",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0-beta.0",
4
+ "description": "Opencli, browser and data runtime adapters.",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "default": "./dist/index.js"
10
+ }
11
+ },
12
+ "files": [
13
+ "dist",
14
+ "channel"
15
+ ],
16
+ "dependencies": {
17
+ "@dsh-workbench/shared": "1.0.0-beta.0"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^24.19.0",
21
+ "@vitest/coverage-v8": "^5.0.2",
22
+ "vitest": "^5.0.2"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "license": "MIT",
28
+ "scripts": {
29
+ "build": "tsc -p tsconfig.json",
30
+ "test": "vitest run --testTimeout=20000",
31
+ "coverage": "vitest run --coverage --coverage.reporter=text-summary --testTimeout=20000",
32
+ "arch": "arch-guard",
33
+ "check:runtimes": "node ../../scripts/check-adapter-package.mjs --runtime"
34
+ }
6
35
  }