@@ -29,6 +29,7 @@ use base::utils::test_utils::create_test_user_worker;
2929use base:: utils:: test_utils:: ensure_npm_package_installed;
3030use base:: utils:: test_utils:: test_user_runtime_opts;
3131use base:: utils:: test_utils:: test_user_worker_pool_policy;
32+ use base:: utils:: test_utils:: TestBed ;
3233use base:: utils:: test_utils:: TestBedBuilder ;
3334use base:: utils:: test_utils:: { self } ;
3435use base:: worker;
@@ -212,6 +213,7 @@ async fn test_not_trigger_pku_sigsegv_due_to_jit_compilation_non_cli() {
212213 . init_opts ( WorkerContextInitOpts {
213214 service_path : "./test_cases/slow_resp" . into ( ) ,
214215 no_module_cache : false ,
216+ no_npm : None ,
215217 env_vars : HashMap :: new ( ) ,
216218 timing : None ,
217219 maybe_eszip : None ,
@@ -371,6 +373,7 @@ async fn test_main_worker_boot_error() {
371373 . init_opts ( WorkerContextInitOpts {
372374 service_path : "./test_cases/meow" . into ( ) ,
373375 no_module_cache : false ,
376+ no_npm : None ,
374377 env_vars : HashMap :: new ( ) ,
375378 timing : None ,
376379 maybe_eszip : None ,
@@ -494,6 +497,7 @@ async fn test_main_worker_user_worker_mod_evaluate_exception() {
494497 . init_opts ( WorkerContextInitOpts {
495498 service_path : "./test_cases/main" . into ( ) ,
496499 no_module_cache : false ,
500+ no_npm : None ,
497501 env_vars : HashMap :: new ( ) ,
498502 timing : None ,
499503 maybe_eszip : None ,
@@ -877,6 +881,7 @@ async fn test_worker_boot_invalid_imports() {
877881 let opts = WorkerContextInitOpts {
878882 service_path : "./test_cases/invalid_imports" . into ( ) ,
879883 no_module_cache : false ,
884+ no_npm : None ,
880885 env_vars : HashMap :: new ( ) ,
881886 timing : None ,
882887 maybe_eszip : None ,
@@ -905,6 +910,7 @@ async fn test_worker_boot_with_0_byte_eszip() {
905910 let opts = WorkerContextInitOpts {
906911 service_path : "./test_cases/meow" . into ( ) ,
907912 no_module_cache : false ,
913+ no_npm : None ,
908914 env_vars : HashMap :: new ( ) ,
909915 timing : None ,
910916 maybe_eszip : Some ( EszipPayloadKind :: VecKind ( vec ! [ ] ) ) ,
@@ -932,6 +938,7 @@ async fn test_worker_boot_with_invalid_entrypoint() {
932938 let opts = WorkerContextInitOpts {
933939 service_path : "./test_cases/meow" . into ( ) ,
934940 no_module_cache : false ,
941+ no_npm : None ,
935942 env_vars : HashMap :: new ( ) ,
936943 timing : None ,
937944 maybe_eszip : None ,
@@ -4095,6 +4102,80 @@ async fn test_user_workers_cleanup_idle_workers() {
40954102 unreachable ! ( "test failed" ) ;
40964103}
40974104
4105+ #[ tokio:: test]
4106+ #[ serial]
4107+ async fn test_no_npm ( ) {
4108+ async fn send_it (
4109+ no_npm : bool ,
4110+ tx : mpsc:: UnboundedSender < WorkerEventWithMetadata > ,
4111+ ) -> ( TestBed , u16 ) {
4112+ let tb = TestBedBuilder :: new ( "./test_cases/main" )
4113+ . with_per_worker_policy ( None )
4114+ . with_worker_event_sender ( Some ( tx) )
4115+ . build ( )
4116+ . await ;
4117+
4118+ let resp = tb
4119+ . request ( |mut b| {
4120+ b = b. uri ( "/npm-import-with-package-json" ) ;
4121+ if no_npm {
4122+ b = b. header ( "x-no-npm" , HeaderValue :: from_static ( "1" ) )
4123+ }
4124+ b. body ( Body :: empty ( ) ) . context ( "can't make request" )
4125+ } )
4126+ . await
4127+ . unwrap ( ) ;
4128+
4129+ ( tb, resp. status ( ) . as_u16 ( ) )
4130+ }
4131+
4132+ {
4133+ // Since `noNpm` is configured, it will not discover package.json, and
4134+ // `npm:is-even` will resolve normally using Deno's original module
4135+ // resolution method.
4136+ let ( tx, mut rx) = mpsc:: unbounded_channel ( ) ;
4137+ let ( tb, status_code) = send_it ( true , tx) . await ;
4138+
4139+ assert_eq ! ( status_code, StatusCode :: OK ) ;
4140+
4141+ rx. close ( ) ;
4142+ tb. exit ( Duration :: from_secs ( TESTBED_DEADLINE_SEC ) ) . await ;
4143+ }
4144+ {
4145+ // Note that `noNpm` is not set this time. In this case, it will try to
4146+ // discover package.json and will eventually find it.
4147+ //
4148+ // This causes it to switch to Byonm mode and try to resolve modules from
4149+ // the adjacent node_modules/.
4150+ //
4151+ // However, since node_modules/ does not exist anywhere, the attempt to
4152+ // resolve `npm:is-even` will fail.
4153+ let ( tx, mut rx) = mpsc:: unbounded_channel ( ) ;
4154+ let ( tb, status_code) = send_it ( false , tx) . await ;
4155+
4156+ assert_eq ! ( status_code, StatusCode :: INTERNAL_SERVER_ERROR ) ;
4157+
4158+ rx. close ( ) ;
4159+ tb. exit ( Duration :: from_secs ( TESTBED_DEADLINE_SEC ) ) . await ;
4160+
4161+ while let Some ( ev) = rx. recv ( ) . await {
4162+ let WorkerEvents :: BootFailure ( ev) = ev. event else {
4163+ continue ;
4164+ } ;
4165+
4166+ assert ! ( ev. msg. starts_with(
4167+ "worker boot error: failed to bootstrap runtime: failed to create the \
4168+ graph: Could not find a matching package for 'npm:is-even' in the node_modules \
4169+ directory."
4170+ ) ) ;
4171+
4172+ return ;
4173+ }
4174+
4175+ unreachable ! ( "test failed" ) ;
4176+ }
4177+ }
4178+
40984179#[ derive( Deserialize ) ]
40994180struct ErrorResponsePayload {
41004181 msg : String ,
0 commit comments