@dsh-workbench/adapter-runtime 1.2.12 → 1.3.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.
- package/channel/core.js +2 -1
- package/channel/opencli-plugin.json +1 -1
- package/channel/package.json +1 -1
- package/dist/browser/browser-read.d.ts +2 -0
- package/dist/browser/browser-read.js +57 -16
- package/dist/data/attachment.d.ts +3 -0
- package/dist/data/attachment.js +31 -0
- package/dist/data/channel.js +3 -0
- package/dist/data/command-read.d.ts +7 -0
- package/dist/data/command-read.js +52 -0
- package/dist/data/http.d.ts +5 -0
- package/dist/data/http.js +36 -0
- package/dist/data/index.d.ts +3 -8
- package/dist/data/index.js +197 -205
- package/dist/data/plan-cache.d.ts +11 -0
- package/dist/data/plan-cache.js +53 -0
- package/dist/data/queue.d.ts +11 -0
- package/dist/data/queue.js +50 -0
- package/dist/data/result.d.ts +9 -0
- package/dist/data/result.js +93 -0
- package/dist/data/submit-plan.d.ts +7 -0
- package/dist/data/submit-plan.js +150 -0
- package/dist/data/types.d.ts +19 -0
- package/dist/data/types.js +4 -0
- package/package.json +2 -2
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export function failure(code, message, extra = {}) {
|
|
2
|
+
return { ok: false, error: { code, message, ...extra } };
|
|
3
|
+
}
|
|
4
|
+
export function parse(text) {
|
|
5
|
+
try {
|
|
6
|
+
return { ok: true, data: JSON.parse(text) };
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return failure('parse', '返回的不是 JSON', { detail: text.slice(0, 2000) });
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function asRecord(value) {
|
|
13
|
+
return typeof value === 'object' && value !== null ? value : undefined;
|
|
14
|
+
}
|
|
15
|
+
function dataCodeOf(pageCode) {
|
|
16
|
+
const code = pageCode.trim().toUpperCase().replace(/-/g, '_');
|
|
17
|
+
if (code === 'NOT_LOGGED_IN' ||
|
|
18
|
+
code === 'AUTH_FAILED' ||
|
|
19
|
+
code === 'AUTH_REQUIRED' ||
|
|
20
|
+
code === 'UNAUTHORIZED' ||
|
|
21
|
+
code === 'UNAUTHENTICATED' ||
|
|
22
|
+
code.includes('UNAUTH')) {
|
|
23
|
+
return 'auth';
|
|
24
|
+
}
|
|
25
|
+
if (code === 'PERMISSION_DENIED' || code === 'FORBIDDEN' || code === 'NO_PERMISSION') {
|
|
26
|
+
return 'permission-denied';
|
|
27
|
+
}
|
|
28
|
+
if (code === 'TIMEOUT' || code === 'CLI_TIMEOUT')
|
|
29
|
+
return 'timeout';
|
|
30
|
+
if (code === 'ABORTED' || code === 'CANCELLED')
|
|
31
|
+
return 'aborted';
|
|
32
|
+
if (code === 'HTTP_403')
|
|
33
|
+
return 'permission-denied';
|
|
34
|
+
if (/^HTTP_\d{3}$/.test(code))
|
|
35
|
+
return 'http';
|
|
36
|
+
if (code === 'PARSE' || code === 'SHAPE')
|
|
37
|
+
return 'parse';
|
|
38
|
+
return 'browser';
|
|
39
|
+
}
|
|
40
|
+
function statusOf(pageCode) {
|
|
41
|
+
const match = /^http-(\d{3})$/i.exec(pageCode.trim());
|
|
42
|
+
return match === null ? undefined : Number(match[1]);
|
|
43
|
+
}
|
|
44
|
+
export function browserFailure(result, loginUrl) {
|
|
45
|
+
const pageCode = result.pageCode ?? result.code;
|
|
46
|
+
const mapped = dataCodeOf(result.code);
|
|
47
|
+
const code = mapped === 'browser' ? dataCodeOf(pageCode) : mapped;
|
|
48
|
+
const status = statusOf(result.code) ?? statusOf(pageCode);
|
|
49
|
+
const helpUrl = result.helpUrl ?? (code === 'auth' ? loginUrl : undefined);
|
|
50
|
+
return {
|
|
51
|
+
ok: false,
|
|
52
|
+
error: {
|
|
53
|
+
code,
|
|
54
|
+
message: result.message,
|
|
55
|
+
...(status === undefined ? {} : { status }),
|
|
56
|
+
...(result.detail === undefined ? {} : { detail: result.detail }),
|
|
57
|
+
...(result.hint === undefined ? {} : { hint: result.hint }),
|
|
58
|
+
...(helpUrl === undefined ? {} : { helpUrl }),
|
|
59
|
+
...(pageCode === '' ? {} : { pageCode }),
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export function pageFailure(pageCode, message, loginUrl) {
|
|
64
|
+
const code = dataCodeOf(pageCode);
|
|
65
|
+
const status = statusOf(pageCode);
|
|
66
|
+
const helpUrl = code === 'auth' ? loginUrl : undefined;
|
|
67
|
+
return {
|
|
68
|
+
ok: false,
|
|
69
|
+
error: {
|
|
70
|
+
code,
|
|
71
|
+
message,
|
|
72
|
+
...(status === undefined ? {} : { status }),
|
|
73
|
+
...(helpUrl === undefined ? {} : { helpUrl }),
|
|
74
|
+
...(pageCode === '' ? {} : { pageCode }),
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export function pickOf(value, path) {
|
|
79
|
+
let at = value;
|
|
80
|
+
for (const key of path.split('.')) {
|
|
81
|
+
const box = asRecord(at);
|
|
82
|
+
if (box === undefined)
|
|
83
|
+
return undefined;
|
|
84
|
+
at = box[key];
|
|
85
|
+
}
|
|
86
|
+
return at;
|
|
87
|
+
}
|
|
88
|
+
export function allFailure(names, code, message, extra = {}) {
|
|
89
|
+
const out = {};
|
|
90
|
+
for (const name of names)
|
|
91
|
+
out[name] = { ok: false, error: { code, message, ...extra } };
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { DataFailure, PlanResults, RequestPlan, SubmitPlanOptions } from '@dsh-workbench/shared/host';
|
|
2
|
+
import type { SaveBytes } from './types.js';
|
|
3
|
+
export type SubmitRun = (script: string) => Promise<{
|
|
4
|
+
readonly ok: true;
|
|
5
|
+
readonly value: unknown;
|
|
6
|
+
} | DataFailure>;
|
|
7
|
+
export declare function submitPlanOf(plan: RequestPlan, options: SubmitPlanOptions, run: SubmitRun, saveBytes?: SaveBytes | undefined): Promise<PlanResults>;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { allSame, planProblemOf, planResultsOf, scriptOfPlan } from '@dsh-workbench/shared/host';
|
|
2
|
+
import { attachOf } from './attachment.js';
|
|
3
|
+
import { failure } from './result.js';
|
|
4
|
+
const MAX_PRIOR_BYTES = 128_000;
|
|
5
|
+
function invalid(plan, message) {
|
|
6
|
+
return allSame(plan, { code: 'invalid-request', message });
|
|
7
|
+
}
|
|
8
|
+
function hasOwnIdempotencyKey(step) {
|
|
9
|
+
const headers = step.request.headers ?? {};
|
|
10
|
+
return Object.keys(headers).some((one) => one.toLowerCase() === 'idempotency-key');
|
|
11
|
+
}
|
|
12
|
+
function idempotent(step, key) {
|
|
13
|
+
const headers = step.request.headers ?? {};
|
|
14
|
+
if (Object.keys(headers).some((name) => name.toLowerCase() === 'idempotency-key'))
|
|
15
|
+
return step;
|
|
16
|
+
return {
|
|
17
|
+
...step,
|
|
18
|
+
request: {
|
|
19
|
+
...step.request,
|
|
20
|
+
headers: { ...headers, 'Idempotency-Key': `${key}:${step.name}` },
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function confirmWithAbort(confirm, step, index, signal) {
|
|
25
|
+
if (signal === undefined)
|
|
26
|
+
return Promise.resolve(confirm(step, index));
|
|
27
|
+
if (signal.aborted)
|
|
28
|
+
return Promise.resolve(false);
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
let settled = false;
|
|
31
|
+
const settle = (value, error) => {
|
|
32
|
+
if (settled)
|
|
33
|
+
return;
|
|
34
|
+
settled = true;
|
|
35
|
+
signal.removeEventListener('abort', onAbort);
|
|
36
|
+
if (error === undefined)
|
|
37
|
+
resolve(value);
|
|
38
|
+
else
|
|
39
|
+
reject(error);
|
|
40
|
+
};
|
|
41
|
+
const onAbort = () => {
|
|
42
|
+
settle(false);
|
|
43
|
+
};
|
|
44
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
45
|
+
Promise.resolve()
|
|
46
|
+
.then(() => confirm(step, index))
|
|
47
|
+
.then((value) => {
|
|
48
|
+
settle(value);
|
|
49
|
+
}, (error) => {
|
|
50
|
+
settle(false, error);
|
|
51
|
+
})
|
|
52
|
+
.catch(() => { });
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function isAborted(signal) {
|
|
56
|
+
return signal?.aborted === true;
|
|
57
|
+
}
|
|
58
|
+
function skipped(step, message, pageCode = 'skipped') {
|
|
59
|
+
return { ok: false, error: { code: 'skipped', message, pageCode } };
|
|
60
|
+
}
|
|
61
|
+
export async function submitPlanOf(plan, options, run, saveBytes) {
|
|
62
|
+
if (options.idempotencyKey.trim() === '')
|
|
63
|
+
return invalid(plan, 'submitPlan 要一个非空的幂等键');
|
|
64
|
+
const problem = planProblemOf(plan)[0];
|
|
65
|
+
if (problem !== undefined)
|
|
66
|
+
return invalid(plan, `${problem.path}: ${problem.problem}`);
|
|
67
|
+
if (plan.origin === undefined || plan.origin === '') {
|
|
68
|
+
return invalid(plan, '这份计划只写了 site,没经过宿主解析');
|
|
69
|
+
}
|
|
70
|
+
for (const step of plan.steps) {
|
|
71
|
+
if (step.request.method === 'GET')
|
|
72
|
+
return invalid(plan, `第 ${step.name} 步是 GET:写计划只收写请求`);
|
|
73
|
+
if (hasOwnIdempotencyKey(step)) {
|
|
74
|
+
return invalid(plan, `第 ${step.name} 步自己带了 Idempotency-Key:交给 submitPlan 的 idempotencyKey 统一加`);
|
|
75
|
+
}
|
|
76
|
+
if (step.repeat !== undefined || step.while !== undefined) {
|
|
77
|
+
return invalid(plan, `第 ${step.name} 步带分页:写计划不支持分页`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const out = Object.create(null);
|
|
81
|
+
const done = {};
|
|
82
|
+
for (const [index, step] of plan.steps.entries()) {
|
|
83
|
+
if (isAborted(options.signal)) {
|
|
84
|
+
out[step.name] = failure('aborted', '已停止', { pageCode: 'aborted' });
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
const priorTooLarge = Buffer.byteLength(JSON.stringify(Object.fromEntries(Object.entries(done).map(([name, value]) => [name, { value }]))), 'utf8') >
|
|
88
|
+
MAX_PRIOR_BYTES;
|
|
89
|
+
if (priorTooLarge) {
|
|
90
|
+
out[step.name] = failure('invalid-request', `前面几步的结果带不进这一步(超过 ${String(MAX_PRIOR_BYTES)} 字节):把大字段留到最后一步`, { pageCode: 'prior-too-large' });
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
let confirmed;
|
|
94
|
+
try {
|
|
95
|
+
confirmed = await confirmWithAbort(options.confirm, step, index, options.signal);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
out[step.name] = failure('invalid-request', `确认失败:${error instanceof Error ? error.message : String(error)}`);
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
if (!confirmed) {
|
|
102
|
+
out[step.name] = isAborted(options.signal)
|
|
103
|
+
? failure('aborted', '已停止', { pageCode: 'aborted' })
|
|
104
|
+
: skipped(step, '没有确认,没有写', 'declined');
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
const partial = {
|
|
108
|
+
...plan,
|
|
109
|
+
steps: plan.steps.slice(0, index + 1).map((one) => idempotent(one, options.idempotencyKey)),
|
|
110
|
+
};
|
|
111
|
+
const prior = Object.fromEntries(Object.entries(done).map(([name, value]) => [name, { value }]));
|
|
112
|
+
let script;
|
|
113
|
+
try {
|
|
114
|
+
script = scriptOfPlan(partial, prior);
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
out[step.name] = failure('invalid-request', error instanceof Error ? error.message : String(error));
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
const ran = await run(script);
|
|
121
|
+
if (!ran.ok) {
|
|
122
|
+
out[step.name] = ran;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
const page = planResultsOf(ran.value, partial.steps.map((one) => one.name), {
|
|
126
|
+
...(plan.loginUrl === undefined ? {} : { loginUrl: plan.loginUrl }),
|
|
127
|
+
});
|
|
128
|
+
const got = page[step.name];
|
|
129
|
+
if (got === undefined) {
|
|
130
|
+
out[step.name] = failure('parse', '这一步没有结果');
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
const final = step.response?.saveAs === 'attachment' ? await attachOf(step, got, saveBytes) : got;
|
|
134
|
+
out[step.name] = final;
|
|
135
|
+
try {
|
|
136
|
+
options.onStep?.(step, index, final);
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
}
|
|
140
|
+
if (!final.ok)
|
|
141
|
+
break;
|
|
142
|
+
done[step.name] = final.data;
|
|
143
|
+
}
|
|
144
|
+
for (const step of plan.steps) {
|
|
145
|
+
if (out[step.name] !== undefined)
|
|
146
|
+
continue;
|
|
147
|
+
out[step.name] = skipped(step, '前面没有完成,没有写');
|
|
148
|
+
}
|
|
149
|
+
return out;
|
|
150
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Seam } from '@dsh-workbench/shared/host';
|
|
2
|
+
import type { QueueWaitEvent } from './queue.js';
|
|
3
|
+
export interface SaveBytesInput {
|
|
4
|
+
readonly data: Uint8Array;
|
|
5
|
+
readonly contentType: string;
|
|
6
|
+
readonly name: string;
|
|
7
|
+
}
|
|
8
|
+
export type SaveBytes = (input: SaveBytesInput) => Promise<unknown>;
|
|
9
|
+
export interface DataReaderOptions {
|
|
10
|
+
readonly seam?: Seam | undefined;
|
|
11
|
+
readonly command?: string | undefined;
|
|
12
|
+
readonly fetch?: typeof fetch | undefined;
|
|
13
|
+
readonly channel?: boolean | undefined;
|
|
14
|
+
readonly channelInstalled?: (() => boolean) | undefined;
|
|
15
|
+
readonly queueWaitMs?: number | undefined;
|
|
16
|
+
readonly onQueue?: ((event: QueueWaitEvent) => void) | undefined;
|
|
17
|
+
readonly saveBytes?: SaveBytes | undefined;
|
|
18
|
+
}
|
|
19
|
+
export declare function sessionOf(value: string | undefined): string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dsh-workbench/adapter-runtime",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0-beta.0",
|
|
4
4
|
"description": "Opencli, browser and data runtime adapters.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"bin"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@dsh-workbench/shared": "1.
|
|
20
|
+
"@dsh-workbench/shared": "1.3.0-beta.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/node": "^24.19.0",
|