Files
SnowLuma/.pnpm-store/v11/files/6e/60892c0f731a17cf5f1946495cb5b2976cf55df4286b1ea67ceb8b702ddc09c4409bfed036d548b72f8b5a9a670463e4fd85f2aae6844f7a682d7346f464bd
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

68 lines
2.4 KiB
Plaintext

const require_path = require("./path.cjs");
//#region src/rewrite.ts
/** Compose multiple rewrite pairs into a single in/out rewrite. */
/** Compose multiple rewrite pairs into a single in/out rewrite. */
function composeRewrites(rewrites) {
return {
input: ({ url }) => {
for (const rewrite of rewrites) url = executeRewriteInput(rewrite, url);
return url;
},
output: ({ url }) => {
for (let i = rewrites.length - 1; i >= 0; i--) url = executeRewriteOutput(rewrites[i], url);
return url;
}
};
}
/** Create a rewrite pair that strips/adds a basepath on input/output. */
/** Create a rewrite pair that strips/adds a basepath on input/output. */
function rewriteBasepath(opts) {
const trimmedBasepath = require_path.trimPath(opts.basepath);
const normalizedBasepath = `/${trimmedBasepath}`;
const normalizedBasepathWithSlash = `${normalizedBasepath}/`;
const checkBasepath = opts.caseSensitive ? normalizedBasepath : normalizedBasepath.toLowerCase();
const checkBasepathWithSlash = opts.caseSensitive ? normalizedBasepathWithSlash : normalizedBasepathWithSlash.toLowerCase();
return {
input: ({ url }) => {
const pathname = opts.caseSensitive ? url.pathname : url.pathname.toLowerCase();
if (pathname === checkBasepath) url.pathname = "/";
else if (pathname.startsWith(checkBasepathWithSlash)) url.pathname = url.pathname.slice(normalizedBasepath.length);
return url;
},
output: ({ url }) => {
url.pathname = require_path.joinPaths([
"/",
trimmedBasepath,
url.pathname
]);
return url;
}
};
}
/** Execute a location input rewrite if provided. */
/** Execute a location input rewrite if provided. */
function executeRewriteInput(rewrite, url) {
const res = rewrite?.input?.({ url });
if (res) {
if (typeof res === "string") return new URL(res);
else if (res instanceof URL) return res;
}
return url;
}
/** Execute a location output rewrite if provided. */
/** Execute a location output rewrite if provided. */
function executeRewriteOutput(rewrite, url) {
const res = rewrite?.output?.({ url });
if (res) {
if (typeof res === "string") return new URL(res);
else if (res instanceof URL) return res;
}
return url;
}
//#endregion
exports.composeRewrites = composeRewrites;
exports.executeRewriteInput = executeRewriteInput;
exports.executeRewriteOutput = executeRewriteOutput;
exports.rewriteBasepath = rewriteBasepath;
//# sourceMappingURL=rewrite.cjs.map