diff --git a/babel.config.cjs.json b/babel.config.cjs.json index 9482329f6..b7c9bb40c 100644 --- a/babel.config.cjs.json +++ b/babel.config.cjs.json @@ -9,9 +9,5 @@ } ], "@babel/preset-typescript" - ], - "plugins": [ - ["babel-plugin-transform-import-meta"], - ["babel-plugin-add-import-extension"] ] } diff --git a/babel.config.esm.json b/babel.config.esm.json index 129c867aa..4e13e4803 100644 --- a/babel.config.esm.json +++ b/babel.config.esm.json @@ -2,6 +2,5 @@ "presets": [ ["@babel/preset-env", { "targets": { "node": "16" }, "modules": false }], "@babel/preset-typescript" - ], - "plugins": [["babel-plugin-add-import-extension"]] + ] } diff --git a/src/api/BaseApi.ts b/src/api/BaseApi.ts index f1703e8fb..df0952503 100644 --- a/src/api/BaseApi.ts +++ b/src/api/BaseApi.ts @@ -7,13 +7,11 @@ import axios, { } from 'axios'; import axiosRetry from 'axios-retry'; import { randomUUID } from 'crypto'; -import fs from 'fs'; import HttpsProxyAgent from 'https-proxy-agent'; -import path from 'path'; -import { fileURLToPath } from 'url'; import _curlirize from '../ext/axios-curlirize/curlirize'; import StateImpl, { State } from '../shared/State'; +import { getUserAgent } from '../shared/Version'; import { curlirizeMessage, printMessage } from '../utils/Console'; import { mergeDeep } from '../utils/JsonUtils'; import { setupPollyForFrodoLib } from '../utils/SetupPollyForFrodoLib'; @@ -22,12 +20,6 @@ if (process.env.FRODO_MOCK) { setupPollyForFrodoLib({ state: StateImpl({}) }); } -const __dirname = path.dirname(fileURLToPath(import.meta.url)); - -const pkg = JSON.parse( - fs.readFileSync(path.resolve(__dirname, '../../package.json'), 'utf8') -); - axiosRetry(axios, { retries: 3, shouldResetTimeout: true, @@ -43,7 +35,7 @@ const maxSockets = 100; const maxFreeSockets = 10; const freeSocketTimeout = 30000; -const userAgent = `${pkg.name}/${pkg.version}`; +const userAgent = getUserAgent(); const transactionId = `frodo-${randomUUID()}`; let httpAgent, httpsAgent; diff --git a/src/ops/AdminOps.ts b/src/ops/AdminOps.ts index edf6b0772..65cb80181 100644 --- a/src/ops/AdminOps.ts +++ b/src/ops/AdminOps.ts @@ -1,6 +1,4 @@ -import fs from 'fs'; -import path from 'path'; -import { fileURLToPath, URL } from 'url'; +import { URL } from 'url'; import util from 'util'; import { v4 as uuidv4 } from 'uuid'; @@ -39,6 +37,12 @@ import { import { accessTokenRfc7523AuthZGrant } from './OAuth2OidcOps'; import { updateOAuth2TrustedJwtIssuer } from './OAuth2TrustedJwtIssuerOps'; import { getRealmManagedOrganization } from './OrganizationOps'; +import templateIpAddresses from './templates/autoaccess/IPAddresses.json'; +import templateUserAgents from './templates/autoaccess/UserAgents.json'; +import templateUsernames from './templates/autoaccess/Usernames.json'; +import GENERIC_EXTENSION_ATTRIBUTES from './templates/cloud/GenericExtensionAttributesTemplate.json'; +import OAUTH2_CLIENT from './templates/OAuth2ClientTemplate.json'; +import OAUTH2_ISSUER from './templates/OAuth2TrustedJwtIssuerTemplate.json'; export type Admin = { generateRfc7523AuthZGrantArtefacts( @@ -337,30 +341,6 @@ export default (state: State): Admin => { }; }; -const __dirname = path.dirname(fileURLToPath(import.meta.url)); - -const OAUTH2_CLIENT: OAuth2ClientSkeleton = JSON.parse( - fs.readFileSync( - path.resolve(__dirname, './templates/OAuth2ClientTemplate.json'), - 'utf8' - ) -); -const OAUTH2_ISSUER: OAuth2TrustedJwtIssuerSkeleton = JSON.parse( - fs.readFileSync( - path.resolve(__dirname, './templates/OAuth2TrustedJwtIssuerTemplate.json'), - 'utf8' - ) -); -const GENERIC_EXTENSION_ATTRIBUTES = JSON.parse( - fs.readFileSync( - path.resolve( - __dirname, - './templates/cloud/GenericExtensionAttributesTemplate.json' - ), - 'utf8' - ) -); - const protectedClients = ['ui', 'idm-provisioning']; const protectedSubjects = ['amadmin', 'autoid-resource-server']; @@ -1573,27 +1553,6 @@ export async function repairOrgModel({ } } -const templateUsernames: string[] = JSON.parse( - fs.readFileSync( - path.resolve(__dirname, './templates/autoaccess/Usernames.json'), - 'utf8' - ) -); - -const templateUserAgents: string[] = JSON.parse( - fs.readFileSync( - path.resolve(__dirname, './templates/autoaccess/UserAgents.json'), - 'utf8' - ) -); - -const templateIpAddresses: string[] = JSON.parse( - fs.readFileSync( - path.resolve(__dirname, './templates/autoaccess/IPAddresses.json'), - 'utf8' - ) -); - export function getUniqueValues(values: string[]): string[] { return [...new Set(values)].filter((it) => it); } diff --git a/src/ops/PolicyOps.test.ts b/src/ops/PolicyOps.test.ts index f94d37f72..d14f38b3e 100644 --- a/src/ops/PolicyOps.test.ts +++ b/src/ops/PolicyOps.test.ts @@ -45,7 +45,7 @@ import { type PolicyCondition, type PolicySkeleton } from '../api/PoliciesApi'; import { type PolicySetSkeleton } from '../api/PolicySetApi'; import { type ScriptSkeleton } from '../api/ScriptApi'; import { cloneDeep } from '../utils/JsonUtils'; -import { PolicyExportInterface } from '../../types/ops/PolicyOps'; +import { PolicyExportInterface } from './PolicyOps'; const ctx = autoSetupPolly(); diff --git a/src/ops/PolicySetOps.test.ts b/src/ops/PolicySetOps.test.ts index 714e1f151..afe9c6c93 100644 --- a/src/ops/PolicySetOps.test.ts +++ b/src/ops/PolicySetOps.test.ts @@ -41,7 +41,7 @@ import * as PolicySetOps from './PolicySetOps'; import { autoSetupPolly, filterRecording } from '../utils/AutoSetupPolly'; import { type PolicySetSkeleton } from '../api/PolicySetApi'; import { cloneDeep } from '../utils/JsonUtils'; -import { PolicySetExportInterface } from '../../types/ops/PolicySetOps'; +import { PolicySetExportInterface } from './PolicySetOps'; const ctx = autoSetupPolly(); diff --git a/src/ops/ScriptOps.test.ts b/src/ops/ScriptOps.test.ts index f5b8ea598..3300b6163 100644 --- a/src/ops/ScriptOps.test.ts +++ b/src/ops/ScriptOps.test.ts @@ -29,10 +29,10 @@ * Note: FRODO_DEBUG=1 is optional and enables debug logging for some output * in case things don't function as expected */ -import { FrodoError, state } from '../index'; +import { state } from '../index'; import * as ScriptOps from './ScriptOps'; import { autoSetupPolly, filterRecording } from '../utils/AutoSetupPolly'; -import { type ScriptSkeleton } from '../../types/api/ScriptApi'; +import { ScriptSkeleton } from '../api/ScriptApi'; const ctx = autoSetupPolly(); diff --git a/src/ops/VersionUtils.ts b/src/ops/VersionUtils.ts index 8f0992147..3552fd2e4 100644 --- a/src/ops/VersionUtils.ts +++ b/src/ops/VersionUtils.ts @@ -1,9 +1,6 @@ -import fs from 'fs'; -import path from 'path'; -import { fileURLToPath } from 'url'; - import { generateReleaseApi } from '../api/BaseApi'; import { State } from '../shared/State'; +import { getVersionFromPackage } from '../shared/Version'; export type Version = { getVersion(): string; @@ -27,15 +24,9 @@ export default (state: State): Version => { }; }; -const __dirname = path.dirname(fileURLToPath(import.meta.url)); - -const pkg = JSON.parse( - fs.readFileSync(path.resolve(__dirname, '../../package.json'), 'utf8') -); - export function getVersion({ state }: { state: State }) { // must initialize state to avoid library initialization issues - if (state) return `${pkg.version}`; + if (state) return getVersionFromPackage(); } export async function getAllVersions({ diff --git a/src/shared/State.ts b/src/shared/State.ts index 41836040e..fe5a2c43a 100644 --- a/src/shared/State.ts +++ b/src/shared/State.ts @@ -1,8 +1,4 @@ /* eslint-disable no-console */ -import fs from 'fs'; -import path from 'path'; -import { fileURLToPath } from 'url'; - import { FeatureInterface } from '../api/cloud/FeatureApi'; import { UserSessionMetaType } from '../ops/AuthenticateOps'; import { FrodoError } from '../ops/FrodoError'; @@ -13,12 +9,7 @@ import { ProgressIndicatorType, } from '../utils/Console'; import { cloneDeep } from '../utils/JsonUtils'; - -const __dirname = path.dirname(fileURLToPath(import.meta.url)); - -const pkg = JSON.parse( - fs.readFileSync(path.resolve(__dirname, '../../package.json'), 'utf8') -); +import { getPackageVersion } from './Version'; export type State = { /** @@ -312,7 +303,7 @@ export default (initialState: StateInterface): State => { state.frodoVersion = version; }, getFrodoVersion() { - return state.frodoVersion || `v${pkg.version} [${process.version}]`; + return state.frodoVersion || getPackageVersion(); }, setConnectionProfilesPath(path: string) { diff --git a/src/shared/Version.test.ts b/src/shared/Version.test.ts new file mode 100644 index 000000000..24530ff41 --- /dev/null +++ b/src/shared/Version.test.ts @@ -0,0 +1,7 @@ +import { getUserAgent } from './Version'; + +describe('Versions', () => { + test('user agent is compiled from package', () => { + expect(getUserAgent()).toMatch(/@rockcarver\/frodo-lib\/[0-9.-]+/); + }); +}); \ No newline at end of file diff --git a/src/shared/Version.ts b/src/shared/Version.ts new file mode 100644 index 000000000..7000f4c5a --- /dev/null +++ b/src/shared/Version.ts @@ -0,0 +1,7 @@ +import pkg from '../../package.json'; + +export const getUserAgent = () => `${pkg.name}/${pkg.version}`; + +export const getPackageVersion = () => `v${pkg.version} [${process.version}]`; + +export const getVersionFromPackage = () => pkg.version; diff --git a/tsconfig.json b/tsconfig.json index 00263cce1..4800796c0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -36,7 +36,7 @@ // "types": [], /* Specify type package names to be included without being referenced in a source file. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ - // "resolveJsonModule": true, /* Enable importing .json files. */ + "resolveJsonModule": true, /* Enable importing .json files. */ // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ /* JavaScript Support */ @@ -101,7 +101,7 @@ "skipLibCheck": true /* Skip type checking all .d.ts files. */ }, "files": ["src/index.ts"], - "include": ["src/**/*.ts"], + "include": ["src/**/*.ts","package.json"], "exclude": ["src/**/*.test.ts", "src/**/*.test_.ts"], "typedocOptions": { "name": "Frodo Library",