cdk-real-drift 0.4.15 → 0.4.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -7,6 +7,7 @@ import { CloudControlClient, DeleteResourceCommand, GetResourceCommand, GetResou
7
7
  import { homedir } from "node:os";
8
8
  import { BaseCredentials, CdkAppMultiContext, NonInteractiveIoHost, StackSelectionStrategy, Toolkit } from "@aws-cdk/toolkit-lib";
9
9
  import { CloudFormationClient, DescribeStackResourcesCommand, DescribeStacksCommand, DescribeTypeCommand, GetTemplateCommand, ListExportsCommand, ListStackResourcesCommand } from "@aws-sdk/client-cloudformation";
10
+ import jsonata from "jsonata";
10
11
  import { KMSClient, ListAliasesCommand } from "@aws-sdk/client-kms";
11
12
  import { APIGatewayClient, GetAuthorizersCommand, GetGatewayResponsesCommand, GetModelsCommand, GetRequestValidatorsCommand, GetResourcesCommand, UpdateIntegrationCommand, UpdateIntegrationResponseCommand, UpdateMethodResponseCommand, UpdateRestApiCommand } from "@aws-sdk/client-api-gateway";
12
13
  import { AppConfigClient, ListConfigurationProfilesCommand, ListEnvironmentsCommand } from "@aws-sdk/client-appconfig";
@@ -835,14 +836,26 @@ function applyIgnores(findings, scope, config) {
835
836
 
836
837
  //#endregion
837
838
  //#region src/synth/resolve-app.ts
839
+ function decodeCdkJson(buf) {
840
+ const encoding = buf[0] === 255 && buf[1] === 254 ? "utf-16le" : buf[0] === 254 && buf[1] === 255 ? "utf-16be" : "utf-8";
841
+ return new TextDecoder(encoding).decode(buf);
842
+ }
838
843
  function resolveApp(explicit) {
839
844
  if (explicit) return explicit;
845
+ if (!existsSync("cdk.json")) return void 0;
846
+ let text;
840
847
  try {
841
- if (existsSync("cdk.json")) {
842
- const json = JSON.parse(readFileSync("cdk.json", "utf8"));
843
- if (typeof json.app === "string" && json.app.length > 0) return json.app;
844
- }
845
- } catch {}
848
+ text = decodeCdkJson(readFileSync("cdk.json"));
849
+ } catch (e) {
850
+ throw new Error(`cdk.json exists but could not be read: ${e.message}`);
851
+ }
852
+ let json;
853
+ try {
854
+ json = JSON.parse(text);
855
+ } catch (e) {
856
+ throw new Error(`cdk.json exists but is not valid JSON (an unsupported encoding or a syntax error?): ${e.message}`);
857
+ }
858
+ return typeof json.app === "string" && json.app.length > 0 ? json.app : void 0;
846
859
  }
847
860
 
848
861
  //#endregion
@@ -4827,6 +4840,47 @@ function expandExecuteApiResources(policy, arnPrefix) {
4827
4840
  Statement: newStmts
4828
4841
  };
4829
4842
  }
4843
+ const transformCache = /* @__PURE__ */ new Map();
4844
+ function compileTransform(expr) {
4845
+ if (transformCache.has(expr)) return transformCache.get(expr) ?? null;
4846
+ let compiled = null;
4847
+ try {
4848
+ compiled = jsonata(expr);
4849
+ } catch {
4850
+ compiled = null;
4851
+ }
4852
+ transformCache.set(expr, compiled);
4853
+ return compiled;
4854
+ }
4855
+ function matchesPropertyTransform(transformExpr, declaredValue, parentObject, rootObject, liveValue, leafName) {
4856
+ const scopes = [
4857
+ parentObject,
4858
+ rootObject,
4859
+ { [leafName]: declaredValue },
4860
+ declaredValue
4861
+ ];
4862
+ for (const alt of transformExpr.split(" $OR ")) {
4863
+ const expr = alt.trim();
4864
+ if (!expr) continue;
4865
+ const compiled = compileTransform(expr);
4866
+ if (!compiled) continue;
4867
+ for (const scope of scopes) try {
4868
+ const out = compiled.evaluate(scope);
4869
+ if (out !== void 0 && deepEqual(out, liveValue)) return true;
4870
+ } catch {}
4871
+ }
4872
+ return false;
4873
+ }
4874
+ function resolveParentObject(root, dottedPath) {
4875
+ const segs = dottedPath.split(".");
4876
+ if (segs.length <= 1) return root;
4877
+ let node = root;
4878
+ for (const seg of segs.slice(0, -1)) {
4879
+ if (node == null || typeof node !== "object") return void 0;
4880
+ node = node[seg];
4881
+ }
4882
+ return node;
4883
+ }
4830
4884
  function classifyResource(resource, liveRaw, schema, opts = {}) {
4831
4885
  const { logicalId, resourceType, physicalId, declared: declaredIn } = resource;
4832
4886
  const findings = [];
@@ -5366,6 +5420,8 @@ function classifyResource(resource, liveRaw, schema, opts = {}) {
5366
5420
  declared,
5367
5421
  live
5368
5422
  }) ?? false)) continue;
5423
+ const transformExpr = schema.propertyTransforms?.[d.path] ?? schema.propertyTransforms?.[d.path.replace(/\.\d+(?=\.|$)/g, ".*")];
5424
+ if (transformExpr !== void 0 && matchesPropertyTransform(transformExpr, d.stateValue, resolveParentObject(declared, d.path), declared, d.awsValue, d.path.split(".").at(-1) ?? d.path)) continue;
5369
5425
  const unorderedExtra = unorderedObjArray && Array.isArray(declaredVal) && declaredRemapSource ? {
5370
5426
  path: remapSortedIndexToDeclared(d.path, k, declaredVal, declaredRemapSource),
5371
5427
  wholeArrayRevert: {
@@ -9340,6 +9396,8 @@ function parseSchema(schemaJson) {
9340
9396
  const unorderedArrayPaths = collectUnorderedArrayPaths(schema.definitions ?? {}, schema.properties ?? {});
9341
9397
  const freeFormMapPaths = collectFreeFormMapPaths(schema.definitions ?? {}, schema.properties ?? {});
9342
9398
  const updatable = schema.handlers === void 0 ? void 0 : Object.hasOwn(schema.handlers, "update");
9399
+ const propertyTransforms = {};
9400
+ for (const [ptr, expr] of Object.entries(schema.propertyTransform ?? {})) if (typeof expr === "string") propertyTransforms[pointerToDotted$1(ptr)] = expr;
9343
9401
  return {
9344
9402
  readOnly: topLevel(readOnlyPaths),
9345
9403
  writeOnly: topLevel(writeOnlyPaths),
@@ -9352,6 +9410,7 @@ function parseSchema(schemaJson) {
9352
9410
  unorderedScalarPaths: unorderedArrayPaths.scalar,
9353
9411
  unorderedObjectArrayPaths: unorderedArrayPaths.object,
9354
9412
  freeFormMapPaths,
9413
+ ...Object.keys(propertyTransforms).length > 0 && { propertyTransforms },
9355
9414
  updatable
9356
9415
  };
9357
9416
  }