Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Durable Object jurisdiction API #540

Merged
merged 7 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/durable-objects/src/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ function hexEncode(value: Uint8Array): string {
.join("");
}

function checkJurisdiction(jurisdiction?: string): boolean {
const allowedJurisdictions = ["eu", "fedramp"];
return (
jurisdiction === undefined || allowedJurisdictions.includes(jurisdiction)
);
}

export const kObjectName = Symbol("kObjectName");

export class DurableObjectId {
Expand Down Expand Up @@ -230,7 +237,23 @@ export class DurableObjectNamespace {
this.#ctx = ctx;
}

newUniqueId(_options?: NewUniqueIdOptions): DurableObjectId {
jurisdiction(name: string): DurableObjectNamespace {
if (!checkJurisdiction(name)) {
throw new TypeError(
`jurisdiction called with an unsupported jurisdiction: "${name}"`
);
}
// Ignore jurisdiction restrictions in local mode
DaniFoldi marked this conversation as resolved.
Show resolved Hide resolved
return this;
}

newUniqueId(options?: NewUniqueIdOptions): DurableObjectId {
if (!checkJurisdiction(options?.jurisdiction)) {
throw new TypeError(
`newUniqueId called with an unsupported jurisdiction: "${options?.jurisdiction}"`
);
}

// Create new zero-filled 32 byte buffer
const id = new Uint8Array(32);
// Leave first byte as 0, ensuring no intersection with named IDs
Expand Down
39 changes: 39 additions & 0 deletions packages/durable-objects/test/namespace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,25 @@ test("DurableObjectStub: hides implementation details", async (t) => {
t.deepEqual(getObjectProperties(stub), ["fetch", "id", "name"]);
});

test("DurableObjectNamespace: jurisdiction: returns DurableObjectNamespace", (t) => {
const namespace = new DurableObjectNamespace("OBJECT", throws);
const jurisdictionNamespace = namespace.jurisdiction("eu");
t.is(namespace, jurisdictionNamespace);
});
test("DurableObjectNamespace: jurisdiction: throws TypeError on unsupported value", (t) => {
const namespace = new DurableObjectNamespace("OBJECT", throws);
t.throws(
() => {
namespace.jurisdiction("miniflare");
},
{
instanceOf: TypeError,
message:
'jurisdiction called with an unsupported jurisdiction: "miniflare"',
}
);
});

test("DurableObjectNamespace: newUniqueId: generates unique IDs", (t) => {
const namespace = new DurableObjectNamespace("OBJECT", throws);
const id1 = namespace.newUniqueId();
Expand All @@ -725,6 +744,25 @@ test("DurableObjectNamespace: newUniqueId: IDs tied to generating object", (t) =
message: "ID is not for this Durable Object class.",
});
});
test("DurableObjectNamespace: newUniqueId: allows jurisdiction as option", (t) => {
const namespace = new DurableObjectNamespace("OBJECT", throws);
t.notThrows(() => {
namespace.newUniqueId({ jurisdiction: "eu" });
});
});
test("DurableObjectNamespace: newUniqueId: throws TypeError on unsupported jurisdiction", (t) => {
const namespace = new DurableObjectNamespace("OBJECT", throws);
t.throws(
() => {
namespace.newUniqueId({ jurisdiction: "miniflare" });
},
{
instanceOf: TypeError,
message:
'newUniqueId called with an unsupported jurisdiction: "miniflare"',
}
);
});
test("DurableObjectNamespace: idFromName: generates same ID for same name and object", (t) => {
const namespace = new DurableObjectNamespace("OBJECT", throws);
const id1 = namespace.idFromName("test");
Expand Down Expand Up @@ -818,6 +856,7 @@ test("DurableObjectNamespace: hides implementation details", (t) => {
"get",
"idFromName",
"idFromString",
"jurisdiction",
"newUniqueId",
]);
});
Expand Down