Skip to content

Commit ba8e106

Browse files
committed
simplify options
1 parent cb0b6a7 commit ba8e106

File tree

10 files changed

+77
-103
lines changed

10 files changed

+77
-103
lines changed

src/workerd/api/node/tests/process-stdio-nodejs-test.wd-test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ const unitTests :Workerd.Config = (
1414
],
1515
)
1616
),
17-
]
17+
],
1818
);

src/workerd/io/bundle-fs-test.c++

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ KJ_TEST("Guarding against circular symlinks works") {
116116
fixture.runInIoContext([&](const TestFixture::Environment& env) {
117117
// We don't need to set up a TmpDirStorageScope since we are running
118118
// within a test fixture that sets up an IoContext...
119-
auto vfs =
120-
newVirtualFileSystem(kj::heap<FsMap>(), getTmpDirectoryImpl(), ProcessStdioPrefixed::YES);
119+
auto vfs = newVirtualFileSystem(kj::heap<FsMap>(), getTmpDirectoryImpl());
121120

122121
// Set up circular symlinks
123122
auto maybeTemp = KJ_ASSERT_NONNULL(vfs->resolve(env.js, "file:///"_url));

src/workerd/io/worker-fs.c++

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -895,14 +895,11 @@ class FdHandle final {
895895

896896
class VirtualFileSystemImpl final: public VirtualFileSystem {
897897
public:
898-
VirtualFileSystemImpl(kj::Own<FsMap> fsMap,
899-
kj::Rc<Directory>&& root,
900-
kj::Own<VirtualFileSystem::Observer> observer,
901-
ProcessStdioPrefixed processStdioPrefixed)
898+
VirtualFileSystemImpl(
899+
kj::Own<FsMap> fsMap, kj::Rc<Directory>&& root, kj::Own<VirtualFileSystem::Observer> observer)
902900
: fsMap(kj::mv(fsMap)),
903901
root(kj::mv(root)),
904902
observer(kj::mv(observer)),
905-
processStdioPrefixed(processStdioPrefixed),
906903
weakThis(
907904
kj::rc<WeakRef<VirtualFileSystemImpl>>(kj::Badge<VirtualFileSystemImpl>(), *this)) {}
908905

@@ -1120,7 +1117,6 @@ class VirtualFileSystemImpl final: public VirtualFileSystem {
11201117
kj::Own<FsMap> fsMap;
11211118
mutable kj::Rc<Directory> root;
11221119
kj::Own<VirtualFileSystem::Observer> observer;
1123-
ProcessStdioPrefixed processStdioPrefixed;
11241120
mutable kj::Rc<WeakRef<VirtualFileSystemImpl>> weakThis;
11251121
friend class FdHandle;
11261122

@@ -1266,25 +1262,20 @@ kj::Rc<File> File::newReadable(kj::ArrayPtr<const kj::byte> data) {
12661262
return kj::rc<FileImpl>(data);
12671263
}
12681264

1269-
kj::Own<VirtualFileSystem> newVirtualFileSystem(kj::Own<FsMap> fsMap,
1270-
kj::Rc<Directory>&& root,
1271-
kj::Own<VirtualFileSystem::Observer> observer,
1272-
ProcessStdioPrefixed processStdioPrefixed) {
1273-
return kj::heap<VirtualFileSystemImpl>(
1274-
kj::mv(fsMap), kj::mv(root), kj::mv(observer), processStdioPrefixed);
1265+
kj::Own<VirtualFileSystem> newVirtualFileSystem(
1266+
kj::Own<FsMap> fsMap, kj::Rc<Directory>&& root, kj::Own<VirtualFileSystem::Observer> observer) {
1267+
return kj::heap<VirtualFileSystemImpl>(kj::mv(fsMap), kj::mv(root), kj::mv(observer));
12751268
}
12761269

12771270
kj::Own<VirtualFileSystem> newWorkerFileSystem(kj::Own<FsMap> fsMap,
12781271
kj::Rc<Directory> bundleDirectory,
1279-
kj::Own<VirtualFileSystem::Observer> observer,
1280-
ProcessStdioPrefixed processStdioPrefixed) {
1272+
kj::Own<VirtualFileSystem::Observer> observer) {
12811273
// Our root directory is a read-only directory
12821274
Directory::Builder builder;
12831275
builder.addPath(fsMap->getBundlePath(), kj::mv(bundleDirectory));
12841276
builder.addPath(fsMap->getTempPath(), getTmpDirectoryImpl());
12851277
builder.addPath(fsMap->getDevPath(), getDevDirectory());
1286-
return newVirtualFileSystem(
1287-
kj::mv(fsMap), builder.finish(), kj::mv(observer), processStdioPrefixed);
1278+
return newVirtualFileSystem(kj::mv(fsMap), builder.finish(), kj::mv(observer));
12881279
}
12891280

12901281
kj::Rc<Directory> getTmpDirectoryImpl() {
@@ -1761,10 +1752,7 @@ class DevRandomFile final: public File, public kj::EnableAddRefToThis<DevRandomF
17611752
// - inspector reporting
17621753
// - structured logging
17631754
// - stdio output otherwise
1764-
void writeStdio(jsg::Lock& js,
1765-
VirtualFileSystem::Stdio type,
1766-
kj::ArrayPtr<const kj::byte> bytes,
1767-
ProcessStdioPrefixed processStdioPrefixed) {
1755+
void writeStdio(jsg::Lock& js, VirtualFileSystem::Stdio type, kj::ArrayPtr<const kj::byte> bytes) {
17681756
auto chars = bytes.asChars();
17691757
size_t endPos = chars.size();
17701758
if (chars[endPos - 1] == '\n') endPos--;
@@ -1776,6 +1764,7 @@ void writeStdio(jsg::Lock& js,
17761764
auto methodFunc = methodVal.As<v8::Function>();
17771765

17781766
kj::String outputStr;
1767+
auto processStdioPrefixed = Worker::Isolate::from(js).getProcessStdioPrefixed();
17791768
if (processStdioPrefixed == ProcessStdioPrefixed::YES) {
17801769
if (endPos == 0) {
17811770
v8::Local<v8::Value> args[] = {
@@ -1800,9 +1789,8 @@ void writeStdio(jsg::Lock& js,
18001789
// logging mechanisms. Reads always return EOF (0-byte reads).
18011790
class StdioFile final: public File, public kj::EnableAddRefToThis<StdioFile> {
18021791
public:
1803-
StdioFile(VirtualFileSystem::Stdio type, ProcessStdioPrefixed processStdioPrefixed)
1792+
StdioFile(VirtualFileSystem::Stdio type)
18041793
: type(type),
1805-
processStdioPrefixed(processStdioPrefixed),
18061794
// TODO(sometime): Investigate if we can refactor out the weakref here?
18071795
weakThis(kj::rc<WeakRef<StdioFile>>(kj::Badge<StdioFile>(), *this)) {}
18081796

@@ -1876,11 +1864,11 @@ class StdioFile final: public File, public kj::EnableAddRefToThis<StdioFile> {
18761864
if (lineBuffer.size() > 0) {
18771865
// We have buffered data - append the line data to it
18781866
lineBuffer.addAll(lineData);
1879-
writeStdio(js, type, lineBuffer.asPtr(), processStdioPrefixed);
1867+
writeStdio(js, type, lineBuffer.asPtr());
18801868
lineBuffer.clear();
18811869
} else {
18821870
// No buffered data - log this line directly
1883-
writeStdio(js, type, lineData, processStdioPrefixed);
1871+
writeStdio(js, type, lineData);
18841872
}
18851873

18861874
pos = newlinePos + 1;
@@ -1934,7 +1922,6 @@ class StdioFile final: public File, public kj::EnableAddRefToThis<StdioFile> {
19341922

19351923
private:
19361924
VirtualFileSystem::Stdio type;
1937-
ProcessStdioPrefixed processStdioPrefixed;
19381925
mutable kj::Maybe<kj::String> maybeUniqueId;
19391926

19401927
static constexpr size_t MAX_LINE_BUFFER_SIZE = 4096;
@@ -1956,7 +1943,7 @@ class StdioFile final: public File, public kj::EnableAddRefToThis<StdioFile> {
19561943

19571944
if (self.lineBuffer.size() > 0) {
19581945
if (IoContext::hasCurrent()) {
1959-
writeStdio(js, self.type, self.lineBuffer.asPtr(), self.processStdioPrefixed);
1946+
writeStdio(js, self.type, self.lineBuffer.asPtr());
19601947
}
19611948
self.lineBuffer.clear();
19621949
}
@@ -1999,7 +1986,7 @@ kj::Rc<VirtualFileSystem::OpenedFile> VirtualFileSystemImpl::getStdio(
19991986
KJ_IF_SOME(existing, openedFiles.find(n)) {
20001987
return existing.addRef();
20011988
}
2002-
auto stdioFile = kj::rc<StdioFile>(stdio, processStdioPrefixed);
1989+
auto stdioFile = kj::rc<StdioFile>(stdio);
20031990
kj::Rc<workerd::File> file = kj::mv(stdioFile);
20041991
auto opened = kj::rc<VirtualFileSystem::OpenedFile>(n, true, true, true, kj::mv(file));
20051992
openedFiles.insert(n, opened.addRef());

src/workerd/io/worker-fs.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,7 @@ class VirtualFileSystem {
694694

695695
kj::Own<VirtualFileSystem> newVirtualFileSystem(kj::Own<FsMap> fsMap,
696696
kj::Rc<Directory>&& root,
697-
kj::Own<VirtualFileSystem::Observer> observer,
698-
workerd::ProcessStdioPrefixed processStdioPrefixed) KJ_WARN_UNUSED_RESULT;
697+
kj::Own<VirtualFileSystem::Observer> observer) KJ_WARN_UNUSED_RESULT;
699698

700699
// The FsMap is a configurable mapping of built-in "known" file system
701700
// paths to user-configurable locations. It is used to allow user-specified
@@ -803,8 +802,7 @@ class SymbolicLinkRecursionGuardScope final {
803802
// provides the directory structure for the worker's bundle.
804803
kj::Own<VirtualFileSystem> newWorkerFileSystem(kj::Own<FsMap> fsMap,
805804
kj::Rc<Directory> bundleDirectory,
806-
kj::Own<VirtualFileSystem::Observer> observer,
807-
workerd::ProcessStdioPrefixed processStdioPrefixed) KJ_WARN_UNUSED_RESULT;
805+
kj::Own<VirtualFileSystem::Observer> observer) KJ_WARN_UNUSED_RESULT;
808806

809807
// Exposed only for testing purposes.
810808
kj::Rc<Directory> getTmpDirectoryImpl() KJ_WARN_UNUSED_RESULT;

src/workerd/io/worker.c++

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,7 @@ struct Worker::Isolate::Impl {
575575
progressCounter(impl.lockSuccessCount),
576576
oldCurrentApi(currentApi),
577577
limitEnforcer(isolate.getLimitEnforcer()),
578-
consoleMode(isolate.consoleMode),
579-
structuredLogging(isolate.structuredLogging),
578+
loggingOptions(isolate.loggingOptions),
580579
lock(isolate.api->lock(stackScope)) {
581580
WarnAboutIsolateLockScope::maybeWarn();
582581

@@ -624,7 +623,7 @@ struct Worker::Isolate::Impl {
624623
i.get()->contextCreated(
625624
v8_inspector::V8ContextInfo(context, 1, jsg::toInspectorStringView("Worker")));
626625
}
627-
Worker::setupContext(*lock, context, consoleMode, structuredLogging);
626+
Worker::setupContext(*lock, context, loggingOptions);
628627
}
629628

630629
void disposeContext(jsg::JsContext<api::ServiceWorkerGlobalScope> context) {
@@ -659,11 +658,9 @@ struct Worker::Isolate::Impl {
659658

660659
const IsolateLimitEnforcer& limitEnforcer; // only so we can call getIsolateStats()
661660

662-
ConsoleMode consoleMode;
663-
664661
// When structuredLogging is YES AND consoleMode is STDOUT js logs will be emitted to STDOUT
665-
// as newline separated json objects.
666-
StructuredLogging structuredLogging;
662+
// as newline separated json objects
663+
LoggingOptions loggingOptions;
667664

668665
public:
669666
kj::Own<jsg::Lock> lock;
@@ -1019,16 +1016,12 @@ Worker::Isolate::Isolate(kj::Own<Api> apiParam,
10191016
kj::StringPtr id,
10201017
kj::Own<IsolateLimitEnforcer> limitEnforcerParam,
10211018
InspectorPolicy inspectorPolicy,
1022-
ConsoleMode consoleMode,
1023-
StructuredLogging structuredLogging,
1024-
ProcessStdioPrefixed processStdioPrefixed)
1019+
LoggingOptions loggingOptions)
10251020
: metrics(kj::mv(metricsParam)),
10261021
id(kj::str(id)),
10271022
limitEnforcer(kj::mv(limitEnforcerParam)),
10281023
api(kj::mv(apiParam)),
1029-
consoleMode(consoleMode),
1030-
structuredLogging(structuredLogging),
1031-
processStdioPrefixed(processStdioPrefixed),
1024+
loggingOptions(loggingOptions),
10321025
featureFlagsForFl(makeCompatJson(decompileCompatibilityFlagsForFl(api->getFeatureFlags()))),
10331026
impl(kj::heap<Impl>(*api, *metrics, *limitEnforcer, inspectorPolicy)),
10341027
weakIsolateRef(WeakIsolateRef::wrap(this)),
@@ -1512,10 +1505,8 @@ void setWebAssemblyModuleHasInstance(jsg::Lock& lock, v8::Local<v8::Context> con
15121505
module->DefineOwnProperty(context, v8::Symbol::GetHasInstance(lock.v8Isolate), function));
15131506
}
15141507

1515-
void Worker::setupContext(jsg::Lock& lock,
1516-
v8::Local<v8::Context> context,
1517-
Worker::ConsoleMode consoleMode,
1518-
StructuredLogging structuredLogging) {
1508+
void Worker::setupContext(
1509+
jsg::Lock& lock, v8::Local<v8::Context> context, const LoggingOptions& loggingOptions) {
15191510
// Set WebAssembly.Module @@HasInstance
15201511
setWebAssemblyModuleHasInstance(lock, context);
15211512

@@ -1531,9 +1522,9 @@ void Worker::setupContext(jsg::Lock& lock,
15311522
lock.v8Isolate, jsg::check(console->Get(context, methodStr)).As<v8::Function>());
15321523

15331524
auto f = lock.wrapSimpleFunction(context,
1534-
[consoleMode, level, structuredLogging, original = kj::mv(original)](
1525+
[loggingOptions, level, original = kj::mv(original)](
15351526
jsg::Lock& js, const v8::FunctionCallbackInfo<v8::Value>& info) {
1536-
handleLog(js, consoleMode, level, structuredLogging, original, info);
1527+
handleLog(js, loggingOptions, level, original, info);
15371528
});
15381529
jsg::check(console->Set(context, methodStr, f));
15391530
};
@@ -1848,9 +1839,8 @@ void Worker::processEntrypointClass(jsg::Lock& js,
18481839
}
18491840

18501841
void Worker::handleLog(jsg::Lock& js,
1851-
ConsoleMode consoleMode,
1842+
const LoggingOptions& loggingOptions,
18521843
LogLevel level,
1853-
StructuredLogging structuredLogging,
18541844
const v8::Global<v8::Function>& original,
18551845
const v8::FunctionCallbackInfo<v8::Value>& info) {
18561846
// Call original V8 implementation so messages sent to connected inspector if any
@@ -1947,7 +1937,7 @@ void Worker::handleLog(jsg::Lock& js,
19471937
}
19481938
}
19491939

1950-
if (consoleMode == ConsoleMode::INSPECTOR_ONLY) {
1940+
if (loggingOptions.consoleMode == LoggingOptions::ConsoleMode::INSPECTOR_ONLY) {
19511941
// Lets us dump console.log()s to stdout when running test-runner with --verbose flag, to make
19521942
// it easier to debug tests. Note that when --verbose is not passed, KJ_LOG(INFO, ...) will
19531943
// not even evaluate its arguments, so `message()` will not be called at all.
@@ -1966,7 +1956,7 @@ void Worker::handleLog(jsg::Lock& js,
19661956

19671957
// Log warnings and errors to stderr
19681958
// Always log to stdout when structuredLogging is enabled.
1969-
auto useStderr = level >= LogLevel::WARN && !structuredLogging;
1959+
auto useStderr = level >= LogLevel::WARN && !loggingOptions.structuredLogging;
19701960
auto fd = useStderr ? stderr : stdout;
19711961
auto tty = useStderr ? STDERR_TTY : STDOUT_TTY;
19721962
auto colors =
@@ -1980,7 +1970,7 @@ void Worker::handleLog(jsg::Lock& js,
19801970

19811971
auto levelStr = logLevelToString(level);
19821972
args[length] = v8::Boolean::New(js.v8Isolate, colors);
1983-
args[length + 1] = v8::Boolean::New(js.v8Isolate, bool(structuredLogging));
1973+
args[length + 1] = v8::Boolean::New(js.v8Isolate, bool(loggingOptions.structuredLogging));
19841974
args[length + 2] = jsg::v8StrIntern(js.v8Isolate, levelStr);
19851975
auto formatted = js.toString(
19861976
jsg::check(formatLog->Call(context, js.v8Undefined(), length + 3, args.data())));
@@ -3254,7 +3244,7 @@ void Worker::Isolate::logWarning(kj::StringPtr description, Lock& lock) {
32543244
});
32553245
}
32563246

3257-
if (consoleMode == ConsoleMode::INSPECTOR_ONLY) {
3247+
if (loggingOptions.consoleMode == LoggingOptions::ConsoleMode::INSPECTOR_ONLY) {
32583248
// Run with --verbose to log JS exceptions to stderr. Useful when running tests.
32593249
KJ_LOG(INFO, "console warning", description);
32603250
} else {

src/workerd/io/worker.h

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ namespace workerd {
3535
WD_STRONG_BOOL(StructuredLogging);
3636
WD_STRONG_BOOL(ProcessStdioPrefixed);
3737

38+
struct LoggingOptions {
39+
enum class ConsoleMode {
40+
// Only send `console.log`s to the inspector. Default, production behavior.
41+
INSPECTOR_ONLY,
42+
// Send `console.log`s to the inspector and stdout/err. Behavior running `workerd` locally.
43+
STDOUT,
44+
};
45+
46+
ConsoleMode consoleMode = ConsoleMode::INSPECTOR_ONLY;
47+
StructuredLogging structuredLogging = StructuredLogging::NO;
48+
ProcessStdioPrefixed processStdioPrefixed = ProcessStdioPrefixed::YES;
49+
};
50+
3851
namespace api {
3952
class DurableObjectState;
4053
class DurableObjectStorage;
@@ -112,13 +125,6 @@ class Worker: public kj::AtomicRefcounted {
112125

113126
class LockType;
114127

115-
enum class ConsoleMode {
116-
// Only send `console.log`s to the inspector. Default, production behavior.
117-
INSPECTOR_ONLY,
118-
// Send `console.log`s to the inspector and stdout/err. Behavior running `workerd` locally.
119-
STDOUT,
120-
};
121-
122128
explicit Worker(kj::Own<const Script> script,
123129
kj::Own<WorkerObserver> metrics,
124130
kj::FunctionParam<void(jsg::Lock& lock,
@@ -184,10 +190,8 @@ class Worker: public kj::AtomicRefcounted {
184190
void setConnectOverride(kj::String networkAddress, ConnectFn connectFn);
185191
kj::Maybe<ConnectFn&> getConnectOverride(kj::StringPtr networkAddress);
186192

187-
static void setupContext(jsg::Lock& lock,
188-
v8::Local<v8::Context> context,
189-
Worker::ConsoleMode consoleMode,
190-
StructuredLogging structuredLogging);
193+
static void setupContext(
194+
jsg::Lock& lock, v8::Local<v8::Context> context, const LoggingOptions& loggingOptions);
191195

192196
private:
193197
kj::Own<const Script> script;
@@ -213,9 +217,8 @@ class Worker: public kj::AtomicRefcounted {
213217
friend constexpr bool _kj_internal_isPolymorphic(AsyncWaiter*);
214218

215219
static void handleLog(jsg::Lock& js,
216-
ConsoleMode mode,
220+
const LoggingOptions& loggingOptions,
217221
LogLevel level,
218-
StructuredLogging structuredLogging,
219222
const v8::Global<v8::Function>& original,
220223
const v8::FunctionCallbackInfo<v8::Value>& info);
221224

@@ -333,9 +336,7 @@ class Worker::Isolate: public kj::AtomicRefcounted {
333336
kj::StringPtr id,
334337
kj::Own<IsolateLimitEnforcer> limitEnforcer,
335338
InspectorPolicy inspectorPolicy,
336-
ConsoleMode consoleMode = ConsoleMode::INSPECTOR_ONLY,
337-
StructuredLogging structuredLogging = StructuredLogging::NO,
338-
ProcessStdioPrefixed processStdioPrefixed = ProcessStdioPrefixed::YES);
339+
LoggingOptions loggingOptions = {});
339340

340341
~Isolate() noexcept(false);
341342
KJ_DISALLOW_COPY_AND_MOVE(Isolate);
@@ -451,6 +452,11 @@ class Worker::Isolate: public kj::AtomicRefcounted {
451452

452453
bool isInspectorEnabled() const;
453454

455+
// Get the process stdio prefixed setting from logging options
456+
inline ProcessStdioPrefixed getProcessStdioPrefixed() const {
457+
return loggingOptions.processStdioPrefixed;
458+
}
459+
454460
// Represents a weak reference back to the isolate that code within the isolate can use as an
455461
// indirect pointer when they want to be able to race destruction safely. A caller wishing to
456462
// use a weak reference to the isolate should acquire a strong reference to weakIsolateRef.
@@ -479,9 +485,7 @@ class Worker::Isolate: public kj::AtomicRefcounted {
479485
kj::String id;
480486
kj::Own<IsolateLimitEnforcer> limitEnforcer;
481487
kj::Own<Api> api;
482-
ConsoleMode consoleMode;
483-
StructuredLogging structuredLogging;
484-
ProcessStdioPrefixed processStdioPrefixed;
488+
LoggingOptions loggingOptions;
485489

486490
// If non-null, a serialized JSON object with a single "flags" property, which is a list of
487491
// compatibility enable-flags that are relevant to FL.

0 commit comments

Comments
 (0)