First Commit
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const excludeRegexp = /\/node_modules|\/dump|\/.tmp|\/.next/;
|
||||
|
||||
function traverse(/** @type {String} - Directory path */ dir) {
|
||||
const files = fs.readdirSync(dir);
|
||||
|
||||
if (dir.match(excludeRegexp)) return;
|
||||
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
const filePath = path.resolve(dir, file);
|
||||
const fileStat = fs.statSync(filePath);
|
||||
|
||||
if (fileStat.isDirectory()) {
|
||||
traverse(filePath);
|
||||
continue;
|
||||
}
|
||||
|
||||
const fileMatch = file.match(/\.(jsx?)$/);
|
||||
|
||||
if (fileMatch) {
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
if (fileContent.includes("@ts-check")) continue;
|
||||
fs.writeFileSync(
|
||||
filePath,
|
||||
"// @ts-check\n\n" + fileContent,
|
||||
"utf-8"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
traverse(process.cwd());
|
||||
Executable
+68
@@ -0,0 +1,68 @@
|
||||
// @ts-check
|
||||
|
||||
const { execSync } = require("child_process");
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number | string | null | undefined} port
|
||||
* @returns
|
||||
*/
|
||||
module.exports = async function killProcessOnPort(port) {
|
||||
if (!port) {
|
||||
console.error("Error: No port specified");
|
||||
return;
|
||||
}
|
||||
|
||||
const targetPort = parseInt(port.toString());
|
||||
|
||||
if (isNaN(targetPort)) {
|
||||
console.error("Error: Port must be a number");
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof targetPort !== "number") {
|
||||
console.error("Error: Port must be a number");
|
||||
return;
|
||||
}
|
||||
|
||||
const processId = (() => {
|
||||
try {
|
||||
if (process.platform.match(/win/i)) {
|
||||
const readNetStat = execSync(
|
||||
`netstat -ano | findstr :${targetPort}`
|
||||
).toString("utf-8");
|
||||
const firstLine = readNetStat.match(/.*/)?.[0].trim();
|
||||
const PID = firstLine?.split(" ").at(-1);
|
||||
return PID;
|
||||
}
|
||||
|
||||
return execSync(
|
||||
`lsof -i :${targetPort} | awk '$1 == "COMMAND" { next } { print $2 }'`
|
||||
)
|
||||
.toString()
|
||||
.trim();
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(
|
||||
`Error finding PID on ${process.platform}:`,
|
||||
error.message
|
||||
);
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
|
||||
if (!processId) {
|
||||
console.error(`Error: No process found on port ${targetPort}`);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (process.platform.match(/win/i)) {
|
||||
execSync(`taskkill /F /PID ${processId} /T`);
|
||||
} else {
|
||||
execSync(`kill -9 ${processId}`);
|
||||
}
|
||||
console.log(`Killed process ${processId} on port ${targetPort}`);
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.error("Error:", error.message);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,78 @@
|
||||
// @ts-check
|
||||
|
||||
const httpsRequest = require("../functions/backend/httpsRequest");
|
||||
const cron = require("cron");
|
||||
const path = require("path");
|
||||
const { dbSchemaExecDbUpdate } = require("../functions/backend/dbSchemaExec");
|
||||
const dbHandler = require("../package-shared/functions/backend/dbHandler");
|
||||
|
||||
module.exports = async function () {
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
new cron.CronJob(
|
||||
"20 * * * * *",
|
||||
async () => {
|
||||
/** @type {import("@/package-shared/types").DSQL_MYSQL_user_databases_Type[] | null} */ // @ts-ignore
|
||||
const remoteConnectedDbs = await dbHandler(
|
||||
`SELECT * FROM user_databases WHERE remote_connected = 1`
|
||||
);
|
||||
|
||||
if (remoteConnectedDbs?.[0]) {
|
||||
for (let i = 0; i < remoteConnectedDbs.length; i++) {
|
||||
try {
|
||||
const database = remoteConnectedDbs[i];
|
||||
|
||||
const {
|
||||
user_id,
|
||||
remote_connection_host,
|
||||
remote_db_full_name,
|
||||
remote_connection_key,
|
||||
remote_connection_type,
|
||||
db_full_name,
|
||||
} = database;
|
||||
|
||||
if (database.remote_connection_type == "pull") {
|
||||
const dbSchema = await httpsRequest({
|
||||
url: remote_connection_host,
|
||||
headers: {
|
||||
Authorization: remote_connection_key,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
method: "GET",
|
||||
path: `/api/query/get-schema?database=${remote_db_full_name?.replace(
|
||||
/^datasquirel_user_\d+_/i,
|
||||
""
|
||||
)}`,
|
||||
});
|
||||
|
||||
/** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType | null | undefined} */
|
||||
const remoteDbSchemaData =
|
||||
JSON.parse(dbSchema).payload;
|
||||
|
||||
if (remoteDbSchemaData) {
|
||||
const update = dbSchemaExecDbUpdate({
|
||||
dbSchema: remoteDbSchemaData,
|
||||
database: database,
|
||||
userId: user_id,
|
||||
});
|
||||
|
||||
console.log(update);
|
||||
}
|
||||
}
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
null,
|
||||
true,
|
||||
"America/Los_Angeles"
|
||||
);
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
const ignorePattern = /node_modules/;
|
||||
|
||||
const searchMatchPattern = {
|
||||
pattern: /\"\/admin\/(.*?)\"/,
|
||||
replace: "'/b/$1'",
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {string} param0.dir
|
||||
*/
|
||||
function replaceDir({ dir }) {
|
||||
const dirContent = fs.readdirSync(dir);
|
||||
dirContent.forEach((fileFolder, index) => {
|
||||
const fileFolderPath = path.join(dir, fileFolder);
|
||||
const fsStat = fs.statSync(fileFolderPath);
|
||||
if (!fsStat.isFile()) {
|
||||
return replaceDir({ dir });
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
// @ts-check
|
||||
const fs = require("fs");
|
||||
|
||||
const isLocal = process.env.NEXT_PUBLIC_DSQL_LOCAL || null;
|
||||
const production = process.env.NODE_ENV == "production";
|
||||
const isBuilding = process.env.BUILDING_APP;
|
||||
|
||||
/**
|
||||
* # Grab the current distribution directory
|
||||
* @description This returns the relative path from the CWD. Eg `./.dist/build-1`
|
||||
* @returns {string | undefined}
|
||||
*/
|
||||
function grabDist() {
|
||||
if (isLocal) {
|
||||
return ".local_dist";
|
||||
}
|
||||
|
||||
if (isBuilding) {
|
||||
if (!fs.existsSync("./.dist")) fs.mkdirSync("./.dist");
|
||||
|
||||
if (!fs.existsSync("./.dist/BUILD")) {
|
||||
fs.writeFileSync("./.dist/BUILD", "0", "utf-8");
|
||||
}
|
||||
|
||||
const distDir = (() => {
|
||||
if (isLocal) return ".local_dist";
|
||||
|
||||
try {
|
||||
const buildNumber = fs.readFileSync("./.dist/BUILD", "utf-8");
|
||||
const newBuildNumber = Number(buildNumber) + 1;
|
||||
|
||||
if (newBuildNumber < 0) {
|
||||
throw new Error("Invalid Build Number");
|
||||
}
|
||||
fs.writeFileSync("./.dist/BUILD", String(newBuildNumber));
|
||||
return `.dist/build-${newBuildNumber}`;
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("Build Number Generation Error =>", error.message);
|
||||
process.exit();
|
||||
}
|
||||
})();
|
||||
|
||||
return distDir;
|
||||
}
|
||||
|
||||
if (production) {
|
||||
const distDir = (() => {
|
||||
if (isLocal) return ".local_dist";
|
||||
|
||||
try {
|
||||
const buildNumber = fs.readFileSync("./.dist/BUILD", "utf-8");
|
||||
return `.dist/build-${buildNumber}`;
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("Build Number Parse Error =>", error.message);
|
||||
process.exit();
|
||||
}
|
||||
})();
|
||||
|
||||
return distDir;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
module.exports = grabDist;
|
||||
@@ -0,0 +1,130 @@
|
||||
// @ts-check
|
||||
|
||||
const http = require("http");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const { Server } = require("socket.io");
|
||||
const { parse } = require("url");
|
||||
const userAuth = require("datasquirel/users/user-auth");
|
||||
const decrypt = require("datasquirel/functions/decrypt");
|
||||
const parseCookies = require("datasquirel/utils/functions/parseCookies");
|
||||
const suSocketAuth = require("../package-shared/functions/backend/suSocketAuth");
|
||||
const { WriteStream, ReadStream, write } = require("fs");
|
||||
const { Readable } = require("node:stream");
|
||||
const { spawnSync, spawn } = require("child_process");
|
||||
const pty = require("node-pty");
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {http.Server} server
|
||||
*/
|
||||
module.exports = async function serverSocket(server) {
|
||||
const io = new Server(server);
|
||||
|
||||
io.on("connection", async (socket) => {
|
||||
const req = socket.request;
|
||||
const parsedUrl = parse(req.url || "", true);
|
||||
const { pathname, query, href, search } = parsedUrl;
|
||||
const cookie = req.headers.cookie;
|
||||
const paradigm = req.headers["x-socket-paradigm"];
|
||||
|
||||
const parsedCookies = parseCookies({ request: req });
|
||||
|
||||
const suAdminUser = await suSocketAuth(req);
|
||||
|
||||
const logPath = path.resolve(__dirname, "../log.log");
|
||||
|
||||
if (!suAdminUser) return;
|
||||
|
||||
switch (paradigm) {
|
||||
case "Console":
|
||||
try {
|
||||
socket.emit("console", "Welcome");
|
||||
|
||||
process.stdin.on("data", (data) => {
|
||||
console.log("STDOUT data =>", data.toString("utf8"));
|
||||
});
|
||||
|
||||
const originalConsoleLog = console.log;
|
||||
console.log = function (...args) {
|
||||
const logMessage = args
|
||||
.map((arg) =>
|
||||
typeof arg === "object"
|
||||
? JSON.stringify(arg)
|
||||
: arg
|
||||
)
|
||||
.join(" ");
|
||||
socket.emit("console", logMessage + "\n\r");
|
||||
|
||||
originalConsoleLog.apply(console, args);
|
||||
};
|
||||
|
||||
socket.on("log", (log) => {
|
||||
console.log(log);
|
||||
});
|
||||
|
||||
socket.on("get-log", (log) => {
|
||||
if (fs.existsSync(logPath)) {
|
||||
socket.emit(
|
||||
"console-log",
|
||||
fs.readFileSync(logPath, "utf-8")
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
//////////////////////////////////////////
|
||||
//////////////////////////////////////////
|
||||
//////////////////////////////////////////
|
||||
|
||||
if (process.env.NEXT_PUBLIC_DSQL_LOCAL) {
|
||||
const PAUSE = "\x13";
|
||||
const CANCEL = "\x03";
|
||||
const RESUME = "\x11";
|
||||
|
||||
const ptyProcess = pty.spawn("bash", [], {
|
||||
name: "xterm-color",
|
||||
});
|
||||
|
||||
socket.on("shell", (message) => {
|
||||
ptyProcess.write(message);
|
||||
});
|
||||
|
||||
ptyProcess.onData((data) => {
|
||||
socket.emit("shell", data);
|
||||
});
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
ptyProcess.kill("SIGTERM");
|
||||
});
|
||||
}
|
||||
|
||||
socket.on("clear-log", (message) => {
|
||||
if (fs.existsSync(logPath))
|
||||
fs.writeFileSync(logPath, "", "utf-8");
|
||||
});
|
||||
|
||||
//////////////////////////////////////////
|
||||
//////////////////////////////////////////
|
||||
//////////////////////////////////////////
|
||||
} catch (/** @type {any} */ error) {
|
||||
//////////////////////////////////////////
|
||||
//////////////////////////////////////////
|
||||
//////////////////////////////////////////
|
||||
|
||||
console.log("Error in Console socket =>", error.message);
|
||||
}
|
||||
break;
|
||||
|
||||
//////////////////////////////////////////
|
||||
//////////////////////////////////////////
|
||||
//////////////////////////////////////////
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
io.on("error", (err) => {
|
||||
console.log("Socket Server Error =>", err);
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,75 @@
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
require("dotenv").config({
|
||||
path: path.resolve(__dirname, "../.env"),
|
||||
});
|
||||
|
||||
// const mysql = require("mysql");
|
||||
|
||||
// const connection = mysql.createConnection({
|
||||
// host: process.env.DSQL_DB_HOST,
|
||||
// user: process.env.DSQL_DB_USERNAME,
|
||||
// password: process.env.DSQL_DB_PASSWORD,
|
||||
// database: process.env.DSQL_DB_NAME,
|
||||
// charset: "utf8mb4",
|
||||
// });
|
||||
|
||||
const mysql = require("serverless-mysql");
|
||||
|
||||
const SSL_DIR = "/app/ssl";
|
||||
|
||||
const connection = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: process.env.DSQL_DB_NAME,
|
||||
charset: "utf8mb4",
|
||||
ssl: {
|
||||
ca: fs.readFileSync(`${SSL_DIR}/ca-cert.pem`),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const tableIndex = process.argv.findIndex((str) => str.match(/--table|-t/));
|
||||
const table = tableIndex >= 0 ? process.argv[tableIndex + 1] : null;
|
||||
|
||||
if (!table) {
|
||||
console.log(
|
||||
"Please add a table flag to the arguments: Eg. '--talbe <table_name>'"
|
||||
);
|
||||
process.exit();
|
||||
}
|
||||
|
||||
connection
|
||||
.query("SHOW COLUMNS FROM" + " " + table)
|
||||
.then((result) => {
|
||||
let typedefStart = `/**\n * @typedef {object} MYSQL_${table}_table_def\n`;
|
||||
let typedefMid = "";
|
||||
let typedefEnd = ` */`;
|
||||
console.log("Result =>", result);
|
||||
|
||||
result.forEach((/** @type {any} */ res) => {
|
||||
const parsedResult = JSON.parse(JSON.stringify(res));
|
||||
const { Field, Type, Null, Key, Default, Extra } = parsedResult;
|
||||
const type = (() => {
|
||||
if (Type?.match(/int/i)) return "number";
|
||||
return "string";
|
||||
})();
|
||||
typedefMid += ` * @property {${type}} [${Field}] - NULL=\`${Null}\` Key=\`${Key}\` Default=\`${Default}\` Extra=\`${Extra}\`\n`;
|
||||
});
|
||||
|
||||
console.log(typedefStart + typedefMid + typedefEnd);
|
||||
|
||||
process.exit();
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(error.message);
|
||||
})
|
||||
.finally(() => {
|
||||
/** ********************* Clean up */
|
||||
connection.end();
|
||||
});
|
||||
Reference in New Issue
Block a user