Files
anything-llm/server/utils/http/index.js
Marcello Fitton 4a4378ed99 chore: add ESLint to /server (#5126)
* add eslint config to server

* add break statements to switch case

* add support for browser globals and turn off empty catch blocks

* disable lines with useless try/catch wrappers

* format

* fix no-undef errors

* disbale lines violating no-unsafe-finally

* ignore syncStaticLists.mjs

* use proper null check for creatorId instead of unreachable nullish coalescing

* remove unneeded typescript eslint comment

* make no-unused-private-class-members a warning

* disable line for no-empty-objects

* add new lint script

* fix no-unused-vars violations

* make no-unsued-vars an error

---------

Co-authored-by: shatfield4 <seanhatfield5@gmail.com>
Co-authored-by: Timothy Carambat <rambat1010@gmail.com>
2026-03-05 16:32:45 -08:00

136 lines
3.3 KiB
JavaScript

process.env.NODE_ENV === "development"
? require("dotenv").config({ path: `.env.${process.env.NODE_ENV}` })
: require("dotenv").config();
const JWT = require("jsonwebtoken");
const { User } = require("../../models/user");
const { jsonrepair } = require("jsonrepair");
const extract = require("extract-json-from-string");
function reqBody(request) {
return typeof request.body === "string"
? JSON.parse(request.body)
: request.body;
}
function queryParams(request) {
return request.query;
}
/**
* Creates a JWT with the given info and expiry
* @param {object} info - The info to include in the JWT
* @param {string} expiry - The expiry time for the JWT (default: 30 days)
* @returns {string} The JWT
*/
function makeJWT(info = {}, expiry = "30d") {
if (!process.env.JWT_SECRET)
throw new Error("Cannot create JWT as JWT_SECRET is unset.");
return JWT.sign(info, process.env.JWT_SECRET, { expiresIn: expiry });
}
// Note: Only valid for finding users in multi-user mode
// as single-user mode with password is not a "user"
async function userFromSession(request, response = null) {
if (!!response && !!response.locals?.user) {
return response.locals.user;
}
const auth = request.header("Authorization");
const token = auth ? auth.split(" ")[1] : null;
if (!token) {
return null;
}
const valid = decodeJWT(token);
if (!valid || !valid.id) {
return null;
}
const user = await User.get({ id: valid.id });
return user;
}
function decodeJWT(jwtToken) {
try {
return JWT.verify(jwtToken, process.env.JWT_SECRET);
} catch {}
return { p: null, id: null, username: null };
}
function multiUserMode(response) {
return response?.locals?.multiUserMode;
}
function parseAuthHeader(headerValue = null, apiKey = null) {
if (headerValue === null || apiKey === null) return {};
if (headerValue === "Authorization")
return { Authorization: `Bearer ${apiKey}` };
return { [headerValue]: apiKey };
}
function safeJsonParse(jsonString, fallback = null) {
if (jsonString === null) return fallback;
try {
return JSON.parse(jsonString);
} catch {}
if (jsonString?.startsWith("[") || jsonString?.startsWith("{")) {
try {
const repairedJson = jsonrepair(jsonString);
return JSON.parse(repairedJson);
} catch {}
}
try {
return extract(jsonString)?.[0] || fallback;
} catch {}
return fallback;
}
function isValidUrl(urlString = "") {
try {
const url = new URL(urlString);
if (!["http:", "https:"].includes(url.protocol)) return false;
return true;
} catch {}
return false;
}
function toValidNumber(number = null, fallback = null) {
if (isNaN(Number(number))) return fallback;
return Number(number);
}
/**
* Decode HTML entities from a string.
* The DMR response is encoded with HTML entities, so we need to decode them
* so we can parse the JSON and report the progress percentage.
* @param {string} str - The string to decode.
* @returns {string} The decoded string.
*/
function decodeHtmlEntities(str) {
return str
.replace(/&#34;/g, '"')
.replace(/&#39;/g, "'")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&amp;/g, "&");
}
module.exports = {
reqBody,
multiUserMode,
queryParams,
makeJWT,
decodeJWT,
userFromSession,
parseAuthHeader,
safeJsonParse,
isValidUrl,
toValidNumber,
decodeHtmlEntities,
};