|
| 1 | +use oxc_allocator::{Allocator, Vec}; |
| 2 | +use oxc_ast::{ |
| 3 | + ast::{ |
| 4 | + Argument, BindingPatternKind, BindingProperty, ImportDeclarationSpecifier, |
| 5 | + ImportOrExportKind, Statement, VariableDeclarationKind, |
| 6 | + }, |
| 7 | + AstBuilder, NONE, |
| 8 | +}; |
| 9 | +use oxc_span::{SourceType, SPAN}; |
| 10 | + |
| 11 | +use crate::js_transformer::instructions::FileInstructions; |
| 12 | + |
| 13 | +const INSTRUMENT_IMPORT_SOURCE: &str = "@aikidosec/firewall/instrument/internals"; |
| 14 | +const INSTRUMENT_INSPECT_ARGS_METHOD_NAME: &str = "__instrumentInspectArgs"; |
| 15 | +const INSTRUMENT_MODIFY_ARGS_METHOD_NAME: &str = "__instrumentModifyArgs"; |
| 16 | +const INSTRUMENT_MODIFY_RETURN_VALUE_METHOD_NAME: &str = "__instrumentModifyReturnValue"; |
| 17 | + |
| 18 | +pub fn insert_import_statement<'a>( |
| 19 | + source_type: &SourceType, |
| 20 | + has_module_syntax: bool, |
| 21 | + allocator: &'a Allocator, |
| 22 | + builder: &'a AstBuilder, |
| 23 | + body: &mut Vec<'a, Statement<'a>>, |
| 24 | + file_instructions: &FileInstructions, |
| 25 | +) { |
| 26 | + // Common JS require() statement |
| 27 | + |
| 28 | + if source_type.is_script() || source_type.is_unambiguous() && !has_module_syntax { |
| 29 | + let mut require_args: Vec<'a, Argument<'a>> = builder.vec_with_capacity(1); |
| 30 | + require_args.push(Argument::StringLiteral(builder.alloc_string_literal( |
| 31 | + SPAN, |
| 32 | + allocator.alloc_str(INSTRUMENT_IMPORT_SOURCE), |
| 33 | + None, |
| 34 | + ))); |
| 35 | + |
| 36 | + let mut binding_properties: Vec<'a, BindingProperty<'a>> = builder.vec_with_capacity(3); |
| 37 | + |
| 38 | + if file_instructions.functions.iter().any(|f| f.inspect_args) { |
| 39 | + binding_properties.push(builder.binding_property( |
| 40 | + SPAN, |
| 41 | + builder.property_key_static_identifier(SPAN, INSTRUMENT_INSPECT_ARGS_METHOD_NAME), |
| 42 | + builder.binding_pattern( |
| 43 | + builder.binding_pattern_kind_binding_identifier( |
| 44 | + SPAN, |
| 45 | + INSTRUMENT_INSPECT_ARGS_METHOD_NAME, |
| 46 | + ), |
| 47 | + NONE, |
| 48 | + false, |
| 49 | + ), |
| 50 | + true, |
| 51 | + false, |
| 52 | + )); |
| 53 | + } |
| 54 | + |
| 55 | + if file_instructions.functions.iter().any(|f| f.modify_args) { |
| 56 | + binding_properties.push(builder.binding_property( |
| 57 | + SPAN, |
| 58 | + builder.property_key_static_identifier(SPAN, INSTRUMENT_MODIFY_ARGS_METHOD_NAME), |
| 59 | + builder.binding_pattern( |
| 60 | + builder.binding_pattern_kind_binding_identifier( |
| 61 | + SPAN, |
| 62 | + INSTRUMENT_MODIFY_ARGS_METHOD_NAME, |
| 63 | + ), |
| 64 | + NONE, |
| 65 | + false, |
| 66 | + ), |
| 67 | + true, |
| 68 | + false, |
| 69 | + )); |
| 70 | + } |
| 71 | + |
| 72 | + if file_instructions |
| 73 | + .functions |
| 74 | + .iter() |
| 75 | + .any(|f| f.modify_return_value) |
| 76 | + { |
| 77 | + binding_properties.push(builder.binding_property( |
| 78 | + SPAN, |
| 79 | + builder.property_key_static_identifier( |
| 80 | + SPAN, |
| 81 | + INSTRUMENT_MODIFY_RETURN_VALUE_METHOD_NAME, |
| 82 | + ), |
| 83 | + builder.binding_pattern( |
| 84 | + builder.binding_pattern_kind_binding_identifier( |
| 85 | + SPAN, |
| 86 | + INSTRUMENT_MODIFY_RETURN_VALUE_METHOD_NAME, |
| 87 | + ), |
| 88 | + NONE, |
| 89 | + false, |
| 90 | + ), |
| 91 | + true, |
| 92 | + false, |
| 93 | + )); |
| 94 | + } |
| 95 | + |
| 96 | + let mut declarations = builder.vec_with_capacity(1); |
| 97 | + declarations.push(builder.variable_declarator( |
| 98 | + SPAN, |
| 99 | + VariableDeclarationKind::Const, |
| 100 | + builder.binding_pattern( |
| 101 | + BindingPatternKind::ObjectPattern(builder.alloc_object_pattern( |
| 102 | + SPAN, |
| 103 | + binding_properties, |
| 104 | + NONE, |
| 105 | + )), |
| 106 | + NONE, |
| 107 | + false, |
| 108 | + ), |
| 109 | + Some(builder.expression_call( |
| 110 | + SPAN, |
| 111 | + builder.expression_identifier(SPAN, "require"), |
| 112 | + NONE, |
| 113 | + require_args, |
| 114 | + false, |
| 115 | + )), |
| 116 | + false, |
| 117 | + )); |
| 118 | + |
| 119 | + let var_declaration = Statement::VariableDeclaration(builder.alloc_variable_declaration( |
| 120 | + SPAN, |
| 121 | + VariableDeclarationKind::Const, |
| 122 | + declarations, |
| 123 | + false, |
| 124 | + )); |
| 125 | + |
| 126 | + body.insert(0, var_declaration); |
| 127 | + |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + let mut specifiers: Vec<'a, ImportDeclarationSpecifier<'a>> = builder.vec_with_capacity(3); |
| 132 | + |
| 133 | + if file_instructions.functions.iter().any(|f| f.inspect_args) { |
| 134 | + specifiers.push(builder.import_declaration_specifier_import_specifier( |
| 135 | + SPAN, |
| 136 | + builder.module_export_name_identifier_name(SPAN, INSTRUMENT_INSPECT_ARGS_METHOD_NAME), |
| 137 | + builder.binding_identifier(SPAN, INSTRUMENT_INSPECT_ARGS_METHOD_NAME), |
| 138 | + ImportOrExportKind::Value, |
| 139 | + )); |
| 140 | + } |
| 141 | + |
| 142 | + if file_instructions.functions.iter().any(|f| f.modify_args) { |
| 143 | + specifiers.push(builder.import_declaration_specifier_import_specifier( |
| 144 | + SPAN, |
| 145 | + builder.module_export_name_identifier_name(SPAN, INSTRUMENT_MODIFY_ARGS_METHOD_NAME), |
| 146 | + builder.binding_identifier(SPAN, INSTRUMENT_MODIFY_ARGS_METHOD_NAME), |
| 147 | + ImportOrExportKind::Value, |
| 148 | + )); |
| 149 | + } |
| 150 | + |
| 151 | + if file_instructions |
| 152 | + .functions |
| 153 | + .iter() |
| 154 | + .any(|f| f.modify_return_value) |
| 155 | + { |
| 156 | + specifiers.push(builder.import_declaration_specifier_import_specifier( |
| 157 | + SPAN, |
| 158 | + builder.module_export_name_identifier_name( |
| 159 | + SPAN, |
| 160 | + INSTRUMENT_MODIFY_RETURN_VALUE_METHOD_NAME, |
| 161 | + ), |
| 162 | + builder.binding_identifier(SPAN, INSTRUMENT_MODIFY_RETURN_VALUE_METHOD_NAME), |
| 163 | + ImportOrExportKind::Value, |
| 164 | + )); |
| 165 | + } |
| 166 | + |
| 167 | + let import_stmt = Statement::ImportDeclaration(builder.alloc_import_declaration( |
| 168 | + SPAN, |
| 169 | + Some(specifiers), |
| 170 | + builder.string_literal(SPAN, allocator.alloc_str(INSTRUMENT_IMPORT_SOURCE), None), |
| 171 | + None, |
| 172 | + NONE, |
| 173 | + ImportOrExportKind::Value, |
| 174 | + )); |
| 175 | + |
| 176 | + body.insert(0, import_stmt); |
| 177 | +} |
0 commit comments