truncate
Truncate or extend a file to the given length. If the file is longer, extra bytes are discarded; if shorter, it is zero-padded (or platform behavior).
Basic usage
import { truncate } from '@vooya/fs'
await truncate('./file.txt', 100) // trim or extend to 100 bytes
await truncate('./file.txt') // truncate to 0Methods
truncate(path, len?)
Async. Returns Promise<void>.
| Argument | Type | Description |
|---|---|---|
path | string | File path. |
len | number | Optional. Target length in bytes; default 0. |
truncateSync(path, len?)
Sync. Same arguments; throws on error.
Performance
Single syscall; on par with Node.js. See Benchmarks.
Notes
- Extension: When extending, behavior may be platform-dependent (zero-fill or sparse); same as Node.js.
- Negative lengths: Current supported Node versions clamp negative values to
0; Vooya FS matches that observed behavior. - Known gap: Missing-path errors currently have Node-like messages, but do not expose Node-style
code,path, andsyscallfields yet. - Unsupported Node inputs/APIs:
Bufferpaths,URLpaths, and file-descriptorftruncateAPIs are not part of the current Vooya FStruncatesurface.
Competitor measurements
See the full truncate comparison table for Node 22/24, peer libraries, sync/Promise modes, small and batch workloads, execution routes and measurement limits.
Last updated on