INTEGRITY Dokumentace

Sledování souborů

Sledujte změny v souborovém systému v reálném čase pomocí nativního systému inotify v Linuxu. Metoda watch() metoda vrací stream Server-Sent Events (SSE) s událostmi změn souborů, který zpracujete pomocí parseSSEStream().

Metody

watch()

Sledujte adresář a zaznamenávejte změny v souborovém systému. Vrací stream událostí SSE.

const stream = await sandbox.watch(path: string, options?: WatchOptions): Promise<ReadableStream<Uint8Array>>

Parametry:

Vrací: Promise<ReadableStream<Uint8Array>> : stream SSE FileWatchSSEEvent objekty

import { parseSSEStream } from "@cloudflare/sandbox";

const stream = await sandbox.watch("/workspace/src", {
	recursive: true,
	include: ["*.ts", "*.js"],
});

const controller = new AbortController();

for await (const event of parseSSEStream(stream, controller.signal)) {
	switch (event.type) {
		case "watching":
			console.log(`Watch established on ${event.path} (id: ${event.watchId})`);
			break;
		case "event":
			console.log(`${event.eventType}: ${event.path}`);
			break;
		case "error":
			console.error(`Watch error: ${event.error}`);
			break;
		case "stopped":
			console.log(`Watch stopped: ${event.reason}`);
			break;
	}
}

// Cancel the watch by aborting — cleans up the watcher server-side
controller.abort();
import { parseSSEStream } from "@cloudflare/sandbox";
import type { FileWatchSSEEvent } from "@cloudflare/sandbox";

const stream = await sandbox.watch("/workspace/src", {
	recursive: true,
	include: ["*.ts", "*.js"],
});

const controller = new AbortController();

for await (const event of parseSSEStream<FileWatchSSEEvent>(
	stream,
	controller.signal,
)) {
	switch (event.type) {
		case "watching":
			console.log(`Watch established on ${event.path} (id: ${event.watchId})`);
			break;
		case "event":
			console.log(`${event.eventType}: ${event.path}`);
			break;
		case "error":
			console.error(`Watch error: ${event.error}`);
			break;
		case "stopped":
			console.log(`Watch stopped: ${event.reason}`);
			break;
	}
}

// Cancel the watch by aborting — cleans up the watcher server-side
controller.abort();

Typy

FileWatchSSEEvent

Union type reprezentující všechny události SSE vysílané streamem watch.

type FileWatchSSEEvent =
	| { type: "watching"; path: string; watchId: string }
	| {
			type: "event";
			eventType: FileWatchEventType;
			path: string;
			isDirectory: boolean;
			timestamp: string;
	  }
	| { type: "error"; error: string }
	| { type: "stopped"; reason: string };

FileWatchEventType

Typy změn v souborovém systému, které lze detekovat.

type FileWatchEventType =
	| "create"
	| "modify"
	| "delete"
	| "move_from"
	| "move_to"
	| "attrib";

WatchOptions

Možnosti konfigurace pro sledování adresářů.

interface WatchOptions {
	/** Watch subdirectories recursively (default: true) */
	recursive?: boolean;
	/** Glob patterns to include. Cannot be used together with `exclude`. */
	include?: string[];
	/** Glob patterns to exclude. Cannot be used together with `include`. Default: ['.git', 'node_modules', '.DS_Store'] */
	exclude?: string[];
	/** Session to run the watch in. If omitted, the sandbox's implicit execution mode is used. */
	sessionId?: string;
}

parseSSEStream()

Převede ReadableStream<Uint8Array> do typované AsyncGenerator událostí. Přijímá volitelný AbortSignal pro zrušení streamu.

function parseSSEStream<T>(
	stream: ReadableStream<Uint8Array>,
	signal?: AbortSignal,
): AsyncGenerator<T>;

Parametry:

Nejlepší způsob, jak sledování (watch) zastavit zvenčí zpracovávající smyčky, je přerušit signál:

const controller = new AbortController();

// Cancel after 60 seconds
setTimeout(() => controller.abort(), 60_000);

for await (const event of parseSSEStream<FileWatchSSEEvent>(
	stream,
	controller.signal,
)) {
	// process events
}

Podpora glob vzorů

include a exclude volby přijímají omezenou sadu glob tokenů pro předvídatelné porovnávání:

Token Význam Příklad
* Odpovídá libovolným znakům v rámci segmentu cesty *.ts odpovídá index.ts
** Odpovídá napříč hranicemi adresářů **/*.test.ts
? Odpovídá jednomu znaku ?.js odpovídá a.js

Třídy znaků ([abc]), expanze složených závorek ({a,b}), a escapování zpětným lomítkem není podporováno. Vzory obsahující tyto tokeny jsou odmítnuty s validační chybou.

Poznámky