@@ -136,8 +136,7 @@ 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 = JSG_REQUIRE_NONNULL (
140- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
139+ auto & vfs = workerd::VirtualFileSystem::current (js);
141140 KJ_SWITCH_ONEOF (pathOrFd) {
142141 KJ_CASE_ONEOF (path, FilePath) {
143142 NormalizedFilePath normalizedPath (kj::mv (path));
@@ -188,8 +187,7 @@ kj::Maybe<Stat> FileSystemModule::stat(
188187
189188void FileSystemModule::setLastModified (
190189 jsg::Lock& js, kj::OneOf<int , FilePath> pathOrFd, kj::Date lastModified, StatOptions options) {
191- auto & vfs = JSG_REQUIRE_NONNULL (
192- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
190+ auto & vfs = workerd::VirtualFileSystem::current (js);
193191 KJ_SWITCH_ONEOF (pathOrFd) {
194192 KJ_CASE_ONEOF (path, FilePath) {
195193 NormalizedFilePath normalizedPath (kj::mv (path));
@@ -249,8 +247,7 @@ void FileSystemModule::setLastModified(
249247}
250248
251249void FileSystemModule::truncate (jsg::Lock& js, kj::OneOf<int , FilePath> pathOrFd, uint32_t size) {
252- auto & vfs = JSG_REQUIRE_NONNULL (
253- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
250+ auto & vfs = workerd::VirtualFileSystem::current (js);
254251 KJ_SWITCH_ONEOF (pathOrFd) {
255252 KJ_CASE_ONEOF (path, FilePath) {
256253 NormalizedFilePath normalizedPath (kj::mv (path));
@@ -305,8 +302,7 @@ void FileSystemModule::truncate(jsg::Lock& js, kj::OneOf<int, FilePath> pathOrFd
305302}
306303
307304kj::String FileSystemModule::readLink (jsg::Lock& js, FilePath path, ReadLinkOptions options) {
308- auto & vfs = JSG_REQUIRE_NONNULL (
309- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
305+ auto & vfs = workerd::VirtualFileSystem::current (js);
310306 NormalizedFilePath normalizedPath (kj::mv (path));
311307 KJ_IF_SOME (node, vfs.resolve (js, normalizedPath, {.followLinks = false })) {
312308 KJ_SWITCH_ONEOF (node) {
@@ -340,8 +336,7 @@ kj::String FileSystemModule::readLink(jsg::Lock& js, FilePath path, ReadLinkOpti
340336
341337void FileSystemModule::link (jsg::Lock& js, FilePath from, FilePath to, LinkOptions options) {
342338 // The from argument is where we are creating the link, while the to is the target.
343- auto & vfs = JSG_REQUIRE_NONNULL (
344- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
339+ auto & vfs = workerd::VirtualFileSystem::current (js);
345340 NormalizedFilePath normalizedFrom (kj::mv (from));
346341 NormalizedFilePath normalizedTo (kj::mv (to));
347342
@@ -412,8 +407,7 @@ void FileSystemModule::link(jsg::Lock& js, FilePath from, FilePath to, LinkOptio
412407}
413408
414409void FileSystemModule::unlink (jsg::Lock& js, FilePath path) {
415- auto & vfs = JSG_REQUIRE_NONNULL (
416- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
410+ auto & vfs = workerd::VirtualFileSystem::current (js);
417411 NormalizedFilePath normalizedPath (kj::mv (path));
418412 const jsg::Url& url = normalizedPath;
419413 auto relative = url.getRelative ();
@@ -453,8 +447,7 @@ void FileSystemModule::unlink(jsg::Lock& js, FilePath path) {
453447}
454448
455449int FileSystemModule::open (jsg::Lock& js, FilePath path, OpenOptions options) {
456- auto & vfs = JSG_REQUIRE_NONNULL (
457- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
450+ auto & vfs = workerd::VirtualFileSystem::current (js);
458451 NormalizedFilePath normalizedPath (kj::mv (path));
459452 KJ_SWITCH_ONEOF (vfs.openFd (js, normalizedPath,
460453 workerd::VirtualFileSystem::OpenOptions{
@@ -475,15 +468,13 @@ int FileSystemModule::open(jsg::Lock& js, FilePath path, OpenOptions options) {
475468}
476469
477470void FileSystemModule::close (jsg::Lock& js, int fd) {
478- auto & vfs = JSG_REQUIRE_NONNULL (
479- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
471+ auto & vfs = workerd::VirtualFileSystem::current (js);
480472 vfs.closeFd (js, fd);
481473}
482474
483475uint32_t FileSystemModule::write (
484476 jsg::Lock& js, int fd, kj::Array<jsg::BufferSource> data, WriteOptions options) {
485- auto & vfs = JSG_REQUIRE_NONNULL (
486- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
477+ auto & vfs = workerd::VirtualFileSystem::current (js);
487478
488479 KJ_IF_SOME (opened, vfs.tryGetFd (js, fd)) {
489480 static const auto getPosition = [](jsg::Lock& js, auto & opened, auto & file,
@@ -540,8 +531,7 @@ uint32_t FileSystemModule::write(
540531
541532uint32_t FileSystemModule::read (
542533 jsg::Lock& js, int fd, kj::Array<jsg::BufferSource> data, WriteOptions options) {
543- auto & vfs = JSG_REQUIRE_NONNULL (
544- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
534+ auto & vfs = workerd::VirtualFileSystem::current (js);
545535 KJ_IF_SOME (opened, vfs.tryGetFd (js, fd)) {
546536 if (!opened->read ) {
547537 throwUVException (js, UV_EBADF, " read" _kj);
@@ -583,8 +573,7 @@ uint32_t FileSystemModule::read(
583573}
584574
585575jsg::BufferSource FileSystemModule::readAll (jsg::Lock& js, kj::OneOf<int , FilePath> pathOrFd) {
586- auto & vfs = JSG_REQUIRE_NONNULL (
587- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
576+ auto & vfs = workerd::VirtualFileSystem::current (js);
588577 KJ_SWITCH_ONEOF (pathOrFd) {
589578 KJ_CASE_ONEOF (path, FilePath) {
590579 NormalizedFilePath normalized (kj::mv (path));
@@ -653,8 +642,7 @@ uint32_t FileSystemModule::writeAll(jsg::Lock& js,
653642 kj::OneOf<int , FilePath> pathOrFd,
654643 jsg::BufferSource data,
655644 WriteAllOptions options) {
656- auto & vfs = JSG_REQUIRE_NONNULL (
657- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
645+ auto & vfs = workerd::VirtualFileSystem::current (js);
658646
659647 if (data.size () > kMax ) {
660648 throwUVException (js, UV_EFBIG, " writeAll" _kj);
@@ -814,8 +802,7 @@ uint32_t FileSystemModule::writeAll(jsg::Lock& js,
814802void FileSystemModule::renameOrCopy (
815803 jsg::Lock& js, FilePath src, FilePath dest, RenameOrCopyOptions options) {
816804 // The source must exist, the destination must not.
817- auto & vfs = JSG_REQUIRE_NONNULL (
818- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
805+ auto & vfs = workerd::VirtualFileSystem::current (js);
819806 NormalizedFilePath normalizedSrc (kj::mv (src));
820807 NormalizedFilePath normalizedDest (kj::mv (dest));
821808
@@ -931,8 +918,7 @@ void FileSystemModule::renameOrCopy(
931918
932919jsg::Optional<kj::String> FileSystemModule::mkdir (
933920 jsg::Lock& js, FilePath path, MkdirOptions options) {
934- auto & vfs = JSG_REQUIRE_NONNULL (
935- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
921+ auto & vfs = workerd::VirtualFileSystem::current (js);
936922 NormalizedFilePath normalizedPath (kj::mv (path));
937923 const jsg::Url& url = normalizedPath;
938924
@@ -1077,8 +1063,7 @@ jsg::Optional<kj::String> FileSystemModule::mkdir(
10771063
10781064void FileSystemModule::rm (jsg::Lock& js, FilePath path, RmOptions options) {
10791065 // TODO(node-fs): Implement the force option.
1080- auto & vfs = JSG_REQUIRE_NONNULL (
1081- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
1066+ auto & vfs = workerd::VirtualFileSystem::current (js);
10821067 NormalizedFilePath normalizedPath (kj::mv (path));
10831068 const jsg::Url& url = normalizedPath;
10841069 auto relative = url.getRelative ();
@@ -1193,8 +1178,7 @@ void readdirImpl(jsg::Lock& js,
11931178
11941179kj::Array<FileSystemModule::DirEntHandle> FileSystemModule::readdir (
11951180 jsg::Lock& js, FilePath path, ReadDirOptions options) {
1196- auto & vfs = JSG_REQUIRE_NONNULL (
1197- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
1181+ auto & vfs = workerd::VirtualFileSystem::current (js);
11981182 NormalizedFilePath normalizedPath (kj::mv (path));
11991183
12001184 KJ_IF_SOME (node, vfs.resolve (js, normalizedPath, {.followLinks = false })) {
@@ -1221,8 +1205,7 @@ kj::Array<FileSystemModule::DirEntHandle> FileSystemModule::readdir(
12211205}
12221206
12231207jsg::Ref<FileFdHandle> FileFdHandle::constructor (jsg::Lock& js, int fd) {
1224- auto & vfs = JSG_REQUIRE_NONNULL (
1225- workerd::VirtualFileSystem::tryGetCurrent (js), Error, " No current virtual file system" _kj);
1208+ auto & vfs = workerd::VirtualFileSystem::current (js);
12261209 return js.alloc <FileFdHandle>(js, vfs, fd);
12271210}
12281211
@@ -1386,13 +1369,8 @@ jsg::Ref<jsg::DOMException> fsErrorToDomException(jsg::Lock& js, workerd::FsErro
13861369
13871370jsg::Promise<jsg::Ref<FileSystemDirectoryHandle>> StorageManager::getDirectory (
13881371 jsg::Lock& js, const jsg::TypeHandler<jsg::Ref<jsg::DOMException>>& exception) {
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)));
1372+ auto & vfs = workerd::VirtualFileSystem::current (js);
1373+ return js.resolvedPromise (js.alloc <FileSystemDirectoryHandle>(jsg::USVString (), vfs.getRoot (js)));
13961374}
13971375
13981376FileSystemDirectoryHandle::FileSystemDirectoryHandle (
0 commit comments