2022-02-27 04:07:39 +02:00
|
|
|
import * as fs from 'node:fs';
|
2022-06-26 13:16:32 +03:00
|
|
|
import * as path from 'node:path';
|
2022-05-14 10:09:47 +03:00
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { dirname } from 'node:path';
|
2022-05-21 11:40:43 +03:00
|
|
|
import * as childProcess from 'child_process';
|
|
|
|
import * as http from 'node:http';
|
|
|
|
import { SIGKILL } from 'constants';
|
2022-06-26 13:16:32 +03:00
|
|
|
import WebSocket from 'ws';
|
2020-04-09 17:42:23 +03:00
|
|
|
import fetch from 'node-fetch';
|
2022-02-27 04:07:39 +02:00
|
|
|
import FormData from 'form-data';
|
2022-05-21 11:40:43 +03:00
|
|
|
import { DataSource } from 'typeorm';
|
2022-09-17 21:27:08 +03:00
|
|
|
import got, { RequestError } from 'got';
|
2022-02-27 04:07:39 +02:00
|
|
|
import loadConfig from '../src/config/load.js';
|
2023-02-07 12:50:38 +02:00
|
|
|
import { entities } from '@/postgres.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type * as misskey from 'misskey-js';
|
2021-03-12 16:10:15 +02:00
|
|
|
|
2022-05-14 10:09:47 +03:00
|
|
|
const _filename = fileURLToPath(import.meta.url);
|
|
|
|
const _dirname = dirname(_filename);
|
|
|
|
|
2021-06-12 16:40:17 +03:00
|
|
|
const config = loadConfig();
|
|
|
|
export const port = config.port;
|
2019-01-23 05:15:27 +02:00
|
|
|
|
2022-06-26 13:16:32 +03:00
|
|
|
export const api = async (endpoint: string, params: any, me?: any) => {
|
|
|
|
endpoint = endpoint.replace(/^\//, '');
|
|
|
|
|
|
|
|
const auth = me ? {
|
2022-09-17 21:27:08 +03:00
|
|
|
i: me.token,
|
2022-06-26 13:16:32 +03:00
|
|
|
} : {};
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
try {
|
2023-02-22 11:00:35 +02:00
|
|
|
const res = await got<string>(`http://localhost:${port}/api/${endpoint}`, {
|
2022-09-17 21:27:08 +03:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(Object.assign(auth, params)),
|
|
|
|
retry: {
|
|
|
|
limit: 0,
|
|
|
|
},
|
|
|
|
});
|
2022-06-26 13:16:32 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
const status = res.statusCode;
|
|
|
|
const body = res.statusCode !== 204 ? await JSON.parse(res.body) : null;
|
|
|
|
|
|
|
|
return {
|
|
|
|
status,
|
|
|
|
body,
|
|
|
|
};
|
|
|
|
} catch (err: unknown) {
|
|
|
|
if (err instanceof RequestError && err.response) {
|
|
|
|
const status = err.response.statusCode;
|
|
|
|
const body = await JSON.parse(err.response.body as string);
|
|
|
|
|
|
|
|
return {
|
|
|
|
status,
|
|
|
|
body,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2022-06-26 13:16:32 +03:00
|
|
|
};
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
export const request = async (path: string, params: any, me?: any): Promise<{ body: any, status: number }> => {
|
2019-01-23 05:15:27 +02:00
|
|
|
const auth = me ? {
|
2022-05-21 11:40:43 +03:00
|
|
|
i: me.token,
|
2019-01-23 05:15:27 +02:00
|
|
|
} : {};
|
|
|
|
|
2023-02-22 11:00:35 +02:00
|
|
|
const res = await fetch(`http://localhost:${port}/${path}`, {
|
2021-03-12 16:10:15 +02:00
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
2022-05-21 11:40:43 +03:00
|
|
|
'Content-Type': 'application/json',
|
2021-03-12 16:10:15 +02:00
|
|
|
},
|
2022-05-21 11:40:43 +03:00
|
|
|
body: JSON.stringify(Object.assign(auth, params)),
|
2021-03-12 16:10:15 +02:00
|
|
|
});
|
2019-01-23 05:15:27 +02:00
|
|
|
|
2021-03-12 16:10:15 +02:00
|
|
|
const status = res.status;
|
2022-09-17 21:27:08 +03:00
|
|
|
const body = res.status === 200 ? await res.json().catch() : null;
|
2021-03-12 16:10:15 +02:00
|
|
|
|
|
|
|
return {
|
2022-05-21 11:40:43 +03:00
|
|
|
body, status,
|
2021-03-12 16:10:15 +02:00
|
|
|
};
|
2019-01-23 05:15:27 +02:00
|
|
|
};
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
export const signup = async (params?: any): Promise<any> => {
|
2019-01-23 05:15:27 +02:00
|
|
|
const q = Object.assign({
|
|
|
|
username: 'test',
|
2022-05-21 11:40:43 +03:00
|
|
|
password: 'test',
|
2019-01-23 05:15:27 +02:00
|
|
|
}, params);
|
|
|
|
|
2022-06-26 13:16:32 +03:00
|
|
|
const res = await api('signup', q);
|
2019-01-23 05:15:27 +02:00
|
|
|
|
|
|
|
return res.body;
|
|
|
|
};
|
|
|
|
|
2021-10-31 08:30:22 +02:00
|
|
|
export const post = async (user: any, params?: misskey.Endpoints['notes/create']['req']): Promise<misskey.entities.Note> => {
|
2019-01-23 05:15:27 +02:00
|
|
|
const q = Object.assign({
|
2022-05-21 11:40:43 +03:00
|
|
|
text: 'test',
|
2019-01-23 05:15:27 +02:00
|
|
|
}, params);
|
|
|
|
|
2022-06-26 13:16:32 +03:00
|
|
|
const res = await api('notes/create', q, user);
|
2019-01-23 05:15:27 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
return res.body ? res.body.createdNote : null;
|
2019-01-23 05:15:27 +02:00
|
|
|
};
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
export const react = async (user: any, note: any, reaction: string): Promise<any> => {
|
2022-06-26 13:16:32 +03:00
|
|
|
await api('notes/reactions/create', {
|
2019-01-23 05:15:27 +02:00
|
|
|
noteId: note.id,
|
2022-05-21 11:40:43 +03:00
|
|
|
reaction: reaction,
|
2019-01-23 05:15:27 +02:00
|
|
|
}, user);
|
|
|
|
};
|
|
|
|
|
2022-06-26 13:16:32 +03:00
|
|
|
/**
|
|
|
|
* Upload file
|
|
|
|
* @param user User
|
|
|
|
* @param _path Optional, absolute path or relative from ./resources/
|
|
|
|
*/
|
|
|
|
export const uploadFile = async (user: any, _path?: string): Promise<any> => {
|
|
|
|
const absPath = _path == null ? `${_dirname}/resources/Lenna.jpg` : path.isAbsolute(_path) ? _path : `${_dirname}/resources/${_path}`;
|
|
|
|
|
|
|
|
const formData = new FormData() as any;
|
2022-05-21 11:40:43 +03:00
|
|
|
formData.append('i', user.token);
|
2022-06-26 13:16:32 +03:00
|
|
|
formData.append('file', fs.createReadStream(absPath));
|
|
|
|
formData.append('force', 'true');
|
2022-05-21 11:40:43 +03:00
|
|
|
|
2023-02-22 11:00:35 +02:00
|
|
|
const res = await got<string>(`http://localhost:${port}/api/drive/files/create`, {
|
2022-06-26 13:16:32 +03:00
|
|
|
method: 'POST',
|
2022-05-21 11:40:43 +03:00
|
|
|
body: formData,
|
2022-06-26 13:16:32 +03:00
|
|
|
retry: {
|
|
|
|
limit: 0,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const body = res.statusCode !== 204 ? await JSON.parse(res.body) : null;
|
|
|
|
|
|
|
|
return body;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const uploadUrl = async (user: any, url: string) => {
|
|
|
|
let file: any;
|
2022-09-17 21:27:08 +03:00
|
|
|
const marker = Math.random().toString();
|
2022-06-26 13:16:32 +03:00
|
|
|
|
|
|
|
const ws = await connectStream(user, 'main', (msg) => {
|
2022-09-17 21:27:08 +03:00
|
|
|
if (msg.type === 'urlUploadFinished' && msg.body.marker === marker) {
|
|
|
|
file = msg.body.file;
|
2022-05-21 11:40:43 +03:00
|
|
|
}
|
|
|
|
});
|
2022-06-26 13:16:32 +03:00
|
|
|
|
|
|
|
await api('drive/files/upload-from-url', {
|
|
|
|
url,
|
2022-09-17 21:27:08 +03:00
|
|
|
marker,
|
2022-06-26 13:16:32 +03:00
|
|
|
force: true,
|
|
|
|
}, user);
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
await sleep(7000);
|
2022-06-26 13:16:32 +03:00
|
|
|
ws.close();
|
|
|
|
|
|
|
|
return file;
|
2020-04-09 17:42:23 +03:00
|
|
|
};
|
2019-01-23 05:15:27 +02:00
|
|
|
|
2019-04-18 08:29:17 +03:00
|
|
|
export function connectStream(user: any, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> {
|
2019-04-07 15:50:36 +03:00
|
|
|
return new Promise((res, rej) => {
|
2023-02-22 11:00:35 +02:00
|
|
|
const ws = new WebSocket(`ws://localhost:${port}/streaming?i=${user.token}`);
|
2019-01-23 05:15:27 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
ws.on('open', () => {
|
|
|
|
ws.on('message', data => {
|
|
|
|
const msg = JSON.parse(data.toString());
|
2022-05-25 17:19:39 +03:00
|
|
|
if (msg.type === 'channel' && msg.body.id === 'a') {
|
2019-04-07 15:50:36 +03:00
|
|
|
listener(msg.body);
|
2022-05-25 17:19:39 +03:00
|
|
|
} else if (msg.type === 'connected' && msg.body.id === 'a') {
|
2019-04-07 15:50:36 +03:00
|
|
|
res(ws);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
type: 'connect',
|
|
|
|
body: {
|
|
|
|
channel: channel,
|
|
|
|
id: 'a',
|
|
|
|
pong: true,
|
2022-05-21 11:40:43 +03:00
|
|
|
params: params,
|
|
|
|
},
|
2019-04-07 15:50:36 +03:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-01-09 07:35:04 +02:00
|
|
|
|
2022-07-03 14:54:54 +03:00
|
|
|
export const waitFire = async (user: any, channel: string, trgr: () => any, cond: (msg: Record<string, any>) => boolean, params?: any) => {
|
2022-06-26 13:16:32 +03:00
|
|
|
return new Promise<boolean>(async (res, rej) => {
|
|
|
|
let timer: NodeJS.Timeout;
|
|
|
|
|
|
|
|
let ws: WebSocket;
|
|
|
|
try {
|
|
|
|
ws = await connectStream(user, channel, msg => {
|
|
|
|
if (cond(msg)) {
|
|
|
|
ws.close();
|
|
|
|
if (timer) clearTimeout(timer);
|
|
|
|
res(true);
|
|
|
|
}
|
2022-07-03 14:54:54 +03:00
|
|
|
}, params);
|
2022-06-26 13:16:32 +03:00
|
|
|
} catch (e) {
|
|
|
|
rej(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ws!) return;
|
|
|
|
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
ws.close();
|
|
|
|
res(false);
|
2022-07-03 14:54:54 +03:00
|
|
|
}, 3000);
|
2022-06-26 13:16:32 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
await trgr();
|
|
|
|
} catch (e) {
|
|
|
|
ws.close();
|
|
|
|
if (timer) clearTimeout(timer);
|
|
|
|
rej(e);
|
|
|
|
}
|
2022-09-17 21:27:08 +03:00
|
|
|
});
|
2022-06-26 13:16:32 +03:00
|
|
|
};
|
|
|
|
|
2021-03-13 16:22:54 +02:00
|
|
|
export const simpleGet = async (path: string, accept = '*/*'): Promise<{ status?: number, type?: string, location?: string }> => {
|
2021-03-12 16:10:15 +02:00
|
|
|
// node-fetchだと3xxを取れない
|
|
|
|
return await new Promise((resolve, reject) => {
|
2023-02-22 11:00:35 +02:00
|
|
|
const req = http.request(`http://localhost:${port}${path}`, {
|
2021-03-12 16:10:15 +02:00
|
|
|
headers: {
|
2022-05-21 11:40:43 +03:00
|
|
|
Accept: accept,
|
|
|
|
},
|
2021-03-12 16:10:15 +02:00
|
|
|
}, res => {
|
|
|
|
if (res.statusCode! >= 400) {
|
|
|
|
reject(res);
|
|
|
|
} else {
|
|
|
|
resolve({
|
|
|
|
status: res.statusCode,
|
|
|
|
type: res.headers['content-type'],
|
|
|
|
location: res.headers.location,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
req.end();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-01-09 07:35:04 +02:00
|
|
|
export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProcess) => void, moreProcess: () => Promise<void> = async () => {}) {
|
|
|
|
return (done: (err?: Error) => any) => {
|
2022-05-14 10:09:47 +03:00
|
|
|
const p = childProcess.spawn('node', [_dirname + '/../index.js'], {
|
2020-01-09 07:35:04 +02:00
|
|
|
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
2022-05-21 11:40:43 +03:00
|
|
|
env: { NODE_ENV: 'test', PATH: process.env.PATH },
|
2020-01-09 07:35:04 +02:00
|
|
|
});
|
|
|
|
callbackSpawnedProcess(p);
|
|
|
|
p.on('message', message => {
|
|
|
|
if (message === 'ok') moreProcess().then(() => done()).catch(e => done(e));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2021-06-05 08:54:07 +03:00
|
|
|
|
2021-06-12 16:40:17 +03:00
|
|
|
export async function initTestDb(justBorrow = false, initEntities?: any[]) {
|
|
|
|
if (process.env.NODE_ENV !== 'test') throw 'NODE_ENV is not a test';
|
|
|
|
|
2022-05-21 16:07:01 +03:00
|
|
|
const db = new DataSource({
|
2021-06-12 16:40:17 +03:00
|
|
|
type: 'postgres',
|
|
|
|
host: config.db.host,
|
|
|
|
port: config.db.port,
|
|
|
|
username: config.db.user,
|
|
|
|
password: config.db.pass,
|
|
|
|
database: config.db.db,
|
|
|
|
synchronize: true && !justBorrow,
|
|
|
|
dropSchema: true && !justBorrow,
|
2022-09-17 21:27:08 +03:00
|
|
|
entities: initEntities ?? entities,
|
2021-06-12 16:40:17 +03:00
|
|
|
});
|
2022-05-21 16:07:01 +03:00
|
|
|
|
|
|
|
await db.initialize();
|
|
|
|
|
|
|
|
return db;
|
2021-06-12 16:40:17 +03:00
|
|
|
}
|
|
|
|
|
2022-06-26 13:16:32 +03:00
|
|
|
export function startServer(timeout = 60 * 1000): Promise<childProcess.ChildProcess> {
|
2021-06-12 16:40:17 +03:00
|
|
|
return new Promise((res, rej) => {
|
|
|
|
const t = setTimeout(() => {
|
|
|
|
p.kill(SIGKILL);
|
|
|
|
rej('timeout to start');
|
|
|
|
}, timeout);
|
|
|
|
|
2022-05-14 10:09:47 +03:00
|
|
|
const p = childProcess.spawn('node', [_dirname + '/../built/index.js'], {
|
2021-06-12 16:40:17 +03:00
|
|
|
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
2022-05-21 11:40:43 +03:00
|
|
|
env: { NODE_ENV: 'test', PATH: process.env.PATH },
|
2021-06-12 16:40:17 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
p.on('error', e => rej(e));
|
|
|
|
|
|
|
|
p.on('message', message => {
|
|
|
|
if (message === 'ok') {
|
|
|
|
clearTimeout(t);
|
|
|
|
res(p);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
export function shutdownServer(p: childProcess.ChildProcess | undefined, timeout = 20 * 1000) {
|
|
|
|
if (p == null) return Promise.resolve('nop');
|
2021-06-05 08:54:07 +03:00
|
|
|
return new Promise((res, rej) => {
|
|
|
|
const t = setTimeout(() => {
|
|
|
|
p.kill(SIGKILL);
|
|
|
|
res('force exit');
|
|
|
|
}, timeout);
|
|
|
|
|
|
|
|
p.once('exit', () => {
|
|
|
|
clearTimeout(t);
|
|
|
|
res('exited');
|
|
|
|
});
|
|
|
|
|
|
|
|
p.kill();
|
|
|
|
});
|
|
|
|
}
|
2022-06-26 13:16:32 +03:00
|
|
|
|
|
|
|
export function sleep(msec: number) {
|
|
|
|
return new Promise<void>(res => {
|
|
|
|
setTimeout(() => {
|
|
|
|
res();
|
|
|
|
}, msec);
|
|
|
|
});
|
|
|
|
}
|