Repository: awslabs/llrt
File: llrt_core/src/vm.rs (Rust)

Code before:
    )?;
    globals.set("__bootstrap", js_bootstrap)?;

    globals.set(
        "require",
        Func::from(move |ctx, specifier: String| -> Result<Value> {
            struct Args<'js>(Ctx<'js>);
            let Args(ctx) = Args(ctx);
            let specifier = if let Some(striped_specifier) = &specifier.strip_prefix("node:") {
                striped_specifier.to_string()
            } else {
                specifier
            };
            let import_name = if module_names.contains(specifier.as_str())
                || BYTECODE_CACHE.contains_key(&specifier)
                || specifier.starts_with('/')
            {
                specifier
            } else {
                let module_name = get_script_or_module_name(ctx.clone());
                let abs_path = resolve_path([module_name].iter());
                let import_directory = dirname(abs_path);
                let ads_specifier = if specifier.ends_with(".js") || specifier.ends_with(".cjs") {
                    specifier
                } else if specifier.starts_with("./") || specifier.starts_with("../") {
                    format!("{}/{}.js", import_directory, specifier)
                } else {
                    get_module_path_in_node_modules(&ctx, &specifier)?
                };
                resolve_path([import_directory, ads_specifier].iter())
            };

            if import_name.ends_with(".json") {
                let source = std::fs::read_to_string(import_name)?;
                return json_parse(&ctx, source);
            }

            let mut map = require_in_progress.lock().unwrap();
            if let Some(obj) = map.get(&import_name) {
                return Ok(obj.clone().into_value());
            }

            let obj = Object::new(ctx.clone())?;
            map.insert(import_name.clone(), obj.clone());
            drop(map);

            trace!("Require: {}", import_name);

            let mut options = EvalOptions::default();
            options.strict = false;
            options.promise = true;

Review comment (suggestion):
Is it always .js? might be .mjs or .cjs no?
Use `[import_directory,"/",specifier,".js"].concat()` here

Code after:
        Result::Ok(true)
    }))?;

    globals.prop("exports", exports_proxy)?;

    globals.set("__bootstrap", Object::new(ctx.clone())?)?;

    globals.set(
        "require",
        Func::from(move |ctx, specifier: String| -> Result<Value> {
            struct Args<'js>(Ctx<'js>);
            let Args(ctx) = Args(ctx);
            let specifier = if let Some(striped_specifier) = &specifier.strip_prefix("node:") {
                striped_specifier.to_string()
            } else {
                specifier
            };
            let import_name = if module_names.contains(specifier.as_str())
                || BYTECODE_CACHE.contains_key(&specifier)
                || is_absolute(&specifier)
            {
                specifier
            } else {
                let module_name = get_script_or_module_name(ctx.clone());
                let abs_path = resolve_path([module_name].iter());
                let import_directory = dirname(abs_path);
                let ads_specifier = if specifier.ends_with(".js")
                    || specifier.ends_with(".mjs")
                    || specifier.ends_with(".cjs")
                {
                    specifier
                } else if specifier.starts_with("./") || specifier.starts_with("../") {
                    [&import_directory, "/", &specifier].concat()
                } else {
                    get_module_path_in_node_modules(&ctx, &specifier)?
                };
                resolve_path([import_directory, ads_specifier].iter())
            };

            if import_name.ends_with(".json") {
                let source = std::fs::read_to_string(import_name)?;
                return json_parse(&ctx, source);
            }

            let mut map = require_in_progress.lock().unwrap();
            if let Some(obj) = map.get(&import_name) {
                return Ok(obj.clone().into_value());
            }

            let obj = Object::new(ctx.clone())?;
            map.insert(import_name.clone(), obj.clone());