@linxin666/dsh-remote-bridge 0.4.20 → 0.4.22

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/lib/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { createRequire } from "node:module";
2
2
  import { basename, dirname, isAbsolute, join, resolve, sep } from "node:path";
3
3
  import { createHash, createPrivateKey, createPublicKey, randomBytes, sign, timingSafeEqual, verify } from "node:crypto";
4
- import { chmod, mkdir, open, readFile, realpath, rename, stat } from "node:fs/promises";
4
+ import { chmod, lstat, mkdir, open, readFile, rename } from "node:fs/promises";
5
5
  import { deflateRawSync } from "node:zlib";
6
6
  import { homedir, networkInterfaces } from "node:os";
7
7
  import { isAbsolute as isAbsolute$1, join as join$1 } from "node:path/posix";
@@ -1298,8 +1298,9 @@ const CODE_UI_LIST_UNAVAILABLE = "ui-list-unavailable";
1298
1298
  const CODE_SESSION_NOT_FOLLOWABLE = "session-not-followable";
1299
1299
  /**
1300
1300
  * 该路径不在这次会话自己的持久 transcript 里:白名单只有两处来源,客户端自选的路径
1301
- * 一律不在其中。`realpath()` 解析结果不再等于该字符串时也回这一码——那个路径已经
1302
- * 指向别处,就不再是这次会话点过的那个文件。
1301
+ * 一律不在其中。该名字本身是个链接时也回这一码——它已被换成指向别处的一层门面,就
1302
+ * 不再是这次会话点过的那个文件。判据看的是条目自身(`lstat`),不比较 `realpath()`
1303
+ * 的解析结果:同一个文件的两种写法(Windows 8.3 短名、macOS `/var` 前缀)不等于换目标。
1303
1304
  */
1304
1305
  const CODE_PATH_NOT_IN_SESSION = "path-not-in-session";
1305
1306
  /**
@@ -2334,10 +2335,13 @@ function mediaTypeOfSignature(head) {
2334
2335
  *
2335
2336
  * The whitelist is the first decision and it is not this function's: a caller
2336
2337
  * hands over a path it already matched byte for byte. What is left is whether the
2337
- * file at that path is still the one the session named — `realpath()` has to
2338
- * return the same string, so a symlink pointing anywhere else is refused rather
2339
- * than followed — whether it is a regular file at all, and whether its own first
2340
- * bytes are one of the four image signatures. The extension is never consulted: a
2338
+ * name at that path is still the file the session named — the name itself must not
2339
+ * be a link, so a symlink pointing anywhere else is refused rather than followed —
2340
+ * whether it is a regular file at all, and whether its own first bytes are one of
2341
+ * the four image signatures. The link test reads the entry itself (`lstat`) rather
2342
+ * than resolving it: comparing `realpath()` against the stored string rejects every
2343
+ * 8.3 short name (Windows temp directories are `RUNNER~1`) and every macOS `/var`
2344
+ * path, where two spellings of one file are not a substitution attack. The extension is never consulted: a
2341
2345
  * `.png` that is not a PNG is refused, and a PNG named `.txt` is served.
2342
2346
  *
2343
2347
  * The answer's `image` is the reference the session's transcript named; this
@@ -2347,24 +2351,14 @@ function mediaTypeOfSignature(head) {
2347
2351
  */
