mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-02-12 10:00:36 +01:00
Remove HTTP/2 workaround for pygsite
This commit is contained in:
parent
e4a48cd28f
commit
0391179c3c
@ -220,76 +220,25 @@ async function downloadChubCharacter(id) {
|
|||||||
return { buffer, fileName, fileType };
|
return { buffer, fileName, fileType };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Makes an HTTP/2 request to the specified endpoint.
|
|
||||||
*
|
|
||||||
* THIS IS A WORKAROUND FOR NODE-FETCH NOT SUPPORTING HTTP/2 AND PYGSITE USING BROKEN AHH AWS LOAD BALANCER.
|
|
||||||
* @param {string} endpoint URL to make the request to
|
|
||||||
* @param {string} method HTTP method to use
|
|
||||||
* @param {string} body Request body
|
|
||||||
* @param {object} headers Request headers
|
|
||||||
* @returns {Promise<string>} Response body
|
|
||||||
*/
|
|
||||||
function makeHttp2Request(endpoint, method, body, headers) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
try {
|
|
||||||
const http2 = require('http2');
|
|
||||||
const url = new URL(endpoint);
|
|
||||||
const client = http2.connect(url.origin);
|
|
||||||
|
|
||||||
const req = client.request({
|
|
||||||
':method': method,
|
|
||||||
':path': url.pathname,
|
|
||||||
...headers,
|
|
||||||
});
|
|
||||||
req.setEncoding('utf8');
|
|
||||||
|
|
||||||
req.on('response', (headers) => {
|
|
||||||
const status = Number(headers[':status']);
|
|
||||||
|
|
||||||
if (status < 200 || status >= 300) {
|
|
||||||
reject(new Error(`Request failed with status ${status}`));
|
|
||||||
}
|
|
||||||
|
|
||||||
let data = '';
|
|
||||||
|
|
||||||
req.on('data', (chunk) => {
|
|
||||||
data += chunk;
|
|
||||||
});
|
|
||||||
|
|
||||||
req.on('end', () => {
|
|
||||||
resolve(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
req.on('error', (err) => {
|
|
||||||
reject(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (body) {
|
|
||||||
req.write(body);
|
|
||||||
}
|
|
||||||
|
|
||||||
req.end();
|
|
||||||
} catch (e) {
|
|
||||||
reject(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads a character card from the Pygsite.
|
* Downloads a character card from the Pygsite.
|
||||||
* @param {string} id UUID of the character
|
* @param {string} id UUID of the character
|
||||||
* @returns {Promise<{buffer: Buffer, fileName: string, fileType: string}>}
|
* @returns {Promise<{buffer: Buffer, fileName: string, fileType: string}>}
|
||||||
*/
|
*/
|
||||||
async function downloadPygmalionCharacter(id) {
|
async function downloadPygmalionCharacter(id) {
|
||||||
const result = await makeHttp2Request(
|
const result = await fetch('https://server.pygmalion.chat/galatea.v1.PublicCharacterService/CharacterExport', {
|
||||||
'https://server.pygmalion.chat/galatea.v1.PublicCharacterService/CharacterExport',
|
method: 'POST',
|
||||||
'POST',
|
headers: { 'Content-Type': 'application/json' },
|
||||||
JSON.stringify({ 'character_id': id }),
|
body: JSON.stringify({ 'character_id': id }),
|
||||||
{ 'content-type': 'application/json' },
|
});
|
||||||
);
|
|
||||||
const jsonData = JSON.parse(result);
|
if (!result.ok) {
|
||||||
|
const text = await result.text();
|
||||||
|
console.log('Pygsite returned error', result.status, text);
|
||||||
|
throw new Error('Failed to download character');
|
||||||
|
}
|
||||||
|
|
||||||
|
const jsonData = await result.json();
|
||||||
const card = jsonData?.card;
|
const card = jsonData?.card;
|
||||||
|
|
||||||
if (!card || typeof card !== 'object') {
|
if (!card || typeof card !== 'object') {
|
||||||
|
61
src/util.js
61
src/util.js
@ -365,7 +365,7 @@ function getImages(path) {
|
|||||||
/**
|
/**
|
||||||
* Pipe a fetch() response to an Express.js Response, including status code.
|
* Pipe a fetch() response to an Express.js Response, including status code.
|
||||||
* @param {import('node-fetch').Response} from The Fetch API response to pipe from.
|
* @param {import('node-fetch').Response} from The Fetch API response to pipe from.
|
||||||
* @param {Express.Response} to The Express response to pipe to.
|
* @param {import('express').Response} to The Express response to pipe to.
|
||||||
*/
|
*/
|
||||||
function forwardFetchResponse(from, to) {
|
function forwardFetchResponse(from, to) {
|
||||||
let statusCode = from.status;
|
let statusCode = from.status;
|
||||||
@ -399,6 +399,64 @@ function forwardFetchResponse(from, to) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Makes an HTTP/2 request to the specified endpoint.
|
||||||
|
*
|
||||||
|
* @deprecated Use `node-fetch` if possible.
|
||||||
|
* @param {string} endpoint URL to make the request to
|
||||||
|
* @param {string} method HTTP method to use
|
||||||
|
* @param {string} body Request body
|
||||||
|
* @param {object} headers Request headers
|
||||||
|
* @returns {Promise<string>} Response body
|
||||||
|
*/
|
||||||
|
function makeHttp2Request(endpoint, method, body, headers) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
try {
|
||||||
|
const http2 = require('http2');
|
||||||
|
const url = new URL(endpoint);
|
||||||
|
const client = http2.connect(url.origin);
|
||||||
|
|
||||||
|
const req = client.request({
|
||||||
|
':method': method,
|
||||||
|
':path': url.pathname,
|
||||||
|
...headers,
|
||||||
|
});
|
||||||
|
req.setEncoding('utf8');
|
||||||
|
|
||||||
|
req.on('response', (headers) => {
|
||||||
|
const status = Number(headers[':status']);
|
||||||
|
|
||||||
|
if (status < 200 || status >= 300) {
|
||||||
|
reject(new Error(`Request failed with status ${status}`));
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = '';
|
||||||
|
|
||||||
|
req.on('data', (chunk) => {
|
||||||
|
data += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on('end', () => {
|
||||||
|
console.log(data);
|
||||||
|
resolve(data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
req.on('error', (err) => {
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (body) {
|
||||||
|
req.write(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
req.end();
|
||||||
|
} catch (e) {
|
||||||
|
reject(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds YAML-serialized object to the object.
|
* Adds YAML-serialized object to the object.
|
||||||
* @param {object} obj Object
|
* @param {object} obj Object
|
||||||
@ -547,4 +605,5 @@ module.exports = {
|
|||||||
excludeKeysByYaml,
|
excludeKeysByYaml,
|
||||||
trimV1,
|
trimV1,
|
||||||
Cache,
|
Cache,
|
||||||
|
makeHttp2Request,
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user