forklaunch 1.15.0

Launch faster with forklaunch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use std::{collections::HashMap, fs::read_to_string, path::Path};

use anyhow::{Context, Result, bail};
use convert_case::{Case, Casing};
use oxc_allocator::Allocator;
use oxc_ast::ast::SourceType;
use oxc_codegen::{Codegen, CodegenOptions};
use serde_json::from_str;

use crate::{
    constants::{
        ERROR_FAILED_TO_PARSE_PACKAGE_JSON, ERROR_FAILED_TO_READ_PACKAGE_JSON,
        error_failed_to_read_file,
    },
    core::package_json::package_json_constants::CORE_VERSION,
    core::{
        ast::{
            injections::inject_into_client_sdk::{ClientSdkSpecialCase, inject_into_client_sdk},
            parse_ast_program::parse_ast_program,
            transformations::transform_client_sdk::{
                transform_client_sdk_add_sdk_with_special_case, transform_client_sdk_change_sdk,
                transform_client_sdk_remove_sdk,
            },
        },
        manifest::{ProjectEntry, ProjectType},
        package_json::project_package_json::ProjectPackageJson,
        rendered_template::{RenderedTemplate, RenderedTemplatesCache},
    },
};

/// Regenerates `client-sdk/compliance.ts` based on the current project list.
///
/// The generated file imports per-service SDK factories from `./clientSdk` and
/// hardcodes parallel `Promise.all` calls to each service's
/// `compliance.eraseUserData` / `exportUserData` method. Result types are
/// inferred from the SDK factories so callers narrow on the real discriminated
/// `code` union without casts.
///
/// A project participates if:
/// - it is a Service or Worker (libraries are skipped)
/// - it has a database resource (compliance erase/export touches persisted data)
/// - the app has an `iam` project (the generated controller resolves
///   `JWKS_PUBLIC_KEY_URL`, which only exists when iam is configured)
///
/// Special-case: when the iam project's variant is `iam-better-auth`, the
/// `clientIamSdkClient` factory returns `{ core, betterAuth }`, so compliance
/// is accessed via `config.iam.core.compliance.*`.
pub(crate) fn regenerate_client_sdk_compliance(
    rendered_templates_cache: &mut RenderedTemplatesCache,
    base_path: &Path,
    projects: &[ProjectEntry],
) -> Result<()> {
    let path = base_path.join("client-sdk").join("compliance.ts");

    let stub = || RenderedTemplate {
        path: path.clone(),
        content: "// This file is regenerated automatically when services are added, removed,\n// or renamed. Do not edit by hand — your changes will be overwritten.\n//\n// When at least one db-backed service exists alongside an iam project, this\n// file will export `createComplianceClient` with hardcoded calls to each\n// service's `compliance.eraseUserData` / `exportUserData` SDK method.\n\nexport {};\n".to_string(),
        context: Some("Failed to write client-sdk compliance.ts".to_string()),
    };

    // Compliance controllers depend on JWKS_PUBLIC_KEY_URL, which only exists
    // when an iam project is configured. Without iam, no service exposes a
    // working `compliance.eraseUserData`/`exportUserData` SDK method.
    if !projects.iter().any(|p| p.name == "iam") {
        rendered_templates_cache
            .insert(path.to_string_lossy().to_string(), stub());
        return Ok(());
    }

    let mut compliant: Vec<&ProjectEntry> = projects
        .iter()
        .filter(|p| {
            matches!(p.r#type, ProjectType::Service | ProjectType::Worker)
                && p.resources
                    .as_ref()
                    .and_then(|r| r.database.as_ref())
                    .is_some()
        })
        .collect();
    compliant.sort_by(|a, b| a.name.cmp(&b.name));

    if compliant.is_empty() {
        rendered_templates_cache
            .insert(path.to_string_lossy().to_string(), stub());
        return Ok(());
    }

    let mut imports: Vec<String> = Vec::new();
    let mut type_aliases: Vec<String> = Vec::new();
    let mut config_fields: Vec<String> = Vec::new();
    let mut destructure_names: Vec<String> = Vec::new();
    let mut erase_calls: Vec<String> = Vec::new();
    let mut export_calls: Vec<String> = Vec::new();

    for project in &compliant {
        let camel = project.name.to_case(Case::Camel);
        let pascal = project.name.to_case(Case::Pascal);

        let factory = format!("{}SdkClient", camel);
        let type_alias = format!("{}Client", pascal);

        // `clientIamSdkClient` is emitted as a wrapped `{ core, betterAuth }`
        // factory when the iam project uses the better-auth variant.
        let is_better_auth = project.name == "iam"
            && project.variant.as_deref() == Some("iam-better-auth");
        let access = if is_better_auth {
            format!("config.{}.core.compliance", camel)
        } else {
            format!("config.{}.compliance", camel)
        };

        imports.push(factory.clone());
        type_aliases.push(format!(
            "type {} = Awaited<ReturnType<typeof {}>>;",
            type_alias, factory
        ));
        config_fields.push(format!("  {}: {};", camel, type_alias));
        destructure_names.push(camel.clone());
        erase_calls.push(format!(
            "        {}.eraseUserData({{ params: {{ userId }}, headers }})",
            access
        ));
        export_calls.push(format!(
            "        {}.exportUserData({{ params: {{ userId }}, headers }})",
            access
        ));
    }

    let imports_line = format!("import {{ {} }} from './clientSdk';", imports.join(", "));
    let type_aliases_block = type_aliases.join("\n");
    let config_fields_block = config_fields.join("\n");
    let destructure = destructure_names.join(", ");
    let return_object = destructure_names.join(", ");
    let erase_block = erase_calls.join(",\n");
    let export_block = export_calls.join(",\n");

    let content = format!(
"{imports_line}

{type_aliases_block}

/**
 * Compliance fan-out client. Calls erase/export on every registered service
 * in parallel. Per-service responses are the exact discriminated unions
 * produced by each SDK (e.g. `{{ code: 200; response: {{...}} }} | {{ code: 404; response: string }}`),
 * so callers narrow on `code` without any casts.
 *
 * This file is regenerated when services are added, removed, or renamed.
 *
 * Requires a JWT token from a user with SYSTEM role.
 */
export function createComplianceClient(config: {{
  token: string;
{config_fields_block}
}}) {{
  const headers = {{ authorization: `Bearer ${{config.token}}` }} as const;

  return {{
    async erase(userId: string) {{
      const [{destructure}] = await Promise.all([
{erase_block}
      ]);
      return {{ {return_object} }};
    }},

    async export(userId: string) {{
      const [{destructure}] = await Promise.all([
{export_block}
      ]);
      return {{ {return_object} }};
    }}
  }};
}}
"
    );

    rendered_templates_cache.insert(
        path.to_string_lossy().to_string(),
        RenderedTemplate {
            path,
            content,
            context: Some("Failed to write client-sdk compliance.ts".to_string()),
        },
    );

    Ok(())
}

pub(crate) fn get_client_sdk_additional_deps(
    app_name: &String,
    is_billing_enabled: bool,
    is_iam_enabled: bool,
    is_messaging_enabled: bool,
    is_cac_enabled: bool,
) -> HashMap<String, String> {
    let mut additional_deps = HashMap::new();

    if is_billing_enabled {
        additional_deps.insert(format!("@{app_name}/billing"), "workspace:*".to_string());
    }
    if is_iam_enabled {
        additional_deps.insert(format!("@{app_name}/iam"), "workspace:*".to_string());
    }
    if is_messaging_enabled {
        additional_deps.insert(format!("@{app_name}/messaging"), "workspace:*".to_string());
    }
    if is_cac_enabled {
        additional_deps.insert(format!("@{app_name}/cac"), "workspace:*".to_string());
    }
    additional_deps
}

pub(crate) fn add_project_to_client_sdk(
    rendered_templates_cache: &mut RenderedTemplatesCache,
    base_path: &Path,
    app_name: &str,
    name: &str,
    special_case: Option<ClientSdkSpecialCase>,
) -> Result<()> {
    let kebab_case_app_name = &app_name.to_case(Case::Kebab);
    let kebab_case_name = &name.to_case(Case::Kebab);

    let sdk_ts_path = base_path.join("client-sdk").join("clientSdk.ts");
    let sdk_package_json_path = base_path.join("client-sdk").join("package.json");

    let new_ts_content = transform_client_sdk_add_sdk_with_special_case(
        rendered_templates_cache,
        base_path,
        app_name,
        name,
        special_case,
    )?;

    rendered_templates_cache.insert(
        sdk_ts_path.to_string_lossy().to_string(),
        RenderedTemplate {
            path: sdk_ts_path,
            content: new_ts_content,
            context: Some("Failed to write client SDK".to_string()),
        },
    );

    let sdk_pkg_template = rendered_templates_cache.get(&sdk_package_json_path)?;
    let mut client_sdk_project_json: ProjectPackageJson = if let Some(template) = sdk_pkg_template {
        from_str(&template.content).context(ERROR_FAILED_TO_PARSE_PACKAGE_JSON)?
    } else {
        from_str(
            &read_to_string(&sdk_package_json_path)
                .with_context(|| ERROR_FAILED_TO_READ_PACKAGE_JSON)?,
        )
        .with_context(|| ERROR_FAILED_TO_PARSE_PACKAGE_JSON)?
    };

    client_sdk_project_json
        .dev_dependencies
        .as_mut()
        .unwrap()
        .additional_deps
        .insert(
            format!("@{}/{}", &kebab_case_app_name, &kebab_case_name),
            "workspace:*".to_string(),
        );

    // Ensure @forklaunch/core is a dependency (needed by compliance.ts)
    if let Some(ref mut deps) = client_sdk_project_json.dependencies {
        if deps.forklaunch_core.is_none() {
            deps.forklaunch_core = Some(CORE_VERSION.to_string());
        }
    }

    rendered_templates_cache.insert(
        sdk_package_json_path.to_string_lossy().to_string(),
        RenderedTemplate {
            path: sdk_package_json_path,
            content: serde_json::to_string_pretty(&client_sdk_project_json)?,
            context: Some("Failed to write SDK package.json".to_string()),
        },
    );

    Ok(())
}

pub(crate) fn remove_project_from_client_sdk(
    rendered_templates_cache: &mut RenderedTemplatesCache,
    base_path: &Path,
    app_name: &str,
    name: &str,
) -> Result<()> {
    let kebab_case_app_name = &app_name.to_case(Case::Kebab);
    let kebab_case_name = &name.to_case(Case::Kebab);

    let sdk_ts_path = base_path.join("client-sdk").join("clientSdk.ts");
    let sdk_package_json_path = base_path.join("client-sdk").join("package.json");

    let new_ts_content =
        transform_client_sdk_remove_sdk(rendered_templates_cache, base_path, app_name, name)?;

    rendered_templates_cache.insert(
        sdk_ts_path.to_string_lossy().to_string(),
        RenderedTemplate {
            path: sdk_ts_path,
            content: new_ts_content,
            context: Some("Failed to write client SDK".to_string()),
        },
    );

    let sdk_pkg_template = rendered_templates_cache.get(&sdk_package_json_path)?;
    let mut client_sdk_project_json: ProjectPackageJson = if let Some(template) = sdk_pkg_template {
        from_str(&template.content).context(ERROR_FAILED_TO_PARSE_PACKAGE_JSON)?
    } else {
        from_str(
            &read_to_string(&sdk_package_json_path)
                .with_context(|| ERROR_FAILED_TO_READ_PACKAGE_JSON)?,
        )
        .with_context(|| ERROR_FAILED_TO_PARSE_PACKAGE_JSON)?
    };

    if let Some(ref mut dev_deps) = client_sdk_project_json.dev_dependencies {
        dev_deps
            .additional_deps
            .remove(&format!("@{}/{}", &kebab_case_app_name, &kebab_case_name));
    }

    if let Some(ref mut deps) = client_sdk_project_json.dependencies {
        deps.additional_deps
            .remove(&format!("@{}/{}", &kebab_case_app_name, &kebab_case_name));
    }

    rendered_templates_cache.insert(
        sdk_package_json_path.to_string_lossy().to_string(),
        RenderedTemplate {
            path: sdk_package_json_path,
            content: serde_json::to_string_pretty(&client_sdk_project_json)?,
            context: Some("Failed to write SDK package.json".to_string()),
        },
    );

    Ok(())
}

pub(crate) fn change_project_in_client_sdk(
    rendered_templates: &mut RenderedTemplatesCache,
    base_path: &Path,
    app_name: &str,
    existing_name: &str,
    name: &str,
) -> Result<()> {
    let kebab_case_app_name = &app_name.to_case(Case::Kebab);
    let kebab_case_existing_name = &existing_name.to_case(Case::Kebab);
    let kebab_case_name = &name.to_case(Case::Kebab);

    rendered_templates.insert(
        base_path
            .join("client-sdk")
            .join("clientSdk.ts")
            .to_string_lossy(),
        RenderedTemplate {
            path: base_path.join("client-sdk").join("clientSdk.ts"),
            content: transform_client_sdk_change_sdk(
                rendered_templates,
                base_path,
                app_name,
                existing_name,
                name,
            )?,
            context: None,
        },
    );

    let sdk_package_json_path = base_path.join("client-sdk").join("package.json");
    let sdk_pkg_template = rendered_templates.get(&sdk_package_json_path)?;
    let mut client_sdk_project_json: ProjectPackageJson = if let Some(template) = sdk_pkg_template {
        from_str(&template.content).context(ERROR_FAILED_TO_PARSE_PACKAGE_JSON)?
    } else {
        bail!(error_failed_to_read_file(&sdk_package_json_path));
    };

    let additional_deps = &mut client_sdk_project_json
        .dev_dependencies
        .as_mut()
        .unwrap()
        .additional_deps;

    additional_deps.remove(&format!(
        "@{}/{}",
        &kebab_case_app_name, &kebab_case_existing_name
    ));
    additional_deps.insert(
        format!("@{}/{}", &kebab_case_app_name, &kebab_case_name),
        "workspace:*".to_string(),
    );

    rendered_templates.insert(
        sdk_package_json_path.to_string_lossy().to_string(),
        RenderedTemplate {
            path: sdk_package_json_path,
            content: serde_json::to_string_pretty(&client_sdk_project_json)?,
            context: None,
        },
    );

    Ok(())
}

pub(crate) fn add_project_vec_to_client_sdk<'a>(
    app_name: &str,
    projects_to_add: &Vec<String>,
    ast_program_text: &String,
    project_json: &mut ProjectPackageJson,
) -> Result<(String, ProjectPackageJson)> {
    let allocator = Allocator::default();
    let mut ast_program_ast = parse_ast_program(&allocator, ast_program_text, SourceType::ts());
    let kebab_case_app_name = &app_name.to_case(Case::Kebab);
    for project in projects_to_add {
        let kebab_case_project = &project.to_case(Case::Kebab);
        inject_into_client_sdk(
            &allocator,
            &mut ast_program_ast,
            app_name,
            kebab_case_project,
            "",
            None,
        )?;

        let kebab_case_project = &project.to_case(Case::Kebab);
        project_json
            .dev_dependencies
            .as_mut()
            .unwrap()
            .additional_deps
            .insert(
                format!("@{}/{}", &kebab_case_app_name, &kebab_case_project),
                "workspace:*".to_string(),
            );
    }

    // TODO: validate client SDK changes
    Ok((
        Codegen::new()
            .with_options(CodegenOptions::default())
            .build(&ast_program_ast)
            .code,
        project_json.clone(),
    ))
}