flora is a fast and secure runtime that lets you write discord bots for your servers, with a rich TypeScript SDK, without worrying about running infrastructure. [mirror]
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: make bundle limits configurable

+75 -11
+32 -4
apps/runtime/src/bundler.rs
··· 1 1 use crate::transpile::transpile_if_typescript; 2 2 use deno_core::ModuleName; 3 + use flora_config::RuntimeConfig; 3 4 use oxc::{ 4 5 allocator::Allocator, 5 6 ast::ast::{ ··· 49 50 const MAX_FILES: usize = 200; 50 51 const MAX_TOTAL_BYTES: usize = 1_048_576; 51 52 53 + #[derive(Debug, Clone, Copy)] 54 + pub struct BundleLimits { 55 + pub max_files: usize, 56 + pub max_total_bytes: usize, 57 + } 58 + 59 + impl BundleLimits { 60 + pub fn from_config(config: &RuntimeConfig) -> Self { 61 + Self { 62 + max_files: config.max_bundle_files, 63 + max_total_bytes: config.max_bundle_total_bytes, 64 + } 65 + } 66 + } 67 + 68 + impl Default for BundleLimits { 69 + fn default() -> Self { 70 + Self { 71 + max_files: MAX_FILES, 72 + max_total_bytes: MAX_TOTAL_BYTES, 73 + } 74 + } 75 + } 76 + 52 77 pub fn bundle_files( 53 78 bundle_name: &str, 54 79 entry: &str, 55 80 files: &[DeploymentFile], 81 + limits: BundleLimits, 56 82 ) -> Result<BundleOutput, BundleError> { 57 - if files.len() > MAX_FILES { 83 + if files.len() > limits.max_files { 58 84 return Err(BundleError::InvalidPath(format!( 59 - "too many files (max {MAX_FILES})" 85 + "too many files (max {})", 86 + limits.max_files 60 87 ))); 61 88 } 62 89 ··· 64 91 let mut file_map = HashMap::new(); 65 92 for file in files { 66 93 total_bytes = total_bytes.saturating_add(file.contents.len()); 67 - if total_bytes > MAX_TOTAL_BYTES { 94 + if total_bytes > limits.max_total_bytes { 68 95 return Err(BundleError::InvalidPath(format!( 69 - "bundle exceeds size limit (max {MAX_TOTAL_BYTES} bytes)" 96 + "bundle exceeds size limit (max {} bytes)", 97 + limits.max_total_bytes 70 98 ))); 71 99 } 72 100 let normalized = normalize_path(&file.path)?;
+9 -3
apps/runtime/src/discord_handler.rs
··· 1 1 use crate::{ 2 - bundler::{DeploymentFile, bundle_files}, 2 + bundler::{BundleLimits, DeploymentFile, bundle_files}, 3 3 deployments::DeploymentService, 4 4 runtime::BotRuntime, 5 5 }; ··· 19 19 pub runtime: Arc<BotRuntime>, 20 20 pub http: Arc<serenity::http::Http>, 21 21 pub application_id: Arc<std::sync::RwLock<Option<ApplicationId>>>, 22 + pub bundle_limits: BundleLimits, 22 23 pub deployments: DeploymentService, 23 24 } 24 25 ··· 817 818 }, 818 819 ]; 819 820 let bundle_name = format!("guild:{guild_str}.bundle.js"); 820 - let bundle = bundle_files(&bundle_name, DEFAULT_GUILD_ENTRY, &files) 821 - .map_err(|err| eyre!(err.to_string()))?; 821 + let bundle = bundle_files( 822 + &bundle_name, 823 + DEFAULT_GUILD_ENTRY, 824 + &files, 825 + self.bundle_limits, 826 + ) 827 + .map_err(|err| eyre!(err.to_string()))?; 822 828 let deployment = self 823 829 .deployments 824 830 .upsert_deployment(
+7 -2
apps/runtime/src/handlers/deployments/upsert.rs
··· 89 89 ensure_guild_admin(&state, &identity, &guild_id).await?; 90 90 91 91 let bundle_name = format!("guild:{guild_id}.bundle.js"); 92 - let bundled = bundle_files(&bundle_name, &request.entry, &request.files) 93 - .map_err(|err| ApiError::bad_request(err.to_string()))?; 92 + let bundled = bundle_files( 93 + &bundle_name, 94 + &request.entry, 95 + &request.files, 96 + state.bundle_limits, 97 + ) 98 + .map_err(|err| ApiError::bad_request(err.to_string()))?; 94 99 95 100 let deployment = state 96 101 .deployments
+3
apps/runtime/src/main.rs
··· 141 141 let app_info = http.get_current_application_info().await?; 142 142 http.set_application_id(app_info.id); 143 143 144 + let bundle_limits = bundler::BundleLimits::from_config(&config.runtime); 144 145 let runtime = Arc::new(BotRuntime::new( 145 146 http.clone(), 146 147 kv_service.clone(), ··· 172 173 runtime: runtime.clone(), 173 174 http: http.clone(), 174 175 application_id: Arc::new(std::sync::RwLock::new(Some(app_info.id))), 176 + bundle_limits, 175 177 deployments: deployment_service.clone(), 176 178 }); 177 179 ··· 186 188 tokens: token_service.clone(), 187 189 kv: kv_service.clone(), 188 190 secrets: secret_service.clone(), 191 + bundle_limits, 189 192 http: http.clone(), 190 193 }; 191 194
+4 -2
apps/runtime/src/state.rs
··· 1 1 use crate::{ 2 - auth::AuthService, deployments::DeploymentService, kv::KvService, runtime::BotRuntime, 3 - secrets::SecretService, tokens::TokenService, 2 + auth::AuthService, bundler::BundleLimits, deployments::DeploymentService, kv::KvService, 3 + runtime::BotRuntime, secrets::SecretService, tokens::TokenService, 4 4 }; 5 5 use serenity::http::Http; 6 6 use std::sync::Arc; ··· 20 20 pub kv: KvService, 21 21 /// Secret storage and encryption. 22 22 pub secrets: SecretService, 23 + /// Deployment bundle size/count limits. 24 + pub bundle_limits: BundleLimits, 23 25 /// Bot HTTP client for guild permission checks. 24 26 pub http: Arc<Http>, 25 27 }
+14
config.template.toml
··· 116 116 # Default value: 1048576 117 117 # max_script_bytes = 1048576 118 118 119 + # Maximum number of deployment files accepted by bundler. 120 + # 121 + # Can also be specified via environment variable `RUNTIME_MAX_BUNDLE_FILES`. 122 + # 123 + # Default value: 200 124 + # max_bundle_files = 200 125 + 126 + # Maximum total deployment bundle source bytes accepted by bundler. 127 + # 128 + # Can also be specified via environment variable `RUNTIME_MAX_BUNDLE_TOTAL_BYTES`. 129 + # 130 + # Default value: 1048576 131 + # max_bundle_total_bytes = 1048576 132 + 119 133 # Maximum number of cron jobs per guild (or default runtime). 120 134 # 121 135 # Can also be specified via environment variable `RUNTIME_MAX_CRON_JOBS`.
+6
crates/flora_config/src/lib.rs
··· 93 93 /// Max script size in bytes (SDK + deployment). Default: 1MB. 94 94 #[config(env = "RUNTIME_MAX_SCRIPT_BYTES", default = 1_048_576)] 95 95 pub max_script_bytes: usize, 96 + /// Maximum number of deployment files accepted by bundler. 97 + #[config(env = "RUNTIME_MAX_BUNDLE_FILES", default = 200)] 98 + pub max_bundle_files: usize, 99 + /// Maximum total deployment bundle source bytes accepted by bundler. 100 + #[config(env = "RUNTIME_MAX_BUNDLE_TOTAL_BYTES", default = 1_048_576)] 101 + pub max_bundle_total_bytes: usize, 96 102 /// Maximum number of cron jobs per guild (or default runtime). 97 103 #[config(env = "RUNTIME_MAX_CRON_JOBS", default = 32)] 98 104 pub max_cron_jobs: usize,