INTEGRITY Dokumentace

Workers binding

Vazbu Artifacts pro Workers použijte k vytváření, importu, kontrole, forkování a mazání repozitářů přímo z vašeho Workeru. Vazba Artifacts vrací handly repozitářů, které umožňují operace s platností pro daný repozitář, jako je správa tokenů nebo forkování.

Zkontrolujte Jmenné prostory nejprve a poté zvolte název jmenného prostoru, který zde navážete.

Nakonfigurujte binding

Přidejte binding Artifacts do konfiguračního souboru Wrangler:

{
  "$schema": "./node_modules/wrangler/config-schema.json",
  "artifacts": [
    {
      "binding": "ARTIFACTS",
      "namespace": "default"
    }
  ]
}
[[artifacts]]
binding = "ARTIFACTS"
namespace = "default" # replace with your Artifacts namespace
# remote = true # optional: use the remote Artifacts service in local dev

Poté, co spustíte npx wrangler types, prostředí vašeho Workeru vypadá takto:

export interface Env {
	ARTIFACTS: Artifacts;
}

Wrangler vygeneruje Artifacts typ pro konzumenty a přímo ho naváže do vašeho prostředí.

V pojmenovaných prostředích Wrangler, artifacts se nedědí. Binding zopakujte v každém prostředí, kde ho potřebujete.

Za běhu nasazené Workery používají nakonfigurovaný binding přímo. Pro místní příkazy Wrangler, například wrangler dev, wrangler deploy, nebo wrangler types, nejprve se ověřte pomocí Wrangler. Informace o místní autentizaci OAuth najdete v wrangler login. Pro prostředí CI nebo headless prostředí najdete informace v Spouštění Wrangler v CI/CD.

Metody jmenného prostoru

Použijte metody namespace na env.ARTIFACTS k vytváření, výpisu, kontrole, importu nebo mazání repozitářů.

create(name, opts?)

create() vrátí metadata repozitáře včetně name, remote, defaultBranch, a počáteční token. Tyto hodnoty uložte, pokud je budete později potřebovat.

async function createRepo(artifacts) {
	const created = await artifacts.create("starter-repo", {
		description: "Repository for automation experiments",
		readOnly: false,
		setDefaultBranch: "main",
	});

	return {
		defaultBranch: created.defaultBranch,
		name: created.name,
		remote: created.remote,
		initialToken: created.token,
	};
}
async function createRepo(artifacts: Artifacts) {
	const created = await artifacts.create("starter-repo", {
		description: "Repository for automation experiments",
		readOnly: false,
		setDefaultBranch: "main",
	});

	return {
		defaultBranch: created.defaultBranch,
		name: created.name,
		remote: created.remote,
		initialToken: created.token,
	};
}

get(name)

get() vrátí popisovač existujícího repozitáře. Pomocí tohoto popisovače volejte asynchronní metody repozitáře, například createToken(), listTokens(), revokeToken(), a fork().

async function getRepoHandle(artifacts) {
	const repo = await artifacts.get("starter-repo");
	const token = await repo.createToken("read", 3600);
	return token;
}
async function getRepoHandle(artifacts: Artifacts) {
	const repo = await artifacts.get("starter-repo");
	const token = await repo.createToken("read", 3600);
	return token;
}

list(opts?)

async function listRepos(artifacts) {
	const page = await artifacts.list({ limit: 10 });

	return {
		repos: page.repos.map((repo) => ({
			name: repo.name,
			status: repo.status,
		})),
		nextCursor: page.cursor ?? null,
	};
}
async function listRepos(artifacts: Artifacts) {
	const page = await artifacts.list({ limit: 10 });

	return {
		repos: page.repos.map((repo) => ({
			name: repo.name,
			status: repo.status,
		})),
		nextCursor: page.cursor ?? null,
	};
}

Každý uvedený repozitář obsahuje status hodnotu ready, importing, nebo forking.

import(params)

Naimportujte repozitář z externího vzdáleného git repozitáře.

import() vrátí metadata repozitáře včetně name, remote, defaultBranch, a počáteční token. Uložte remote a name hodnoty, pokud je budete později potřebovat.

async function importFromGitHub(artifacts) {
	const imported = await artifacts.import({
		source: {
			url: "https://github.com/cloudflare/workers-sdk",
			branch: "main",
		},
		target: {
			name: "workers-sdk",
		},
	});

	return {
		name: imported.name,
		remote: imported.remote,
		token: imported.token,
	};
}
async function importFromGitHub(artifacts: Artifacts) {
	const imported = await artifacts.import({
		source: {
			url: "https://github.com/cloudflare/workers-sdk",
			branch: "main",
		},
		target: {
			name: "workers-sdk",
		},
	});

	return {
		name: imported.name,
		remote: imported.remote,
		token: imported.token,
	};
}

delete(name)

async function deleteRepo(artifacts) {
	return artifacts.delete("starter-repo");
}
async function deleteRepo(artifacts: Artifacts) {
	return artifacts.delete("starter-repo");
}

Metody popisovače repozitáře

