Fix and remove some scripts + use ajv/typescript-json-schema required by patches

This commit is contained in:
Madeline 2022-09-26 13:23:05 +10:00
parent 4cdb547f76
commit 5bcfa5ec0b
11 changed files with 12 additions and 384 deletions

Binary file not shown.

View File

@ -3,6 +3,9 @@
{
"path": "src"
},
{
"path": "src-slowcord"
},
{
"path": "assets"
},

BIN
package-lock.json generated

Binary file not shown.

View File

@ -3,11 +3,13 @@
"version": "1.0.0",
"description": "A Fosscord server written in Node.js",
"scripts": {
"postinstall": "npx patch-package",
"start": "node dist/bundle/start.js",
"build": "tsc -p ."
"build": "tsc -p .",
"generate:rights": "node scripts/rights.js"
},
"main": "dist/bundle/start.js",
"types": "src/bundle/index.js",
"main": "dist/bundle/index.js",
"types": "src/bundle/index.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/fosscord/fosscord-server.git"
@ -39,7 +41,7 @@
"typescript": "^4.8.3"
},
"dependencies": {
"ajv": "^8.11.0",
"ajv": "^8.6.2",
"ajv-formats": "^2.1.1",
"bcrypt": "^5.0.1",
"cheerio": "^1.0.0-rc.12",
@ -67,6 +69,7 @@
"sharp": "^0.31.0",
"sqlite3": "^5.1.1",
"typeorm": "^0.3.10",
"typescript-json-schema": "^0.50.1",
"ws": "^8.9.0"
},
"optionalDependencies": {

View File

@ -1,72 +0,0 @@
const { execSync } = require("child_process");
const path = require("path");
const fs = require("fs");
const { getSystemErrorMap } = require("util");
const { argv } = require("process");
var steps = 2, i = 0;
if (argv.includes("clean")) steps++;
if (argv.includes("copyonly")) steps--;
const dirs = ["api", "util", "cdn", "gateway", "bundle"];
const verbose = argv.includes("verbose") || argv.includes("v");
var copyRecursiveSync = function(src, dest) {
if(verbose) console.log(`cpsync: ${src} -> ${dest}`);
var exists = fs.existsSync(src);
if(!exists){
console.log(src + " doesn't exist, not copying!");
return;
}
var stats = exists && fs.statSync(src);
var isDirectory = exists && stats.isDirectory();
if (isDirectory) {
fs.mkdirSync(dest, {recursive: true});
fs.readdirSync(src).forEach(function(childItemName) {
copyRecursiveSync(path.join(src, childItemName),
path.join(dest, childItemName));
});
} else {
fs.copyFileSync(src, dest);
}
};
if (argv.includes("clean")) {
console.log(`[${++i}/${steps}] Cleaning...`);
dirs.forEach((a) => {
var d = "../" + a + "/dist";
if (fs.existsSync(d)) {
fs.rmSync(d, { recursive: true });
if (verbose) console.log(`Deleted ${d}!`);
}
});
}
console.log(`[${++i}/${steps}] Copying src files...`);
copyRecursiveSync(path.join(__dirname, "..", "..", "api", "assets"), path.join(__dirname, "..", "dist", "api", "assets"));
copyRecursiveSync(path.join(__dirname, "..", "..", "api", "client_test"), path.join(__dirname, "..", "dist", "api", "client_test"));
copyRecursiveSync(path.join(__dirname, "..", "..", "api", "locales"), path.join(__dirname, "..", "dist", "api", "locales"));
dirs.forEach((a) => {
copyRecursiveSync("../" + a + "/src", "dist/" + a + "/src");
if (verbose) console.log(`Copied ${"../" + a + "/dist"} -> ${"dist/" + a + "/src"}!`);
});
if (!argv.includes("copyonly")) {
console.log(`[${++i}/${steps}] Compiling src files ...`);
console.log(
execSync(
'node "' +
path.join(__dirname, "..", "node_modules", "typescript", "lib", "tsc.js") +
'" -p "' +
path.join(__dirname, "..") +
'"',
{
cwd: path.join(__dirname, ".."),
shell: true,
env: process.env,
encoding: "utf8"
}
)
);
}

View File

@ -1,31 +0,0 @@
DROP TABLE applications;
DROP TABLE attachments;
DROP TABLE audit_logs;
DROP TABLE bans;
DROP TABLE connected_accounts;
DROP TABLE emojis;
DROP TABLE invites;
DROP TABLE member_roles;
DROP TABLE message_channel_mentions;
DROP TABLE message_role_mentions;
DROP TABLE message_stickers;
DROP TABLE message_user_mentions;
DROP TABLE messages;
DROP TABLE rate_limits;
DROP TABLE read_states;
DROP TABLE recipients;
DROP TABLE relationships;
DROP TABLE roles;
DROP TABLE sessions;
DROP TABLE stickers;
DROP TABLE team_members;
DROP TABLE teams;
DROP TABLE templates;
DROP TABLE voice_states;
DROP TABLE webhooks;
DROP TABLE channels;
DROP TABLE members;
DROP TABLE guilds;
DROP TABLE client_release;
-- DROP TABLE users;
-- DROP TABLE config;

View File

@ -1,137 +0,0 @@
// https://mermade.github.io/openapi-gui/#
// https://editor.swagger.io/
const getRouteDescriptions = require("../jest/getRouteDescriptions");
const path = require("path");
const fs = require("fs");
require("missing-native-js-functions");
const openapiPath = path.join(__dirname, "..", "assets", "openapi.json");
const SchemaPath = path.join(__dirname, "..", "assets", "schemas.json");
const schemas = JSON.parse(fs.readFileSync(SchemaPath, { encoding: "utf8" }));
const specification = JSON.parse(fs.readFileSync(openapiPath, { encoding: "utf8" }));
function combineSchemas(schemas) {
var definitions = {};
for (const name in schemas) {
definitions = {
...definitions,
...schemas[name].definitions,
[name]: { ...schemas[name], definitions: undefined, $schema: undefined }
};
}
for (const key in definitions) {
specification.components.schemas[key] = definitions[key];
delete definitions[key].additionalProperties;
delete definitions[key].$schema;
const definition = definitions[key];
if (typeof definition.properties === "object") {
for (const property of Object.values(definition.properties)) {
if (Array.isArray(property.type)) {
if (property.type.includes("null")) {
property.type = property.type.find((x) => x !== "null");
property.nullable = true;
}
}
}
}
}
return definitions;
}
function getTag(key) {
return key.match(/\/([\w-]+)/)[1];
}
function apiRoutes() {
const routes = getRouteDescriptions();
const tags = Array.from(routes.keys()).map((x) => getTag(x));
specification.tags = [...specification.tags.map((x) => x.name), ...tags].unique().map((x) => ({ name: x }));
routes.forEach((route, pathAndMethod) => {
const [p, method] = pathAndMethod.split("|");
const path = p.replace(/:(\w+)/g, "{$1}");
let obj = specification.paths[path]?.[method] || {};
if (!obj.description) {
const permission = route.permission ? `##### Requires the \`\`${route.permission}\`\` permission\n` : "";
const event = route.test?.event ? `##### Fires a \`\`${route.test?.event}\`\` event\n` : "";
obj.description = permission + event;
}
if (route.body) {
obj.requestBody = {
required: true,
content: {
"application/json": {
schema: { $ref: `#/components/schemas/${route.body}` }
}
}
}.merge(obj.requestBody);
}
if (!obj.responses) {
obj.responses = {
default: {
description: "not documented"
}
};
}
if (route.test?.response) {
const status = route.test.response.status || 200;
let schema = {
allOf: [
{
$ref: `#/components/schemas/${route.test.response.body}`
},
{
example: route.test.body
}
]
};
if (!route.test.body) schema = schema.allOf[0];
obj.responses = {
[status]: {
...(route.test.response.body
? {
description: obj.responses[status].description || "",
content: {
"application/json": {
schema: schema
}
}
}
: {})
}
}.merge(obj.responses);
delete obj.responses.default;
}
if (p.includes(":")) {
obj.parameters = p.match(/:\w+/g)?.map((x) => ({
name: x.replace(":", ""),
in: "path",
required: true,
schema: { type: "string" },
description: x.replace(":", "")
}));
}
obj.tags = [...(obj.tags || []), getTag(p)].unique();
specification.paths[path] = { ...specification.paths[path], [method]: obj };
});
}
function main() {
combineSchemas(schemas);
apiRoutes();
fs.writeFileSync(
openapiPath,
JSON.stringify(specification, null, 4).replaceAll("#/definitions", "#/components/schemas").replaceAll("bigint", "number")
);
}
main();

View File

@ -48,14 +48,7 @@ function modify(obj) {
}
function main() {
const files = [
...walk(path.join(__dirname, "..", "src", "routes")),
...walk(path.join(__dirname, "..", "..", "util", "src")),
];
const program = TJS.getProgramFromFiles(
files,
compilerOptions
);
const program = TJS.programFromConfig("tsconfig.json")
const generator = TJS.buildGenerator(program, settings);
if (!generator || !program) return;

View File

@ -1,23 +0,0 @@
const path = require("path");
const fs = require("fs");
const parts = ["api", "util", "cdn", "gateway"];
const bundle = require("../package.json");
for (const part of parts) {
const { devDependencies, dependencies } = require(path.join(
"..",
"..",
part,
"package.json"
));
bundle.devDependencies = { ...bundle.devDependencies, ...devDependencies };
bundle.dependencies = { ...bundle.dependencies, ...dependencies };
delete bundle.dependencies["@fosscord/util"];
}
fs.writeFileSync(
path.join(__dirname, "..", "package.json"),
JSON.stringify(bundle, null, "\t"),
{ encoding: "utf8" }
);

View File

@ -1,109 +0,0 @@
const { config } = require("dotenv");
config();
const { createConnection } = require("typeorm");
const { initDatabase } = require("../../dist/util/Database");
require("missing-native-js-functions");
const {
Application,
Attachment,
Ban,
Channel,
ConfigEntity,
ConnectedAccount,
Emoji,
Guild,
Invite,
Member,
Message,
ReadState,
Recipient,
Relationship,
Role,
Sticker,
Team,
TeamMember,
Template,
User,
VoiceState,
Webhook,
} = require("../../dist/entities/index");
async function main() {
if (!process.env.TO) throw new Error("TO database env connection string not set");
// manually arrange them because of foreign keys
const entities = [
ConfigEntity,
User,
Guild,
Channel,
Invite,
Role,
Ban,
Application,
Emoji,
ConnectedAccount,
Member,
ReadState,
Recipient,
Relationship,
Sticker,
Team,
TeamMember,
Template,
VoiceState,
Webhook,
Message,
Attachment,
];
const oldDB = await initDatabase();
const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite";
const isSqlite = type.includes("sqlite");
// @ts-ignore
const newDB = await createConnection({
type,
url: isSqlite ? undefined : process.env.TO,
database: isSqlite ? process.env.TO : undefined,
entities,
name: "new",
synchronize: true,
});
let i = 0;
try {
for (const entity of entities) {
const entries = await oldDB.manager.find(entity);
// @ts-ignore
console.log("migrating " + entries.length + " " + entity.name + " ...");
for (const entry of entries) {
console.log(i++);
try {
await newDB.manager.insert(entity, entry);
} catch (error) {
try {
if (!entry.id) throw new Error("object doesn't have a unique id: " + entry);
await newDB.manager.update(entity, { id: entry.id }, entry);
} catch (error) {
console.error("couldn't migrate " + i + " " + entity.name, error);
}
}
}
// @ts-ignore
console.log("migrated " + entries.length + " " + entity.name);
}
} catch (error) {
console.error(error.message);
}
console.log("SUCCESS migrated all data");
await newDB.close();
}
main().caught();

View File

@ -1,3 +1,4 @@
require('module-alias/register');
const { Rights } = require("..");
const allRights = new Rights(1).bitfield;