mirror of
https://github.com/wukko/cobalt.git
synced 2025-06-12 13:17:45 +02:00
api: use zod for request schema validation
This commit is contained in:
@ -138,8 +138,8 @@ export function runAPI(express, app, __dirname) {
|
||||
request.youtubeDubLang = lang;
|
||||
}
|
||||
|
||||
const normalizedRequest = normalizeRequest(request);
|
||||
if (!normalizedRequest) {
|
||||
const { success, data: normalizedRequest } = await normalizeRequest(request);
|
||||
if (!success) {
|
||||
return fail('ErrorCantProcess');
|
||||
}
|
||||
|
||||
|
@ -3,8 +3,8 @@ import match from "../processing/match.js";
|
||||
import { extract } from "../processing/url.js";
|
||||
|
||||
export async function runTest(url, params, expect) {
|
||||
const normalized = normalizeRequest({ url, ...params });
|
||||
if (!normalized) {
|
||||
const { success, data: normalized } = await normalizeRequest({ url, ...params });
|
||||
if (!success) {
|
||||
throw "invalid request";
|
||||
}
|
||||
|
||||
|
@ -1,26 +1,7 @@
|
||||
import ipaddr from "ipaddr.js";
|
||||
|
||||
import { normalizeURL } from "./url.js";
|
||||
import { createStream } from "../stream/manage.js";
|
||||
import { verifyLanguageCode } from "../misc/utils.js";
|
||||
|
||||
const apiRequest = {
|
||||
option: {
|
||||
audioFormat: ["best", "mp3", "ogg", "wav", "opus"],
|
||||
downloadMode: ["auto", "audio", "mute"],
|
||||
filenameStyle: ["classic", "pretty", "basic", "nerdy"],
|
||||
videoQuality: ["max", "4320", "2160", "1440", "1080", "720", "480", "360", "240", "144"],
|
||||
youtubeVideoCodec: ["h264", "av1", "vp9"],
|
||||
},
|
||||
boolean: [
|
||||
"disableMetadata",
|
||||
"tiktokFullAudio",
|
||||
"tiktokH265",
|
||||
"twitterGif",
|
||||
"youtubeDubBrowserLang",
|
||||
"youtubeDubLang"
|
||||
]
|
||||
}
|
||||
import { apiSchema } from "./schema.js";
|
||||
|
||||
export function createResponse(responseType, responseData) {
|
||||
const internalError = (code) => {
|
||||
@ -91,49 +72,7 @@ export function createResponse(responseType, responseData) {
|
||||
}
|
||||
|
||||
export function normalizeRequest(request) {
|
||||
try {
|
||||
let template = {
|
||||
audioFormat: "mp3",
|
||||
url: normalizeURL(decodeURIComponent(request.url)),
|
||||
youtubeVideoCodec: "h264",
|
||||
videoQuality: "720",
|
||||
filenameStyle: "classic",
|
||||
downloadMode: "auto",
|
||||
tiktokFullAudio: false,
|
||||
disableMetadata: false,
|
||||
youtubeDubBrowserLang: false,
|
||||
youtubeDubLang: false,
|
||||
twitterGif: false,
|
||||
tiktokH265: false
|
||||
}
|
||||
|
||||
const requestKeys = Object.keys(request);
|
||||
const templateKeys = Object.keys(template);
|
||||
|
||||
if (requestKeys.length > templateKeys.length + 1 || !request.url) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const i in requestKeys) {
|
||||
const key = requestKeys[i];
|
||||
const item = request[key];
|
||||
|
||||
if (String(key) !== "url" && templateKeys.includes(key)) {
|
||||
if (apiRequest.boolean.includes(key)) {
|
||||
template[key] = !!item;
|
||||
} else if (apiRequest.option[key] && apiRequest.option[key].includes(item)) {
|
||||
template[key] = String(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (template.youtubeDubBrowserLang)
|
||||
template.youtubeDubLang = verifyLanguageCode(request.youtubeDubLang);
|
||||
|
||||
return template
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
return apiSchema.safeParseAsync(request).catch(() => ({ success: false }));
|
||||
}
|
||||
|
||||
export function getIP(req) {
|
||||
|
42
api/src/processing/schema.js
Normal file
42
api/src/processing/schema.js
Normal file
@ -0,0 +1,42 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { normalizeURL } from "./url.js";
|
||||
import { verifyLanguageCode } from "../misc/utils.js";
|
||||
|
||||
export const apiSchema = z.object({
|
||||
url: z.string()
|
||||
.min(1)
|
||||
.transform((url) => normalizeURL(decodeURIComponent(url))),
|
||||
|
||||
audioFormat: z.enum(
|
||||
["best", "mp3", "ogg", "wav", "opus"]
|
||||
).default("mp3"),
|
||||
|
||||
downloadMode: z.enum(
|
||||
["auto", "audio", "mute"]
|
||||
).default("auto"),
|
||||
|
||||
filenameStyle: z.enum(
|
||||
["classic", "pretty", "basic", "nerdy"]
|
||||
).default("classic"),
|
||||
|
||||
youtubeVideoCodec: z.enum(
|
||||
["h264", "av1", "vp9"]
|
||||
).default("h264"),
|
||||
|
||||
videoQuality: z.enum([
|
||||
"max", "4320", "2160", "1440", "1080", "720", "480", "360", "240", "144"
|
||||
]).default("720"),
|
||||
|
||||
youtubeDubLang: z.string()
|
||||
.length(2)
|
||||
.transform(verifyLanguageCode)
|
||||
.optional(),
|
||||
|
||||
disableMetadata: z.boolean().default(false),
|
||||
tiktokFullAudio: z.boolean().default(false),
|
||||
tiktokH265: z.boolean().default(false),
|
||||
twitterGif: z.boolean().default(false),
|
||||
youtubeDubBrowserLang: z.boolean().default(false),
|
||||
})
|
||||
.strict();
|
@ -34,8 +34,10 @@ for (let i in services) {
|
||||
let params = {...{url: test.url}, ...test.params};
|
||||
console.log(params);
|
||||
|
||||
let chck = normalizeRequest(params);
|
||||
if (chck) {
|
||||
let chck = await normalizeRequest(params);
|
||||
if (chck.success) {
|
||||
chck = chck.data;
|
||||
|
||||
const parsed = extract(chck.url);
|
||||
if (parsed === null) {
|
||||
throw `Invalid URL: ${chck.url}`
|
||||
|
Reference in New Issue
Block a user