dsh-local-link 0.2.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.mjs CHANGED
@@ -333,6 +333,7 @@ const DIAGNOSTIC_CODES = /* @__PURE__ */ new Set([
333
333
  "REQUEST_REJECTED",
334
334
  "AUTH_REQUIRED",
335
335
  "HTTP_UPSTREAM_ERROR",
336
+ "BROWSER_AUTH_HANDOFF_FAILED",
336
337
  "INDEX_REWRITE_ERROR",
337
338
  "WS_REJECTED",
338
339
  "WS_UPSTREAM_ERROR",
@@ -464,8 +465,20 @@ function ensureResponsiveViewport(html) {
464
465
  const replacement = content.test(match[0]) ? match[0].replace(content, (_whole, quote, value) => `content=${quote}${value},viewport-fit=cover${quote}`) : match[0].replace(/\s*\/?>$/u, " content=\"width=device-width,initial-scale=1,viewport-fit=cover\">");
465
466
  return `${html.slice(0, match.index)}${replacement}${html.slice(match.index + match[0].length)}`;
466
467
  }
467
- function removeHostDirectoryPickers(entries) {
468
- for (let index = entries.length - 1; index >= 0; index -= 1) if (HOST_DIRECTORY_PICKER_IDS.has(String(entries[index]?.id))) entries.splice(index, 1);
468
+ function removeHostDirectoryPickers(manifest, entries) {
469
+ const removed = /* @__PURE__ */ new Set();
470
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
471
+ const id = String(entries[index]?.id);
472
+ if (!HOST_DIRECTORY_PICKER_IDS.has(id)) continue;
473
+ removed.add(id);
474
+ entries.splice(index, 1);
475
+ }
476
+ if (!Array.isArray(manifest.batches) || removed.size === 0) return;
477
+ for (const value of manifest.batches) {
478
+ if (value === null || typeof value !== "object" || Array.isArray(value)) continue;
479
+ const batch = value;
480
+ if (Array.isArray(batch.entries)) batch.entries = batch.entries.filter((id) => !removed.has(String(id)));
481
+ }
469
482
  }
470
483
  /**
471
484
  * Keep the shipped Harness root and every dynamic child slot. The gateway only
@@ -483,7 +496,7 @@ function rewriteAuthenticatedIndex(html) {
483
496
  if (!Array.isArray(manifest.entries)) throw new Error("DSH boot manifest has no entries");
484
497
  const entries = manifest.entries;
485
498
  if (!entries.some((entry) => entry.id === CLIENT_ID)) throw new Error("dsh-local-link client is absent from DSH boot manifest");
486
- removeHostDirectoryPickers(entries);
499
+ removeHostDirectoryPickers(manifest, entries);
487
500
  return ensureResponsiveViewport(`${html.slice(0, assignment.index)}${RANDOM_UUID_POLYFILL}${assignment[0]}${JSON.stringify(manifest)};${html.slice(scriptEnd)}`);
488
501
  }
489
502
  //#endregion
@@ -507,6 +520,7 @@ var pair_timeout = "连接超时。请生成新二维码后重试。";
507
520
  //#endregion
508
521
  //#region src/gateway/pair-page.ts
509
522
  const PAIR_PATH = "/__dsh-local-link/pair";
523
+ const CONNECT_PATH = "/__dsh-local-link/connect";
510
524
  const pairCopies = {
511
525
  en: {
512
526
  title: pair_title$1,
@@ -567,7 +581,7 @@ const PAIR_PAGE = `<!doctype html>
567
581
  request.send(JSON.stringify({token,device:{type,browser}}));
568
582
  });
569
583
  if(sessionId)try{localStorage.setItem('dsh.sessions.current',JSON.stringify({sessionId}));}catch{}
570
- location.replace('/');
584
+ location.replace('${CONNECT_PATH}');
571
585
  }catch(error){stop(error instanceof Error?error.message:copy.failed);}})();
572
586
  <\/script>
573
587
  </html>`;
@@ -575,7 +589,11 @@ const PAIR_PAGE = `<!doctype html>
575
589
  //#region src/gateway/local-gateway.ts
576
590
  const COOKIE_NAME = "dsh_local_link_device";
577
591
  const MAX_PAIR_BODY = 4096;
578
- const EVENT_PATHS = /* @__PURE__ */ new Set(["/api/events.mux", "/api/events.host"]);
592
+ const STREAM_PATHS = /* @__PURE__ */ new Set([
593
+ "/api/events.mux",
594
+ "/api/events.host",
595
+ "/api/remote.mux"
596
+ ]);
579
597
  const HOP_BY_HOP_HEADERS = /* @__PURE__ */ new Set([
580
598
  "connection",
581
599
  "keep-alive",
@@ -624,6 +642,10 @@ function send(response, status, body, type = "text/plain; charset=utf-8") {
624
642
  });
625
643
  response.end(body);
626
644
  }
645
+ function upstreamCookieHeader(request) {
646
+ const cookies = (request.headers.cookie ?? "").split(";").map((part) => part.trim()).filter((part) => part !== "" && part.split("=", 1)[0] !== COOKIE_NAME);
647
+ return cookies.length > 0 ? cookies.join("; ") : void 0;
648
+ }
627
649
  function proxyRequestHeaders(request, websocket = false) {
628
650
  const headers = {};
629
651
  for (const [name, value] of Object.entries(request.headers)) {
@@ -631,13 +653,15 @@ function proxyRequestHeaders(request, websocket = false) {
631
653
  if (value === void 0 || lower === "cookie" || lower.startsWith("x-forwarded-") || !websocket && HOP_BY_HOP_HEADERS.has(lower)) continue;
632
654
  headers[lower] = value;
633
655
  }
656
+ const cookie = upstreamCookieHeader(request);
657
+ if (cookie !== void 0) headers.cookie = cookie;
634
658
  return headers;
635
659
  }
636
- function proxyResponseHeaders(headers) {
660
+ function proxyResponseHeaders(headers, forwardSetCookie = false) {
637
661
  const result = {};
638
662
  for (const [name, value] of Object.entries(headers)) {
639
663
  const lower = name.toLowerCase();
640
- if (value === void 0 || HOP_BY_HOP_HEADERS.has(lower) || lower === "set-cookie") continue;
664
+ if (value === void 0 || HOP_BY_HOP_HEADERS.has(lower) || lower === "set-cookie" && !forwardSetCookie) continue;
641
665
  result[lower] = value;
642
666
  }
643
667
  return result;
@@ -653,11 +677,14 @@ function serializeHeaders(headers) {
653
677
  }
654
678
  function requestKind(pathname) {
655
679
  if (pathname === "/") return "root";
656
- if (EVENT_PATHS.has(pathname)) return "events";
680
+ if (STREAM_PATHS.has(pathname)) return "events";
657
681
  if (pathname.startsWith("/api/")) return "api";
658
682
  if (/\.[a-z0-9]{1,8}$/iu.test(pathname)) return "asset";
659
683
  return "page";
660
684
  }
685
+ function supportedWebSocketTarget(target) {
686
+ return target.search === "" && STREAM_PATHS.has(target.pathname);
687
+ }
661
688
  var LocalGateway = class {
662
689
  config;
663
690
  devices;
@@ -666,6 +693,7 @@ var LocalGateway = class {
666
693
  upgradedSockets = /* @__PURE__ */ new Set();
667
694
  server;
668
695
  addresses = [];
696
+ browserAuthenticationUrl;
669
697
  constructor(config) {
670
698
  this.config = config;
671
699
  this.devices = new DeviceStore(config.stateFile, config.deviceTtlMs);
@@ -711,6 +739,12 @@ var LocalGateway = class {
711
739
  trustedAuthorities() {
712
740
  return Object.freeze(this.addresses.map((address) => formatAuthority(address, this.config.listenPort)));
713
741
  }
742
+ attachBrowserAuthentication(factory) {
743
+ this.browserAuthenticationUrl = factory;
744
+ return () => {
745
+ if (this.browserAuthenticationUrl === factory) this.browserAuthenticationUrl = void 0;
746
+ };
747
+ }
714
748
  diagnosticEvents() {
715
749
  return this.diagnostics.list();
716
750
  }
@@ -784,10 +818,36 @@ var LocalGateway = class {
784
818
  send(response, 401, "This browser is not paired. Open Local access in the computer sidebar and scan its QR code.");
785
819
  return;
786
820
  }
821
+ if (url.pathname === "/__dsh-local-link/connect" && request.method === "GET") {
822
+ const authenticationUrl = this.browserAuthenticationUrl;
823
+ if (authenticationUrl === void 0) {
824
+ response.writeHead(303, {
825
+ location: "/",
826
+ "cache-control": "no-store"
827
+ });
828
+ response.end();
829
+ return;
830
+ }
831
+ try {
832
+ const origin = new URL(`http://${request.headers.host ?? ""}`);
833
+ const target = new URL(authenticationUrl(origin.href));
834
+ if (target.origin !== origin.origin || target.pathname !== "/" || target.hash !== "") throw new Error("invalid authentication handoff");
835
+ response.writeHead(303, {
836
+ location: `${target.pathname}${target.search}`,
837
+ "cache-control": "no-store",
838
+ "referrer-policy": "no-referrer"
839
+ });
840
+ response.end();
841
+ } catch {
842
+ this.diagnostics.record("error", "BROWSER_AUTH_HANDOFF_FAILED", { reason: "invalid_target" });
843
+ send(response, 502, "DeepSeek Harness browser authentication is unavailable");
844
+ }
845
+ return;
846
+ }
787
847
  const interceptIndex = request.method === "GET" && url.pathname === "/";
788
- this.proxyHttp(request, response, interceptIndex);
848
+ this.proxyHttp(request, response, interceptIndex, interceptIndex && url.searchParams.has("token"));
789
849
  }
790
- proxyHttp(request$1, response, interceptIndex) {
850
+ proxyHttp(request$1, response, interceptIndex, browserTokenExchange) {
791
851
  const headers = proxyRequestHeaders(request$1);
792
852
  if (interceptIndex) headers["accept-encoding"] = "identity";
793
853
  const upstreamRequest = request({
@@ -799,7 +859,7 @@ var LocalGateway = class {
799
859
  agent: false
800
860
  }, (upstreamResponse) => {
801
861
  if (!interceptIndex || !String(upstreamResponse.headers["content-type"] ?? "").includes("text/html")) {
802
- response.writeHead(upstreamResponse.statusCode ?? 502, proxyResponseHeaders(upstreamResponse.headers));
862
+ response.writeHead(upstreamResponse.statusCode ?? 502, proxyResponseHeaders(upstreamResponse.headers, browserTokenExchange));
803
863
  upstreamResponse.pipe(response);
804
864
  return;
805
865
  }
@@ -837,8 +897,7 @@ var LocalGateway = class {
837
897
  client.end("HTTP/1.1 401 Unauthorized\r\nConnection: close\r\n\r\n");
838
898
  return;
839
899
  }
840
- const target = new URL(request.url ?? "/", this.config.upstreamOrigin);
841
- if (target.search !== "" || !EVENT_PATHS.has(target.pathname)) {
900
+ if (!supportedWebSocketTarget(new URL(request.url ?? "/", this.config.upstreamOrigin))) {
842
901
  this.diagnostics.record("warn", "WS_REJECTED", { reason: "unsupported_path" });
843
902
  client.end("HTTP/1.1 404 Not Found\r\nConnection: close\r\n\r\n");
844
903
  return;
@@ -912,6 +971,11 @@ async function apply(ctx, config) {
912
971
  const gateway = new LocalGateway(parseConfig(config));
913
972
  await gateway.start();
914
973
  ctx.provide("localLinkGateway", Object.freeze({ trustedHosts: gateway.trustedAuthorities() }));
974
+ ctx.inject(["connection"], (scope) => {
975
+ const connection = scope.connection;
976
+ if (typeof connection?.authenticatedUrl !== "function") return;
977
+ scope.effect(() => gateway.attachBrowserAuthentication((baseUrl) => connection.authenticatedUrl?.(baseUrl) ?? baseUrl), "dsh-local-link: Harness browser authentication handoff");
978
+ });
915
979
  const adminRoute = {
916
980
  kind: "prefix",
917
981
  path: ADMIN_PREFIX,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-local-link",
3
- "version": "0.2.1",
3
+ "version": "1.0.0",
4
4
  "description": "DeepSeek Harness plugin for lightweight LAN access to the current Web UI from phones, tablets, and other computers.",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -27,6 +27,7 @@
27
27
  "docs/images/*.jpg",
28
28
  "docs/images/*.png",
29
29
  "docs/ARCHITECTURE.md",
30
+ "docs/COMPATIBILITY.md",
30
31
  "docs/DIAGNOSTICS.md",
31
32
  "docs/MOBILE_VIEW.md",
32
33
  "docs/SECURITY.md",
@@ -54,7 +55,6 @@
54
55
  },
55
56
  "scripts": {
56
57
  "build": "tsdown",
57
- "clean": "node ./scripts/clean.mjs",
58
58
  "prepack": "npm run build",
59
59
  "release:check": "npm run verify && pnpm audit --prod && npm publish --dry-run",
60
60
  "test": "vitest run",
@@ -86,7 +86,7 @@
86
86
  "author": "DoNotEatMe",
87
87
  "peerDependencies": {
88
88
  "@deepseek-ai/cordis": "^4.0.1",
89
- "@deepseek-ai/dsh-host-webserver": "^0.1.1-rc.2",
89
+ "@deepseek-ai/dsh-host-webserver": ">=0.1.0-rc.8 <0.1.2 || 0.1.2-alpha.1",
90
90
  "react": "^18.2.0"
91
91
  },
92
92
  "peerDependenciesMeta": {
@@ -128,11 +128,11 @@
128
128
  },
129
129
  "repository": {
130
130
  "type": "git",
131
- "url": "git+https://github.com/DoNotEatMe/dsh-local-link.git"
131
+ "url": "git+https://github.com/donoteatme/dsh-local-link.git"
132
132
  },
133
133
  "packageManager": "pnpm@11.7.0",
134
134
  "bugs": {
135
- "url": "https://github.com/DoNotEatMe/dsh-local-link/issues"
135
+ "url": "https://github.com/donoteatme/dsh-local-link/issues"
136
136
  },
137
- "homepage": "https://github.com/DoNotEatMe/dsh-local-link#readme"
137
+ "homepage": "https://github.com/donoteatme/dsh-local-link#readme"
138
138
  }