Skip to content

Commit

Permalink
Fix/for infra (#31)
Browse files Browse the repository at this point in the history
* feat: add urljoin

* fix: dockerfiles
  • Loading branch information
vgorkavenko committed Sep 19, 2024
1 parent 16e54bf commit d442f0d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20.12.1-alpine as building
FROM node:20.12.1-alpine AS building

WORKDIR /app

Expand All @@ -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"]
2 changes: 1 addition & 1 deletion Dockerfile.cli
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20.12.1-alpine as building
FROM node:20.12.1-alpine AS building

WORKDIR /app

Expand Down
6 changes: 3 additions & 3 deletions src/common/providers/base/rest-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
66 changes: 66 additions & 0 deletions src/common/providers/base/utils/func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit d442f0d

Please sign in to comment.