MCPcopy
hub / github.com/coder/mux / write

Method write

src/node/utils/sessionFile.ts:68–90  ·  view source on GitHub ↗

* Write JSON file to workspace session directory with file locking. * Creates session directory if it doesn't exist. * * If options.shouldWrite returns false, the write is skipped (and the session * directory is not created).

(
    workspaceId: string,
    data: T,
    options?: SessionFileWriteOptions
  )

Source from the content-addressed store, hash-verified

66 * directory is not created).
67 */
68 async write(
69 workspaceId: string,
70 data: T,
71 options?: SessionFileWriteOptions
72 ): Promise<Result<void>> {
73 return this.fileLocks.withLock(workspaceId, async () => {
74 try {
75 if (options?.shouldWrite && !options.shouldWrite()) {
76 return Ok(undefined);
77 }
78
79 const sessionDir = this.config.getSessionDir(workspaceId);
80 await fs.mkdir(sessionDir, { recursive: true });
81 const filePath = this.getFilePath(workspaceId);
82 // Atomic write prevents corruption if app crashes mid-write
83 await writeFileAtomic(filePath, JSON.stringify(data, null, 2));
84 return Ok(undefined);
85 } catch (error) {
86 const message = getErrorMessage(error);
87 return Err(`Failed to write ${this.fileName}: ${message}`);
88 }
89 });
90 }
91
92 /**
93 * Delete JSON file from workspace session directory with file locking.

Callers

nothing calls this directly

Calls 6

getFilePathMethod · 0.95
OkFunction · 0.90
getErrorMessageFunction · 0.90
ErrFunction · 0.90
withLockMethod · 0.80
getSessionDirMethod · 0.65

Tested by

no test coverage detected