@eryx/tempfile Module

JSON

Temporary files and directories.

open() returns a file-like object with an explicit :close() method. openDir() returns a directory handle with path and :close().

Warning

Temporary resources are explicit-lifecycle. You must call :close() on returned handles to ensure cleanup. Unlike some runtimes, this module does not rely on GC finalizers.

Summary

Classes

path: string
file: fs.File
path: string
TempDir:close()()

Functions

TempDir:close()()
tempfile.open(name: string?, mode: string?)TempFile
tempfile.withTempFile<T>(fn: ((file: TempFile) → ...T), options: { name: string?, mode: string? }?)...T
tempfile.withTempDir<T>(fn: ((path: string) → ...T), name: string?)...T

API Reference

Classes

TempFile

Properties

path: string

Absolute path to the temp file.

file: fs.File

The underlying open file

TempFile:close

Closes the file handle (if open) and deletes the file path.

TempFile:close()()

TempDir

Represents a temporary directory handle.

Use path for filesystem operations and call :close() to remove it recursively.

Properties

path: string

Absolute path to the temp directory.

TempDir:close

Removes the directory recursively.

TempDir:close()()

Functions

TempFile:close

TempFile:close()()

TempDir:close

TempDir:close()()

tempfile.tempdir

Returns the process temporary directory.

tempfile.tempdir()string

Returns

string Absolute path to the OS temp directory.

tempfile.mktemp

Creates a unique temp file, then immediately closes and deletes it.

This is useful when you only need a collision-resistant path string.

tempfile.mktemp(name: string?)string

Parameters

name: string?

string? Optional name/template prefix. If it includes X characters, each X is replaced with random alphanumeric characters.

Returns

string A unique temp file path that does not exist on return.

tempfile.open

Creates and opens a unique temp file.

The returned handle behaves like fs.File and also supports :close() to close and delete the file path.

tempfile.open(name: string?, mode: string?)TempFile

Parameters

name: string?

string? Optional name/template prefix. If it includes X characters, each X is replaced with random alphanumeric characters.

mode: string?

string? File mode ("r", "w", "a", "r+", "w+", "a+"). Defaults to "w+".

Returns

TempFile Open temp file handle with explicit cleanup methods.

tempfile.openDir

Creates a unique temporary directory.

tempfile.openDir(name: string?)TempDir

Parameters

name: string?

string? Optional name/template prefix. If it includes X characters, each X is replaced with random alphanumeric characters.

Returns

TempDir Directory handle with path and explicit cleanup methods.

tempfile.withTempFile

Creates a temp file, calls fn(file), and always cleans it up.

file:close() is guaranteed after callback completion, including error paths. Any callback error is rethrown.

tempfile.withTempFile<T>(fn: ((file: TempFile) → ...T), options: { name: string?, mode: string? }?)...T

Parameters

fn: ((file: TempFile) → ...T)

(file: TempFile) -> T Callback that uses the temp file handle.

options: { name: string?, mode: string? }?

{ name: string?, mode: string? }? Optional creation options.

Returns

...T

T The callback return value.

tempfile.withTempDir

Creates a temp directory, calls fn(path), and always removes it.

Directory cleanup is guaranteed after callback completion, including error paths. Any callback error is rethrown.

tempfile.withTempDir<T>(fn: ((path: string) → ...T), name: string?)...T

Parameters

fn: ((path: string) → ...T)

(path: string) -> T Callback that receives the temp directory path.

name: string?

string? Optional name/template prefix.

Returns

...T

T The callback return value.