Skip to content

Commit

Permalink
refactor: request
Browse files Browse the repository at this point in the history
  • Loading branch information
VirgilClyne committed Nov 18, 2024
1 parent cbc156c commit f7f4f34
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 60 deletions.
37 changes: 21 additions & 16 deletions src/function/database.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
export default {
"Location": {
"Settings": {
"Switch": true,
"PEP": {
"GCC": "US"
}
}
},
"Maps": {
"Settings": {
"Switch": true,
"UrlInfoSet": {
"Dispatcher": "AutoNavi",
"Directions": "AutoNavi",
Expand All @@ -27,28 +25,35 @@ export default {
"GeoManifest": {
"Dynamic": {
"Config": {
"CountryCode": {
"default": "CN",
"iOS": "AUTO",
"iPadOS": "AUTO",
"watchOS": "US",
"macOS": "AUTO"
}
"CountryCode": "CN"
}
}
},
"Config": {
"Announcements": {
"Environment:": {
"default": "AUTO",
"iOS": "AUTO",
"iPadOS": "AUTO",
"watchOS": "AUTO",
"macOS": "AUTO"
}
"Environment:": "CN"
}
}
},
"Configs": {}
},
"Watch": {
"Settings": {
"UrlInfoSet": {
"LocationShift": "Apple"
},
"GeoManifest": {
"Dynamic": {
"Config": {
"CountryCode": "US"
}
}
},
"Config": {
"Announcements": {
"Environment:": "CN"
}
}
}
}
}
74 changes: 42 additions & 32 deletions src/request.dev.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { $app, Lodash as _, Storage, fetch, notification, log, logError, wait, done } from "@nsnanocat/util";
import { URL } from "@nsnanocat/url";
import database from "./function/database.mjs";
import setENV from "./function/setENV.mjs";
import GEOPDPlaceRequest from "./class/GEOPDPlaceRequest.mjs";
Expand All @@ -17,18 +16,22 @@ log(`⚠ METHOD: ${METHOD}, HOST: ${HOST}, PATH: ${PATH}`, "");
// 解析格式
const FORMAT = ($request.headers?.["Content-Type"] ?? $request.headers?.["content-type"])?.split(";")?.[0];
log(`⚠ FORMAT: ${FORMAT}`, "");
const PLATFORM = ["Maps"];
if (url.searchParams.get("os") === "watchos") PLATFORM.push("Watch");
log(`⚠ PLATFORM: ${PLATFORM}`, "");
!(async () => {
/**
* @type {{Settings: import('./interface').Settings}}
*/
const { Settings, Caches, Configs } = setENV("iRingo", ["Location", "Maps"], database);
const { Settings, Caches, Configs } = setENV("iRingo", PLATFORM, database);
// 创建空数据
let body = {};
// 方法判断
switch (METHOD) {
case "POST":
case "PUT":
case "PATCH":
// biome-ignore lint/suspicious/noFallthroughSwitchClause: <explanation>
case "DELETE":
// 格式判断
switch (FORMAT) {
Expand Down Expand Up @@ -73,7 +76,7 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
case "application/vnd.google.protobuf":
case "application/grpc":
case "application/grpc+proto":
case "application/octet-stream":
case "application/octet-stream": {
//log(`🚧 $request: ${JSON.stringify($request, null, 2)}`, "");
let rawBody = $app === "Quantumult X" ? new Uint8Array($request.bodyBytes ?? []) : ($request.body ?? new Uint8Array());
//log(`🚧 isBuffer? ${ArrayBuffer.isView(rawBody)}: ${JSON.stringify(rawBody, null, 2)}`, "");
Expand All @@ -82,7 +85,7 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
case "dispatcher.is.autonavi.com":
switch (PATH) {
case "/dispatcher.arpc":
case "/dispatcher":
case "/dispatcher": {
/****************** initialization start *******************/
// 先拆分aRPC校验头和protobuf数据体
const headerIndex = rawBody.findIndex((element, index) => element === 0x0a && index > 47);
Expand All @@ -105,12 +108,14 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
rawBody.set(body, Header.length);
/****************** initialization finish *******************/
break;
}
}
break;
}
// 写入二进制数据
$request.body = rawBody;
break;
}
}
//break; // 不中断,继续处理URL
case "GET":
Expand Down Expand Up @@ -148,7 +153,7 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
}
break;
case "gspe35-ssl.ls.apple.com":
case "gspe35-ssl.ls.apple.cn":
case "gspe35-ssl.ls.apple.cn": {
const OS = url.searchParams.get("os");
switch (PATH) {
case "/config/announcements":
Expand All @@ -160,16 +165,16 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
switch (Settings?.Config?.Announcements?.Environment?.default) {
case "AUTO":
/*
switch (Caches?.pep?.gcc) {
default:
url.searchParams.set("environment", "prod");
break;
case "CN":
case undefined:
url.searchParams.set("environment", "prod-cn");
break;
};
*/
switch (Caches?.pep?.gcc) {
default:
url.searchParams.set("environment", "prod");
break;
case "CN":
case undefined:
url.searchParams.set("environment", "prod-cn");
break;
};
*/
break;
case "CN":
default:
Expand All @@ -184,16 +189,16 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
switch (Settings?.Config?.Announcements?.Environment?.watchOS) {
case "AUTO":
/*
switch (Caches?.pep?.gcc) {
default:
url.searchParams.set("environment", "prod");
break;
case "CN":
case undefined:
url.searchParams.set("environment", "prod-cn");
break;
};
*/
switch (Caches?.pep?.gcc) {
default:
url.searchParams.set("environment", "prod");
break;
case "CN":
case undefined:
url.searchParams.set("environment", "prod-cn");
break;
};
*/
break;
case "XX":
default:
Expand Down Expand Up @@ -251,23 +256,24 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
break;
}
break;
}
}
break;
case "CONNECT":
case "TRACE":
break;
}
$request.url = url.toString();
log(`🚧 调试信息`, `$request.url: ${$request.url}`, "");
log("🚧 调试信息", `$request.url: ${$request.url}`, "");
})()
.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 ($app) {
switch ($platform) {
default:
done({ response: $response });
break;
Expand All @@ -280,9 +286,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}`, "");
done();
break;
}
});
34 changes: 22 additions & 12 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ log(`⚠ METHOD: ${METHOD}, HOST: ${HOST}, PATH: ${PATH}`, "");
// 解析格式
const FORMAT = ($request.headers?.["Content-Type"] ?? $request.headers?.["content-type"])?.split(";")?.[0];
log(`⚠ FORMAT: ${FORMAT}`, "");
const PLATFORM = ["Maps"];
if (url.searchParams.get("os") === "watchos") PLATFORM.push("Watch");
log(`⚠ PLATFORM: ${PLATFORM}`, "");
!(async () => {
/**
* @type {{Settings: import('./interface').Settings}}
*/
const { Settings, Caches, Configs } = setENV("iRingo", ["Location", "Maps"], database);
const { Settings, Caches, Configs } = setENV("iRingo", PLATFORM, database);
// 创建空数据
let body = {};
// 方法判断
switch (METHOD) {
case "POST":
case "PUT":
case "PATCH":
// biome-ignore lint/suspicious/noFallthroughSwitchClause: <explanation>
case "DELETE":
// 格式判断
switch (FORMAT) {
Expand Down Expand Up @@ -71,13 +75,14 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
case "application/vnd.google.protobuf":
case "application/grpc":
case "application/grpc+proto":
case "application/octet-stream":
log(`🚧 $request: ${JSON.stringify($request, null, 2)}`, "");
case "application/octet-stream": {
//log(`🚧 $request: ${JSON.stringify($request, null, 2)}`, "");
let rawBody = $app === "Quantumult X" ? new Uint8Array($request.bodyBytes ?? []) : ($request.body ?? new Uint8Array());
log(`🚧 isBuffer? ${ArrayBuffer.isView(rawBody)}: ${JSON.stringify(rawBody, null, 2)}`, "");
//log(`🚧 isBuffer? ${ArrayBuffer.isView(rawBody)}: ${JSON.stringify(rawBody, null, 2)}`, "");
// 写入二进制数据
$request.body = rawBody;
break;
}
}
//break; // 不中断,继续处理URL
case "GET":
Expand Down Expand Up @@ -115,7 +120,7 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
}
break;
case "gspe35-ssl.ls.apple.com":
case "gspe35-ssl.ls.apple.cn":
case "gspe35-ssl.ls.apple.cn": {
const OS = url.searchParams.get("os");
switch (PATH) {
case "/config/announcements":
Expand Down Expand Up @@ -196,23 +201,24 @@ log(`⚠ FORMAT: ${FORMAT}`, "");
break;
}
break;
}
}
break;
case "CONNECT":
case "TRACE":
break;
}
$request.url = url.toString();
log(`🚧 调试信息`, `$request.url: ${$request.url}`, "");
log("🚧 调试信息", `$request.url: ${$request.url}`, "");
})()
.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 ($app) {
switch ($platform) {
default:
done({ response: $response });
break;
Expand All @@ -225,9 +231,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}`, "");
done();
break;
}
});

0 comments on commit f7f4f34

Please sign in to comment.