upgrade node dependencies, replace got with fetch

master
lauren n. liberda 2023-04-21 17:39:44 +02:00
parent 7e885720da
commit be7ad262e5
No known key found for this signature in database
GPG Key ID: 734C629FD04BD319
3 changed files with 1032 additions and 1242 deletions

View File

@ -11,22 +11,21 @@
"dev": "ts-node src/index.ts"
},
"dependencies": {
"@koa/router": "^10.0.0",
"got": "^11.8.2",
"koa": "^2.13.1",
"sharp": "^0.28.2"
"@koa/router": "^12.0.0",
"koa": "^2.14.2",
"sharp": "^0.32.0"
},
"devDependencies": {
"@types/koa": "^2.13.1",
"@types/koa__router": "^8.0.4",
"@types/sharp": "^0.28.1",
"@typescript-eslint/eslint-plugin": "^4.23.0",
"@typescript-eslint/parser": "^4.23.0",
"eslint": "^7.26.0",
"eslint-config-airbnb-typescript": "^12.3.1",
"eslint-config-prettier": "^8.3.0",
"prettier": "^2.3.0",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
"@types/koa": "^2.13.6",
"@types/koa__router": "^12.0.0",
"@types/sharp": "^0.31.1",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"eslint": "^8.38.0",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.8.0",
"prettier": "^2.8.7",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
}
}

View File

@ -1,6 +1,5 @@
import Koa from 'koa';
import Router from '@koa/router';
import Got, { Response } from 'got';
import sharp from 'sharp';
import dns from 'dns/promises';
import assert from 'assert';
@ -10,15 +9,14 @@ assert(DEST_SERVER, 'missing DEST_SERVER env variable');
assert(SERVER_PRETTY_NAME, 'missing SERVER_PRETTY_NAME env variable');
const IS_PRODUCTION = process.env.NODE_ENV === 'production';
const FUCKED_UP_SERVERS = ['matrix.org'];
const got = Got.extend({
headers: {
'User-Agent': 'why-is-synapse (https://git.sakamoto.pl/selfisekai/why-is-synapse)',
},
throwHttpErrors: false,
hooks: {
beforeRequest: IS_PRODUCTION ? [] : [(req) => console.log(`${req.method} ${req.url.href}`)],
},
});
const defaultHeaders = {
'User-Agent': 'why-is-synapse (https://git.sakamoto.pl/selfisekai/why-is-synapse)',
};
const existValueOrNoExistKey = (obj: Record<string, any>): Record<string, string> =>
Object.fromEntries(
Object.entries(obj).filter(([, val]) => typeof val === 'string') as [string, string][],
);
const serverHostnameCache = new Map<string, string>();
@ -26,9 +24,11 @@ const getServerHostname = async (serverName: string) => {
const cached = serverHostnameCache.get(serverName);
if (cached) return cached;
const jsonRes = await got.get(`https://${serverName}/.well-known/matrix/server`);
if (jsonRes.statusCode === 200) {
let res = JSON.parse(jsonRes.body)['m.server'];
const jsonRes = await fetch(`https://${serverName}/.well-known/matrix/server`, {
headers: defaultHeaders,
});
if (jsonRes.status === 200) {
let res = (await jsonRes.json())['m.server'];
if (typeof res === 'string') {
if (!res.includes(':')) {
res += ':8448'; // default port per spec
@ -62,7 +62,7 @@ media.get(
async (ctx) => {
const { serverName, mediaId, filename } = ctx.params;
function sendFile(f: Response<Buffer>) {
function sendFile(f: Response) {
ctx.res.statusCode = 200;
(
['content-type', filename ? 'content-disposition' : null].filter(
@ -70,7 +70,7 @@ media.get(
) as string[]
).forEach((header) => {
if (!f) return; // typescript is bork
const val = f.headers[header];
const val = f.headers.get(header);
if (!val) return;
ctx.res.setHeader(header, val);
});
@ -81,15 +81,17 @@ media.get(
ctx.res.end();
}
let file: Response<Buffer> | null = null;
let file: Response | null = null;
if (!FUCKED_UP_SERVERS.includes(serverName)) {
file = await got.get(`${DEST_SERVER}/_matrix/media/r0/download/${serverName}/${mediaId}`, {
responseType: 'buffer',
file = await fetch(`${DEST_SERVER}/_matrix/media/r0/download/${serverName}/${mediaId}`, {
headers: {
Authorization: ctx.headers['authorization'],
...defaultHeaders,
...existValueOrNoExistKey({
Authorization: ctx.headers['authorization'],
}),
},
});
if (file && file.statusCode === 200) {
if (file && file.status === 200) {
sendFile(file);
return;
}
@ -101,13 +103,10 @@ media.get(
}
const remoteHost = await getServerHostname(serverName);
file = await got.get(
`https://${remoteHost}/_matrix/media/r0/download/${serverName}/${mediaId}`,
{
responseType: 'buffer',
},
);
if (file && file.statusCode === 200) {
file = await fetch(`https://${remoteHost}/_matrix/media/r0/download/${serverName}/${mediaId}`, {
headers: defaultHeaders,
});
if (file && file.status === 200) {
sendFile(file);
return;
}
@ -128,31 +127,30 @@ media.get('/thumbnail/:serverName/:mediaId', async (ctx) => {
const width = parseInt(ctx.query.width, 10);
const height = parseInt(ctx.query.height, 10);
let file: Response<Buffer> | null = null;
let file: Response | null = null;
if (!FUCKED_UP_SERVERS.includes(serverName)) {
file = await got.get(`${DEST_SERVER}/_matrix/media/r0/download/${serverName}/${mediaId}`, {
responseType: 'buffer',
file = await fetch(`${DEST_SERVER}/_matrix/media/r0/download/${serverName}/${mediaId}`, {
headers: defaultHeaders,
});
}
if (!file || file.statusCode !== 200) {
if (!file || file.status !== 200) {
if (serverName === SERVER_PRETTY_NAME) {
ctx.res.statusCode = 500;
return;
}
const remoteHost = await getServerHostname(serverName);
file = await got.get(
`https://${remoteHost}/_matrix/media/r0/download/${serverName}/${mediaId}`,
{
responseType: 'buffer',
headers: {
file = await fetch(`https://${remoteHost}/_matrix/media/r0/download/${serverName}/${mediaId}`, {
headers: {
...defaultHeaders,
...existValueOrNoExistKey({
Authorization: ctx.headers['authorization'],
},
}),
},
);
});
}
if (file && file.statusCode === 200) {
const resizer = sharp(file.body).resize(width, height, {
if (file && file.status === 200) {
const resizer = sharp(Buffer.from(await file.arrayBuffer())).resize(width, height, {
fit: method === 'crop' ? 'cover' : 'inside',
});
const img = await resizer.toBuffer({ resolveWithObject: true });

2167
yarn.lock

File diff suppressed because it is too large Load Diff