diff --git a/engine/query/update-api-schema-from-local-db.js b/engine/query/update-api-schema-from-local-db.js index a152bf7..745cf00 100644 --- a/engine/query/update-api-schema-from-local-db.js +++ b/engine/query/update-api-schema-from-local-db.js @@ -7,6 +7,7 @@ const https = require("https"); const http = require("http"); const path = require("path"); 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 dbSchema = JSON.parse(fs.readFileSync(dbSchemaPath, "utf8")); - - const scheme = process.env.DSQL_HTTP_SCHEME; - const localHost = process.env.DSQL_LOCAL_HOST; - const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + const { host, port, scheme } = grabHostNames(); /** * Make https request @@ -68,9 +66,7 @@ async function updateApiSchemaFromLocalDb() { const reqPayload = reqPayloadString; - const httpsRequest = ( - scheme?.match(/^http$/i) ? http : https - ).request( + const httpsRequest = scheme.request( { method: "POST", headers: { @@ -78,8 +74,8 @@ async function updateApiSchemaFromLocalDb() { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: localHostPort || 443, - hostname: localHost || "datasquirel.com", + port, + hostname: host, path: `/api/query/update-schema-from-single-database`, }, diff --git a/engine/user/social/google-auth.js b/engine/user/social/google-auth.js index f4e0ecf..571b902 100644 --- a/engine/user/social/google-auth.js +++ b/engine/user/social/google-auth.js @@ -6,13 +6,9 @@ * ============================================================================== */ 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 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 */ - const scheme = process.env.DSQL_HTTP_SCHEME; - const localHost = process.env.DSQL_LOCAL_HOST; - const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + const { host, port, scheme } = grabHostNames(); try { /** @@ -73,7 +67,7 @@ async function localGoogleAuth({ */ const payloadResponse = await httpsRequest({ method: "POST", - hostname: localHost || "datasquirel.com", + hostname: host, path: "/user/grab-google-user-from-token", body: { token: token, diff --git a/engine/user/social/utils/httpsRequest.js b/engine/user/social/utils/httpsRequest.js index 200cd39..7710af6 100644 --- a/engine/user/social/utils/httpsRequest.js +++ b/engine/user/social/utils/httpsRequest.js @@ -1,9 +1,10 @@ +// @ts-check + /** * Imports * ============================================================================== */ -const https = require("https"); -const http = require("http"); +const grabHostNames = require("../../../../package-shared/utils/grab-host-names"); ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// @@ -28,18 +29,17 @@ const http = require("http"); function httpsRequest({ url, method, hostname, path, href, headers, body }) { const reqPayloadString = body ? JSON.stringify(body) : null; - const scheme = process.env.DSQL_HTTP_SCHEME; - const localHost = process.env.DSQL_LOCAL_HOST; - const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + const { host, port, scheme } = grabHostNames(); //////////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// + /** @type {any} */ let requestOptions = { method: method, - hostname: localHost || hostname, - port: localHostPort || 443, + hostname: host, + port, headers: {}, }; @@ -59,7 +59,7 @@ function httpsRequest({ url, method, hostname, path, href, headers, body }) { //////////////////////////////////////////////// return new Promise((res, rej) => { - const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( + const httpsRequest = scheme.request( /* ====== Request Options object ====== */ // @ts-ignore url ? url : requestOptions, diff --git a/package-shared/utils/grab-host-names.js b/package-shared/utils/grab-host-names.js new file mode 100644 index 0000000..3baff49 --- /dev/null +++ b/package-shared/utils/grab-host-names.js @@ -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; diff --git a/package.json b/package.json index 7a5764a..9e6c191 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/datasquirel", - "version": "2.6.2", + "version": "2.6.3", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": { diff --git a/users/add-user.js b/users/add-user.js index 9723aca..de266d8 100644 --- a/users/add-user.js +++ b/users/add-user.js @@ -10,6 +10,7 @@ const http = require("http"); const path = require("path"); const fs = require("fs"); 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, } = process.env; - const scheme = process.env.DSQL_HTTP_SCHEME; - const localHost = process.env.DSQL_LOCAL_HOST; - const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + const { host, port, scheme } = grabHostNames(); if ( DSQL_HOST?.match(/./) && @@ -97,7 +96,7 @@ async function addUser({ encryptionKey, }); - const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( + const httpsRequest = scheme.request( { method: "POST", headers: { @@ -105,8 +104,8 @@ async function addUser({ "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: localHostPort || 443, - hostname: localHost || "datasquirel.com", + port, + hostname: host, path: `/api/user/add-user`, }, diff --git a/users/get-user.js b/users/get-user.js index 2d08ad8..26f1f64 100644 --- a/users/get-user.js +++ b/users/get-user.js @@ -1,11 +1,16 @@ +// @ts-check + /** * ============================================================================== * Imports * ============================================================================== */ +const path = require("path"); +const fs = require("fs"); const https = require("https"); const http = require("http"); 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 {string[]} [params.fields] - fields to select * - * @returns { Promise} + * @returns { Promise} */ async function getUser({ key, userId, database, fields }) { /** @@ -58,9 +63,7 @@ async function getUser({ key, userId, database, fields }) { fields: [...new Set(updatedFields)], }); - const scheme = process.env.DSQL_HTTP_SCHEME; - const localHost = process.env.DSQL_LOCAL_HOST; - const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + const { host, port, scheme } = grabHostNames(); /** * Check for local DB settings @@ -83,7 +86,7 @@ async function getUser({ key, userId, database, fields }) { DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./) ) { - /** @type {DSQL_DatabaseSchemaType | undefined} */ + /** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */ let dbSchema; try { @@ -109,7 +112,7 @@ async function getUser({ key, userId, database, fields }) { * @description make a request to datasquirel.com */ const httpResponse = await new Promise((resolve, reject) => { - const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( + const httpsRequest = scheme.request( { method: "POST", headers: { @@ -117,8 +120,8 @@ async function getUser({ key, userId, database, fields }) { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: localHostPort || 443, - hostname: localHost || "datasquirel.com", + port, + hostname: host, path: `/api/user/get-user`, }, diff --git a/users/login-user.js b/users/login-user.js index 56531ce..1857212 100644 --- a/users/login-user.js +++ b/users/login-user.js @@ -11,6 +11,7 @@ const fs = require("fs"); const path = require("path"); const encrypt = require("../functions/encrypt"); 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, token, }) { - const scheme = process.env.DSQL_HTTP_SCHEME; - const localHost = process.env.DSQL_LOCAL_HOST; - const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + const { host, port, scheme } = grabHostNames(); const defaultTempLoginFieldName = "temp_login_code"; const emailLoginTempCodeFieldName = email_login @@ -171,9 +170,7 @@ async function loginUser({ const reqPayloadJSON = JSON.stringify(reqPayload); - const httpsRequest = ( - scheme?.match(/^http$/i) ? http : https - ).request( + const httpsRequest = scheme.request( { method: "POST", headers: { @@ -181,8 +178,8 @@ async function loginUser({ "Content-Length": Buffer.from(reqPayloadJSON).length, Authorization: key, }, - port: localHostPort || 443, - hostname: localHost || "datasquirel.com", + port, + hostname: host, path: `/api/user/login-user`, }, diff --git a/users/reauth-user.js b/users/reauth-user.js index 8d62455..73f5377 100644 --- a/users/reauth-user.js +++ b/users/reauth-user.js @@ -13,6 +13,7 @@ const encrypt = require("../functions/encrypt"); const userAuth = require("./user-auth"); 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 */ - const scheme = process.env.DSQL_HTTP_SCHEME; - const localHost = process.env.DSQL_LOCAL_HOST; - const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + const { host, port, scheme } = grabHostNames(); const existingUser = userAuth({ database, @@ -134,9 +133,7 @@ async function reauthUser({ additionalFields, }); - const httpsRequest = ( - scheme?.match(/^http$/i) ? http : https - ).request( + const httpsRequest = scheme.request( { method: "POST", headers: { @@ -144,8 +141,8 @@ async function reauthUser({ "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: localHostPort || 443, - hostname: localHost || "datasquirel.com", + port, + hostname: host, path: `/api/user/reauth-user`, }, diff --git a/users/send-email-code.js b/users/send-email-code.js index 2125db8..f86e51f 100644 --- a/users/send-email-code.js +++ b/users/send-email-code.js @@ -12,6 +12,7 @@ const path = require("path"); const encrypt = require("../functions/encrypt"); const loginLocalUser = require("../engine/user/login-user"); 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, sender, }) { - const scheme = process.env.DSQL_HTTP_SCHEME; - const localHost = process.env.DSQL_LOCAL_HOST; - const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + const { host, port, scheme } = grabHostNames(); const defaultTempLoginFieldName = "temp_login_code"; const emailLoginTempCodeFieldName = temp_code_field @@ -164,9 +163,7 @@ async function sendEmailCode({ ), }); - const httpsRequest = ( - scheme?.match(/^http$/i) ? http : https - ).request( + const httpsRequest = scheme.request( { method: "POST", headers: { @@ -174,8 +171,8 @@ async function sendEmailCode({ "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: localHostPort || 443, - hostname: localHost || "datasquirel.com", + port, + hostname: host, path: `/api/user/send-email-code`, }, diff --git a/users/social/github-auth.js b/users/social/github-auth.js index 94a5ae6..1b42aee 100644 --- a/users/social/github-auth.js +++ b/users/social/github-auth.js @@ -11,6 +11,7 @@ const fs = require("fs"); const path = require("path"); const encrypt = require("../../functions/encrypt"); 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 */ - const scheme = process.env.DSQL_HTTP_SCHEME; - const localHost = process.env.DSQL_LOCAL_HOST; - const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + const { host, port, scheme } = grabHostNames(); if (!key || key?.match(/ /)) { return { @@ -193,9 +192,7 @@ async function githubAuth({ additionalFields, }); - const httpsRequest = ( - scheme?.match(/^http$/i) ? http : https - ).request( + const httpsRequest = scheme.request( { method: "POST", headers: { @@ -203,8 +200,8 @@ async function githubAuth({ "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: localHostPort || 443, - hostname: localHost || "datasquirel.com", + port, + hostname: host, path: `/api/user/github-login`, }, diff --git a/users/social/google-auth.js b/users/social/google-auth.js index 7305f0e..6645312 100644 --- a/users/social/google-auth.js +++ b/users/social/google-auth.js @@ -11,6 +11,7 @@ const fs = require("fs"); const path = require("path"); const encrypt = require("../../functions/encrypt"); 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, additionalFields, }) { - const scheme = process.env.DSQL_HTTP_SCHEME; - const localHost = process.env.DSQL_LOCAL_HOST; - const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + const { host, port, scheme } = grabHostNames(); /** * Check inputs @@ -189,9 +188,7 @@ async function googleAuth({ additionalFields, }); - const httpsRequest = ( - scheme?.match(/^http$/i) ? http : https - ).request( + const httpsRequest = scheme.request( { method: "POST", headers: { @@ -199,8 +196,8 @@ async function googleAuth({ "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: localHostPort || 443, - hostname: localHost || "datasquirel.com", + port, + hostname: host, path: `/api/user/google-login`, }, diff --git a/users/update-user.js b/users/update-user.js index 173523a..2cfe48f 100644 --- a/users/update-user.js +++ b/users/update-user.js @@ -10,6 +10,7 @@ const https = require("https"); const path = require("path"); const fs = require("fs"); 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, } = process.env; - const scheme = process.env.DSQL_HTTP_SCHEME; - const localHost = process.env.DSQL_LOCAL_HOST; - const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + const { host, port, scheme } = grabHostNames(); if ( DSQL_HOST?.match(/./) && @@ -87,7 +86,7 @@ async function updateUser({ key, payload, database }) { database, }); - const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( + const httpsRequest = scheme.request( { method: "POST", headers: { @@ -95,8 +94,8 @@ async function updateUser({ key, payload, database }) { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: localHostPort || 443, - hostname: localHost || "datasquirel.com", + port, + hostname: host, path: `/api/user/update-user`, }, diff --git a/utils/delete-file.js b/utils/delete-file.js index 08a0513..201d7c1 100644 --- a/utils/delete-file.js +++ b/utils/delete-file.js @@ -1,3 +1,5 @@ +// @ts-check + /** * ============================================================================== * Imports @@ -5,6 +7,7 @@ */ const http = require("http"); const https = require("https"); +const grabHostNames = require("../package-shared/utils/grab-host-names"); /** ****************************************************************************** */ /** ****************************************************************************** */ @@ -19,7 +22,7 @@ const https = require("https"); * @property {{ * urlPath: 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 */ @@ -36,15 +39,7 @@ const https = require("https"); * @returns { Promise } - Image Url */ async function uploadImage({ key, url }) { - 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; + const { host, port, scheme } = grabHostNames(); try { /** @@ -55,9 +50,7 @@ async function uploadImage({ key, url }) { const httpResponse = await new Promise((resolve, reject) => { const reqPayload = JSON.stringify({ url: url }); - const httpsRequest = ( - scheme?.match(/^http$/i) ? http : https - ).request( + const httpsRequest = scheme.request( { method: "POST", headers: { @@ -65,8 +58,8 @@ async function uploadImage({ key, url }) { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: remoteHostPort || localHostPort || 443, - hostname: remoteHost || localHost || "datasquirel.com", + port, + hostname: host, path: `/api/query/delete-file`, }, diff --git a/utils/get-schema.js b/utils/get-schema.js index 04ecbaf..576b0d5 100644 --- a/utils/get-schema.js +++ b/utils/get-schema.js @@ -7,6 +7,7 @@ */ const http = require("http"); const https = require("https"); +const grabHostNames = require("../package-shared/utils/grab-host-names"); /** ****************************************************************************** */ /** ****************************************************************************** */ @@ -28,15 +29,7 @@ const https = require("https"); * @returns { Promise } - Return Object */ async function getSchema({ key, database, field, table }) { - 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; + const { host, port, scheme } = grabHostNames(); /** * Make https request @@ -53,7 +46,7 @@ async function getSchema({ key, database, field, table }) { .map((k) => `${k}=${queryObject[k]}`) .join("&"); - (scheme?.match(/^http$/i) ? http : https) + scheme .request( { method: "GET", @@ -61,8 +54,8 @@ async function getSchema({ key, database, field, table }) { "Content-Type": "application/json", Authorization: key, }, - port: remoteHostPort || localHostPort || 443, - hostname: remoteHost || localHost || "datasquirel.com", + port, + hostname: host, path: "/api/query/get-schema" + (query?.match(/./) ? `?${query}` : ""), diff --git a/utils/get.js b/utils/get.js index a756087..8c55832 100644 --- a/utils/get.js +++ b/utils/get.js @@ -11,6 +11,7 @@ const path = require("path"); const fs = require("fs"); const localGet = require("../engine/query/get"); 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 } - Return Object */ async function get({ key, db, query, queryValues, tableName }) { - 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; + const { host, port, scheme } = grabHostNames(); /** * Check for local DB settings @@ -109,12 +102,12 @@ async function get({ key, db, query, queryValues, tableName }) { "Content-Type": "application/json", Authorization: key, }, - port: remoteHostPort || localHostPort || 443, - hostname: remoteHost || localHost || "datasquirel.com", + port, + hostname: host, path: encodeURI(path), }; - (scheme?.match(/^http$/i) ? http : https) + scheme .request( requestObject, diff --git a/utils/post.js b/utils/post.js index 28e1051..6771620 100644 --- a/utils/post.js +++ b/utils/post.js @@ -8,6 +8,7 @@ const https = require("https"); const path = require("path"); const fs = require("fs"); 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 } - Return Object */ async function post({ key, query, queryValues, database, tableName }) { - 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; + const { host, port, scheme } = grabHostNames(); /** * Check for local DB settings @@ -104,7 +97,7 @@ async function post({ key, query, queryValues, database, tableName }) { const reqPayload = reqPayloadString; - const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( + const httpsRequest = scheme.request( { method: "POST", headers: { @@ -112,8 +105,8 @@ async function post({ key, query, queryValues, database, tableName }) { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: remoteHostPort || localHostPort || 443, - hostname: remoteHost || localHost || "datasquirel.com", + port, + hostname: host, path: `/api/query/post`, }, diff --git a/utils/upload-file.js b/utils/upload-file.js index c823618..20bd1f0 100644 --- a/utils/upload-file.js +++ b/utils/upload-file.js @@ -7,6 +7,7 @@ */ const http = require("http"); const https = require("https"); +const grabHostNames = require("../package-shared/utils/grab-host-names"); /** ****************************************************************************** */ /** ****************************************************************************** */ @@ -43,15 +44,7 @@ const https = require("https"); * @returns { Promise } - Return Object */ async function uploadImage({ key, payload }) { - 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; + const { host, port, scheme } = grabHostNames(); try { /** @@ -62,9 +55,7 @@ async function uploadImage({ key, payload }) { const httpResponse = await new Promise((resolve, reject) => { const reqPayload = JSON.stringify(payload); - const httpsRequest = ( - scheme?.match(/^http$/i) ? http : https - ).request( + const httpsRequest = scheme.request( { method: "POST", headers: { @@ -72,8 +63,8 @@ async function uploadImage({ key, payload }) { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: remoteHostPort || localHostPort || 443, - hostname: remoteHost || localHost || "datasquirel.com", + port, + hostname: host, path: `/api/query/add-file`, }, diff --git a/utils/upload-image.js b/utils/upload-image.js index d7b3c08..48d2f7d 100644 --- a/utils/upload-image.js +++ b/utils/upload-image.js @@ -7,6 +7,7 @@ */ const http = require("http"); const https = require("https"); +const grabHostNames = require("../package-shared/utils/grab-host-names"); /** ****************************************************************************** */ /** ****************************************************************************** */ @@ -45,15 +46,7 @@ const https = require("https"); * @returns { Promise } - Return Object */ async function uploadImage({ key, payload }) { - 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; + const { host, port, scheme } = grabHostNames(); try { /** @@ -64,9 +57,7 @@ async function uploadImage({ key, payload }) { const httpResponse = await new Promise((resolve, reject) => { const reqPayload = JSON.stringify(payload); - const httpsRequest = ( - scheme?.match(/^http$/i) ? http : https - ).request( + const httpsRequest = scheme.request( { method: "POST", headers: { @@ -74,8 +65,8 @@ async function uploadImage({ key, payload }) { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: remoteHostPort || localHostPort || 443, - hostname: remoteHost || localHost || "datasquirel.com", + port, + hostname: host, path: `/api/query/add-image`, },