INTEGRITY Dokumentace

Připojte se k Workers bindings

Sandboxy mají přístup k Workers bindings : KV, R2, D1, Durable Objects a další, a to prostřednictvím outbound handlery. Odchozí handler zachytává požadavky HTTP ze sandboxu a běží uvnitř běhového prostředí Workers, kde jsou k dispozici všechny vaše nakonfigurované vazby.

Sandbox odešle obyčejný HTTP požadavek na virtuální hostname, například http://my.kv/some-key), a outbound handler ho přeloží pomocí navázaného prostředku. Uvnitř sandboxu není potřeba žádné SDK ani klientská knihovna.

Používejte bindings v outbound handlerech

Definujte outboundByHost handler pro každý virtuální hostname. Daný env argument vám dává přístup ke každému bindingu deklarovanému v konfiguraci Wrangler.

export class MySandbox extends Sandbox {}

MySandbox.outboundByHost = {
	"my.kv": async (request, env, ctx) => {
		const url = new URL(request.url);
		const key = url.pathname.slice(1);
		const value = await env.KV.get(key);
		return new Response(value ?? "", { status: value ? 200 : 404 });
	},
	"my.r2": async (request, env, ctx) => {
		const url = new URL(request.url);
		// Scope access to this sandbox's ID
		const path = `${ctx.containerId}${url.pathname}`;
		const object = await env.R2.get(path);
		return new Response(object?.body ?? null, { status: object ? 200 : 404 });
	},
};
export class MySandbox extends Sandbox {}

MySandbox.outboundByHost = {
	"my.kv": async (request: Request, env: Env, ctx: OutboundHandlerContext) => {
		const url = new URL(request.url);
		const key = url.pathname.slice(1);
		const value = await env.KV.get(key);
		return new Response(value ?? "", { status: value ? 200 : 404 });
	},
	"my.r2": async (request: Request, env: Env, ctx: OutboundHandlerContext) => {
		const url = new URL(request.url);
		// Scope access to this sandbox's ID
		const path = `${ctx.containerId}${url.pathname}`;
		const object = await env.R2.get(path);
		return new Response(object?.body ?? null, { status: object ? 200 : 404 });
	},
};

Sandbox volá http://my.kv/some-key a handler ho přeloží pomocí KV bindingu. Volání http://my.r2/file.png čte z R2 a je omezeno na aktuální instanci sandboxu.

Přístup ke stavu Durable Object

ctx argument zpřístupňuje containerId, což vám umožňuje pracovat s vlastním Durable Object sandboxu z odchozího handleru.

export class MySandbox extends Sandbox {}

MySandbox.outboundByHost = {
	"get-state.do": async (request, env, ctx) => {
		const id = env.MY_SANDBOX.idFromString(ctx.containerId);
		const stub = env.MY_SANDBOX.get(id);
		// Assumes getStateForKey is defined on your DO
		return stub.getStateForKey(request.body);
	},
};
export class MySandbox extends Sandbox {}

MySandbox.outboundByHost = {
	"get-state.do": async (
		request: Request,
		env: Env,
		ctx: { containerId: string },
	) => {
		const id = env.MY_SANDBOX.idFromString(ctx.containerId);
		const stub = env.MY_SANDBOX.get(id);
		// Assumes getStateForKey is defined on your DO
		return stub.getStateForKey(request.body);
	},
};