From 592ec54b588c28252583ed68fc9f201d590ac37e Mon Sep 17 00:00:00 2001 From: corymhall <43035978+corymhall@users.noreply.github.com> Date: Thu, 14 Nov 2024 10:11:07 -0500 Subject: [PATCH] adding license information for cdk code --- src/cdk-logical-id.ts | 88 +++++++++++++++++++++++++++++++++++++++++++ src/stack.ts | 72 +---------------------------------- 2 files changed, 89 insertions(+), 71 deletions(-) create mode 100644 src/cdk-logical-id.ts diff --git a/src/cdk-logical-id.ts b/src/cdk-logical-id.ts new file mode 100644 index 00000000..cfe55ba3 --- /dev/null +++ b/src/cdk-logical-id.ts @@ -0,0 +1,88 @@ +// Copyright 2018-2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// +// 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. +// +// NOTE: +// Most of this code was copied from https://github.com/aws/aws-cdk/blob/ccab485b87a7090ddf0773508d7b8ee84ff654b0/packages/aws-cdk-lib/core/lib/private/uniqueid.ts +// with the modification of removing logic related to adding the path hash to the logicalId +import * as cdk from 'aws-cdk-lib/core'; +/** + * Resources with this ID are hidden from humans + * + * They do not appear in the human-readable part of the logical ID + */ +const HIDDEN_FROM_HUMAN_ID = 'Resource'; + +/** + * Resources with this ID are complete hidden from the logical ID calculation. + */ +const HIDDEN_ID = 'Default'; +const MAX_HUMAN_LEN = 240; // this is the value in CDK and seems like a good default to keep + +/** + * Calculates a unique ID for a set of textual components. + * + * This is forked from the internal cdk implementation with the removal of the hash suffix. + * We remove the hash from the CDK logical ID calculation because Pulumi already handles + * adding a unique random suffix and we do not want to end up with a double hash. + * @see https://github.com/aws/aws-cdk/blob/ccab485b87a7090ddf0773508d7b8ee84ff654b0/packages/aws-cdk-lib/core/lib/private/uniqueid.ts?plain=1#L32 + * + * @param components The path components + * @returns a unique alpha-numeric identifier with a maximum length of 255 + */ +export function makeUniqueId(components: string[]) { + components = components.filter((x) => x !== HIDDEN_ID); + + if (components.length === 0) { + throw new Error('Unable to calculate a unique id for an empty set of components'); + } + + // Lazy require in order to break a module dependency cycle + const unresolvedTokens = components.filter((c) => cdk.Token.isUnresolved(c)); + if (unresolvedTokens.length > 0) { + throw new Error(`ID components may not include unresolved tokens: ${unresolvedTokens.join(',')}`); + } + + const human = removeDupes(components) + .filter((x) => x !== HIDDEN_FROM_HUMAN_ID) + .map(removeNonAlphanumeric) + .join('') + .slice(0, MAX_HUMAN_LEN); + + return human; +} + +/** + * Remove duplicate "terms" from the path list + * + * If the previous path component name ends with this component name, skip the + * current component. + */ +function removeDupes(path: string[]): string[] { + const ret = new Array(); + + for (const component of path) { + if (ret.length === 0 || !ret[ret.length - 1].endsWith(component)) { + ret.push(component); + } + } + + return ret; +} + +/** + * Removes all non-alphanumeric characters in a string. + */ +function removeNonAlphanumeric(s: string) { + return s.replace(/[^A-Za-z0-9]/g, ''); +} diff --git a/src/stack.ts b/src/stack.ts index 952d5a58..2bc18c5e 100644 --- a/src/stack.ts +++ b/src/stack.ts @@ -18,6 +18,7 @@ import { AppConverter, StackConverter } from './converters/app-converter'; import { PulumiSynthesizer, PulumiSynthesizerBase } from './synthesizer'; import { AwsCdkCli, ICloudAssemblyDirectoryProducer } from '@aws-cdk/cli-lib-alpha'; import { CdkConstruct } from './interop'; +import { makeUniqueId } from './cdk-logical-id'; export type AppOutputs = { [outputId: string]: pulumi.Output }; @@ -324,74 +325,3 @@ function generateAppId(): string { .replace(/[^a-z0-9-.]/g, '-') .slice(-17); } - -/** - * Resources with this ID are hidden from humans - * - * They do not appear in the human-readable part of the logical ID - */ -const HIDDEN_FROM_HUMAN_ID = 'Resource'; - -/** - * Resources with this ID are complete hidden from the logical ID calculation. - */ -const HIDDEN_ID = 'Default'; -const MAX_HUMAN_LEN = 240; // this is the value in CDK and seems like a good default to keep - -/** - * Calculates a unique ID for a set of textual components. - * - * This is forked from the internal cdk implementation with the removal of the hash suffix. - * We remove the hash from the CDK logical ID calculation because Pulumi already handles - * adding a unique random suffix and we do not want to end up with a double hash. - * @see https://github.com/aws/aws-cdk/blob/ccab485b87a7090ddf0773508d7b8ee84ff654b0/packages/aws-cdk-lib/core/lib/private/uniqueid.ts?plain=1#L32 - * - * @param components The path components - * @returns a unique alpha-numeric identifier with a maximum length of 255 - */ -function makeUniqueId(components: string[]) { - components = components.filter((x) => x !== HIDDEN_ID); - - if (components.length === 0) { - throw new Error('Unable to calculate a unique id for an empty set of components'); - } - - // Lazy require in order to break a module dependency cycle - const unresolvedTokens = components.filter((c) => cdk.Token.isUnresolved(c)); - if (unresolvedTokens.length > 0) { - throw new Error(`ID components may not include unresolved tokens: ${unresolvedTokens.join(',')}`); - } - - const human = removeDupes(components) - .filter((x) => x !== HIDDEN_FROM_HUMAN_ID) - .map(removeNonAlphanumeric) - .join('') - .slice(0, MAX_HUMAN_LEN); - - return human; -} - -/** - * Remove duplicate "terms" from the path list - * - * If the previous path component name ends with this component name, skip the - * current component. - */ -function removeDupes(path: string[]): string[] { - const ret = new Array(); - - for (const component of path) { - if (ret.length === 0 || !ret[ret.length - 1].endsWith(component)) { - ret.push(component); - } - } - - return ret; -} - -/** - * Removes all non-alphanumeric characters in a string. - */ -function removeNonAlphanumeric(s: string) { - return s.replace(/[^A-Za-z0-9]/g, ''); -}