UNPKG

genezio

Version:

Command line utility to interact with Genezio infrastructure.

197 lines (196 loc) 8.06 kB
/* eslint-disable @typescript-eslint/no-explicit-any */ import { getAstSummary } from "../generateSdk/utils/getAstSummary.js"; import { CloudProviderIdentifier } from "./cloudProviderIdentifier.js"; import { FunctionType, Language, TriggerType, } from "../projectConfiguration/yaml/models.js"; import path from "path"; import { UserError } from "../errors.js"; export class ParameterType { constructor(name, type, optional = false) { this.name = name; this.type = type; this.optional = optional; } } export class MethodConfiguration { constructor(name, parameters, returnType, type, docString, cronString) { this.name = name; this.parameters = parameters.map((parameter) => new ParameterType(parameter, "any")); this.type = type ?? TriggerType.jsonrpc; this.cronString = cronString; this.returnType = returnType; this.docString = docString; } } export class ClassConfiguration { constructor(name, path, type, language, methods, types, version, docString, timeout, storageSize, instanceSize, vcpuCount, memoryMb, maxConcurrentRequestsPerInstance, maxConcurrentInstances, cooldownTime, persistent) { this.name = name; this.path = path; this.type = type; this.methods = methods; this.language = language; this.types = types; this.version = version; this.docString = docString; this.timeout = timeout; this.storageSize = storageSize; this.instanceSize = instanceSize; this.vcpuCount = vcpuCount; this.memoryMb = memoryMb; this.maxConcurrentRequestsPerInstance = maxConcurrentRequestsPerInstance; this.maxConcurrentInstances = maxConcurrentInstances; this.cooldownTime = cooldownTime; this.persistent = persistent; } } export class FunctionConfiguration { constructor(name, path, handler, language, entry, type, timeout, storageSize, instanceSize, vcpuCount, memoryMb, maxConcurrentRequestsPerInstance, maxConcurrentInstances, cooldownTime, persistent, healthcheckPath) { this.name = name; this.path = path; this.handler = handler; this.language = language; this.entry = entry; this.type = type; this.timeout = timeout; this.storageSize = storageSize; this.instanceSize = instanceSize; this.vcpuCount = vcpuCount; this.memoryMb = memoryMb; this.maxConcurrentRequestsPerInstance = maxConcurrentRequestsPerInstance; this.maxConcurrentInstances = maxConcurrentInstances; this.cooldownTime = cooldownTime; this.persistent = persistent; this.healthcheckPath = healthcheckPath; } } export class DatabaseConfiguration { constructor(name, region, type) { this.name = name; this.region = region; this.type = type; } } export class AuthenticationConfiguration { constructor(database) { this.database = database; } } export class NativeAuthDatabaseConfiguration { constructor(name) { this.name = name; } } export class YourOwnAuthDatabaseConfiguration { constructor(type, uri) { this.type = type; this.uri = uri; } } export class SdkConfiguration { constructor(language, path) { this.language = language; this.path = path; } } export class Workspace { constructor(backend) { this.backend = backend; } } /** * This class represents the complete image of the project. * * It combines information from the YAML configuration with the information from the AST Summary. */ export class ProjectConfiguration { constructor(yamlConfiguration, cloudProvider, sdkHandlerResponse) { this.name = yamlConfiguration.name; this.region = yamlConfiguration.region; switch (yamlConfiguration.backend?.language.name) { case Language.ts: case Language.js: this.options = { nodeRuntime: yamlConfiguration.backend.language.runtime, architecture: yamlConfiguration.backend.language.architecture, }; break; case Language.python: case Language.pythonAsgi: this.options = { pythonRuntime: yamlConfiguration.backend.language.runtime, architecture: yamlConfiguration.backend.language.architecture, }; break; } this.cloudProvider = cloudProvider || CloudProviderIdentifier.GENEZIO_CLOUD; this.workspace = new Workspace(yamlConfiguration.backend?.path || process.cwd()); // Generate AST Summary this.astSummary = { version: "2", classes: getAstSummary(sdkHandlerResponse.classesInfo), }; this.classes = this.astSummary.classes.map((c) => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const yamlClass = yamlConfiguration.backend?.classes?.find((yamlC) => path.join(yamlConfiguration.backend?.path || ".", yamlC.path) === c.path); if (!yamlClass) { throw new UserError(`[Project Configuration] Class configuration not found for ${c.path}`); } const methods = c?.methods.map((m) => { const yamlMethod = yamlClass.methods?.find((yamlM) => yamlM.name === m.name); const cronString = yamlMethod !== undefined && (yamlMethod?.type === TriggerType.cron || (yamlMethod?.type === undefined && yamlClass.type === TriggerType.cron)) ? yamlMethod.cronString : undefined; return { name: m.name, parameters: m.params.map((p) => new ParameterType(p.name, p.type, p.optional)), cronString: cronString, language: c.language, auth: yamlMethod?.auth, type: yamlMethod?.type || yamlClass.type || TriggerType.jsonrpc, returnType: m.returnType, docString: m.docString, }; }); return { name: c.name, path: c.path, type: yamlClass?.type ?? TriggerType.jsonrpc, language: yamlConfiguration.backend?.language.name || "ts", methods: methods, types: c.types, version: this.astSummary.version, docString: c.docString, timeout: c.timeout, storageSize: c.storageSize, instanceSize: c.instanceSize, vcpuCount: c.vcpuCount, memoryMb: c.memoryMb, maxConcurrentRequestsPerInstance: c.maxConcurrentRequestsPerInstance, maxConcurrentInstances: c.maxConcurrentInstances, cooldownTime: c.cooldownTime, persistent: c.persistent, }; }); this.functions = yamlConfiguration.backend?.functions?.map((f) => { return { name: `function-${f.name}`, path: f.path, language: yamlConfiguration.backend?.language.name || "ts", handler: f.handler || "handler", entry: f.entry, type: f.type || FunctionType.aws, timeout: f.timeout, storageSize: f.storageSize, instanceSize: f.instanceSize, vcpuCount: f.vcpuCount, memoryMb: f.memoryMb, maxConcurrentRequestsPerInstance: f.maxConcurrentRequestsPerInstance, maxConcurrentInstances: f.maxConcurrentInstances, cooldownTime: f.cooldownTime, healthcheckPath: f.healthcheckPath, }; }) || []; } }