MCPcopy
hub / github.com/codedogQBY/ReadAny / LANBackend

Class LANBackend

packages/core/src/sync/lan-backend.ts:36–160  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34 * This is a client-side implementation that connects to a LAN server.
35 */
36export class LANBackend implements ISyncBackend {
37 readonly type = "lan" as const;
38 private serverUrl: string;
39 private pairCode: string;
40 private deviceName: string;
41 private abortController: AbortController | null = null;
42
43 constructor(serverUrl: string, pairCode: string, deviceName: string) {
44 this.serverUrl = serverUrl.replace(/\/+$/, "");
45 this.pairCode = pairCode;
46 this.deviceName = deviceName;
47 }
48
49 private getFetchFn() {
50 const platform = getPlatformService();
51 return platform.fetch ? platform.fetch.bind(platform) : globalThis.fetch.bind(globalThis);
52 }
53
54 async testConnection(): Promise<boolean> {
55 try {
56 const fetchFn = this.getFetchFn();
57 const response = await fetchFn(`${this.serverUrl}/ping`, {
58 method: "GET",
59 headers: {
60 "X-Pair-Code": this.pairCode,
61 },
62 });
63 return response.ok;
64 } catch {
65 return false;
66 }
67 }
68
69 async ensureDirectories(): Promise<void> {
70 // No-op for LAN - server handles this
71 }
72
73 async put(_path: string, _data: Uint8Array): Promise<void> {
74 throw new Error("LAN backend does not support upload (receive-only)");
75 }
76
77 async get(path: string): Promise<Uint8Array> {
78 const fetchFn = this.getFetchFn();
79 const response = await fetchFn(`${this.serverUrl}/file${path}`, {
80 method: "GET",
81 headers: {
82 "X-Pair-Code": this.pairCode,
83 },
84 });
85
86 if (!response.ok) {
87 throw new Error(`LAN GET failed for ${path}: ${response.status}`);
88 }
89
90 const buffer = await response.arrayBuffer();
91 return new Uint8Array(buffer);
92 }
93

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected