From f8e1d7882475c9cc64fd953c60b6e5211b1f3a06 Mon Sep 17 00:00:00 2001 From: Adrian Tuschek Date: Fri, 29 Jul 2022 18:03:43 -0700 Subject: [PATCH 1/4] Debugging effective fial url (Work in Progress) --- .../helpers/utils/EffectiveURL.ts | 4 +- packages/hoppscotch-data/src/pathVariables.ts | 74 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 packages/hoppscotch-data/src/pathVariables.ts diff --git a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts index fbdad1a6b..06814bda4 100644 --- a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts +++ b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts @@ -14,10 +14,12 @@ import { HoppRESTHeader, HoppRESTParam, } from "@hoppscotch/data" +import { parseTemplateStringV } from "@hoppscotch/data/src/pathVariables" import { arrayFlatMap, arraySort } from "../functional/array" import { toFormData } from "../functional/formData" import { tupleToRecord } from "../functional/record" import { getGlobalVariables } from "~/newstore/environments" +import { parseTemplateStringEV } from "@hoppscotch/data/src/pathVariables" export interface EffectiveHoppRESTRequest extends HoppRESTRequest { /** @@ -306,7 +308,7 @@ export function getEffectiveRESTRequest( return { ...request, - effectiveFinalURL: parseTemplateString( + effectiveFinalURL: parseTemplateStringV( request.endpoint, envVariables, request.vars diff --git a/packages/hoppscotch-data/src/pathVariables.ts b/packages/hoppscotch-data/src/pathVariables.ts new file mode 100644 index 000000000..4809ace30 --- /dev/null +++ b/packages/hoppscotch-data/src/pathVariables.ts @@ -0,0 +1,74 @@ +import { pipe } from "fp-ts/function" +import * as E from "fp-ts/Either" +import {parseTemplateStringE} from "./environment"; + +export type Environment = { + name: string + variables: { + key: string + value: string + }[] +} + +export type Variables = { + key: string + value: string +}[] + +const REGEX_PATH_VAR = /{{([^>]*)}}/g // "{{myVariable}}" + +/** + * How much times can we expand environment variables + */ +const ENV_MAX_EXPAND_LIMIT = 10 + +/** + * Error state when there is a suspected loop while + * recursively expanding variables + */ +const REGEX_ENV_VAR = /<<([^>]*)>>/g // "<>" +const ENV_EXPAND_LOOP = "ENV_EXPAND_LOOP" as const + + +export function parseTemplateStringEV( + str: string, + variables: Environment["variables"], + pathVariables : Variables +) { + if (!variables || !str || !pathVariables) { + return E.right(str) + } + + let result = str + let depth = 0 + + while (result.match(REGEX_ENV_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) { + result = decodeURI(encodeURI(result)).replace( + REGEX_ENV_VAR, + (_, p1) => variables.find((x) => x.key === p1)?.value || "" + ) + depth++ + } + while (result.match(REGEX_PATH_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) { + result = decodeURI(encodeURI(result)).replace( + REGEX_ENV_VAR, + (_, p1) => pathVariables.find((x) => x.key === p1)?.value || "" + ) + } + return depth > ENV_MAX_EXPAND_LIMIT + ? E.left(ENV_EXPAND_LOOP) + : E.right(result) +} + +/** + * @deprecated Use `parseTemplateStringE` instead + */ + +export const parseTemplateStringEV = ( + str: string, + variables: Environment["variables"] +) => + pipe( + parseTemplateStringEV(str, variables, pathVariables), + E.getOrElse(() => str) + ) From 14183d8b917cf9af1550226422200fbbd7f1dde5 Mon Sep 17 00:00:00 2001 From: Jason Casareno Date: Fri, 29 Jul 2022 18:07:06 -0700 Subject: [PATCH 2/4] Debugging effective final url (WIP) --- packages/hoppscotch-app/helpers/utils/EffectiveURL.ts | 1 - packages/hoppscotch-data/src/pathVariables.ts | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts index 06814bda4..e8e8e08ff 100644 --- a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts +++ b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts @@ -19,7 +19,6 @@ import { arrayFlatMap, arraySort } from "../functional/array" import { toFormData } from "../functional/formData" import { tupleToRecord } from "../functional/record" import { getGlobalVariables } from "~/newstore/environments" -import { parseTemplateStringEV } from "@hoppscotch/data/src/pathVariables" export interface EffectiveHoppRESTRequest extends HoppRESTRequest { /** diff --git a/packages/hoppscotch-data/src/pathVariables.ts b/packages/hoppscotch-data/src/pathVariables.ts index 4809ace30..411e8e22f 100644 --- a/packages/hoppscotch-data/src/pathVariables.ts +++ b/packages/hoppscotch-data/src/pathVariables.ts @@ -64,9 +64,10 @@ export function parseTemplateStringEV( * @deprecated Use `parseTemplateStringE` instead */ -export const parseTemplateStringEV = ( +export const parseTemplateStringV = ( str: string, - variables: Environment["variables"] + variables: Environment["variables"], + pathVariables: Variables ) => pipe( parseTemplateStringEV(str, variables, pathVariables), From 4f8b346024bdacf9ee49ab663242b7cb522ac6d4 Mon Sep 17 00:00:00 2001 From: Jason Casareno Date: Mon, 1 Aug 2022 14:36:17 -0700 Subject: [PATCH 3/4] Deleted unwanted file --- packages/hoppscotch-data/src/pathVariables.ts | 75 ------------------- 1 file changed, 75 deletions(-) delete mode 100644 packages/hoppscotch-data/src/pathVariables.ts diff --git a/packages/hoppscotch-data/src/pathVariables.ts b/packages/hoppscotch-data/src/pathVariables.ts deleted file mode 100644 index 411e8e22f..000000000 --- a/packages/hoppscotch-data/src/pathVariables.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { pipe } from "fp-ts/function" -import * as E from "fp-ts/Either" -import {parseTemplateStringE} from "./environment"; - -export type Environment = { - name: string - variables: { - key: string - value: string - }[] -} - -export type Variables = { - key: string - value: string -}[] - -const REGEX_PATH_VAR = /{{([^>]*)}}/g // "{{myVariable}}" - -/** - * How much times can we expand environment variables - */ -const ENV_MAX_EXPAND_LIMIT = 10 - -/** - * Error state when there is a suspected loop while - * recursively expanding variables - */ -const REGEX_ENV_VAR = /<<([^>]*)>>/g // "<>" -const ENV_EXPAND_LOOP = "ENV_EXPAND_LOOP" as const - - -export function parseTemplateStringEV( - str: string, - variables: Environment["variables"], - pathVariables : Variables -) { - if (!variables || !str || !pathVariables) { - return E.right(str) - } - - let result = str - let depth = 0 - - while (result.match(REGEX_ENV_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) { - result = decodeURI(encodeURI(result)).replace( - REGEX_ENV_VAR, - (_, p1) => variables.find((x) => x.key === p1)?.value || "" - ) - depth++ - } - while (result.match(REGEX_PATH_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) { - result = decodeURI(encodeURI(result)).replace( - REGEX_ENV_VAR, - (_, p1) => pathVariables.find((x) => x.key === p1)?.value || "" - ) - } - return depth > ENV_MAX_EXPAND_LIMIT - ? E.left(ENV_EXPAND_LOOP) - : E.right(result) -} - -/** - * @deprecated Use `parseTemplateStringE` instead - */ - -export const parseTemplateStringV = ( - str: string, - variables: Environment["variables"], - pathVariables: Variables -) => - pipe( - parseTemplateStringEV(str, variables, pathVariables), - E.getOrElse(() => str) - ) From 6a3308379004f8398aea9de08dd9521091ff7f85 Mon Sep 17 00:00:00 2001 From: Adrian Tuschek Date: Mon, 1 Aug 2022 16:12:35 -0700 Subject: [PATCH 4/4] Fixed recursive variables bug --- packages/hoppscotch-data/src/variables.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/hoppscotch-data/src/variables.ts b/packages/hoppscotch-data/src/variables.ts index 239a8d2ac..77c1be3a5 100644 --- a/packages/hoppscotch-data/src/variables.ts +++ b/packages/hoppscotch-data/src/variables.ts @@ -40,6 +40,7 @@ export function parseTemplateStringEV( let result = str let depth = 0 + let errorBound = 0 while (result.match(REGEX_ENV_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) { result = decodeURI(encodeURI(result)).replace( @@ -50,18 +51,21 @@ export function parseTemplateStringEV( } /** - * TODO: Create an error state when there is a suspected loop while recursively expanding these variables + * TODO: Create an error statement for variables */ - while (result.match(REGEX_MY_VAR) != null && depth <= ENV_MAX_EXPAND_LIMIT) { + while (result.match(REGEX_MY_VAR) != null && errorBound <= ENV_MAX_EXPAND_LIMIT) { result = decodeURI(encodeURI(result)).replace( REGEX_MY_VAR, (_, p1) => myVariables.find((x) => x.key === p1)?.value || "" ) + errorBound++ } - return depth > ENV_MAX_EXPAND_LIMIT - ? E.left(ENV_EXPAND_LOOP) - : E.right(result) + if (depth <= ENV_MAX_EXPAND_LIMIT && errorBound <= ENV_MAX_EXPAND_LIMIT) { + return E.right(result) + } else { + return E.left(ENV_EXPAND_LOOP) + } } /**