Skip to content

Adds deprecation warning for usage of future non-default parsing extensions #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions app-config-default-extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ module.exports = {
eqDirective(),
parseDirective(),
hiddenDirective(),
v1Compat(),
envDirective(aliases, environmentOverride, environmentSourceNames),
envVarDirective(aliases, environmentOverride, environmentSourceNames),
extendsDirective(),
extendsSelfDirective(),
overrideDirective(),
encryptedDirective(symmetricKey),
timestampDirective(),
substituteDirective(aliases, environmentOverride, environmentSourceNames),
gitRefDirectives(),

// these will be removed in v3
v1Compat(true),
gitRefDirectives(undefined, true),
encryptedDirective(symmetricKey, true),
];
},
defaultEnvExtensions() {
Expand Down
12 changes: 11 additions & 1 deletion app-config-encryption/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import type { ParsingExtension } from '@app-config/core';
import { named } from '@app-config/extension-utils';
import { logger } from '@app-config/logging';
import { DecryptedSymmetricKey, decryptValue } from './encryption';

export * from './encryption';
export * from './secret-agent';
export * from './secret-agent-tls';

/** Decrypts inline encrypted values */
export default function encryptedDirective(symmetricKey?: DecryptedSymmetricKey): ParsingExtension {
export default function encryptedDirective(
symmetricKey?: DecryptedSymmetricKey,
shouldShowDeprecationNotice?: true,
): ParsingExtension {
return named('encryption', (value) => {
if (typeof value === 'string' && value.startsWith('enc:')) {
return async (parse) => {
if (shouldShowDeprecationNotice) {
logger.warn(
'Detected deprecated use of @app-config/encryption parsing extension. Please install @app-config/encryption and add it to your meta file "parsingExtensions".',
);
}

const decrypted = await decryptValue(value, symmetricKey);

return parse(decrypted, { fromSecrets: true, parsedFromEncryptedValue: true });
Expand Down
36 changes: 36 additions & 0 deletions app-config-extensions/src/substitute-directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@ export function substituteDirective(
validateObject(value, [...ctx, key]);
if (Array.isArray(value)) throw new AppConfigError('$substitute was given an array');

if (value.$name) {
logger.warn(
`Detected deprecated use of $name in a $substitute directive. Use 'name' instead.`,
);
}

const name = (await parse(selectDefined(value.name, value.$name))).toJSON();

validateString(name, [...ctx, key, [InObject, 'name']]);

const parseValue = async (strValue: string | null) => {
const parseBool = (await parse(selectDefined(value.parseBool, value.$parseBool))).toJSON();

if (value.$parseBool) {
logger.warn(
`Detected deprecated use of $parseBool in a $substitute directive. Use 'parseBool' instead.`,
);
}

if (parseBool) {
const parsed =
strValue !== null && (strValue.toLowerCase() === 'true' || strValue === '1');
Expand All @@ -48,6 +60,12 @@ export function substituteDirective(

const parseInt = (await parse(selectDefined(value.parseInt, value.$parseInt))).toJSON();

if (value.$parseInt) {
logger.warn(
`Detected deprecated use of $parseInt in a $substitute directive. Use 'parseInt' instead.`,
);
}

if (parseInt) {
const parsed = Number.parseInt(strValue, 10);

Expand All @@ -58,6 +76,12 @@ export function substituteDirective(
return parse(parsed, { shouldFlatten: true });
}

if (value.$parseFloat) {
logger.warn(
`Detected deprecated use of $parseFloat in a $substitute directive. Use 'parseFloat' instead.`,
);
}

const parseFloat = (
await parse(selectDefined(value.parseFloat, value.$parseFloat))
).toJSON();
Expand Down Expand Up @@ -89,6 +113,18 @@ export function substituteDirective(
const fallback = (await parse(selectDefined(value.fallback, value.$fallback))).toJSON();
const allowNull = (await parse(selectDefined(value.allowNull, value.$allowNull))).toJSON();

if (value.$fallback) {
logger.warn(
`Detected deprecated use of $fallback in a $substitute directive. Use 'fallback' instead.`,
);
}

if (value.$allowNull) {
logger.warn(
`Detected deprecated use of $allowNull in a $substitute directive. Use 'allowNull' instead.`,
);
}

if (allowNull) {
validateStringOrNull(fallback, [...ctx, key, [InObject, 'fallback']]);
} else {
Expand Down
1 change: 1 addition & 0 deletions app-config-git/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dependencies": {
"@app-config/core": "^2.4.6",
"@app-config/extension-utils": "^2.4.6",
"@app-config/logging": "^2.4.6",
"simple-git": "2"
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions app-config-git/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import simpleGit from 'simple-git';
import { ParsingExtension, AppConfigError, Fallbackable } from '@app-config/core';
import { named, forKey, validateOptions } from '@app-config/extension-utils';
import { logger } from '@app-config/logging';

class GitError extends Fallbackable {}

/** Access to the git branch and commit ref */
export default function gitRefDirectives(
getStatus: typeof gitStatus = gitStatus,
shouldShowDeprecationNotice?: true,
): ParsingExtension {
return named(
'$git',
Expand All @@ -15,6 +17,12 @@ export default function gitRefDirectives(
validateOptions(
(SchemaBuilder) => SchemaBuilder.stringSchema(),
(value) => async (parse) => {
if (shouldShowDeprecationNotice) {
logger.warn(
'Detected deprecated use of @app-config/git parsing extension. Please install @app-config/git and add it to your meta file "parsingExtensions".',
);
}

switch (value) {
case 'commit':
return getStatus().then(({ commitRef }) => parse(commitRef, { shouldFlatten: true }));
Expand Down
1 change: 1 addition & 0 deletions app-config-git/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"references": [
{ "path": "../app-config-test-utils" },
{ "path": "../app-config-core" },
{ "path": "../app-config-logging" },
{ "path": "../app-config-extension-utils" }
]
}
36 changes: 25 additions & 11 deletions app-config-v1-compat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FileSource } from '@app-config/node';
import { logger } from '@app-config/logging';

/** V1 app-config compatibility */
export default function v1Compat(): ParsingExtension {
export default function v1Compat(shouldShowDeprecationNotice?: true): ParsingExtension {
return named('v1-compat', (value, [_, key], context) => {
// only apply in top-level app-config property
if (context[context.length - 1]?.[0] !== Root) {
Expand All @@ -16,16 +16,6 @@ export default function v1Compat(): ParsingExtension {

if (key === 'app-config' && isObject(value)) {
return async (parse, _, ctx) => {
if (ctx instanceof FileSource) {
logger.warn(
`Using V1 compatibility layer for special 'app-config' property in ${ctx.filePath}! This functionality is deprecated and may be removed in the future.`,
);
} else {
logger.warn(
`Using V1 compatibility layer for special 'app-config' property! This functionality is deprecated and may be removed in the future.`,
);
}

const resolveAmbiguousFilename = async (filepath: string) => {
let resolvedPath = filepath;

Expand Down Expand Up @@ -56,13 +46,25 @@ export default function v1Compat(): ParsingExtension {
// TODO: multiple properties defined

if ('extends' in value) {
if (shouldShowDeprecationNotice) {
logger.warn(
'Detected deprecated use of @app-config/v1-compat parsing extension. Please install @app-config/v1-compat and add it to your meta file "parsingExtensions".',
);
}

return parse(
{ $extends: await resolveAmbiguousFilename(value.extends as string) },
{ shouldMerge: true },
);
}

if ('extendsOptional' in value) {
if (shouldShowDeprecationNotice) {
logger.warn(
'Detected deprecated use of @app-config/v1-compat parsing extension. Please install @app-config/v1-compat and add it to your meta file "parsingExtensions".',
);
}

return parse(
{
$extends: {
Expand All @@ -75,13 +77,25 @@ export default function v1Compat(): ParsingExtension {
}

if ('override' in value) {
if (shouldShowDeprecationNotice) {
logger.warn(
'Detected deprecated use of @app-config/v1-compat parsing extension. Please install @app-config/v1-compat and add it to your meta file "parsingExtensions".',
);
}

return parse(
{ $override: await resolveAmbiguousFilename(value.override as string) },
{ shouldOverride: true },
);
}

if ('overrideOptional' in value) {
if (shouldShowDeprecationNotice) {
logger.warn(
'Detected deprecated use of @app-config/v1-compat parsing extension. Please install @app-config/v1-compat and add it to your meta file "parsingExtensions".',
);
}

return parse(
{
$override: {
Expand Down