@thincoder/core 0.10.13 → 0.11.2
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/CHANGELOG.md +74 -1
- package/advisor/convergence.mjs +34 -3
- package/advisor/loop.mjs +42 -10
- package/advisor/messages.mjs +46 -27
- package/advisor/notice.mjs +1 -1
- package/advisor/project-context.mjs +253 -14
- package/advisor/run.mjs +7 -4
- package/advisor.mjs +29 -43
- package/agent/batch-attribution.mjs +87 -0
- package/agent/batch-changed-surface.mjs +129 -0
- package/agent/completion.mjs +57 -4
- package/agent/digest-account.mjs +73 -46
- package/agent/dispatch-gates.mjs +6 -0
- package/agent/dispatch-run.mjs +1 -1
- package/agent/dispatch.mjs +35 -22
- package/agent/family-tools.mjs +36 -17
- package/agent/helpers.mjs +123 -95
- package/agent/offload.mjs +84 -0
- package/agent/post-turn.mjs +24 -10
- package/agent/record-results.mjs +19 -7
- package/agent/run-stages.mjs +127 -61
- package/agent/run-start.mjs +45 -8
- package/agent/setup-reminders.mjs +9 -2
- package/agent/setup.mjs +27 -22
- package/agent/spawn-child.mjs +38 -1
- package/agent/suspension.mjs +1 -1
- package/agent/timers.mjs +5 -3
- package/agent/turn-loop.mjs +60 -6
- package/agent/turn-projection.mjs +386 -0
- package/agent/turn-task-state.mjs +274 -0
- package/agent/turn-tool-process.mjs +168 -0
- package/agent/write-face.mjs +99 -0
- package/agent/write-gate-ledger.mjs +569 -0
- package/agent/write-gate.mjs +39 -13
- package/agent-tools/advisor-async.mjs +29 -169
- package/agent-tools/advisor-launch.mjs +178 -0
- package/agent-tools/advisor-settle.mjs +28 -1
- package/agent-tools/advisor.mjs +136 -31
- package/agent-tools/async-settle.mjs +2 -2
- package/agent-tools/audit-block.mjs +6 -6
- package/agent-tools/bash-async.mjs +8 -2
- package/agent-tools/batch-closeout.mjs +444 -0
- package/agent-tools/batch-gate.mjs +152 -138
- package/agent-tools/batch-ledger-bind.mjs +392 -0
- package/agent-tools/batch-ledger-link.mjs +73 -0
- package/agent-tools/batch-ledger-view.mjs +109 -0
- package/agent-tools/batch-lifecycle.mjs +245 -376
- package/agent-tools/batch-paths.mjs +73 -47
- package/agent-tools/batch-read.mjs +1 -1
- package/agent-tools/batch-record-write.mjs +417 -0
- package/agent-tools/batch-regions.mjs +215 -0
- package/agent-tools/batch-reopen.mjs +154 -0
- package/agent-tools/batch-segment-vocab.mjs +379 -0
- package/agent-tools/batch-skeleton.mjs +344 -196
- package/agent-tools/batch-supersede.mjs +232 -0
- package/agent-tools/batch-tier-gate.mjs +297 -0
- package/agent-tools/batch.mjs +189 -253
- package/agent-tools/consult.mjs +7 -4
- package/agent-tools/context.mjs +47 -17
- package/agent-tools/design-review-record.mjs +225 -0
- package/agent-tools/design-slots.mjs +24 -24
- package/agent-tools/design-token.mjs +318 -39
- package/agent-tools/doc-check.mjs +55 -29
- package/agent-tools/doc-graph.mjs +140 -0
- package/agent-tools/eng.mjs +10 -9
- package/agent-tools/escalate-async.mjs +5 -5
- package/agent-tools/manifest.mjs +34 -15
- package/agent-tools/read-history.mjs +115 -14
- package/agent-tools/review-facts.mjs +40 -15
- package/agent-tools/settings.mjs +23 -2
- package/agent-tools/spawn-gates.mjs +19 -22
- package/agent-tools/ssh-session.mjs +120 -111
- package/agent-tools/ssh-text.mjs +155 -0
- package/agent-tools/ssh-transfer.mjs +210 -0
- package/agent-tools/ssh-transport.mjs +183 -81
- package/agent-tools/subagent-actions-query.mjs +11 -7
- package/agent-tools/subagent-actions.mjs +1 -1
- package/agent-tools/subagent-async.mjs +11 -11
- package/agent-tools/subagent-brief.mjs +76 -0
- package/agent-tools/subagent-design-token.mjs +168 -0
- package/agent-tools/subagent-run.mjs +1 -1
- package/agent-tools/subagent-scheduler.mjs +45 -11
- package/agent-tools/subagent-spawn.mjs +143 -104
- package/agent-tools/subagent.mjs +26 -23
- package/agent-tools/taskbook-block.mjs +94 -0
- package/agent-tools/timer.mjs +12 -8
- package/agent-tools/verify.mjs +13 -4
- package/agent-tools.mjs +7 -3
- package/agent.mjs +20 -1
- package/attachments.mjs +3 -3
- package/browser/actions.mjs +9 -3
- package/browser/cdp.mjs +45 -17
- package/browser/launch.mjs +25 -43
- package/browser/pool.mjs +299 -0
- package/browser/queue.mjs +2 -0
- package/browser/session-state.mjs +450 -0
- package/browser/session.mjs +127 -151
- package/browser/snapshot.mjs +89 -0
- package/client-identity.mjs +22 -6
- package/config-defaults.mjs +2 -1
- package/config-migrate.mjs +40 -0
- package/config-normalize.mjs +28 -2
- package/config-presets.mjs +5 -3
- package/config-reload.mjs +59 -0
- package/config.mjs +62 -98
- package/context-degrade.mjs +38 -10
- package/context-echo.mjs +11 -0
- package/context.mjs +165 -68
- package/conventions.mjs +11 -9
- package/declaration.mjs +33 -7
- package/declared-repos.mjs +94 -0
- package/doc-check/doc-names.mjs +73 -0
- package/doc-check/doc-tokens.mjs +110 -0
- package/doc-check/existence.mjs +160 -0
- package/doc-check/index.mjs +312 -163
- package/doc-check/line-refs.mjs +38 -0
- package/doc-check/paragraph.mjs +220 -91
- package/doc-graph/anchors.mjs +474 -0
- package/doc-graph/doc-batch-face.mjs +59 -0
- package/doc-graph/doc-suffix.mjs +27 -0
- package/doc-graph/graph-tokens.mjs +160 -0
- package/doc-graph.mjs +778 -0
- package/explore-distill.mjs +12 -6
- package/i18n.mjs +2 -2
- package/index-discover.mjs +4 -1
- package/instance-inbox-watch.mjs +272 -0
- package/instance-inbox.mjs +218 -0
- package/instance-registry.mjs +179 -0
- package/knowledge/README.md +3 -0
- package/knowledge/prompt-resources.md +6 -0
- package/ledger-close-proof.mjs +95 -62
- package/ledger-close.mjs +188 -0
- package/ledger-cmd.mjs +76 -244
- package/ledger-consistency.mjs +113 -0
- package/ledger-db.mjs +70 -37
- package/ledger-executors.mjs +1 -1
- package/ledger-migrate.mjs +4 -4
- package/ledger-read.mjs +10 -8
- package/ledger-surface.mjs +1 -1
- package/ledger-tools.mjs +83 -24
- package/ledger-variant-notice.mjs +1 -1
- package/ledger-write-ops.mjs +462 -0
- package/ledger.mjs +2 -2
- package/manifest-discovery.mjs +5 -3
- package/manifest-schema.mjs +28 -2
- package/memory/memory-tool.mjs +5 -2
- package/memory/schema.mjs +6 -27
- package/package.json +3 -1
- package/path-resolve.mjs +498 -0
- package/permission.mjs +4 -0
- package/prompt-files.mjs +91 -1
- package/prompt-overlays.mjs +14 -6
- package/prompts/advisor-design.md +66 -22
- package/prompts/advisor-round1.md +37 -3
- package/prompts/advisor-round2.md +44 -10
- package/prompts/advisor-round3.md +43 -9
- package/prompts/common.md +15 -375
- package/prompts/consult-base.md +6 -1
- package/prompts/discipline-engineering.md +251 -95
- package/prompts/discipline-normal.md +4 -250
- package/prompts/knowledge/business-gates.md +22 -0
- package/prompts/knowledge/decision-quality.md +23 -0
- package/prompts/knowledge/doc-lookup.md +35 -0
- package/prompts/knowledge/document-writing.md +51 -0
- package/prompts/knowledge/documentation-evaluation.md +56 -0
- package/prompts/knowledge/evidence-discipline.md +57 -0
- package/prompts/knowledge/normal-coding-workflow.md +104 -0
- package/prompts/knowledge/normal-delivery-collaboration.md +108 -0
- package/prompts/knowledge/normal-work-management.md +57 -0
- package/prompts/knowledge/product-security.md +23 -0
- package/prompts/knowledge/project-records.md +62 -0
- package/prompts/knowledge/tool-advisor.md +29 -0
- package/prompts/knowledge/tool-apply-patch.md +18 -0
- package/prompts/knowledge/tool-bash.md +39 -0
- package/prompts/knowledge/tool-batch-eng-coder.md +12 -0
- package/prompts/knowledge/tool-batch-eng-designer.md +12 -0
- package/prompts/knowledge/tool-batch-review.md +12 -0
- package/prompts/knowledge/tool-batch.md +80 -0
- package/prompts/knowledge/tool-browser.md +57 -0
- package/prompts/knowledge/tool-code-search.md +4 -0
- package/prompts/knowledge/tool-consult-start.md +12 -0
- package/prompts/knowledge/tool-consult-stop.md +6 -0
- package/prompts/knowledge/tool-context.md +6 -0
- package/prompts/knowledge/tool-delete.md +17 -0
- package/prompts/knowledge/tool-doc-check.md +70 -0
- package/prompts/knowledge/tool-doc-graph-read.md +20 -0
- package/prompts/knowledge/tool-doc-graph.md +49 -0
- package/prompts/knowledge/tool-doc-search.md +6 -0
- package/prompts/knowledge/tool-edit.md +38 -0
- package/prompts/knowledge/tool-eng.md +2 -0
- package/prompts/knowledge/tool-execute.md +16 -0
- package/prompts/knowledge/tool-fetch.md +12 -0
- package/prompts/knowledge/tool-file-ops.md +16 -0
- package/prompts/knowledge/tool-get-current-time.md +7 -0
- package/prompts/knowledge/tool-git-read.md +19 -0
- package/prompts/knowledge/tool-git.md +29 -0
- package/prompts/knowledge/tool-glob.md +15 -0
- package/prompts/knowledge/tool-goal.md +8 -0
- package/prompts/knowledge/tool-grep.md +18 -0
- package/prompts/knowledge/tool-hashline-edit.md +14 -0
- package/prompts/knowledge/tool-insert-after.md +15 -0
- package/prompts/knowledge/tool-instance-list.md +6 -0
- package/prompts/knowledge/tool-instance-send.md +10 -0
- package/prompts/knowledge/tool-knowledge.md +18 -0
- package/prompts/knowledge/tool-ledger-read.md +35 -0
- package/prompts/knowledge/tool-ledger.md +42 -0
- package/prompts/knowledge/tool-lint.md +16 -0
- package/prompts/knowledge/tool-ls.md +13 -0
- package/prompts/knowledge/tool-lsp.md +11 -0
- package/prompts/knowledge/tool-main-history.md +2 -0
- package/prompts/knowledge/tool-manifest-read.md +14 -0
- package/prompts/knowledge/tool-manifest.md +31 -0
- package/prompts/knowledge/tool-memory-read.md +20 -0
- package/prompts/knowledge/tool-memory.md +15 -0
- package/prompts/knowledge/tool-notify-parent.md +12 -0
- package/prompts/knowledge/tool-operations.md +114 -0
- package/prompts/knowledge/tool-peer-instances.md +2 -0
- package/prompts/knowledge/tool-plan.md +6 -0
- package/prompts/knowledge/tool-process-read.md +13 -0
- package/prompts/knowledge/tool-process.md +15 -0
- package/prompts/knowledge/tool-question.md +16 -0
- package/prompts/knowledge/tool-read-history-batch.md +22 -0
- package/prompts/knowledge/tool-read-history.md +36 -0
- package/prompts/knowledge/tool-read-image.md +11 -0
- package/prompts/knowledge/tool-read.md +21 -0
- package/prompts/knowledge/tool-recent-changes.md +6 -0
- package/prompts/knowledge/tool-repo-outline.md +4 -0
- package/prompts/knowledge/tool-settings-read.md +12 -0
- package/prompts/knowledge/tool-settings.md +18 -0
- package/prompts/knowledge/tool-skill.md +6 -0
- package/prompts/knowledge/tool-ssh.md +52 -0
- package/prompts/knowledge/tool-subagent.md +59 -0
- package/prompts/knowledge/tool-task.md +12 -0
- package/prompts/knowledge/tool-team-ledger-read.md +11 -0
- package/prompts/knowledge/tool-team-ledger.md +11 -0
- package/prompts/knowledge/tool-timer.md +23 -0
- package/prompts/knowledge/tool-tree.md +14 -0
- package/prompts/knowledge/tool-verify.md +19 -0
- package/prompts/knowledge/tool-wait-for.md +25 -0
- package/prompts/knowledge/tool-websearch.md +19 -0
- package/prompts/knowledge/tool-write.md +9 -0
- package/prompts/knowledge/upstream-protocol.md +43 -0
- package/prompts/persona-eng-coder.md +29 -3
- package/prompts/persona-eng-designer.md +131 -159
- package/prompts/persona-engineering.md +202 -131
- package/prompts/persona-explore.md +1 -1
- package/prompts/persona-plan.md +6 -1
- package/prompts/persona-tester.md +55 -0
- package/prompts/subagent-base.md +2 -0
- package/provider/anthropic.mjs +6 -4
- package/provider/core.mjs +5 -4
- package/provider/errors.mjs +29 -4
- package/provider/google.mjs +39 -12
- package/provider/rate.mjs +11 -3
- package/provider/responses.mjs +3 -1
- package/provider/sse.mjs +15 -4
- package/provider-flows.mjs +2 -2
- package/session-identity.mjs +21 -0
- package/session-index-build.mjs +1 -1
- package/session-index-pass.mjs +1 -1
- package/session-index-query.mjs +1 -1
- package/session-index.mjs +1 -1
- package/session-lifecycle.mjs +79 -19
- package/session-slot-write.mjs +37 -5
- package/session-slots.mjs +9 -14
- package/session.mjs +29 -8
- package/skip-dirs.mjs +48 -0
- package/team-ledger.mjs +17 -13
- package/text-budget.mjs +4 -0
- package/token-ttl.mjs +73 -4
- package/token-window.mjs +244 -34
- package/tool-docs/advisor.md +2 -27
- package/tool-docs/apply_patch.md +1 -13
- package/tool-docs/bash.md +2 -37
- package/tool-docs/batch-eng-coder.md +3 -0
- package/tool-docs/batch-eng-designer.md +3 -0
- package/tool-docs/batch-review.md +3 -0
- package/tool-docs/batch.md +3 -1
- package/tool-docs/browser.md +2 -47
- package/tool-docs/code_search.md +3 -1
- package/tool-docs/consult_start.md +2 -4
- package/tool-docs/consult_stop.md +2 -2
- package/tool-docs/context.md +1 -5
- package/tool-docs/delete.md +2 -14
- package/tool-docs/doc_check.md +2 -72
- package/tool-docs/doc_graph-read.md +3 -0
- package/tool-docs/doc_graph.md +3 -0
- package/tool-docs/doc_search.md +3 -1
- package/tool-docs/edit.md +2 -18
- package/tool-docs/eng.md +3 -1
- package/tool-docs/execute.md +2 -12
- package/tool-docs/fetch.md +2 -12
- package/tool-docs/file_ops.md +2 -16
- package/tool-docs/get_current_time.md +1 -6
- package/tool-docs/git-read.md +3 -0
- package/tool-docs/git.md +2 -27
- package/tool-docs/glob.md +2 -10
- package/tool-docs/goal.md +2 -5
- package/tool-docs/grep.md +2 -18
- package/tool-docs/hashline_edit.md +2 -13
- package/tool-docs/insert_after.md +2 -14
- package/tool-docs/instance_list.md +3 -0
- package/tool-docs/instance_send.md +3 -0
- package/tool-docs/knowledge.md +3 -0
- package/tool-docs/ledger-read.md +3 -0
- package/tool-docs/ledger.md +3 -8
- package/tool-docs/lint.md +2 -9
- package/tool-docs/ls.md +2 -11
- package/tool-docs/lsp.md +2 -9
- package/tool-docs/main_history.md +3 -1
- package/tool-docs/manifest-read.md +2 -7
- package/tool-docs/manifest.md +2 -28
- package/tool-docs/memory-read.md +3 -0
- package/tool-docs/memory.md +3 -11
- package/tool-docs/notify_parent.md +2 -13
- package/tool-docs/peer_instances.md +3 -1
- package/tool-docs/plan.md +2 -4
- package/tool-docs/process-read.md +3 -0
- package/tool-docs/process.md +2 -13
- package/tool-docs/question.md +3 -17
- package/tool-docs/read.md +2 -19
- package/tool-docs/read_history-batch.md +3 -0
- package/tool-docs/read_history.md +2 -6
- package/tool-docs/read_image.md +2 -7
- package/tool-docs/recent_changes.md +3 -1
- package/tool-docs/repo_outline.md +3 -1
- package/tool-docs/settings-read.md +3 -0
- package/tool-docs/settings.md +2 -5
- package/tool-docs/skill.md +3 -5
- package/tool-docs/ssh.md +2 -50
- package/tool-docs/subagent.md +2 -63
- package/tool-docs/task.md +2 -6
- package/tool-docs/team_ledger-read.md +3 -0
- package/tool-docs/team_ledger.md +3 -9
- package/tool-docs/timer.md +3 -13
- package/tool-docs/tree.md +2 -13
- package/tool-docs/verify.md +3 -1
- package/tool-docs/wait_for.md +2 -32
- package/tool-docs/websearch.md +2 -17
- package/tool-docs/write.md +2 -10
- package/tools/bash.mjs +12 -2
- package/tools/browser.mjs +52 -7
- package/tools/doc-write-gate.mjs +73 -0
- package/tools/edit-batch.mjs +176 -91
- package/tools/edit-diff.mjs +129 -45
- package/tools/edit-receipt.mjs +39 -0
- package/tools/execute.mjs +11 -5
- package/tools/file.mjs +32 -34
- package/tools/git.mjs +43 -2
- package/tools/index.mjs +6 -0
- package/tools/knowledge.mjs +143 -0
- package/tools/linter.mjs +3 -3
- package/tools/ops.mjs +4 -1
- package/tools/patch.mjs +127 -38
- package/tools/process-doc-mark.mjs +139 -0
- package/tools/search.mjs +15 -6
- package/tools/shared.mjs +31 -7
- package/tools/ssh.mjs +48 -7
- package/tools/tree.mjs +4 -1
- package/tools/write-gate-params.mjs +31 -0
- package/tools/write-path.mjs +79 -6
- package/traces/trace-store.mjs +4 -3
- package/window-policy.mjs +203 -0
- package/agent/light-round.mjs +0 -63
- package/agent/unbooked-pen.mjs +0 -62
- package/doc-check/anchors.mjs +0 -135
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* browser/session-state.mjs — 会话态动作(`cookies` ∥ `storage` 各三形)∥ 域过滤 ∥ 写后回读核对 ∥ 回执文法
|
|
3
|
+
* (设计权威 = `docs/core/design/BROWSER-TOOL.md` §2.2 两行 ∥ §3.4 会话态面 ∥ §6 KD-27 / KD-28 / KD-29 / KD-30)。
|
|
4
|
+
*
|
|
5
|
+
* 两条路的语义差(KD-27):cookie 是**浏览器级**存储(全 profile 一份)⇒ 给什么 `domain` 写什么域,跨域直写成立;
|
|
6
|
+
* storage 只有页内 `window.localStorage` / `window.sessionStorage` 一条路(无 CDP 专用域)⇒ 只作用于当前页 origin,
|
|
7
|
+
* 写别的源要先 `navigate`(浏览器存储本身的界,不是保守选择)。
|
|
8
|
+
* 取面(KD-29):三形全走**页面会话**的 `Network` 域——`get` = `Network.getAllCookies`(全 profile,含 httpOnly;
|
|
9
|
+
* 页级 `getCookies` 只回当前页域条目,而需求要「当前会话可见的全部」)· `set` = `Network.setCookie` 逐条 ·
|
|
10
|
+
* `clear` 有 `domain` 逐条 `Network.deleteCookies`、无 `domain` `Network.clearBrowserCookies`。
|
|
11
|
+
* **不设取面回落**:实测(本机 Edge 147.0.3912.60)浏览器级连接上只有 Storage 域,`Network.*` 全报 `-32601`;
|
|
12
|
+
* 真实分野是连接级别,回落分支护的是一个不存在的场景。
|
|
13
|
+
* 成功判据(KD-30):`Network.setCookie` 的 `success` **不是**有效性判据(`sameSite:"Bogus"` 与
|
|
14
|
+
* `domain:"https://nope"` 都回 `success:true`,后者被存成域 `https`)⇒ 客户端先拦非法形,再按 `name`+`domain`
|
|
15
|
+
* 回读核对:**逐字比 `value`**(请求给了 `path` 的再核 `path`)——只核 `name`+`domain` 时同名同域覆盖写会拿
|
|
16
|
+
* 旧值假通过,而 AC-BT6 要的恰是「回执值与写入值逐字相等」;未落地 ∥ 值是旧值 ⇒ 明示错并点名存储里的实际
|
|
17
|
+
* 内容(不谎报成功)。
|
|
18
|
+
* 域匹配(§2.2「全等 ∥ 子域」):落在**主机边界**上——纯字符串后缀会把 `127.0.0.1` 认成 `0.0.1` 的子域,
|
|
19
|
+
* 而带域 `clear` 是破坏性动作,认错一次就删掉别的主机条目。IP 字面量没有子域 ⇒ 只走全等。
|
|
20
|
+
* 免审(N-BT7 ∥ 用户 2026-11-06 裁):分类在 `tools/browser.mjs` 的 `isReadonlyAction`——本档不碰门面。
|
|
21
|
+
* 回执值照出、**不打码**(AC-BT6):cookie 逐条一行、标记尾置(`; httpOnly` 等)——这一列正是 AC-BT1 的对照面。
|
|
22
|
+
* 形检(§2.2 的形判规则,一条判法管**全部**声明了类型的参数):**键不存在 = 没给**(落缺省或当前页源);
|
|
23
|
+
* **给了就必须是声明类型且形合法**,否则显式拒——`null` 也算「给了但形不对」,不是「没给」。没有第二个判法,
|
|
24
|
+
* 不设「这个字段宽松一点」的例外:逐字段的句子清单下一个字段加进来就会漏(本批的 `httpOnly` ∥ `secure` ∥
|
|
25
|
+
* `sameSite` ∥ `expires` ∥ `path` 就是这么漏掉的——`httpOnly:"true"` 被静默收成 `false`,得到一条不是 httpOnly
|
|
26
|
+
* 的 cookie 加一张成功回执,而 AC-BT1 的主题正是 httpOnly)。
|
|
27
|
+
* 「当作没给」的后果不是无害的:`domain` 非法 ⇒ `clear` 升级成全域清空 ∥ `key` 非法 ⇒ `store.clear()` 清整份
|
|
28
|
+
* store;逐项 `cookies[].domain` 与顶层同一判法(`null` ∥ 非串 ∥ 空串 ∥ 带端口都拒)。`cookies set` 未给 `domain`
|
|
29
|
+
* 且页面无 http(s) origin ⇒ 明示拒,不落到浏览器的原始 CDP 错。`area` 不另立形检句:它本就有值域句
|
|
30
|
+
* (枚举二选一)⇒ 给了 `null` ∥ 非字符串 ∥ 空串一律落 `unknown storage area "<x>"`(`null` 渲染成 `"null"`),
|
|
31
|
+
* 不静默落缺省(缺省只在「键不存在」时取)。
|
|
32
|
+
* 读不出不得顶替空集合(两类面同一条):`Network.getAllCookies` 不回数组 ∥ storage 全量面的页侧回执缺
|
|
33
|
+
* `entries` ⇒ 明示错,不回 `0 cookie(s)` ∥ `0 key(s)` ∥ `0 removed`——那是一个读失败伪装成的事实。
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
const mark = (kind) => `/*thincoder-browser:${kind}*/`
|
|
37
|
+
|
|
38
|
+
export const COOKIE_OPS = ["get", "set", "clear"]
|
|
39
|
+
export const STORAGE_OPS = ["get", "set", "clear"]
|
|
40
|
+
export const STORAGE_AREAS = ["localStorage", "sessionStorage"]
|
|
41
|
+
export const SAME_SITE_VALUES = ["Strict", "Lax", "None"]
|
|
42
|
+
/** 回执正文上限(N-BT3 族——同 `evaluate` / `clipboard read` 的 8000 字符)。 */
|
|
43
|
+
export const SESSION_STATE_MAX_CHARS = 8_000
|
|
44
|
+
|
|
45
|
+
/** 域的比对形(cookie 域带前导点 ∥ 大小写不敏感——协议里 `Domain` 的写法与 `getAllCookies` 的回读形不同)。 */
|
|
46
|
+
const bare = (domain) => String(domain ?? "").replace(/^\./, "").toLowerCase()
|
|
47
|
+
|
|
48
|
+
/** 主机是不是 IP 字面量(IPv4 四点形 ∥ 带冒号的 IPv6):IP 没有子域——`0.0.1` 不是 `127.0.0.1` 的父域。 */
|
|
49
|
+
function isIpLiteral(host) {
|
|
50
|
+
if (host.includes(":")) return true
|
|
51
|
+
return /^\d{1,3}(?:\.\d{1,3}){3}$/.test(host)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 域匹配(`get` 过滤 ∥ `clear` 作用域):全等 ∥ 子域(cookie 的域本就含子域——`Domain=.example.test` 覆盖其下全部)。
|
|
55
|
+
* 子域只在两边都是主机名时成立:`endsWith("." + w)` 保证落在标签边界上,但边界还不够——`127.0.0.1` 也以
|
|
56
|
+
* `.0.0.1` 结尾;IP 字面量没有子域关系,故两侧任一为 IP 就只剩全等。否则带域 `clear` 会删掉别的主机条目。 */
|
|
57
|
+
function domainMatches(cookieDomain, wanted) {
|
|
58
|
+
const c = bare(cookieDomain)
|
|
59
|
+
const w = bare(wanted)
|
|
60
|
+
if (c === w) return true
|
|
61
|
+
if (isIpLiteral(c) || isIpLiteral(w)) return false
|
|
62
|
+
return c.endsWith(`.${w}`)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function hostOf(url) {
|
|
66
|
+
try { return new URL(String(url)).hostname } catch { return "" }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function unknownCookieOp(op) {
|
|
70
|
+
return `unknown cookies op "${op}" — ops: ${COOKIE_OPS.join(", ")}`
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function unknownStorageOp(op) {
|
|
74
|
+
return `unknown storage op "${op}" — ops: ${STORAGE_OPS.join(", ")}`
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function unknownStorageArea(area) {
|
|
78
|
+
return `unknown storage area "${area}" — areas: ${STORAGE_AREAS.join(", ")}`
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** `sameSite` 归一(大小写不敏感 ⇒ 协议枚举名);不在册 ⇒ null。值域 = 声明类型(string)+ 枚举二选一:
|
|
82
|
+
* **非字符串一律 null**——判形不靠 stringify(`["Lax"]` 会 stringify 成合法枚举名,那样「非字符串 ⇒ 拒」
|
|
83
|
+
* 就落空;兄弟参数 `area` 已经为此收过一手);**不 trim**——设计只给了「大小写不敏感」这一条归一,
|
|
84
|
+
* `" Lax "` 不在枚举里,多一条就是没写进设计的宽松面。 */
|
|
85
|
+
export function normalizeSameSite(value) {
|
|
86
|
+
if (typeof value !== "string") return null
|
|
87
|
+
const raw = value.toLowerCase()
|
|
88
|
+
return SAME_SITE_VALUES.find((v) => v.toLowerCase() === raw) ?? null
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 主机串带端口(`<host>:<port>`):判法 = **只含一个冒号且冒号后是纯数字**。IPv6 字面量本身有多个冒号(`::1`),
|
|
92
|
+
* 按「含冒号」一刀切会把「IP 字面量只走全等」那条的合法形一起拒掉;而 `<host>:<port>` 恰是单冒号 + 纯数字尾巴。 */
|
|
93
|
+
function hostWithPort(host) {
|
|
94
|
+
const parts = host.split(":")
|
|
95
|
+
return parts.length === 2 && /^\d+$/.test(parts[1])
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** 裸主机名检(§2.2:含 `://` ∥ 含 `/` ∥ 含空白 ∥ 带端口 ⇒ 拒)。浏览器不拒这类输入——它把 `domain:"https://nope"`
|
|
99
|
+
* 存成域 `https`(KD-30 的实测来历),所以这一拦只能由客户端做。端口那一条的理由是**静默零比报错坏**:cookie
|
|
100
|
+
* 的域只记主机、端口从不入域,而 `localhost:3000` 是本地 dev 抄域名最自然的写法——放行它,`get` ∥ `clear`
|
|
101
|
+
* 都在零匹配上安静地回 `0 cookie(s)` ∥ `0 removed`,读起来像「那个域本来就没有」。 */
|
|
102
|
+
function badBareHost(domain) {
|
|
103
|
+
if (typeof domain !== "string") return true
|
|
104
|
+
return domain.trim() === "" || /:\/\//.test(domain) || domain.includes("/") || /\s/.test(domain) || hostWithPort(domain)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** `domain` 形检(§2.2 · U86 ∥ U93):给了但不是**非空裸主机串**(非串 ∥ 空串 ∥ 带 scheme ∥ 带 path ∥ 含空白
|
|
108
|
+
* ∥ 带端口)⇒ 明示拒。顶层与逐项**同一个判法**(`badBareHost` 单源):只拦「空串 ∥ 非串」的话,顶层这一支会把
|
|
109
|
+
* `domain:"https://nope"` 静默落成一次空过滤——`[cookies clear] 0 removed (domain "https://nope")`
|
|
110
|
+
* 读起来像「那个域已经干净」,实际一条都没动(与 U80 逐项那一格同一个形);`localhost:3000` 同形(U93)。
|
|
111
|
+
* 文案单源——工具面(`tools/browser.mjs` 的 `validateArgs`)与动作面共用这一句。 */
|
|
112
|
+
export function cookieDomainError(domain) {
|
|
113
|
+
if (!badBareHost(domain)) return null
|
|
114
|
+
return 'cookies "domain" must be a non-empty host string (omit it to address every domain)'
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** `key` 形检(§2.2 · U86):给了但不是字符串 ⇒ 明示拒。空串是合法键名,不拒。 */
|
|
118
|
+
export function storageKeyError(key) {
|
|
119
|
+
if (typeof key === "string") return null
|
|
120
|
+
return 'storage "key" must be a string (omit it to address every key)'
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** `domain` 归一到过滤面:**未给** ⇒ null(不设过滤);**给了** ⇒ 形检,形不对就明示拒——不静默当作没给
|
|
124
|
+
* (U86:`clear` 会把一个畸形输入兑现成全域清空)。 */
|
|
125
|
+
function domainFilter(domain) {
|
|
126
|
+
if (domain === undefined) return null
|
|
127
|
+
const invalid = cookieDomainError(domain)
|
|
128
|
+
if (invalid) throw new Error(invalid)
|
|
129
|
+
return domain
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** `cookies set` 的逐项校验(§2.2 的形判规则 + 字段实例表;返回因由消息或 null——工具面与动作面共用同一处
|
|
133
|
+
* 文案,D2 单一权威)。判法只有一条:键不存在 = 没给;给了就必须是声明类型且形合法,否则显式拒;`null`
|
|
134
|
+
* 也算「给了但形不对」。逐项字段不各写一套判法——字段加进来就漏,规则不漏。 */
|
|
135
|
+
export function cookieSetError(cookies) {
|
|
136
|
+
if (!Array.isArray(cookies) || cookies.length === 0) return "cookies set requires cookies"
|
|
137
|
+
for (const item of cookies) {
|
|
138
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) return "cookies set requires name and value on every cookie"
|
|
139
|
+
// 键缺失与「给了但类型不对」分两句(§2.2 字段表):前者说的是缺什么,后者点名是哪个字段的形不对。
|
|
140
|
+
if (item.name === undefined || item.value === undefined) return "cookies set requires name and value on every cookie"
|
|
141
|
+
if (typeof item.name !== "string") return 'cookies set "name" must be a string'
|
|
142
|
+
if (item.name === "") return "cookies set requires name and value on every cookie"
|
|
143
|
+
if (typeof item.value !== "string") return 'cookies set "value" must be a string'
|
|
144
|
+
// 逐项与顶层同一个判法(§2.2 · U86):`null` ∥ 非字符串 ∥ 空串一律拒,只有「键不存在」才算没给。
|
|
145
|
+
// 逐项放行 `null` 会让同一个参数两级两套语义,读者只能靠猜。
|
|
146
|
+
if (item.domain !== undefined && badBareHost(item.domain)) {
|
|
147
|
+
return `cookies set "${item.name}" domain must be a bare host such as "example.test" (no scheme, no path, no port)`
|
|
148
|
+
}
|
|
149
|
+
// `path` 按协议就是绝对路径:以 `/` 开头才算形合法——`""` 既不是合法 path,也不是「没给」。
|
|
150
|
+
if (item.path !== undefined && (typeof item.path !== "string" || !item.path.startsWith("/"))) {
|
|
151
|
+
return 'cookies set "path" must be a string starting with "/"'
|
|
152
|
+
}
|
|
153
|
+
// `expires` 声明类型 = number(Unix 秒),**不收字符串、不做 `Number()` 强转**:`Number("")` 是 `0`,
|
|
154
|
+
// 一个空串会被写成一条**立即过期**的 cookie(等于删),而错要等到回读面才露头。校验的责任是在写之前
|
|
155
|
+
// 拦住,不是让回读去发现。
|
|
156
|
+
if (item.expires !== undefined && !Number.isFinite(item.expires)) {
|
|
157
|
+
return 'cookies set "expires" must be Unix seconds (omit it for a session cookie)'
|
|
158
|
+
}
|
|
159
|
+
// `httpOnly` ∥ `secure` 收非布尔的后果最重:`"true"` 会被静默收成 `false`,得到一条**不是 httpOnly** 的
|
|
160
|
+
// cookie 加一张成功回执——而 AC-BT1 的整个主题就是 httpOnly。
|
|
161
|
+
if (item.httpOnly !== undefined && typeof item.httpOnly !== "boolean") {
|
|
162
|
+
return 'cookies set "httpOnly" must be a boolean (omit it for false)'
|
|
163
|
+
}
|
|
164
|
+
if (item.secure !== undefined && typeof item.secure !== "boolean") {
|
|
165
|
+
return 'cookies set "secure" must be a boolean (omit it for false)'
|
|
166
|
+
}
|
|
167
|
+
// `sameSite` 去掉了 `""` 与 `null` 的豁免:同一个「给了但形不对」,`domain` 拒、它不拒就是两级两判。
|
|
168
|
+
// 非字符串那一支由 `normalizeSameSite` 自己挡(它只收 string,且不做任何 stringify)——判形单源。
|
|
169
|
+
if (item.sameSite !== undefined && normalizeSameSite(item.sameSite) === null) {
|
|
170
|
+
return 'cookies set "sameSite" must be Strict, Lax or None'
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return null
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** 一条 cookie 的行形(§2.2):`name=value; domain=<d>; path=<p>[; expires=<ISO>][; httpOnly][; secure][; sameSite=<S>]`。 */
|
|
177
|
+
function cookieLine(cookie) {
|
|
178
|
+
const parts = [`${cookie.name}=${cookie.value}`, `domain=${cookie.domain}`, `path=${cookie.path || "/"}`]
|
|
179
|
+
const expires = Number(cookie.expires)
|
|
180
|
+
if (Number.isFinite(expires) && expires > 0) parts.push(`expires=${new Date(expires * 1000).toISOString()}`)
|
|
181
|
+
if (cookie.httpOnly) parts.push("httpOnly")
|
|
182
|
+
if (cookie.secure) parts.push("secure")
|
|
183
|
+
if (cookie.sameSite) parts.push(`sameSite=${cookie.sameSite}`)
|
|
184
|
+
return parts.join("; ")
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** 正文截断(§2.2 两行自有标记形):`[truncated: showing <k> of <N> — <提示>]`。 */
|
|
188
|
+
function capBody(header, lines, note) {
|
|
189
|
+
let body = ""
|
|
190
|
+
let shown = 0
|
|
191
|
+
for (const line of lines) {
|
|
192
|
+
const next = body === "" ? line : `${body}\n${line}`
|
|
193
|
+
if (next.length > SESSION_STATE_MAX_CHARS) break
|
|
194
|
+
body = next
|
|
195
|
+
shown += 1
|
|
196
|
+
}
|
|
197
|
+
const text = body === "" ? header : `${header}\n${body}`
|
|
198
|
+
if (shown < lines.length) return `${text}\n[truncated: showing ${shown} of ${lines.length} — ${note}]`
|
|
199
|
+
return text
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// ── cookies(浏览器级面——三形全走页面会话的 Network 域,§2.2 ∥ KD-27 / KD-29)────────────────
|
|
203
|
+
|
|
204
|
+
async function allCookies(h) {
|
|
205
|
+
const res = await h.call("Network.getAllCookies")
|
|
206
|
+
// 回执畸形 ⇒ 明示错:把「没读到」报成「本来就没有」会让 `get` 回 `0 cookie(s)`、无 `domain` 的 `clear` 回
|
|
207
|
+
// `0 removed`——一个读失败被伪装成一个关于 cookie store 的事实(空集合不得顶替读不出)。
|
|
208
|
+
if (!Array.isArray(res?.cookies)) throw h.pageError("cookies failed — the browser returned no cookie list (Network.getAllCookies)")
|
|
209
|
+
return res.cookies
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async function cookiesGet(filter, h) {
|
|
213
|
+
const all = await allCookies(h)
|
|
214
|
+
const picked = filter ? all.filter((cookie) => domainMatches(cookie.domain, filter)) : all
|
|
215
|
+
const header = `[cookies get] ${picked.length} cookie(s)${filter ? ` [domain "${filter}"]` : ""}`
|
|
216
|
+
return capBody(header, picked.map(cookieLine), "pass domain to narrow")
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** 未落地句(§2.2 · KD-30 两形):找不到同名同域条目 ⇒ 点名实际落点;条目在但值/path 不符 ⇒ 点名存储里的值。 */
|
|
220
|
+
function describeUnlanded(u) {
|
|
221
|
+
if (u.held !== undefined) {
|
|
222
|
+
const tail = u.heldPath ? ` at path "${u.heldPath}"` : ""
|
|
223
|
+
return `cookies set "${u.name}" @ ${u.domain} did not land — the browser holds "${u.held}"${tail}`
|
|
224
|
+
}
|
|
225
|
+
if (u.actual) return `cookies set "${u.name}" @ ${u.domain} did not land — the browser stored ${u.name}@${u.actual}`
|
|
226
|
+
return `cookies set "${u.name}" @ ${u.domain} did not land`
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** `set`:逐条写 → 按 `name`+`domain` 回读核对(逐字比 `value`,给了 `path` 再核 `path`——KD-30)→
|
|
230
|
+
* 只列核对通过的条目;有一条未落地 ⇒ 先报错、再点名(未落地的项 + 已落地的条数)。
|
|
231
|
+
* **不做回滚**——cookie store 无事务,删掉已落地的条目反而伪造一个干净态。 */
|
|
232
|
+
async function cookiesSet(args, h) {
|
|
233
|
+
const invalid = cookieSetError(args.cookies)
|
|
234
|
+
if (invalid) throw new Error(invalid)
|
|
235
|
+
const info = await h.pageInfo()
|
|
236
|
+
// 未给 `domain` 的条目要落到当前页源 ⇒ 页面没有 http(s) origin(`about:blank` ∥ `data:`)时无源可落:
|
|
237
|
+
// 明示拒(与 `storage` 首条件同族——同类面不能一边有明示句、一边漏原始错),且在**写之前**拒。
|
|
238
|
+
// 给了 `domain` 的条目不受此限:跨域直写是 F-BT20 的本体,页面在哪无关。
|
|
239
|
+
// 形检已过 ⇒ 只有「键不存在」算没给(`null` ∥ 空串在 `cookieSetError` 里已拒——逐项与顶层同一判法)。
|
|
240
|
+
const needsOrigin = args.cookies.some((item) => item.domain === undefined)
|
|
241
|
+
if (needsOrigin && !httpOriginOrNull(info.url)) {
|
|
242
|
+
throw h.pageError(`cookies set requires domain or an http(s) page origin — current page is "${info.url}"`)
|
|
243
|
+
}
|
|
244
|
+
const wanted = args.cookies.map((item) => {
|
|
245
|
+
const domain = item.domain === undefined ? hostOf(info.url) : item.domain
|
|
246
|
+
return { item, domain, key: bare(domain) }
|
|
247
|
+
})
|
|
248
|
+
for (const { item } of wanted) {
|
|
249
|
+
const params = { name: item.name, value: item.value }
|
|
250
|
+
// 跨域直写(F-BT20):给什么 `domain` 写什么域——原样交给浏览器,不设开关、不设审批门
|
|
251
|
+
if (item.domain !== undefined) params.domain = item.domain
|
|
252
|
+
else params.url = info.url // 未给 domain ⇒ 落到当前页源
|
|
253
|
+
// 形检已过(§2.2 形判规则)⇒ 逐字段**原样**交给浏览器:不再有「收成 false」∥ `Number()` 强转这类静默转换,
|
|
254
|
+
// 校验与传值两处同看一个值,才不会出现「校验看的是 A、传给浏览器的却是 B」。
|
|
255
|
+
if (item.path !== undefined) params.path = item.path
|
|
256
|
+
if (item.httpOnly !== undefined) params.httpOnly = item.httpOnly
|
|
257
|
+
if (item.secure !== undefined) params.secure = item.secure
|
|
258
|
+
const sameSite = normalizeSameSite(item.sameSite)
|
|
259
|
+
if (sameSite) params.sameSite = sameSite
|
|
260
|
+
if (item.expires !== undefined) params.expires = item.expires
|
|
261
|
+
await h.call("Network.setCookie", params)
|
|
262
|
+
}
|
|
263
|
+
const landed = await allCookies(h)
|
|
264
|
+
const verified = []
|
|
265
|
+
const unlanded = []
|
|
266
|
+
for (const { item, domain, key } of wanted) {
|
|
267
|
+
// 回读核对(KD-30):按 `name`+`domain` 定位后**逐字比 `value`**(请求给了 `path` 的再核 `path`)。
|
|
268
|
+
// 只核 `name`+`domain` 不够:同名同域覆盖写时旧条目就在那儿,回读会假通过。
|
|
269
|
+
const candidates = landed.filter((cookie) => cookie.name === item.name && bare(cookie.domain) === key)
|
|
270
|
+
const pathGiven = item.path !== undefined
|
|
271
|
+
const hit = candidates.find((cookie) => cookie.value === item.value && (!pathGiven || (cookie.path || "/") === item.path))
|
|
272
|
+
if (hit) { verified.push(hit); continue }
|
|
273
|
+
if (candidates.length > 0) {
|
|
274
|
+
// 条目在、值(或 path)不是写入值 ⇒ 点名存储里的实际内容(头 80 字符,与错误句族同额度)
|
|
275
|
+
const shown = candidates.find((cookie) => pathGiven && (cookie.path || "/") === item.path) ?? candidates[0]
|
|
276
|
+
const shownPath = shown.path || "/"
|
|
277
|
+
unlanded.push({
|
|
278
|
+
name: item.name,
|
|
279
|
+
domain,
|
|
280
|
+
held: String(shown.value ?? "").slice(0, 80),
|
|
281
|
+
heldPath: pathGiven && shownPath !== item.path ? shownPath : null,
|
|
282
|
+
})
|
|
283
|
+
continue
|
|
284
|
+
}
|
|
285
|
+
const elsewhere = landed.find((cookie) => cookie.name === item.name) // 落点与请求不符(KD-30 的实测形态)
|
|
286
|
+
unlanded.push({ name: item.name, domain, actual: elsewhere?.domain ?? null })
|
|
287
|
+
}
|
|
288
|
+
if (unlanded.length > 0) {
|
|
289
|
+
const first = unlanded[0]
|
|
290
|
+
const lines = [describeUnlanded(first)]
|
|
291
|
+
if (unlanded.length > 1) lines.push(`unlanded: ${unlanded.map((u) => `${u.name}@${u.domain}`).join(", ")}`)
|
|
292
|
+
lines.push(`${verified.length} of ${wanted.length} cookie(s) landed — no rollback (the cookie store has no transactions)`)
|
|
293
|
+
throw h.pageError(lines.join("\n"))
|
|
294
|
+
}
|
|
295
|
+
const listed = verified.map((cookie) => `${cookie.name}@${cookie.domain}(${cookie.path || "/"})`)
|
|
296
|
+
return `[cookies set] ${listed.length} cookie(s): ${listed.join(", ")}`
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** `clear`:有 `domain` 逐条删(只删该域及其子域);无 `domain` 清空整份共享 profile(§3.4 的硬后果——
|
|
300
|
+
* 他实例的登录态一并消失,这是 F-BT20「按 domain ∥ 全部」的直接后果,不另设确认门)。 */
|
|
301
|
+
async function cookiesClear(filter, h) {
|
|
302
|
+
const all = await allCookies(h)
|
|
303
|
+
if (!filter) {
|
|
304
|
+
await h.call("Network.clearBrowserCookies")
|
|
305
|
+
return `[cookies clear] ${all.length} removed (all domains)`
|
|
306
|
+
}
|
|
307
|
+
const picked = all.filter((cookie) => domainMatches(cookie.domain, filter))
|
|
308
|
+
for (const cookie of picked) {
|
|
309
|
+
await h.call("Network.deleteCookies", { name: cookie.name, domain: cookie.domain, path: cookie.path })
|
|
310
|
+
}
|
|
311
|
+
return `[cookies clear] ${picked.length} removed (domain "${filter}")`
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** cookies(§2.2):op ∈ get / set / clear。不受 `browser.allowDomains` 收束(KD-28:它是浏览器级面,
|
|
315
|
+
* 用当前页或目标域去检都等于给跨域写新增一道门——F-BT20 明令不设)。 */
|
|
316
|
+
async function actCookies(args, ctx, h) {
|
|
317
|
+
const op = String(args.op ?? "")
|
|
318
|
+
if (!COOKIE_OPS.includes(op)) throw new Error(unknownCookieOp(op))
|
|
319
|
+
// 形检在前(`domain` 未给 ⇒ null;给了但形不对 ⇒ 明示拒,不升到全域清空——U86)——在开会话、发命令之前。
|
|
320
|
+
// `set` 的逐项形检同样提到会话之前(与 `domain` 同拍):非法 payload 不该先把浏览器拉起来再拒;
|
|
321
|
+
// `cookiesSet` 里那道保留作第二道(沿 `validateArgs` ∥ `domainFilter` 的既有双道纪律)。
|
|
322
|
+
const filter = domainFilter(args.domain)
|
|
323
|
+
if (op === "set") {
|
|
324
|
+
const invalid = cookieSetError(args.cookies)
|
|
325
|
+
if (invalid) throw new Error(invalid)
|
|
326
|
+
}
|
|
327
|
+
await h.ensureSession(args)
|
|
328
|
+
if (op === "get") return cookiesGet(filter, h)
|
|
329
|
+
if (op === "set") return cookiesSet(args, h)
|
|
330
|
+
return cookiesClear(filter, h)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// ── storage(页级面——页内 window.localStorage ∥ sessionStorage,§2.2 ∥ KD-27)────────────────
|
|
334
|
+
|
|
335
|
+
/** 页侧 store 引用(两域各是一个独立作用域对象——不串档由浏览器保证,本档按 `area` 二选一取)。 */
|
|
336
|
+
const storeRef = (area) => `window[${JSON.stringify(area)}]`
|
|
337
|
+
|
|
338
|
+
/** 页侧读表达式:单键 ⇒ `{ok, found, value}`;全量 ⇒ `{ok, entries: [[k, v], …]}`。 */
|
|
339
|
+
function storageGetExpression(area, key, single) {
|
|
340
|
+
const body = single
|
|
341
|
+
? `var v = store.getItem(${JSON.stringify(key)})
|
|
342
|
+
return { ok: true, found: v !== null, value: v }`
|
|
343
|
+
: `var entries = []
|
|
344
|
+
for (var i = 0; i < store.length; i++) { var k = store.key(i); entries.push([k, store.getItem(k)]) }
|
|
345
|
+
return { ok: true, entries: entries }`
|
|
346
|
+
return `${mark("storage-get")}(function () {
|
|
347
|
+
var store = ${storeRef(area)}
|
|
348
|
+
if (!store) return { ok: false }
|
|
349
|
+
${body.replace(/\n/g, "\n ")}
|
|
350
|
+
})()`
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** 页侧写表达式:`setItem` 收字符串;值以 JSON 字面量嵌入 ⇒ 换行 ∥ 引号 ∥ 中文 ∥ 长串都无损(AC-BT5)。 */
|
|
354
|
+
function storageSetExpression(area, key, value) {
|
|
355
|
+
return `${mark("storage-set")}(function () {
|
|
356
|
+
var store = ${storeRef(area)}
|
|
357
|
+
if (!store) return { ok: false }
|
|
358
|
+
store.setItem(${JSON.stringify(key)}, ${JSON.stringify(value)})
|
|
359
|
+
return { ok: true }
|
|
360
|
+
})()`
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** 页侧清表达式:单键 ⇒ `removeItem`(removed = 0/1);全清 ⇒ `clear()`(removed = 原键数)。 */
|
|
364
|
+
function storageClearExpression(area, key, single) {
|
|
365
|
+
const body = single
|
|
366
|
+
? `var had = store.getItem(${JSON.stringify(key)}) !== null
|
|
367
|
+
store.removeItem(${JSON.stringify(key)})
|
|
368
|
+
return { ok: true, removed: had ? 1 : 0 }`
|
|
369
|
+
: `var n = store.length
|
|
370
|
+
store.clear()
|
|
371
|
+
return { ok: true, removed: n }`
|
|
372
|
+
return `${mark("storage-clear")}(function () {
|
|
373
|
+
var store = ${storeRef(area)}
|
|
374
|
+
if (!store) return { ok: false }
|
|
375
|
+
${body.replace(/\n/g, "\n ")}
|
|
376
|
+
})()`
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** 页 origin 的读法(`cookies set` 的源前提 ∥ `storage` 的可用前提共用一处):无真实 origin
|
|
380
|
+
* (`about:blank` ∥ `data:` ∥ 不可解析)⇒ null。 */
|
|
381
|
+
function httpOriginOrNull(url) {
|
|
382
|
+
let origin = null
|
|
383
|
+
try { origin = new URL(String(url)).origin } catch { origin = null }
|
|
384
|
+
return origin && origin !== "null" ? origin : null
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** 当前页 origin(回执头与 storage 的可用前提);无真实页 origin(`about:blank` ∥ `data:`)⇒ 明示拒
|
|
388
|
+
* (`window.localStorage` 在这两类页上不可用)。句形刻意复刻 `clipboard.mjs` 的 `pageOrigin`(设计 §13
|
|
389
|
+
* 「同款句形」)而不导出复用——动作模块间不互相 import(DAG 单向,§5)。 */
|
|
390
|
+
function httpOrigin(h, info) {
|
|
391
|
+
const origin = httpOriginOrNull(info.url)
|
|
392
|
+
if (!origin) {
|
|
393
|
+
throw h.pageError(`storage requires an http/https page origin — current page is "${info.url}"`)
|
|
394
|
+
}
|
|
395
|
+
return origin
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/** storage(§2.2):op ∈ get / set / clear × area ∈ localStorage ∥ sessionStorage(缺省 localStorage)。
|
|
399
|
+
* 受 `browser.allowDomains` 收束(KD-28:页内动作,与 click / type / evaluate 同面)。 */
|
|
400
|
+
async function actStorage(args, ctx, h) {
|
|
401
|
+
const op = String(args.op ?? "")
|
|
402
|
+
if (!STORAGE_OPS.includes(op)) throw new Error(unknownStorageOp(op))
|
|
403
|
+
// 「键不存在」才取缺省(§2.2):给了 `null` ∥ 非字符串 ∥ 空串 ⇒ 值域句(`null` 渲染成 `"null"`)。
|
|
404
|
+
// `?? "localStorage"` 会把一个形不对的输入静默兑现成缺省域——与 `domain` ∥ `key` 同一类:行为不能照走。
|
|
405
|
+
const given = args.area !== undefined
|
|
406
|
+
const area = given ? String(args.area) : "localStorage"
|
|
407
|
+
// 判形不靠 stringify(§2.2「非字符串 ⇒ 拒」):`String()` 只用来渲染错误句——`["localStorage"]` 也
|
|
408
|
+
// stringify 成合法域名,那样这一条就落空了;兄弟参数 `domain` ∥ `key` 的形检同样只认 `typeof`。
|
|
409
|
+
if (given && typeof args.area !== "string") throw new Error(unknownStorageArea(area))
|
|
410
|
+
if (!STORAGE_AREAS.includes(area)) throw new Error(unknownStorageArea(area))
|
|
411
|
+
const single = typeof args.key === "string"
|
|
412
|
+
if (op === "set" && (!single || typeof args.value !== "string")) throw new Error("storage set requires key and value")
|
|
413
|
+
// 给了 `key` 但不是字符串 ⇒ 明示拒:当作没给会让 `clear` 落到 `store.clear()`(清整份 store——U86)。
|
|
414
|
+
if (args.key !== undefined) {
|
|
415
|
+
const invalidKey = storageKeyError(args.key)
|
|
416
|
+
if (invalidKey) throw new Error(invalidKey)
|
|
417
|
+
}
|
|
418
|
+
await h.ensureSession(args)
|
|
419
|
+
const info = await h.pageInfo()
|
|
420
|
+
h.assertHostAllowed(info.url, ctx)
|
|
421
|
+
const origin = httpOrigin(h, info)
|
|
422
|
+
const head = `[storage ${op} ${area} @ ${origin}]`
|
|
423
|
+
if (op === "get") {
|
|
424
|
+
const res = await h.evalRaw(storageGetExpression(area, args.key, single))
|
|
425
|
+
if (!res?.ok) throw h.pageError(`storage ${op} failed — ${area} is unavailable on the current page`)
|
|
426
|
+
if (single) {
|
|
427
|
+
const label = JSON.stringify(args.key)
|
|
428
|
+
if (!res.found) return `${head} ${label} — not set`
|
|
429
|
+
return `${head} ${label} = ${JSON.stringify(String(res.value ?? ""))}`
|
|
430
|
+
}
|
|
431
|
+
// 全量面回执畸形 ⇒ 明示错(§2.2 storage 侧同句形):页侧没给出键表就把「没读到」报成 `0 key(s)`,等于把一个
|
|
432
|
+
// 读失败伪装成一个关于 storage 的事实——与 cookies 取面同一条理由(空集合不得顶替读不出)。
|
|
433
|
+
if (!Array.isArray(res.entries)) throw h.pageError(`storage failed — the page returned no key list (${area})`)
|
|
434
|
+
const entries = Array.from(res.entries, (pair) => [String(pair?.[0] ?? ""), String(pair?.[1] ?? "")])
|
|
435
|
+
const lines = entries.map(([k, v]) => `${k} = ${JSON.stringify(v)}`)
|
|
436
|
+
return capBody(`${head} ${entries.length} key(s)`, lines, "pass key to narrow")
|
|
437
|
+
}
|
|
438
|
+
if (op === "set") {
|
|
439
|
+
const res = await h.evalRaw(storageSetExpression(area, args.key, args.value))
|
|
440
|
+
if (!res?.ok) throw h.pageError(`storage set failed — ${area} is unavailable on the current page`)
|
|
441
|
+
return `${head} ${JSON.stringify(args.key)} ← ${args.value.length} chars`
|
|
442
|
+
}
|
|
443
|
+
const res = await h.evalRaw(storageClearExpression(area, args.key, single))
|
|
444
|
+
if (!res?.ok) throw h.pageError(`storage clear failed — ${area} is unavailable on the current page`)
|
|
445
|
+
if (single) return `${head} ${JSON.stringify(args.key)} removed`
|
|
446
|
+
return `${head} ${Number(res.removed) || 0} key(s) removed`
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/** 会话态分发表(会话档并入总表——§13「对齐(形)」)。 */
|
|
450
|
+
export const SESSION_STATE_ACTIONS = { cookies: actCookies, storage: actStorage }
|