Commit 1f135625 authored by Will Binns-Smith's avatar Will Binns-Smith

Add server-side HMR infrastructure

Add server HMR functions to enable hot module replacement for server-side
code in Turbopack:

Rust (crates/next-api/src/project.rs):
- server_hmr_content: Get server module content by identifier
- server_hmr_version: Get server module version
- server_hmr_version_state: Get version state for server HMR session
- server_hmr_update: Compute server HMR update delta
- server_hmr_identifiers: List all server HMR identifiers

NAPI bindings (crates/napi/src/next_api/project.rs):
- project_server_hmr_events: Subscribe to server HMR events
- project_server_hmr_identifiers_subscribe: Subscribe to server identifiers

TypeScript (packages/next/src/build/swc):
- serverHmrEvents(): Subscribe to server HMR events
- serverHmrIdentifiersSubscribe(): Subscribe to server identifiers

Debug test code added to hot-reloader-turbopack.ts to verify the
subscriptions work correctly during development.
parent 34057d35
......@@ -1232,6 +1232,35 @@ fn project_client_hmr_update_operation(
project.client_hmr_update(identifier, *state)
}
#[turbo_tasks::function(operation)]
fn project_server_hmr_update_operation(
project: ResolvedVc<Project>,
identifier: RcStr,
state: ResolvedVc<VersionState>,
) -> Vc<Update> {
project.server_hmr_update(identifier, *state)
}
#[turbo_tasks::function(operation)]
async fn server_hmr_update_with_issues_operation(
project: ResolvedVc<Project>,
identifier: RcStr,
state: ResolvedVc<VersionState>,
) -> Result<Vc<HmrUpdateWithIssues>> {
let update_op = project_server_hmr_update_operation(project, identifier, state);
let update = update_op.read_strongly_consistent().await?;
let issues = get_issues(update_op, NEXT_ISSUE_FILTER).await?;
let diagnostics = get_diagnostics(update_op).await?;
let effects = Arc::new(get_effects(update_op).await?);
Ok(HmrUpdateWithIssues {
update,
issues,
diagnostics,
effects,
}
.cell())
}
#[tracing::instrument(level = "info", name = "get HMR events", skip(project, func))]
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_client_hmr_events(
......@@ -1317,6 +1346,91 @@ pub fn project_client_hmr_events(
)
}
#[tracing::instrument(level = "info", name = "get server HMR events", skip(project, func))]
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_server_hmr_events(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
identifier: RcStr,
func: JsFunction,
) -> napi::Result<External<RootTask>> {
let container = project.container;
let session = TransientInstance::new(());
subscribe(
project.turbopack_ctx.clone(),
func,
{
let outer_identifier = identifier.clone();
let session = session.clone();
move || {
let identifier: RcStr = outer_identifier.clone();
let session = session.clone();
async move {
let project = container.project().to_resolved().await?;
let state = project
.server_hmr_version_state(identifier.clone(), session)
.to_resolved()
.await?;
let update_op =
server_hmr_update_with_issues_operation(project, identifier.clone(), state);
let update = update_op.read_strongly_consistent().await?;
let HmrUpdateWithIssues {
update,
issues,
diagnostics,
effects,
} = &*update;
effects.apply().await?;
match &**update {
Update::Missing | Update::None => {}
Update::Total(TotalUpdate { to }) => {
state.set(to.clone()).await?;
}
Update::Partial(PartialUpdate { to, .. }) => {
state.set(to.clone()).await?;
}
}
Ok((Some(update.clone()), issues.clone(), diagnostics.clone()))
}
}
},
move |ctx| {
let (update, issues, diags) = ctx.value;
let napi_issues = issues
.iter()
.map(|issue| NapiIssue::from(&**issue))
.collect();
let update_issues = issues
.iter()
.map(|issue| Issue::from(&**issue))
.collect::<Vec<_>>();
let identifier = ResourceIdentifier {
path: identifier.clone(),
headers: None,
};
let update = match update.as_deref() {
None | Some(Update::Missing) | Some(Update::Total(_)) => {
ClientUpdateInstruction::restart(&identifier, &update_issues)
}
Some(Update::Partial(update)) => ClientUpdateInstruction::partial(
&identifier,
&update.instruction,
&update_issues,
),
Some(Update::None) => ClientUpdateInstruction::issues(&identifier, &update_issues),
};
Ok(vec![TurbopackResult {
result: ctx.env.to_js_value(&update)?,
issues: napi_issues,
diagnostics: diags.iter().map(|d| NapiDiagnostic::from(d)).collect(),
}])
},
)
}
#[napi(object)]
struct HmrIdentifiers {
pub identifiers: Vec<RcStr>,
......@@ -1355,6 +1469,31 @@ fn project_container_client_hmr_identifiers_operation(
container.client_hmr_identifiers()
}
#[turbo_tasks::function(operation)]
fn project_server_hmr_identifiers_operation(
container: ResolvedVc<ProjectContainer>,
) -> Vc<Vec<RcStr>> {
container.project().server_hmr_identifiers()
}
#[turbo_tasks::function(operation)]
async fn get_server_hmr_identifiers_with_issues_operation(
container: ResolvedVc<ProjectContainer>,
) -> Result<Vc<HmrIdentifiersWithIssues>> {
let hmr_identifiers_op = project_server_hmr_identifiers_operation(container);
let hmr_identifiers = hmr_identifiers_op.read_strongly_consistent().await?;
let issues = get_issues(hmr_identifiers_op, NEXT_ISSUE_FILTER).await?;
let diagnostics = get_diagnostics(hmr_identifiers_op).await?;
let effects = Arc::new(get_effects(hmr_identifiers_op).await?);
Ok(HmrIdentifiersWithIssues {
identifiers: hmr_identifiers,
issues,
diagnostics,
effects,
}
.cell())
}
#[tracing::instrument(level = "info", name = "get HMR identifiers", skip_all)]
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_client_hmr_identifiers_subscribe(
......@@ -1400,6 +1539,51 @@ pub fn project_client_hmr_identifiers_subscribe(
)
}
#[tracing::instrument(level = "info", name = "get server HMR identifiers", skip_all)]
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_server_hmr_identifiers_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
func: JsFunction,
) -> napi::Result<External<RootTask>> {
let container = project.container;
subscribe(
project.turbopack_ctx.clone(),
func,
move || async move {
let hmr_identifiers_with_issues_op =
get_server_hmr_identifiers_with_issues_operation(container);
let HmrIdentifiersWithIssues {
identifiers,
issues,
diagnostics,
effects,
} = &*hmr_identifiers_with_issues_op
.read_strongly_consistent()
.await?;
effects.apply().await?;
Ok((identifiers.clone(), issues.clone(), diagnostics.clone()))
},
move |ctx| {
let (identifiers, issues, diagnostics) = ctx.value;
Ok(vec![TurbopackResult {
result: HmrIdentifiers {
identifiers: ReadRef::into_owned(identifiers),
},
issues: issues
.iter()
.map(|issue| NapiIssue::from(&**issue))
.collect(),
diagnostics: diagnostics
.iter()
.map(|d| NapiDiagnostic::from(d))
.collect(),
}])
},
)
}
pub enum UpdateMessage {
Start,
End(UpdateInfo),
......
......@@ -2109,6 +2109,93 @@ impl Project {
}
}
/// Get server module content by identifier (path relative to node_root).
/// This is the server-side equivalent of `hmr_content`.
#[turbo_tasks::function]
async fn server_hmr_content(
self: Vc<Self>,
identifier: RcStr,
) -> Result<Vc<OptionVersionedContent>> {
if let Some(map) = self.await?.versioned_content_map {
// Key difference from hmr_content: use node_root instead of client_relative_path
let content = map.get(self.node_root().await?.join(&identifier)?);
Ok(content)
} else {
bail!("must be in dev mode to hmr")
}
}
/// Get server module version.
/// This is the server-side equivalent of `hmr_version`.
#[turbo_tasks::function]
async fn server_hmr_version(self: Vc<Self>, identifier: RcStr) -> Result<Vc<Box<dyn Version>>> {
let content = self.server_hmr_content(identifier).await?;
if let Some(content) = &*content {
Ok(content.version())
} else {
Ok(Vc::upcast(NotFoundVersion::new()))
}
}
/// Get the version state for a server HMR session. Initialized with the first seen
/// version in that session.
/// This is the server-side equivalent of `hmr_version_state`.
#[turbo_tasks::function]
pub async fn server_hmr_version_state(
self: Vc<Self>,
identifier: RcStr,
session: TransientInstance<()>,
) -> Result<Vc<VersionState>> {
let version = self.server_hmr_version(identifier);
// The session argument is important to avoid caching this function between
// sessions.
let _ = session;
// INVALIDATION: This is intentionally untracked to avoid invalidating this
// function completely. We want to initialize the VersionState with the
// first seen version of the session.
let state = VersionState::new(
version
.into_trait_ref()
.strongly_consistent()
.untracked()
.await?,
)
.await?;
Ok(state)
}
/// Emits opaque HMR events whenever a change is detected in server-side modules
/// internally known as `identifier`.
/// This is the server-side equivalent of `hmr_update`.
#[turbo_tasks::function]
pub async fn server_hmr_update(
self: Vc<Self>,
identifier: RcStr,
from: Vc<VersionState>,
) -> Result<Vc<Update>> {
let from = from.get();
let content = self.server_hmr_content(identifier).await?;
if let Some(content) = *content {
Ok(content.update(from))
} else {
Ok(Update::Missing.cell())
}
}
/// Gets a list of all server HMR identifiers that can be subscribed to.
/// These are paths relative to node_root (e.g., "server/app/page.js").
/// This is the server-side equivalent of `hmr_identifiers`.
#[turbo_tasks::function]
pub async fn server_hmr_identifiers(self: Vc<Self>) -> Result<Vc<Vec<RcStr>>> {
if let Some(map) = self.await?.versioned_content_map {
Ok(map.keys_in_path(self.node_root().owned().await?))
} else {
bail!("must be in dev mode to hmr")
}
}
/// Completion when server side changes are detected in output assets
/// referenced from the roots
#[turbo_tasks::function]
......
......@@ -316,6 +316,11 @@ export declare function projectClientHmrEvents(
identifier: RcStr,
func: (...args: any[]) => any
): { __napiType: 'RootTask' }
export declare function projectServerHmrEvents(
project: { __napiType: 'Project' },
identifier: RcStr,
func: (...args: any[]) => any
): { __napiType: 'RootTask' }
export interface HmrIdentifiers {
identifiers: Array<RcStr>
}
......@@ -323,6 +328,10 @@ export declare function projectClientHmrIdentifiersSubscribe(
project: { __napiType: 'Project' },
func: (...args: any[]) => any
): { __napiType: 'RootTask' }
export declare function projectServerHmrIdentifiersSubscribe(
project: { __napiType: 'Project' },
func: (...args: any[]) => any
): { __napiType: 'RootTask' }
export interface NapiUpdateMessage {
updateType: string
value?: NapiUpdateInfo
......
......@@ -756,6 +756,27 @@ function bindingToApi(
)
}
serverHmrEvents(identifier: string) {
return subscribe<TurbopackResult<Update>>(true, async (callback) =>
binding.projectServerHmrEvents(
this._nativeProject,
identifier,
callback
)
)
}
serverHmrIdentifiersSubscribe() {
return subscribe<TurbopackResult<HmrIdentifiers>>(
false,
async (callback) =>
binding.projectServerHmrIdentifiersSubscribe(
this._nativeProject,
callback
)
)
}
traceSource(
stackFrame: TurbopackStackFrame,
currentDirectoryFileUrl: string
......
......@@ -251,6 +251,14 @@ export interface Project {
TurbopackResult<HmrIdentifiers>
>
serverHmrEvents(
identifier: string
): AsyncIterableIterator<TurbopackResult<Update>>
serverHmrIdentifiersSubscribe(): AsyncIterableIterator<
TurbopackResult<HmrIdentifiers>
>
getSourceForAsset(filePath: string): Promise<string | null>
getSourceMap(filePath: string): Promise<string | null>
......
......@@ -1464,6 +1464,49 @@ export async function createHotReloaderTurbopack(
process.exit(1)
})
// DEBUG: Test server HMR identifiers subscription
const serverHmrSubscriptions = new Map<
string,
AsyncIterableIterator<unknown>
>()
;(async () => {
const serverHmrIdentifiers = project.serverHmrIdentifiersSubscribe()
// Skip initial
await serverHmrIdentifiers.next()
for await (const data of serverHmrIdentifiers) {
console.log('[Server HMR] Identifiers updated:', data.identifiers.length)
// For testing, subscribe to page.js files
for (const identifier of data.identifiers) {
if (
identifier.includes('page.js') &&
!serverHmrSubscriptions.has(identifier)
) {
console.log('[Server HMR] Subscribing to:', identifier)
const subscription = project.serverHmrEvents(identifier)
serverHmrSubscriptions.set(identifier, subscription)
// Start listening for changes in background
;(async () => {
// Skip initial
await subscription.next()
for await (const update of subscription) {
console.log('[Server HMR] Update for', identifier, ':', update)
}
})().catch((err) => {
console.error(
'[Server HMR] Event subscription error for',
identifier,
':',
err
)
})
}
}
}
})().catch((err) => {
console.error('[Server HMR] Identifiers subscription error:', err)
})
// Write empty manifests
await currentEntriesHandling
await manifestLoader.writeManifests({
......
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