@ricsam/linkfs
Version:
Rewrites filesystem paths
653 lines (648 loc) • 24.7 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __moduleCache = /* @__PURE__ */ new WeakMap;
var __toCommonJS = (from) => {
var entry = __moduleCache.get(from), desc;
if (entry)
return entry;
entry = __defProp({}, "__esModule", { value: true });
if (from && typeof from === "object" || typeof from === "function")
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
}));
__moduleCache.set(from, entry);
return entry;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {
get: all[name],
enumerable: true,
configurable: true,
set: (newValue) => all[name] = () => newValue
});
};
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
// ../../../../../bun-vfs$$/node_modules/url/index.js
var exports_url = {};
__export(exports_url, {
resolveObject: () => at,
resolve: () => rt,
parse: () => I2,
format: () => et,
default: () => ot,
Url: () => m,
URLSearchParams: () => X2,
URL: () => J2
});
function F(s) {
return typeof s == "string";
}
function K2(s) {
return typeof s == "object" && s !== null;
}
function w(s) {
return s === null;
}
function Y2(s) {
return s == null;
}
function m() {
this.protocol = null, this.slashes = null, this.auth = null, this.host = null, this.port = null, this.hostname = null, this.hash = null, this.search = null, this.query = null, this.pathname = null, this.path = null, this.href = null;
}
function I2(s, r, t) {
if (s && K2(s) && s instanceof m)
return s;
var a = new m;
return a.parse(s, r, t), a;
}
function et(s) {
return F(s) && (s = I2(s)), s instanceof m ? s.format() : m.prototype.format.call(s);
}
function rt(s, r) {
return I2(s, false, true).resolve(r);
}
function at(s, r) {
return s ? I2(s, false, true).resolveObject(r) : r;
}
var J2, X2, S2, k2, H2, Q2, E2, N2, M, D2, tt = 255, G2, st, ht, Z2, j2, B2, ot;
var init_url = __esm(() => {
({ URL: J2, URLSearchParams: X2 } = globalThis);
S2 = /^([a-z0-9.+-]+:)/i;
k2 = /:[0-9]*$/;
H2 = /^(\/\/?(?!\/)[^\?\s]*)(\?[^\s]*)?$/;
Q2 = ["<", ">", '"', "`", " ", "\r", `
`, "\t"];
E2 = ["{", "}", "|", "\\", "^", "`"].concat(Q2);
N2 = ["'"].concat(E2);
M = ["%", "/", "?", ";", "#"].concat(N2);
D2 = ["/", "?", "#"];
G2 = /^[+a-z0-9A-Z_-]{0,63}$/;
st = /^([+a-z0-9A-Z_-]{0,63})(.*)$/;
ht = { javascript: true, "javascript:": true };
Z2 = { javascript: true, "javascript:": true };
j2 = { http: true, https: true, ftp: true, gopher: true, file: true, "http:": true, "https:": true, "ftp:": true, "gopher:": true, "file:": true };
B2 = { parse(s) {
var r = decodeURIComponent;
return (s + "").replace(/\+/g, " ").split("&").filter(Boolean).reduce(function(t, a, o) {
var l = a.split("="), f = r(l[0] || ""), h2 = r(l[1] || ""), g2 = t[f];
return t[f] = g2 === undefined ? h2 : [].concat(g2, h2), t;
}, {});
}, stringify(s) {
var r = encodeURIComponent;
return Object.keys(s || {}).reduce(function(t, a) {
return [].concat(s[a]).forEach(function(o) {
t.push(r(a) + "=" + r(o));
}), t;
}, []).join("&").replace(/\s/g, "+");
} };
m.prototype.parse = function(s, r, t) {
if (!F(s))
throw new TypeError("Parameter 'url' must be a string, not " + typeof s);
var a = s.indexOf("?"), o = a !== -1 && a < s.indexOf("#") ? "?" : "#", l = s.split(o), f = /\\/g;
l[0] = l[0].replace(f, "/"), s = l.join(o);
var h2 = s;
if (h2 = h2.trim(), !t && s.split("#").length === 1) {
var g2 = H2.exec(h2);
if (g2)
return this.path = h2, this.href = h2, this.pathname = g2[1], g2[2] ? (this.search = g2[2], r ? this.query = B2.parse(this.search.substr(1)) : this.query = this.search.substr(1)) : r && (this.search = "", this.query = {}), this;
}
var c = S2.exec(h2);
if (c) {
c = c[0];
var v = c.toLowerCase();
this.protocol = v, h2 = h2.substr(c.length);
}
if (t || c || h2.match(/^\/\/[^@\/]+@[^@\/]+/)) {
var U2 = h2.substr(0, 2) === "//";
U2 && !(c && Z2[c]) && (h2 = h2.substr(2), this.slashes = true);
}
if (!Z2[c] && (U2 || c && !j2[c])) {
for (var u2 = -1, n = 0;n < D2.length; n++) {
var b = h2.indexOf(D2[n]);
b !== -1 && (u2 === -1 || b < u2) && (u2 = b);
}
var R2, p;
u2 === -1 ? p = h2.lastIndexOf("@") : p = h2.lastIndexOf("@", u2), p !== -1 && (R2 = h2.slice(0, p), h2 = h2.slice(p + 1), this.auth = decodeURIComponent(R2)), u2 = -1;
for (var n = 0;n < M.length; n++) {
var b = h2.indexOf(M[n]);
b !== -1 && (u2 === -1 || b < u2) && (u2 = b);
}
u2 === -1 && (u2 = h2.length), this.host = h2.slice(0, u2), h2 = h2.slice(u2), this.parseHost(), this.hostname = this.hostname || "";
var C = this.hostname[0] === "[" && this.hostname[this.hostname.length - 1] === "]";
if (!C)
for (var e = this.hostname.split(/\./), n = 0, i = e.length;n < i; n++) {
var O2 = e[n];
if (!!O2 && !O2.match(G2)) {
for (var y2 = "", d = 0, L2 = O2.length;d < L2; d++)
O2.charCodeAt(d) > 127 ? y2 += "x" : y2 += O2[d];
if (!y2.match(G2)) {
var x2 = e.slice(0, n), q = e.slice(n + 1), A = O2.match(st);
A && (x2.push(A[1]), q.unshift(A[2])), q.length && (h2 = "/" + q.join(".") + h2), this.hostname = x2.join(".");
break;
}
}
}
this.hostname.length > tt ? this.hostname = "" : this.hostname = this.hostname.toLowerCase(), C || (this.hostname = new J2(`https://${this.hostname}`).hostname);
var $2 = this.port ? ":" + this.port : "", V2 = this.hostname || "";
this.host = V2 + $2, this.href += this.host, C && (this.hostname = this.hostname.substr(1, this.hostname.length - 2), h2[0] !== "/" && (h2 = "/" + h2));
}
if (!ht[v])
for (var n = 0, i = N2.length;n < i; n++) {
var P2 = N2[n];
if (h2.indexOf(P2) !== -1) {
var z = encodeURIComponent(P2);
z === P2 && (z = escape(P2)), h2 = h2.split(P2).join(z);
}
}
var T2 = h2.indexOf("#");
T2 !== -1 && (this.hash = h2.substr(T2), h2 = h2.slice(0, T2));
var _2 = h2.indexOf("?");
if (_2 !== -1 ? (this.search = h2.substr(_2), this.query = h2.substr(_2 + 1), r && (this.query = B2.parse(this.query)), h2 = h2.slice(0, _2)) : r && (this.search = "", this.query = {}), h2 && (this.pathname = h2), j2[v] && this.hostname && !this.pathname && (this.pathname = "/"), this.pathname || this.search) {
var $2 = this.pathname || "", W = this.search || "";
this.path = $2 + W;
}
return this.href = this.format(), this;
};
m.prototype.format = function() {
var s = this.auth || "";
s && (s = encodeURIComponent(s), s = s.replace(/%3A/i, ":"), s += "@");
var r = this.protocol || "", t = this.pathname || "", a = this.hash || "", o = false, l = "";
this.host ? o = s + this.host : this.hostname && (o = s + (this.hostname.indexOf(":") === -1 ? this.hostname : "[" + this.hostname + "]"), this.port && (o += ":" + this.port)), this.query && K2(this.query) && Object.keys(this.query).length && (l = B2.stringify(this.query));
var f = this.search || l && "?" + l || "";
return r && r.substr(-1) !== ":" && (r += ":"), this.slashes || (!r || j2[r]) && o !== false ? (o = "//" + (o || ""), t && t.charAt(0) !== "/" && (t = "/" + t)) : o || (o = ""), a && a.charAt(0) !== "#" && (a = "#" + a), f && f.charAt(0) !== "?" && (f = "?" + f), t = t.replace(/[?#]/g, function(h2) {
return encodeURIComponent(h2);
}), f = f.replace("#", "%23"), r + o + t + f + a;
};
m.prototype.resolve = function(s) {
return this.resolveObject(I2(s, false, true)).format();
};
m.prototype.resolveObject = function(s) {
if (F(s)) {
var r = new m;
r.parse(s, false, true), s = r;
}
for (var t = new m, a = Object.keys(this), o = 0;o < a.length; o++) {
var l = a[o];
t[l] = this[l];
}
if (t.hash = s.hash, s.href === "")
return t.href = t.format(), t;
if (s.slashes && !s.protocol) {
for (var f = Object.keys(s), h2 = 0;h2 < f.length; h2++) {
var g2 = f[h2];
g2 !== "protocol" && (t[g2] = s[g2]);
}
return j2[t.protocol] && t.hostname && !t.pathname && (t.path = t.pathname = "/"), t.href = t.format(), t;
}
if (s.protocol && s.protocol !== t.protocol) {
if (!j2[s.protocol]) {
for (var c = Object.keys(s), v = 0;v < c.length; v++) {
var U2 = c[v];
t[U2] = s[U2];
}
return t.href = t.format(), t;
}
if (t.protocol = s.protocol, !s.host && !Z2[s.protocol]) {
for (var i = (s.pathname || "").split("/");i.length && !(s.host = i.shift()); )
;
s.host || (s.host = ""), s.hostname || (s.hostname = ""), i[0] !== "" && i.unshift(""), i.length < 2 && i.unshift(""), t.pathname = i.join("/");
} else
t.pathname = s.pathname;
if (t.search = s.search, t.query = s.query, t.host = s.host || "", t.auth = s.auth, t.hostname = s.hostname || s.host, t.port = s.port, t.pathname || t.search) {
var u2 = t.pathname || "", n = t.search || "";
t.path = u2 + n;
}
return t.slashes = t.slashes || s.slashes, t.href = t.format(), t;
}
var b = t.pathname && t.pathname.charAt(0) === "/", R2 = s.host || s.pathname && s.pathname.charAt(0) === "/", p = R2 || b || t.host && s.pathname, C = p, e = t.pathname && t.pathname.split("/") || [], i = s.pathname && s.pathname.split("/") || [], O2 = t.protocol && !j2[t.protocol];
if (O2 && (t.hostname = "", t.port = null, t.host && (e[0] === "" ? e[0] = t.host : e.unshift(t.host)), t.host = "", s.protocol && (s.hostname = null, s.port = null, s.host && (i[0] === "" ? i[0] = s.host : i.unshift(s.host)), s.host = null), p = p && (i[0] === "" || e[0] === "")), R2)
t.host = s.host || s.host === "" ? s.host : t.host, t.hostname = s.hostname || s.hostname === "" ? s.hostname : t.hostname, t.search = s.search, t.query = s.query, e = i;
else if (i.length)
e || (e = []), e.pop(), e = e.concat(i), t.search = s.search, t.query = s.query;
else if (!Y2(s.search)) {
if (O2) {
t.hostname = t.host = e.shift();
var y2 = t.host && t.host.indexOf("@") > 0 ? t.host.split("@") : false;
y2 && (t.auth = y2.shift(), t.host = t.hostname = y2.shift());
}
return t.search = s.search, t.query = s.query, (!w(t.pathname) || !w(t.search)) && (t.path = (t.pathname ? t.pathname : "") + (t.search ? t.search : "")), t.href = t.format(), t;
}
if (!e.length)
return t.pathname = null, t.search ? t.path = "/" + t.search : t.path = null, t.href = t.format(), t;
for (var d = e.slice(-1)[0], L2 = (t.host || s.host || e.length > 1) && (d === "." || d === "..") || d === "", x2 = 0, q = e.length;q >= 0; q--)
d = e[q], d === "." ? e.splice(q, 1) : d === ".." ? (e.splice(q, 1), x2++) : x2 && (e.splice(q, 1), x2--);
if (!p && !C)
for (;x2--; x2)
e.unshift("..");
p && e[0] !== "" && (!e[0] || e[0].charAt(0) !== "/") && e.unshift(""), L2 && e.join("/").substr(-1) !== "/" && e.push("");
var A = e[0] === "" || e[0] && e[0].charAt(0) === "/";
if (O2) {
t.hostname = t.host = A ? "" : e.length ? e.shift() : "";
var y2 = t.host && t.host.indexOf("@") > 0 ? t.host.split("@") : false;
y2 && (t.auth = y2.shift(), t.host = t.hostname = y2.shift());
}
return p = p || t.host && e.length, p && !A && e.unshift(""), e.length ? t.pathname = e.join("/") : (t.pathname = null, t.path = null), (!w(t.pathname) || !w(t.search)) && (t.path = (t.pathname ? t.pathname : "") + (t.search ? t.search : "")), t.auth = s.auth || t.auth, t.slashes = t.slashes || s.slashes, t.href = t.format(), t;
};
m.prototype.parseHost = function() {
var s = this.host, r = k2.exec(s);
r && (r = r[0], r !== ":" && (this.port = r.substr(1)), s = s.substr(0, s.length - r.length)), s && (this.hostname = s);
};
ot = { parse: I2, resolve: rt, resolveObject: at, format: et, Url: m, URL: J2, URLSearchParams: X2 };
});
// ../../../../../bun-vfs$$/node_modules/path/index.js
var L = Object.create;
var h = Object.defineProperty;
var D = Object.getOwnPropertyDescriptor;
var T = Object.getOwnPropertyNames;
var _ = Object.getPrototypeOf;
var E = Object.prototype.hasOwnProperty;
var R = (s, e) => () => (e || s((e = { exports: {} }).exports, e), e.exports);
var N = (s, e, r, t) => {
if (e && typeof e == "object" || typeof e == "function")
for (let i of T(e))
!E.call(s, i) && i !== r && h(s, i, { get: () => e[i], enumerable: !(t = D(e, i)) || t.enumerable });
return s;
};
var j = (s, e, r) => (r = s != null ? L(_(s)) : {}, N(e || !s || !s.__esModule ? h(r, "default", { value: s, enumerable: true }) : r, s));
var k = R((W, w) => {
function v(s) {
if (typeof s != "string")
throw new TypeError("Path must be a string. Received " + JSON.stringify(s));
}
function C(s, e) {
for (var r = "", t = 0, i = -1, a = 0, n, l = 0;l <= s.length; ++l) {
if (l < s.length)
n = s.charCodeAt(l);
else {
if (n === 47)
break;
n = 47;
}
if (n === 47) {
if (!(i === l - 1 || a === 1))
if (i !== l - 1 && a === 2) {
if (r.length < 2 || t !== 2 || r.charCodeAt(r.length - 1) !== 46 || r.charCodeAt(r.length - 2) !== 46) {
if (r.length > 2) {
var f = r.lastIndexOf("/");
if (f !== r.length - 1) {
f === -1 ? (r = "", t = 0) : (r = r.slice(0, f), t = r.length - 1 - r.lastIndexOf("/")), i = l, a = 0;
continue;
}
} else if (r.length === 2 || r.length === 1) {
r = "", t = 0, i = l, a = 0;
continue;
}
}
e && (r.length > 0 ? r += "/.." : r = "..", t = 2);
} else
r.length > 0 ? r += "/" + s.slice(i + 1, l) : r = s.slice(i + 1, l), t = l - i - 1;
i = l, a = 0;
} else
n === 46 && a !== -1 ? ++a : a = -1;
}
return r;
}
function F(s, e) {
var r = e.dir || e.root, t = e.base || (e.name || "") + (e.ext || "");
return r ? r === e.root ? r + t : r + s + t : t;
}
var m = { resolve: function() {
for (var e = "", r = false, t, i = arguments.length - 1;i >= -1 && !r; i--) {
var a;
i >= 0 ? a = arguments[i] : (t === undefined && (t = process.cwd()), a = t), v(a), a.length !== 0 && (e = a + "/" + e, r = a.charCodeAt(0) === 47);
}
return e = C(e, !r), r ? e.length > 0 ? "/" + e : "/" : e.length > 0 ? e : ".";
}, normalize: function(e) {
if (v(e), e.length === 0)
return ".";
var r = e.charCodeAt(0) === 47, t = e.charCodeAt(e.length - 1) === 47;
return e = C(e, !r), e.length === 0 && !r && (e = "."), e.length > 0 && t && (e += "/"), r ? "/" + e : e;
}, isAbsolute: function(e) {
return v(e), e.length > 0 && e.charCodeAt(0) === 47;
}, join: function() {
if (arguments.length === 0)
return ".";
for (var e, r = 0;r < arguments.length; ++r) {
var t = arguments[r];
v(t), t.length > 0 && (e === undefined ? e = t : e += "/" + t);
}
return e === undefined ? "." : m.normalize(e);
}, relative: function(e, r) {
if (v(e), v(r), e === r || (e = m.resolve(e), r = m.resolve(r), e === r))
return "";
for (var t = 1;t < e.length && e.charCodeAt(t) === 47; ++t)
;
for (var i = e.length, a = i - t, n = 1;n < r.length && r.charCodeAt(n) === 47; ++n)
;
for (var l = r.length, f = l - n, c = a < f ? a : f, d = -1, o = 0;o <= c; ++o) {
if (o === c) {
if (f > c) {
if (r.charCodeAt(n + o) === 47)
return r.slice(n + o + 1);
if (o === 0)
return r.slice(n + o);
} else
a > c && (e.charCodeAt(t + o) === 47 ? d = o : o === 0 && (d = 0));
break;
}
var A = e.charCodeAt(t + o), z = r.charCodeAt(n + o);
if (A !== z)
break;
A === 47 && (d = o);
}
var b = "";
for (o = t + d + 1;o <= i; ++o)
(o === i || e.charCodeAt(o) === 47) && (b.length === 0 ? b += ".." : b += "/..");
return b.length > 0 ? b + r.slice(n + d) : (n += d, r.charCodeAt(n) === 47 && ++n, r.slice(n));
}, _makeLong: function(e) {
return e;
}, dirname: function(e) {
if (v(e), e.length === 0)
return ".";
for (var r = e.charCodeAt(0), t = r === 47, i = -1, a = true, n = e.length - 1;n >= 1; --n)
if (r = e.charCodeAt(n), r === 47) {
if (!a) {
i = n;
break;
}
} else
a = false;
return i === -1 ? t ? "/" : "." : t && i === 1 ? "//" : e.slice(0, i);
}, basename: function(e, r) {
if (r !== undefined && typeof r != "string")
throw new TypeError('"ext" argument must be a string');
v(e);
var t = 0, i = -1, a = true, n;
if (r !== undefined && r.length > 0 && r.length <= e.length) {
if (r.length === e.length && r === e)
return "";
var l = r.length - 1, f = -1;
for (n = e.length - 1;n >= 0; --n) {
var c = e.charCodeAt(n);
if (c === 47) {
if (!a) {
t = n + 1;
break;
}
} else
f === -1 && (a = false, f = n + 1), l >= 0 && (c === r.charCodeAt(l) ? --l === -1 && (i = n) : (l = -1, i = f));
}
return t === i ? i = f : i === -1 && (i = e.length), e.slice(t, i);
} else {
for (n = e.length - 1;n >= 0; --n)
if (e.charCodeAt(n) === 47) {
if (!a) {
t = n + 1;
break;
}
} else
i === -1 && (a = false, i = n + 1);
return i === -1 ? "" : e.slice(t, i);
}
}, extname: function(e) {
v(e);
for (var r = -1, t = 0, i = -1, a = true, n = 0, l = e.length - 1;l >= 0; --l) {
var f = e.charCodeAt(l);
if (f === 47) {
if (!a) {
t = l + 1;
break;
}
continue;
}
i === -1 && (a = false, i = l + 1), f === 46 ? r === -1 ? r = l : n !== 1 && (n = 1) : r !== -1 && (n = -1);
}
return r === -1 || i === -1 || n === 0 || n === 1 && r === i - 1 && r === t + 1 ? "" : e.slice(r, i);
}, format: function(e) {
if (e === null || typeof e != "object")
throw new TypeError('The "pathObject" argument must be of type Object. Received type ' + typeof e);
return F("/", e);
}, parse: function(e) {
v(e);
var r = { root: "", dir: "", base: "", ext: "", name: "" };
if (e.length === 0)
return r;
var t = e.charCodeAt(0), i = t === 47, a;
i ? (r.root = "/", a = 1) : a = 0;
for (var n = -1, l = 0, f = -1, c = true, d = e.length - 1, o = 0;d >= a; --d) {
if (t = e.charCodeAt(d), t === 47) {
if (!c) {
l = d + 1;
break;
}
continue;
}
f === -1 && (c = false, f = d + 1), t === 46 ? n === -1 ? n = d : o !== 1 && (o = 1) : n !== -1 && (o = -1);
}
return n === -1 || f === -1 || o === 0 || o === 1 && n === f - 1 && n === l + 1 ? f !== -1 && (l === 0 && i ? r.base = r.name = e.slice(1, f) : r.base = r.name = e.slice(l, f)) : (l === 0 && i ? (r.name = e.slice(1, n), r.base = e.slice(1, f)) : (r.name = e.slice(l, n), r.base = e.slice(l, f)), r.ext = e.slice(n, f)), l > 0 ? r.dir = e.slice(0, l - 1) : i && (r.dir = "/"), r;
}, sep: "/", delimiter: ":", win32: null, posix: null };
m.posix = m;
w.exports = m;
});
var x = j(k());
var u = x;
var J = x;
var P = function(s) {
return s;
};
var S = function() {
throw new Error("Not implemented");
};
u.parse ??= S;
J.parse ??= S;
var g = { resolve: u.resolve.bind(u), normalize: u.normalize.bind(u), isAbsolute: u.isAbsolute.bind(u), join: u.join.bind(u), relative: u.relative.bind(u), toNamespacedPath: P, dirname: u.dirname.bind(u), basename: u.basename.bind(u), extname: u.extname.bind(u), format: u.format.bind(u), parse: u.parse.bind(u), sep: "/", delimiter: ":", win32: undefined, posix: undefined, _makeLong: P };
var y = { sep: "\\", delimiter: ";", win32: undefined, ...g, posix: g };
g.win32 = y.win32 = y;
g.posix = g;
var { resolve: B, normalize: G, isAbsolute: H, join: K, relative: Q, toNamespacedPath: U, dirname: V, basename: X, extname: Y, format: Z, parse: $, sep: I, delimiter: O } = g;
// src/args.ts
var staticProps = [
"constants",
"F_OK",
"R_OK",
"W_OK",
"X_OK",
"Stats",
"StatFs",
"Dirent",
"Dir",
"ReadStream",
"WriteStream",
"FSWatcher",
"StatWatcher",
"FileHandle"
];
var rewritableMethods = [
"accessSync",
"appendFileSync",
"chmodSync",
"chownSync",
"copyFileSync",
"cpSync",
"existsSync",
"lchmodSync",
"lchownSync",
"linkSync",
"lstatSync",
"mkdirSync",
"mkdtempSync",
"openSync",
"opendirSync",
"readdirSync",
"readFileSync",
"readlinkSync",
"realpathSync",
"renameSync",
"rmdirSync",
"rmSync",
"statSync",
"statfsSync",
"symlinkSync",
"truncateSync",
"unlinkSync",
"utimesSync",
"writeFileSync",
"access",
"appendFile",
"chmod",
"chown",
"copyFile",
"cp",
"createReadStream",
"createWriteStream",
"exists",
"lchmod",
"lchown",
"lutimes",
"link",
"lstat",
"mkdir",
"mkdtemp",
"open",
"opendir",
"openAsBlob",
"readdir",
"readFile",
"readlink",
"realpath",
"rename",
"rmdir",
"rm",
"stat",
"statfs",
"symlink",
"truncate",
"unlink",
"unwatchFile",
"utimes",
"watch",
"watchFile",
"writeFile"
];
var proxyableMethods = [
"ftruncateSync",
"fchownSync",
"fchmodSync",
"fstatSync",
"closeSync",
"futimesSync",
"fsyncSync",
"writeSync",
"readSync",
"readvSync",
"writevSync",
"fdatasyncSync",
"ftruncate",
"fchown",
"fchmod",
"fstat",
"close",
"futimes",
"fsync",
"write",
"writev",
"read",
"readv",
"fdatasync"
];
var multiArgMethods = {
rename: [0, 1],
renameSync: [0, 1],
copyFile: [0, 1],
copyFileSync: [0, 1],
link: [0, 1],
linkSync: [0, 1],
symlink: [0, 1],
symlinkSync: [0, 1],
cp: [0, 1],
cpSync: [0, 1]
};
// src/index.ts
function link(fs, rewrites) {
if (!(rewrites instanceof Array))
throw TypeError("rewrites must be a list of 2-tuples");
if (typeof rewrites[0] === "string")
rewrites = [rewrites];
const rews = [];
for (const [from, to] of rewrites) {
rews.push([B(from), B(to)]);
}
const realFs = fs;
let lfs = {};
for (const prop of staticProps)
lfs[prop] = realFs[prop];
const rewritePath = (realFs2, newFs, method, indicesToRewrite = [0]) => {
const func = realFs2[method];
if (typeof func !== "function")
return;
newFs[method] = (...args) => {
indicesToRewrite.forEach((index) => {
const path = args[index];
if (typeof path !== "string" && !Buffer.isBuffer(path)) {
if (!(init_url(), __toCommonJS(exports_url)).URL || !(path instanceof (init_url(), __toCommonJS(exports_url)).URL))
return func.apply(realFs2, args);
}
let filename = B(String(path));
for (const [from, to] of rews) {
if (filename.startsWith(from)) {
const rootRegex = /(?:^[a-zA-Z]:\\$)|(?:^\/$)/;
const isRoot = from.match(rootRegex);
const baseRegex = "^(" + from.replace(/\\/g, "\\\\") + ")";
if (isRoot) {
const regex = new RegExp(baseRegex);
filename = filename.replace(regex, () => to + I);
} else {
const regex = new RegExp(baseRegex + "(\\\\|/|$)");
filename = filename.replace(regex, (match, p1, p2) => to + p2);
}
}
}
args[index] = filename;
});
return func.apply(realFs2, args);
};
};
for (const method of rewritableMethods) {
rewritePath(realFs, lfs, method, multiArgMethods[method] ?? [0]);
}
if (realFs.promises) {
lfs.promises = {};
for (const method of rewritableMethods) {
rewritePath(realFs.promises, lfs.promises, method, multiArgMethods[method] ?? [0]);
}
}
for (const method of proxyableMethods) {
const func = realFs[method];
if (typeof func !== "function")
continue;
lfs[method] = func.bind(realFs);
}
return lfs;
}
export {
link
};