* Load a YJS document from disk. * Returns null if the document doesn't exist.
(id: string)
| 216 | * Returns null if the document doesn't exist. |
| 217 | */ |
| 218 | async function handleLoad(id: string): Promise<Uint8Array | null> { |
| 219 | const filePath = getDocPath(id) |
| 220 | const legacyNestedPath = getLegacyNestedDocPath(id) |
| 221 | |
| 222 | try { |
| 223 | const data = new Uint8Array(await fs.readFile(filePath)) |
| 224 | if (isExpectedTaskDoc(id, data)) { |
| 225 | logger.debug(`[YjsStorage] Loaded document: ${id} (${data.length} bytes)`) |
| 226 | return data |
| 227 | } |
| 228 | |
| 229 | try { |
| 230 | const legacyData = new Uint8Array(await fs.readFile(legacyNestedPath)) |
| 231 | if (isExpectedTaskDoc(id, legacyData)) { |
| 232 | await writeMigratedDoc(filePath, legacyData) |
| 233 | logger.warn(`[YjsStorage] Recovered task document from legacy nested path: ${id}`) |
| 234 | return legacyData |
| 235 | } |
| 236 | } catch (legacyError) { |
| 237 | if ((legacyError as NodeJS.ErrnoException).code !== "ENOENT") { |
| 238 | logger.warn(`[YjsStorage] Failed to inspect legacy nested document for ${id}:`, legacyError) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | logger.warn(`[YjsStorage] Loaded task document with mismatched metadata: ${id}`) |
| 243 | return data |
| 244 | } catch (error) { |
| 245 | if ((error as NodeJS.ErrnoException).code === "ENOENT") { |
| 246 | try { |
| 247 | const legacyData = new Uint8Array(await fs.readFile(legacyNestedPath)) |
| 248 | if (isExpectedTaskDoc(id, legacyData)) { |
| 249 | await writeMigratedDoc(filePath, legacyData) |
| 250 | logger.warn(`[YjsStorage] Migrated task document from legacy nested path: ${id}`) |
| 251 | return legacyData |
| 252 | } |
| 253 | } catch (legacyError) { |
| 254 | if ((legacyError as NodeJS.ErrnoException).code !== "ENOENT") { |
| 255 | logger.warn(`[YjsStorage] Failed to load legacy nested document for ${id}:`, legacyError) |
| 256 | } |
| 257 | } |
| 258 | return null |
| 259 | } |
| 260 | throw error |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | export async function loadYjsDocument(id: string): Promise<Uint8Array | null> { |
| 265 | return handleLoad(id) |
no test coverage detected