Skip to content

Commit ac41eea

Browse files
committed
fix loading TypeScript main modules
1 parent 4098fc2 commit ac41eea

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ Also, this enables portability of edge functions to those users who want to self
1717
* Hot reload when a service changes
1818
* Support import maps
1919
* Check verify-jwt
20-
* configuarble memory limit (update via flag)
21-
* configurable wall clock limit (update via flag)
2220
* better error messages for incorrect module loading paths (local)
2321
* handle 404 errors
2422
* Support snapshotting the runtime

base/src/js_worker.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ fn start_runtime(
137137

138138
let module_path = service_path.join("index.ts");
139139
// TODO: handle file missing error
140-
let main_module_code = fs::read_to_string(&module_path).unwrap();
141140
let main_module_url = Url::from_file_path(
142141
std::env::current_dir()
143142
.map(|p| p.join(&module_path))
@@ -153,9 +152,7 @@ fn start_runtime(
153152
.unwrap();
154153

155154
let future = async move {
156-
let mod_id = js_runtime
157-
.load_main_module(&main_module_url, Some(main_module_code))
158-
.await?;
155+
let mod_id = js_runtime.load_main_module(&main_module_url, None).await?;
159156
let result = js_runtime.mod_evaluate(mod_id);
160157
js_runtime.run_event_loop(false).await?;
161158

@@ -165,7 +162,7 @@ fn start_runtime(
165162
let res = local.block_on(&runtime, future);
166163

167164
if res.is_err() {
168-
debug!("worker thread panicked {:?}", res.as_ref().err().unwrap());
165+
error!("worker thread panicked {:?}", res.as_ref().err().unwrap());
169166
}
170167
shutdown_tx
171168
.send(())
@@ -183,14 +180,13 @@ fn start_controller_thread(
183180
.build()
184181
.unwrap();
185182

186-
// TODO: make it configurable
187183
let future = async move {
188184
tokio::select! {
189185
_ = tokio::time::sleep(Duration::from_millis(worker_timeout_ms)) => {
190-
error!("worker timed out. (duration {})", human_elapsed(worker_timeout_ms))
186+
debug!("max duration reached for the worker. terminating the worker. (duration {})", human_elapsed(worker_timeout_ms))
191187
}
192188
Some(val) = memory_limit_rx.recv() => {
193-
error!("memory limit reached for worker. (used: {})", bytes_to_display(val))
189+
error!("memory limit reached for the worker. terminating the worker. (used: {})", bytes_to_display(val))
194190
}
195191
}
196192
};
@@ -199,6 +195,8 @@ fn start_controller_thread(
199195
let ok = v8_thread_safe_handle.terminate_execution();
200196
if ok {
201197
debug!("worker terminated");
198+
} else {
199+
debug!("worker already terminated");
202200
}
203201
});
204202
}

base/src/js_worker/js/bootstrap.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
((window) => {
44
const core = Deno.core;
55
const ops = core.ops;
6-
const { ObjectDefineProperties }= window.__bootstrap.primordials;
6+
const { ObjectDefineProperties, ObjectPrototypeIsPrototypeOf }= window.__bootstrap.primordials;
77

88
const base64 = window.__bootstrap.base64;
99
const Console = window.__bootstrap.console.Console;
10+
const colors = window.__bootstrap.colors;
1011
const crypto = window.__bootstrap.crypto;
1112
const domException = window.__bootstrap.domException;
1213
const encoding = window.__bootstrap.encoding;
@@ -19,6 +20,7 @@
1920
const fileReader = window.__bootstrap.fileReader;
2021
const formData = window.__bootstrap.formData;
2122
const headers = window.__bootstrap.headers;
23+
const inspectArgs = window.__bootstrap.console.inspectArgs;
2224
const streams = window.__bootstrap.streams;
2325
const timers = window.__bootstrap.timers;
2426
const url = window.__bootstrap.url;
@@ -260,6 +262,23 @@
260262
// },
261263
// );
262264
//}
265+
//
266+
267+
function formatException(error) {
268+
if (ObjectPrototypeIsPrototypeOf(ErrorPrototype, error)) {
269+
return null;
270+
} else if (typeof error == "string") {
271+
return `Uncaught ${
272+
inspectArgs([quoteString(error)], {
273+
colors: !colors.getNoColor(),
274+
})
275+
}`;
276+
} else {
277+
return `Uncaught ${
278+
inspectArgs([error], { colors: !colors.getNoColor() })
279+
}`;
280+
}
281+
}
263282

264283
function runtimeStart(runtimeOptions, source) {
265284
core.setMacrotaskCallback(timers.handleTimerMacrotask);
@@ -274,7 +293,7 @@
274293
//);
275294
//build.setBuildInfo(runtimeOptions.target);
276295
//util.setLogDebug(runtimeOptions.debugFlag, source);
277-
//colors.setNoColor(runtimeOptions.noColor || !runtimeOptions.isTty);
296+
colors.setNoColor(runtimeOptions.noColor || !runtimeOptions.isTty);
278297

279298
// deno-lint-ignore prefer-primordials
280299
Error.prepareStackTrace = core.prepareStackTrace;
@@ -299,7 +318,8 @@
299318
denoVersion: "NA",
300319
v8Version: "NA",
301320
tsVersion: "NA",
302-
noColor: false,
321+
noColor: true,
322+
isTty: false
303323
});
304324
})(this);
305325

examples/foo/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { serve } from "https://deno.land/[email protected]/http/server.ts"
22

3-
Deno.env.set("foo", "bar");
3+
interface reqPayload {
4+
name: string;
5+
}
46

5-
serve(async (req) => {
6-
const { name } = await req.json()
7+
console.log('server started');
8+
9+
serve(async (req: Request) => {
10+
const { name } : reqPayload = await req.json()
711
const data = {
812
message: `Hello ${name} from foo!`,
913
}
@@ -12,4 +16,4 @@ serve(async (req) => {
1216
JSON.stringify(data),
1317
{ headers: { "Content-Type": "application/json", "Connection": "keep-alive" } },
1418
)
15-
})
19+
}, { port: 9005 })

0 commit comments

Comments
 (0)