@@ -136,7 +136,8 @@ Stat::Stat(const workerd::Stat& stat)
136136
137137kj::Maybe<Stat> FileSystemModule::stat (
138138 jsg::Lock& js, kj::OneOf<int , FilePath> pathOrFd, StatOptions options) {
139- auto & vfs = workerd::VirtualFileSystem::current (js);
139+ auto & vfs = JSG_REQUIRE_NONNULL (
140+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
140141 KJ_SWITCH_ONEOF (pathOrFd) {
141142 KJ_CASE_ONEOF (path, FilePath) {
142143 NormalizedFilePath normalizedPath (kj::mv (path));
@@ -187,7 +188,8 @@ kj::Maybe<Stat> FileSystemModule::stat(
187188
188189void FileSystemModule::setLastModified (
189190 jsg::Lock& js, kj::OneOf<int , FilePath> pathOrFd, kj::Date lastModified, StatOptions options) {
190- auto & vfs = workerd::VirtualFileSystem::current (js);
191+ auto & vfs = JSG_REQUIRE_NONNULL (
192+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
191193 KJ_SWITCH_ONEOF (pathOrFd) {
192194 KJ_CASE_ONEOF (path, FilePath) {
193195 NormalizedFilePath normalizedPath (kj::mv (path));
@@ -247,7 +249,8 @@ void FileSystemModule::setLastModified(
247249}
248250
249251void FileSystemModule::truncate (jsg::Lock& js, kj::OneOf<int , FilePath> pathOrFd, uint32_t size) {
250- auto & vfs = workerd::VirtualFileSystem::current (js);
252+ auto & vfs = JSG_REQUIRE_NONNULL (
253+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
251254 KJ_SWITCH_ONEOF (pathOrFd) {
252255 KJ_CASE_ONEOF (path, FilePath) {
253256 NormalizedFilePath normalizedPath (kj::mv (path));
@@ -302,7 +305,8 @@ void FileSystemModule::truncate(jsg::Lock& js, kj::OneOf<int, FilePath> pathOrFd
302305}
303306
304307kj::String FileSystemModule::readLink (jsg::Lock& js, FilePath path, ReadLinkOptions options) {
305- auto & vfs = workerd::VirtualFileSystem::current (js);
308+ auto & vfs = JSG_REQUIRE_NONNULL (
309+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
306310 NormalizedFilePath normalizedPath (kj::mv (path));
307311 KJ_IF_SOME (node, vfs.resolve (js, normalizedPath, {.followLinks = false })) {
308312 KJ_SWITCH_ONEOF (node) {
@@ -336,7 +340,8 @@ kj::String FileSystemModule::readLink(jsg::Lock& js, FilePath path, ReadLinkOpti
336340
337341void FileSystemModule::link (jsg::Lock& js, FilePath from, FilePath to, LinkOptions options) {
338342 // The from argument is where we are creating the link, while the to is the target.
339- auto & vfs = workerd::VirtualFileSystem::current (js);
343+ auto & vfs = JSG_REQUIRE_NONNULL (
344+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
340345 NormalizedFilePath normalizedFrom (kj::mv (from));
341346 NormalizedFilePath normalizedTo (kj::mv (to));
342347
@@ -407,7 +412,8 @@ void FileSystemModule::link(jsg::Lock& js, FilePath from, FilePath to, LinkOptio
407412}
408413
409414void FileSystemModule::unlink (jsg::Lock& js, FilePath path) {
410- auto & vfs = workerd::VirtualFileSystem::current (js);
415+ auto & vfs = JSG_REQUIRE_NONNULL (
416+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
411417 NormalizedFilePath normalizedPath (kj::mv (path));
412418 const jsg::Url& url = normalizedPath;
413419 auto relative = url.getRelative ();
@@ -447,7 +453,8 @@ void FileSystemModule::unlink(jsg::Lock& js, FilePath path) {
447453}
448454
449455int FileSystemModule::open (jsg::Lock& js, FilePath path, OpenOptions options) {
450- auto & vfs = workerd::VirtualFileSystem::current (js);
456+ auto & vfs = JSG_REQUIRE_NONNULL (
457+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
451458 NormalizedFilePath normalizedPath (kj::mv (path));
452459 KJ_SWITCH_ONEOF (vfs.openFd (js, normalizedPath,
453460 workerd::VirtualFileSystem::OpenOptions{
@@ -468,13 +475,15 @@ int FileSystemModule::open(jsg::Lock& js, FilePath path, OpenOptions options) {
468475}
469476
470477void FileSystemModule::close (jsg::Lock& js, int fd) {
471- auto & vfs = workerd::VirtualFileSystem::current (js);
478+ auto & vfs = JSG_REQUIRE_NONNULL (
479+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
472480 vfs.closeFd (js, fd);
473481}
474482
475483uint32_t FileSystemModule::write (
476484 jsg::Lock& js, int fd, kj::Array<jsg::BufferSource> data, WriteOptions options) {
477- auto & vfs = workerd::VirtualFileSystem::current (js);
485+ auto & vfs = JSG_REQUIRE_NONNULL (
486+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
478487
479488 KJ_IF_SOME (opened, vfs.tryGetFd (js, fd)) {
480489 static const auto getPosition = [](jsg::Lock& js, auto & opened, auto & file,
@@ -531,7 +540,8 @@ uint32_t FileSystemModule::write(
531540
532541uint32_t FileSystemModule::read (
533542 jsg::Lock& js, int fd, kj::Array<jsg::BufferSource> data, WriteOptions options) {
534- auto & vfs = workerd::VirtualFileSystem::current (js);
543+ auto & vfs = JSG_REQUIRE_NONNULL (
544+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
535545 KJ_IF_SOME (opened, vfs.tryGetFd (js, fd)) {
536546 if (!opened->read ) {
537547 throwUVException (js, UV_EBADF, " read" _kj);
@@ -573,7 +583,8 @@ uint32_t FileSystemModule::read(
573583}
574584
575585jsg::BufferSource FileSystemModule::readAll (jsg::Lock& js, kj::OneOf<int , FilePath> pathOrFd) {
576- auto & vfs = workerd::VirtualFileSystem::current (js);
586+ auto & vfs = JSG_REQUIRE_NONNULL (
587+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
577588 KJ_SWITCH_ONEOF (pathOrFd) {
578589 KJ_CASE_ONEOF (path, FilePath) {
579590 NormalizedFilePath normalized (kj::mv (path));
@@ -642,7 +653,8 @@ uint32_t FileSystemModule::writeAll(jsg::Lock& js,
642653 kj::OneOf<int , FilePath> pathOrFd,
643654 jsg::BufferSource data,
644655 WriteAllOptions options) {
645- auto & vfs = workerd::VirtualFileSystem::current (js);
656+ auto & vfs = JSG_REQUIRE_NONNULL (
657+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
646658
647659 if (data.size () > kMax ) {
648660 throwUVException (js, UV_EFBIG, " writeAll" _kj);
@@ -802,7 +814,8 @@ uint32_t FileSystemModule::writeAll(jsg::Lock& js,
802814void FileSystemModule::renameOrCopy (
803815 jsg::Lock& js, FilePath src, FilePath dest, RenameOrCopyOptions options) {
804816 // The source must exist, the destination must not.
805- auto & vfs = workerd::VirtualFileSystem::current (js);
817+ auto & vfs = JSG_REQUIRE_NONNULL (
818+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
806819 NormalizedFilePath normalizedSrc (kj::mv (src));
807820 NormalizedFilePath normalizedDest (kj::mv (dest));
808821
@@ -918,7 +931,8 @@ void FileSystemModule::renameOrCopy(
918931
919932jsg::Optional<kj::String> FileSystemModule::mkdir (
920933 jsg::Lock& js, FilePath path, MkdirOptions options) {
921- auto & vfs = workerd::VirtualFileSystem::current (js);
934+ auto & vfs = JSG_REQUIRE_NONNULL (
935+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
922936 NormalizedFilePath normalizedPath (kj::mv (path));
923937 const jsg::Url& url = normalizedPath;
924938
@@ -1063,7 +1077,8 @@ jsg::Optional<kj::String> FileSystemModule::mkdir(
10631077
10641078void FileSystemModule::rm (jsg::Lock& js, FilePath path, RmOptions options) {
10651079 // TODO(node-fs): Implement the force option.
1066- auto & vfs = workerd::VirtualFileSystem::current (js);
1080+ auto & vfs = JSG_REQUIRE_NONNULL (
1081+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
10671082 NormalizedFilePath normalizedPath (kj::mv (path));
10681083 const jsg::Url& url = normalizedPath;
10691084 auto relative = url.getRelative ();
@@ -1178,7 +1193,8 @@ void readdirImpl(jsg::Lock& js,
11781193
11791194kj::Array<FileSystemModule::DirEntHandle> FileSystemModule::readdir (
11801195 jsg::Lock& js, FilePath path, ReadDirOptions options) {
1181- auto & vfs = workerd::VirtualFileSystem::current (js);
1196+ auto & vfs = JSG_REQUIRE_NONNULL (
1197+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
11821198 NormalizedFilePath normalizedPath (kj::mv (path));
11831199
11841200 KJ_IF_SOME (node, vfs.resolve (js, normalizedPath, {.followLinks = false })) {
@@ -1205,7 +1221,8 @@ kj::Array<FileSystemModule::DirEntHandle> FileSystemModule::readdir(
12051221}
12061222
12071223jsg::Ref<FileFdHandle> FileFdHandle::constructor (jsg::Lock& js, int fd) {
1208- auto & vfs = workerd::VirtualFileSystem::current (js);
1224+ auto & vfs = JSG_REQUIRE_NONNULL (
1225+ workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
12091226 return js.alloc <FileFdHandle>(js, vfs, fd);
12101227}
12111228
@@ -1369,8 +1386,13 @@ jsg::Ref<jsg::DOMException> fsErrorToDomException(jsg::Lock& js, workerd::FsErro
13691386
13701387jsg::Promise<jsg::Ref<FileSystemDirectoryHandle>> StorageManager::getDirectory (
13711388 jsg::Lock& js, const jsg::TypeHandler<jsg::Ref<jsg::DOMException>>& exception) {
1372- auto & vfs = workerd::VirtualFileSystem::current (js);
1373- return js.resolvedPromise (js.alloc <FileSystemDirectoryHandle>(jsg::USVString (), vfs.getRoot (js)));
1389+ KJ_IF_SOME (vfs, workerd::VirtualFileSystem::tryGetCurrent (js)) {
1390+ return js.resolvedPromise (
1391+ js.alloc <FileSystemDirectoryHandle>(jsg::USVString (), vfs.getRoot (js)));
1392+ }
1393+
1394+ auto ex = js.domException (kj::str (" SecurityError" ), kj::str (" No current virtual file system" ));
1395+ return js.rejectedPromise <jsg::Ref<FileSystemDirectoryHandle>>(exception.wrap (js, kj::mv (ex)));
13741396}
13751397
13761398FileSystemDirectoryHandle::FileSystemDirectoryHandle (
0 commit comments