import {httpStatusToCloudError, RuntimeUnavailableError} from '../cloud-spi/errors' export type LightsailStaticDeployment = {status: 'deployed'; files: number} export type LightsailNodeDeployment = {status: 'deployed'; files: number; port: number; releaseId: string} export async function deployLightsailStaticFiles(instanceName: string, form: FormData): Promise { const endpoint = process.env.FLOCI_ENDPOINT if (!endpoint) throw new RuntimeUnavailableError('TheCloud diagnostics are unavailable') let response: Response try { response = await fetch(new URL(`/_floci/diagnostics/lightsail/instances/${encodeURIComponent(instanceName)}/static-files`, endpoint).toString(), { method: 'POST', body: form, redirect: 'error', signal: AbortSignal.timeout(30_000), }) } catch (error) { throw new RuntimeUnavailableError('TheCloud diagnostics are unavailable', {cause: error}) } const body = await response.json().catch(() => ({})) as {message?: unknown; files?: unknown; status?: unknown} if (!response.ok) throw httpStatusToCloudError(response.status, typeof body.message === 'string' ? body.message : 'Static file deployment failed') if (body.status !== 'deployed' || typeof body.files !== 'number') { throw new RuntimeUnavailableError('TheCloud diagnostics returned an invalid deployment response') } return {status: 'deployed', files: body.files} } export async function deployLightsailNodeApplication(instanceName: string, form: FormData): Promise { const endpoint = process.env.FLOCI_ENDPOINT if (!endpoint) throw new RuntimeUnavailableError('TheCloud diagnostics are unavailable') let response: Response try { response = await fetch(new URL(`/_floci/diagnostics/lightsail/instances/${encodeURIComponent(instanceName)}/node-application`, endpoint).toString(), { method: 'POST', body: form, redirect: 'error', signal: AbortSignal.timeout(360_000), }) } catch (error) { throw new RuntimeUnavailableError('TheCloud diagnostics are unavailable', {cause: error}) } const body = await response.json().catch(() => ({})) as {message?: unknown; files?: unknown; status?: unknown; port?: unknown; releaseId?: unknown} if (!response.ok) throw httpStatusToCloudError(response.status, typeof body.message === 'string' ? body.message : 'Node.js deployment failed') if (body.status !== 'deployed' || typeof body.files !== 'number' || typeof body.port !== 'number' || typeof body.releaseId !== 'string') { throw new RuntimeUnavailableError('TheCloud diagnostics returned an invalid Node.js deployment response') } return {status: 'deployed', files: body.files, port: body.port, releaseId: body.releaseId} }