Sign in

Tim Smart

@timsmart.co
223 followers 8 following 9 posts

I hack on effect.website

PostsRepliesMedia
Tim Smart @timsmart.co · 16/12/2024
🤔
340
Tim Smart @timsmart.co · 05/12/2024
import * as Fs from "node:fs/promises"

export async function* readReverse(path: string, chunkSize = 1024) {
  const file = await Fs.open(path, "r")
  const stat = await file.stat()

  let pos = stat.size
  while (true) {
    pos = Math.max(0, pos - chunkSize)

    const chunk = Buffer.alloc(chunkSize)
    const result = await file.read(chunk, 0, chunkSize, pos)
    yield chunk.subarray(0, result.bytesRead).reverse()

    if (pos === 0) break
  }
}
230