Spaces:
Running
Running
File size: 2,410 Bytes
5bab120 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import ipaddr from "ipaddr.js";
import { createStream } from "../stream/manage.js";
import { apiSchema } from "./schema.js";
export function createResponse(responseType, responseData) {
const internalError = (code) => {
return {
status: 500,
body: {
status: "error",
error: {
code: code || "error.api.fetch.critical",
},
critical: true
}
}
}
try {
let status = 200,
response = {};
if (responseType === "error") {
status = 400;
}
switch (responseType) {
case "error":
response = {
error: {
code: responseData?.code,
context: responseData?.context,
}
}
break;
case "redirect":
response = {
url: responseData?.u,
filename: responseData?.filename
}
break;
case "tunnel":
response = {
url: createStream(responseData),
filename: responseData?.filename
}
break;
case "picker":
response = {
picker: responseData?.picker,
audio: responseData?.u,
audioFilename: responseData?.filename
}
break;
case "critical":
return internalError(responseData?.code);
default:
throw "unreachable"
}
return {
status,
body: {
status: responseType,
...response
}
}
} catch {
return internalError()
}
}
export function normalizeRequest(request) {
return apiSchema.safeParseAsync(request).catch(() => (
{ success: false }
));
}
export function getIP(req) {
const strippedIP = req.ip.replace(/^::ffff:/, '');
const ip = ipaddr.parse(strippedIP);
if (ip.kind() === 'ipv4') {
return strippedIP;
}
const prefix = 56;
const v6Bytes = ip.toByteArray();
v6Bytes.fill(0, prefix / 8);
return ipaddr.fromByteArray(v6Bytes).toString();
}
|