-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
Copy pathproject.rs
1359 lines (1225 loc) · 46.1 KB
/
project.rs
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::{io::Write, path::PathBuf, sync::Arc, thread, time::Duration};
use anyhow::{anyhow, bail, Context, Result};
use napi::{
bindgen_prelude::{within_runtime_if_available, External},
threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode},
JsFunction, Status,
};
use next_api::{
entrypoints::Entrypoints,
operation::{
EntrypointsOperation, InstrumentationOperation, MiddlewareOperation, RouteOperation,
},
project::{
DefineEnv, DraftModeOptions, PartialProjectOptions, Project, ProjectContainer,
ProjectOptions, WatchOptions,
},
route::Endpoint,
};
use next_core::tracing_presets::{
TRACING_NEXT_OVERVIEW_TARGETS, TRACING_NEXT_TARGETS, TRACING_NEXT_TURBOPACK_TARGETS,
TRACING_NEXT_TURBO_TASKS_TARGETS,
};
use once_cell::sync::Lazy;
use rand::Rng;
use tokio::{io::AsyncWriteExt, time::Instant};
use tracing::Instrument;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Registry};
use turbo_rcstr::RcStr;
use turbo_tasks::{
get_effects, Completion, Effects, OperationVc, ReadRef, ResolvedVc, TransientInstance,
UpdateInfo, Vc,
};
use turbo_tasks_fs::{
get_relative_path_to, util::uri_from_file, DiskFileSystem, FileContent, FileSystem,
FileSystemPath,
};
use turbopack_core::{
diagnostics::PlainDiagnostic,
error::PrettyPrintError,
issue::PlainIssue,
source_map::{OptionSourceMap, OptionStringifiedSourceMap, SourceMap, Token},
version::{PartialUpdate, TotalUpdate, Update, VersionState},
PROJECT_FILESYSTEM_NAME, SOURCE_URL_PROTOCOL,
};
use turbopack_ecmascript_hmr_protocol::{ClientUpdateInstruction, ResourceIdentifier};
use turbopack_trace_utils::{
exit::{ExitHandler, ExitReceiver},
filter_layer::FilterLayer,
raw_trace::RawTraceLayer,
trace_writer::TraceWriter,
};
use url::Url;
use super::{
endpoint::ExternalEndpoint,
utils::{
create_turbo_tasks, get_diagnostics, get_issues, subscribe, NapiDiagnostic, NapiIssue,
NextTurboTasks, RootTask, TurbopackResult, VcArc,
},
};
use crate::{register, util::DhatProfilerGuard};
/// Used by [`benchmark_file_io`]. This is a noisy benchmark, so set the
/// threshold high.
const SLOW_FILESYSTEM_THRESHOLD: Duration = Duration::from_millis(100);
static SOURCE_MAP_PREFIX: Lazy<String> = Lazy::new(|| format!("{}///", SOURCE_URL_PROTOCOL));
static SOURCE_MAP_PREFIX_PROJECT: Lazy<String> =
Lazy::new(|| format!("{}///[{}]/", SOURCE_URL_PROTOCOL, PROJECT_FILESYSTEM_NAME));
#[napi(object)]
#[derive(Clone, Debug)]
pub struct NapiEnvVar {
pub name: String,
pub value: String,
}
#[napi(object)]
pub struct NapiDraftModeOptions {
pub preview_mode_id: String,
pub preview_mode_encryption_key: String,
pub preview_mode_signing_key: String,
}
impl From<NapiDraftModeOptions> for DraftModeOptions {
fn from(val: NapiDraftModeOptions) -> Self {
DraftModeOptions {
preview_mode_id: val.preview_mode_id.into(),
preview_mode_encryption_key: val.preview_mode_encryption_key.into(),
preview_mode_signing_key: val.preview_mode_signing_key.into(),
}
}
}
#[napi(object)]
pub struct NapiWatchOptions {
/// Whether to watch the filesystem for file changes.
pub enable: bool,
/// Enable polling at a certain interval if the native file watching doesn't work (e.g.
/// docker).
pub poll_interval_ms: Option<f64>,
}
#[napi(object)]
pub struct NapiProjectOptions {
/// A root path from which all files must be nested under. Trying to access
/// a file outside this root will fail. Think of this as a chroot.
pub root_path: String,
/// A path inside the root_path which contains the app/pages directories.
pub project_path: String,
/// next.config's distDir. Project initialization occurs eariler than
/// deserializing next.config, so passing it as separate option.
pub dist_dir: String,
/// Filesystem watcher options.
pub watch: NapiWatchOptions,
/// The contents of next.config.js, serialized to JSON.
pub next_config: String,
/// The contents of ts/config read by load-jsconfig, serialized to JSON.
pub js_config: String,
/// A map of environment variables to use when compiling code.
pub env: Vec<NapiEnvVar>,
/// A map of environment variables which should get injected at compile
/// time.
pub define_env: NapiDefineEnv,
/// The mode in which Next.js is running.
pub dev: bool,
/// The server actions encryption key.
pub encryption_key: String,
/// The build id.
pub build_id: String,
/// Options for draft mode.
pub preview_props: NapiDraftModeOptions,
/// The browserslist query to use for targeting browsers.
pub browserslist_query: String,
/// When the code is minified, this opts out of the default mangling of
/// local names for variables, functions etc., which can be useful for
/// debugging/profiling purposes.
pub no_mangling: bool,
}
/// [NapiProjectOptions] with all fields optional.
#[napi(object)]
pub struct NapiPartialProjectOptions {
/// A root path from which all files must be nested under. Trying to access
/// a file outside this root will fail. Think of this as a chroot.
pub root_path: Option<String>,
/// A path inside the root_path which contains the app/pages directories.
pub project_path: Option<String>,
/// next.config's distDir. Project initialization occurs eariler than
/// deserializing next.config, so passing it as separate option.
pub dist_dir: Option<Option<String>>,
/// Filesystem watcher options.
pub watch: Option<NapiWatchOptions>,
/// The contents of next.config.js, serialized to JSON.
pub next_config: Option<String>,
/// The contents of ts/config read by load-jsconfig, serialized to JSON.
pub js_config: Option<String>,
/// A map of environment variables to use when compiling code.
pub env: Option<Vec<NapiEnvVar>>,
/// A map of environment variables which should get injected at compile
/// time.
pub define_env: Option<NapiDefineEnv>,
/// The mode in which Next.js is running.
pub dev: Option<bool>,
/// The server actions encryption key.
pub encryption_key: Option<String>,
/// The build id.
pub build_id: Option<String>,
/// Options for draft mode.
pub preview_props: Option<NapiDraftModeOptions>,
/// The browserslist query to use for targeting browsers.
pub browserslist_query: Option<String>,
/// When the code is minified, this opts out of the default mangling of
/// local names for variables, functions etc., which can be useful for
/// debugging/profiling purposes.
pub no_mangling: Option<bool>,
}
#[napi(object)]
#[derive(Clone, Debug)]
pub struct NapiDefineEnv {
pub client: Vec<NapiEnvVar>,
pub edge: Vec<NapiEnvVar>,
pub nodejs: Vec<NapiEnvVar>,
}
#[napi(object)]
pub struct NapiTurboEngineOptions {
/// Use the new backend with persistent caching enabled.
pub persistent_caching: Option<bool>,
/// An upper bound of memory that turbopack will attempt to stay under.
pub memory_limit: Option<f64>,
/// Track dependencies between tasks. If false, any change during build will error.
pub dependency_tracking: Option<bool>,
}
impl From<NapiWatchOptions> for WatchOptions {
fn from(val: NapiWatchOptions) -> Self {
WatchOptions {
enable: val.enable,
poll_interval: val
.poll_interval_ms
.filter(|interval| !interval.is_nan() && interval.is_finite() && *interval > 0.0)
.map(|interval| Duration::from_secs_f64(interval / 1000.0)),
}
}
}
impl From<NapiProjectOptions> for ProjectOptions {
fn from(val: NapiProjectOptions) -> Self {
ProjectOptions {
root_path: val.root_path.into(),
project_path: val.project_path.into(),
watch: val.watch.into(),
next_config: val.next_config.into(),
js_config: val.js_config.into(),
env: val
.env
.into_iter()
.map(|var| (var.name.into(), var.value.into()))
.collect(),
define_env: val.define_env.into(),
dev: val.dev,
encryption_key: val.encryption_key.into(),
build_id: val.build_id.into(),
preview_props: val.preview_props.into(),
browserslist_query: val.browserslist_query.into(),
no_mangling: val.no_mangling,
}
}
}
impl From<NapiPartialProjectOptions> for PartialProjectOptions {
fn from(val: NapiPartialProjectOptions) -> Self {
PartialProjectOptions {
root_path: val.root_path.map(From::from),
project_path: val.project_path.map(From::from),
watch: val.watch.map(From::from),
next_config: val.next_config.map(From::from),
js_config: val.js_config.map(From::from),
env: val.env.map(|env| {
env.into_iter()
.map(|var| (var.name.into(), var.value.into()))
.collect()
}),
define_env: val.define_env.map(|env| env.into()),
dev: val.dev,
encryption_key: val.encryption_key.map(From::from),
build_id: val.build_id.map(From::from),
preview_props: val.preview_props.map(|props| props.into()),
}
}
}
impl From<NapiDefineEnv> for DefineEnv {
fn from(val: NapiDefineEnv) -> Self {
DefineEnv {
client: val
.client
.into_iter()
.map(|var| (var.name.into(), var.value.into()))
.collect(),
edge: val
.edge
.into_iter()
.map(|var| (var.name.into(), var.value.into()))
.collect(),
nodejs: val
.nodejs
.into_iter()
.map(|var| (var.name.into(), var.value.into()))
.collect(),
}
}
}
pub struct ProjectInstance {
turbo_tasks: NextTurboTasks,
container: Vc<ProjectContainer>,
exit_receiver: tokio::sync::Mutex<Option<ExitReceiver>>,
}
#[napi(ts_return_type = "Promise<{ __napiType: \"Project\" }>")]
pub async fn project_new(
options: NapiProjectOptions,
turbo_engine_options: NapiTurboEngineOptions,
) -> napi::Result<External<ProjectInstance>> {
register();
let (exit, exit_receiver) = ExitHandler::new_receiver();
if let Some(dhat_profiler) = DhatProfilerGuard::try_init() {
exit.on_exit(async move {
tokio::task::spawn_blocking(move || drop(dhat_profiler))
.await
.unwrap()
});
}
let mut trace = std::env::var("NEXT_TURBOPACK_TRACING")
.ok()
.filter(|v| !v.is_empty());
if cfg!(feature = "tokio-console") && trace.is_none() {
// ensure `trace` is set to *something* so that the `tokio-console` feature works, otherwise
// you just get empty output from `tokio-console`, which can be confusing.
trace = Some("overview".to_owned());
}
if let Some(mut trace) = trace {
// Trace presets
match trace.as_str() {
"overview" | "1" => {
trace = TRACING_NEXT_OVERVIEW_TARGETS.join(",");
}
"next" => {
trace = TRACING_NEXT_TARGETS.join(",");
}
"turbopack" => {
trace = TRACING_NEXT_TURBOPACK_TARGETS.join(",");
}
"turbo-tasks" => {
trace = TRACING_NEXT_TURBO_TASKS_TARGETS.join(",");
}
_ => {}
}
let subscriber = Registry::default();
if cfg!(feature = "tokio-console") {
trace = format!("{trace},tokio=trace,runtime=trace");
}
#[cfg(feature = "tokio-console")]
let subscriber = subscriber.with(console_subscriber::spawn());
let subscriber = subscriber.with(FilterLayer::try_new(&trace).unwrap());
let dist_dir = options.dist_dir.clone();
let internal_dir = PathBuf::from(&options.project_path).join(dist_dir);
std::fs::create_dir_all(&internal_dir)
.context("Unable to create .next directory")
.unwrap();
let trace_file = internal_dir.join("trace-turbopack");
let trace_writer = std::fs::File::create(trace_file.clone()).unwrap();
let (trace_writer, trace_writer_guard) = TraceWriter::new(trace_writer);
let subscriber = subscriber.with(RawTraceLayer::new(trace_writer));
exit.on_exit(async move {
tokio::task::spawn_blocking(move || drop(trace_writer_guard));
});
let trace_server = std::env::var("NEXT_TURBOPACK_TRACE_SERVER").ok();
if trace_server.is_some() {
thread::spawn(move || {
turbopack_trace_server::start_turbopack_trace_server(trace_file);
});
println!("Turbopack trace server started. View trace at https://turbo-trace-viewer.vercel.app/");
}
subscriber.init();
}
let memory_limit = turbo_engine_options
.memory_limit
.map(|m| m as usize)
.unwrap_or(usize::MAX);
let persistent_caching = turbo_engine_options.persistent_caching.unwrap_or_default();
let dependency_tracking = turbo_engine_options.dependency_tracking.unwrap_or(true);
let turbo_tasks = create_turbo_tasks(
PathBuf::from(&options.dist_dir),
persistent_caching,
memory_limit,
dependency_tracking,
)?;
let stats_path = std::env::var_os("NEXT_TURBOPACK_TASK_STATISTICS");
if let Some(stats_path) = stats_path {
let task_stats = turbo_tasks.task_statistics().enable().clone();
exit.on_exit(async move {
tokio::task::spawn_blocking(move || {
let mut file = std::fs::File::create(&stats_path)
.with_context(|| format!("failed to create or open {stats_path:?}"))?;
serde_json::to_writer(&file, &task_stats)
.context("failed to serialize or write task statistics")?;
file.flush().context("failed to flush file")
})
.await
.unwrap()
.unwrap();
});
}
let options: ProjectOptions = options.into();
let container = turbo_tasks
.run_once(async move {
let project = ProjectContainer::new("next.js".into(), options.dev);
let project = project.to_resolved().await?;
project.initialize(options).await?;
Ok(project)
})
.await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;
turbo_tasks.spawn_once_task(async move {
benchmark_file_io(container.project().node_root())
.await
.inspect_err(|err| tracing::warn!(%err, "failed to benchmark file IO"))
});
Ok(External::new_with_size_hint(
ProjectInstance {
turbo_tasks,
container: *container,
exit_receiver: tokio::sync::Mutex::new(Some(exit_receiver)),
},
100,
))
}
/// A very simple and low-overhead, but potentially noisy benchmark to detect
/// very slow disk IO. Warns the user (via `println!`) if the benchmark takes
/// more than `SLOW_FILESYSTEM_THRESHOLD`.
///
/// This idea is copied from Bun:
/// - https://x.com/jarredsumner/status/1637549427677364224
/// - https://github.com/oven-sh/bun/blob/06a9aa80c38b08b3148bfeabe560/src/install/install.zig#L3038
#[tracing::instrument]
async fn benchmark_file_io(directory: Vc<FileSystemPath>) -> Result<Vc<Completion>> {
// try to get the real file path on disk so that we can use it with tokio
let fs = Vc::try_resolve_downcast_type::<DiskFileSystem>(directory.fs())
.await?
.context(anyhow!(
"expected node_root to be a DiskFileSystem, cannot benchmark"
))?
.await?;
let directory = fs.to_sys_path(directory).await?;
let temp_path = directory.join(format!(
"tmp_file_io_benchmark_{:x}",
rand::random::<u128>()
));
let mut random_buffer = [0u8; 512];
rand::thread_rng().fill(&mut random_buffer[..]);
// perform IO directly with tokio (skipping `tokio_tasks_fs`) to avoid the
// additional noise/overhead of tasks caching, invalidation, file locks,
// etc.
let start = Instant::now();
async move {
for _ in 0..3 {
// create a new empty file
let mut file = tokio::fs::File::create(&temp_path).await?;
file.write_all(&random_buffer).await?;
file.sync_all().await?;
drop(file);
// remove the file
tokio::fs::remove_file(&temp_path).await?;
}
anyhow::Ok(())
}
.instrument(tracing::info_span!("benchmark file IO (measurement)"))
.await?;
if Instant::now().duration_since(start) > SLOW_FILESYSTEM_THRESHOLD {
println!(
"Slow filesystem detected. If {} is a network drive, consider moving it to a local \
folder. If you have an antivirus enabled, consider excluding your project directory.",
directory.to_string_lossy(),
);
}
Ok(Completion::new())
}
#[napi]
pub async fn project_update(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
options: NapiPartialProjectOptions,
) -> napi::Result<()> {
let turbo_tasks = project.turbo_tasks.clone();
let options = options.into();
let container = project.container;
turbo_tasks
.run_once(async move {
container.update(options).await?;
Ok(())
})
.await
.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string()))?;
Ok(())
}
/// Runs exit handlers for the project registered using the [`ExitHandler`] API.
///
/// This is called by `project_shutdown`, so if you're calling that API, you shouldn't call this
/// one.
#[napi]
pub async fn project_on_exit(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
) {
project_on_exit_internal(&project).await
}
async fn project_on_exit_internal(project: &ProjectInstance) {
let exit_receiver = project.exit_receiver.lock().await.take();
exit_receiver
.expect("`project.onExitSync` must only be called once")
.run_exit_handler()
.await;
}
/// Runs `project_on_exit`, and then waits for turbo_tasks to gracefully shut down.
///
/// This is used in builds where it's important that we completely persist turbo-tasks to disk, but
/// it's skipped in the development server (`project_on_exit` is used instead with a short timeout),
/// where we prioritize fast exit and user responsiveness over all else.
#[napi]
pub async fn project_shutdown(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
) {
project_on_exit_internal(&project).await;
project.turbo_tasks.stop_and_wait().await;
}
#[napi(object)]
#[derive(Default)]
struct AppPageNapiRoute {
/// The relative path from project_path to the route file
pub original_name: Option<String>,
pub html_endpoint: Option<External<ExternalEndpoint>>,
pub rsc_endpoint: Option<External<ExternalEndpoint>>,
}
#[napi(object)]
#[derive(Default)]
struct NapiRoute {
/// The router path
pub pathname: String,
/// The relative path from project_path to the route file
pub original_name: Option<String>,
/// The type of route, eg a Page or App
pub r#type: &'static str,
pub pages: Option<Vec<AppPageNapiRoute>>,
// Different representations of the endpoint
pub endpoint: Option<External<ExternalEndpoint>>,
pub html_endpoint: Option<External<ExternalEndpoint>>,
pub rsc_endpoint: Option<External<ExternalEndpoint>>,
pub data_endpoint: Option<External<ExternalEndpoint>>,
}
impl NapiRoute {
fn from_route(pathname: String, value: RouteOperation, turbo_tasks: &NextTurboTasks) -> Self {
let convert_endpoint = |endpoint: OperationVc<Box<dyn Endpoint>>| {
Some(External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(),
endpoint,
))))
};
match value {
RouteOperation::Page {
html_endpoint,
data_endpoint,
} => NapiRoute {
pathname,
r#type: "page",
html_endpoint: convert_endpoint(html_endpoint),
data_endpoint: convert_endpoint(data_endpoint),
..Default::default()
},
RouteOperation::PageApi { endpoint } => NapiRoute {
pathname,
r#type: "page-api",
endpoint: convert_endpoint(endpoint),
..Default::default()
},
RouteOperation::AppPage(pages) => NapiRoute {
pathname,
r#type: "app-page",
pages: Some(
pages
.into_iter()
.map(|page_route| AppPageNapiRoute {
original_name: Some(page_route.original_name.into_owned()),
html_endpoint: convert_endpoint(page_route.html_endpoint),
rsc_endpoint: convert_endpoint(page_route.rsc_endpoint),
})
.collect(),
),
..Default::default()
},
RouteOperation::AppRoute {
original_name,
endpoint,
} => NapiRoute {
pathname,
original_name: Some(original_name.into_owned()),
r#type: "app-route",
endpoint: convert_endpoint(endpoint),
..Default::default()
},
RouteOperation::Conflict => NapiRoute {
pathname,
r#type: "conflict",
..Default::default()
},
}
}
}
#[napi(object)]
struct NapiMiddleware {
pub endpoint: External<ExternalEndpoint>,
}
impl NapiMiddleware {
fn from_middleware(value: &MiddlewareOperation, turbo_tasks: &NextTurboTasks) -> Result<Self> {
Ok(NapiMiddleware {
endpoint: External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(),
value.endpoint,
))),
})
}
}
#[napi(object)]
struct NapiInstrumentation {
pub node_js: External<ExternalEndpoint>,
pub edge: External<ExternalEndpoint>,
}
impl NapiInstrumentation {
fn from_instrumentation(
value: &InstrumentationOperation,
turbo_tasks: &NextTurboTasks,
) -> Result<Self> {
Ok(NapiInstrumentation {
node_js: External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(),
value.node_js,
))),
edge: External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(),
value.edge,
))),
})
}
}
#[napi(object)]
struct NapiEntrypoints {
pub routes: Vec<NapiRoute>,
pub middleware: Option<NapiMiddleware>,
pub instrumentation: Option<NapiInstrumentation>,
pub pages_document_endpoint: External<ExternalEndpoint>,
pub pages_app_endpoint: External<ExternalEndpoint>,
pub pages_error_endpoint: External<ExternalEndpoint>,
}
#[turbo_tasks::value(serialization = "none")]
struct EntrypointsWithIssues {
entrypoints: ReadRef<EntrypointsOperation>,
issues: Arc<Vec<ReadRef<PlainIssue>>>,
diagnostics: Arc<Vec<ReadRef<PlainDiagnostic>>>,
effects: Arc<Effects>,
}
#[turbo_tasks::function(operation)]
async fn get_entrypoints_with_issues_operation(
container: ResolvedVc<ProjectContainer>,
) -> Result<Vc<EntrypointsWithIssues>> {
let entrypoints_operation =
EntrypointsOperation::new(project_container_entrypoints_operation(container));
let entrypoints = entrypoints_operation.read_strongly_consistent().await?;
let issues = get_issues(entrypoints_operation).await?;
let diagnostics = get_diagnostics(entrypoints_operation).await?;
let effects = Arc::new(get_effects(entrypoints_operation).await?);
Ok(EntrypointsWithIssues {
entrypoints,
issues,
diagnostics,
effects,
}
.cell())
}
#[turbo_tasks::function(operation)]
fn project_container_entrypoints_operation(
// the container is a long-lived object with internally mutable state, there's no risk of it
// becoming stale
container: ResolvedVc<ProjectContainer>,
) -> Vc<Entrypoints> {
container.entrypoints()
}
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_entrypoints_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
func: JsFunction,
) -> napi::Result<External<RootTask>> {
let turbo_tasks = project.turbo_tasks.clone();
let container = project.container;
subscribe(
turbo_tasks.clone(),
func,
move || {
async move {
let entrypoints_with_issues_op =
get_entrypoints_with_issues_operation(container.to_resolved().await?);
let EntrypointsWithIssues {
entrypoints,
issues,
diagnostics,
effects,
} = &*entrypoints_with_issues_op
.read_strongly_consistent()
.await?;
effects.apply().await?;
Ok((entrypoints.clone(), issues.clone(), diagnostics.clone()))
}
.instrument(tracing::info_span!("entrypoints subscription"))
},
move |ctx| {
let (entrypoints, issues, diags) = ctx.value;
Ok(vec![TurbopackResult {
result: NapiEntrypoints {
routes: entrypoints
.routes
.iter()
.map(|(pathname, route)| {
NapiRoute::from_route(
pathname.clone().into(),
route.clone(),
&turbo_tasks,
)
})
.collect::<Vec<_>>(),
middleware: entrypoints
.middleware
.as_ref()
.map(|m| NapiMiddleware::from_middleware(m, &turbo_tasks))
.transpose()?,
instrumentation: entrypoints
.instrumentation
.as_ref()
.map(|m| NapiInstrumentation::from_instrumentation(m, &turbo_tasks))
.transpose()?,
pages_document_endpoint: External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(),
entrypoints.pages_document_endpoint,
))),
pages_app_endpoint: External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(),
entrypoints.pages_app_endpoint,
))),
pages_error_endpoint: External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(),
entrypoints.pages_error_endpoint,
))),
},
issues: issues
.iter()
.map(|issue| NapiIssue::from(&**issue))
.collect(),
diagnostics: diags.iter().map(|d| NapiDiagnostic::from(d)).collect(),
}])
},
)
}
#[turbo_tasks::value(serialization = "none")]
struct HmrUpdateWithIssues {
update: ReadRef<Update>,
issues: Arc<Vec<ReadRef<PlainIssue>>>,
diagnostics: Arc<Vec<ReadRef<PlainDiagnostic>>>,
effects: Arc<Effects>,
}
#[turbo_tasks::function(operation)]
async fn hmr_update_with_issues_operation(
project: ResolvedVc<Project>,
identifier: RcStr,
state: ResolvedVc<VersionState>,
) -> Result<Vc<HmrUpdateWithIssues>> {
let update_op = project_hmr_update_operation(project, identifier, state);
let update = update_op.read_strongly_consistent().await?;
let issues = get_issues(update_op).await?;
let diagnostics = get_diagnostics(update_op).await?;
let effects = Arc::new(get_effects(update_op).await?);
Ok(HmrUpdateWithIssues {
update,
issues,
diagnostics,
effects,
}
.cell())
}
#[turbo_tasks::function(operation)]
fn project_hmr_update_operation(
project: ResolvedVc<Project>,
identifier: RcStr,
state: ResolvedVc<VersionState>,
) -> Vc<Update> {
project.hmr_update(identifier, *state)
}
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_hmr_events(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
identifier: String,
func: JsFunction,
) -> napi::Result<External<RootTask>> {
let turbo_tasks = project.turbo_tasks.clone();
let project = project.container;
let session = TransientInstance::new(());
subscribe(
turbo_tasks.clone(),
func,
{
let outer_identifier = identifier.clone();
let session = session.clone();
move || {
let identifier: RcStr = outer_identifier.clone().into();
let session = session.clone();
async move {
let project = project.project().to_resolved().await?;
let state = project
.hmr_version_state(identifier.clone(), session)
.to_resolved()
.await?;
let update_op =
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()))
}
.instrument(tracing::info_span!(
"HMR subscription",
identifier = outer_identifier
))
}
},
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).into())
.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<String>,
}
#[turbo_tasks::value(serialization = "none")]
struct HmrIdentifiersWithIssues {
identifiers: ReadRef<Vec<RcStr>>,
issues: Arc<Vec<ReadRef<PlainIssue>>>,
diagnostics: Arc<Vec<ReadRef<PlainDiagnostic>>>,
effects: Arc<Effects>,
}
#[turbo_tasks::function(operation)]
async fn get_hmr_identifiers_with_issues_operation(
container: ResolvedVc<ProjectContainer>,
) -> Result<Vc<HmrIdentifiersWithIssues>> {
let hmr_identifiers_op = project_container_hmr_identifiers_operation(container);
let hmr_identifiers = hmr_identifiers_op.read_strongly_consistent().await?;
let issues = get_issues(hmr_identifiers_op).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())
}
#[turbo_tasks::function(operation)]
fn project_container_hmr_identifiers_operation(
container: ResolvedVc<ProjectContainer>,
) -> Vc<Vec<RcStr>> {
container.hmr_identifiers()
}
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
pub fn project_hmr_identifiers_subscribe(
#[napi(ts_arg_type = "{ __napiType: \"Project\" }")] project: External<ProjectInstance>,
func: JsFunction,
) -> napi::Result<External<RootTask>> {
let turbo_tasks = project.turbo_tasks.clone();
let container = project.container;
subscribe(
turbo_tasks.clone(),
func,
move || async move {
let hmr_identifiers_with_issues_op =
get_hmr_identifiers_with_issues_operation(container.to_resolved().await?);
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: identifiers
.iter()
.map(|ident| ident.to_string())
.collect::<Vec<_>>(),
},
issues: issues