Skip to content

Commit 0c21495

Browse files
authored
refactor(toolkit-lib): move message definitions to shared package (#220)
Move the message definitions to the shared package and make them public. This also required moving a bunch of type definitions to the shared package because they are used as payloads for the message types. In the CLI it's now possible to access those numbers and access their associated payload types in a type-checked way. A type-safe `switch` statement now looks like this: <img width="583" alt="image" src="https://github.com/user-attachments/assets/01c1cbf6-2e1a-4b8f-bd5c-180f72ff2f55" /> This change also makes the `data` field of an `IoMessage` required. The type of a message without payload is now `IoMessage<void>`: `data: void` is only inhabited by `data: undefined` which sort of tracks because `m.data === undefined` even if there is no payload (don't look too closely at the different behavior for `'data' in m` 😎 ). The type can no longer be `never` because a type like `{ data: never }` can not be inhabited by any values. We could also use `data: undefined`, *also* only admitting `undefined`, but `void` reads more clearly for its intent. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license
1 parent e20f1a8 commit 0c21495

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+383
-324
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export * from './io-host';
22
export * from './io-message';
33
export * from './toolkit-action';
4+
export * from './payloads';
5+
export * from './messages';

packages/@aws-cdk/tmp-toolkit-helpers/src/api/io/io-message.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export interface IoMessage<T> {
6262
/**
6363
* The data attached to the message.
6464
*/
65-
readonly data?: T;
65+
readonly data: T;
6666
}
6767

