Skip to content

Commit

Permalink
chore: update ip version
Browse files Browse the repository at this point in the history
  • Loading branch information
easy1090 committed Jul 22, 2024
1 parent c1cd5c1 commit 47949ab
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 24 deletions.
2 changes: 0 additions & 2 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"body-parser": "1.20.1",
"cors": "2.8.5",
"dayjs": "1.11.6",
"ip": "2.0.1",
"lodash": "^4.17.21",
"open": "^8.4.0",
"serve-static": "1.15.0",
Expand All @@ -39,7 +38,6 @@
"devDependencies": {
"@types/body-parser": "1.19.2",
"@types/cors": "2.8.13",
"@types/ip": "1.1.3",
"@types/lodash": "^4.17.0",
"@types/node": "^16",
"@types/serve-static": "1.15.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/sdk/server/apis/project.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SDK } from '@rsdoctor/types';
import ip from 'ip';
import { BaseAPI } from './base';
import { Router } from '../router';
import { getLocalIpAddress } from '../utils';

export class ProjectAPI extends BaseAPI {
@Router.get(SDK.ServerAPI.API.Env)
Expand All @@ -11,7 +11,7 @@ export class ProjectAPI extends BaseAPI {
const { server } = this.ctx;

return {
ip: ip.address(),
ip: getLocalIpAddress(),
port: server.port,
};
}
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/sdk/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import serve from 'serve-static';
import { Bundle } from '@rsdoctor/utils/common';
import assert from 'assert';
import bodyParser from 'body-parser';
import ip from 'ip';
import cors from 'cors';
import { PassThrough } from 'stream';
import { Socket } from './socket';
Expand All @@ -13,6 +12,7 @@ import * as APIs from './apis';
import { chalk, logger } from '@rsdoctor/utils/logger';
import { openBrowser } from '@/sdk/utils/openBrowser';
import path from 'path';
import { getLocalIpAddress } from './utils';
export * from './utils';

export class RsdoctorServer implements SDK.RsdoctorServerInstance {
Expand Down Expand Up @@ -45,7 +45,8 @@ export class RsdoctorServer implements SDK.RsdoctorServerInstance {
}

public get host(): string {
const host = ip.address();
const host = getLocalIpAddress();
console.log('host::::::', host);
return host;
}

Expand Down
13 changes: 13 additions & 0 deletions packages/sdk/src/sdk/server/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SDK } from '@rsdoctor/types';
import os from 'os';

export function getDataByPagination<T>(
data: T[],
Expand All @@ -18,3 +19,15 @@ export function getDataByPagination<T>(
},
};
}

export function getLocalIpAddress() {
const interfaces = os.networkInterfaces();
for (const iface of Object.values(interfaces)) {
for (const alias of iface as os.NetworkInterfaceInfo[]) {
if (alias.family === 'IPv4' && !alias.internal) {
return alias.address;
}
}
}
return '127.0.0.1'; // fallback to localhost if no external IP found
}
4 changes: 2 additions & 2 deletions packages/sdk/tests/server/apis/project.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, it, expect, vi } from 'vitest';
import ip from 'ip';
import { Manifest, SDK } from '@rsdoctor/types';
import { Manifest as ManifestShared } from '@rsdoctor/utils/common';

import { cwd, setupSDK } from '../../utils';
import { getLocalIpAddress } from '@/sdk/server/utils';

vi.setConfig({ testTimeout: 50000 });

Expand All @@ -13,7 +13,7 @@ describe('test server/apis/project.ts', () => {
it(`test api: ${SDK.ServerAPI.API.Env}`, async () => {
const env = (await target.get(SDK.ServerAPI.API.Env)).toJSON();

expect(env.ip).toEqual(ip.address());
expect(env.ip).toEqual(getLocalIpAddress());
expect(env.port).toEqual(target.server.port);
});

Expand Down
16 changes: 0 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 47949ab

Please sign in to comment.