Skip to content

Commit

Permalink
Fix FsVirtual on Windows due to path.resolve converting "/" into "C:\".
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Sep 5, 2024
1 parent d66d6b8 commit 1e6a84a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions webworker/fs-virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const dirent = (path, directory) => {
};
};

const normalize = (path) => path.replace(/^[A-Za-z]:/u, "").replaceAll("\\", "/");

/* eslint-disable no-param-reassign */

class FsVirtual {
constructor(files) {

Expand All @@ -26,13 +30,15 @@ class FsVirtual {
this.promises = {};

this.promises.access = (path) => {
path = normalize(path);
if (this.files.has(path)) {
return Promise.resolve();
}
return Promise.reject(new Error(`fs-virtual:promises.access(${path})`));
};

this.promises.readFile = (path) => {
path = normalize(path);
const content = this.files.get(path);
if (content) {
return Promise.resolve(content);
Expand All @@ -41,31 +47,36 @@ class FsVirtual {
};

this.promises.stat = (path) => {
path = normalize(path);
if (this.files.has(path)) {
return Promise.resolve(dirent(path));
}
return Promise.reject(new Error(`fs-virtual:promises.stat(${path})`));
};

this.promises.writeFile = (path, data) => {
path = normalize(path);
this.files.set(path, data);
};

this.access = (path, mode, callback) => {
path = normalize(path);
if (this.files.has(path)) {
return (callback || mode)();
}
return (callback || mode)(new Error(`fs-virtual:access(${path})`));
};

this.lstat = (path, callback) => {
path = normalize(path);
if (this.files.has(path)) {
return callback(null, dirent(path, false));
}
return callback(null, dirent(path, true));
};

this.readdir = (path, options, callback) => {
path = normalize(path);
const names = [];
for (const file of this.files.keys()) {
if (file.startsWith(path)) {
Expand All @@ -79,6 +90,7 @@ class FsVirtual {
};

this.readFile = (path, options, callback) => {
path = normalize(path);
const content = this.files.get(path);
if (content) {
return callback(null, content);
Expand Down

0 comments on commit 1e6a84a

Please sign in to comment.