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

Method get

packages/core/src/sync/lan-server.ts:104–151  ·  view source on GitHub ↗
(path: string)

Source from the content-addressed store, hash-verified

102 }
103
104 async get(path: string): Promise<Uint8Array> {
105 const platform = getPlatformService();
106 const adapter = getSyncAdapter();
107
108 if (path.startsWith(`${LAN_SYNC_DIR}/device-`) && path.endsWith(".json")) {
109 const snapshot = await this.getCurrentDeviceSnapshot();
110 const expectedPath = getLanDeviceSnapshotPath(snapshot.deviceId);
111 if (path !== expectedPath) {
112 const err = new Error("File not found");
113 (err as any).statusCode = 404;
114 throw err;
115 }
116 return new TextEncoder().encode(JSON.stringify(snapshot));
117 }
118
119 const resolvedPath = await this.mapVirtualPath(path);
120
121 // Special handling for database to ensure consistency (snapshot via vacuum)
122 const dbPath = await adapter.getDatabasePath();
123 if (resolvedPath === dbPath) {
124 const tempDir = await adapter.getTempDir();
125 const snapshotPath = adapter.joinPath(tempDir, `sync_snapshot_${Date.now()}.db`);
126 try {
127 console.log(`[LAN Server] Creating DB snapshot for sync at ${snapshotPath}...`);
128 await adapter.vacuumInto(snapshotPath);
129 const data = await platform.readFile(snapshotPath);
130 await adapter.deleteFile(snapshotPath);
131 return data;
132 } catch (e) {
133 console.error(`[LAN Server] Failed to snapshot database:`, e);
134 // Fallback to direct read if vacuum fails (might be inconsistent but better than nothing)
135 return await platform.readFile(resolvedPath);
136 }
137 }
138
139 if (!(await adapter.fileExists(resolvedPath))) {
140 const err = new Error("File not found");
141 (err as any).statusCode = 404;
142 throw err;
143 }
144
145 try {
146 return await platform.readFile(resolvedPath);
147 } catch (e) {
148 console.error(`[LAN Server] Failed to read file ${resolvedPath}:`, e);
149 throw e;
150 }
151 }
152
153 async getJSON<T>(path: string): Promise<T | null> {
154 try {

Callers 1

getJSONMethod · 0.95

Calls 13

mapVirtualPathMethod · 0.95
getPlatformServiceFunction · 0.90
getSyncAdapterFunction · 0.90
getLanDeviceSnapshotPathFunction · 0.85
encodeMethod · 0.80
getDatabasePathMethod · 0.65
getTempDirMethod · 0.65
joinPathMethod · 0.65
vacuumIntoMethod · 0.65
readFileMethod · 0.65
deleteFileMethod · 0.65

Tested by

no test coverage detected