@dsh-workbench/adapter-runtime 1.2.12 → 1.3.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/channel/core.js CHANGED
@@ -207,7 +207,8 @@ export async function perform(page, kwargs, method) {
207
207
  if (new TextEncoder().encode(text).length > maxBytes) {
208
208
  return fail('too-large', `返回超过 ${Math.round(maxBytes / 1024)} KB`);
209
209
  }
210
- if (response.status === 401 || response.status === 403) return fail('not-logged-in', `HTTP ${response.status}`);
210
+ if (response.status === 401) return fail('not-logged-in', 'HTTP 401');
211
+ if (response.status === 403) return fail('forbidden', `HTTP 403 ${text.slice(0, 200)}`);
211
212
  if (!response.ok) return fail(`http-${response.status}`, text.slice(0, 200));
212
213
 
213
214
  const kind = textOf(kwargs.kind) === 'text' ? 'text' : 'json';
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "workbench",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "DSH 工作台的公共通道:用浏览器登录态在 Node 侧发请求",
5
5
  "opencli": ">=1.8.0 <2.0.0"
6
6
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencli-plugin-workbench",
3
- "version": "1.2.12",
3
+ "version": "1.3.0",
4
4
  "private": true,
5
5
  "type": "module",
6
6
  "description": "DSH 工作台的公共通道:带浏览器登录态的 Node 侧 fetch,只服务 cookie / cookieValue 两种凭据来源",
@@ -22,7 +22,9 @@ export interface BrowserReadOptions {
22
22
  readonly open?: boolean;
23
23
  readonly timeoutMs?: number;
24
24
  readonly retryMs?: number;
25
+ readonly retry?: boolean;
25
26
  readonly settleMs?: number;
27
+ readonly signal?: AbortSignal;
26
28
  readonly platform?: NodeJS.Platform;
27
29
  }
28
30
  export declare function evalScriptArgOf(script: string, platform?: NodeJS.Platform): string;
@@ -46,8 +46,25 @@ function blockerFor(code) {
46
46
  return 'auth-required';
47
47
  return undefined;
48
48
  }
49
- function sleep(ms) {
50
- return new Promise((resolve) => setTimeout(resolve, ms));
49
+ function abortedNow(signal) {
50
+ return signal?.aborted === true;
51
+ }
52
+ function sleep(ms, signal) {
53
+ return new Promise((resolve) => {
54
+ if (signal?.aborted === true) {
55
+ resolve();
56
+ return;
57
+ }
58
+ const timer = setTimeout(() => {
59
+ signal?.removeEventListener('abort', onAbort);
60
+ resolve();
61
+ }, ms);
62
+ function onAbort() {
63
+ clearTimeout(timer);
64
+ resolve();
65
+ }
66
+ signal?.addEventListener('abort', onAbort, { once: true });
67
+ });
51
68
  }
52
69
  export function evalScriptArgOf(script, platform = process.platform) {
53
70
  if (platform !== 'win32')
@@ -56,14 +73,19 @@ export function evalScriptArgOf(script, platform = process.platform) {
56
73
  const decoded = `eval(new TextDecoder().decode(Uint8Array.from(atob('${bytes}'), (one) => one.charCodeAt(0))))`;
57
74
  return `(async () => { const value = ${decoded}; return typeof value === 'function' ? value() : value; })()`;
58
75
  }
59
- async function shellRun(seam, command, argv, timeoutMs, platform) {
76
+ async function shellRun(seam, command, argv, timeoutMs, platform, signal) {
77
+ if (signal?.aborted === true)
78
+ return { kind: 'aborted', detail: '已停止', stderr: '' };
60
79
  const result = await seam.run({
61
80
  command: backgroundLine(command, argv, platform),
62
81
  intent: noWrite(),
63
82
  retry: 'safe',
64
83
  timeoutMs,
65
84
  env: OPENCLI_WINDOW_ENV,
85
+ ...(signal === undefined ? {} : { signal }),
66
86
  });
87
+ if (result.aborted)
88
+ return { kind: 'aborted', detail: '已停止', stderr: result.stderr };
67
89
  if (result.timedOut) {
68
90
  return { kind: 'timeout', detail: `没在 ${Math.round(timeoutMs / 1000)} 秒内返回`, stderr: result.stderr };
69
91
  }
@@ -72,11 +94,11 @@ async function shellRun(seam, command, argv, timeoutMs, platform) {
72
94
  }
73
95
  return { kind: 'ok', stdout: result.stdout };
74
96
  }
75
- async function evalOnce(seam, command, session, script, timeoutMs, retryMs, platform) {
97
+ async function evalOnce(seam, command, session, script, timeoutMs, retryMs, retry, platform, signal) {
76
98
  const run = async () => {
77
99
  let attempt;
78
100
  try {
79
- attempt = await shellRun(seam, command, ['browser', session, 'eval', evalScriptArgOf(script, platform), '--window', 'background'], timeoutMs, platform);
101
+ attempt = await shellRun(seam, command, ['browser', session, 'eval', evalScriptArgOf(script, platform), '--window', 'background'], timeoutMs, platform, signal);
80
102
  }
81
103
  catch (error) {
82
104
  return {
@@ -90,6 +112,8 @@ async function evalOnce(seam, command, session, script, timeoutMs, retryMs, plat
90
112
  },
91
113
  };
92
114
  }
115
+ if (attempt.kind === 'aborted')
116
+ return { kind: 'aborted' };
93
117
  if (attempt.kind === 'timeout') {
94
118
  return {
95
119
  kind: 'failure',
@@ -132,17 +156,19 @@ async function evalOnce(seam, command, session, script, timeoutMs, retryMs, plat
132
156
  return { kind: 'value', value };
133
157
  };
134
158
  const first = await run();
135
- if (first.kind === 'value')
159
+ if (first.kind === 'value' || first.kind === 'aborted')
136
160
  return first;
137
- if (first.result.code !== 'cli-exec-failed')
161
+ if (!retry || first.result.code !== 'cli-exec-failed')
138
162
  return first;
139
- await sleep(retryMs);
163
+ await sleep(retryMs, signal);
164
+ if (abortedNow(signal))
165
+ return { kind: 'aborted' };
140
166
  return run();
141
167
  }
142
- async function openOnce(seam, command, session, url, timeoutMs, platform) {
168
+ async function openOnce(seam, command, session, url, timeoutMs, platform, signal) {
143
169
  let attempt;
144
170
  try {
145
- attempt = await shellRun(seam, command, ['browser', session, 'open', url, '--window', 'background'], timeoutMs, platform);
171
+ attempt = await shellRun(seam, command, ['browser', session, 'open', url, '--window', 'background'], timeoutMs, platform, signal);
146
172
  }
147
173
  catch (error) {
148
174
  return {
@@ -156,6 +182,9 @@ async function openOnce(seam, command, session, url, timeoutMs, platform) {
156
182
  }
157
183
  if (attempt.kind === 'ok')
158
184
  return undefined;
185
+ if (attempt.kind === 'aborted') {
186
+ return { ok: false, code: 'aborted', pageCode: 'aborted', message: '已停止' };
187
+ }
159
188
  return {
160
189
  ok: false,
161
190
  code: attempt.kind === 'timeout' ? 'cli-timeout' : 'cli-exec-failed',
@@ -166,15 +195,22 @@ async function openOnce(seam, command, session, url, timeoutMs, platform) {
166
195
  };
167
196
  }
168
197
  export async function browserRead(options) {
169
- const { seam, script, url, loginUrl, open = true } = options;
198
+ const { seam, script, url, loginUrl, open = true, signal } = options;
170
199
  const command = options.command ?? 'opencli';
171
200
  const session = options.session ?? BROWSER_READ_SESSION;
172
201
  const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
173
202
  const retryMs = options.retryMs ?? DEFAULT_RETRY_MS;
203
+ const retry = options.retry ?? true;
174
204
  const settleMs = options.settleMs ?? DEFAULT_SETTLE_MS;
175
205
  const platform = options.platform ?? process.platform;
176
206
  const at = now();
177
- const first = await evalOnce(seam, command, session, script, timeoutMs, retryMs, platform);
207
+ if (signal?.aborted === true) {
208
+ return { ok: false, code: 'aborted', pageCode: 'aborted', at, message: '已停止' };
209
+ }
210
+ const first = await evalOnce(seam, command, session, script, timeoutMs, retryMs, retry, platform, signal);
211
+ if (first.kind === 'aborted') {
212
+ return { ok: false, code: 'aborted', pageCode: 'aborted', at, message: '已停止' };
213
+ }
178
214
  if (first.kind === 'failure')
179
215
  return { ...first.result, at };
180
216
  let page = first.value;
@@ -190,11 +226,17 @@ export async function browserRead(options) {
190
226
  ...(url === undefined ? {} : { helpUrl: url }),
191
227
  };
192
228
  }
193
- const opened = await openOnce(seam, command, session, url, timeoutMs, platform);
229
+ const opened = await openOnce(seam, command, session, url, timeoutMs, platform, signal);
194
230
  if (opened !== undefined)
195
231
  return { ...opened, at };
196
- await sleep(settleMs);
197
- const second = await evalOnce(seam, command, session, script, timeoutMs, retryMs, platform);
232
+ await sleep(settleMs, signal);
233
+ if (abortedNow(signal)) {
234
+ return { ok: false, code: 'aborted', pageCode: 'aborted', at, message: '已停止' };
235
+ }
236
+ const second = await evalOnce(seam, command, session, script, timeoutMs, retryMs, retry, platform, signal);
237
+ if (second.kind === 'aborted') {
238
+ return { ok: false, code: 'aborted', pageCode: 'aborted', at, message: '已停止' };
239
+ }
198
240
  if (second.kind === 'failure')
199
241
  return { ...second.result, at };
200
242
  page = second.value;
@@ -236,7 +278,6 @@ export async function browserRead(options) {
236
278
  message,
237
279
  hint: '这个账号没有权限',
238
280
  detail: JSON.stringify(page),
239
- ...(loginUrl === undefined && url === undefined ? {} : { helpUrl: loginUrl ?? url }),
240
281
  };
241
282
  }
242
283
  return {
@@ -0,0 +1,3 @@
1
+ import type { PlanStep, PlanStepResult } from '@dsh-workbench/shared/host';
2
+ import type { SaveBytes } from './types.js';
3
+ export declare function attachOf(step: PlanStep, one: PlanStepResult<unknown>, saveBytes: SaveBytes | undefined): Promise<PlanStepResult<unknown>>;
@@ -0,0 +1,31 @@
1
+ import { failure } from './result.js';
2
+ export async function attachOf(step, one, saveBytes) {
3
+ if (!one.ok)
4
+ return one;
5
+ const box = one.data;
6
+ if (typeof box?.base64 !== 'string') {
7
+ return failure('parse', '这一步不是二进制,存不成附件', { pageCode: 'attachment-shape' });
8
+ }
9
+ const base64 = box.base64;
10
+ const bytes = /^[A-Za-z0-9+/]*={0,2}$/.test(base64) && base64.length % 4 === 0 ? Buffer.from(base64, 'base64') : undefined;
11
+ const size = box.size;
12
+ if (bytes === undefined || (typeof size === 'number' && bytes.length !== size)) {
13
+ return failure('parse', '附件内容是坏的(base64 不完整)', { pageCode: 'attachment-broken' });
14
+ }
15
+ if (saveBytes === undefined) {
16
+ return failure('invalid-request', '这个运行时不支持保存附件', { pageCode: 'no-attachment-service' });
17
+ }
18
+ try {
19
+ const ref = await saveBytes({
20
+ data: Uint8Array.from(bytes),
21
+ contentType: typeof box.contentType === 'string' && box.contentType !== '' ? box.contentType : 'application/octet-stream',
22
+ name: step.name,
23
+ });
24
+ return { ...one, data: ref };
25
+ }
26
+ catch (error) {
27
+ return failure('command', `附件没存下来:${error instanceof Error ? error.message : String(error)}`, {
28
+ pageCode: 'attachment-save-failed',
29
+ });
30
+ }
31
+ }
@@ -24,6 +24,9 @@ function queryOf(recipe, overrides) {
24
24
  return query;
25
25
  }
26
26
  export function channelUrlOf(recipe, overrides) {
27
+ if (recipe.origin === undefined || recipe.origin === '') {
28
+ throw new Error('这条配方只写了 site,没经过宿主解析');
29
+ }
27
30
  const path = overrides.path ?? recipe.request.path;
28
31
  const url = new URL(path.startsWith('/') ? `${recipe.origin}${path}` : path);
29
32
  for (const [name, value] of Object.entries(queryOf(recipe, overrides)))
@@ -0,0 +1,7 @@
1
+ import type { DataCallOptions, DataRequest, DataResult, Seam } from '@dsh-workbench/shared/host';
2
+ export declare function createCommandRead(options: {
3
+ readonly seam: Seam | undefined;
4
+ readonly command: string;
5
+ }): (request: Extract<DataRequest, {
6
+ kind: 'command';
7
+ }>, callOptions: DataCallOptions) => Promise<DataResult<unknown>>;
@@ -0,0 +1,52 @@
1
+ import { WorkbenchDataError } from '@dsh-workbench/shared/host';
2
+ import { createOpencli } from '../cli/index.js';
3
+ import { DEFAULT_TIMEOUT_MS } from './http.js';
4
+ import { failure } from './result.js';
5
+ export function createCommandRead(options) {
6
+ const openclis = new Map();
7
+ const opencliOf = (plugin, timeoutMs) => {
8
+ const key = `${plugin}:${String(timeoutMs)}`;
9
+ const cached = openclis.get(key);
10
+ if (cached !== undefined)
11
+ return cached;
12
+ const created = createOpencli({ seam: options.seam, plugin, command: options.command, timeoutMs });
13
+ openclis.set(key, created);
14
+ return created;
15
+ };
16
+ return async (request, callOptions) => {
17
+ if (callOptions.signal?.aborted === true)
18
+ return failure('aborted', '已停止', { pageCode: 'aborted' });
19
+ if (options.seam === undefined)
20
+ return failure('invalid-request', '没有执行缝,跑不了命令');
21
+ if (request.adapter !== 'opencli') {
22
+ return failure('invalid-request', `不认识的命令适配器:${request.adapter}`);
23
+ }
24
+ const plugin = request.argv[0];
25
+ if (plugin === undefined)
26
+ return failure('invalid-request', '命令缺少 argv[0](插件名)');
27
+ const timeoutMs = request.timeoutMs ?? DEFAULT_TIMEOUT_MS;
28
+ try {
29
+ const { value } = await opencliOf(plugin, timeoutMs).json(request.argv.slice(1));
30
+ return { ok: true, data: value };
31
+ }
32
+ catch (error) {
33
+ if (!(error instanceof WorkbenchDataError))
34
+ return failure('command', String(error));
35
+ const code = error.code === 'auth-required'
36
+ ? 'auth'
37
+ : error.code === 'permission-denied'
38
+ ? 'permission-denied'
39
+ : error.code === 'cli-timeout'
40
+ ? 'timeout'
41
+ : error.code === 'cli-bad-output'
42
+ ? 'parse'
43
+ : 'command';
44
+ return failure(code, error.message, {
45
+ ...(error.detail === undefined ? {} : { detail: error.detail }),
46
+ ...(error.hint === undefined ? {} : { hint: error.hint }),
47
+ ...(error.helpUrl === undefined ? {} : { helpUrl: error.helpUrl }),
48
+ pageCode: error.code,
49
+ });
50
+ }
51
+ };
52
+ }
@@ -0,0 +1,5 @@
1
+ import type { DataCallOptions, DataRequest, DataResult } from '@dsh-workbench/shared/host';
2
+ export declare const DEFAULT_TIMEOUT_MS = 20000;
3
+ export declare function readHttp(request: Extract<DataRequest, {
4
+ kind: 'http';
5
+ }>, callOptions: DataCallOptions, fetchOf: typeof fetch | undefined): Promise<DataResult<unknown>>;
@@ -0,0 +1,36 @@
1
+ import { failure, parse } from './result.js';
2
+ export const DEFAULT_TIMEOUT_MS = 20_000;
3
+ export async function readHttp(request, callOptions, fetchOf) {
4
+ if (fetchOf === undefined)
5
+ return failure('invalid-request', '这个运行时没有 fetch');
6
+ const timeoutMs = request.timeoutMs ?? DEFAULT_TIMEOUT_MS;
7
+ const controller = new AbortController();
8
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
9
+ const signal = callOptions.signal === undefined ? controller.signal : AbortSignal.any([callOptions.signal, controller.signal]);
10
+ try {
11
+ const response = await fetchOf(request.url, {
12
+ method: 'GET',
13
+ ...(request.headers === undefined ? {} : { headers: request.headers }),
14
+ signal,
15
+ });
16
+ const text = await response.text();
17
+ if (!response.ok) {
18
+ const code = response.status === 401 ? 'auth' : response.status === 403 ? 'permission-denied' : 'http';
19
+ return failure(code, `HTTP ${response.status}`, {
20
+ status: response.status,
21
+ detail: text.slice(0, 2000),
22
+ });
23
+ }
24
+ return parse(text);
25
+ }
26
+ catch (error) {
27
+ if (callOptions.signal?.aborted === true)
28
+ return failure('aborted', '已停止', { pageCode: 'aborted' });
29
+ if (controller.signal.aborted)
30
+ return failure('timeout', `读超时(${timeoutMs}ms)`);
31
+ return failure('http', error instanceof Error ? error.message : String(error));
32
+ }
33
+ finally {
34
+ clearTimeout(timer);
35
+ }
36
+ }
@@ -1,11 +1,6 @@
1
- import { type Seam } from '@dsh-workbench/shared/host';
2
1
  import type { DataReader } from '@dsh-workbench/shared/host';
2
+ import { type DataReaderOptions } from './types.js';
3
3
  export * from './channel-plugin.js';
4
- export interface DataReaderOptions {
5
- readonly seam?: Seam | undefined;
6
- readonly command?: string | undefined;
7
- readonly fetch?: typeof fetch | undefined;
8
- readonly channel?: boolean | undefined;
9
- readonly channelInstalled?: (() => boolean) | undefined;
10
- }
4
+ export type { QueueWaitEvent } from './queue.js';
5
+ export type { DataReaderOptions, SaveBytes, SaveBytesInput } from './types.js';
11
6
  export declare function createDataReader(options?: DataReaderOptions): DataReader;