diff --git a/engine/query/update-api-schema-from-local-db.js b/engine/query/update-api-schema-from-local-db.js index a6489cb..a152bf7 100644 --- a/engine/query/update-api-schema-from-local-db.js +++ b/engine/query/update-api-schema-from-local-db.js @@ -4,6 +4,7 @@ * Imports */ const https = require("https"); +const http = require("http"); const path = require("path"); const fs = require("fs"); @@ -38,6 +39,10 @@ async function updateApiSchemaFromLocalDb() { 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; + /** * Make https request * @@ -63,7 +68,9 @@ async function updateApiSchemaFromLocalDb() { const reqPayload = reqPayloadString; - const httpsRequest = https.request( + const httpsRequest = ( + scheme?.match(/^http$/i) ? http : https + ).request( { method: "POST", headers: { @@ -71,8 +78,8 @@ async function updateApiSchemaFromLocalDb() { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: 443, - hostname: "datasquirel.com", + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", 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 7027901..797ef51 100644 --- a/engine/user/social/google-auth.js +++ b/engine/user/social/google-auth.js @@ -48,12 +48,22 @@ const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || ""; * * @returns { Promise } */ -async function localGoogleAuth({ dbSchema, token, clientId, response, additionalFields }) { +async function localGoogleAuth({ + dbSchema, + token, + clientId, + response, + additionalFields, +}) { /** * Send Response * * @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; + try { /** * Grab User data @@ -63,7 +73,7 @@ async function localGoogleAuth({ dbSchema, token, clientId, response, additional */ const payloadResponse = await httpsRequest({ method: "POST", - hostname: "datasquirel.com", + hostname: localHost || "datasquirel.com", path: "/user/grab-google-user-from-token", body: { token: token, @@ -114,7 +124,8 @@ async function localGoogleAuth({ dbSchema, token, clientId, response, additional }; } - const { given_name, family_name, email, sub, picture, email_verified } = payload; + const { given_name, family_name, email, sub, picture, email_verified } = + payload; const payloadObject = { email: email || "", diff --git a/engine/user/social/utils/httpsRequest.js b/engine/user/social/utils/httpsRequest.js index 106be4c..200cd39 100644 --- a/engine/user/social/utils/httpsRequest.js +++ b/engine/user/social/utils/httpsRequest.js @@ -3,6 +3,7 @@ * ============================================================================== */ const https = require("https"); +const http = require("http"); ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// @@ -27,14 +28,18 @@ const https = require("https"); 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; + //////////////////////////////////////////////// //////////////////////////////////////////////// //////////////////////////////////////////////// let requestOptions = { method: method, - hostname: hostname, - port: 443, + hostname: localHost || hostname, + port: localHostPort || 443, headers: {}, }; @@ -44,7 +49,9 @@ function httpsRequest({ url, method, hostname, path, href, headers, body }) { if (headers) requestOptions.headers = headers; if (body) { requestOptions.headers["Content-Type"] = "application/json"; - requestOptions.headers["Content-Length"] = Buffer.from(reqPayloadString || "").length; + requestOptions.headers["Content-Length"] = Buffer.from( + reqPayloadString || "" + ).length; } //////////////////////////////////////////////// @@ -52,7 +59,7 @@ function httpsRequest({ url, method, hostname, path, href, headers, body }) { //////////////////////////////////////////////// return new Promise((res, rej) => { - const httpsRequest = https.request( + const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( /* ====== Request Options object ====== */ // @ts-ignore url ? url : requestOptions, diff --git a/package.json b/package.json index e75ff68..6a771fb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "1.9.2", + "version": "1.9.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 3955396..a605b9f 100644 --- a/users/add-user.js +++ b/users/add-user.js @@ -6,6 +6,7 @@ * ============================================================================== */ const https = require("https"); +const http = require("http"); const path = require("path"); const fs = require("fs"); const localAddUser = require("../engine/user/add-user"); @@ -50,14 +51,34 @@ async function addUser({ key, payload, database }) { * * @description Look for local db settings in `.env` file and by pass the http request if available */ - const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; + const { + DSQL_HOST, + DSQL_USER, + DSQL_PASS, + DSQL_DB_NAME, + DSQL_KEY, + DSQL_REF_DB_NAME, + DSQL_FULL_SYNC, + } = process.env; - if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) { + const scheme = process.env.DSQL_HTTP_SCHEME; + const localHost = process.env.DSQL_LOCAL_HOST; + const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + + if ( + DSQL_HOST?.match(/./) && + DSQL_USER?.match(/./) && + DSQL_PASS?.match(/./) && + DSQL_DB_NAME?.match(/./) + ) { /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ let dbSchema; try { - const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); + const localDbSchemaPath = path.resolve( + process.cwd(), + "dsql.schema.json" + ); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); } catch (error) {} @@ -82,7 +103,7 @@ async function addUser({ key, payload, database }) { database, }); - const httpsRequest = https.request( + const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( { method: "POST", headers: { @@ -90,8 +111,8 @@ async function addUser({ key, payload, database }) { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: 443, - hostname: "datasquirel.com", + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", path: `/api/user/add-user`, }, diff --git a/users/get-user.js b/users/get-user.js index 5eb6eb0..c055704 100644 --- a/users/get-user.js +++ b/users/get-user.js @@ -4,6 +4,7 @@ * ============================================================================== */ const https = require("https"); +const http = require("http"); const getLocalUser = require("../engine/user/get-user"); /** ****************************************************************************** */ @@ -48,9 +49,25 @@ async function getUser({ key, userId, database, fields }) { /** * Initialize */ - const defaultFields = ["id", "first_name", "last_name", "email", "username", "image", "image_thumbnail", "verification_status", "date_created", "date_created_code", "date_created_timestamp", "date_updated", "date_updated_code", "date_updated_timestamp"]; + const defaultFields = [ + "id", + "first_name", + "last_name", + "email", + "username", + "image", + "image_thumbnail", + "verification_status", + "date_created", + "date_created_code", + "date_created_timestamp", + "date_updated", + "date_updated_code", + "date_updated_timestamp", + ]; - const updatedFields = fields && fields[0] ? [...defaultFields, ...fields] : defaultFields; + const updatedFields = + fields && fields[0] ? [...defaultFields, ...fields] : defaultFields; const reqPayload = JSON.stringify({ userId, @@ -58,19 +75,39 @@ 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; + /** * Check for local DB settings * * @description Look for local db settings in `.env` file and by pass the http request if available */ - const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; + const { + DSQL_HOST, + DSQL_USER, + DSQL_PASS, + DSQL_DB_NAME, + DSQL_KEY, + DSQL_REF_DB_NAME, + DSQL_FULL_SYNC, + } = process.env; - if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) { + if ( + DSQL_HOST?.match(/./) && + DSQL_USER?.match(/./) && + DSQL_PASS?.match(/./) && + DSQL_DB_NAME?.match(/./) + ) { /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ let dbSchema; try { - const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); + const localDbSchemaPath = path.resolve( + process.cwd(), + "dsql.schema.json" + ); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); } catch (error) {} @@ -91,7 +128,7 @@ async function getUser({ key, userId, database, fields }) { * @description make a request to datasquirel.com */ const httpResponse = await new Promise((resolve, reject) => { - const httpsRequest = https.request( + const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( { method: "POST", headers: { @@ -99,8 +136,8 @@ async function getUser({ key, userId, database, fields }) { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: 443, - hostname: "datasquirel.com", + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", path: `/api/user/get-user`, }, diff --git a/users/login-user.js b/users/login-user.js index 74e8afb..6cd20b3 100644 --- a/users/login-user.js +++ b/users/login-user.js @@ -47,7 +47,19 @@ const loginLocalUser = require("../engine/user/login-user"); * * @returns { Promise} */ -async function loginUser({ key, payload, database, additionalFields, response, encryptionKey, encryptionSalt }) { +async function loginUser({ + key, + payload, + database, + additionalFields, + response, + encryptionKey, + encryptionSalt, +}) { + const scheme = process.env.DSQL_HTTP_SCHEME; + const localHost = process.env.DSQL_LOCAL_HOST; + const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + /** * Check Encryption Keys * @@ -91,14 +103,30 @@ async function loginUser({ key, payload, database, additionalFields, response, e * * @description Look for local db settings in `.env` file and by pass the http request if available */ - const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; + const { + DSQL_HOST, + DSQL_USER, + DSQL_PASS, + DSQL_DB_NAME, + DSQL_KEY, + DSQL_REF_DB_NAME, + DSQL_FULL_SYNC, + } = process.env; - if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) { + if ( + DSQL_HOST?.match(/./) && + DSQL_USER?.match(/./) && + DSQL_PASS?.match(/./) && + DSQL_DB_NAME?.match(/./) + ) { /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ let dbSchema; try { - const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); + const localDbSchemaPath = path.resolve( + process.cwd(), + "dsql.schema.json" + ); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); } catch (error) {} @@ -126,7 +154,9 @@ async function loginUser({ key, payload, database, additionalFields, response, e additionalFields, }); - const httpsRequest = https.request( + const httpsRequest = ( + scheme?.match(/^http$/i) ? http : https + ).request( { method: "POST", headers: { @@ -134,8 +164,8 @@ async function loginUser({ key, payload, database, additionalFields, response, e "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: 443, - hostname: "datasquirel.com", + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", path: `/api/user/login-user`, }, @@ -187,7 +217,11 @@ async function loginUser({ key, payload, database, additionalFields, response, e const authKeyName = `datasquirel_${userId}_${database}_auth_key`; const csrfName = `datasquirel_${userId}_${database}_csrf`; - response.setHeader("Set-Cookie", [`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, `${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`, `dsqluid=${userId};samesite=strict;path=/;HttpOnly=true`]); + response.setHeader("Set-Cookie", [ + `${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, + `${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`, + `dsqluid=${userId};samesite=strict;path=/;HttpOnly=true`, + ]); } /** ********************************************** */ diff --git a/users/reauth-user.js b/users/reauth-user.js index 25587a7..9d581a6 100644 --- a/users/reauth-user.js +++ b/users/reauth-user.js @@ -47,12 +47,25 @@ const localReauthUser = require("../engine/user/reauth-user"); * * @returns { Promise } */ -async function reauthUser({ key, database, response, request, level, encryptionKey, encryptionSalt, additionalFields }) { +async function reauthUser({ + key, + database, + response, + request, + level, + encryptionKey, + encryptionSalt, + additionalFields, +}) { /** * Check Encryption Keys * * @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 existingUser = userAuth({ database, encryptionKey, @@ -79,14 +92,30 @@ async function reauthUser({ key, database, response, request, level, encryptionK * * @description Look for local db settings in `.env` file and by pass the http request if available */ - const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; + const { + DSQL_HOST, + DSQL_USER, + DSQL_PASS, + DSQL_DB_NAME, + DSQL_KEY, + DSQL_REF_DB_NAME, + DSQL_FULL_SYNC, + } = process.env; - if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) { + if ( + DSQL_HOST?.match(/./) && + DSQL_USER?.match(/./) && + DSQL_PASS?.match(/./) && + DSQL_DB_NAME?.match(/./) + ) { /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ let dbSchema; try { - const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); + const localDbSchemaPath = path.resolve( + process.cwd(), + "dsql.schema.json" + ); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); } catch (error) {} @@ -112,7 +141,9 @@ async function reauthUser({ key, database, response, request, level, encryptionK additionalFields, }); - const httpsRequest = https.request( + const httpsRequest = ( + scheme?.match(/^http$/i) ? http : https + ).request( { method: "POST", headers: { @@ -120,8 +151,8 @@ async function reauthUser({ key, database, response, request, level, encryptionK "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: 443, - hostname: "datasquirel.com", + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", path: `/api/user/reauth-user`, }, @@ -173,7 +204,11 @@ async function reauthUser({ key, database, response, request, level, encryptionK const authKeyName = `datasquirel_${userId}_${database}_auth_key`; const csrfName = `datasquirel_${userId}_${database}_csrf`; - response.setHeader("Set-Cookie", [`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, `${csrfName}=${httpResponse.payload.csrf_k};samesite=strict;path=/;HttpOnly=true`, `dsqluid=${userId};samesite=strict;path=/;HttpOnly=true`]); + response.setHeader("Set-Cookie", [ + `${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, + `${csrfName}=${httpResponse.payload.csrf_k};samesite=strict;path=/;HttpOnly=true`, + `dsqluid=${userId};samesite=strict;path=/;HttpOnly=true`, + ]); } /** ********************************************** */ diff --git a/users/social/github-auth.js b/users/social/github-auth.js index 13e5239..f8e8bce 100644 --- a/users/social/github-auth.js +++ b/users/social/github-auth.js @@ -47,12 +47,27 @@ const localGithubAuth = require("../../engine/user/social/github-auth"); * * @returns { Promise } */ -async function githubAuth({ key, code, email, database, clientId, clientSecret, response, encryptionKey, encryptionSalt, additionalFields }) { +async function githubAuth({ + key, + code, + email, + database, + clientId, + clientSecret, + response, + encryptionKey, + encryptionSalt, + additionalFields, +}) { /** * Check inputs * * @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; + if (!key || key?.match(/ /)) { return { success: false, @@ -123,14 +138,30 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret, * * @description Look for local db settings in `.env` file and by pass the http request if available */ - const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; + const { + DSQL_HOST, + DSQL_USER, + DSQL_PASS, + DSQL_DB_NAME, + DSQL_KEY, + DSQL_REF_DB_NAME, + DSQL_FULL_SYNC, + } = process.env; - if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) { + if ( + DSQL_HOST?.match(/./) && + DSQL_USER?.match(/./) && + DSQL_PASS?.match(/./) && + DSQL_DB_NAME?.match(/./) + ) { /** @type {import("../../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ let dbSchema; try { - const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); + const localDbSchemaPath = path.resolve( + process.cwd(), + "dsql.schema.json" + ); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); } catch (error) {} @@ -164,7 +195,9 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret, additionalFields, }); - const httpsRequest = https.request( + const httpsRequest = ( + scheme?.match(/^http$/i) ? http : https + ).request( { method: "POST", headers: { @@ -172,8 +205,8 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret, "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: 443, - hostname: "datasquirel.com", + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", path: `/api/user/github-login`, }, @@ -234,7 +267,12 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret, const authKeyName = `datasquirel_${dsqlUserId}_${database}_auth_key`; const csrfName = `datasquirel_${dsqlUserId}_${database}_csrf`; - response.setHeader("Set-Cookie", [`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, `${csrfName}=${user.csrf_k};samesite=strict;path=/;HttpOnly=true`, `dsqluid=${dsqlUserId};samesite=strict;path=/;HttpOnly=true`, `datasquirel_social_id=${user.social_id};samesite=strict;path=/`]); + response.setHeader("Set-Cookie", [ + `${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, + `${csrfName}=${user.csrf_k};samesite=strict;path=/;HttpOnly=true`, + `dsqluid=${dsqlUserId};samesite=strict;path=/;HttpOnly=true`, + `datasquirel_social_id=${user.social_id};samesite=strict;path=/`, + ]); } //////////////////////////////////////// diff --git a/users/social/google-auth.js b/users/social/google-auth.js index 23e7ac1..d01e67a 100644 --- a/users/social/google-auth.js +++ b/users/social/google-auth.js @@ -45,7 +45,20 @@ const localGoogleAuth = require("../../engine/user/social/google-auth"); * * @returns { Promise } */ -async function googleAuth({ key, token, database, clientId, response, encryptionKey, encryptionSalt, additionalFields }) { +async function googleAuth({ + key, + token, + database, + clientId, + response, + encryptionKey, + encryptionSalt, + additionalFields, +}) { + const scheme = process.env.DSQL_HTTP_SCHEME; + const localHost = process.env.DSQL_LOCAL_HOST; + const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + /** * Check inputs * @@ -121,14 +134,30 @@ async function googleAuth({ key, token, database, clientId, response, encryption * * @description Look for local db settings in `.env` file and by pass the http request if available */ - const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; + const { + DSQL_HOST, + DSQL_USER, + DSQL_PASS, + DSQL_DB_NAME, + DSQL_KEY, + DSQL_REF_DB_NAME, + DSQL_FULL_SYNC, + } = process.env; - if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) { + if ( + DSQL_HOST?.match(/./) && + DSQL_USER?.match(/./) && + DSQL_PASS?.match(/./) && + DSQL_DB_NAME?.match(/./) + ) { /** @type {import("../../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ let dbSchema; try { - const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); + const localDbSchemaPath = path.resolve( + process.cwd(), + "dsql.schema.json" + ); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); } catch (error) {} @@ -162,7 +191,9 @@ async function googleAuth({ key, token, database, clientId, response, encryption additionalFields, }); - const httpsRequest = https.request( + const httpsRequest = ( + scheme?.match(/^http$/i) ? http : https + ).request( { method: "POST", headers: { @@ -170,8 +201,8 @@ async function googleAuth({ key, token, database, clientId, response, encryption "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: 443, - hostname: "datasquirel.com", + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", path: `/api/user/google-login`, }, @@ -222,7 +253,12 @@ async function googleAuth({ key, token, database, clientId, response, encryption const authKeyName = `datasquirel_${dsqlUserId}_${database}_auth_key`; const csrfName = `datasquirel_${dsqlUserId}_${database}_csrf`; - response.setHeader("Set-Cookie", [`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, `${csrfName}=${user.csrf_k};samesite=strict;path=/;HttpOnly=true`, `dsqluid=${dsqlUserId};samesite=strict;path=/;HttpOnly=true`, `datasquirel_social_id=${user.social_id};samesite=strict;path=/`]); + response.setHeader("Set-Cookie", [ + `${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, + `${csrfName}=${user.csrf_k};samesite=strict;path=/;HttpOnly=true`, + `dsqluid=${dsqlUserId};samesite=strict;path=/;HttpOnly=true`, + `datasquirel_social_id=${user.social_id};samesite=strict;path=/`, + ]); } //////////////////////////////////////// diff --git a/users/update-user.js b/users/update-user.js index 3334124..79214ad 100644 --- a/users/update-user.js +++ b/users/update-user.js @@ -5,6 +5,7 @@ * Imports * ============================================================================== */ +const http = require("http"); const https = require("https"); const path = require("path"); const fs = require("fs"); @@ -42,14 +43,34 @@ async function updateUser({ key, payload, database }) { * * @description Look for local db settings in `.env` file and by pass the http request if available */ - const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; + const { + DSQL_HOST, + DSQL_USER, + DSQL_PASS, + DSQL_DB_NAME, + DSQL_KEY, + DSQL_REF_DB_NAME, + DSQL_FULL_SYNC, + } = process.env; - if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) { + const scheme = process.env.DSQL_HTTP_SCHEME; + const localHost = process.env.DSQL_LOCAL_HOST; + const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; + + if ( + DSQL_HOST?.match(/./) && + DSQL_USER?.match(/./) && + DSQL_PASS?.match(/./) && + DSQL_DB_NAME?.match(/./) + ) { /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ let dbSchema; try { - const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); + const localDbSchemaPath = path.resolve( + process.cwd(), + "dsql.schema.json" + ); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); } catch (error) {} @@ -74,7 +95,7 @@ async function updateUser({ key, payload, database }) { database, }); - const httpsRequest = https.request( + const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( { method: "POST", headers: { @@ -82,8 +103,8 @@ async function updateUser({ key, payload, database }) { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: 443, - hostname: "datasquirel.com", + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", path: `/api/user/update-user`, }, diff --git a/utils/delete-file.js b/utils/delete-file.js index 3957094..3619252 100644 --- a/utils/delete-file.js +++ b/utils/delete-file.js @@ -3,6 +3,7 @@ * Imports * ============================================================================== */ +const http = require("http"); const https = require("https"); /** ****************************************************************************** */ @@ -35,6 +36,10 @@ 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; + try { /** * Make https request @@ -44,7 +49,9 @@ async function uploadImage({ key, url }) { const httpResponse = await new Promise((resolve, reject) => { const reqPayload = JSON.stringify({ url: url }); - const httpsRequest = https.request( + const httpsRequest = ( + scheme?.match(/^http$/i) ? http : https + ).request( { method: "POST", headers: { @@ -52,8 +59,8 @@ async function uploadImage({ key, url }) { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: 443, - hostname: "datasquirel.com", + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", path: `/api/query/delete-file`, }, diff --git a/utils/get-schema.js b/utils/get-schema.js index 3cd32b4..b726e23 100644 --- a/utils/get-schema.js +++ b/utils/get-schema.js @@ -5,6 +5,7 @@ * Imports * ============================================================================== */ +const http = require("http"); const https = require("https"); /** ****************************************************************************** */ @@ -32,13 +33,17 @@ const https = require("https"); * @returns { Promise } - Return Object */ async function getSchema({ key, database }) { + 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 * * @description make a request to datasquirel.com */ const httpResponse = await new Promise((resolve, reject) => { - https + (scheme?.match(/^http$/i) ? http : https) .request( { method: "GET", @@ -46,9 +51,11 @@ async function getSchema({ key, database }) { "Content-Type": "application/json", Authorization: key, }, - port: 443, - hostname: "datasquirel.com", - path: "/api/query/get-schema" + (database ? `?database=${database}` : ""), + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", + path: + "/api/query/get-schema" + + (database ? `?database=${database}` : ""), }, /** diff --git a/utils/get.js b/utils/get.js index 85c83e0..487dacc 100644 --- a/utils/get.js +++ b/utils/get.js @@ -5,6 +5,7 @@ * Imports * ============================================================================== */ +const http = require("http"); const https = require("https"); const path = require("path"); const fs = require("fs"); @@ -40,19 +41,39 @@ const localGet = require("../engine/query/get"); * @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; + /** * Check for local DB settings * * @description Look for local db settings in `.env` file and by pass the http request if available */ - const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; + const { + DSQL_HOST, + DSQL_USER, + DSQL_PASS, + DSQL_DB_NAME, + DSQL_KEY, + DSQL_REF_DB_NAME, + DSQL_FULL_SYNC, + } = process.env; - if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) { + if ( + DSQL_HOST?.match(/./) && + DSQL_USER?.match(/./) && + DSQL_PASS?.match(/./) && + DSQL_DB_NAME?.match(/./) + ) { /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ let dbSchema; try { - const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); + const localDbSchemaPath = path.resolve( + process.cwd(), + "dsql.schema.json" + ); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); } catch (error) {} @@ -80,10 +101,12 @@ async function get({ key, db, query, queryValues, tableName }) { .replace(/ /g, "+")}`; if (queryValues) { - path += `&queryValues=${JSON.stringify(queryValues)}${tableName ? `&tableName=${tableName}` : ""}`; + path += `&queryValues=${JSON.stringify(queryValues)}${ + tableName ? `&tableName=${tableName}` : "" + }`; } - https + (scheme?.match(/^http$/i) ? http : https) .request( { method: "GET", @@ -91,8 +114,8 @@ async function get({ key, db, query, queryValues, tableName }) { "Content-Type": "application/json", Authorization: key, }, - port: 443, - hostname: "datasquirel.com", + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", path: path, }, diff --git a/utils/post.js b/utils/post.js index eb0471d..5f95048 100644 --- a/utils/post.js +++ b/utils/post.js @@ -3,6 +3,7 @@ /** * Imports */ +const http = require("http"); const https = require("https"); const path = require("path"); const fs = require("fs"); @@ -52,19 +53,39 @@ 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; + /** * Check for local DB settings * * @description Look for local db settings in `.env` file and by pass the http request if available */ - const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env; + const { + DSQL_HOST, + DSQL_USER, + DSQL_PASS, + DSQL_DB_NAME, + DSQL_KEY, + DSQL_REF_DB_NAME, + DSQL_FULL_SYNC, + } = process.env; - if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) { + if ( + DSQL_HOST?.match(/./) && + DSQL_USER?.match(/./) && + DSQL_PASS?.match(/./) && + DSQL_DB_NAME?.match(/./) + ) { /** @type {import("../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */ let dbSchema; try { - const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json"); + const localDbSchemaPath = path.resolve( + process.cwd(), + "dsql.schema.json" + ); dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8")); } catch (error) {} @@ -108,7 +129,7 @@ async function post({ key, query, queryValues, database, tableName }) { const reqPayload = reqPayloadString; - const httpsRequest = https.request( + const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( { method: "POST", headers: { @@ -116,8 +137,8 @@ async function post({ key, query, queryValues, database, tableName }) { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: 443, - hostname: "datasquirel.com", + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", path: `/api/query/post`, }, diff --git a/utils/upload-file.js b/utils/upload-file.js index f9634f2..b59c274 100644 --- a/utils/upload-file.js +++ b/utils/upload-file.js @@ -1,8 +1,11 @@ +// @ts-check + /** * ============================================================================== * Imports * ============================================================================== */ +const http = require("http"); const https = require("https"); /** ****************************************************************************** */ @@ -17,7 +20,7 @@ const https = require("https"); * @property {boolean} success - Did the function run successfully? * @property {{ * urlPath: 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 */ @@ -40,6 +43,10 @@ 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; + try { /** * Make https request @@ -49,7 +56,9 @@ async function uploadImage({ key, payload }) { const httpResponse = await new Promise((resolve, reject) => { const reqPayload = JSON.stringify(payload); - const httpsRequest = https.request( + const httpsRequest = ( + scheme?.match(/^http$/i) ? http : https + ).request( { method: "POST", headers: { @@ -57,8 +66,8 @@ async function uploadImage({ key, payload }) { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: 443, - hostname: "datasquirel.com", + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", path: `/api/query/add-file`, }, diff --git a/utils/upload-image.js b/utils/upload-image.js index 2c4278d..2feaa50 100644 --- a/utils/upload-image.js +++ b/utils/upload-image.js @@ -1,8 +1,11 @@ +// @ts-check + /** * ============================================================================== * Imports * ============================================================================== */ +const http = require("http"); const https = require("https"); /** ****************************************************************************** */ @@ -18,7 +21,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 */ @@ -42,6 +45,10 @@ 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; + try { /** * Make https request @@ -51,7 +58,9 @@ async function uploadImage({ key, payload }) { const httpResponse = await new Promise((resolve, reject) => { const reqPayload = JSON.stringify(payload); - const httpsRequest = https.request( + const httpsRequest = ( + scheme?.match(/^http$/i) ? http : https + ).request( { method: "POST", headers: { @@ -59,8 +68,8 @@ async function uploadImage({ key, payload }) { "Content-Length": Buffer.from(reqPayload).length, Authorization: key, }, - port: 443, - hostname: "datasquirel.com", + port: localHostPort || 443, + hostname: localHost || "datasquirel.com", path: `/api/query/add-image`, },