ENF News Bot
Automated News Bot
- Joined
- Apr 15, 2022
- Messages
- 2,591
Sandboxes now support real-time filesystem watching via
https://developers.cloudflare.com/changelog/rss/#sandboxwatchpath-options
Pass a directory path and optional filters. The returned stream is a standard
Server-side consumption with
https://developers.cloudflare.com/changelog/rss/#server-side-consumption-with-parsessestream
Use
Each event includes a
To update to the latest version:
For full API details, refer to the Sandbox file watching reference.
Continue reading...
sandbox.watch(). The method returns a Server-Sent Events ↗︎ stream backed by native inotify, so your Worker receives create, modify, delete, and move events as they happen inside the container.sandbox.watch(path, options)
https://developers.cloudflare.com/changelog/rss/#sandboxwatchpath-optionsPass a directory path and optional filters. The returned stream is a standard
ReadableStream you can proxy directly to a browser client or consume server-side.
Code:
// Stream events to a browser client
const stream = await sandbox.watch("/workspace/src", {
recursive: true,
include: ["*.ts", "*.js"],
});
return new Response(stream, {
headers: { "Content-Type": "text/event-stream" },
});
Code:
// Stream events to a browser client
const stream = await sandbox.watch("/workspace/src", {
recursive: true,
include: ["*.ts", "*.js"],
});
return new Response(stream, {
headers: { "Content-Type": "text/event-stream" },
});
Server-side consumption with parseSSEStream
https://developers.cloudflare.com/changelog/rss/#server-side-consumption-with-parsessestreamUse
parseSSEStream to iterate over events inside a Worker without forwarding them to a client.
Code:
import { parseSSEStream } from "@cloudflare/sandbox";
const stream = await sandbox.watch("/workspace/src", { recursive: true });
for await (const event of parseSSEStream(stream)) {
console.log(event.type, event.path);
}
Code:
import { parseSSEStream } from "@cloudflare/sandbox";
import type { FileWatchSSEEvent } from "@cloudflare/sandbox";
const stream = await sandbox.watch("/workspace/src", { recursive: true });
for await (const event of parseSSEStream<FileWatchSSEEvent>(stream)) {
console.log(event.type, event.path);
}
Each event includes a
type field (create, modify, delete, or move) and the affected path. Move events also include a from field with the original path.Options
https://developers.cloudflare.com/changelog/rss/#options| Option | Type | Description |
|---|---|---|
recursive | boolean | Watch subdirectories. Defaults to false. |
include | string[] | Glob patterns to filter events. Omit to receive all events. |
Upgrade
https://developers.cloudflare.com/changelog/rss/#upgradeTo update to the latest version:
npm i @cloudflare/sandbox@latestFor full API details, refer to the Sandbox file watching reference.
Continue reading...