Watch a directory
Watch a directory when your application must react to files created or changed inside a running sandbox.
import { FilesystemEventType, Sandbox } from "@abox-dev/sdk";
const sandbox = await Sandbox.create();
try {
let receiveEvent;
const nextWrite = new Promise((resolve) => {
receiveEvent = resolve;
});
const watcher = await sandbox.files.watchDir(
"~",
(event) => {
if (event.type === FilesystemEventType.WRITE) receiveEvent(event);
},
{ includeEntry: true, user: "root" },
);
await sandbox.files.write("~/message.txt", "Hello", { user: "root" });
const event = await nextWrite;
console.log(event.type, event.entry?.path);
await watcher.stop();
} finally {
await sandbox.kill();
}JavaScript delivers events to the callback passed to watchDir. Python stores them until get_new_events() is called. Go exposes events through the WatchHandle.Events channel. Stop the watcher when it is no longer needed.
Set recursive to watch existing subdirectories. Set includeEntry in JavaScript, include_entry in Python, or IncludeEntry in Go to receive file information with an event. Entry information can be absent after a file is removed.
Watching a network filesystem requires the explicit allowNetworkMounts, allow_network_mounts, or AllowNetworkMounts option. Such events are best effort: changes made by a different client are not delivered to the sandbox watcher.
To resolve the watched path for a specific user, set user in JavaScript/Python or WatchOptions.User in Go. For example, watching ~ with user root selects root's home directory. An empty Go user uses the template default.