@dsh-workbench/shared 1.5.2-alpha.4 → 1.5.2-rc.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.
Files changed (344) hide show
  1. package/dist/components/Activity.css +69 -0
  2. package/dist/components/Activity.d.ts +56 -0
  3. package/dist/components/Activity.js +47 -0
  4. package/dist/components/ApprovalDetail.css +3 -0
  5. package/dist/components/ApprovalDetail.d.ts +42 -0
  6. package/dist/components/ApprovalDetail.js +53 -0
  7. package/dist/components/ApprovalPlan.css +39 -0
  8. package/dist/components/ApprovalPlan.d.ts +27 -0
  9. package/dist/components/ApprovalPlan.js +19 -0
  10. package/dist/components/Card.css +19 -0
  11. package/dist/components/Card.d.ts +26 -0
  12. package/dist/components/Card.js +18 -0
  13. package/dist/components/CardAnchor.d.ts +21 -0
  14. package/dist/components/CardAnchor.js +19 -0
  15. package/dist/components/CardAnchor.module.css +3 -0
  16. package/dist/components/CardBoundary.d.ts +42 -0
  17. package/dist/components/CardBoundary.js +58 -0
  18. package/dist/components/CardBoundary.module.css +5 -0
  19. package/dist/components/CardHead.css +87 -0
  20. package/dist/components/CardHead.d.ts +30 -0
  21. package/dist/components/CardHead.js +27 -0
  22. package/dist/components/ChoiceGroup.css +182 -0
  23. package/dist/components/ChoiceGroup.d.ts +54 -0
  24. package/dist/components/ChoiceGroup.js +63 -0
  25. package/dist/components/ConfirmBar.css +26 -0
  26. package/dist/components/ConfirmBar.d.ts +36 -0
  27. package/dist/components/ConfirmBar.js +26 -0
  28. package/dist/components/EmptyIllustration.d.ts +12 -0
  29. package/dist/components/EmptyIllustration.js +32 -0
  30. package/dist/components/Field.css +20 -0
  31. package/dist/components/Field.d.ts +18 -0
  32. package/dist/components/Field.js +15 -0
  33. package/dist/components/Form.css +6 -0
  34. package/dist/components/Form.d.ts +22 -0
  35. package/dist/components/Form.js +60 -0
  36. package/dist/components/FormActions.css +13 -0
  37. package/dist/components/FormActions.d.ts +29 -0
  38. package/dist/components/FormActions.js +43 -0
  39. package/dist/components/FormNodes.d.ts +107 -0
  40. package/dist/components/FormNodes.js +165 -0
  41. package/dist/components/FormRows.css +107 -0
  42. package/dist/components/FormRows.d.ts +36 -0
  43. package/dist/components/FormRows.js +26 -0
  44. package/dist/components/FormTypes.d.ts +84 -0
  45. package/dist/components/FormTypes.js +1 -0
  46. package/dist/components/Link.css +22 -0
  47. package/dist/components/Link.d.ts +22 -0
  48. package/dist/components/Link.js +17 -0
  49. package/dist/components/LongTaskRow.css +171 -0
  50. package/dist/components/LongTaskRow.d.ts +94 -0
  51. package/dist/components/LongTaskRow.js +103 -0
  52. package/dist/components/Note.css +13 -0
  53. package/dist/components/Note.d.ts +20 -0
  54. package/dist/components/Note.js +14 -0
  55. package/dist/components/ResourceStates.css +114 -0
  56. package/dist/components/ResourceStates.d.ts +44 -0
  57. package/dist/components/ResourceStates.js +41 -0
  58. package/dist/components/Rows.css +42 -0
  59. package/dist/components/Rows.d.ts +28 -0
  60. package/dist/components/Rows.js +21 -0
  61. package/dist/components/SaveBar.d.ts +24 -0
  62. package/dist/components/SaveBar.js +25 -0
  63. package/dist/components/SelectField.css +182 -0
  64. package/dist/components/SelectField.d.ts +89 -0
  65. package/dist/components/SelectField.js +260 -0
  66. package/dist/components/SelectFieldNodes.d.ts +49 -0
  67. package/dist/components/SelectFieldNodes.js +43 -0
  68. package/dist/components/SettingsSection.css +25 -0
  69. package/dist/components/SettingsSection.d.ts +20 -0
  70. package/dist/components/SettingsSection.js +17 -0
  71. package/dist/components/SetupShell.d.ts +93 -0
  72. package/dist/components/SetupShell.js +64 -0
  73. package/dist/components/SetupShell.module.css +81 -0
  74. package/dist/components/Spinner.css +5 -0
  75. package/dist/components/Spinner.d.ts +15 -0
  76. package/dist/components/Spinner.js +13 -0
  77. package/dist/components/Stack.css +6 -0
  78. package/dist/components/Stack.d.ts +14 -0
  79. package/dist/components/Stack.js +10 -0
  80. package/dist/components/StateBlock.css +38 -0
  81. package/dist/components/StateBlock.d.ts +24 -0
  82. package/dist/components/StateBlock.js +22 -0
  83. package/dist/components/TextField.css +32 -0
  84. package/dist/components/TextField.d.ts +14 -0
  85. package/dist/components/TextField.js +10 -0
  86. package/dist/components/board-module.d.ts +307 -0
  87. package/dist/components/board-module.js +1 -0
  88. package/dist/components/card-view-validate.d.ts +5 -0
  89. package/dist/components/card-view-validate.js +256 -0
  90. package/dist/components/icons.d.ts +63 -0
  91. package/dist/components/icons.js +107 -0
  92. package/dist/components/layout/icon-command.d.ts +8 -0
  93. package/dist/components/layout/icon-command.js +21 -0
  94. package/dist/contract/capture.d.ts +16 -0
  95. package/dist/contract/capture.js +1 -0
  96. package/dist/contract/diagnostics.d.ts +34 -0
  97. package/dist/contract/diagnostics.js +124 -0
  98. package/dist/contract/needs.d.ts +3 -1
  99. package/dist/contract/needs.js +4 -2
  100. package/dist/contract/protocol/bundle-install.d.ts +1 -1
  101. package/dist/contract/protocol/unit-manager.d.ts +50 -0
  102. package/dist/contract/protocol/unit-manager.js +2 -0
  103. package/dist/contract/unit.js +2 -1
  104. package/dist/host/adapter-contract.d.ts +3 -0
  105. package/dist/host/adapter-contract.js +95 -0
  106. package/dist/host/adapter-set.d.ts +27 -0
  107. package/dist/host/adapter-set.js +258 -0
  108. package/dist/host/adapter-types.d.ts +87 -0
  109. package/dist/host/adapter-types.js +23 -0
  110. package/dist/host/adapters/platform.d.ts +14 -0
  111. package/dist/host/adapters/platform.js +40 -0
  112. package/dist/host/adapters.d.ts +28 -0
  113. package/dist/host/adapters.js +9 -0
  114. package/dist/host/auth-loss.d.ts +33 -0
  115. package/dist/host/auth-loss.js +107 -0
  116. package/dist/host/board-fact.d.ts +8 -0
  117. package/dist/host/board-fact.js +16 -0
  118. package/dist/host/boot-record.d.ts +11 -0
  119. package/dist/host/boot-record.js +31 -0
  120. package/dist/host/browser.d.ts +3 -0
  121. package/dist/host/browser.js +3 -0
  122. package/dist/host/bundle-manager.d.ts +4 -0
  123. package/dist/host/bundle-manager.js +338 -0
  124. package/dist/host/changelog.d.ts +28 -0
  125. package/dist/host/changelog.js +201 -0
  126. package/dist/host/cli-error.d.ts +8 -0
  127. package/dist/host/cli-error.js +70 -0
  128. package/dist/host/cli.d.ts +4 -0
  129. package/dist/host/cli.js +9 -0
  130. package/dist/host/config-sync.d.ts +40 -0
  131. package/dist/host/config-sync.js +267 -0
  132. package/dist/host/context-ref.d.ts +13 -0
  133. package/dist/host/context-ref.js +41 -0
  134. package/dist/host/data-error.d.ts +15 -0
  135. package/dist/host/data-error.js +23 -0
  136. package/dist/host/data-plan-check.d.ts +10 -0
  137. package/dist/host/data-plan-check.js +315 -0
  138. package/dist/host/data-plan.d.ts +15 -0
  139. package/dist/host/data-plan.js +283 -0
  140. package/dist/host/data-root.d.ts +5 -0
  141. package/dist/host/data-root.js +23 -0
  142. package/dist/host/diy-debug.d.ts +80 -0
  143. package/dist/host/diy-debug.js +89 -0
  144. package/dist/host/diy-preset.d.ts +26 -0
  145. package/dist/host/diy-preset.js +115 -0
  146. package/dist/host/diy-record.d.ts +135 -0
  147. package/dist/host/diy-record.js +163 -0
  148. package/dist/host/dsh-compat.d.ts +27 -0
  149. package/dist/host/dsh-compat.js +75 -0
  150. package/dist/host/dsh-plugins.d.ts +5 -0
  151. package/dist/host/dsh-plugins.js +51 -0
  152. package/dist/host/env-policy.d.ts +82 -0
  153. package/dist/host/env-policy.js +181 -0
  154. package/dist/host/extension.d.ts +7 -0
  155. package/dist/host/extension.js +40 -0
  156. package/dist/host/facts.d.ts +31 -0
  157. package/dist/host/facts.js +58 -0
  158. package/dist/host/fs-write.d.ts +96 -0
  159. package/dist/host/fs-write.js +183 -0
  160. package/dist/host/git.d.ts +5 -0
  161. package/dist/host/git.js +1 -0
  162. package/dist/host/host-actions.d.ts +2 -0
  163. package/dist/host/host-actions.js +5 -0
  164. package/dist/host/host-kit.d.ts +37 -0
  165. package/dist/host/host-kit.js +27 -0
  166. package/dist/host/http.d.ts +47 -0
  167. package/dist/host/http.js +289 -0
  168. package/dist/host/install-failure.d.ts +50 -0
  169. package/dist/host/install-failure.js +184 -0
  170. package/dist/host/installed-module.d.ts +25 -0
  171. package/dist/host/installed-module.js +289 -0
  172. package/dist/host/installed-sources.d.ts +10 -0
  173. package/dist/host/installed-sources.js +63 -0
  174. package/dist/host/lease.d.ts +70 -0
  175. package/dist/host/lease.js +264 -0
  176. package/dist/host/lib-capabilities.d.ts +22 -0
  177. package/dist/host/lib-capabilities.js +37 -0
  178. package/dist/host/lib-install.d.ts +19 -0
  179. package/dist/host/lib-install.js +40 -0
  180. package/dist/host/lib-runners.d.ts +18 -0
  181. package/dist/host/lib-runners.js +46 -0
  182. package/dist/host/local-declaration.d.ts +7 -0
  183. package/dist/host/local-declaration.js +33 -0
  184. package/dist/host/long-task.d.ts +97 -0
  185. package/dist/host/long-task.js +297 -0
  186. package/dist/host/market-index-claim.d.ts +19 -0
  187. package/dist/host/market-index-claim.js +87 -0
  188. package/dist/host/market-index-doc.d.ts +12 -0
  189. package/dist/host/market-index-doc.js +1 -0
  190. package/dist/host/market-index-parse.d.ts +28 -0
  191. package/dist/host/market-index-parse.js +413 -0
  192. package/dist/host/market-index.d.ts +13 -0
  193. package/dist/host/market-index.js +105 -0
  194. package/dist/host/module-root.d.ts +1 -0
  195. package/dist/host/module-root.js +5 -0
  196. package/dist/host/needs.d.ts +4 -0
  197. package/dist/host/needs.js +2 -0
  198. package/dist/host/npm-registry.d.ts +10 -0
  199. package/dist/host/npm-registry.js +16 -0
  200. package/dist/host/package-content.d.ts +8 -0
  201. package/dist/host/package-content.js +90 -0
  202. package/dist/host/package-kind.d.ts +13 -0
  203. package/dist/host/package-kind.js +51 -0
  204. package/dist/host/package-route.d.ts +33 -0
  205. package/dist/host/package-route.js +68 -0
  206. package/dist/host/panel-state.d.ts +120 -0
  207. package/dist/host/panel-state.js +177 -0
  208. package/dist/host/patch-blocks.d.ts +13 -0
  209. package/dist/host/patch-blocks.js +52 -0
  210. package/dist/host/patch.d.ts +4 -0
  211. package/dist/host/patch.js +7 -0
  212. package/dist/host/paths.d.ts +12 -0
  213. package/dist/host/paths.js +20 -0
  214. package/dist/host/payload.d.ts +12 -0
  215. package/dist/host/payload.js +16 -0
  216. package/dist/host/plan-results.d.ts +5 -0
  217. package/dist/host/plan-results.js +71 -0
  218. package/dist/host/plan-run.d.ts +41 -0
  219. package/dist/host/plan-run.js +87 -0
  220. package/dist/host/platform.d.ts +14 -0
  221. package/dist/host/platform.js +40 -0
  222. package/dist/host/plugin-ledger.d.ts +32 -0
  223. package/dist/host/plugin-ledger.js +106 -0
  224. package/dist/host/plugin.d.ts +3 -0
  225. package/dist/host/plugin.js +38 -0
  226. package/dist/host/port-types.d.ts +233 -0
  227. package/dist/host/port-types.js +1 -0
  228. package/dist/host/preflight.d.ts +32 -0
  229. package/dist/host/preflight.js +1 -0
  230. package/dist/host/process-alive.d.ts +6 -0
  231. package/dist/host/process-alive.js +16 -0
  232. package/dist/host/profile-bundles.d.ts +20 -0
  233. package/dist/host/profile-bundles.js +48 -0
  234. package/dist/host/profile-create.d.ts +19 -0
  235. package/dist/host/profile-create.js +124 -0
  236. package/dist/host/profile-port.d.ts +4 -0
  237. package/dist/host/profile-port.js +4 -0
  238. package/dist/host/profile-registries.d.ts +10 -0
  239. package/dist/host/profile-registries.js +149 -0
  240. package/dist/host/provision.d.ts +32 -0
  241. package/dist/host/provision.js +105 -0
  242. package/dist/host/read-json.d.ts +1 -0
  243. package/dist/host/read-json.js +11 -0
  244. package/dist/host/recipe-rules.d.ts +5 -0
  245. package/dist/host/recipe-rules.js +412 -0
  246. package/dist/host/recipe.d.ts +14 -0
  247. package/dist/host/recipe.js +332 -0
  248. package/dist/host/registry-declaration.d.ts +21 -0
  249. package/dist/host/registry-declaration.js +42 -0
  250. package/dist/host/reinstall-record.d.ts +16 -0
  251. package/dist/host/reinstall-record.js +54 -0
  252. package/dist/host/release-age.d.ts +24 -0
  253. package/dist/host/release-age.js +92 -0
  254. package/dist/host/release-packages.generated.d.ts +1 -0
  255. package/dist/host/release-packages.generated.js +10 -0
  256. package/dist/host/release-probe.d.ts +37 -0
  257. package/dist/host/release-probe.js +156 -0
  258. package/dist/host/runtime/browser-argv.d.ts +1 -0
  259. package/dist/host/runtime/browser-argv.js +9 -0
  260. package/dist/host/runtime/dsh-compat.d.ts +4 -0
  261. package/dist/host/runtime/dsh-compat.js +32 -16
  262. package/dist/host/runtime/front-app.d.ts +6 -0
  263. package/dist/host/runtime/front-app.js +70 -0
  264. package/dist/host/runtime/verified-dsh.d.ts +5 -0
  265. package/dist/host/runtime/verified-dsh.js +20 -0
  266. package/dist/host/shell-render.d.ts +52 -0
  267. package/dist/host/shell-render.js +117 -0
  268. package/dist/host/shell-text.d.ts +30 -0
  269. package/dist/host/shell-text.js +40 -0
  270. package/dist/host/shell.d.ts +25 -0
  271. package/dist/host/shell.js +55 -0
  272. package/dist/host/site.d.ts +45 -0
  273. package/dist/host/site.js +182 -0
  274. package/dist/host/spec.d.ts +8 -0
  275. package/dist/host/spec.js +24 -0
  276. package/dist/host/submit-confirm.d.ts +19 -0
  277. package/dist/host/submit-confirm.js +45 -0
  278. package/dist/host/supply/builtin-catalog.d.ts +3 -0
  279. package/dist/host/supply/builtin-catalog.js +38 -0
  280. package/dist/host/supply/preflight.d.ts +14 -1
  281. package/dist/host/supply/supply-index.js +6 -0
  282. package/dist/host/supply-index.d.ts +38 -0
  283. package/dist/host/supply-index.js +209 -0
  284. package/dist/host/supply-port.d.ts +5 -0
  285. package/dist/host/supply-port.js +50 -0
  286. package/dist/host/time.d.ts +22 -0
  287. package/dist/host/time.js +33 -0
  288. package/dist/host/tokens.d.ts +26 -0
  289. package/dist/host/tokens.js +154 -0
  290. package/dist/host/tool-approval.d.ts +86 -0
  291. package/dist/host/tool-approval.js +118 -0
  292. package/dist/host/tool-kit.d.ts +8 -0
  293. package/dist/host/tool-kit.js +22 -0
  294. package/dist/host/toolbox-port.d.ts +16 -0
  295. package/dist/host/toolbox-port.js +1 -0
  296. package/dist/host/unit-config.d.ts +34 -0
  297. package/dist/host/unit-config.js +28 -0
  298. package/dist/host/unit-data.d.ts +3 -0
  299. package/dist/host/unit-data.js +127 -0
  300. package/dist/host/unit-face.d.ts +95 -0
  301. package/dist/host/unit-face.js +1 -0
  302. package/dist/host/unit-plugin.d.ts +24 -0
  303. package/dist/host/unit-plugin.js +25 -0
  304. package/dist/host/update-journal-manifest.d.ts +19 -0
  305. package/dist/host/update-journal-manifest.js +134 -0
  306. package/dist/host/update-journal-types.d.ts +33 -0
  307. package/dist/host/update-journal-types.js +2 -0
  308. package/dist/host/update-journal.d.ts +43 -0
  309. package/dist/host/update-journal.js +409 -0
  310. package/dist/host/value-handoff.d.ts +16 -0
  311. package/dist/host/value-handoff.js +22 -0
  312. package/dist/host/version-range.d.ts +2 -0
  313. package/dist/host/version-range.js +13 -0
  314. package/dist/host/version.d.ts +4 -0
  315. package/dist/host/version.js +32 -0
  316. package/dist/host/workspace-store.d.ts +19 -0
  317. package/dist/host/workspace-store.js +133 -0
  318. package/dist/host/write-result.d.ts +1 -0
  319. package/dist/host/write-result.js +18 -0
  320. package/dist/host.d.ts +2 -0
  321. package/dist/host.js +2 -0
  322. package/dist/i18n/en.js +14 -3
  323. package/dist/i18n/index.d.ts +15 -4
  324. package/dist/i18n/zh.d.ts +14 -3
  325. package/dist/i18n/zh.js +14 -3
  326. package/dist/lib/bundle-requirements.d.ts +1 -0
  327. package/dist/lib/bundle-requirements.js +9 -0
  328. package/package.json +1 -1
  329. package/src/contract/__tests__/unit.test.ts +7 -0
  330. package/src/contract/needs.ts +5 -3
  331. package/src/contract/protocol/bundle-install.ts +1 -0
  332. package/src/contract/unit.ts +2 -1
  333. package/src/host/runtime/__tests__/dsh-compat.test.ts +19 -1
  334. package/src/host/runtime/__tests__/verified-dsh.test.ts +55 -0
  335. package/src/host/runtime/dsh-compat.ts +36 -12
  336. package/src/host/runtime/verified-dsh.ts +22 -0
  337. package/src/host/supply/__tests__/builtin-catalog.test.ts +44 -0
  338. package/src/host/supply/builtin-catalog.ts +38 -0
  339. package/src/host/supply/preflight.ts +16 -1
  340. package/src/host/supply/supply-index.ts +3 -0
  341. package/src/host.ts +2 -0
  342. package/src/i18n/en.ts +17 -3
  343. package/src/i18n/zh.ts +16 -3
  344. package/src/lib/bundle-requirements.ts +11 -0
