Skip to content

Commit

Permalink
feat: isResponseAvailability()
Browse files Browse the repository at this point in the history
Update isResponseAvailability.mjs
Update request.dev.js
  • Loading branch information
VirgilClyne committed Nov 1, 2024
1 parent 5e256ca commit 9c77a5f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/function/isResponseAvailability.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { log } from "@nsnanocat/util";
*/
export default function isResponseAvailability(response = {}) {
log("☑️ Determine Response Availability", "");
log(`statusCode: ${response.statusCode}`, `headers: ${JSON.stringify(response.headers)}`, "");
log(`statusCode: ${response.statusCode}`, `headers: ${JSON.stringify(response.headers, null, 2)}`, "");
const FORMAT = (response?.headers?.["Content-Type"] ?? response?.headers?.["content-type"])?.split(";")?.[0];
log("🚧 Determine Response Availability", `FORMAT: ${FORMAT}`, "");
let isAvailable = true;
Expand All @@ -23,7 +23,7 @@ export default function isResponseAvailability(response = {}) {
break;
case undefined:
case "": // 无内容
switch (response?.headers?.["content-length"] ?? response?.headers?.["content-length"]) {
switch (response?.headers?.["content-length"] ?? response?.headers?.["Content-Length"]) {
case undefined:
isAvailable = true;
break;
Expand Down
26 changes: 15 additions & 11 deletions src/request.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
switch (PATHs?.[0]) {
case "bangumi": // 番剧-web
switch (PATHs?.[1]) {
case "play": { // 番剧-播放页-web
case "play": {
// 番剧-播放页-web
const URLRegex = /ss(?<seasonId>[0-9]+)|ep(?<epId>[0-9]+)/;
({ seasonId: infoGroup.seasonId, epId: infoGroup.epId } = PATHs?.[2].match(URLRegex)?.groups);
infoGroup.seasonId = Number.parseInt(infoGroup.seasonId, 10) || infoGroup.seasonId;
Expand Down Expand Up @@ -380,7 +381,7 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
) {
case "Shadowrocket":
case "Quantumult X":
delete $request.policy;
$request.policy = undefined;
break;
}
break;
Expand Down Expand Up @@ -411,9 +412,8 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
}
if (!$response) {
// 无(构造)回复数据
switch (
$platform // 已有指定策略的请求,根据策略fetch
) {
switch ($platform) {
// 已有指定策略的请求,根据策略fetch
case "Shadowrocket":
case "Quantumult X":
if ($request.policy) $response = await fetch($request);
Expand All @@ -428,9 +428,9 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
})()
.catch(e => logError(e))
.finally(() => {
switch ($response) {
default: // 有构造回复数据,返回构造的回复数据
//log(`🚧 finally`, `echo $response: ${JSON.stringify($response, null, 2)}`, "");
switch (typeof $response) {
case "object": // 有构造回复数据,返回构造的回复数据
log("🚧 finally", `echo $response: ${JSON.stringify($response, null, 2)}`, "");
if ($response.headers?.["Content-Encoding"]) $response.headers["Content-Encoding"] = "identity";
if ($response.headers?.["content-encoding"]) $response.headers["content-encoding"] = "identity";
switch ($platform) {
Expand All @@ -446,10 +446,13 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
break;
}
break;
case undefined: // 无构造回复数据,发送修改的请求数据
//log(`🚧 finally`, `$request: ${JSON.stringify($request, null, 2)}`, "");
case "undefined": // 无构造回复数据,发送修改的请求数据
log("🚧 finally", `$request: ${JSON.stringify($request, null, 2)}`, "");
done($request);
break;
default:
logError(`不合法的 $response 类型: ${typeof $response}`, "");
break;
}
});

Expand Down Expand Up @@ -487,11 +490,12 @@ async function mutiFetch(request = {}, proxies = {}, locales = []) {
await Promise.allSettled(
locales.map(async locale => {
request.policy = proxies[locale];
if ($platform === "Quantumult X") request.body = request.bodyBytes;
//if ($platform === "Quantumult X") request.body = request.bodyBytes;
responses[locale] = await fetch(request);
}),
);
for (const locale in responses) {
log("🚧 mutiFetch", `locale: ${locale}`);
if (!isResponseAvailability(responses[locale])) delete responses[locale];
}
const availableLocales = Object.keys(responses);
Expand Down

0 comments on commit 9c77a5f

Please sign in to comment.