You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
if(existing){if(existing.fingerprint!==input.fingerprint)thrownewOperationConflict("The idempotency key is already bound to a different immutable input.");returnexisting}
this.db.prepare("INSERT OR IGNORE INTO wp_codebox_api_sites (site_id, principal, idempotency_key, fingerprint, artifact_sha256, artifact_size, slug, name, site_title, operation_id, created_at) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ? WHERE (SELECT COUNT(*) FROM wp_codebox_api_sites WHERE principal = ?) < ? AND NOT EXISTS (SELECT 1 FROM wp_codebox_sites WHERE site_id = ?)").bind(site.id,token.principal,input.key,input.fingerprint,input.artifactSha256,input.artifactSize,input.options.slug,input.options.name,input.options.siteTitle,now,token.principal,token.maxSites,site.id),
268
-
this.db.prepare("INSERT INTO wp_codebox_sites (site_id, hostname, origin, state, created_at, activated_at, updated_at) SELECT ?, ?, ?, 'active', ?, ?, ? WHERE EXISTS (SELECT 1 FROM wp_codebox_api_sites WHERE site_id = ? AND principal = ? AND idempotency_key = ? AND fingerprint = ?)").bind(site.id,site.hostname,site.origin,now,now,now,site.id,token.principal,input.key,input.fingerprint),
294
+
this.db.prepare("INSERT OR IGNORE INTO wp_codebox_api_sites (site_id, principal, idempotency_key, fingerprint, artifact_sha256, artifact_size, slug, name, site_title, operation_id, created_at) SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ? WHERE (SELECT COUNT(*) FROM wp_codebox_api_sites a LEFT JOIN wp_codebox_site_lifecycles l ON l.site_id = a.site_id WHERE a.principal = ? AND (l.site_id IS NULL OR l.state != 'tombstoned')) < ? AND NOT EXISTS (SELECT 1 FROM wp_codebox_sites WHERE site_id = ?)").bind(site.id,token.principal,input.key,input.fingerprint,input.artifactSha256,input.artifactSize,input.options.slug,input.options.name,input.options.siteTitle,now,token.principal,token.maxSites,site.id),
295
+
this.db.prepare("INSERT INTO wp_codebox_sites (site_id, hostname, origin, generation, state, created_at, activated_at, updated_at) SELECT ?, ?, ?, ?, 'active', ?, ?, ? WHERE EXISTS (SELECT 1 FROM wp_codebox_api_sites WHERE site_id = ? AND principal = ? AND idempotency_key = ? AND fingerprint = ?)").bind(site.id,site.hostname,site.origin,allocationIdentity(site.id).generation,now,now,now,site.id,token.principal,input.key,input.fingerprint),
if(raced){if(raced.fingerprint!==input.fingerprint)thrownewOperationConflict("The idempotency key is already bound to a different immutable input.");returnraced}
276
306
}
277
-
constcount=awaitthis.db.prepare("SELECT COUNT(*) AS count FROM wp_codebox_api_sites WHERE principal = ?").bind(token.principal).first<{count: number}>()
307
+
constcount=awaitthis.db.prepare("SELECT COUNT(*) AS count FROM wp_codebox_api_sites a LEFT JOIN wp_codebox_site_lifecycles l ON l.site_id = a.site_id WHERE a.principal = ? AND (l.site_id IS NULL OR l.state != 'tombstoned')").bind(token.principal).first<{count: number}>()
asyncbySite(siteId: string): Promise<AdministratorClaimRecord|null>{awaitthis.schema();constrow=awaitthis.db.prepare("SELECT capability_digest, credential_digest, expires_at, state FROM wp_codebox_api_admin_claims WHERE site_id = ?").bind(siteId).first<{capability_digest: string;credential_digest: string;expires_at: number;state: AdministratorClaimMetadata["state"]}>();returnrow ? {capabilityDigest: row.capability_digest,credentialDigest: row.credential_digest,expiresAt: row.expires_at,state: row.state} : null}
328
359
asyncexpire(siteId: string): Promise<void>{awaitthis.db.prepare("UPDATE wp_codebox_api_admin_claims SET state = 'expired', updated_at = ? WHERE site_id = ? AND state = 'pending' AND expires_at <= ?").bind(Date.now(),siteId,Date.now()).run()}
329
-
asyncconsume(siteId: string,digest: string): Promise<boolean>{constresult=awaitthis.db.prepare("UPDATE wp_codebox_api_admin_claims SET state = 'consumed', updated_at = ? WHERE site_id = ? AND state = 'pending' AND capability_digest = ? AND expires_at > ?").bind(Date.now(),siteId,digest,Date.now()).run();returnresult.meta.changes===1}
360
+
asyncconsume(siteId: string,digest: string): Promise<boolean>{constnow=Date.now();constresult=awaitthis.db.prepare("UPDATE wp_codebox_api_admin_claims SET state = 'consumed', updated_at = ? WHERE site_id = ? AND state = 'pending' AND capability_digest = ? AND expires_at > ? AND EXISTS (SELECT 1 FROM wp_codebox_sites s LEFT JOIN wp_codebox_site_lifecycles l ON l.site_id = s.site_id AND l.generation = s.generation WHERE s.site_id = wp_codebox_api_admin_claims.site_id AND (l.site_id IS NULL OR (l.state = 'active' AND l.expires_at > ?)))").bind(now,siteId,digest,now,now).run();returnresult.meta.changes===1}
330
361
privateasyncschema(): Promise<void>{awaitthis.db.prepare("CREATE TABLE IF NOT EXISTS wp_codebox_api_admin_claims (site_id TEXT PRIMARY KEY, capability_digest TEXT NOT NULL, credential_digest TEXT NOT NULL, expires_at INTEGER NOT NULL, state TEXT NOT NULL CHECK (state IN ('pending','consumed','expired')), created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL)").run()}
0 commit comments