Skip to content

@plinthjs/storage ​

Plinth's filesystem abstraction. A uniform async Disk contract with two drivers, plus a FilesystemManager for named disks.

Disks ​

ts
import { MemoryDisk, LocalDisk, FilesystemManager } from '@plinthjs/storage'

// In-memory, fully deterministic - great for tests and ephemeral work.
const memory = new MemoryDisk()
await memory.put('logs/app.log', 'boot\n')
await memory.append('logs/app.log', 'ready\n')
await memory.get('logs/app.log') // "boot\nready\n"
await memory.allFiles('logs') // ["logs/app.log"]

// Local filesystem, confined under a root directory.
const local = new LocalDisk({ root: '/var/app/storage' })
await local.put('avatars/1.png', '...') // creates parent dirs as needed
local.path('avatars/1.png') // "/var/app/storage/avatars/1.png"

Every Disk exposes the same surface: get, put, exists/missing, delete, copy, move, size, append, prepend, files/allFiles, directories, makeDirectory, deleteDirectory, and path. Paths are POSIX-style and relative to the disk root.

LocalDisk rejects any path that escapes its root (../../etc/passwd) with a PathTraversalError.

The manager ​

ts
const storage = new FilesystemManager({
  default: 'local',
  disks: {
    local: new LocalDisk({ root: '/var/app/storage' }),
    tmp: new MemoryDisk(),
  },
})

await storage.disk().put('a.txt', 'x') // default disk ('local')
await storage.disk('tmp').put('b.txt', 'y')
storage.addDisk('cache', new MemoryDisk())

memoryFilesystem() is a shortcut for a manager whose default disk is a fresh MemoryDisk - the Storage::fake() equivalent.

Released under the MIT License.