Skip to content

Commit d32a041

Browse files
authored
fix: add integration tests for commonjs and enhance entrypoint discovery (#503)
* chore: add/update integration tests for import_map feature * fix: check main field in package.json as well if byonm is enabled * chore: add integration tests for commonjs * chore: add examples for commonjs
1 parent 1e680ab commit d32a041

File tree

33 files changed

+567
-53
lines changed

33 files changed

+567
-53
lines changed

crates/base/src/runtime/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use deno::deno_http;
3535
use deno::deno_http::DefaultHttpPropertyExtractor;
3636
use deno::deno_io;
3737
use deno::deno_net;
38+
use deno::deno_package_json;
3839
use deno::deno_telemetry;
3940
use deno::deno_tls;
4041
use deno::deno_tls::deno_native_certs::load_native_certs;
@@ -528,7 +529,7 @@ where
528529
}
529530
}
530531
if !is_some_entry_point && !found {
531-
bail!("could not find an appropriate entrypoint");
532+
main_module_url = Some(base_dir_url.clone());
532533
}
533534
}
534535
if is_some_entry_point {
@@ -561,11 +562,22 @@ where
561562

562563
let mut builder = DenoOptionsBuilder::new();
563564

564-
if let Some(module_url) = main_module_url {
565+
if let Some(module_url) = main_module_url.as_ref() {
565566
builder.set_entrypoint(Some(module_url.to_file_path().unwrap()));
566567
}
567568
emitter_factory.set_deno_options(builder.build()?);
568569

570+
let deno_options = emitter_factory.deno_options()?;
571+
if !is_some_entry_point
572+
&& main_module_url.is_some_and(|it| it == base_dir_url)
573+
&& deno_options
574+
.workspace()
575+
.root_pkg_json()
576+
.and_then(|it| it.main(deno_package_json::NodeModuleKind::Cjs))
577+
.is_none()
578+
{
579+
bail!("could not find an appropriate entrypoint");
580+
}
569581
let mut metadata = Metadata::default();
570582
let eszip = generate_binary_eszip(
571583
&mut metadata,

crates/base/src/utils/test_utils.rs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
#![allow(dead_code)]
22

33
use std::marker::PhantomPinned;
4+
use std::path::Path;
45
use std::path::PathBuf;
56
use std::sync::Arc;
67
use std::task::ready;
78
use std::task::Poll;
89
use std::time::Duration;
910

10-
use crate::server::ServerFlags;
11-
use crate::worker::pool::SupervisorPolicy;
12-
use crate::worker::pool::WorkerPoolPolicy;
13-
use crate::worker::TerminationToken;
14-
use crate::worker::{self};
15-
1611
use anyhow::bail;
1712
use anyhow::Context;
1813
use anyhow::Error;
1914
use either::Either::Right;
2015
use ext_event_worker::events::WorkerEventWithMetadata;
16+
use ext_workers::context::MainWorkerRuntimeOpts;
17+
use ext_workers::context::Timing;
18+
use ext_workers::context::UserWorkerRuntimeOpts;
19+
use ext_workers::context::WorkerContextInitOpts;
20+
use ext_workers::context::WorkerRequestMsg;
21+
use ext_workers::context::WorkerRuntimeOpts;
2122
use futures_util::future::BoxFuture;
2223
use futures_util::Future;
2324
use futures_util::FutureExt;
2425
use http_v02::Request;
2526
use http_v02::Response;
2627
use hyper_v014::Body;
2728
use pin_project::pin_project;
28-
29-
use ext_workers::context::MainWorkerRuntimeOpts;
30-
use ext_workers::context::Timing;
31-
use ext_workers::context::UserWorkerRuntimeOpts;
32-
use ext_workers::context::WorkerContextInitOpts;
33-
use ext_workers::context::WorkerRequestMsg;
34-
use ext_workers::context::WorkerRuntimeOpts;
3529
use scopeguard::ScopeGuard;
30+
use tokio::process::Command;
3631
use tokio::sync::mpsc;
3732
use tokio::sync::oneshot;
3833
use tokio::sync::Notify;
3934
use tokio::time::timeout;
4035
use tokio_util::sync::CancellationToken;
4136

37+
use crate::server::ServerFlags;
38+
use crate::worker;
39+
use crate::worker::pool::SupervisorPolicy;
40+
use crate::worker::pool::WorkerPoolPolicy;
41+
use crate::worker::TerminationToken;
42+
4243
pub struct CreateTestUserWorkerArgs(
4344
WorkerContextInitOpts,
4445
Option<SupervisorPolicy>,
@@ -372,6 +373,31 @@ pub fn test_user_runtime_opts() -> UserWorkerRuntimeOpts {
372373
}
373374
}
374375

376+
pub async fn ensure_npm_package_installed<P>(path: P)
377+
where
378+
P: AsRef<Path>,
379+
{
380+
let cwd = std::env::current_dir().unwrap();
381+
let path = cwd.join(path);
382+
383+
assert!(path.is_dir());
384+
385+
let output = Command::new("npm")
386+
.current_dir(path)
387+
.arg("i")
388+
.output()
389+
.await
390+
.unwrap();
391+
392+
if !output.status.success() {
393+
let stdout = String::from_utf8_lossy(&output.stdout);
394+
let stderr = String::from_utf8_lossy(&output.stderr);
395+
panic!(
396+
"failed to execute npm command\n\nSTDOUT: ${stdout}\n\nSTDERR: ${stderr}"
397+
);
398+
}
399+
}
400+
375401
async fn wait_termination(token: TerminationToken) {
376402
token.outbound.cancelled().await;
377403
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const http = require("http");
2+
3+
console.log(require);
4+
5+
const server = http.createServer((_, resp) => {
6+
resp.writeHead(200, {
7+
"content-type": "text-plain",
8+
});
9+
resp.write("meow");
10+
resp.end();
11+
});
12+
13+
server.listen(8080);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"workspaces": [],
3+
"type": "commonjs",
4+
"main": "meow.js"
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const express = require("express");
2+
const app = express();
3+
const expressWs = require("express-ws")(app);
4+
const port = 8080;
5+
6+
expressWs.app.ws("/commonjs-express-websocket", (ws) => {
7+
ws.send("meow");
8+
ws.on("message", (msg) => {
9+
ws.send(msg);
10+
});
11+
});
12+
13+
app.listen(port, () => {
14+
console.log(`app listening on port ${port}`);
15+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"workspaces": [],
3+
"type": "commonjs",
4+
"main": "index.js",
5+
"dependencies": {
6+
"express": "^4.21.2",
7+
"express-ws": "^5.0.2"
8+
},
9+
"devDependencies": {
10+
"@types/express-ws": "^3.0.5"
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const express = require("express");
2+
const app = express();
3+
const port = 8080;
4+
5+
console.log(require);
6+
7+
app.get("/commonjs-express", (_, res) => {
8+
res.send("meow");
9+
});
10+
11+
app.listen(port, () => {
12+
console.log(`app listening on port ${port}`);
13+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"workspaces": [],
3+
"type": "commonjs",
4+
"main": "index.js",
5+
"dependencies": {
6+
"express": "^4.21.2"
7+
}
8+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const { serve } = require("@hono/node-server");
2+
const { Hono } = require("hono");
3+
const { createNodeWebSocket } = require("@hono/node-ws");
4+
5+
const app = new Hono();
6+
const { upgradeWebSocket, injectWebSocket } = createNodeWebSocket({ app });
7+
const port = 8080;
8+
9+
app.get(
10+
"/commonjs-hono-websocket",
11+
upgradeWebSocket(() => {
12+
return {
13+
onOpen(_evt, ws) {
14+
ws.send("meow");
15+
},
16+
onMessage(evt, ws) {
17+
ws.send(evt.data);
18+
},
19+
};
20+
}),
21+
);
22+
23+
const server = serve({
24+
fetch: app.fetch,
25+
port,
26+
overrideGlobalObjects: false,
27+
});
28+
29+
injectWebSocket(server);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"workspaces": [],
3+
"type": "commonjs",
4+
"dependencies": {
5+
"@hono/node-server": "^1.13.8",
6+
"@hono/node-ws": "^1.1.0",
7+
"hono": "^4.7.2"
8+
}
9+
}

0 commit comments

Comments
 (0)