( inboxPath: string, content: string, )
| 150 | } |
| 151 | |
| 152 | async function writeMailboxAtomic( |
| 153 | inboxPath: string, |
| 154 | content: string, |
| 155 | ): Promise<void> { |
| 156 | const bytes = Buffer.byteLength(content, 'utf8') |
| 157 | if (bytes > MAX_MAILBOX_FILE_BYTES) { |
| 158 | throw new Error( |
| 159 | `Compacted mailbox still exceeds ${MAX_MAILBOX_FILE_BYTES} bytes`, |
| 160 | ) |
| 161 | } |
| 162 | const tempPath = `${inboxPath}.${process.pid}.${randomBytes(8).toString('hex')}.tmp` |
| 163 | try { |
| 164 | await writeFile(tempPath, content, 'utf-8') |
| 165 | await rename(tempPath, inboxPath) |
| 166 | } catch (error) { |
| 167 | await unlink(tempPath).catch(() => undefined) |
| 168 | throw error |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | export function compactMailboxMessages( |
| 173 | messages: TeammateMessage[], |
no test coverage detected