Skip to content

Commit

Permalink
- Added localhost resolution to an IP Address
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidArayan committed Feb 15, 2024
1 parent 3b03f1a commit b0c5a92
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
7 changes: 6 additions & 1 deletion sdk-core/src/core/query/core-query.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CoreObject, CoreObjectAttributes } from '../core-object';
import { GlobalObjectPool } from '../global-object-pool';
import { Service } from '../service';
import { DNS } from '../util/dns';
import { Util } from '../util/util';
import { CoreError } from './errors/core-error';
import { ContainsQuery } from './queries/contains-query';
Expand Down Expand Up @@ -174,7 +175,10 @@ export abstract class CoreQuery<T extends CoreObject<U>, U extends CoreObjectAtt

public static async fetch<Input extends CoreObject<CoreObjectAttributes>, Output extends CoreObject<CoreObjectAttributes>>(service: Service, input: Input, output: Output, encodedURL: string, type: QueryFetchType, abort?: AbortSignal): Promise<Array<Output>> {
const results: Array<Output> = new Array<Output>();
const url: string = `${service.config.url}${encodedURL}`;

// this resolves instantly if the url is not a localhost
const baseUrl: string = await DNS.resolveLocalhost(service.config.url);
const url: string = `${baseUrl}${encodedURL}`;

if (!fetch) {
CoreError.init(url, {
Expand All @@ -187,6 +191,7 @@ export abstract class CoreQuery<T extends CoreObject<U>, U extends CoreObjectAtt
return results;
}

// this resolves instantly if not running NodeJS or if health checking is disabled (by default)
const serviceHealth: boolean = await service.checkHealth();

if (!serviceHealth) {
Expand Down
51 changes: 49 additions & 2 deletions sdk-core/src/core/util/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,59 @@ export class DNS {

accept(status);
});
}).catch((err) => {
console.error("DNS.check - " + err);
}).catch((_err) => {
DNS._DNSCache.set(hostname, { status: false });

accept(false);
});
});
}

/**
* resolves a localhost into a valid internal ip address
*/
public static resolveLocalhost(hostname: string): Promise<string> {
return new Promise<string>((accept, reject) => {
if (!Util.isNode()) {
return accept(hostname);
}

const url: URL = new URL(hostname);

if (url.hostname !== 'localhost') {
return accept(hostname);
}

// resolve localhost to an ip-address
// this is needed for example in docker environments
import('os').then((os) => {
const nets: any = os.networkInterfaces();
const results: any = {};

for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4;

if (net.family === familyV4Value && !net.internal) {
if (!results[name]) {
results[name] = [];
}

results[name].push(net.address);
}
}
}

const result = results.en0 && results.en0.length > 0 ? results.en0[0] : null;

if (!result) {
return accept(hostname);
}

return accept(`${url.protocol}//${result}${(url.port !== '' ? `:${url.port}` : '')}${(url.pathname !== '/' ? url.pathname : '')}`);
}).catch((_err) => {
accept(hostname);
});
});
}
}
3 changes: 2 additions & 1 deletion sdk-core/src/generator/generators/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export class Project {
output += `\t\t path: path.resolve(__dirname, './build')\n`;
output += `\t },\n`;
output += `\t externals: [\n`;
output += `\t\t 'dns'\n`;
output += `\t\t 'dns',\n`;
output += `\t\t 'os'\n`;
output += `\t ],\n`;
output += `\t plugins: [new CleanWebpackPlugin()],\n`;
output += `\t optimization: {\n`;
Expand Down

0 comments on commit b0c5a92

Please sign in to comment.