@@ -0,0 +1,23 @@
1
+ export class WorkbenchDataError extends Error {
2
+ code;
3
+ detail;
4
+ hint;
5
+ project;
6
+ helpUrl;
7
+ failure;
8
+ constructor(code, message, extra = {}) {
9
+ super(message);
10
+ this.name = 'WorkbenchDataError';
11
+ this.code = code;
12
+ if (extra.detail !== undefined)
13
+ this.detail = extra.detail;
14
+ if (extra.hint !== undefined)
15
+ this.hint = extra.hint;
16
+ if (extra.project !== undefined)
17
+ this.project = extra.project;
18
+ if (extra.helpUrl !== undefined)
19
+ this.helpUrl = extra.helpUrl;
20
+ if (extra.failure !== undefined)
21
+ this.failure = extra.failure;
22
+ }
23
+ }
@@ -0,0 +1,10 @@
1
+ import type { RequestPlan } from '../contract/data.js';
2
+ export declare const PLAN_MAX_STEPS = 32;
3
+ export declare const PLAN_DEFAULT_TIMEOUT_MS = 25000;
4
+ export declare const PLAN_DEFAULT_LOOP = 20;
5
+ export declare const PLAN_DEFAULT_MAX_BYTES = 2000000;
6
+ export interface PlanProblem {
7
+ readonly path: string;
8
+ readonly problem: string;
9
+ }
10
+ export declare function planProblemOf(plan: RequestPlan): readonly PlanProblem[];
@@ -0,0 +1,315 @@
1
+ import { pathSegmentsProblem } from '../contract/guards.js';
2
+ import { authLossProblemsOf } from './auth-loss.js';
3
+ const DEFAULT_MAX_BYTES = 2_000_000;
4
+ const MAX_MAX_BYTES = 8_000_000;
5
+ const MAX_STEPS = 32;
6
+ const DEFAULT_TIMEOUT_MS = 25_000;
7
+ const MIN_TIMEOUT_MS = 1_000;
8
+ const MAX_TIMEOUT_MS = 120_000;
9
+ const MAX_LOOP = 50;
10
+ const DEFAULT_LOOP = 20;
11
+ const REF = /\{\{\s*([A-Za-z0-9_]+(?:\.[A-Za-z0-9_]+)*)\s*\}\}/g;
12
+ const METHODS = new Set(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']);
13
+ const CONTENT_TYPES = new Set(['json', 'form']);
14
+ export const PLAN_MAX_STEPS = MAX_STEPS;
15
+ export const PLAN_DEFAULT_TIMEOUT_MS = DEFAULT_TIMEOUT_MS;
16
+ export const PLAN_DEFAULT_LOOP = DEFAULT_LOOP;
17
+ export const PLAN_DEFAULT_MAX_BYTES = DEFAULT_MAX_BYTES;
18
+ function refsOf(value, acc = []) {
19
+ if (typeof value === 'string') {
20
+ for (const hit of value.matchAll(REF))
21
+ acc.push(hit[1] ?? '');
22
+ return acc;
23
+ }
24
+ if (Array.isArray(value)) {
25
+ for (const one of value)
26
+ refsOf(one, acc);
27
+ return acc;
28
+ }
29
+ if (value !== null && typeof value === 'object') {
30
+ for (const one of Object.values(value))
31
+ refsOf(one, acc);
32
+ }
33
+ return acc;
34
+ }
35
+ function requestValuesOf(step) {
36
+ return [
37
+ step.request.path,
38
+ step.request.query,
39
+ step.request.headers,
40
+ step.request.body,
41
+ step.repeat?.over,
42
+ ];
43
+ }
44
+ const IDENT = /^[A-Za-z][A-Za-z0-9_]*$/;
45
+ function primitiveValue(value) {
46
+ return value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean';
47
+ }
48
+ function originProblemOf(origin) {
49
+ let url;
50
+ try {
51
+ url = new URL(origin);
52
+ }
53
+ catch {
54
+ return 'origin 要是合法 URL';
55
+ }
56
+ if (url.protocol !== 'http:' && url.protocol !== 'https:')
57
+ return 'origin 只能是 http / https';
58
+ if (url.pathname !== '/' || url.search !== '' || url.hash !== '') {
59
+ return 'origin 只能是站点根(不要带 path / query / hash)';
60
+ }
61
+ return undefined;
62
+ }
63
+ function absoluteUrlProblemOf(value) {
64
+ if (typeof value !== 'string' || value.trim() === '')
65
+ return 'loginUrl 不能为空';
66
+ let url;
67
+ try {
68
+ url = new URL(value);
69
+ }
70
+ catch {
71
+ return 'loginUrl 要是完整 URL(http / https 开头)';
72
+ }
73
+ if (url.protocol !== 'http:' && url.protocol !== 'https:')
74
+ return 'loginUrl 只能是 http / https';
75
+ if (url.hostname === '')
76
+ return 'loginUrl 要带站点名';
77
+ return undefined;
78
+ }
79
+ function maxBytesProblemOf(value) {
80
+ if (value === undefined)
81
+ return undefined;
82
+ if (!Number.isInteger(value) || value < 1 || value > MAX_MAX_BYTES) {
83
+ return `maxBytes 要在 1..${String(MAX_MAX_BYTES)}`;
84
+ }
85
+ return undefined;
86
+ }
87
+ function timeoutProblemOf(value) {
88
+ if (value === undefined)
89
+ return undefined;
90
+ if (!Number.isInteger(value) || value < MIN_TIMEOUT_MS || value > MAX_TIMEOUT_MS) {
91
+ return `timeoutMs 要在 ${String(MIN_TIMEOUT_MS)}..${String(MAX_TIMEOUT_MS)}`;
92
+ }
93
+ return undefined;
94
+ }
95
+ export function planProblemOf(plan) {
96
+ const problems = [];
97
+ if (typeof plan !== 'object' || plan === null) {
98
+ return [{ path: 'plan', problem: '计划要是一个对象' }];
99
+ }
100
+ if (!Array.isArray(plan.steps)) {
101
+ return [{ path: 'steps', problem: 'steps 要是数组' }];
102
+ }
103
+ const originText = typeof plan.origin === 'string' ? plan.origin.trim() : '';
104
+ const siteText = typeof plan.site === 'string' ? plan.site.trim() : '';
105
+ if (plan.site !== undefined && siteText === '') {
106
+ problems.push({ path: 'site', problem: 'site 不能为空' });
107
+ }
108
+ if (originText === '' && siteText === '') {
109
+ problems.push({ path: 'origin', problem: '计划要有 origin 或 site 至少一个' });
110
+ }
111
+ else if (originText !== '') {
112
+ const originProblem = originProblemOf(originText);
113
+ if (originProblem !== undefined)
114
+ problems.push({ path: 'origin', problem: originProblem });
115
+ }
116
+ if (plan.loginUrl !== undefined) {
117
+ const problem = absoluteUrlProblemOf(plan.loginUrl);
118
+ if (problem !== undefined)
119
+ problems.push({ path: 'loginUrl', problem });
120
+ }
121
+ const authLoss = plan.authLoss ?? undefined;
122
+ const timeoutProblem = timeoutProblemOf(plan.timeoutMs);
123
+ if (timeoutProblem !== undefined)
124
+ problems.push({ path: 'timeoutMs', problem: timeoutProblem });
125
+ if (plan.steps.length === 0)
126
+ problems.push({ path: 'steps', problem: '至少要有一步' });
127
+ if (plan.steps.length > MAX_STEPS) {
128
+ problems.push({ path: 'steps', problem: `一次最多 ${String(MAX_STEPS)} 步` });
129
+ }
130
+ if (authLoss !== undefined) {
131
+ for (const one of authLossProblemsOf('authLoss', authLoss)) {
132
+ problems.push({
133
+ path: one.path,
134
+ problem: one.problem.startsWith(one.path)
135
+ ? one.problem.slice(one.path.length).replace(/^[::]\s*/, '')
136
+ : one.problem,
137
+ });
138
+ }
139
+ }
140
+ const names = new Set();
141
+ plan.steps.forEach((raw, index) => {
142
+ const at = `steps[${String(index)}]`;
143
+ if (typeof raw !== 'object' || raw === null) {
144
+ problems.push({ path: at, problem: '每一步要是一个对象' });
145
+ return;
146
+ }
147
+ const step = {
148
+ ...raw,
149
+ ...(raw.response === null ? { response: undefined } : {}),
150
+ ...(raw.repeat === null ? { repeat: undefined } : {}),
151
+ ...(raw.while === null ? { while: undefined } : {}),
152
+ };
153
+ if (step.response !== undefined &&
154
+ (typeof step.response !== 'object' || step.response === null || Array.isArray(step.response))) {
155
+ problems.push({ path: `${at}.response`, problem: 'response 要是一个对象' });
156
+ return;
157
+ }
158
+ if (typeof step.request !== 'object' || step.request === null) {
159
+ problems.push({ path: `${at}.request`, problem: 'request 要是一个对象' });
160
+ return;
161
+ }
162
+ if (typeof step.name !== 'string' || !IDENT.test(step.name)) {
163
+ problems.push({ path: `${at}.name`, problem: 'name 要是字母开头的标识符' });
164
+ }
165
+ if (step.name === 'page' || step.name === 'index') {
166
+ problems.push({ path: `${at}.name`, problem: 'page / index 是内置的序号变量,不能当步骤名' });
167
+ }
168
+ if (names.has(step.name))
169
+ problems.push({ path: `${at}.name`, problem: `步骤名重复:${step.name}` });
170
+ if (!METHODS.has(step.request.method)) {
171
+ problems.push({ path: `${at}.request.method`, problem: 'method 只能是 GET / POST / PUT / PATCH / DELETE' });
172
+ }
173
+ if (typeof step.request.path !== 'string' || step.request.path.trim() === '') {
174
+ problems.push({ path: `${at}.request.path`, problem: 'path 不能为空' });
175
+ }
176
+ else {
177
+ const path = step.request.path.trim();
178
+ if (path.startsWith('//') || /^[A-Za-z][A-Za-z0-9+.-]*:\/\//.test(path)) {
179
+ problems.push({
180
+ path: `${at}.request.path`,
181
+ problem: 'path 只能是站内路径:写完整 URL 或 // 开头就绕过了 origin 收口',
182
+ });
183
+ }
184
+ }
185
+ if (step.request.contentType !== undefined && !CONTENT_TYPES.has(step.request.contentType)) {
186
+ problems.push({ path: `${at}.request.contentType`, problem: 'contentType 只能是 json / form' });
187
+ }
188
+ const locals = new Set();
189
+ if (step.repeat !== undefined || step.while !== undefined) {
190
+ locals.add('index');
191
+ locals.add('page');
192
+ }
193
+ if (step.repeat !== undefined)
194
+ locals.add(step.repeat.as ?? 'item');
195
+ const refs = refsOf(requestValuesOf(step));
196
+ for (const ref of refs) {
197
+ const head = ref.split('.')[0] ?? '';
198
+ if (head === '')
199
+ problems.push({ path: at, problem: '引用里没有步骤名' });
200
+ else if (!names.has(head) && !locals.has(head)) {
201
+ problems.push({ path: at, problem: `引用了还没出现的步骤:${head}` });
202
+ }
203
+ }
204
+ if (step.repeat !== undefined && step.while !== undefined) {
205
+ problems.push({ path: at, problem: 'repeat 与 while 只能给一个' });
206
+ }
207
+ if (step.repeat !== undefined) {
208
+ for (const ref of refsOf(step.repeat.over)) {
209
+ const head = ref.split('.')[0] ?? '';
210
+ const ownAs = step.repeat.as ?? 'item';
211
+ if (head === 'index' || head === 'page' || (head === ownAs && !names.has(head))) {
212
+ problems.push({
213
+ path: `${at}.repeat.over`,
214
+ problem: `repeat.over 用不了 ${head}:算区间的时候循环还没开始`,
215
+ });
216
+ }
217
+ }
218
+ if (typeof step.repeat.over !== 'string' || step.repeat.over.trim() === '') {
219
+ problems.push({ path: `${at}.repeat.over`, problem: 'repeat.over 不能为空' });
220
+ }
221
+ const as = step.repeat.as ?? 'item';
222
+ if (!IDENT.test(as))
223
+ problems.push({ path: `${at}.repeat.as`, problem: 'repeat.as 要是字母开头的标识符' });
224
+ if (as === 'index' || as === 'page') {
225
+ problems.push({ path: `${at}.repeat.as`, problem: 'repeat.as 不能用 index / page(那是内置的序号变量)' });
226
+ }
227
+ if (step.repeat.max !== undefined &&
228
+ (!Number.isInteger(step.repeat.max) || step.repeat.max < 1 || step.repeat.max > MAX_LOOP)) {
229
+ problems.push({ path: `${at}.repeat.max`, problem: `repeat.max 要在 1..${String(MAX_LOOP)}` });
230
+ }
231
+ }
232
+ if (step.while !== undefined) {
233
+ if (typeof step.while.path !== 'string' || step.while.path.trim() === '') {
234
+ problems.push({ path: `${at}.while.path`, problem: 'while.path 不能为空' });
235
+ }
236
+ const whilePathProblem = pathSegmentsProblem(step.while.path, `${at}.while.path`);
237
+ if (whilePathProblem !== undefined)
238
+ problems.push({ path: `${at}.while.path`, problem: whilePathProblem });
239
+ if (step.while.equals === undefined) {
240
+ problems.push({ path: `${at}.while.equals`, problem: 'while.equals 不能为空' });
241
+ }
242
+ else if (!primitiveValue(step.while.equals)) {
243
+ problems.push({ path: `${at}.while.equals`, problem: 'while.equals 只支持字符串 / 数字 / 布尔 / null' });
244
+ }
245
+ if (step.while.max !== undefined &&
246
+ (!Number.isInteger(step.while.max) || step.while.max < 1 || step.while.max > MAX_LOOP)) {
247
+ problems.push({ path: `${at}.while.max`, problem: `while.max 要在 1..${String(MAX_LOOP)}` });
248
+ }
249
+ if (step.response?.kind !== undefined && step.response.kind !== 'json') {
250
+ problems.push({ path: `${at}.while`, problem: 'while 只支持 json 响应' });
251
+ }
252
+ }
253
+ const responseKind = step.response?.kind ?? 'json';
254
+ if (responseKind === 'html') {
255
+ if (typeof step.response?.selector !== 'string' || step.response.selector.trim() === '') {
256
+ problems.push({ path: `${at}.response.selector`, problem: 'html 响应要写 selector' });
257
+ }
258
+ if (step.response?.path !== undefined) {
259
+ problems.push({ path: `${at}.response.path`, problem: 'html 响应用 selector,不要再写 path' });
260
+ }
261
+ if (step.response?.maxBytes !== undefined) {
262
+ problems.push({ path: `${at}.response.maxBytes`, problem: 'html 响应不支持 maxBytes' });
263
+ }
264
+ for (const [name, selector] of Object.entries(step.response?.fields ?? {})) {
265
+ if (typeof selector !== 'string' || selector.trim() === '') {
266
+ problems.push({ path: `${at}.response.fields.${name}`, problem: 'fields 的选择器不能为空' });
267
+ }
268
+ }
269
+ }
270
+ else if (step.response?.selector !== undefined || step.response?.fields !== undefined) {
271
+ problems.push({ path: `${at}.response`, problem: 'selector / fields 只用于 html 响应' });
272
+ }
273
+ if (step.response?.saveAs !== undefined) {
274
+ if (step.repeat !== undefined || step.while !== undefined) {
275
+ problems.push({ path: `${at}.response.saveAs`, problem: 'saveAs 与分页只能给一个' });
276
+ }
277
+ if (step.response.saveAs !== 'attachment') {
278
+ problems.push({ path: `${at}.response.saveAs`, problem: 'saveAs 目前只支持 attachment' });
279
+ }
280
+ if ((step.response.kind ?? 'json') !== 'bytes') {
281
+ problems.push({ path: `${at}.response.saveAs`, problem: 'saveAs 只用于 bytes 响应' });
282
+ }
283
+ }
284
+ if (step.request.contentType === 'form' && step.request.body !== undefined) {
285
+ const body = step.request.body;
286
+ if (typeof body !== 'object' || body === null || Array.isArray(body)) {
287
+ problems.push({ path: `${at}.request.body`, problem: 'form 的 body 要是一个对象' });
288
+ }
289
+ else {
290
+ for (const [key, one] of Object.entries(body)) {
291
+ if (typeof one === 'object' && one !== null) {
292
+ problems.push({ path: `${at}.request.body.${key}`, problem: 'form 的值只能是字符串或数字' });
293
+ }
294
+ }
295
+ }
296
+ }
297
+ const kind = step.response?.kind ?? 'json';
298
+ if (kind !== 'json' && kind !== 'text' && kind !== 'bytes' && kind !== 'html') {
299
+ problems.push({ path: `${at}.response.kind`, problem: 'kind 只能是 json / text / bytes / html' });
300
+ }
301
+ if (step.response?.path !== undefined &&
302
+ (typeof step.response.path !== 'string' || step.response.path.trim() === '')) {
303
+ problems.push({ path: `${at}.response.path`, problem: 'response.path 不能为空' });
304
+ }
305
+ const responsePathProblem = pathSegmentsProblem(step.response?.path, `${at}.response.path`);
306
+ if (responsePathProblem !== undefined) {
307
+ problems.push({ path: `${at}.response.path`, problem: responsePathProblem });
308
+ }
309
+ const maxBytesProblem = maxBytesProblemOf(step.response?.maxBytes);
310
+ if (maxBytesProblem !== undefined)
311
+ problems.push({ path: `${at}.response.maxBytes`, problem: maxBytesProblem });
312
+ names.add(step.name);
313
+ });
314
+ return problems;
315
+ }
@@ -0,0 +1,15 @@
1
+ import type { RequestPlan } from '../contract/data.js';
2
+ import { planProblemOf, type PlanProblem } from './data-plan-check.js';
3
+ import { planResultsOf } from './plan-results.js';
4
+ export { planProblemOf, planResultsOf };
5
+ export type { PlanProblem };
6
+ /**
7
+ * 把一份读取计划编译成页面里跑的脚本:一次 eval、多次 fetch,循环与字段提取都在页面内完成。
8
+ * @param plan 已声明的读取计划。
9
+ * @param prior 已经跑过的步骤结果(写计划分步执行时用):给了就不重跑,直接进 `done` 供后面引用。
10
+ * @returns 可直接交给 `browser eval` 的脚本文本。
11
+ */
12
+ export declare function scriptOfPlan(plan: RequestPlan, prior?: Readonly<Record<string, {
13
+ readonly value: unknown;
14
+ }>>): string;
15
+ export { allSame } from './plan-results.js';
@@ -0,0 +1,283 @@
1
+ import { authLossScriptLines } from './auth-loss.js';
2
+ import { PLAN_DEFAULT_LOOP, PLAN_DEFAULT_MAX_BYTES, PLAN_DEFAULT_TIMEOUT_MS, PLAN_MAX_STEPS, planProblemOf, } from './data-plan-check.js';
3
+ import { planResultsOf } from './plan-results.js';
4
+ const DEFAULT_MAX_BYTES = PLAN_DEFAULT_MAX_BYTES;
5
+ const DEFAULT_TIMEOUT_MS = PLAN_DEFAULT_TIMEOUT_MS;
6
+ const MAX_STEPS = PLAN_MAX_STEPS;
7
+ const DEFAULT_LOOP = PLAN_DEFAULT_LOOP;
8
+ export { planProblemOf, planResultsOf };
9
+ function quote(value) {
10
+ return JSON.stringify(value);
11
+ }
12
+ function outputOf(step) {
13
+ const kind = step.response?.kind ?? 'json';
14
+ const maxBytes = step.response?.maxBytes ?? DEFAULT_MAX_BYTES;
15
+ const path = step.response?.path;
16
+ const selector = step.response?.selector;
17
+ const fields = step.response?.fields;
18
+ return `{ kind: ${quote(kind)}, path: ${path === undefined ? 'undefined' : quote(path)}, maxBytes: ${String(maxBytes)}, selector: ${selector === undefined ? 'undefined' : quote(selector)}, fields: ${fields === undefined ? 'undefined' : JSON.stringify(fields)} }`;
19
+ }
20
+ function loopOf(step) {
21
+ if (step.repeat !== undefined) {
22
+ const repeat = {
23
+ over: step.repeat.over,
24
+ as: step.repeat.as ?? 'item',
25
+ max: step.repeat.max ?? DEFAULT_LOOP,
26
+ };
27
+ return `repeat: ${JSON.stringify(repeat)}, while: undefined`;
28
+ }
29
+ if (step.while !== undefined) {
30
+ const whilePlan = { ...step.while, max: step.while.max ?? DEFAULT_LOOP };
31
+ return `repeat: undefined, while: ${JSON.stringify(whilePlan)}`;
32
+ }
33
+ return 'repeat: undefined, while: undefined';
34
+ }
35
+ /**
36
+ * 把一份读取计划编译成页面里跑的脚本:一次 eval、多次 fetch,循环与字段提取都在页面内完成。
37
+ * @param plan 已声明的读取计划。
38
+ * @param prior 已经跑过的步骤结果(写计划分步执行时用):给了就不重跑,直接进 `done` 供后面引用。
39
+ * @returns 可直接交给 `browser eval` 的脚本文本。
40
+ */
41
+ export function scriptOfPlan(plan, prior = {}) {
42
+ const problem = planProblemOf(plan)[0];
43
+ if (problem !== undefined)
44
+ throw new Error(`${problem.path}: ${problem.problem}`);
45
+ const steps = plan.steps
46
+ .map((step) => ` { name: ${quote(step.name)}, method: ${quote(step.request.method)}, path: ${quote(step.request.path)}, ` +
47
+ `query: ${JSON.stringify(step.request.query ?? {})}, headers: ${JSON.stringify(step.request.headers ?? {})}, ` +
48
+ `body: ${JSON.stringify(step.request.body ?? null)}, contentType: ${quote(step.request.contentType ?? 'json')}, ` +
49
+ `response: ${outputOf(step)}, ${loopOf(step)} }`)
50
+ .join(',\n');
51
+ if (plan.origin === undefined || plan.origin === '') {
52
+ throw new Error('这份计划还没解析站点:缺少 origin(只写 site 的计划要先经宿主解析)');
53
+ }
54
+ const origin = new URL(plan.origin).origin;
55
+ const timeoutMs = plan.timeoutMs ?? DEFAULT_TIMEOUT_MS;
56
+ return [
57
+ '(async () => {',
58
+ ` if (location.origin !== ${quote(origin)}) return { ok: false, code: "not-on-site" };`,
59
+ ' const clock = () => new Date().getTime();',
60
+ ' const pick = (value, path) => String(path).split(".").filter((one) => one !== "").reduce((one, key) => (one === null || one === undefined ? one : one[key]), value);',
61
+ ' const subst = (value, done, vars) => {',
62
+ ' if (typeof value === "string") {',
63
+ ' let missing = null;',
64
+ ' const text = value.replace(/\\{\\{\\s*([A-Za-z0-9_]+(?:\\.[A-Za-z0-9_]+)*)\\s*\\}\\}/g, (all, path) => {',
65
+ ' const got = pick(Object.assign({}, done, vars), path);',
66
+ ' if (got === undefined || got === null) { if (missing === null) missing = path; return all; }',
67
+ ' if (typeof got === "object") { if (missing === null) missing = path + "(对象)"; return all; }',
68
+ ' return String(got);',
69
+ ' });',
70
+ ' return { value: text, missing: missing };',
71
+ ' }',
72
+ ' if (Array.isArray(value)) {',
73
+ ' let missing = null;',
74
+ ' const out = value.map((one) => { const got = subst(one, done, vars); if (missing === null) missing = got.missing; return got.value; });',
75
+ ' return { value: out, missing: missing };',
76
+ ' }',
77
+ ' if (value !== null && typeof value === "object") {',
78
+ ' let missing = null;',
79
+ ' const out = {};',
80
+ ' for (const key of Object.keys(value)) { const got = subst(value[key], done, vars); if (missing === null) missing = got.missing; out[key] = got.value; }',
81
+ ' return { value: out, missing: missing };',
82
+ ' }',
83
+ ' return { value: value, missing: null };',
84
+ ' };',
85
+ ' const itemsOf = (value, max) => {',
86
+ ' if (typeof value !== "string") return null;',
87
+ ' const parts = value.split("..");',
88
+ ' if (parts.length !== 2) return null;',
89
+ ' if (!/^\\d+$/.test(parts[0].trim()) || !/^\\d+$/.test(parts[1].trim())) return null;',
90
+ ' const from = Number(parts[0]);',
91
+ ' const to = Number(parts[1]);',
92
+ ' if (!Number.isInteger(from) || !Number.isInteger(to)) return null;',
93
+ ' if (to < from) return [];',
94
+ ' if (to - from + 1 > max) return new Array(max + 1).fill(0);',
95
+ ' const out = [];',
96
+ ' for (let at = from; at <= to; at += 1) out.push(at);',
97
+ ' return out;',
98
+ ' };',
99
+ ...authLossScriptLines(plan.authLoss),
100
+ ` const prior = ${JSON.stringify(prior)};`,
101
+ ' const steps = [',
102
+ steps,
103
+ ' ];',
104
+ ' if (steps.length === 0) return { ok: true, steps: {} };',
105
+ ` if (steps.length > ${String(MAX_STEPS)}) return { ok: false, code: "too-many-steps", message: "一次最多 ${String(MAX_STEPS)} 步" };`,
106
+ ` const deadline = clock() + ${String(timeoutMs)} - 500;`,
107
+ ' const done = {};',
108
+ ' const results = {};',
109
+ ' const fail = (name, code, message, started) => {',
110
+ ' results[name] = { ok: false, code: code, message: message, elapsedMs: clock() - started };',
111
+ ' };',
112
+ ' const runOnce = async (step, vars) => {',
113
+ ' const substituted = subst({ path: step.path, query: step.query, headers: step.headers, body: step.body }, done, vars);',
114
+ ' if (substituted.missing !== null) return { ok: false, code: "ref-missing", message: "引用取不到:" + substituted.missing };',
115
+ ' const request = substituted.value;',
116
+ ' const raw = String(request.path).trim();',
117
+ ' const rel = raw.charAt(0) === "/" ? raw : "/" + raw;',
118
+ ' const url = new URL(location.origin + rel);',
119
+ ' for (const key of Object.keys(request.query)) url.searchParams.set(key, String(request.query[key]));',
120
+ ' const left = deadline - clock();',
121
+ ' if (left <= 0) return { ok: false, code: "timeout", message: "读超时" };',
122
+ ' const init = { method: step.method, credentials: "include", headers: request.headers };',
123
+ ' if (typeof AbortSignal !== "undefined" && typeof AbortSignal.timeout === "function") init.signal = AbortSignal.timeout(Math.max(1, left));',
124
+ ' if (step.method !== "GET" && step.method !== "HEAD") {',
125
+ ' if (step.contentType === "form") {',
126
+ ' const form = new URLSearchParams();',
127
+ ' for (const key of Object.keys(request.body || {})) form.set(key, String(request.body[key]));',
128
+ ' init.body = form.toString();',
129
+ ' init.headers = Object.assign({ "Content-Type": "application/x-www-form-urlencoded; charset=utf-8", "X-Requested-With": "XMLHttpRequest" }, request.headers);',
130
+ ' } else {',
131
+ ' init.body = JSON.stringify(request.body === null ? {} : request.body);',
132
+ ' init.headers = Object.assign({ "Content-Type": "application/json" }, request.headers);',
133
+ ' }',
134
+ ' }',
135
+ ' let response;',
136
+ ' try {',
137
+ ' response = await fetch(url.toString(), init);',
138
+ ' } catch (error) {',
139
+ ' const timeout = error instanceof Error && error.name === "TimeoutError";',
140
+ ' return { ok: false, code: timeout ? "timeout" : "fetch-failed", message: String(error) };',
141
+ ' }',
142
+ ' if (response.status === 401) {',
143
+ ' return { ok: false, code: "not-logged-in", message: "HTTP 401" };',
144
+ ' }',
145
+ ' if (response.status === 403) {',
146
+ ' let body = "";',
147
+ ' try {',
148
+ ' body = (await response.text()).slice(0, 2000);',
149
+ ' } catch (error) {',
150
+ ' body = "";',
151
+ ' }',
152
+ ' if (authLoss !== null && body !== "") {',
153
+ ' let json = null;',
154
+ ' try {',
155
+ ' json = JSON.parse(body);',
156
+ ' } catch (error) {',
157
+ ' json = null;',
158
+ ' }',
159
+ ' const lost = json === null ? null : authLostOf(json);',
160
+ ' if (lost !== null) return { ok: false, code: "not-logged-in", message: lost };',
161
+ ' }',
162
+ ' return { ok: false, code: "forbidden", message: "HTTP 403 " + body.slice(0, 200) };',
163
+ ' }',
164
+ ' if (response.redirected && String(response.url).indexOf("/login") >= 0) {',
165
+ ' return { ok: false, code: "not-logged-in", message: "跳到了登录页" };',
166
+ ' }',
167
+ ' if (!response.ok) return { ok: false, code: "http", message: "HTTP " + response.status };',
168
+ ' try {',
169
+ ' if (step.response.kind === "bytes") {',
170
+ ' const blob = await response.blob();',
171
+ ' if (blob.size > step.response.maxBytes) {',
172
+ ' return { ok: false, code: "too-large", message: "响应超过 " + step.response.maxBytes + " 字节" };',
173
+ ' }',
174
+ ' const buffer = new Uint8Array(await blob.arrayBuffer());',
175
+ ' let text = "";',
176
+ ' for (let at = 0; at < buffer.length; at += 8192) {',
177
+ ' text += String.fromCharCode.apply(null, buffer.subarray(at, at + 8192));',
178
+ ' }',
179
+ ' return { ok: true, value: { base64: btoa(text), size: blob.size, contentType: blob.type || "" }, raw: null };',
180
+ ' }',
181
+ ' const text = await response.text();',
182
+ ' const bytes = typeof TextEncoder === "undefined" ? text.length : new TextEncoder().encode(text).length;',
183
+ ' if (bytes > step.response.maxBytes) {',
184
+ ' return { ok: false, code: "too-large", message: "响应超过 " + step.response.maxBytes + " 字节" };',
185
+ ' }',
186
+ ' if (step.response.kind === "text") return { ok: true, value: text, raw: text };',
187
+ ' if (step.response.kind === "html") {',
188
+ ' const doc = new DOMParser().parseFromString(text, "text/html");',
189
+ ' const nodes = Array.from(doc.querySelectorAll(step.response.selector));',
190
+ ' const value = nodes.map((node) => {',
191
+ ' if (step.response.fields === undefined) return { text: String(node.textContent || "").trim(), html: node.innerHTML };',
192
+ ' const row = {};',
193
+ ' for (const name of Object.keys(step.response.fields)) {',
194
+ ' const found = node.querySelector(step.response.fields[name]);',
195
+ ' row[name] = found === null ? null : String(found.textContent || "").trim();',
196
+ ' }',
197
+ ' return row;',
198
+ ' });',
199
+ ' return { ok: true, value: value, raw: value };',
200
+ ' }',
201
+ ' if (text.trim() === "") return { ok: true, value: null, raw: null };',
202
+ ' let json;',
203
+ ' try {',
204
+ ' json = JSON.parse(text);',
205
+ ' } catch (error) {',
206
+ ' return { ok: false, code: "shape", message: "这一跳不是 JSON" };',
207
+ ' }',
208
+ ' const lost = authLostOf(json);',
209
+ ' if (lost !== null) return { ok: false, code: "not-logged-in", message: lost };',
210
+ ' let value = json;',
211
+ ' if (step.response.path !== undefined) {',
212
+ ' value = pick(json, step.response.path);',
213
+ ' if (value === undefined) return { ok: false, code: "path-missing", message: "返回里没有 " + step.response.path + " 这一格" };',
214
+ ' }',
215
+ ' return { ok: true, value: value, raw: json };',
216
+ ' } catch (error) {',
217
+ ' const timeout = error instanceof Error && error.name === "TimeoutError";',
218
+ ' return { ok: false, code: timeout ? "timeout" : "parse", message: String(error) };',
219
+ ' }',
220
+ ' };',
221
+ ' for (const step of steps) {',
222
+ ' const started = clock();',
223
+ ' if (Object.prototype.hasOwnProperty.call(prior, step.name)) {',
224
+ ' const hit = prior[step.name];',
225
+ ' done[step.name] = hit.value;',
226
+ ' results[step.name] = { ok: true, value: hit.value, elapsedMs: 0 };',
227
+ ' continue;',
228
+ ' }',
229
+ ' if (clock() >= deadline) { fail(step.name, "timeout", "读超时", started); break; }',
230
+ ' if (step.while !== undefined) {',
231
+ ' const out = [];',
232
+ ' let broken = false;',
233
+ ' for (let page = 1; page <= step.while.max; page += 1) {',
234
+ ' if (clock() >= deadline) { fail(step.name, "timeout", "读超时", started); broken = true; break; }',
235
+ ' const got = await runOnce(step, { page: page, index: page - 1 });',
236
+ ' if (!got.ok) { fail(step.name, got.code, got.message, started); broken = true; break; }',
237
+ ' out.push(got.value);',
238
+ ' if (pick(got.raw, step.while.path) === step.while.equals) {',
239
+ ' done[step.name] = out;',
240
+ ' results[step.name] = { ok: true, value: out, elapsedMs: clock() - started };',
241
+ ' break;',
242
+ ' }',
243
+ ' if (page === step.while.max) {',
244
+ ' fail(step.name, "too-many-iterations", "循环 " + step.while.max + " 次还没到终止条件", started);',
245
+ ' broken = true;',
246
+ ' }',
247
+ ' }',
248
+ ' if (broken) break;',
249
+ ' continue;',
250
+ ' }',
251
+ ' if (step.repeat !== undefined) {',
252
+ ' const over = subst(step.repeat.over, done, {});',
253
+ ' if (over.missing !== null) { fail(step.name, "ref-missing", "引用取不到:" + over.missing, started); break; }',
254
+ ' const items = itemsOf(over.value, step.repeat.max);',
255
+ ' if (items === null) { fail(step.name, "repeat-invalid", "repeat.over 要是数字区间(如 1..12)", started); break; }',
256
+ ' if (items.length > step.repeat.max) { fail(step.name, "too-many-iterations", "repeat 超过 " + step.repeat.max + " 项", started); break; }',
257
+ ' const out = [];',
258
+ ' let broken = false;',
259
+ ' for (let index = 0; index < items.length; index += 1) {',
260
+ ' const vars = {};',
261
+ ' vars[step.repeat.as] = items[index];',
262
+ ' vars.index = index;',
263
+ ' vars.page = index + 1;',
264
+ ' if (clock() >= deadline) { fail(step.name, "timeout", "读超时", started); broken = true; break; }',
265
+ ' const got = await runOnce(step, vars);',
266
+ ' if (!got.ok) { fail(step.name, got.code, got.message, started); broken = true; break; }',
267
+ ' out.push(got.value);',
268
+ ' }',
269
+ ' if (broken) break;',
270
+ ' done[step.name] = out;',
271
+ ' results[step.name] = { ok: true, value: out, elapsedMs: clock() - started };',
272
+ ' continue;',
273
+ ' }',
274
+ ' const got = await runOnce(step, {});',
275
+ ' if (!got.ok) { fail(step.name, got.code, got.message, started); break; }',
276
+ ' done[step.name] = got.value;',
277
+ ' results[step.name] = { ok: true, value: got.value, elapsedMs: clock() - started };',
278
+ ' }',
279
+ ' return { ok: true, steps: results };',
280
+ '})()',
281
+ ].join('\n');
282
+ }
283
+ export { allSame } from './plan-results.js';
@@ -0,0 +1,5 @@
1
+ export declare function dshHome(): string;
2
+ export declare function workbenchRoot(): string;
3
+ export declare function workbenchHostLock(): string;
4
+ export declare function workbenchSelfUpdateMarker(): string;
5
+ export declare function workspacesRoot(): string;