diff --git a/platform/packages/api/src/provisioning/durable-objects.ts b/platform/packages/api/src/provisioning/durable-objects.ts index ef802ce62..88aecd8d4 100644 --- a/platform/packages/api/src/provisioning/durable-objects.ts +++ b/platform/packages/api/src/provisioning/durable-objects.ts @@ -1,3 +1,4 @@ +import { setTimeout as delay } from 'node:timers/promises'; import { and, eq, sql } from 'drizzle-orm'; import { createAssetUploadSession, @@ -350,19 +351,31 @@ export function managedDurableObjectBindings(manifest: DeployManifest): Array, worker: string, manifest: DeployManifest, + delaysMs: ReadonlyArray = [250, 500, 1_000], ): Promise { - 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 | 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.'); } } diff --git a/platform/packages/api/test/unit/deploy-durable-objects.test.ts b/platform/packages/api/test/unit/deploy-durable-objects.test.ts index 459bd11a6..134e32a27 100644 --- a/platform/packages/api/test/unit/deploy-durable-objects.test.ts +++ b/platform/packages/api/test/unit/deploy-durable-objects.test.ts @@ -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, @@ -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); + }); +});