Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

refactor(deps): remove ip for node native implementation #1302

Merged
merged 8 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions __tests__/server/config/env/runTime.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ expect.extend({
},
});

jest.mock('ip', () => ({
address: () => 'localhost',
jest.mock('../../../../src/server/utils/getIP', () => ({
getIp: () => 'localhost',
}));

jest.mock('yargs', () => ({ argv: { rootModuleName: 'my-module' } }));
Expand Down
51 changes: 51 additions & 0 deletions __tests__/server/utils/getIP.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2024 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import os from 'os';
import { getIp } from '../../../src/server/utils/getIP';

const spy = jest.spyOn(os, 'networkInterfaces');

describe('getIp', () => {
it('returns a valid IP address', () => {
spy.mockReturnValue({
addresses: [
{
address: '1.1.1.1',
family: 'IPv4',
internal: true,
},
{
address: '2.2.2.2',
family: 'IPv4',
internal: false,
},
{
address: '3.3.3.3',
family: 'IPv6',
internal: false,
},
],
});
expect(getIp()).toBe('2.2.2.2');
});
it('returns 127.0.0.1 if no addresses found', () => {
spy.mockReturnValue({
addresses: [],
});
expect(getIp()).toBe('127.0.0.1');
});
});
9 changes: 4 additions & 5 deletions __tests__/server/utils/stateConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

jest.mock('fs', () => ({ existsSync: jest.fn() }));
jest.mock('ip', () => ({ address: jest.fn() }));
jest.mock('../../../src/server/utils/getIP', () => ({ getIp: jest.fn() }));
jest.mock('fake/path/.dev/endpoints/index.js', () => jest.fn(), { virtual: true });
jest.mock('yargs', () => ({ argv: {} }));
jest.mock('../../../src/server/utils/envVarAllowList', () => [
Expand All @@ -34,17 +33,17 @@ describe('stateConfig methods', () => {
let restoreModuleStateConfig;
let backupModuleStateConfig;
let fs;
let ip;
let getIp;
let yargs;

const originalEnvVars = process.env;

const reloadMocks = () => {
fs = require('fs');
ip = require('ip');
getIp = require('../../../src/server/utils/getIP').getIp;
yargs = require('yargs');
jest.spyOn(process, 'cwd').mockImplementation(() => 'fake/path/');
ip.address.mockImplementation(() => '127.0.0.1');
getIp.mockImplementation(() => '127.0.0.1');
yargs.argv = {};
fs.existsSync.mockImplementation(() => false);
process.env.ONE_CONFIG_ENV = 'qa';
Expand Down
6 changes: 3 additions & 3 deletions __tests__/server/utils/watchLocalModules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import {
getModules,
resetModuleRegistry,
} from 'holocron/moduleRegistry';
import { address } from 'ip';
import watchLocalModules from '../../../src/server/utils/watchLocalModules';
import { getIp } from '../../../src/server/utils/getIP';

const ip = address();
const ip = getIp();

jest.mock('chokidar', () => {
const listeners = {};
Expand Down Expand Up @@ -66,7 +66,7 @@ jest.mock('fs', () => {
readFileSync: jest.fn(actual.readFileSync),
};
});
jest.spyOn(console, 'error').mockImplementation(() => {});
jest.spyOn(console, 'error').mockImplementation(() => { });

describe('watchLocalModules', () => {
beforeEach(() => jest.clearAllMocks());
Expand Down
6 changes: 0 additions & 6 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
"holocron": "^1.9.2",
"holocron-module-route": "^1.7.0",
"immutable": "^4.1.0",
"ip": "^1.1.8",
"joi": "^17.6.0",
"lean-intl": "^4.2.2",
"matcher": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import contentSecurityPolicyBuilder from 'content-security-policy-builder';
import ip from 'ip';
import { getIp } from './getIP';

export default contentSecurityPolicyBuilder({
directives: {
Expand All @@ -30,7 +30,7 @@ export default contentSecurityPolicyBuilder({
// used for our sample app deployment in heroku
'*.surge.sh',
// used for local development
`${ip.address()}:3001`,
`${getIp()}:3001`,
// used for local development
'localhost:3001',
],
Expand All @@ -49,7 +49,7 @@ export default contentSecurityPolicyBuilder({
"'self'",
'*.api.frank',
// used for local development
`${ip.address()}:3001`,
`${getIp()}:3001`,
// used for local development
'localhost:3001',
// used by integration tests running in docker where domain names are aliased
Expand Down
28 changes: 28 additions & 0 deletions prod-sample/sample-modules/frank-lloyd-root/0.0.0/src/getIP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import { networkInterfaces } from 'os';

export const getIp = () => {
const interfaces = networkInterfaces();

const ipAddresses = Object.keys(interfaces)
.map((name) => interfaces[name].find((iface) => iface.family === 'IPv4'
&& !iface.internal && iface.address !== '127.0.0.1'))
.filter(Boolean);

return ipAddresses[0]?.address ?? '127.0.0.1';
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import contentSecurityPolicyBuilder from 'content-security-policy-builder';
import ip from 'ip';
import { getIp } from './getIP';

export default contentSecurityPolicyBuilder({
directives: {
Expand All @@ -31,7 +31,7 @@ export default contentSecurityPolicyBuilder({
// used by integration tests running in docker where domain names are aliased
'https://sample-cdn.frank',
// used for local development
`${ip.address()}:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`,
`${getIp()}:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`,
// used for local development
`localhost:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`,
],
Expand All @@ -52,7 +52,7 @@ export default contentSecurityPolicyBuilder({
// used for our sample app deployment in heroku
'*.surge.sh',
// used for local development
`${ip.address()}:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`,
`${getIp()}:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`,
// used for local development
`localhost:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`,
// used by integration tests running in docker where domain names are aliased
Expand Down
28 changes: 28 additions & 0 deletions prod-sample/sample-modules/frank-lloyd-root/0.0.1/src/getIP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import { networkInterfaces } from 'os';

export const getIp = () => {
const interfaces = networkInterfaces();

const ipAddresses = Object.keys(interfaces)
.map((name) => interfaces[name].find((iface) => iface.family === 'IPv4'
&& !iface.internal && iface.address !== '127.0.0.1'))
.filter(Boolean);

return ipAddresses[0]?.address ?? '127.0.0.1';
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import contentSecurityPolicyBuilder from 'content-security-policy-builder';
import ip from 'ip';
import { getIp } from './getIP';

export default contentSecurityPolicyBuilder({
directives: {
Expand All @@ -30,7 +30,7 @@ export default contentSecurityPolicyBuilder({
// used by integration tests running in docker where domain names are aliased
'https://sample-cdn.frank',
// used for local development
`${ip.address()}:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`,
`${getIp()}:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`,
// used for local development
`localhost:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`,
],
Expand All @@ -51,7 +51,7 @@ export default contentSecurityPolicyBuilder({
// used for our sample app deployment in heroku
'*.surge.sh',
// used for local development
`${ip.address()}:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`,
`${getIp()}:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`,
// used for local development
`localhost:${process.env.HTTP_ONE_APP_DEV_CDN_PORT || 3001}`,
// used by integration tests running in docker where domain names are aliased
Expand Down
28 changes: 28 additions & 0 deletions prod-sample/sample-modules/frank-lloyd-root/0.0.2/src/getIP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import { networkInterfaces } from 'os';

export const getIp = () => {
const interfaces = networkInterfaces();

const ipAddresses = Object.keys(interfaces)
.map((name) => interfaces[name].find((iface) => iface.family === 'IPv4'
&& !iface.internal && iface.address !== '127.0.0.1'))
.filter(Boolean);

return ipAddresses[0]?.address ?? '127.0.0.1';
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import contentSecurityPolicyBuilder from 'content-security-policy-builder';
import ip from 'ip';
import { getIp } from './getIP';

export default contentSecurityPolicyBuilder({
directives: {
Expand All @@ -29,7 +29,7 @@ export default contentSecurityPolicyBuilder({
manifestSrc: [
"'self'",
'https://sample-cdn.frank',
`${ip.address()}:3001`,
`${getIp()}:3001`,
'localhost:3001',
],
scriptSrc: [
Expand All @@ -39,7 +39,7 @@ export default contentSecurityPolicyBuilder({
// used for our sample app deployment in heroku
'https://one-app-statics.surge.sh',
// used for local development
`${ip.address()}:3001`,
`${getIp()}:3001`,
// used for local development
'localhost:3001',
],
Expand All @@ -60,7 +60,7 @@ export default contentSecurityPolicyBuilder({
"'self'",
'*.api.frank',
// used for local development
`${ip.address()}:3001`,
`${getIp()}:3001`,
// used for local development
'localhost:3001',
// used by integration tests running in docker where domain names are aliased
Expand Down
28 changes: 28 additions & 0 deletions prod-sample/sample-modules/frank-lloyd-root/0.0.3/src/getIP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import { networkInterfaces } from 'os';

export const getIp = () => {
const interfaces = networkInterfaces();

const ipAddresses = Object.keys(interfaces)
.map((name) => interfaces[name].find((iface) => iface.family === 'IPv4'
&& !iface.internal && iface.address !== '127.0.0.1'))
.filter(Boolean);

return ipAddresses[0]?.address ?? '127.0.0.1';
};
Loading
Loading