Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

Durable Object ID

最終更新 Markdown で表示Agent セットアップ

説明

Durable Object ID は、Durable Object を識別するための 64 桁の 16 進数です。64 桁の 16 進数すべてが有効な ID ではありません。Durable Object ID は、DurableObjectNamespace インターフェイス経由で間接的に作ります。

DurableObjectId インターフェイスは、新規または既存の Durable Object を指します。このインターフェイスは、DurableObjectNamespace::get が Durable Object へリクエストを送るための DurableObjectStub を取得するときに、もっともよく使います。Durable Object の ID を作っても、Durable Object 自体は作られません。Durable Object は、DurableObjectId からスタブを作ったあと、遅延して作成されます。実際にアクセスされるまでオブジェクトが構築されないようにするためです。

メソッド

toString

toString は、DurableObjectId を 64 桁の 16 進文字列に変換します。この文字列はログや、セッション Cookie など別の場所への保存に使えます。この文字列を使うと、DurableObjectNamespace::idFromStringDurableObjectId を再構築できます。

// 新しい一意の ID を作成する
const id = env.MY_DURABLE_OBJECT.newUniqueId();
// ID を文字列に変換し、セッション Cookie など別の場所に保存する
const session_id = id.toString();

...
// 文字列から ID を再作成する
const id = env.MY_DURABLE_OBJECT.idFromString(session_id);

パラメーター

  • なし。

戻り値

  • 64 桁の 16 進文字列。

equals

equals は、2 つの DurableObjectId インスタンスが等しいかを比較します。

const id1 = env.MY_DURABLE_OBJECT.newUniqueId();
const id2 = env.MY_DURABLE_OBJECT.newUniqueId();
console.assert(!id1.equals(id2), "Different unique ids should never be equal.");
id1 = env.MY_DURABLE_OBJECT.newUniqueId()
id2 = env.MY_DURABLE_OBJECT.newUniqueId()
assert not id1.equals(id2), "Different unique ids should never be equal."

パラメーター

  • 比較対象となる必須の DurableObjectId

戻り値

  • 真偽値。等しい場合は true、それ以外は false です。

プロパティ

name

nameDurableObjectId の任意プロパティで、DurableObjectNamespace::idFromNameDurableObjectId を作ったときに使った名前を返します。DurableObjectNamespace::newUniqueId で作った場合、この値は undefined です。

呼び出し側が idFromName() または getByName() を使うと、Durable Object 内部の ctx.id でも name プロパティを使えます。次の場合、ctx.id.nameundefined になります。

  • 呼び出し側が idFromString() で Durable Object にアクセスした場合。元の ID が idFromName() で作られていても同じです。
  • 1,024 バイトを超える名前は ctx.id に渡りません。
  • Durable Object が newUniqueId() で作られた場合。
const uniqueId = env.MY_DURABLE_OBJECT.newUniqueId();
const fromNameId = env.MY_DURABLE_OBJECT.idFromName("foo");
console.assert(uniqueId.name === undefined, "unique ids have no name");
console.assert(
	fromNameId.name === "foo",
	"name matches parameter to idFromName",
);
const uniqueId: DurableObjectId = env.MY_DURABLE_OBJECT.newUniqueId();
const fromNameId: DurableObjectId = env.MY_DURABLE_OBJECT.idFromName("foo");
console.assert(uniqueId.name === undefined, "unique ids have no name");
console.assert(
	fromNameId.name === "foo",
	"name matches parameter to idFromName",
);
unique_id = env.MY_DURABLE_OBJECT.newUniqueId()
from_name_id = env.MY_DURABLE_OBJECT.idFromName("foo")
assert unique_id.name is None, "unique ids have no name"
assert from_name_id.name == "foo", "name matches parameter to idFromName"

同じ name は、Durable Object 内部でも ctx.id.name で使えます。

import { DurableObject } from "cloudflare:workers";

export class ChatRoom extends DurableObject {
	async getRoomName() {
		return this.ctx.id.name; // "foo" when accessed via getByName("foo")
	}
}
import { DurableObject } from "cloudflare:workers";

export class ChatRoom extends DurableObject<Env> {
	async getRoomName(): Promise<string | undefined> {
		return this.ctx.id.name; // "foo" when accessed via getByName("foo")
	}
}
from workers import DurableObject

class ChatRoom(DurableObject):
    async def get_room_name(self):
        return self.ctx.id.name  # "foo" when accessed via get_by_name("foo")

jurisdiction

jurisdictionDurableObjectId の任意プロパティで、その ID が制限されている 管轄(jurisdiction) を返します。例は "eu""fedramp" です。同じ値は Durable Object 内部の ctx.id.jurisdiction でも使え、alarm ハンドラーidFromString() 経由でアクセスしたオブジェクトでも使えます。管轄を引数で渡したり、ストレージに保存したりしなくても、リージョンを意識した判断ができます。

jurisdiction は、次を含むすべての ID 作成経路で保持されます。

  • 管轄で制限したサブ名前空間から作った ID。例は env.MY_DURABLE_OBJECT.jurisdiction("eu").idFromName("foo").newUniqueId() です。
  • env.MY_DURABLE_OBJECT.newUniqueId({ jurisdiction: "eu" }) で作った ID。
  • idFromString() で文字列から復元した ID。管轄は文字列自体にエンコードされているため、どの名前空間バインディングでも使えます。

ctx.id.jurisdictionundefined になるのは、次の 2 つの場合です。

  • Durable Object が、管轄で制限した名前空間で作られていない場合。
  • Durable Object のアラームが 2026-03-15 より前にスケジュールされた場合。値を補うには、fetch() または RPC ハンドラーからアラームを再スケジュールします。
const plainId = env.MY_DURABLE_OBJECT.idFromName("foo");
const euId = env.MY_DURABLE_OBJECT.jurisdiction("eu").idFromName("foo");
console.assert(plainId.jurisdiction === undefined, "no jurisdiction set");
console.assert(euId.jurisdiction === "eu", "jurisdiction matches namespace");
plain_id = env.MY_DURABLE_OBJECT.idFromName("foo")
eu_id = env.MY_DURABLE_OBJECT.jurisdiction("eu").idFromName("foo")
assert plain_id.jurisdiction is None, "no jurisdiction set"
assert eu_id.jurisdiction == "eu", "jurisdiction matches namespace"

関連リソース

役に立ちましたか?