api: get rid of getJSON
let's just call `match` directly
This commit is contained in:
parent
c1958596e9
commit
c5c3682462
3 changed files with 20 additions and 22 deletions
|
@ -5,13 +5,15 @@ import { randomBytes } from "crypto";
|
||||||
const ipSalt = randomBytes(64).toString('hex');
|
const ipSalt = randomBytes(64).toString('hex');
|
||||||
|
|
||||||
import { env, version } from "../modules/config.js";
|
import { env, version } from "../modules/config.js";
|
||||||
import { getJSON } from "../modules/api.js";
|
import match from "../modules/processing/match.js";
|
||||||
import { apiJSON, checkJSONPost, getIP, languageCode } from "../modules/sub/utils.js";
|
import { apiJSON, checkJSONPost, getIP, languageCode } from "../modules/sub/utils.js";
|
||||||
import { Bright, Cyan } from "../modules/sub/consoleText.js";
|
import { Bright, Cyan } from "../modules/sub/consoleText.js";
|
||||||
import stream from "../modules/stream/stream.js";
|
import stream from "../modules/stream/stream.js";
|
||||||
import loc from "../localization/manager.js";
|
import loc from "../localization/manager.js";
|
||||||
import { generateHmac } from "../modules/sub/crypto.js";
|
import { generateHmac } from "../modules/sub/crypto.js";
|
||||||
import { verifyStream, getInternalStream } from "../modules/stream/manage.js";
|
import { verifyStream, getInternalStream } from "../modules/stream/manage.js";
|
||||||
|
import { extract } from "../modules/processing/url.js";
|
||||||
|
import { errorUnsupported } from "../modules/sub/errors.js";
|
||||||
|
|
||||||
export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
|
export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
|
||||||
const corsConfig = !env.corsWildcard ? {
|
const corsConfig = !env.corsWildcard ? {
|
||||||
|
@ -105,8 +107,14 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
|
||||||
|
|
||||||
let chck = checkJSONPost(request);
|
let chck = checkJSONPost(request);
|
||||||
if (!chck) throw new Error();
|
if (!chck) throw new Error();
|
||||||
|
|
||||||
|
const parsed = extract(chck.url);
|
||||||
|
if (parsed === null) {
|
||||||
|
return apiJSON(0, { t: errorUnsupported(lang) })
|
||||||
|
}
|
||||||
|
|
||||||
j = await getJSON(chck.url, lang, chck);
|
j = await match(parsed.host, parsed.patternMatch, chck.url, lang, chck)
|
||||||
|
.catch(() => apiJSON(0, { t: loc(lang, 'ErrorSomethingWentWrong') }))
|
||||||
} else {
|
} else {
|
||||||
j = apiJSON(0, {
|
j = apiJSON(0, {
|
||||||
t: !contentCon ? "invalid content type header" : loc(lang, 'ErrorNoLink')
|
t: !contentCon ? "invalid content type header" : loc(lang, 'ErrorNoLink')
|
||||||
|
|
|
@ -1,18 +0,0 @@
|
||||||
import { apiJSON } from "./sub/utils.js";
|
|
||||||
import { errorUnsupported } from "./sub/errors.js";
|
|
||||||
import loc from "../localization/manager.js";
|
|
||||||
import match from "./processing/match.js";
|
|
||||||
import { extract } from "./processing/url.js";
|
|
||||||
|
|
||||||
export async function getJSON(url, lang, obj) {
|
|
||||||
try {
|
|
||||||
const parsed = extract(url);
|
|
||||||
if (parsed === null) {
|
|
||||||
return apiJSON(0, { t: errorUnsupported(lang) });
|
|
||||||
}
|
|
||||||
|
|
||||||
return await match(parsed.host, parsed.patternMatch, url, lang, obj)
|
|
||||||
} catch (e) {
|
|
||||||
return apiJSON(0, { t: loc(lang, 'ErrorSomethingWentWrong') })
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,11 +1,14 @@
|
||||||
import "dotenv/config";
|
import "dotenv/config";
|
||||||
import "../modules/sub/alias-envs.js";
|
import "../modules/sub/alias-envs.js";
|
||||||
|
|
||||||
import { getJSON } from "../modules/api.js";
|
|
||||||
import { services } from "../modules/config.js";
|
import { services } from "../modules/config.js";
|
||||||
|
import { extract } from "../modules/processing/url.js";
|
||||||
|
import match from "../modules/processing/match.js";
|
||||||
import { loadJSON } from "../modules/sub/loadFromFs.js";
|
import { loadJSON } from "../modules/sub/loadFromFs.js";
|
||||||
import { checkJSONPost } from "../modules/sub/utils.js";
|
import { checkJSONPost } from "../modules/sub/utils.js";
|
||||||
|
import { env } from "../modules/config.js";
|
||||||
|
|
||||||
|
env.apiURL = 'http://localhost:9000'
|
||||||
let tests = loadJSON('./src/test/tests.json');
|
let tests = loadJSON('./src/test/tests.json');
|
||||||
|
|
||||||
let noTest = [];
|
let noTest = [];
|
||||||
|
@ -35,7 +38,12 @@ for (let i in services) {
|
||||||
let chck = checkJSONPost(params);
|
let chck = checkJSONPost(params);
|
||||||
if (chck) {
|
if (chck) {
|
||||||
chck["ip"] = "d21ec524bc2ade41bef569c0361ac57728c69e2764b5cb3cb310fe36568ca53f"; // random sha256
|
chck["ip"] = "d21ec524bc2ade41bef569c0361ac57728c69e2764b5cb3cb310fe36568ca53f"; // random sha256
|
||||||
let j = await getJSON(chck["url"], "en", chck);
|
const parsed = extract(chck.url);
|
||||||
|
if (parsed === null) {
|
||||||
|
throw `Invalid URL: ${chck.url}`
|
||||||
|
}
|
||||||
|
|
||||||
|
let j = await match(parsed.host, parsed.patternMatch, "en", chck);
|
||||||
console.log('\nReceived:');
|
console.log('\nReceived:');
|
||||||
console.log(j)
|
console.log(j)
|
||||||
if (j.status === test.expected.code && j.body.status === test.expected.status) {
|
if (j.status === test.expected.code && j.body.status === test.expected.status) {
|
||||||
|
|
Loading…
Reference in a new issue