File size: 4,375 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use swc_core::{
    ecma::{
        ast::*,
        visit::{VisitMut, VisitMutWith},
    },
    quote,
};

enum WrappedExpr {
    Eval,
    WasmCompile,
    WasmInstantiate,
}

/// Replaces call / expr to dynamic evaluation in the give code to
/// wrapped expression (__next_eval__, __next_webassembly_compile__,..) to raise
/// corresponding error.
///
/// This transform is specific to edge runtime which are not allowed to
/// call certain dynamic evaluation (eval, webassembly.instantiate, etc)
///
/// check middleware-plugin for corresponding webpack side transform.
pub fn next_middleware_dynamic() -> MiddlewareDynamic {
    MiddlewareDynamic {}
}

pub struct MiddlewareDynamic {}

impl VisitMut for MiddlewareDynamic {
    fn visit_mut_expr(&mut self, expr: &mut Expr) {
        let mut should_wrap = None;

        expr.visit_mut_children_with(self);

        if let Expr::Call(call_expr) = &expr {
            let callee = &call_expr.callee;
            if let Callee::Expr(callee) = callee {
                // `eval('some')`, or `Function('some')`
                if let Expr::Ident(ident) = &**callee {
                    if ident.sym == "eval" || ident.sym == "Function" {
                        should_wrap = Some(WrappedExpr::Eval);
                    }
                }

                if let Expr::Member(MemberExpr {
                    obj,
                    prop: MemberProp::Ident(prop_ident),
                    ..
                }) = &**callee
                {
                    if let Expr::Ident(ident) = &**obj {
                        // `global.eval('some')`
                        if ident.sym == "global" && prop_ident.sym == "eval" {
                            should_wrap = Some(WrappedExpr::Eval);
                        }

                        // `WebAssembly.compile('some')` & `WebAssembly.instantiate('some')`
                        if ident.sym == "WebAssembly" {
                            if prop_ident.sym == "compile" {
                                should_wrap = Some(WrappedExpr::WasmCompile);
                            } else if prop_ident.sym == "instantiate" {
                                should_wrap = Some(WrappedExpr::WasmInstantiate);
                            }
                        }
                    }

                    if let Expr::Member(MemberExpr {
                        obj,
                        prop: MemberProp::Ident(member_prop_ident),
                        ..
                    }) = &**obj
                    {
                        if let Expr::Ident(ident) = &**obj {
                            // `global.WebAssembly.compile('some')` &
                            // `global.WebAssembly.instantiate('some')`
                            if ident.sym == "global" && member_prop_ident.sym == "WebAssembly" {
                                if prop_ident.sym == "compile" {
                                    should_wrap = Some(WrappedExpr::WasmCompile);
                                } else if prop_ident.sym == "instantiate" {
                                    should_wrap = Some(WrappedExpr::WasmInstantiate);
                                }
                            }
                        }
                    }
                }
            }

            match should_wrap {
                Some(WrappedExpr::Eval) => {
                    *expr = quote!("__next_eval__(function() { return $orig_call });" as Expr, orig_call: Expr = Expr::Call(call_expr.clone()));
                }
                Some(WrappedExpr::WasmCompile) => {
                    *expr = quote!("__next_webassembly_compile__(function() { return $orig_call });" as Expr, orig_call: Expr = Expr::Call(call_expr.clone()));
                }
                Some(WrappedExpr::WasmInstantiate) => {
                    *expr = quote!("__next_webassembly_instantiate__(function() { return $orig_call });" as Expr, orig_call: Expr = Expr::Call(call_expr.clone()));
                }
                None => {}
            }
        }

        if let Expr::New(NewExpr { callee, .. }) = &expr {
            if let Expr::Ident(ident) = &**callee {
                if ident.sym == "Function" {
                    *expr = quote!("__next_eval__(function() { return $orig_call });" as Expr, orig_call: Expr = expr.clone());
                }
            }
        }
    }
}