2348
2352
  async function readAttachment(facts) {
2349
2353
  const path = facts.path;
2350
- let resolved;
2354
+ let size;
2351
2355
  try {
2352
- resolved = await realpath(path);
2353
- } catch {
2354
- return {
2356
+ const info = await lstat(path);
2357
+ if (info.isSymbolicLink()) return {
2355
2358
  ok: false,
2356
- code: CODE_ATTACHMENT_NOT_FOUND,
2357
- msg: "no file at " + path
2359
+ code: CODE_PATH_NOT_IN_SESSION,
2360
+ msg: path + " is a link, not the file this session named"
2358
2361
  };
2359
- }
2360
- if (resolved !== path) return {
2361
- ok: false,
2362
- code: CODE_PATH_NOT_IN_SESSION,
2363
- msg: path + " resolves to " + resolved + ", which this session never named"
2364
- };
2365
- let size;
2366
- try {
2367
- const info = await stat(path);
2368
2362
  if (!info.isFile()) return {
2369
2363
  ok: false,
2370
2364
  code: CODE_ATTACHMENT_NOT_FOUND,
@@ -11919,16 +11913,39 @@ function makePairTokenRoute(deps) {
11919
11913
  };
11920
11914
  }
11921
11915
  //#endregion
11922
- //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-typert-protocol@0.2.2-rc.3_@deepseek-ai+cordis@4.1.0/node_modules/@deepseek-ai/dsh-typert-protocol/lib/index.js
11916
+ //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-error@0.2.3-alpha.2_@deepseek-ai+cordis@4.2.0-alpha.1/node_modules/@deepseek-ai/dsh-error/lib/index.js
11917
+ /**
11918
+ * Harness error base with a stable machine-routable code and chained cause.
11919
+ * Package errors extend it so tool results and replay can retain failure class.
11920
+ * @module @deepseek-ai/dsh-error
11921
+ */
11922
+ /**
11923
+ * Base class for all harness errors. Carries a `code` (stable, programmatic —
11924
+ * e.g. `NO_ADAPTER`, `INVALID_ARGS`) distinct from the
11925
+ * human-readable `message`, and supports `cause` chaining via the standard
11926
+ * `ErrorOptions`. `name` defaults to the subclass constructor name.
11927
+ */
11928
+ var HarnessError = class extends Error {
11929
+ /** Stable machine-routable failure class (e.g. `RATE_LIMIT`); route on this, never by parsing `message`. */
11930
+ code;
11931
+ constructor(message, code, options) {
11932
+ super(message, options);
11933
+ this.code = code;
11934
+ this.name = new.target.name;
11935
+ }
11936
+ };
11937
+ //#endregion
11938
+ //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-typert-protocol@0.2.3-alpha.2_@deepseek-ai+cordis@4.2.0-alpha.1_@deeps_6153b0a06151bba505d5f32876686bf8/node_modules/@deepseek-ai/dsh-typert-protocol/lib/index.js
11923
11939
  /** The one Remote failure class shared by owners, the Gateway, and consumers. */
11924
11940
  /**
11925
- * One Remote call failure: a real Error carrying its stable code and typed
11941
+ * One Remote call failure: a `HarnessError` carrying its stable code and typed
11926
11942
  * details. Owners throw it at the failure point; the Host Gateway encodes it
11927
11943
  * onto the wire unchanged; the Client face rebuilds an instance for the
11928
11944
  * `RemoteResult` error branch, so `throw result.error` keeps throw semantics.
11929
- * Discrimination is always by `code`, never by instanceof.
11945
+ * A tool body that throws it records `name: 'RemoteError'` and its Remote code
11946
+ * on `tool/result`. Discrimination is always by `code`, never by instanceof.
11930
11947
  */
11931
- var RemoteError = class extends Error {
11948
+ var RemoteError = class extends HarnessError {
11932
11949
  code;
11933
11950
  details;
11934
11951
  /** Structural marker: cross-realm/bundle identification never uses instanceof. */
@@ -11940,7 +11957,7 @@ var RemoteError = class extends Error {
11940
11957
  * @param options - standard Error options (`cause` survives in-process only).
11941
11958
  */
11942
11959
  constructor(code, message, details, options) {
11943
- super(message, options);
11960
+ super(message, code, options);
11944
11961
  this.code = code;
11945
11962
  this.details = details;
11946
11963
  this.name = "RemoteError";
@@ -12006,8 +12023,8 @@ var TypertRemoteService = class extends Service {
12006
12023
  * tree registers it on the root, where it outlives any one Service.
12007
12024
  */
12008
12025
  function provideInvocationAccessor(ctx) {
12009
- if (Object.hasOwn(ctx.root.reflect.props, "invocation")) return;
12010
- ctx.root.accessor("invocation", { get: () => void 0 });
12026
+ if (Object.hasOwn(ctx[Context.root].reflect.props, "invocation")) return;
12027
+ ctx[Context.root].accessor("invocation", { get: () => void 0 });
12011
12028
  }
12012
12029
  function Remote(methodExportOrOptions, context) {
12013
12030
  if (typeof methodExportOrOptions === "string") {
@@ -12101,7 +12118,7 @@ function validateName(subject, value) {
12101
12118
  if (!isTypertRemoteSegment(value)) throw new TypeError(`typert-protocol: ${subject} must contain only RPC endpoint segment characters`);
12102
12119
  }
12103
12120
  //#endregion
12104
- //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-util-values@0.2.2-rc.3_@deepseek-ai+cordis@4.1.0/node_modules/@deepseek-ai/dsh-util-values/lib/index.js
12121
+ //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-util-values@0.2.3-alpha.2_@deepseek-ai+cordis@4.2.0-alpha.1/node_modules/@deepseek-ai/dsh-util-values/lib/index.js
12105
12122
  /**
12106
12123
  * Lazily scanned view of one JSON object's top-level fields, built from text
12107
12124
  * that may still be streaming or from an already parsed object. Nothing is
@@ -12920,7 +12937,7 @@ function deepFreeze(value) {
12920
12937
  return value;
12921
12938
  }
12922
12939
  //#endregion
12923
- //#region ../../node_modules/.pnpm/@deepseek-ai+cosmokit@1.9.0/node_modules/@deepseek-ai/cosmokit/lib/index.js
12940
+ //#region ../../node_modules/.pnpm/@deepseek-ai+cosmokit@1.10.0-alpha.1/node_modules/@deepseek-ai/cosmokit/lib/index.js
12924
12941
  /** Return true when a value is `null` or `undefined`. */
12925
12942
  function isNullable(value) {
12926
12943
  return value === null || value === void 0;
@@ -13174,7 +13191,7 @@ var Time;
13174
13191
  Time.template = template;
13175
13192
  })(Time || (Time = {}));
13176
13193
  //#endregion
13177
- //#region ../../node_modules/.pnpm/@deepseek-ai+schemastery@3.19.0/node_modules/@deepseek-ai/schemastery/lib/index.mjs
13194
+ //#region ../../node_modules/.pnpm/@deepseek-ai+schemastery@3.20.0-alpha.1/node_modules/@deepseek-ai/schemastery/lib/index.mjs
13178
13195
  const kSchema = Symbol.for("schemastery");
13179
13196
  const kValidationError = Symbol.for("ValidationError");
13180
13197
  globalThis.__schemastery_index__ ??= 0;
@@ -13807,41 +13824,11 @@ defineMethod("transform", [
13807
13824
  "preserve"
13808
13825
  ], ({ inner }, isInner) => inner.toString(isInner));
13809
13826
  //#endregion
13810
- //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-timeout@0.2.2-rc.3_@deepseek-ai+cordis@4.1.0/node_modules/@deepseek-ai/dsh-timeout/lib/index.js
13827
+ //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-timeout@0.2.3-alpha.2_@deepseek-ai+cordis@4.2.0-alpha.1/node_modules/@deepseek-ai/dsh-timeout/lib/index.js
13811
13828
  /** Largest delay Node schedules without clamping it to one millisecond. */
13812
13829
  const MAX_TIMER_DELAY_MS = 2147483647;
13813
13830
  //#endregion
13814
- //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-error@0.2.2-rc.3_@deepseek-ai+cordis@4.1.0/node_modules/@deepseek-ai/dsh-error/lib/index.js
13815
- /**
13816
- * Harness error base with a stable machine-routable code and chained cause.
13817
- * Package errors extend it so tool results and replay can retain failure class.
13818
- * @module @deepseek-ai/dsh-error
13819
- */
13820
- /**
13821
- * Base class for all harness errors. Carries a `code` (stable, programmatic —
13822
- * e.g. `NO_ADAPTER`, `INVALID_ARGS`) distinct from the
13823
- * human-readable `message`, and supports `cause` chaining via the standard
13824
- * `ErrorOptions`. `name` defaults to the subclass constructor name.
13825
- */
13826
- var HarnessError = class extends Error {
13827
- /** Stable machine-routable failure class (e.g. `RATE_LIMIT`); route on this, never by parsing `message`. */
13828
- code;
13829
- constructor(message, code, options) {
13830
- super(message, options);
13831
- this.code = code;
13832
- this.name = new.target.name;
13833
- }
13834
- };
13835
- //#endregion
13836
- //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-llm@0.2.2-rc.3_@deepseek-ai+cordis@4.1.0_@deepseek-ai+dsh-error@0.2.2-rc.3_@deepseek-ai+cordis@4.1.0_/node_modules/@deepseek-ai/dsh-llm/lib/index.js
13837
- /**
13838
- * Detach and deep-freeze a message whose identity already exists.
13839
- * @param message - complete message, including its stable identity.
13840
- * @returns an immutable snapshot that preserves the identity.
13841
- */
13842
- function freezeMessage(message) {
13843
- return deepFreeze(structuredClone(message));
13844
- }
13831
+ //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-llm@0.2.3-alpha.2_@deepseek-ai+cordis@4.2.0-alpha.1_@deepseek-ai+dsh-e_3734babba1c9d85ae3e99b1bc6b8c6ed/node_modules/@deepseek-ai/dsh-llm/lib/index.js
13845
13832
  /**
13846
13833
  * LLM failure codes and helpers over the shared Harness error base. The base
13847
13834
  * class lives in `@deepseek-ai/dsh-error` and is re-exported here, so both
@@ -13860,6 +13847,7 @@ const LLM_FAILURE_CODES = {
13860
13847
  ACCOUNT_TOKEN_INVALID: {},
13861
13848
  AUTH: {},
13862
13849
  CLIENT_UPGRADE_REQUIRED: {},
13850
+ CONTENT_REFUSED: {},
13863
13851
  CONTEXT_WINDOW_EXCEEDED: {},
13864
13852
  DISCOVERY_FAILED: {},
13865
13853
  DISCOVERY_UNSUPPORTED: {},
@@ -13923,6 +13911,14 @@ new RegExp(String.raw`(?:^|[^a-z0-9])context[\s_-](?:length|window)[\s_-]` + Str
13923
13911
  new RegExp(String.raw`\b(?:request|prompt|input|messages?)\s+(?:is\s+|are\s+)?` + String.raw`too\s+(?:large|long)\s+for\s+(?:(?:this|the)\s+)?` + String.raw`(?:model(?:'s)?\s+)?context(?:\s+window)?\b`, "i");
13924
13912
  new RegExp(String.raw`\b(?:input|prompt|request|messages?)\b.{0,40}` + String.raw`\b(?:exceed(?:s|ed)?|overflows?|is\s+larger\s+than)\b.{0,40}` + String.raw`\b(?:the\s+)?(?:model(?:'s)?\s+)?context(?:\s+(?:length|window))?\b`, "i");
13925
13913
  /**
13914
+ * Detach and deep-freeze a message whose identity already exists.
13915
+ * @param message - complete message, including its stable identity.
13916
+ * @returns an immutable snapshot that preserves the identity.
13917
+ */
13918
+ function freezeMessage(message) {
13919
+ return deepFreeze(structuredClone(message));
13920
+ }
13921
+ /**
13926
13922
  * Provider-owned request-retry policy configuration and resolution.
13927
13923
  *
13928
13924
  * Adapters expose one resolved policy per registered provider route; the
@@ -14116,10 +14112,11 @@ function failureSnapshot(value) {
14116
14112
  }
14117
14113
  /** Read an SDK error message without letting an accessor replace the primary failure. */
14118
14114
  function errorMessage(error) {
14115
+ let message;
14119
14116
  try {
14120
- const message = error.message;
14121
- if (typeof message === "string" && message.length > 0) return message;
14117
+ message = error.message;
14122
14118
  } catch (_sdkMessageGetter) {}
14119
+ if (typeof message === "string" && message.length > 0) return message;
14123
14120
  return "LLM adapter failed";
14124
14121
  }
14125
14122
  /** Trust only Harness-owned codes; third-party SDK codes are not our taxonomy. */
@@ -14729,7 +14726,7 @@ var LlmError = class LlmError extends HarnessError {
14729
14726
  * @param provider - registered provider route.
14730
14727
  * @param model - exact model id.
14731
14728
  * @param image - normalized image reference.
14732
- * @returns dimensions and byte target used by the adapter's request serializer.
14729
+ * @returns maximum dimensions and byte budgets used by the request serializer; readImageRequest returns the actual encoded dimensions.
14733
14730
  */
14734
14731
  imageRequestTarget(provider, model, image) {
14735
14732
  return this.registration(provider).adapter.imageRequestTarget(provider, model, image);
@@ -15102,7 +15099,7 @@ function detachDirectoryEntry(entry) {
15102
15099
  };
15103
15100
  }
15104
15101
  //#endregion
15105
- //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-tools@0.2.2-rc.3_c6df380a2007cdef080fa53578fb6f5f/node_modules/@deepseek-ai/dsh-tools/lib/shared-DGx6D7zk.js
15102
+ //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-tools@0.2.3-alpha.2_fe5258332cf8d58c43bca084c938026e/node_modules/@deepseek-ai/dsh-tools/lib/shared-DGx6D7zk.js
15106
15103
  /** The selected provider is missing or unloaded; another provider must not take its calls. */
15107
15104
  var ToolExecutorUnavailableError = class extends HarnessError {
15108
15105
  constructor() {
@@ -15639,7 +15636,7 @@ var ToolActivationError = class extends HarnessError {
15639
15636
  }
15640
15637
  };
15641
15638
  //#endregion
15642
- //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-scope@0.2.2-rc.3_@deepseek-ai+cordis@4.1.0/node_modules/@deepseek-ai/dsh-scope/lib/index.js
15639
+ //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-scope@0.2.3-alpha.2_@deepseek-ai+cordis@4.2.0-alpha.1/node_modules/@deepseek-ai/dsh-scope/lib/index.js
15643
15640
  /**
15644
15641
  * Shared insertion-ordered storage and effect ownership for scope-aware registries.
15645
15642
  *
@@ -15892,7 +15889,7 @@ function scopeOf(ctx) {
15892
15889
  return ctx[kScope];
15893
15890
  }
15894
15891
  //#endregion
15895
- //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-tools@0.2.2-rc.3_c6df380a2007cdef080fa53578fb6f5f/node_modules/@deepseek-ai/dsh-tools/lib/index.js
15892
+ //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-tools@0.2.3-alpha.2_fe5258332cf8d58c43bca084c938026e/node_modules/@deepseek-ai/dsh-tools/lib/index.js
15896
15893
  /** Unified JSON-value schema DSL, inference, compilation, and tool definition helpers. @module dsh-tools/schema */
15897
15894
  const ANNOTATION_KEYS = [
15898
15895
  "description",
@@ -16468,6 +16465,7 @@ var GuardLayer = class {
16468
16465
  /**
16469
16466
  * Provide the executor for the calling scope; an unscoped contribution is the deployment default.
16470
16467
  * Rendering any executor-owned section requires the viewing scope's selection to remain available.
16468
+ * Disposal retires its sections; a replacement's same-name sections supersede those notices.
16471
16469
  * @param executor - initialized provider owned by the contributing plugin.
16472
16470
  * @returns the effect disposer. Unloading leaves the selection unavailable until it is replaced.
16473
16471
  */
@@ -28439,7 +28437,7 @@ var require_negotiator = /* @__PURE__ */ __commonJSMin(((exports, module) => {
28439
28437
  Negotiator.prototype.preferredMediaTypes = Negotiator.prototype.mediaTypes;
28440
28438
  }));
28441
28439
  //#endregion
28442
- //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-host-webserver@0.2.2-rc.3_@deepseek-ai+cordis@4.1.0_supports-color@7.2.0/node_modules/@deepseek-ai/dsh-host-webserver/lib/index.js
28440
+ //#region ../../node_modules/.pnpm/@deepseek-ai+dsh-host-webserver@0.2.3-alpha.2_@deepseek-ai+cordis@4.2.0-alpha.1_supports-color@7.2.0/node_modules/@deepseek-ai/dsh-host-webserver/lib/index.js
28443
28441
  var lib_exports = /* @__PURE__ */ __exportAll({
28444
28442
  WebServer: () => WebServer,
28445
28443
  bindUrlHost: () => bindUrlHost,
@@ -28548,6 +28546,8 @@ function renderIndexInjections(html, rows) {
28548
28546
  * Route handlers retain direct response ownership; a route or the fallback may
28549
28547
  * instead give a resource, which maps a Fetch `Request` to a `Response` and
28550
28548
  * also answers in-process reads.
28549
+ * WebServer events use the instance as their subject: ordinary listeners follow
28550
+ * its webServer isolation, while explicitly global listeners receive every instance.
28551
28551
  */
28552
28552
  const DEFAULT_COMPRESSION = "none";
28553
28553
  const DEFAULT_COMPRESSION_LEVEL = 1;
@@ -28734,7 +28734,8 @@ async function writeResponse(res, response, head, signal) {
28734
28734
  * initialization, and the boot process reports the failed fiber. Route
28735
28735
  * registration order does not affect requests because configured named routes
28736
28736
  * must be distinct, and a request no named route matches is answered 404
28737
- * until the fallback owner registers.
28737
+ * until the fallback owner registers. HTTP and upgrade admission refusals
28738
+ * carry `Cache-Control: no-store`.
28738
28739
  */
28739
28740
  var WebServer = class extends Service {
28740
28741
  config;
@@ -28840,12 +28841,12 @@ var WebServer = class extends Service {
28840
28841
  * composition). One owner only — a second registration throws, because two
28841
28842
  * fallbacks cannot compose.
28842
28843
  * @param fallback - a handler owning the full response lifecycle of
28843
- * unmatched requests, or `{ resource }` answering them.
28844
+ * unmatched requests, or an object declaring `handler` or `resource` and auth.
28844
28845
  * @returns the disposer releasing the seat.
28845
28846
  */
28846
28847
  registerFallback(fallback) {
28847
28848
  if (this.fallback !== void 0) throw new Error("webserver: fallback already registered");
28848
- this.fallback = fallback;
28849
+ this.fallback = typeof fallback === "function" ? { handler: fallback } : fallback;
28849
28850
  return () => {
28850
28851
  this.fallback = void 0;
28851
28852
  };
@@ -28856,6 +28857,7 @@ var WebServer = class extends Service {
28856
28857
  * path picks the responder by the HTTP match order — exact, longest prefix,
28857
28858
  * then the fallback — and only a resource answers. This method checks no
28858
28859
  * cookie, Host, or Origin: the caller admits the reader before reading.
28860
+ * A resolved deny policy returns 403 even for an explicitly admitted Peer.
28859
28861
  * @param request - the read; its URL path selects the responder.
28860
28862
  * @param peer - the Peer the read speaks for, passed to the resource.
28861
28863
  * @returns the resource's response, without a body for HEAD as over HTTP;
@@ -28870,8 +28872,16 @@ var WebServer = class extends Service {
28870
28872
  status: 405,
28871
28873
  headers: { allow: RESOURCE_METHODS }
28872
28874
  });
28873
- const responder = this.responderFor(new URL(request.url).pathname);
28874
- if (typeof responder !== "object" || !("resource" in responder)) return new Response(null, { status: 404 });
28875
+ const url = new URL(request.url);
28876
+ const responder = this.responderFor(url.pathname);
28877
+ if (responder === void 0 || !("resource" in responder)) return new Response(null, { status: 404 });
28878
+ if (this.authPolicy(responder.auth, {
28879
+ headers: request.headers,
28880
+ method: request.method,
28881
+ url,
28882
+ transport: "http",
28883
+ signal: request.signal
28884
+ }) === "deny") return new Response(null, { status: 403 });
28875
28885
  const response = await responder.resource(request, peer);
28876
28886
  if (request.method === "GET") return response;
28877
28887
  await response.body?.cancel();
@@ -28907,13 +28917,45 @@ var WebServer = class extends Service {
28907
28917
  res.end();
28908
28918
  return;
28909
28919
  }
28910
- if (typeof responder === "function") await responder(req, res);
28911
- else if ("resource" in responder) await this.serveResource(req, res, target, responder.resource);
28912
- else await responder.handler(req, res);
28920
+ const abort = new AbortController();
28921
+ const cleanup = () => {
28922
+ res.off("close", onClose);
28923
+ res.off("finish", cleanup);
28924
+ };
28925
+ const onClose = () => {
28926
+ abort.abort();
28927
+ cleanup();
28928
+ };
28929
+ res.once("close", onClose);
28930
+ res.once("finish", cleanup);
28931
+ try {
28932
+ const request = {
28933
+ headers: req.headers,
28934
+ method: req.method,
28935
+ url: target,
28936
+ transport: "http",
28937
+ signal: abort.signal,
28938
+ socket: req.socket
28939
+ };
28940
+ const admission = this.authorize(request, responder.auth);
28941
+ if (abort.signal.aborted || request.socket?.destroyed) return;
28942
+ if (admission.kind === "deny") {
28943
+ res.writeHead(admission.status, { "Cache-Control": "no-store" });
28944
+ res.end(admission.status === 401 ? "unauthorized" : admission.status === 503 ? "authentication unavailable" : "forbidden");
28945
+ return;
28946
+ }
28947
+ if ("resource" in responder) await this.serveResource(req, res, target, responder.resource, abort.signal, admission.peer);
28948
+ else await responder.handler(req, res, admission.peer);
28949
+ } finally {
28950
+ if (res.writableEnded || abort.signal.aborted) cleanup();
28951
+ }
28913
28952
  };
28914
28953
  const listener = (req, res) => {
28915
28954
  const next = () => {
28916
- handle(req, res).catch((err) => {
28955
+ const dispatch = async () => {
28956
+ await this.ctx.waterfall(this, "webserver/request", req, res, () => handle(req, res));
28957
+ };
28958
+ dispatch().catch((err) => {
28917
28959
  this.ctx.logger.warn(err instanceof Error ? err : new Error(String(err)));
28918
28960
  if (res.headersSent) {
28919
28961
  res.destroy();
@@ -28959,9 +29001,11 @@ var WebServer = class extends Service {
28959
29001
  this.upgradedSockets.delete(socket);
28960
29002
  });
28961
29003
  let route;
29004
+ let target;
28962
29005
  try {
28963
29006
  /* v8 ignore next -- node:http always sets url on server requests. */
28964
- route = this.upgrades.get(new URL(req.url ?? "/", "http://x").pathname);
29007
+ target = new URL(req.url ?? "/", "http://x");
29008
+ route = this.upgrades.get(target.pathname);
28965
29009
  } catch (error) {
28966
29010
  this.ctx.logger.warn(error instanceof Error ? error : new Error(String(error)));
28967
29011
  socket.destroy();
@@ -28972,8 +29016,36 @@ var WebServer = class extends Service {
28972
29016
  return;
28973
29017
  }
28974
29018
  this.upgradedSockets.add(socket);
29019
+ const abort = new AbortController();
29020
+ socket.once("close", () => {
29021
+ abort.abort();
29022
+ });
28975
29023
  try {
28976
- Promise.resolve(route.handler(req, socket, head)).catch((error) => {
29024
+ const admission = this.authorize({
29025
+ headers: req.headers,
29026
+ method: req.method,
29027
+ url: target,
29028
+ transport: "upgrade",
29029
+ signal: abort.signal,
29030
+ socket
29031
+ }, route.auth);
29032
+ if (abort.signal.aborted || socket.destroyed) return;
29033
+ if (admission.kind === "deny") {
29034
+ const reason = {
29035
+ 401: "Unauthorized",
29036
+ 403: "Forbidden",
29037
+ 503: "Service Unavailable"
29038
+ }[admission.status];
29039
+ const body = reason.toLowerCase();
29040
+ socket.end(`HTTP/1.1 ${String(admission.status)} ${reason}\r\nConnection: close\r\nCache-Control: no-store\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: ${String(Buffer.byteLength(body))}\r\n\r\n${body}`);
29041
+ return;
29042
+ }
29043
+ const dispatch = async () => {
29044
+ await this.ctx.waterfall(this, "webserver/upgrade", req, socket, head, admission.peer, async () => {
29045
+ if (!socket.destroyed) await route.handler(req, socket, head, admission.peer);
29046
+ });
29047
+ };
29048
+ dispatch().catch((error) => {
28977
29049
  this.ctx.logger.warn(error instanceof Error ? error : new Error(String(error)));
28978
29050
  socket.destroy();
28979
29051
  });
@@ -29017,6 +29089,31 @@ var WebServer = class extends Service {
29017
29089
  responderFor(pathname) {
29018
29090
  return this.match(pathname) ?? this.fallback;
29019
29091
  }
29092
+ authPolicy(auth, request) {
29093
+ return typeof auth === "function" ? auth(request) : auth ?? "required";
29094
+ }
29095
+ authorize(request, auth) {
29096
+ const policy = this.authPolicy(auth, request);
29097
+ if (policy === "deny") return {
29098
+ kind: "deny",
29099
+ status: 403
29100
+ };
29101
+ const admission = this.ctx.bail(this, "webserver/authorize", request, policy) ?? (policy === "anonymous" ? { kind: "allow" } : {
29102
+ kind: "deny",
29103
+ status: 503
29104
+ });
29105
+ if (admission.kind === "allow") {
29106
+ if (policy === "required" && admission.peer === void 0) return {
29107
+ kind: "deny",
29108
+ status: 503
29109
+ };
29110
+ if (admission.peer?.signal.aborted) return {
29111
+ kind: "deny",
29112
+ status: 401
29113
+ };
29114
+ }
29115
+ return admission;
29116
+ }
29020
29117
  /** Longest-prefix-wins over the prefix table after an exact-table miss. */
29021
29118
  match(pathname) {
29022
29119
  const exact = this.exact.get(pathname);
@@ -29031,13 +29128,13 @@ var WebServer = class extends Service {
29031
29128
  /**
29032
29129
  * Answer one HTTP exchange from a resource. Only GET and HEAD reach it;
29033
29130
  * any other method receives 405 and its request body is never read. The
29034
- * request becomes a Fetch `Request` without a Peer whose URL is the request
29131
+ * request becomes a Fetch `Request` whose URL is the request
29035
29132
  * target resolved against a placeholder origin, as the match reads it; the
29036
29133
  * client's authority stays only in the `host` header. The connection
29037
29134
  * closing before this method ends the response aborts the request signal
29038
29135
  * and cancels the response body.
29039
29136
  */
29040
- async serveResource(req, res, target, resource) {
29137
+ async serveResource(req, res, target, resource, signal, peer) {
29041
29138
  if (req.method !== "GET" && req.method !== "HEAD") {
29042
29139
  res.writeHead(405, { allow: RESOURCE_METHODS });
29043
29140
  res.end();
@@ -29045,25 +29142,12 @@ var WebServer = class extends Service {
29045
29142
  }
29046
29143
  const headers = new Headers();
29047
29144
  for (const [name, value] of Object.entries(req.headers)) for (const item of typeof value === "string" ? [value] : value ?? []) headers.append(name, item);
29048
- const abort = new AbortController();
29049
- let ended = false;
29050
- const onClose = () => {
29051
- if (!ended) abort.abort();
29052
- };
29053
- res.on("close", onClose);
29054
- try {
29055
- await writeResponse(res, await resource(new Request(target, {
29056
- method: req.method,
29057
- headers,
29058
- signal: abort.signal
29059
- })), req.method === "HEAD", abort.signal);
29060
- if (!abort.signal.aborted) {
29061
- ended = true;
29062
- res.end();
29063
- }
29064
- } finally {
29065
- res.off("close", onClose);
29066
- }
29145
+ await writeResponse(res, await resource(new Request(target, {
29146
+ method: req.method,
29147
+ headers,
29148
+ signal
29149
+ }), peer), req.method === "HEAD", signal);
29150
+ if (!signal.aborted) res.end();
29067
29151
  }
29068
29152
  /**
29069
29153
  * Run an index.html body through the registered taps in registration order
@@ -29086,7 +29170,7 @@ var WebServer = class extends Service {
29086
29170
  */
29087
29171
  collectIndexInjections(peer) {
29088
29172
  const table = [];
29089
- this.ctx.emit("webserver/index-inject", table, peer);
29173
+ this.ctx.emit(this, "webserver/index-inject", table, peer);
29090
29174
  return table;
29091
29175
  }
29092
29176
  /**
@@ -75,10 +75,13 @@ export declare function mediaTypeOfSignature(head: Buffer): string | undefined;
75
75
  *
76
76
  * The whitelist is the first decision and it is not this function's: a caller
77
77
  * hands over a path it already matched byte for byte. What is left is whether the
78
- * file at that path is still the one the session named — `realpath()` has to
79
- * return the same string, so a symlink pointing anywhere else is refused rather
80
- * than followed — whether it is a regular file at all, and whether its own first
81
- * bytes are one of the four image signatures. The extension is never consulted: a
78
+ * name at that path is still the file the session named — the name itself must not
79
+ * be a link, so a symlink pointing anywhere else is refused rather than followed —
80
+ * whether it is a regular file at all, and whether its own first bytes are one of
81
+ * the four image signatures. The link test reads the entry itself (`lstat`) rather
82
+ * than resolving it: comparing `realpath()` against the stored string rejects every
83
+ * 8.3 short name (Windows temp directories are `RUNNER~1`) and every macOS `/var`
84
+ * path, where two spellings of one file are not a substitution attack. The extension is never consulted: a
82
85
  * `.png` that is not a PNG is refused, and a PNG named `.txt` is served.
83
86
  *
84
87
  * The answer's `image` is the reference the session's transcript named; this
@@ -501,8 +501,9 @@ export declare const CODE_UI_LIST_UNAVAILABLE = "ui-list-unavailable";
501
501
  export declare const CODE_SESSION_NOT_FOLLOWABLE = "session-not-followable";
502
502
  /**
503
503
  * 该路径不在这次会话自己的持久 transcript 里:白名单只有两处来源,客户端自选的路径
504
- * 一律不在其中。`realpath()` 解析结果不再等于该字符串时也回这一码——那个路径已经
505
- * 指向别处,就不再是这次会话点过的那个文件。
504
+ * 一律不在其中。该名字本身是个链接时也回这一码——它已被换成指向别处的一层门面,就
505
+ * 不再是这次会话点过的那个文件。判据看的是条目自身(`lstat`),不比较 `realpath()`
506
+ * 的解析结果:同一个文件的两种写法(Windows 8.3 短名、macOS `/var` 前缀)不等于换目标。
506
507
  */
507
508
  export declare const CODE_PATH_NOT_IN_SESSION = "path-not-in-session";
508
509
  /**
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@linxin666/dsh-remote-bridge",
3
3
  "description": "DSH remote bridge: the host half serves protocol v1 over one exact /ios/v1 WebSocket route — session roster, history baseline, prompting, live turn increments with tool activity, interrupt, and in-process agent approval — to a device paired by a one-shot bootstrap token and a P-256 challenge-response, with the token endpoint admitted only from this machine; the browser half adds one sidebar-foot panel that shows that same one-shot token as a QR symbol and draws an interactive card inside the tool row without answering it, and adds no wire surface",
4
- "version": "0.4.20",
4
+ "version": "0.4.22",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",
7
7
  "engines": {
8
- "node": ">=24.9.0"
8
+ "node": "^24.22.3 || >=26.13.1"
9
9
  },
10
10
  "main": "lib/index.js",
11
11
  "types": "lib/types/index.d.ts",
@@ -37,7 +37,7 @@
37
37
  "license": "Apache-2.0",
38
38
  "dsh": {
39
39
  "engines": {
40
- "dsh": ">=0.2.2-rc.3"
40
+ "dsh": ">=0.2.3-alpha.2"
41
41
  },
42
42
  "bundle": {
43
43
  "patch": "./cordis.patch.yml"
@@ -57,23 +57,23 @@
57
57
  "yaml": "^2.8.2"
58
58
  },
59
59
  "peerDependencies": {
60
- "@deepseek-ai/dsh": ">=0.2.2-rc.3",
60
+ "@deepseek-ai/dsh": ">=0.2.3-alpha.2",
61
61
  "react": "^18.2.0",
62
62
  "react-dom": "^18.2.0"
63
63
  },
64
64
  "devDependencies": {
65
- "@deepseek-ai/cordis": "^4.1.0",
66
- "@deepseek-ai/dsh-api-gateway": "^0.2.2-rc.3",
67
- "@deepseek-ai/dsh-client-locale": "^0.2.2-rc.3",
68
- "@deepseek-ai/dsh-client-ui-primitives": "^0.2.2-rc.3",
69
- "@deepseek-ai/dsh-client-ui-renderer": "^0.2.2-rc.3",
70
- "@deepseek-ai/dsh-client-ui-sidebar": "^0.2.2-rc.3",
71
- "@deepseek-ai/dsh-client-ui-slots": "^0.2.2-rc.3",
72
- "@deepseek-ai/dsh-host-webserver": "^0.2.2-rc.3",
73
- "@deepseek-ai/dsh-session": "^0.2.2-rc.3",
74
- "@deepseek-ai/dsh-tools": "^0.2.2-rc.3",
75
- "@deepseek-ai/dsh-typert-protocol": "^0.2.2-rc.3",
76
- "@deepseek-ai/dsh-user-approval": "^0.2.2-rc.3",
65
+ "@deepseek-ai/cordis": "~4.2.0-alpha.1",
66
+ "@deepseek-ai/dsh-api-gateway": "^0.2.3-alpha.2",
67
+ "@deepseek-ai/dsh-client-locale": "^0.2.3-alpha.2",
68
+ "@deepseek-ai/dsh-client-ui-primitives": "^0.2.3-alpha.2",
69
+ "@deepseek-ai/dsh-client-ui-renderer": "^0.2.3-alpha.2",
70
+ "@deepseek-ai/dsh-client-ui-sidebar": "^0.2.3-alpha.2",
71
+ "@deepseek-ai/dsh-client-ui-slots": "^0.2.3-alpha.2",
72
+ "@deepseek-ai/dsh-host-webserver": "^0.2.3-alpha.2",
73
+ "@deepseek-ai/dsh-session": "^0.2.3-alpha.2",
74
+ "@deepseek-ai/dsh-tools": "^0.2.3-alpha.2",
75
+ "@deepseek-ai/dsh-typert-protocol": "^0.2.3-alpha.2",
76
+ "@deepseek-ai/dsh-user-approval": "^0.2.3-alpha.2",
77
77
  "@testing-library/dom": "^10.4.1",
78
78
  "@testing-library/react": "^16.3.2",
79
79
  "@types/node": "^22.20.0",