Disposable Temporary Files

Built-in, native disposable temporary file manager integrated into __sys__.fs for creating short-lived scratch files with zero-config automatic cleanup.

XyPriss allows developers to generate temporary scratch files (such as PDF exports, image conversions, session tokens, or upload buffers) stored within an isolated user directory (__sys__.fs.tmpUserDir). All temporary files are safely tracked and automatically unlinked when TTL expires, when the server reloads or exits, or on demand.

Zero Node.js Dependency
Under the hood, the temporary file manager relies 100% on the native XyPriss system API via the XHSC (XyPriss Hyper-System Core) binary. Path resolution, directory creation, file writing, statistics, and deletion are executed off the Node.js event loop.

Automatic Cleanup Triggers

TTL Timeout

Pass a duration string (e.g. "30s", "5m", "1h"). Background timers automatically unlink expired files.

Process Exit / Reload

Process termination hooks (exit, SIGINT, SIGTERM) or live reloads purge session scratch files automatically.

Manual On-Demand

Invoke .cleanup() or .remove() on the returned file handle to delete the file immediately in a finally block.

API Reference

1. Sub-API Namespace (__sys__.fs.tmp)

  • __sys__.fs.tmp.write(content, options?): Promise<TempFileResult> — Write temporary file (async).
  • __sys__.fs.tmp.writeSync(content, options?): TempFileResult — Write temporary file (sync).
  • __sys__.fs.tmp.read(path): Promise<string> — Read temporary file (async).
  • __sys__.fs.tmp.readSync(path): string — Read temporary file (sync).
  • __sys__.fs.tmp.remove(path): boolean — Remove a specific temporary file on disk.
  • __sys__.fs.tmp.dir: string — Access the isolated user temporary directory (tmpUserDir).
  • __sys__.fs.tmp.cleanup(): number / purge(): number — Purge all active temporary files in current session.

2. Direct Shorthand Methods (__sys__.fs)

  • __sys__.fs.writeTempFile(content, options?): Promise<TempFileResult>
  • __sys__.fs.writeTempFileSync(content, options?): TempFileResult
  • Aliases: createTempFile, createTempFileSync, writeTmpFile, writeTmpFileSync
  • __sys__.fs.cleanupTempFiles(): number

Configuration Options (TempFileOptions)

OptionTypeDefaultDescription
prefixstring"tmp_"Optional filename prefix.
extensionstring".xtmp"File extension or suffix (e.g. ".json", ".pdf", ".txt").
suffixstringAlias for extension.
filenamestringCustom exact filename. Overrides automatic prefix/suffix generation.
ttlnumber | string0Expiration duration in ms or human string (e.g. "30s", "5m", "1h").
autoCleanupOnExitbooleantrueIf true, automatically unlinks the file when process exits.

Return Handle (TempFileResult)

TempFileResult Interface
interface TempFileResult {
    /** Absolute path to the created temporary file inside tmpUserDir */
    path: string;
    /** Filename of the temporary file */
    filename: string;
    /** Size of the temporary file in bytes */
    size: number;
    /** Timestamp when the file was created (in ms) */
    createdAt: number;
    /** Expiration timestamp in ms (or null if no TTL) */
    expiresAt: number | null;
    /** Manually deletes the temporary file immediately. Returns true if deleted. */
    remove: () => boolean;
    /** Alias for remove() */
    cleanup: () => boolean;
}

Code Examples

1. Temporary PDF Export with 5-Minute TTL

typescript
import { __sys__ } from "xypriss";

// Create a disposable PDF report expiring in 5 minutes
const report = await __sys__.fs.writeTempFile(pdfBuffer, {
    prefix: "financial_report_",
    extension: ".pdf",
    ttl: "5m",
});

console.log(`Temporary PDF generated at: ${report.path}`);
// Deliver to client or pipeline...
// Will be automatically unlinked after 5 minutes or on server restart.

2. Immediate On-Demand Cleanup

typescript
import { __sys__ } from "xypriss";

const tmp = __sys__.fs.writeTmpFileSync(JSON.stringify(payload), {
    prefix: "cache_",
    extension: ".json",
});

try {
    // Process payload...
} finally {
    // Ensure immediate deletion as soon as processing completes
    tmp.cleanup();
}

3. Session Scratch Space with Shorthand Alias

typescript
import { __sys__ } from "xypriss";

const scratch = await __sys__.fs.createTempFile("temporary log data", {
    prefix: "user_session_",
    extension: ".log",
    ttl: "1h",
});
Best Practices
  • Human-Readable TTLs: Use intuitive strings like "30s", "10m", "2h".
  • Automatic Exit Cleanup: Keep autoCleanupOnExit: true (default) so process restarts sweep orphan scratch files.
  • Finally Blocks: Call tmp.cleanup() inside finally blocks when generating large files to release disk space immediately.
Security & Advanced Filesystem

Learn about AES-256 encryption, file shredding, and advisory locking.