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 3 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
48 changes: 48 additions & 0 deletions __tests__/server/utils/getIP.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2023 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 0.0.0.0 if no addresses found', () => {
spy.mockReturnValue({
addresses: [],
});
expect(getIp()).toBe('0.0.0.0');
});
});
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
12 changes: 12 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,12 @@
import { networkInterfaces } from 'os';

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

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

return ipAddress[0] ? ipAddress[0].address : '0.0.0.0';
};
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
12 changes: 12 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,12 @@
import { networkInterfaces } from 'os';

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

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

return ipAddress[0] ? ipAddress[0].address : '0.0.0.0';
};
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
12 changes: 12 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,12 @@
import { networkInterfaces } from 'os';

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

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

return ipAddress[0] ? ipAddress[0].address : '0.0.0.0';
};
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
12 changes: 12 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,12 @@
import { networkInterfaces } from 'os';

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

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

return ipAddress[0] ? ipAddress[0].address : '0.0.0.0';
};
4 changes: 2 additions & 2 deletions src/server/config/env/runTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* permissions and limitations under the License.
*/

const ip = require('ip').address();
const { preprocessEnvVar } = require('@americanexpress/env-config-utils');
const isFetchableUrlInNode = require('@americanexpress/env-config-utils/isFetchableUrlInNode');
const isFetchableUrlInBrowser = require('@americanexpress/env-config-utils/isFetchableUrlInBrowser');
const { argv } = require('yargs');
const bytes = require('bytes');
const ip = require('../../utils/getIP').getIp;
dogpatch626 marked this conversation as resolved.
Show resolved Hide resolved

const isPositiveIntegerIfDefined = (input) => {
if (input === undefined) {
Expand Down Expand Up @@ -133,7 +133,7 @@ const runTime = [
{
name: 'HOLOCRON_MODULE_MAP_URL',
defaultValue: () => (process.env.NODE_ENV === 'development'
? `http://${ip}:${process.env.HTTP_ONE_APP_DEV_CDN_PORT}/static/module-map.json`
? `http://${ip()}:${process.env.HTTP_ONE_APP_DEV_CDN_PORT}/static/module-map.json`
: undefined),
validate: isFetchableUrlInNode,
},
Expand Down
4 changes: 2 additions & 2 deletions src/server/plugins/csp.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import fp from 'fastify-plugin';
import { v4 as uuidV4 } from 'uuid';
import ip from 'ip';
import { getIp } from '../utils/getIP';

function parsePolicy(headerValue) {
const policy = {};
Expand Down Expand Up @@ -71,7 +71,7 @@ const csp = (fastify, _opts, done) => {
const scriptNonce = uuidV4();
let updatedPolicy;
if (process.env.NODE_ENV === 'development') {
const developmentAdditions = `${ip.address()}:* localhost:* ws://localhost:*`;
const developmentAdditions = `${getIp()}:* localhost:* ws://localhost:*`;
let updatedScriptSrc;
if (process.env.ONE_CSP_ALLOW_INLINE_SCRIPTS === 'true') {
updatedScriptSrc = insertSource(policy, 'script-src', developmentAdditions);
Expand Down
4 changes: 2 additions & 2 deletions src/server/utils/devCdnFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import cors from '@fastify/cors';
import compress from '@fastify/compress';
import fastifyStatic from '@fastify/static';
import Fastify from 'fastify';
import ip from 'ip';
import { ProxyAgent } from 'proxy-agent';
import fetch from 'node-fetch';
import { getIp } from './getIP';
import logger from './logging/logger';

import { getCachedModuleFiles, writeToCache, removeExistingEntryIfConflicting } from './cdnCache';
Expand Down Expand Up @@ -122,7 +122,7 @@ export const oneAppDevCdnFactory = ({
oneAppDevCdn.register(cors, {
origin: [
`http://localhost:${appPort}`,
`http://${ip.address()}:${appPort}`,
`http://${getIp()}:${appPort}`,
undefined,
],
});
Expand Down
12 changes: 12 additions & 0 deletions src/server/utils/getIP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { networkInterfaces } from 'os';

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

const ipAddress = Object.keys(interfaces)
dogpatch626 marked this conversation as resolved.
Show resolved Hide resolved
.map((name) => interfaces[name].find((iface) => iface.family === 'IPv4'
&& !iface.internal && iface.address !== '127.0.0.1'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a blocker but && iface.address !== '127.0.0.1' is no longer needed

.filter(Boolean);

return ipAddress[0] ? ipAddress[0].address : '0.0.0.0';
dogpatch626 marked this conversation as resolved.
Show resolved Hide resolved
giulianok marked this conversation as resolved.
Show resolved Hide resolved
dogpatch626 marked this conversation as resolved.
Show resolved Hide resolved
};
Loading
Loading