Major Bugfix and Refactoring

This commit is contained in:
Benjamin Toby 2024-11-13 14:13:10 +01:00
parent a1e7439958
commit d4e210abdb
19 changed files with 131 additions and 166 deletions

View File

@ -7,6 +7,7 @@ const https = require("https");
const http = require("http"); const http = require("http");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const grabHostNames = require("../../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -38,10 +39,7 @@ async function updateApiSchemaFromLocalDb() {
const key = process.env.DSQL_KEY || ""; const key = process.env.DSQL_KEY || "";
const dbSchema = JSON.parse(fs.readFileSync(dbSchemaPath, "utf8")); const dbSchema = JSON.parse(fs.readFileSync(dbSchemaPath, "utf8"));
const { host, port, scheme } = grabHostNames();
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
/** /**
* Make https request * Make https request
@ -68,9 +66,7 @@ async function updateApiSchemaFromLocalDb() {
const reqPayload = reqPayloadString; const reqPayload = reqPayloadString;
const httpsRequest = ( const httpsRequest = scheme.request(
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -78,8 +74,8 @@ async function updateApiSchemaFromLocalDb() {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: localHostPort || 443, port,
hostname: localHost || "datasquirel.com", hostname: host,
path: `/api/query/update-schema-from-single-database`, path: `/api/query/update-schema-from-single-database`,
}, },

View File

@ -6,13 +6,9 @@
* ============================================================================== * ==============================================================================
*/ */
const http = require("http"); const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const encrypt = require("../../../functions/encrypt");
const decrypt = require("../../../functions/decrypt");
const handleSocialDb = require("./utils/handleSocialDb"); const handleSocialDb = require("./utils/handleSocialDb");
const httpsRequest = require("./utils/httpsRequest"); const httpsRequest = require("./utils/httpsRequest");
const grabHostNames = require("../../../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -60,9 +56,7 @@ async function localGoogleAuth({
* *
* @description Send a boolean response * @description Send a boolean response
*/ */
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
try { try {
/** /**
@ -73,7 +67,7 @@ async function localGoogleAuth({
*/ */
const payloadResponse = await httpsRequest({ const payloadResponse = await httpsRequest({
method: "POST", method: "POST",
hostname: localHost || "datasquirel.com", hostname: host,
path: "/user/grab-google-user-from-token", path: "/user/grab-google-user-from-token",
body: { body: {
token: token, token: token,

View File

@ -1,9 +1,10 @@
// @ts-check
/** /**
* Imports * Imports
* ============================================================================== * ==============================================================================
*/ */
const https = require("https"); const grabHostNames = require("../../../../package-shared/utils/grab-host-names");
const http = require("http");
////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////
@ -28,18 +29,17 @@ const http = require("http");
function httpsRequest({ url, method, hostname, path, href, headers, body }) { function httpsRequest({ url, method, hostname, path, href, headers, body }) {
const reqPayloadString = body ? JSON.stringify(body) : null; const reqPayloadString = body ? JSON.stringify(body) : null;
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
//////////////////////////////////////////////// ////////////////////////////////////////////////
//////////////////////////////////////////////// ////////////////////////////////////////////////
//////////////////////////////////////////////// ////////////////////////////////////////////////
/** @type {any} */
let requestOptions = { let requestOptions = {
method: method, method: method,
hostname: localHost || hostname, hostname: host,
port: localHostPort || 443, port,
headers: {}, headers: {},
}; };
@ -59,7 +59,7 @@ function httpsRequest({ url, method, hostname, path, href, headers, body }) {
//////////////////////////////////////////////// ////////////////////////////////////////////////
return new Promise((res, rej) => { return new Promise((res, rej) => {
const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( const httpsRequest = scheme.request(
/* ====== Request Options object ====== */ /* ====== Request Options object ====== */
// @ts-ignore // @ts-ignore
url ? url : requestOptions, url ? url : requestOptions,

View File

@ -0,0 +1,35 @@
// @ts-check
const https = require("https");
const http = require("http");
/**
* @typedef {object} GrabHostNamesReturn
* @property {string} host
* @property {number | string} port
* @property {typeof http | typeof https} scheme
*/
/**
* # Grab Names For Query
* @returns {GrabHostNamesReturn}
*/
function grabHostNames() {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const remoteHost = process.env.DSQL_API_REMOTE_HOST?.match(/.*\..*/)
? process.env.DSQL_API_REMOTE_HOST
: undefined;
const remoteHostPort = process.env.DSQL_API_REMOTE_HOST_PORT?.match(/./)
? process.env.DSQL_API_REMOTE_HOST_PORT
: undefined;
return {
host: remoteHost || localHost || "datasquirel.com",
port: remoteHostPort || localHostPort || 443,
scheme: scheme?.match(/^http$/i) ? http : https,
};
}
module.exports = grabHostNames;

View File

@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/datasquirel", "name": "@moduletrace/datasquirel",
"version": "2.6.2", "version": "2.6.3",
"description": "Cloud-based SQL data management tool", "description": "Cloud-based SQL data management tool",
"main": "index.js", "main": "index.js",
"bin": { "bin": {

View File

@ -10,6 +10,7 @@ const http = require("http");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const localAddUser = require("../engine/user/add-user"); const localAddUser = require("../engine/user/add-user");
const grabHostNames = require("../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -54,9 +55,7 @@ async function addUser({
DSQL_FULL_SYNC, DSQL_FULL_SYNC,
} = process.env; } = process.env;
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
if ( if (
DSQL_HOST?.match(/./) && DSQL_HOST?.match(/./) &&
@ -97,7 +96,7 @@ async function addUser({
encryptionKey, encryptionKey,
}); });
const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( const httpsRequest = scheme.request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -105,8 +104,8 @@ async function addUser({
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: localHostPort || 443, port,
hostname: localHost || "datasquirel.com", hostname: host,
path: `/api/user/add-user`, path: `/api/user/add-user`,
}, },

View File

@ -1,11 +1,16 @@
// @ts-check
/** /**
* ============================================================================== * ==============================================================================
* Imports * Imports
* ============================================================================== * ==============================================================================
*/ */
const path = require("path");
const fs = require("fs");
const https = require("https"); const https = require("https");
const http = require("http"); const http = require("http");
const getLocalUser = require("../engine/user/get-user"); const getLocalUser = require("../engine/user/get-user");
const grabHostNames = require("../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -26,7 +31,7 @@ const getLocalUser = require("../engine/user/get-user");
* @param {number} params.userId - user id * @param {number} params.userId - user id
* @param {string[]} [params.fields] - fields to select * @param {string[]} [params.fields] - fields to select
* *
* @returns { Promise<import("../types/user.td").GetUserFunctionReturn>} * @returns { Promise<import("../package-shared/types").GetUserFunctionReturn>}
*/ */
async function getUser({ key, userId, database, fields }) { async function getUser({ key, userId, database, fields }) {
/** /**
@ -58,9 +63,7 @@ async function getUser({ key, userId, database, fields }) {
fields: [...new Set(updatedFields)], fields: [...new Set(updatedFields)],
}); });
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
/** /**
* Check for local DB settings * Check for local DB settings
@ -83,7 +86,7 @@ async function getUser({ key, userId, database, fields }) {
DSQL_PASS?.match(/./) && DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./) DSQL_DB_NAME?.match(/./)
) { ) {
/** @type {DSQL_DatabaseSchemaType | undefined} */ /** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
let dbSchema; let dbSchema;
try { try {
@ -109,7 +112,7 @@ async function getUser({ key, userId, database, fields }) {
* @description make a request to datasquirel.com * @description make a request to datasquirel.com
*/ */
const httpResponse = await new Promise((resolve, reject) => { const httpResponse = await new Promise((resolve, reject) => {
const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( const httpsRequest = scheme.request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -117,8 +120,8 @@ async function getUser({ key, userId, database, fields }) {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: localHostPort || 443, port,
hostname: localHost || "datasquirel.com", hostname: host,
path: `/api/user/get-user`, path: `/api/user/get-user`,
}, },

View File

@ -11,6 +11,7 @@ const fs = require("fs");
const path = require("path"); const path = require("path");
const encrypt = require("../functions/encrypt"); const encrypt = require("../functions/encrypt");
const loginLocalUser = require("../engine/user/login-user"); const loginLocalUser = require("../engine/user/login-user");
const grabHostNames = require("../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -56,9 +57,7 @@ async function loginUser({
temp_code_field, temp_code_field,
token, token,
}) { }) {
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const defaultTempLoginFieldName = "temp_login_code"; const defaultTempLoginFieldName = "temp_login_code";
const emailLoginTempCodeFieldName = email_login const emailLoginTempCodeFieldName = email_login
@ -171,9 +170,7 @@ async function loginUser({
const reqPayloadJSON = JSON.stringify(reqPayload); const reqPayloadJSON = JSON.stringify(reqPayload);
const httpsRequest = ( const httpsRequest = scheme.request(
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -181,8 +178,8 @@ async function loginUser({
"Content-Length": Buffer.from(reqPayloadJSON).length, "Content-Length": Buffer.from(reqPayloadJSON).length,
Authorization: key, Authorization: key,
}, },
port: localHostPort || 443, port,
hostname: localHost || "datasquirel.com", hostname: host,
path: `/api/user/login-user`, path: `/api/user/login-user`,
}, },

View File

@ -13,6 +13,7 @@ const encrypt = require("../functions/encrypt");
const userAuth = require("./user-auth"); const userAuth = require("./user-auth");
const localReauthUser = require("../engine/user/reauth-user"); const localReauthUser = require("../engine/user/reauth-user");
const grabHostNames = require("../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -56,9 +57,7 @@ async function reauthUser({
* *
* @description Check Encryption Keys * @description Check Encryption Keys
*/ */
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const existingUser = userAuth({ const existingUser = userAuth({
database, database,
@ -134,9 +133,7 @@ async function reauthUser({
additionalFields, additionalFields,
}); });
const httpsRequest = ( const httpsRequest = scheme.request(
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -144,8 +141,8 @@ async function reauthUser({
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: localHostPort || 443, port,
hostname: localHost || "datasquirel.com", hostname: host,
path: `/api/user/reauth-user`, path: `/api/user/reauth-user`,
}, },

View File

@ -12,6 +12,7 @@ const path = require("path");
const encrypt = require("../functions/encrypt"); const encrypt = require("../functions/encrypt");
const loginLocalUser = require("../engine/user/login-user"); const loginLocalUser = require("../engine/user/login-user");
const localSendEmailCode = require("../engine/user/send-email-code"); const localSendEmailCode = require("../engine/user/send-email-code");
const grabHostNames = require("../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -54,9 +55,7 @@ async function sendEmailCode({
mail_port, mail_port,
sender, sender,
}) { }) {
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const defaultTempLoginFieldName = "temp_login_code"; const defaultTempLoginFieldName = "temp_login_code";
const emailLoginTempCodeFieldName = temp_code_field const emailLoginTempCodeFieldName = temp_code_field
@ -164,9 +163,7 @@ async function sendEmailCode({
), ),
}); });
const httpsRequest = ( const httpsRequest = scheme.request(
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -174,8 +171,8 @@ async function sendEmailCode({
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: localHostPort || 443, port,
hostname: localHost || "datasquirel.com", hostname: host,
path: `/api/user/send-email-code`, path: `/api/user/send-email-code`,
}, },

View File

@ -11,6 +11,7 @@ const fs = require("fs");
const path = require("path"); const path = require("path");
const encrypt = require("../../functions/encrypt"); const encrypt = require("../../functions/encrypt");
const localGithubAuth = require("../../engine/user/social/github-auth"); const localGithubAuth = require("../../engine/user/social/github-auth");
const grabHostNames = require("../../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -64,9 +65,7 @@ async function githubAuth({
* *
* @description Check inputs * @description Check inputs
*/ */
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
if (!key || key?.match(/ /)) { if (!key || key?.match(/ /)) {
return { return {
@ -193,9 +192,7 @@ async function githubAuth({
additionalFields, additionalFields,
}); });
const httpsRequest = ( const httpsRequest = scheme.request(
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -203,8 +200,8 @@ async function githubAuth({
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: localHostPort || 443, port,
hostname: localHost || "datasquirel.com", hostname: host,
path: `/api/user/github-login`, path: `/api/user/github-login`,
}, },

View File

@ -11,6 +11,7 @@ const fs = require("fs");
const path = require("path"); const path = require("path");
const encrypt = require("../../functions/encrypt"); const encrypt = require("../../functions/encrypt");
const localGoogleAuth = require("../../engine/user/social/google-auth"); const localGoogleAuth = require("../../engine/user/social/google-auth");
const grabHostNames = require("../../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -55,9 +56,7 @@ async function googleAuth({
encryptionSalt, encryptionSalt,
additionalFields, additionalFields,
}) { }) {
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
/** /**
* Check inputs * Check inputs
@ -189,9 +188,7 @@ async function googleAuth({
additionalFields, additionalFields,
}); });
const httpsRequest = ( const httpsRequest = scheme.request(
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -199,8 +196,8 @@ async function googleAuth({
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: localHostPort || 443, port,
hostname: localHost || "datasquirel.com", hostname: host,
path: `/api/user/google-login`, path: `/api/user/google-login`,
}, },

View File

@ -10,6 +10,7 @@ const https = require("https");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const localUpdateUser = require("../engine/user/update-user"); const localUpdateUser = require("../engine/user/update-user");
const grabHostNames = require("../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -47,9 +48,7 @@ async function updateUser({ key, payload, database }) {
DSQL_FULL_SYNC, DSQL_FULL_SYNC,
} = process.env; } = process.env;
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
if ( if (
DSQL_HOST?.match(/./) && DSQL_HOST?.match(/./) &&
@ -87,7 +86,7 @@ async function updateUser({ key, payload, database }) {
database, database,
}); });
const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( const httpsRequest = scheme.request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -95,8 +94,8 @@ async function updateUser({ key, payload, database }) {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: localHostPort || 443, port,
hostname: localHost || "datasquirel.com", hostname: host,
path: `/api/user/update-user`, path: `/api/user/update-user`,
}, },

View File

@ -1,3 +1,5 @@
// @ts-check
/** /**
* ============================================================================== * ==============================================================================
* Imports * Imports
@ -5,6 +7,7 @@
*/ */
const http = require("http"); const http = require("http");
const https = require("https"); const https = require("https");
const grabHostNames = require("../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -19,7 +22,7 @@ const https = require("https");
* @property {{ * @property {{
* urlPath: string, * urlPath: string,
* urlThumbnailPath: string * urlThumbnailPath: string
* }} payload - Payload containing the url for the image and its thumbnail * } | null} payload - Payload containing the url for the image and its thumbnail
* @property {string} [msg] - An optional message * @property {string} [msg] - An optional message
*/ */
@ -36,15 +39,7 @@ const https = require("https");
* @returns { Promise<FunctionReturn> } - Image Url * @returns { Promise<FunctionReturn> } - Image Url
*/ */
async function uploadImage({ key, url }) { async function uploadImage({ key, url }) {
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const remoteHost = process.env.DSQL_API_REMOTE_HOST?.match(/.*\..*/)
? process.env.DSQL_API_REMOTE_HOST
: undefined;
const remoteHostPort = process.env.DSQL_API_REMOTE_HOST_PORT?.match(/./)
? process.env.DSQL_API_REMOTE_HOST_PORT
: undefined;
try { try {
/** /**
@ -55,9 +50,7 @@ async function uploadImage({ key, url }) {
const httpResponse = await new Promise((resolve, reject) => { const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({ url: url }); const reqPayload = JSON.stringify({ url: url });
const httpsRequest = ( const httpsRequest = scheme.request(
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -65,8 +58,8 @@ async function uploadImage({ key, url }) {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: remoteHostPort || localHostPort || 443, port,
hostname: remoteHost || localHost || "datasquirel.com", hostname: host,
path: `/api/query/delete-file`, path: `/api/query/delete-file`,
}, },

View File

@ -7,6 +7,7 @@
*/ */
const http = require("http"); const http = require("http");
const https = require("https"); const https = require("https");
const grabHostNames = require("../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -28,15 +29,7 @@ const https = require("https");
* @returns { Promise<GetSchemaReturn> } - Return Object * @returns { Promise<GetSchemaReturn> } - Return Object
*/ */
async function getSchema({ key, database, field, table }) { async function getSchema({ key, database, field, table }) {
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const remoteHost = process.env.DSQL_API_REMOTE_HOST?.match(/.*\..*/)
? process.env.DSQL_API_REMOTE_HOST
: undefined;
const remoteHostPort = process.env.DSQL_API_REMOTE_HOST_PORT?.match(/./)
? process.env.DSQL_API_REMOTE_HOST_PORT
: undefined;
/** /**
* Make https request * Make https request
@ -53,7 +46,7 @@ async function getSchema({ key, database, field, table }) {
.map((k) => `${k}=${queryObject[k]}`) .map((k) => `${k}=${queryObject[k]}`)
.join("&"); .join("&");
(scheme?.match(/^http$/i) ? http : https) scheme
.request( .request(
{ {
method: "GET", method: "GET",
@ -61,8 +54,8 @@ async function getSchema({ key, database, field, table }) {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: key, Authorization: key,
}, },
port: remoteHostPort || localHostPort || 443, port,
hostname: remoteHost || localHost || "datasquirel.com", hostname: host,
path: path:
"/api/query/get-schema" + "/api/query/get-schema" +
(query?.match(/./) ? `?${query}` : ""), (query?.match(/./) ? `?${query}` : ""),

View File

@ -11,6 +11,7 @@ const path = require("path");
const fs = require("fs"); const fs = require("fs");
const localGet = require("../engine/query/get"); const localGet = require("../engine/query/get");
const serializeQuery = require("./functions/serialize-query"); const serializeQuery = require("./functions/serialize-query");
const grabHostNames = require("../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -34,15 +35,7 @@ const serializeQuery = require("./functions/serialize-query");
* @returns { Promise<import("../package-shared/types").GetReturn> } - Return Object * @returns { Promise<import("../package-shared/types").GetReturn> } - Return Object
*/ */
async function get({ key, db, query, queryValues, tableName }) { async function get({ key, db, query, queryValues, tableName }) {
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const remoteHost = process.env.DSQL_API_REMOTE_HOST?.match(/.*\..*/)
? process.env.DSQL_API_REMOTE_HOST
: undefined;
const remoteHostPort = process.env.DSQL_API_REMOTE_HOST_PORT?.match(/./)
? process.env.DSQL_API_REMOTE_HOST_PORT
: undefined;
/** /**
* Check for local DB settings * Check for local DB settings
@ -109,12 +102,12 @@ async function get({ key, db, query, queryValues, tableName }) {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: key, Authorization: key,
}, },
port: remoteHostPort || localHostPort || 443, port,
hostname: remoteHost || localHost || "datasquirel.com", hostname: host,
path: encodeURI(path), path: encodeURI(path),
}; };
(scheme?.match(/^http$/i) ? http : https) scheme
.request( .request(
requestObject, requestObject,

View File

@ -8,6 +8,7 @@ const https = require("https");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const localPost = require("../engine/query/post"); const localPost = require("../engine/query/post");
const grabHostNames = require("../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -31,15 +32,7 @@ const localPost = require("../engine/query/post");
* @returns { Promise<import("../package-shared/types").PostReturn> } - Return Object * @returns { Promise<import("../package-shared/types").PostReturn> } - Return Object
*/ */
async function post({ key, query, queryValues, database, tableName }) { async function post({ key, query, queryValues, database, tableName }) {
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const remoteHost = process.env.DSQL_API_REMOTE_HOST?.match(/.*\..*/)
? process.env.DSQL_API_REMOTE_HOST
: undefined;
const remoteHostPort = process.env.DSQL_API_REMOTE_HOST_PORT?.match(/./)
? process.env.DSQL_API_REMOTE_HOST_PORT
: undefined;
/** /**
* Check for local DB settings * Check for local DB settings
@ -104,7 +97,7 @@ async function post({ key, query, queryValues, database, tableName }) {
const reqPayload = reqPayloadString; const reqPayload = reqPayloadString;
const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( const httpsRequest = scheme.request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -112,8 +105,8 @@ async function post({ key, query, queryValues, database, tableName }) {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: remoteHostPort || localHostPort || 443, port,
hostname: remoteHost || localHost || "datasquirel.com", hostname: host,
path: `/api/query/post`, path: `/api/query/post`,
}, },

View File

@ -7,6 +7,7 @@
*/ */
const http = require("http"); const http = require("http");
const https = require("https"); const https = require("https");
const grabHostNames = require("../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -43,15 +44,7 @@ const https = require("https");
* @returns { Promise<FunctionReturn> } - Return Object * @returns { Promise<FunctionReturn> } - Return Object
*/ */
async function uploadImage({ key, payload }) { async function uploadImage({ key, payload }) {
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const remoteHost = process.env.DSQL_API_REMOTE_HOST?.match(/.*\..*/)
? process.env.DSQL_API_REMOTE_HOST
: undefined;
const remoteHostPort = process.env.DSQL_API_REMOTE_HOST_PORT?.match(/./)
? process.env.DSQL_API_REMOTE_HOST_PORT
: undefined;
try { try {
/** /**
@ -62,9 +55,7 @@ async function uploadImage({ key, payload }) {
const httpResponse = await new Promise((resolve, reject) => { const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify(payload); const reqPayload = JSON.stringify(payload);
const httpsRequest = ( const httpsRequest = scheme.request(
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -72,8 +63,8 @@ async function uploadImage({ key, payload }) {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: remoteHostPort || localHostPort || 443, port,
hostname: remoteHost || localHost || "datasquirel.com", hostname: host,
path: `/api/query/add-file`, path: `/api/query/add-file`,
}, },

View File

@ -7,6 +7,7 @@
*/ */
const http = require("http"); const http = require("http");
const https = require("https"); const https = require("https");
const grabHostNames = require("../package-shared/utils/grab-host-names");
/** ****************************************************************************** */ /** ****************************************************************************** */
/** ****************************************************************************** */ /** ****************************************************************************** */
@ -45,15 +46,7 @@ const https = require("https");
* @returns { Promise<FunctionReturn> } - Return Object * @returns { Promise<FunctionReturn> } - Return Object
*/ */
async function uploadImage({ key, payload }) { async function uploadImage({ key, payload }) {
const scheme = process.env.DSQL_HTTP_SCHEME; const { host, port, scheme } = grabHostNames();
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const remoteHost = process.env.DSQL_API_REMOTE_HOST?.match(/.*\..*/)
? process.env.DSQL_API_REMOTE_HOST
: undefined;
const remoteHostPort = process.env.DSQL_API_REMOTE_HOST_PORT?.match(/./)
? process.env.DSQL_API_REMOTE_HOST_PORT
: undefined;
try { try {
/** /**
@ -64,9 +57,7 @@ async function uploadImage({ key, payload }) {
const httpResponse = await new Promise((resolve, reject) => { const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify(payload); const reqPayload = JSON.stringify(payload);
const httpsRequest = ( const httpsRequest = scheme.request(
scheme?.match(/^http$/i) ? http : https
).request(
{ {
method: "POST", method: "POST",
headers: { headers: {
@ -74,8 +65,8 @@ async function uploadImage({ key, payload }) {
"Content-Length": Buffer.from(reqPayload).length, "Content-Length": Buffer.from(reqPayload).length,
Authorization: key, Authorization: key,
}, },
port: remoteHostPort || localHostPort || 443, port,
hostname: remoteHost || localHost || "datasquirel.com", hostname: host,
path: `/api/query/add-image`, path: `/api/query/add-image`,
}, },