mirror of
https://activitypub.software/TransFem-org/Sharkey.git
synced 2024-12-15 00:55:48 +01:00
Update utils.ts
This commit is contained in:
parent
02ec5b1dbe
commit
2205c61edf
1 changed files with 33 additions and 37 deletions
|
@ -1,14 +1,15 @@
|
||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { dirname } from 'node:path';
|
import { dirname } from 'node:path';
|
||||||
|
import * as childProcess from 'child_process';
|
||||||
|
import * as http from 'node:http';
|
||||||
|
import { SIGKILL } from 'constants';
|
||||||
import * as WebSocket from 'ws';
|
import * as WebSocket from 'ws';
|
||||||
import * as misskey from 'misskey-js';
|
import * as misskey from 'misskey-js';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import FormData from 'form-data';
|
import FormData from 'form-data';
|
||||||
import * as childProcess from 'child_process';
|
import { DataSource } from 'typeorm';
|
||||||
import * as http from 'node:http';
|
|
||||||
import loadConfig from '../src/config/load.js';
|
import loadConfig from '../src/config/load.js';
|
||||||
import { SIGKILL } from 'constants';
|
|
||||||
import { entities } from '../src/db/postgre.js';
|
import { entities } from '../src/db/postgre.js';
|
||||||
|
|
||||||
const _filename = fileURLToPath(import.meta.url);
|
const _filename = fileURLToPath(import.meta.url);
|
||||||
|
@ -27,29 +28,29 @@ export const async = (fn: Function) => (done: Function) => {
|
||||||
|
|
||||||
export const request = async (endpoint: string, params: any, me?: any): Promise<{ body: any, status: number }> => {
|
export const request = async (endpoint: string, params: any, me?: any): Promise<{ body: any, status: number }> => {
|
||||||
const auth = me ? {
|
const auth = me ? {
|
||||||
i: me.token
|
i: me.token,
|
||||||
} : {};
|
} : {};
|
||||||
|
|
||||||
const res = await fetch(`http://localhost:${port}/api${endpoint}`, {
|
const res = await fetch(`http://localhost:${port}/api${endpoint}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
body: JSON.stringify(Object.assign(auth, params))
|
body: JSON.stringify(Object.assign(auth, params)),
|
||||||
});
|
});
|
||||||
|
|
||||||
const status = res.status;
|
const status = res.status;
|
||||||
const body = res.status !== 204 ? await res.json().catch() : null;
|
const body = res.status !== 204 ? await res.json().catch() : null;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
body, status
|
body, status,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const signup = async (params?: any): Promise<any> => {
|
export const signup = async (params?: any): Promise<any> => {
|
||||||
const q = Object.assign({
|
const q = Object.assign({
|
||||||
username: 'test',
|
username: 'test',
|
||||||
password: 'test'
|
password: 'test',
|
||||||
}, params);
|
}, params);
|
||||||
|
|
||||||
const res = await request('/signup', q);
|
const res = await request('/signup', q);
|
||||||
|
@ -59,7 +60,7 @@ export const signup = async (params?: any): Promise<any> => {
|
||||||
|
|
||||||
export const post = async (user: any, params?: misskey.Endpoints['notes/create']['req']): Promise<misskey.entities.Note> => {
|
export const post = async (user: any, params?: misskey.Endpoints['notes/create']['req']): Promise<misskey.entities.Note> => {
|
||||||
const q = Object.assign({
|
const q = Object.assign({
|
||||||
text: 'test'
|
text: 'test',
|
||||||
}, params);
|
}, params);
|
||||||
|
|
||||||
const res = await request('/notes/create', q, user);
|
const res = await request('/notes/create', q, user);
|
||||||
|
@ -70,26 +71,26 @@ export const post = async (user: any, params?: misskey.Endpoints['notes/create']
|
||||||
export const react = async (user: any, note: any, reaction: string): Promise<any> => {
|
export const react = async (user: any, note: any, reaction: string): Promise<any> => {
|
||||||
await request('/notes/reactions/create', {
|
await request('/notes/reactions/create', {
|
||||||
noteId: note.id,
|
noteId: note.id,
|
||||||
reaction: reaction
|
reaction: reaction,
|
||||||
}, user);
|
}, user);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const uploadFile = (user: any, path?: string): Promise<any> => {
|
export const uploadFile = (user: any, path?: string): Promise<any> => {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('i', user.token);
|
formData.append('i', user.token);
|
||||||
formData.append('file', fs.createReadStream(path || _dirname + '/resources/Lenna.png'));
|
formData.append('file', fs.createReadStream(path || _dirname + '/resources/Lenna.png'));
|
||||||
|
|
||||||
return fetch(`http://localhost:${port}/api/drive/files/create`, {
|
return fetch(`http://localhost:${port}/api/drive/files/create`, {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
body: formData,
|
body: formData,
|
||||||
timeout: 30 * 1000,
|
timeout: 30 * 1000,
|
||||||
}).then(res => {
|
}).then(res => {
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
throw `${res.status} ${res.statusText}`;
|
throw `${res.status} ${res.statusText}`;
|
||||||
} else {
|
} else {
|
||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export function connectStream(user: any, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> {
|
export function connectStream(user: any, channel: string, listener: (message: Record<string, any>) => any, params?: any): Promise<WebSocket> {
|
||||||
|
@ -112,8 +113,8 @@ export function connectStream(user: any, channel: string, listener: (message: Re
|
||||||
channel: channel,
|
channel: channel,
|
||||||
id: 'a',
|
id: 'a',
|
||||||
pong: true,
|
pong: true,
|
||||||
params: params
|
params: params,
|
||||||
}
|
},
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -124,8 +125,8 @@ export const simpleGet = async (path: string, accept = '*/*'): Promise<{ status?
|
||||||
return await new Promise((resolve, reject) => {
|
return await new Promise((resolve, reject) => {
|
||||||
const req = http.request(`http://localhost:${port}${path}`, {
|
const req = http.request(`http://localhost:${port}${path}`, {
|
||||||
headers: {
|
headers: {
|
||||||
Accept: accept
|
Accept: accept,
|
||||||
}
|
},
|
||||||
}, res => {
|
}, res => {
|
||||||
if (res.statusCode! >= 400) {
|
if (res.statusCode! >= 400) {
|
||||||
reject(res);
|
reject(res);
|
||||||
|
@ -146,7 +147,7 @@ export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProce
|
||||||
return (done: (err?: Error) => any) => {
|
return (done: (err?: Error) => any) => {
|
||||||
const p = childProcess.spawn('node', [_dirname + '/../index.js'], {
|
const p = childProcess.spawn('node', [_dirname + '/../index.js'], {
|
||||||
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
||||||
env: { NODE_ENV: 'test', PATH: process.env.PATH }
|
env: { NODE_ENV: 'test', PATH: process.env.PATH },
|
||||||
});
|
});
|
||||||
callbackSpawnedProcess(p);
|
callbackSpawnedProcess(p);
|
||||||
p.on('message', message => {
|
p.on('message', message => {
|
||||||
|
@ -158,12 +159,7 @@ export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProce
|
||||||
export async function initTestDb(justBorrow = false, initEntities?: any[]) {
|
export async function initTestDb(justBorrow = false, initEntities?: any[]) {
|
||||||
if (process.env.NODE_ENV !== 'test') throw 'NODE_ENV is not a test';
|
if (process.env.NODE_ENV !== 'test') throw 'NODE_ENV is not a test';
|
||||||
|
|
||||||
try {
|
return new DataSource({
|
||||||
const conn = await getConnection();
|
|
||||||
await conn.close();
|
|
||||||
} catch (e) {}
|
|
||||||
|
|
||||||
return await createConnection({
|
|
||||||
type: 'postgres',
|
type: 'postgres',
|
||||||
host: config.db.host,
|
host: config.db.host,
|
||||||
port: config.db.port,
|
port: config.db.port,
|
||||||
|
@ -172,7 +168,7 @@ export async function initTestDb(justBorrow = false, initEntities?: any[]) {
|
||||||
database: config.db.db,
|
database: config.db.db,
|
||||||
synchronize: true && !justBorrow,
|
synchronize: true && !justBorrow,
|
||||||
dropSchema: true && !justBorrow,
|
dropSchema: true && !justBorrow,
|
||||||
entities: initEntities || entities
|
entities: initEntities || entities,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,7 +181,7 @@ export function startServer(timeout = 30 * 1000): Promise<childProcess.ChildProc
|
||||||
|
|
||||||
const p = childProcess.spawn('node', [_dirname + '/../built/index.js'], {
|
const p = childProcess.spawn('node', [_dirname + '/../built/index.js'], {
|
||||||
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
||||||
env: { NODE_ENV: 'test', PATH: process.env.PATH }
|
env: { NODE_ENV: 'test', PATH: process.env.PATH },
|
||||||
});
|
});
|
||||||
|
|
||||||
p.on('error', e => rej(e));
|
p.on('error', e => rej(e));
|
||||||
|
|
Loading…
Reference in a new issue