Skip to content

Commit

Permalink
Use service name as service worker script name
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot authored and dom96 committed Jan 16, 2023
1 parent 14837fd commit b566e3a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/workerd/io/worker.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ Worker::Script::Script(kj::Own<const Isolate> isolateParam, kj::StringPtr id,
// limit just to be safe. Don't add it to the rollover bank, though.
auto limitScope = isolate->getLimitEnforcer().enterStartupJs(lock, maybeLimitError);
impl->unboundScriptOrMainModule =
jsg::NonModuleScript::compile(script.mainScript, lock);
jsg::NonModuleScript::compile(script.mainScript, lock, script.mainScriptName);
}

break;
Expand Down
4 changes: 4 additions & 0 deletions src/workerd/io/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ class Worker::Script: public kj::AtomicRefcounted {
// Content of the script (JavaScript). Pointer is valid only until the Script constructor
// returns.

kj::StringPtr mainScriptName;
// Name of the script, used as the script origin for stack traces. Pointer is valid only until
// the Script constructor returns.

kj::Function<kj::Array<CompiledGlobal>(jsg::Lock& lock, const ApiIsolate& apiIsolate)>
compileGlobals;
// Callback which will compile the script-level globals, returning a list of them.
Expand Down
4 changes: 2 additions & 2 deletions src/workerd/jsg/modules.c++
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ void NonModuleScript::run(v8::Local<v8::Context> context) const {
check(boundScript->Run(context));
}

NonModuleScript NonModuleScript::compile(kj::StringPtr code, jsg::Lock& js) {
NonModuleScript NonModuleScript::compile(kj::StringPtr code, jsg::Lock& js, kj::StringPtr name) {
// Create a dummy script origin for it to appear in Sources panel.
auto isolate = js.v8Isolate;
v8::ScriptOrigin origin(isolate, v8StrIntern(isolate, "worker.js"));
v8::ScriptOrigin origin(isolate, v8StrIntern(isolate, name));
v8::ScriptCompiler::Source source(v8Str(isolate, code), origin);
return NonModuleScript(js,
check(v8::ScriptCompiler::CompileUnboundScript(isolate, &source)));
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/jsg/modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class NonModuleScript {
// Running the script will create a v8::Script instance bound to the given
// context then will run it to completion.

static jsg::NonModuleScript compile(kj::StringPtr code, jsg::Lock& js);
static jsg::NonModuleScript compile(kj::StringPtr code, jsg::Lock& js, kj::StringPtr name = "worker.js");

private:
v8::Global<v8::UnboundScript> unboundScript;
Expand Down
16 changes: 16 additions & 0 deletions src/workerd/server/server-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,22 @@ KJ_TEST("Server: serve basic Service Worker") {
Bad Request)"_blockquote);
}

KJ_TEST("Server: use service name as Service Worker origin") {
TestServer test(singleWorker(R"((
compatibilityDate = "2022-08-17",
serviceWorkerScript =
`addEventListener("fetch", event => {
` event.respondWith(new Response(new Error("Doh!").stack));
`})
))"_kj));

test.start();
auto conn = test.connect("test-addr");
conn.httpGet200("/", R"(
Error: Doh!
at hello:2:34)"_blockquote);
}

KJ_TEST("Server: serve basic modular Worker") {
TestServer test(singleWorker(R"((
compatibilityDate = "2022-08-17",
Expand Down
6 changes: 3 additions & 3 deletions src/workerd/server/server.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1569,9 +1569,9 @@ kj::Own<Server::Service> Server::makeWorker(kj::StringPtr name, config::Worker::
(*inspector)->registerIsolate(name, isolate.get());
}

auto script = isolate->newScript(name, WorkerdApiIsolate::extractSource(conf, errorReporter),
IsolateObserver::StartType::COLD,
false, errorReporter);
auto script = isolate->newScript(name,
WorkerdApiIsolate::extractSource(name, conf, errorReporter),
IsolateObserver::StartType::COLD, false, errorReporter);

struct FutureSubrequestChannel {
config::ServiceDesignator::Reader designator;
Expand Down
5 changes: 4 additions & 1 deletion src/workerd/server/workerd-api.c++
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ const jsg::TypeHandler<Worker::ApiIsolate::ErrorInterface>&
return kj::downcast<JsgWorkerdIsolate::Lock>(lock).getTypeHandler<ErrorInterface>();
}

Worker::Script::Source WorkerdApiIsolate::extractSource(config::Worker::Reader conf,
Worker::Script::Source WorkerdApiIsolate::extractSource(kj::StringPtr name,
config::Worker::Reader conf,
Worker::ValidationErrorReporter& errorReporter) {
switch (conf.which()) {
case config::Worker::MODULES: {
Expand All @@ -178,6 +179,7 @@ Worker::Script::Source WorkerdApiIsolate::extractSource(config::Worker::Reader c
case config::Worker::SERVICE_WORKER_SCRIPT:
return Worker::Script::ScriptSource {
conf.getServiceWorkerScript(),
name,
[conf,&errorReporter](jsg::Lock& lock, const Worker::ApiIsolate& apiIsolate) {
return kj::downcast<const WorkerdApiIsolate>(apiIsolate)
.compileScriptGlobals(lock, conf, errorReporter);
Expand All @@ -193,6 +195,7 @@ Worker::Script::Source WorkerdApiIsolate::extractSource(config::Worker::Reader c
invalid:
return Worker::Script::ScriptSource {
""_kj,
name,
[](jsg::Lock& lock, const Worker::ApiIsolate& apiIsolate)
-> kj::Array<Worker::Script::CompiledGlobal> {
return nullptr;
Expand Down
3 changes: 2 additions & 1 deletion src/workerd/server/workerd-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class WorkerdApiIsolate final: public Worker::ApiIsolate {
const jsg::TypeHandler<ErrorInterface>&
getErrorInterfaceTypeHandler(jsg::Lock& lock) const override;

static Worker::Script::Source extractSource(config::Worker::Reader conf,
static Worker::Script::Source extractSource(kj::StringPtr name,
config::Worker::Reader conf,
Worker::ValidationErrorReporter& errorReporter);

struct Global {
Expand Down

0 comments on commit b566e3a

Please sign in to comment.