Zavolejte await artifacts.get(name) k získání popisovače repozitáře. Pomocí tohoto popisovače volejte asynchronní metody repozitáře.

createToken(scope?, ttl?)

async function mintReadToken(artifacts) {
	const repo = await artifacts.get("starter-repo");
	return repo.createToken("read", 3600);
}
async function mintReadToken(artifacts: Artifacts) {
	const repo = await artifacts.get("starter-repo");
	return repo.createToken("read", 3600);
}

Na rozdíl od create() a import(), repo.createToken() vrátí strukturovaný výsledek s plaintext a expiresAt. plaintext hodnota je řetězec tokenu Git.

listTokens()

async function listRepoTokens(artifacts) {
	const repo = await artifacts.get("starter-repo");
	const result = await repo.listTokens();
	return {
		total: result.total,
		tokens: result.tokens,
	};
}
async function listRepoTokens(artifacts: Artifacts) {
	const repo = await artifacts.get("starter-repo");
	const result = await repo.listTokens();
	return {
		total: result.total,
		tokens: result.tokens,
	};
}

revokeToken(tokenOrId)

async function revokeToken(artifacts, tokenOrId) {
	const repo = await artifacts.get("starter-repo");
	return repo.revokeToken(tokenOrId);
}
async function revokeToken(artifacts: Artifacts, tokenOrId: string) {
	const repo = await artifacts.get("starter-repo");
	return repo.revokeToken(tokenOrId);
}

fork(name, opts?)

fork() vrátí metadata nového repozitáře. Uložte si remote a name hodnoty, pokud je budete později potřebovat.

async function forkRepo(artifacts) {
	const repo = await artifacts.get("starter-repo");
	const forked = await repo.fork("starter-repo-copy", {
		description: "Fork for testing",
		defaultBranchOnly: true,
		readOnly: false,
	});

	return forked.remote;
}
async function forkRepo(artifacts: Artifacts) {
	const repo = await artifacts.get("starter-repo");
	const forked = await repo.fork("starter-repo-copy", {
		description: "Fork for testing",
		defaultBranchOnly: true,
		readOnly: false,
	});

	return forked.remote;
}

log(opts?)

async function readCommitHistory(artifacts) {
	const repo = await artifacts.get("starter-repo");
	const history = await repo.log({ ref: "main", limit: 10 });
	return history;
}
async function readCommitHistory(artifacts: Artifacts) {
	const repo = await artifacts.get("starter-repo");
	const history = await repo.log({ ref: "main", limit: 10 });
	return history;
}

readCommit(hash)

async function readCommit(artifacts, hash) {
	const repo = await artifacts.get("starter-repo");
	return repo.readCommit(hash);
}
async function readCommit(artifacts: Artifacts, hash: string) {
	const repo = await artifacts.get("starter-repo");
	return repo.readCommit(hash);
}

readTree(hash)

async function readTree(artifacts, hash) {
	const repo = await artifacts.get("starter-repo");
	return repo.readTree(hash);
}
async function readTree(artifacts: Artifacts, hash: string) {
	const repo = await artifacts.get("starter-repo");
	return repo.readTree(hash);
}

Příklad Workeru

Tento příklad kombinuje metody vazby v jedné trase Workeru.

src/index.js
export default {
	async fetch(request, env) {
		const url = new URL(request.url);

		if (request.method === "POST" && url.pathname === "/repos") {
			const created = await env.ARTIFACTS.create("starter-repo");
			return Response.json({
				name: created.name,
				remote: created.remote,
			});
		}

		if (request.method === "POST" && url.pathname === "/tokens") {
			const repo = await env.ARTIFACTS.get("starter-repo");
			const token = await repo.createToken("read", 3600);
			return Response.json(token);
		}

		return Response.json(
			{ message: "Use POST /repos or POST /tokens." },
			{ status: 404 },
		);
	},
};
src/index.ts
interface Env {
	ARTIFACTS: Artifacts;
}

export default {
	async fetch(request: Request, env: Env): Promise<Response> {
		const url = new URL(request.url);

		if (request.method === "POST" && url.pathname === "/repos") {
			const created = await env.ARTIFACTS.create("starter-repo");
			return Response.json({
				name: created.name,
				remote: created.remote,
			});
		}

		if (request.method === "POST" && url.pathname === "/tokens") {
			const repo = await env.ARTIFACTS.get("starter-repo");
			const token = await repo.createToken("read", 3600);
			return Response.json(token);
		}

		return Response.json(
			{ message: "Use POST /repos or POST /tokens." },
			{ status: 404 },
		);
	},
} satisfies ExportedHandler<Env>;

Vygenerované typy

Spustit npx wrangler types ve vašem vlastním projektu a zacházejte s vygenerovaným worker-configuration.d.ts soubor jako jediný zdroj pravdy pro typy bindingů Artifacts v daném prostředí.

Další kroky

REST API

Porovnání metod bindingu s podkladovými trasami HTTP.

Začínáme s Workers

Vazbu použijte v kompletním projektu Workeru od lokálního vývoje až po nasazení.

Git protokol

Vzdálená úložiště repozitářů a tokeny používejte se standardními klienty git-over-HTTPS.