Commit 64731138 authored by Shu Ding's avatar Shu Ding Committed by Janka Uryga

Improve SWC transform ID generation (#69183)

This adds an extra `hash_salt` option to the Server Actions transform's
`generate_action_id` method which defaults to empty for now (so no
actual behavior changes). When set, the salt will be used to prefix the
payload.
parent 9c4efb98
...@@ -50,6 +50,7 @@ impl CustomTransformer for NextServerActions { ...@@ -50,6 +50,7 @@ impl CustomTransformer for NextServerActions {
Config { Config {
is_react_server_layer: matches!(self.transform, ActionsTransform::Server), is_react_server_layer: matches!(self.transform, ActionsTransform::Server),
enabled: true, enabled: true,
hash_salt: "".into(),
}, },
ctx.comments.clone(), ctx.comments.clone(),
); );
......
...@@ -28,6 +28,7 @@ use turbopack_binding::swc::core::{ ...@@ -28,6 +28,7 @@ use turbopack_binding::swc::core::{
pub struct Config { pub struct Config {
pub is_react_server_layer: bool, pub is_react_server_layer: bool,
pub enabled: bool, pub enabled: bool,
pub hash_salt: String,
} }
/// A mapping of hashed action id to the action's exported function name. /// A mapping of hashed action id to the action's exported function name.
...@@ -174,8 +175,11 @@ impl<C: Comments> ServerActions<C> { ...@@ -174,8 +175,11 @@ impl<C: Comments> ServerActions<C> {
.cloned() .cloned()
.map(|id| Some(id.as_arg())) .map(|id| Some(id.as_arg()))
.collect(), .collect(),
&self.file_name, generate_action_id(
export_name.to_string(), &self.config.hash_salt,
&self.file_name,
export_name.to_string().as_str(),
),
); );
if let BlockStmtOrExpr::BlockStmt(block) = &mut *a.body { if let BlockStmtOrExpr::BlockStmt(block) = &mut *a.body {
...@@ -223,7 +227,12 @@ impl<C: Comments> ServerActions<C> { ...@@ -223,7 +227,12 @@ impl<C: Comments> ServerActions<C> {
span: DUMMY_SP, span: DUMMY_SP,
callee: quote_ident!("decryptActionBoundArgs").as_callee(), callee: quote_ident!("decryptActionBoundArgs").as_callee(),
args: vec![ args: vec![
generate_action_id(&self.file_name, &export_name).as_arg(), generate_action_id(
&self.config.hash_salt,
&self.file_name,
&export_name,
)
.as_arg(),
quote_ident!("$$ACTION_CLOSURE_BOUND").as_arg(), quote_ident!("$$ACTION_CLOSURE_BOUND").as_arg(),
], ],
type_args: None, type_args: None,
...@@ -299,8 +308,7 @@ impl<C: Comments> ServerActions<C> { ...@@ -299,8 +308,7 @@ impl<C: Comments> ServerActions<C> {
.cloned() .cloned()
.map(|id| Some(id.as_arg())) .map(|id| Some(id.as_arg()))
.collect(), .collect(),
&self.file_name, generate_action_id(&self.config.hash_salt, &self.file_name, &export_name),
export_name.to_string(),
); );
f.body.visit_mut_with(&mut ClosureReplacer { f.body.visit_mut_with(&mut ClosureReplacer {
...@@ -347,7 +355,12 @@ impl<C: Comments> ServerActions<C> { ...@@ -347,7 +355,12 @@ impl<C: Comments> ServerActions<C> {
span: DUMMY_SP, span: DUMMY_SP,
callee: quote_ident!("decryptActionBoundArgs").as_callee(), callee: quote_ident!("decryptActionBoundArgs").as_callee(),
args: vec![ args: vec![
generate_action_id(&self.file_name, &export_name).as_arg(), generate_action_id(
&self.config.hash_salt,
&self.file_name,
&export_name,
)
.as_arg(),
quote_ident!("$$ACTION_CLOSURE_BOUND").as_arg(), quote_ident!("$$ACTION_CLOSURE_BOUND").as_arg(),
], ],
type_args: None, type_args: None,
...@@ -959,7 +972,8 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -959,7 +972,8 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let ident = Ident::new(id.0.clone(), DUMMY_SP.with_ctxt(id.1)); let ident = Ident::new(id.0.clone(), DUMMY_SP.with_ctxt(id.1));
if !self.config.is_react_server_layer { if !self.config.is_react_server_layer {
let action_id = generate_action_id(&self.file_name, export_name); let action_id =
generate_action_id(&self.config.hash_salt, &self.file_name, export_name);
if export_name == "default" { if export_name == "default" {
let export_expr = ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr( let export_expr = ModuleItem::ModuleDecl(ModuleDecl::ExportDefaultExpr(
...@@ -1009,8 +1023,11 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1009,8 +1023,11 @@ impl<C: Comments> VisitMut for ServerActions<C> {
expr: Box::new(annotate_ident_as_action( expr: Box::new(annotate_ident_as_action(
ident.clone(), ident.clone(),
Vec::new(), Vec::new(),
&self.file_name, generate_action_id(
export_name.to_string(), &self.config.hash_salt,
&self.file_name,
export_name,
),
)), )),
})); }));
} }
...@@ -1083,7 +1100,12 @@ impl<C: Comments> VisitMut for ServerActions<C> { ...@@ -1083,7 +1100,12 @@ impl<C: Comments> VisitMut for ServerActions<C> {
let actions = actions let actions = actions
.into_iter() .into_iter()
.map(|name| (generate_action_id(&self.file_name, &name), name)) .map(|name| {
(
generate_action_id(&self.config.hash_salt, &self.file_name, &name),
name,
)
})
.collect::<ActionsMap>(); .collect::<ActionsMap>();
// Prepend a special comment to the top of the file. // Prepend a special comment to the top of the file.
self.comments.add_leading( self.comments.add_leading(
...@@ -1230,10 +1252,11 @@ fn attach_name_to_expr(ident: Ident, expr: Expr, extra_items: &mut Vec<ModuleIte ...@@ -1230,10 +1252,11 @@ fn attach_name_to_expr(ident: Ident, expr: Expr, extra_items: &mut Vec<ModuleIte
} }
} }
fn generate_action_id(file_name: &str, export_name: &str) -> String { fn generate_action_id(hash_salt: &str, file_name: &str, export_name: &str) -> String {
// Attach a checksum to the action using sha1: // Attach a checksum to the action using sha1:
// $$id = sha1('file_name' + ':' + 'export_name'); // $$id = sha1('hash_salt' + 'file_name' + ':' + 'export_name');
let mut hasher = Sha1::new(); let mut hasher = Sha1::new();
hasher.update(hash_salt.as_bytes());
hasher.update(file_name.as_bytes()); hasher.update(file_name.as_bytes());
hasher.update(b":"); hasher.update(b":");
hasher.update(export_name.as_bytes()); hasher.update(export_name.as_bytes());
...@@ -1245,12 +1268,10 @@ fn generate_action_id(file_name: &str, export_name: &str) -> String { ...@@ -1245,12 +1268,10 @@ fn generate_action_id(file_name: &str, export_name: &str) -> String {
fn annotate_ident_as_action( fn annotate_ident_as_action(
ident: Ident, ident: Ident,
bound: Vec<Option<ExprOrSpread>>, bound: Vec<Option<ExprOrSpread>>,
file_name: &str, action_id: String,
export_name: String,
) -> Expr { ) -> Expr {
// Add the proxy wrapper call `registerServerReference($$id, $$bound, myAction, // Add the proxy wrapper call `registerServerReference($$id, $$bound, myAction,
// maybe_orig_action)`. // maybe_orig_action)`.
let action_id = generate_action_id(file_name, &export_name);
let proxy_expr = Expr::Call(CallExpr { let proxy_expr = Expr::Call(CallExpr {
span: DUMMY_SP, span: DUMMY_SP,
......
...@@ -178,7 +178,8 @@ fn react_server_actions_server_errors(input: PathBuf) { ...@@ -178,7 +178,8 @@ fn react_server_actions_server_errors(input: PathBuf) {
&FileName::Real("/app/item.js".into()), &FileName::Real("/app/item.js".into()),
server_actions::Config { server_actions::Config {
is_react_server_layer: true, is_react_server_layer: true,
enabled: true enabled: true,
hash_salt: "".into()
}, },
tr.comments.as_ref().clone(), tr.comments.as_ref().clone(),
) )
...@@ -214,7 +215,8 @@ fn react_server_actions_client_errors(input: PathBuf) { ...@@ -214,7 +215,8 @@ fn react_server_actions_client_errors(input: PathBuf) {
&FileName::Real("/app/item.js".into()), &FileName::Real("/app/item.js".into()),
server_actions::Config { server_actions::Config {
is_react_server_layer: false, is_react_server_layer: false,
enabled: true enabled: true,
hash_salt: "".into()
}, },
tr.comments.as_ref().clone(), tr.comments.as_ref().clone(),
) )
......
...@@ -381,7 +381,8 @@ fn server_actions_server_fixture(input: PathBuf) { ...@@ -381,7 +381,8 @@ fn server_actions_server_fixture(input: PathBuf) {
&FileName::Real("/app/item.js".into()), &FileName::Real("/app/item.js".into()),
server_actions::Config { server_actions::Config {
is_react_server_layer: true, is_react_server_layer: true,
enabled: true enabled: true,
hash_salt: "".into()
}, },
_tr.comments.as_ref().clone(), _tr.comments.as_ref().clone(),
) )
...@@ -405,7 +406,8 @@ fn server_actions_client_fixture(input: PathBuf) { ...@@ -405,7 +406,8 @@ fn server_actions_client_fixture(input: PathBuf) {
&FileName::Real("/app/item.js".into()), &FileName::Real("/app/item.js".into()),
server_actions::Config { server_actions::Config {
is_react_server_layer: false, is_react_server_layer: false,
enabled: true enabled: true,
hash_salt: "".into()
}, },
_tr.comments.as_ref().clone(), _tr.comments.as_ref().clone(),
) )
......
...@@ -206,6 +206,7 @@ function getBaseSWCOptions({ ...@@ -206,6 +206,7 @@ function getBaseSWCOptions({
// TODO: remove this option // TODO: remove this option
enabled: true, enabled: true,
isReactServerLayer, isReactServerLayer,
hashSalt: '',
} }
: undefined, : undefined,
// For app router we prefer to bundle ESM, // For app router we prefer to bundle ESM,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment