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
Update request.js
  • Loading branch information
VirgilClyne committed Nov 1, 2024
1 parent 4e298b9 commit 7335ee9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 120 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
27 changes: 15 additions & 12 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,15 +446,17 @@ 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;
}
});

/***************** Function *****************/

/**
* Fetch
* @author VirgilClyne
Expand Down Expand Up @@ -487,11 +489,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 = undefined;
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
117 changes: 11 additions & 106 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { $platform, Lodash as _, Storage, fetch, notification, log, logError, wa
import { gRPC } from "@nsnanocat/util";
import database from "./function/database.mjs";
import setENV from "./function/setENV.mjs";
import isResponseAvailability from "./function/isResponseAvailability.mjs";
import { WireType, UnknownFieldHandler, reflectionMergePartial, MESSAGE_TYPE, MessageType, BinaryReader, isJsonObject, typeofJsonValue, jsonWriteOptions } from "@protobuf-ts/runtime";
import { ViewReq } from "./protobuf/bilibili/app/viewunite/v1/viewunite.js";
import { PlayViewUniteReq } from "./protobuf/bilibili/app/playerunite/v1/playerunite.js";
Expand Down Expand Up @@ -353,7 +354,7 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
) {
case "Shadowrocket":
case "Quantumult X":
delete $request.policy;
$request.policy = undefined;
break;
}
break;
Expand Down Expand Up @@ -384,9 +385,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 @@ -401,8 +401,8 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
})()
.catch(e => logError(e))
.finally(() => {
switch ($response) {
default: // 有构造回复数据,返回构造的回复数据
switch (typeof $response) {
case "object": // 有构造回复数据,返回构造的回复数据
if ($response.headers?.["Content-Encoding"]) $response.headers["Content-Encoding"] = "identity";
if ($response.headers?.["content-encoding"]) $response.headers["content-encoding"] = "identity";
switch ($platform) {
Expand All @@ -418,111 +418,16 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
break;
}
break;
case undefined: // 无构造回复数据,发送修改的请求数据
case "undefined": // 无构造回复数据,发送修改的请求数据
done($request);
break;
default:
logError(`不合法的 $response 类型: ${typeof $response}`, "");
break;
}
});

/***************** Function *****************/
/**
* Determine Response Availability
* @author VirgilClyne
* @param {Object} response - Original Response Content
* @return {Boolean} is Available
*/
function isResponseAvailability(response = {}) {
log("☑️ Determine Response Availability", "");
const FORMAT = (response?.headers?.["Content-Type"] ?? response?.headers?.["content-type"])?.split(";")?.[0];
log("🚧 Determine Response Availability", `FORMAT: ${FORMAT}`, "");
let isAvailable = true;
switch (response?.statusCode) {
case 200:
switch (FORMAT) {
case "application/grpc":
case "application/grpc+proto":
switch (response?.headers?.["Grpc-Message"] ?? response?.headers?.["grpc-message"]) {
case "0":
isAvailable = true;
break;
case undefined:
if (Number.parseInt(response?.headers?.["content-length"] ?? response?.headers?.["Content-Length"]) < 1200) isAvailable = false;
else isAvailable = true;
break;
case "-404":
default:
isAvailable = false;
break;
}
break;
case "text/json":
case "application/json":
switch (response?.headers?.["bili-status-code"]) {
case "0":
case undefined: {
const data = JSON.parse(response?.body).data;
switch (response?.headers?.idc) {
case "sgp001":
case "sgp002":
switch (data?.limit) {
case "":
case undefined:
isAvailable = true;
break;
default:
isAvailable = false;
break;
}
break;
case "shjd":
case undefined:
default:
switch (data?.video_info?.code) {
case 0:
default:
isAvailable = true;
break;
case undefined:
isAvailable = false;
break;
}
switch (data?.dialog?.code) {
case undefined:
isAvailable = true;
break;
case 6010001:
default:
isAvailable = false;
break;
}
break;
}
break;
}
case "-404": // 啥都木有
case "-10403":
case "10015001": // 版权地区受限
default:
isAvailable = false;
break;
}
break;
case "text/html":
isAvailable = true;
break;
}
break;
case 403:
case 404:
case 415:
default:
isAvailable = false;
break;
}
log("✅ Determine Response Availability", `isAvailable:${isAvailable}`, "");
return isAvailable;
}

/**
* Fetch
* @author VirgilClyne
Expand Down Expand Up @@ -555,7 +460,7 @@ 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 = undefined;
responses[locale] = await fetch(request);
}),
);
Expand Down

0 comments on commit 7335ee9

Please sign in to comment.