From 357365a1bae8a0021ee2e8443648f680d44b94d1 Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Fri, 13 Dec 2024 14:01:55 +0100 Subject: [PATCH] Updates --- index.d.ts | 4 +- index.js | 7 +- .../utils/backend/parseCookies.d.ts | 5 +- package-shared/utils/backend/parseCookies.js | 76 ++++++++++--------- package.json | 2 +- 5 files changed, 48 insertions(+), 46 deletions(-) diff --git a/index.d.ts b/index.d.ts index 8e319bf..4048a71 100644 --- a/index.d.ts +++ b/index.d.ts @@ -31,6 +31,7 @@ export namespace sql { export { sqlDeleteGenerator }; export { trimSql as trim }; } +import parseCookies = require("./package-shared/utils/backend/parseCookies"); import uploadImage = require("./utils/upload-image"); import uploadFile = require("./utils/upload-file"); import deleteFile = require("./utils/delete-file"); @@ -51,7 +52,7 @@ import sqlInsertGenerator = require("./package-shared/functions/dsql/sql/sql-ins import sqlDeleteGenerator = require("./package-shared/functions/dsql/sql/sql-delete-generator"); import trimSql = require("./package-shared/utils/trim-sql"); export declare namespace utils { - namespace crypto { + export namespace crypto { let encrypt: ({ data, encryptionKey, encryptionSalt }: { data: string; encryptionKey?: string; @@ -67,5 +68,6 @@ export declare namespace utils { encryptionKey?: string; }) => string; } + export { parseCookies }; } export { get, post, getSchema, datasquirelClient as client }; diff --git a/index.js b/index.js index ed75dde..a23709a 100644 --- a/index.js +++ b/index.js @@ -31,11 +31,7 @@ const sqlGenerator = require("./package-shared/functions/dsql/sql/sql-generator" const sqlInsertGenerator = require("./package-shared/functions/dsql/sql/sql-insert-generator"); const sqlDeleteGenerator = require("./package-shared/functions/dsql/sql/sql-delete-generator"); const trimSql = require("./package-shared/utils/trim-sql"); -const serializeQuery = require("./package-shared/utils/serialize-query"); -const serializeCookies = require("./package-shared/utils/serialize-cookies"); -const EJSON = require("./package-shared/utils/ejson"); -const numberfy = require("./package-shared/utils/numberfy"); -const slugify = require("./package-shared/utils/slugify"); +const parseCookies = require("./package-shared/utils/backend/parseCookies"); //////////////////////////////////////// //////////////////////////////////////// @@ -99,6 +95,7 @@ const datasquirel = { decrypt: require("./package-shared/functions/dsql/decrypt"), hash: require("./package-shared/functions/dsql/hashPassword"), }, + parseCookies, }, }; diff --git a/package-shared/utils/backend/parseCookies.d.ts b/package-shared/utils/backend/parseCookies.d.ts index 221c6b3..ab0e658 100644 --- a/package-shared/utils/backend/parseCookies.d.ts +++ b/package-shared/utils/backend/parseCookies.d.ts @@ -1,9 +1,10 @@ -declare function _exports({ request }: { +declare function _exports({ request, cookieString }: { request?: http.IncomingMessage & { [x: string]: any; }; + cookieString?: string; }): { - [x: string]: any; + [x: string]: string; }; export = _exports; import http = require("http"); diff --git a/package-shared/utils/backend/parseCookies.js b/package-shared/utils/backend/parseCookies.js index 6a66e1e..86d28dc 100644 --- a/package-shared/utils/backend/parseCookies.js +++ b/package-shared/utils/backend/parseCookies.js @@ -1,56 +1,58 @@ // @ts-check -/** - * ============================================================================== - * Imports - * ============================================================================== - */ const http = require("http"); /** * Parse request cookies - * ============================================================================== + * =================================================== * - * @description This function takes in a request object and returns the cookies as a JS object + * @description This function takes in a request object and + * returns the cookies as a JS object * * @async * * @param {object} params - main params object * @param {http.IncomingMessage & Object} [params.request] - HTTPS request object + * @param {string} [params.cookieString] * - * @returns {Object} + * @returns {Object} */ -module.exports = function ({ request }) { - if (!request) return {}; +module.exports = function ({ request, cookieString }) { + try { + /** @type {string | undefined} */ + const cookieStr = request + ? request.headers.cookie + : cookieString + ? cookieString + : undefined; - /** @type {string | undefined} */ - const cookieString = request.headers.cookie; + if (!cookieStr) return {}; + + if (!cookieStr || typeof cookieStr !== "string") { + return {}; + } + + /** @type {string[]} */ + const cookieSplitArray = cookieStr.split(";"); + + /** @type {Object} */ + let cookieObject = {}; + + cookieSplitArray.forEach((keyValueString) => { + const [key, value] = keyValueString.split("="); + if (key && typeof key == "string") { + const parsedKey = key.replace(/^ +| +$/, ""); + cookieObject[parsedKey] = + value && typeof value == "string" + ? value.replace(/^ +| +$/, "") + : ""; + } + }); + + return cookieObject; + } catch (/** @type {any} */ error) { + console.log(`ERROR parsing cookies: ${error.message}`); - if (!cookieString || typeof cookieString !== "string") { return {}; } - - /** @type {string[]} */ - const cookieSplitArray = cookieString.split(";"); - - /** @type {*} */ - let cookieObject = {}; - - cookieSplitArray.forEach((keyValueString) => { - const [key, value] = keyValueString.split("="); - if (key && typeof key == "string") { - cookieObject[key.replace(/^ +| +$/, "")] = - value && typeof value == "string" - ? value.replace(/^ +| +$/, "") - : null; - } - }); - - /** - * Make https request - * - * @description make a request to datasquirel.com - */ - - return cookieObject; }; diff --git a/package.json b/package.json index 5751917..2935f2d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/datasquirel", - "version": "3.0.9", + "version": "3.1.0", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": {