Skip to content
Open
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
27 changes: 20 additions & 7 deletions platform/packages/api/src/provisioning/durable-objects.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setTimeout as delay } from 'node:timers/promises';
import { and, eq, sql } from 'drizzle-orm';
import {
createAssetUploadSession,
Expand Down Expand Up @@ -350,19 +351,31 @@ export function managedDurableObjectBindings(manifest: DeployManifest): Array<Bi
}

export async function verifyManagedDurableObjectApplicationBindings(
env: Env,
env: Pick<Env, 'CF_ACCOUNT_ID' | 'CF_API_TOKEN' | 'DISPATCH_NAMESPACE'>,
worker: string,
manifest: DeployManifest,
delaysMs: ReadonlyArray<number> = [250, 500, 1_000],
): Promise<void> {
if (!manifest.durableObjects?.bindings.length) {
const expectedBindings = managedDurableObjectBindings(manifest);
if (!expectedBindings.length) {
return;
}
const actual = await readBindings(env, worker);
for (const expected of managedDurableObjectBindings(manifest)) {
const binding = actual?.find((value) => value.name === expected.name);
if (binding?.type !== expected.type || binding.namespace_id !== expected.namespace_id) {
throw new Error('Application custom Durable Object binding could not be verified.');
const matchesExpected = (actual: Array<Binding> | undefined): boolean =>
expectedBindings.every((expected) => {
const binding = actual?.find((value) => value.name === expected.name);
return binding?.type === expected.type && binding.namespace_id === expected.namespace_id;
});

let actual = await readBindings(env as Env, worker);
for (const wait of delaysMs) {
if (matchesExpected(actual)) {
return;
}
await delay(wait);
actual = await readBindings(env as Env, worker);
}
if (!matchesExpected(actual)) {
throw new Error('Application custom Durable Object binding could not be verified.');
}
}

Expand Down
82 changes: 81 additions & 1 deletion platform/packages/api/test/unit/deploy-durable-objects.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it } from 'vite-plus/test';
import { afterEach, describe, expect, it, vi } from 'vite-plus/test';
import { verifyManagedDurableObjectApplicationBindings } from '../../src/provisioning/durable-objects.ts';
import {
buildDurableObjectUploadMigrations,
buildLiveUploadBindings,
Expand Down Expand Up @@ -634,3 +635,82 @@ describe('validateManifestShape sandbox + static/spa rejection', () => {
).toBeNull();
});
});

describe('verifyManagedDurableObjectApplicationBindings', () => {
afterEach(() => {
vi.unstubAllGlobals();
});

const env = {
CF_ACCOUNT_ID: 'acc-123',
CF_API_TOKEN: 'tok-123',
DISPATCH_NAMESPACE: 'void-prod',
};
const manifest: DeployManifest = {
version: 3,
bindings: {},
durableObjects: {
bindings: [{ bindingName: 'TRACKER', className: 'TrackerDo', storage: 'sqlite' }],
runtime: { protocol: 1, namespaces: { TrackerDo: 'ns-expected' } },
},
};

it('resolves on first read when bindings match immediately', async () => {
const fetchMock = vi.fn(async () =>
Response.json({
success: true,
errors: [],
result: [
{ name: 'TRACKER', type: 'durable_object_namespace', namespace_id: 'ns-expected' },
],
}),
);
vi.stubGlobal('fetch', fetchMock);

await expect(
verifyManagedDurableObjectApplicationBindings(env, 'worker-1', manifest, [0, 0, 0]),
).resolves.toBeUndefined();
expect(fetchMock).toHaveBeenCalledTimes(1);
});

it('retries stale/empty binding reads after Worker upload until bindings converge', async () => {
let bindingCalls = 0;
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
const url = String(input);
if (url.endsWith('/settings')) {
return Response.json({ success: true, errors: [], result: {} });
}
bindingCalls += 1;
return Response.json({
success: true,
errors: [],
result:
bindingCalls === 1
? []
: [{ name: 'TRACKER', type: 'durable_object_namespace', namespace_id: 'ns-expected' }],
});
});
vi.stubGlobal('fetch', fetchMock);

await expect(
verifyManagedDurableObjectApplicationBindings(env, 'worker-1', manifest, [0, 0, 0]),
).resolves.toBeUndefined();
expect(bindingCalls).toBe(2);
});

it('rejects when bindings never match across all retry attempts', async () => {
const fetchMock = vi.fn(async () =>
Response.json({
success: true,
errors: [],
result: [{ name: 'TRACKER', type: 'durable_object_namespace', namespace_id: 'ns-stale' }],
}),
);
vi.stubGlobal('fetch', fetchMock);

await expect(
verifyManagedDurableObjectApplicationBindings(env, 'worker-1', manifest, [0, 0, 0]),
).rejects.toThrow('Application custom Durable Object binding could not be verified.');
expect(fetchMock).toHaveBeenCalledTimes(4);
});
});