6868
/**

packages/@aws-cdk/toolkit-lib/lib/api/io/private/messages.ts renamed to packages/@aws-cdk/tmp-toolkit-helpers/src/api/io/messages.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type * as cxapi from '@aws-cdk/cx-api';
2-
import type { SdkTrace } from '../';
3-
import type { BootstrapEnvironmentProgress } from '../../../actions/bootstrap';
4-
import type { DeployConfirmationRequest, StackDeployProgress } from '../../../actions/deploy';
5-
import type { StackDestroyProgress } from '../../../actions/destroy';
6-
import type { StackDetailsPayload } from '../../../actions/list';
7-
import type { StackRollbackProgress } from '../../../actions/rollback';
8-
import type { FileWatchEvent, WatchSettings } from '../../../actions/watch';
9-
import type { AssemblyData, ConfirmationRequest, Duration, ErrorPayload, StackAndAssemblyData, SuccessfulDeployStackResult } from '../../../toolkit/types';
10-
import type { StackActivity, StackMonitoringControlEvent } from '../../aws-cdk';
11-
import type { MissingContext, UpdatedContext } from '../../cloud-assembly/context';
12-
import * as make from '../../shared-private';
2+
import type { BootstrapEnvironmentProgress } from './payloads/bootstrap-environment-progress';
3+
import type { MissingContext, UpdatedContext } from './payloads/context';
4+
import type { DeployConfirmationRequest, StackDeployProgress, SuccessfulDeployStackResult } from './payloads/deploy';
5+
import type { StackDestroyProgress } from './payloads/destroy';
6+
import type { StackDetailsPayload } from './payloads/list';
7+
import type { StackRollbackProgress } from './payloads/rollback';
8+
import type { SdkTrace } from './payloads/sdk-trace';
9+
import type { StackActivity, StackMonitoringControlEvent } from './payloads/stack-activity';
10+
import type { AssemblyData, ConfirmationRequest, Duration, ErrorPayload, StackAndAssemblyData } from './payloads/types';
11+
import type { FileWatchEvent, WatchSettings } from './payloads/watch';
12+
import * as make from './private';
1313

1414
/**
1515
* We have a rough system by which we assign message codes:
@@ -328,6 +328,10 @@ export const IO = {
328328
}),
329329

330330
// SDK codes
331+
CDK_SDK_I0000: make.trace({
332+
code: 'CDK_SDK_I0000',
333+
description: 'An SDK message.',
334+
}),
331335
CDK_SDK_I0100: make.trace<SdkTrace>({
332336
code: 'CDK_SDK_I0100',
333337
description: 'An SDK trace. SDK traces are emitted as traces to the IoHost, but contain the original SDK logging level.',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type * as cxapi from '@aws-cdk/cx-api';
2+
3+
export interface BootstrapEnvironmentProgress {
4+
/**
5+
* The total number of environments being deployed
6+
*/
7+
readonly total: number;
8+
/**
9+
* The count of the environment currently bootstrapped
10+
*
11+
* This is counting value, not an identifier.
12+
*/
13+
readonly current: number;
14+
/**
15+
* The environment that's currently being bootstrapped
16+
*/
17+
readonly environment: cxapi.Environment;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { CloudFormationStackArtifact } from '@aws-cdk/cx-api';
2+
import type { PermissionChangeType } from './diff';
3+
import type { ConfirmationRequest } from './types';
4+
5+
export interface StackDeployProgress {
6+
/**
7+
* The total number of stacks being deployed
8+
*/
9+
readonly total: number;
10+
/**
11+
* The count of the stack currently attempted to be deployed
12+
*
13+
* This is counting value, not an identifier.
14+
*/
15+
readonly current: number;
16+
/**
17+
* The stack that's currently being deployed
18+
*/
19+
readonly stack: CloudFormationStackArtifact;
20+
}
21+
22+
/**
23+
* Payload for a yes/no confirmation in deploy. Includes information on
24+
* what kind of change is being made.
25+
*/
26+
export interface DeployConfirmationRequest extends ConfirmationRequest {
27+
/**
28+
* The type of change being made to the IAM permissions.
29+
*/
30+
readonly permissionChangeType: PermissionChangeType;
31+
}
32+
33+
export type DeployStackResult =
34+
| SuccessfulDeployStackResult
35+
| NeedRollbackFirstDeployStackResult
36+
| ReplacementRequiresRollbackStackResult
37+
;
38+
39+
/** Successfully deployed a stack */
40+
export interface SuccessfulDeployStackResult {
41+
readonly type: 'did-deploy-stack';
42+
readonly noOp: boolean;
43+
readonly outputs: { [name: string]: string };
44+
readonly stackArn: string;
45+
}
46+
47+
/** The stack is currently in a failpaused state, and needs to be rolled back before the deployment */
48+
export interface NeedRollbackFirstDeployStackResult {
49+
readonly type: 'failpaused-need-rollback-first';
50+
readonly reason: 'not-norollback' | 'replacement';
51+
readonly status: string;
52+
}
53+
54+
/** The upcoming change has a replacement, which requires deploying with --rollback */
55+
export interface ReplacementRequiresRollbackStackResult {
56+
readonly type: 'replacement-requires-rollback';
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { CloudFormationStackArtifact } from '@aws-cdk/cx-api';
2+
3+
export interface StackDestroyProgress {
4+
/**
5+
* The total number of stacks being destroyed
6+
*/
7+
readonly total: number;
8+
/**
9+
* The count of the stack currently attempted to be destroyed
10+
*
11+
* This is counting value, not an identifier.
12+
*/
13+
readonly current: number;
14+
/**
15+
* The stack that's currently being destroyed
16+
*/
17+
readonly stack: CloudFormationStackArtifact;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Different types of permission related changes in a diff
3+
*/
4+
export enum PermissionChangeType {
5+
/**
6+
* No permission changes
7+
*/
8+
NONE = 'none',
9+
10+
/**
11+
* Permissions are broadening
12+
*/
13+
BROADENING = 'broadening',
14+
15+
/**
16+
* Permissions are changed but not broadening
17+
*/
18+
NON_BROADENING = 'non-broadening',
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export * from './bootstrap-environment-progress';
2+
export * from './deploy';
3+
export * from './destroy';
4+
export * from './list';
5+
export * from './sdk-trace';
6+
export * from './context';
7+
export * from './rollback';
8+
export * from './stack-activity';
9+
export * from './types';
10+
export * from './progress';
11+
export * from './watch';
12+
export * from './stack-details';
13+
export * from './diff';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { StackDetails } from './stack-details';
2+
3+
export interface StackDetailsPayload {
4+
stacks: StackDetails[];
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export interface StackProgress {
2+
/**
3+
* The total number of progress monitored resources.
4+
*/
5+
readonly total?: number;
6+
7+
/**
8+
* The number of completed resources.
9+
*/
10+
readonly completed: number;
11+
12+
/**
13+
* The current progress as a [34/42] string, or just [34] if the total is unknown.
14+
*/
15+
readonly formatted: string;
16+
}
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { CloudFormationStackArtifact } from '@aws-cdk/cx-api';
2+
3+
export interface StackRollbackProgress {
4+
/**
5+
* The total number of stacks being rolled back
6+
*/
7+
readonly total: number;
8+
/**
9+
* The count of the stack currently attempted to be rolled back
10+
*
11+
* This is counting value, not an identifier.
12+
*/
13+
readonly current: number;
14+
/**
15+
* The stack that's currently being rolled back
16+
*/
17+
readonly stack: CloudFormationStackArtifact;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* An SDK logging trace.
3+
*
4+
* Only info, warn and error level messages are emitted.
5+
* SDK traces are emitted as traces to the IoHost, but contain the original SDK logging level.
6+
*/
7+
export interface SdkTrace {
8+
/**
9+
* The level the SDK has emitted the original message with
10+
*/
11+
readonly sdkLevel: 'info' | 'warn' | 'error';
12+
13+
/**
14+
* The content of the SDK trace
15+
*
16+
* This will include the request and response data for API calls, including potentially sensitive information.
17+
*
18+
* @see https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/logging-sdk-calls.html
19+
*/
20+
readonly content: any;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { type MetadataEntry } from '@aws-cdk/cloud-assembly-schema';
2+
import { type CloudFormationStackArtifact } from '@aws-cdk/cx-api';
3+
import type { StackEvent } from '@aws-sdk/client-cloudformation';
4+
import type { StackProgress } from './progress';
5+
6+
/**
7+
* Payload when stack monitoring is starting or stopping for a given stack deployment.
8+
*/
9+
export interface StackMonitoringControlEvent {
10+
/**
11+
* A unique identifier for a specific stack deployment.
12+
*
13+
* Use this value to attribute stack activities received for concurrent deployments.
14+
*/
15+
readonly deployment: string;
16+
17+
/**
18+
* The stack artifact that is getting deployed
19+
*/
20+
readonly stack: CloudFormationStackArtifact;
21+
22+
/**
23+
* The name of the Stack that is getting deployed
24+
*/
25+
readonly stackName: string;
26+
27+
/**
28+
* Total number of resources taking part in this deployment
29+
*
30+
* The number might not always be known or accurate.
31+
* Only use for informational purposes and handle the case when it's unavailable.
32+
*/
33+
readonly resourcesTotal?: number;
34+
}
35+
36+
export interface StackActivity {
37+
/**
38+
* A unique identifier for a specific stack deployment.
39+
*
40+
* Use this value to attribute stack activities received for concurrent deployments.
41+
*/
42+
readonly deployment: string;
43+
44+
/**
45+
* The Stack Event as received from CloudFormation
46+
*/
47+
readonly event: StackEvent;
48+
49+
/**
50+
* Additional resource metadata
51+
*/
52+
readonly metadata?: ResourceMetadata;
53+
54+
/**
55+
* The stack progress
56+
*/
57+
readonly progress: StackProgress;
58+
}
59+
60+
export interface ResourceMetadata {
61+
entry: MetadataEntry;
62+
constructPath: string;
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type * as cxapi from '@aws-cdk/cx-api';
2+
3+
/**
4+
* The dependencies of a stack.
5+
*/
6+
export interface StackDependency {
7+
id: string;
8+
dependencies: StackDependency[];
9+
}
10+
11+
/**
12+
* Details of a stack.
13+
*/
14+
export interface StackDetails {
15+
id: string;
16+
name: string;
17+
environment: cxapi.Environment;
18+
dependencies: StackDependency[];
19+
}
20+

packages/@aws-cdk/toolkit-lib/lib/toolkit/types.ts renamed to packages/@aws-cdk/tmp-toolkit-helpers/src/api/io/payloads/types.ts

-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { SuccessfulDeployStackResult as _SuccessfulDeployStackResult } from '../api/aws-cdk';
2-
31
/**
42
* Assembly data returned in the payload of an IO Message.
53
*/
@@ -20,13 +18,6 @@ export interface AssemblyData {
2018
readonly stackIds: string[];
2119
}
2220

23-
/**
24-
* A successful deploy stack result. Intentionally exposed in toolkit-lib so documentation
25-
* can be generated from this interface.
26-
*/
27-
export interface SuccessfulDeployStackResult extends _SuccessfulDeployStackResult {
28-
}
29-
3021
/**
3122
* Stack data returned in the payload of an IO Message.
3223
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
/**
3+
* The computed file watch settings
4+
*/
5+
export interface WatchSettings {
6+
/**
7+
* The directory observed for file changes
8+
*/
9+
readonly watchDir: string;
10+
/**
11+
* List of include patterns for watching files
12+
*/
13+
readonly includes: string[];
14+
/**
15+
* List of excludes patterns for watching files
16+
*/
17+
readonly excludes: string[];
18+
}
19+
20+
export interface FileWatchEvent {
21+
/**
22+
* The change to the path
23+
*/
24+
readonly event: string;
25+
/**
26+
* The path that has an observed event
27+
*/
28+
readonly path?: string;
29+
}

packages/@aws-cdk/tmp-toolkit-helpers/src/api/io/private/io-helper.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type ActionLessRequest<T, U> = Omit<IoRequest<T, U>, 'action'>;
1414
*/
1515
export interface IoHelper extends IIoHost {
1616
notify(msg: ActionLessMessage<unknown>): Promise<void>;
17+
notify<T>(msg: ActionLessMessage<T>): Promise<void>;
1718
requestResponse<T, U>(msg: ActionLessRequest<T, U>): Promise<U>;
1819
}
1920

0 commit comments

Comments
 (0)