( filePath: string, fileSize: number, buf: Buffer, )
| 213 | * Returns `{ head: '', tail: '' }` on any error. |
| 214 | */ |
| 215 | export async function readHeadAndTail( |
| 216 | filePath: string, |
| 217 | fileSize: number, |
| 218 | buf: Buffer, |
| 219 | ): Promise<{ head: string; tail: string }> { |
| 220 | try { |
| 221 | const fh = await fsOpen(filePath, 'r') |
| 222 | try { |
| 223 | const headResult = await fh.read(buf, 0, LITE_READ_BUF_SIZE, 0) |
| 224 | if (headResult.bytesRead === 0) return { head: '', tail: '' } |
| 225 | |
| 226 | const head = buf.toString('utf8', 0, headResult.bytesRead) |
| 227 | |
| 228 | const tailOffset = Math.max(0, fileSize - LITE_READ_BUF_SIZE) |
| 229 | let tail = head |
| 230 | if (tailOffset > 0) { |
| 231 | const tailResult = await fh.read(buf, 0, LITE_READ_BUF_SIZE, tailOffset) |
| 232 | tail = buf.toString('utf8', 0, tailResult.bytesRead) |
| 233 | } |
| 234 | |
| 235 | return { head, tail } |
| 236 | } finally { |
| 237 | await fh.close() |
| 238 | } |
| 239 | } catch { |
| 240 | return { head: '', tail: '' } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | export type LiteSessionFile = { |
| 245 | mtime: number |
no test coverage detected