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