From d442f0d707b6c85b32325124006638f27173efee Mon Sep 17 00:00:00 2001 From: Vladimir Gorkavenko <32727352+vgorkavenko@users.noreply.github.com> Date: Thu, 19 Sep 2024 16:30:55 +0200 Subject: [PATCH] Fix/for infra (#31) * feat: add urljoin * fix: dockerfiles --- Dockerfile | 4 +- Dockerfile.cli | 2 +- src/common/providers/base/rest-provider.ts | 6 +- src/common/providers/base/utils/func.ts | 66 ++++++++++++++++++++++ 4 files changed, 72 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 939f50c..e6c4f64 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:20.12.1-alpine as building +FROM node:20.12.1-alpine AS building WORKDIR /app @@ -22,6 +22,6 @@ RUN mkdir -p ./storage/ && chown -R node:node ./storage/ USER node HEALTHCHECK --interval=120s --timeout=60s --retries=3 \ - CMD sh -c "wget -nv -t1 --spider http://localhost:$HTTP_PORT/health" || exit 1 + CMD sh -c "wget -nv -t1 --spider http://127.0.0.1:$HTTP_PORT/health" || exit 1 CMD ["yarn", "start:prod"] diff --git a/Dockerfile.cli b/Dockerfile.cli index 97e3a66..de86b72 100644 --- a/Dockerfile.cli +++ b/Dockerfile.cli @@ -1,4 +1,4 @@ -FROM node:20.12.1-alpine as building +FROM node:20.12.1-alpine AS building WORKDIR /app diff --git a/src/common/providers/base/rest-provider.ts b/src/common/providers/base/rest-provider.ts index 41c4d99..a5d5254 100644 --- a/src/common/providers/base/rest-provider.ts +++ b/src/common/providers/base/rest-provider.ts @@ -3,7 +3,7 @@ import { request } from 'undici'; import { IncomingHttpHeaders } from 'undici/types/header'; import BodyReadable from 'undici/types/readable'; -import { RequestOptions, RequestPolicy, rejectDelay, retrier } from './utils/func'; +import { RequestOptions, RequestPolicy, rejectDelay, retrier, urljoin } from './utils/func'; import { PrometheusService } from '../../prometheus'; export type RetryOptions = RequestOptions & @@ -97,7 +97,7 @@ export abstract class BaseRestProvider { requestPolicy: this.requestPolicy, ...options, } as RequestOptions; - const { body, headers, statusCode } = await request(new URL(endpoint, base), { + const { body, headers, statusCode } = await request(urljoin(base, endpoint), { method: 'GET', headersTimeout: (options.requestPolicy as RequestPolicy).timeout, signal: options.signal, @@ -123,7 +123,7 @@ export abstract class BaseRestProvider { requestPolicy: this.requestPolicy, ...options, } as RequestOptions; - const { body, headers, statusCode } = await request(new URL(endpoint, base), { + const { body, headers, statusCode } = await request(urljoin(base, endpoint), { method: 'POST', headersTimeout: (options.requestPolicy as RequestPolicy).timeout, signal: options.signal, diff --git a/src/common/providers/base/utils/func.ts b/src/common/providers/base/utils/func.ts index 80036d5..97d1b72 100644 --- a/src/common/providers/base/utils/func.ts +++ b/src/common/providers/base/utils/func.ts @@ -56,3 +56,69 @@ export const retrier = ( } }; }; + +function normalize(strArray: string[]) { + const resultArray = []; + if (strArray.length === 0) { + return ''; + } + + if (typeof strArray[0] !== 'string') { + throw new TypeError('Url must be a string. Received ' + strArray[0]); + } + + // If the first part is a plain protocol, we combine it with the next part. + if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) { + const first = strArray.shift(); + strArray[0] = first + strArray[0]; + } + + // There must be two or three slashes in the file protocol, two slashes in anything else. + if (strArray[0].match(/^file:\/\/\//)) { + strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1:///'); + } else { + strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://'); + } + + for (let i = 0; i < strArray.length; i++) { + let component = strArray[i]; + + if (typeof component !== 'string') { + throw new TypeError('Url must be a string. Received ' + component); + } + + if (component === '') { + continue; + } + + if (i > 0) { + // Removing the starting slashes for each component but the first. + component = component.replace(/^[\/]+/, ''); + } + if (i < strArray.length - 1) { + // Removing the ending slashes for each component but the last. + component = component.replace(/[\/]+$/, ''); + } else { + // For the last component we will combine multiple slashes to a single one. + component = component.replace(/[\/]+$/, '/'); + } + + resultArray.push(component); + } + + let str = resultArray.join('/'); + // Each input component is now separated by a single slash except the possible first plain protocol part. + + // remove trailing slash before parameters or hash + str = str.replace(/\/(\?|&|#[^!])/g, '$1'); + + // replace ? in parameters with & + const parts = str.split('?'); + str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&'); + + return str; +} + +export function urljoin(...args: string[]): string { + return normalize(args); +}