Merge pull request #1062 from RealBeepMcJeep/make_jsdoc_happy2

Make jsdoc/type-checker happy Pt 2
This commit is contained in:
Cohee 2023-08-31 00:33:34 +03:00 committed by GitHub
commit fcfd8b8a53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 42 deletions

View File

@ -7,7 +7,8 @@
"strictFunctionTypes": true, "strictFunctionTypes": true,
"checkJs": true, "checkJs": true,
"allowUmdGlobalAccess": true, "allowUmdGlobalAccess": true,
"allowSyntheticDefaultImports": true "allowSyntheticDefaultImports": true,
"resolveJsonModule": true
}, },
"exclude": [ "exclude": [
"node_modules", "node_modules",

View File

@ -91,10 +91,8 @@ function createDefaultFiles() {
} }
const net = require("net"); const net = require("net");
// work around a node v20 bug: https://github.com/nodejs/node/issues/47822#issuecomment-1564708870 // @ts-ignore work around a node v20 bug: https://github.com/nodejs/node/issues/47822#issuecomment-1564708870
if (net.setDefaultAutoSelectFamily) { if (net.setDefaultAutoSelectFamily) net.setDefaultAutoSelectFamily(false);
net.setDefaultAutoSelectFamily(false);
}
const cliArguments = yargs(hideBin(process.argv)) const cliArguments = yargs(hideBin(process.argv))
.option('disableCsrf', { .option('disableCsrf', {
@ -113,7 +111,7 @@ const cliArguments = yargs(hideBin(process.argv))
type: 'string', type: 'string',
default: 'certs/privkey.pem', default: 'certs/privkey.pem',
describe: 'Path to your private key file.' describe: 'Path to your private key file.'
}).argv; }).parseSync();
// change all relative paths // change all relative paths
const directory = process['pkg'] ? path.dirname(process.execPath) : __dirname; const directory = process['pkg'] ? path.dirname(process.execPath) : __dirname;
@ -367,7 +365,7 @@ if (cliArguments.disableCsrf === false) {
app.get("/csrf-token", (req, res) => { app.get("/csrf-token", (req, res) => {
res.json({ res.json({
"token": generateToken(res) "token": generateToken(res, req)
}); });
}); });
@ -470,7 +468,7 @@ app.get("/notes/*", function (request, response) {
app.get('/deviceinfo', function (request, response) { app.get('/deviceinfo', function (request, response) {
const userAgent = request.header('user-agent'); const userAgent = request.header('user-agent');
const deviceDetector = new DeviceDetector(); const deviceDetector = new DeviceDetector();
const deviceInfo = deviceDetector.parse(userAgent); const deviceInfo = deviceDetector.parse(userAgent || "");
return response.send(deviceInfo); return response.send(deviceInfo);
}); });
app.get('/version', function (_, response) { app.get('/version', function (_, response) {
@ -479,7 +477,7 @@ app.get('/version', function (_, response) {
}) })
//**************Kobold api //**************Kobold api
app.post("/generate", jsonParser, async function (request, response_generate = response) { app.post("/generate", jsonParser, async function (request, response_generate) {
if (!request.body) return response_generate.sendStatus(400); if (!request.body) return response_generate.sendStatus(400);
const request_prompt = request.body.prompt; const request_prompt = request.body.prompt;
@ -623,6 +621,10 @@ app.post("/generate_textgenerationwebui", jsonParser, async function (request, r
}); });
if (request.header('X-Response-Streaming')) { if (request.header('X-Response-Streaming')) {
const streamingUrlHeader = request.header('X-Streaming-URL');
if (streamingUrlHeader === undefined) return response_generate.sendStatus(400);
const streamingUrl = streamingUrlHeader.replace("localhost", "127.0.0.1");
response_generate.writeHead(200, { response_generate.writeHead(200, {
'Content-Type': 'text/plain;charset=utf-8', 'Content-Type': 'text/plain;charset=utf-8',
'Transfer-Encoding': 'chunked', 'Transfer-Encoding': 'chunked',
@ -630,7 +632,6 @@ app.post("/generate_textgenerationwebui", jsonParser, async function (request, r
}); });
async function* readWebsocket() { async function* readWebsocket() {
const streamingUrl = request.header('X-Streaming-URL').replace("localhost", "127.0.0.1");
const websocket = new WebSocket(streamingUrl); const websocket = new WebSocket(streamingUrl);
websocket.on('open', async function () { websocket.on('open', async function () {
@ -661,7 +662,7 @@ app.post("/generate_textgenerationwebui", jsonParser, async function (request, r
websocket.once('error', reject); websocket.once('error', reject);
websocket.once('message', (data, isBinary) => { websocket.once('message', (data, isBinary) => {
websocket.removeListener('error', reject); websocket.removeListener('error', reject);
resolve(data, isBinary); resolve(data);
}); });
}); });
} catch (err) { } catch (err) {
@ -961,13 +962,12 @@ function charaFormatData(data) {
// This is supposed to save all the foreign keys that ST doesn't care about // This is supposed to save all the foreign keys that ST doesn't care about
const char = tryParse(data.json_data) || {}; const char = tryParse(data.json_data) || {};
// This function uses _.cond() to create a series of conditional checks that return the desired output based on the input data. // Checks if data.alternate_greetings is an array, a string, or neither, and acts accordingly. (expected to be an array of strings)
// It checks if data.alternate_greetings is an array, a string, or neither, and acts accordingly. const getAlternateGreetings = data => {
const getAlternateGreetings = data => _.cond([ if (Array.isArray(data.alternate_greetings)) return data.alternate_greetings
[d => Array.isArray(d.alternate_greetings), d => d.alternate_greetings], if (typeof data.alternate_greetings === 'string') return [data.alternate_greetings]
[d => typeof d.alternate_greetings === 'string', d => [d.alternate_greetings]], return []
[_.stubTrue, _.constant([])] }
])(data);
// Spec V1 fields // Spec V1 fields
_.set(char, 'name', data.ch_name); _.set(char, 'name', data.ch_name);
@ -1099,6 +1099,8 @@ app.post("/renamecharacter", jsonParser, async function (request, response) {
try { try {
// Read old file, replace name int it // Read old file, replace name int it
const rawOldData = await charaRead(oldAvatarPath); const rawOldData = await charaRead(oldAvatarPath);
if (rawOldData === false || rawOldData === undefined) throw new Error("Failed to read character file");
const oldData = getCharaCardV2(json5.parse(rawOldData)); const oldData = getCharaCardV2(json5.parse(rawOldData));
_.set(oldData, 'data.name', newName); _.set(oldData, 'data.name', newName);
_.set(oldData, 'name', newName); _.set(oldData, 'name', newName);
@ -1187,26 +1189,22 @@ app.post("/editcharacterattribute", jsonParser, async function (request, respons
try { try {
const avatarPath = path.join(charactersPath, request.body.avatar_url); const avatarPath = path.join(charactersPath, request.body.avatar_url);
charaRead(avatarPath).then((char) => { let charJSON = await charaRead(avatarPath);
char = JSON.parse(char); if (typeof charJSON !== 'string') throw new Error("Failed to read character file");
//check if the field exists
if (char[request.body.field] === undefined && char.data[request.body.field] === undefined) { let char = JSON.parse(charJSON)
console.error('Error: invalid field.'); //check if the field exists
response.status(400).send('Error: invalid field.'); if (char[request.body.field] === undefined && char.data[request.body.field] === undefined) {
return; console.error('Error: invalid field.');
} response.status(400).send('Error: invalid field.');
char[request.body.field] = request.body.value; return;
char.data[request.body.field] = request.body.value; }
char = JSON.stringify(char); char[request.body.field] = request.body.value;
return { char }; char.data[request.body.field] = request.body.value;
}).then(({ char }) => { let newCharJSON = JSON.stringify(char);
charaWrite(avatarPath, char, (request.body.avatar_url).replace('.png', ''), response, 'Character saved'); await charaWrite(avatarPath, newCharJSON, (request.body.avatar_url).replace('.png', ''), response, 'Character saved');
}).catch((err) => { } catch (err) {
console.error('An error occured, character edit invalidated.', err); console.error('An error occured, character edit invalidated.', err);
});
}
catch {
console.error('An error occured, character edit invalidated.');
} }
}); });
@ -1246,6 +1244,10 @@ app.post("/deletecharacter", jsonParser, async function (request, response) {
return response.sendStatus(200); return response.sendStatus(200);
}); });
/**
* @param {express.Response | undefined} response
* @param {{file_name: string} | string} mes
*/
async function charaWrite(img_url, data, target_img, response = undefined, mes = 'ok', crop = undefined) { async function charaWrite(img_url, data, target_img, response = undefined, mes = 'ok', crop = undefined) {
try { try {
// Read the image, resize, and save it as a PNG into the buffer // Read the image, resize, and save it as a PNG into the buffer
@ -4053,23 +4055,24 @@ if (listen && !config.whitelistMode && !config.basicAuthMode) {
} }
} }
if (true === cliArguments.ssl) if (true === cliArguments.ssl) {
https.createServer( https.createServer(
{ {
cert: fs.readFileSync(cliArguments.certPath), cert: fs.readFileSync(cliArguments.certPath),
key: fs.readFileSync(cliArguments.keyPath) key: fs.readFileSync(cliArguments.keyPath)
}, app) }, app)
.listen( .listen(
tavernUrl.port || 443, Number(tavernUrl.port) || 443,
tavernUrl.hostname, tavernUrl.hostname,
setupTasks setupTasks
); );
else } else {
http.createServer(app).listen( http.createServer(app).listen(
tavernUrl.port || 80, Number(tavernUrl.port) || 80,
tavernUrl.hostname, tavernUrl.hostname,
setupTasks setupTasks
); );
}
async function convertWebp() { async function convertWebp() {
const files = fs.readdirSync(directories.characters).filter(e => e.endsWith(".webp")); const files = fs.readdirSync(directories.characters).filter(e => e.endsWith(".webp"));