@aws-blocks/core 0.1.18 → 0.3.0
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/cdk/__fixtures__/import-meta-handler.d.ts +6 -0
- package/dist/cdk/__fixtures__/import-meta-handler.d.ts.map +1 -0
- package/dist/cdk/__fixtures__/import-meta-handler.js +13 -0
- package/dist/cdk/blocks-backend.d.ts +46 -9
- package/dist/cdk/blocks-backend.d.ts.map +1 -1
- package/dist/cdk/blocks-backend.js +66 -67
- package/dist/cdk/blocks-backend.test.js +114 -56
- package/dist/cdk/blocks-defaults.d.ts +70 -0
- package/dist/cdk/blocks-defaults.d.ts.map +1 -0
- package/dist/cdk/blocks-defaults.js +27 -0
- package/dist/cdk/blocks-defaults.test.d.ts +2 -0
- package/dist/cdk/blocks-defaults.test.d.ts.map +1 -0
- package/dist/cdk/blocks-defaults.test.js +24 -0
- package/dist/cdk/blocks-stack.test.js +74 -31
- package/dist/cdk/bundling.d.ts +42 -0
- package/dist/cdk/bundling.d.ts.map +1 -0
- package/dist/cdk/bundling.js +72 -0
- package/dist/cdk/bundling.test.d.ts +2 -0
- package/dist/cdk/bundling.test.d.ts.map +1 -0
- package/dist/cdk/bundling.test.js +80 -0
- package/dist/cdk/compute/compute.d.ts +33 -0
- package/dist/cdk/compute/compute.d.ts.map +1 -0
- package/dist/cdk/compute/compute.js +28 -0
- package/dist/cdk/compute/default-compute-factory.d.ts +33 -0
- package/dist/cdk/compute/default-compute-factory.d.ts.map +1 -0
- package/dist/cdk/compute/default-compute-factory.js +3 -0
- package/dist/cdk/index.d.ts +92 -12
- package/dist/cdk/index.d.ts.map +1 -1
- package/dist/cdk/index.js +141 -25
- package/dist/cdk/internal.d.ts +25 -0
- package/dist/cdk/internal.d.ts.map +1 -0
- package/dist/cdk/internal.js +27 -0
- package/dist/cdk/mixins.d.ts +18 -4
- package/dist/cdk/mixins.d.ts.map +1 -1
- package/dist/cdk/mixins.js +31 -7
- package/dist/cdk/mixins.test.js +45 -0
- package/dist/common/index.d.ts +9 -0
- package/dist/common/index.d.ts.map +1 -1
- package/dist/hosting-secrets.d.ts +48 -0
- package/dist/hosting-secrets.d.ts.map +1 -0
- package/dist/hosting-secrets.js +57 -0
- package/dist/hosting-secrets.test.d.ts +2 -0
- package/dist/hosting-secrets.test.d.ts.map +1 -0
- package/dist/hosting-secrets.test.js +81 -0
- package/dist/hosting.d.ts +88 -6
- package/dist/hosting.d.ts.map +1 -1
- package/dist/hosting.js +83 -24
- package/dist/hosting.test.js +158 -10
- package/dist/index.cdk.d.ts +13 -11
- package/dist/index.cdk.d.ts.map +1 -1
- package/dist/index.cdk.js +13 -8
- package/dist/index.d.ts +8 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -5
- package/dist/lambda-handler.test.js +66 -0
- package/dist/pipeline/index.d.ts +24 -2
- package/dist/pipeline/index.d.ts.map +1 -1
- package/dist/pipeline/index.js +41 -1
- package/dist/pipeline.test.d.ts +2 -0
- package/dist/pipeline.test.d.ts.map +1 -0
- package/dist/pipeline.test.js +47 -0
- package/dist/rpc.d.ts +21 -0
- package/dist/rpc.d.ts.map +1 -1
- package/dist/rpc.js +46 -3
- package/dist/rpc.test.js +58 -1
- package/dist/scripts/config.d.ts +15 -0
- package/dist/scripts/config.d.ts.map +1 -0
- package/dist/scripts/config.js +29 -0
- package/dist/scripts/deploy.d.ts.map +1 -1
- package/dist/scripts/deploy.js +4 -0
- package/dist/scripts/dev-server.d.ts +8 -0
- package/dist/scripts/dev-server.d.ts.map +1 -1
- package/dist/scripts/dev-server.js +31 -0
- package/dist/scripts/generate-client.d.ts.map +1 -1
- package/dist/scripts/generate-client.js +9 -4
- package/dist/scripts/generate-client.test.js +19 -4
- package/dist/scripts/index.d.ts +13 -10
- package/dist/scripts/index.d.ts.map +1 -1
- package/dist/scripts/index.js +11 -8
- package/dist/scripts/preflight-credentials.d.ts +30 -0
- package/dist/scripts/preflight-credentials.d.ts.map +1 -0
- package/dist/scripts/preflight-credentials.js +108 -0
- package/dist/scripts/preflight-credentials.test.d.ts +2 -0
- package/dist/scripts/preflight-credentials.test.d.ts.map +1 -0
- package/dist/scripts/preflight-credentials.test.js +77 -0
- package/dist/scripts/sandbox-args.test.d.ts +2 -0
- package/dist/scripts/sandbox-args.test.d.ts.map +1 -0
- package/dist/scripts/sandbox-args.test.js +77 -0
- package/dist/scripts/sandbox.d.ts +52 -0
- package/dist/scripts/sandbox.d.ts.map +1 -1
- package/dist/scripts/sandbox.js +92 -15
- package/dist/scripts/secret.d.ts +15 -0
- package/dist/scripts/secret.d.ts.map +1 -0
- package/dist/scripts/secret.js +29 -0
- package/dist/scripts/secret.test.d.ts +2 -0
- package/dist/scripts/secret.test.d.ts.map +1 -0
- package/dist/scripts/secret.test.js +28 -0
- package/dist/scripts/telemetry.test.js +2 -0
- package/dist/scripts/typegen.d.ts +15 -0
- package/dist/scripts/typegen.d.ts.map +1 -0
- package/dist/scripts/typegen.js +16 -0
- package/dist/secret-naming.d.ts +58 -0
- package/dist/secret-naming.d.ts.map +1 -0
- package/dist/secret-naming.js +67 -0
- package/dist/secret-naming.test.d.ts +2 -0
- package/dist/secret-naming.test.d.ts.map +1 -0
- package/dist/secret-naming.test.js +40 -0
- package/dist/telemetry/environment.d.ts.map +1 -1
- package/dist/telemetry/environment.js +3 -1
- package/dist/telemetry/telemetry.test.js +10 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/package.json +7 -1
- package/src/cdk/__fixtures__/import-meta-handler.ts +16 -0
- package/src/cdk/blocks-backend.test.ts +308 -237
- package/src/cdk/blocks-backend.ts +98 -77
- package/src/cdk/blocks-defaults.test.ts +27 -0
- package/src/cdk/blocks-defaults.ts +75 -0
- package/src/cdk/blocks-stack.test.ts +160 -110
- package/src/cdk/bundling.test.ts +90 -0
- package/src/cdk/bundling.ts +76 -0
- package/src/cdk/compute/compute.ts +37 -0
- package/src/cdk/compute/default-compute-factory.ts +37 -0
- package/src/cdk/index.ts +178 -27
- package/src/cdk/internal.ts +30 -0
- package/src/cdk/mixins.test.ts +56 -1
- package/src/cdk/mixins.ts +32 -7
- package/src/common/index.ts +9 -0
- package/src/hosting-secrets.test.ts +106 -0
- package/src/hosting-secrets.ts +106 -0
- package/src/hosting.test.ts +244 -63
- package/src/hosting.ts +180 -68
- package/src/index.cdk.ts +79 -36
- package/src/index.ts +35 -16
- package/src/lambda-handler.test.ts +74 -0
- package/src/pipeline/index.ts +64 -11
- package/src/pipeline.test.ts +55 -0
- package/src/rpc.test.ts +66 -1
- package/src/rpc.ts +60 -3
- package/src/scripts/config.ts +35 -0
- package/src/scripts/deploy.ts +5 -0
- package/src/scripts/dev-server.ts +40 -0
- package/src/scripts/generate-client.test.ts +43 -8
- package/src/scripts/generate-client.ts +70 -65
- package/src/scripts/index.ts +20 -17
- package/src/scripts/preflight-credentials.test.ts +110 -0
- package/src/scripts/preflight-credentials.ts +131 -0
- package/src/scripts/sandbox-args.test.ts +92 -0
- package/src/scripts/sandbox.ts +102 -15
- package/src/scripts/secret.test.ts +35 -0
- package/src/scripts/secret.ts +35 -0
- package/src/scripts/telemetry.test.ts +2 -0
- package/src/scripts/typegen.ts +18 -0
- package/src/secret-naming.test.ts +59 -0
- package/src/secret-naming.ts +99 -0
- package/src/telemetry/environment.ts +3 -1
- package/src/telemetry/telemetry.test.ts +12 -0
- package/src/version.ts +1 -1
|
@@ -1,19 +1,64 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import { test, describe, before } from 'node:test';
|
|
5
4
|
import assert from 'node:assert';
|
|
6
|
-
import { fileURLToPath } from 'node:url';
|
|
7
5
|
import { dirname, join } from 'node:path';
|
|
6
|
+
import { before, describe, test } from 'node:test';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
8
|
import * as cdk from 'aws-cdk-lib';
|
|
9
|
-
import {
|
|
9
|
+
import { Match, Template } from 'aws-cdk-lib/assertions';
|
|
10
|
+
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
|
|
10
11
|
import { PolicyStatement } from 'aws-cdk-lib/aws-iam';
|
|
12
|
+
import * as lambda from 'aws-cdk-lib/aws-lambda-nodejs';
|
|
13
|
+
import type { Construct } from 'constructs';
|
|
14
|
+
import type { ScopeParent } from '../common/index.js';
|
|
15
|
+
import { BLOCKS_RPC_PREFIX } from '../constants.js';
|
|
11
16
|
import { BlocksBackend } from './blocks-backend.js';
|
|
17
|
+
import { BlocksPresets } from './blocks-defaults.js';
|
|
18
|
+
import { Compute } from './compute/compute.js';
|
|
19
|
+
import type { DefaultComputeFactory } from './compute/default-compute-factory.js';
|
|
12
20
|
import { Scope } from './index.js';
|
|
13
21
|
|
|
22
|
+
// A real app gets its default compute from @aws-blocks/bb-lambda-compute (via
|
|
23
|
+
// @aws-blocks/blocks), which core's own tests can't depend on. Use an
|
|
24
|
+
// equivalent inline stub: a Compute that owns a NodejsFunction + API Gateway,
|
|
25
|
+
// so create() can build the default and the handler/gateway/apiUrl accessors
|
|
26
|
+
// and synth-shape assertions have something real to resolve to. It is passed to
|
|
27
|
+
// each create() via the internal `defaultComputeFactory` option (see
|
|
28
|
+
// makeBackend), exactly as @aws-blocks/blocks injects LambdaCompute.
|
|
29
|
+
class StubLambdaCompute extends Compute {
|
|
30
|
+
readonly fn: lambda.NodejsFunction;
|
|
31
|
+
readonly apiGateway: apigateway.RestApi;
|
|
32
|
+
readonly apiUrl: string;
|
|
33
|
+
|
|
34
|
+
constructor(scope: ScopeParent, id: string) {
|
|
35
|
+
super(id, { parent: scope });
|
|
36
|
+
this.fn = new lambda.NodejsFunction(this, 'Handler', {
|
|
37
|
+
entry: this.backendHandlerPath,
|
|
38
|
+
runtime: cdk.aws_lambda.Runtime.NODEJS_22_X,
|
|
39
|
+
handler: 'handler',
|
|
40
|
+
role: this.executionRole,
|
|
41
|
+
environment: { BLOCKS_STACK_NAME: this.backendStackName },
|
|
42
|
+
bundling: { minify: true, esbuildArgs: { '--conditions': 'aws-runtime' } },
|
|
43
|
+
});
|
|
44
|
+
this.apiGateway = new apigateway.RestApi(this, 'API', { restApiName: 'Blocks API' });
|
|
45
|
+
this.apiGateway.root.addProxy({
|
|
46
|
+
defaultIntegration: new apigateway.LambdaIntegration(this.fn),
|
|
47
|
+
anyMethod: true,
|
|
48
|
+
});
|
|
49
|
+
this.apiUrl = `${this.apiGateway.url}${BLOCKS_RPC_PREFIX.slice(1)}`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
setEnv(key: string, value: string): void {
|
|
53
|
+
this.fn.addEnvironment(key, value);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const stubComputeFactory: DefaultComputeFactory = (root) => new StubLambdaCompute(root as never, 'DefaultCompute');
|
|
58
|
+
|
|
14
59
|
// Simulate the CDK condition being active (tests import CDK files directly)
|
|
15
60
|
before(() => {
|
|
16
|
-
|
|
61
|
+
process.env.NODE_OPTIONS = (process.env.NODE_OPTIONS ?? '') + ' --conditions=cdk';
|
|
17
62
|
});
|
|
18
63
|
|
|
19
64
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
@@ -22,275 +67,301 @@ const sideEffectBackendPath = join(__dirname, '__fixtures__', 'side-effect-backe
|
|
|
22
67
|
const factoryBackendPath = join(__dirname, '__fixtures__', 'factory-backend.js');
|
|
23
68
|
const fullIdConstructBackendPath = join(__dirname, '__fixtures__', 'fullid-construct-backend.js');
|
|
24
69
|
const EXECUTION_ROLE_MARKER_ACTION = 'blocks-test:MarkerAction';
|
|
70
|
+
const importMetaHandlerPath = join(__dirname, '__fixtures__', 'import-meta-handler.js');
|
|
25
71
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const backend1 = await BlocksBackend.create(stack, 'Stage1', {
|
|
32
|
-
backendHandlerPath: handlerPath,
|
|
33
|
-
backendCDKPath: sideEffectBackendPath,
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
const backend2 = await BlocksBackend.create(stack, 'Stage2', {
|
|
37
|
-
backendHandlerPath: handlerPath,
|
|
38
|
-
backendCDKPath: sideEffectBackendPath,
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const findMarker = (scope: cdk.aws_lambda_nodejs.NodejsFunction | any) =>
|
|
42
|
-
scope.node.tryFindChild('SideEffectMarker');
|
|
72
|
+
// Wraps BlocksBackend.create, injecting the stub default-compute factory the way
|
|
73
|
+
// @aws-blocks/blocks injects LambdaCompute — so tests don't repeat it 15 times.
|
|
74
|
+
const makeBackend = (scope: Construct, id: string, backendCDKPath: string) =>
|
|
75
|
+
BlocksBackend.create(scope, id, { backendHandlerPath: handlerPath, backendCDKPath, defaults: BlocksPresets.production, defaultComputeFactory: stubComputeFactory });
|
|
43
76
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
77
|
+
describe('ESM cache-busting (multi-stage)', () => {
|
|
78
|
+
test('BlocksBackend.create() with same backendCDKPath but different IDs produces constructs in each', async () => {
|
|
79
|
+
const app = new cdk.App();
|
|
80
|
+
const stack = new cdk.Stack(app, 'TestStack');
|
|
81
|
+
|
|
82
|
+
const backend1 = await makeBackend(stack, 'Stage1', sideEffectBackendPath);
|
|
83
|
+
|
|
84
|
+
const backend2 = await makeBackend(stack, 'Stage2', sideEffectBackendPath);
|
|
85
|
+
|
|
86
|
+
const findMarker = (scope: cdk.aws_lambda_nodejs.NodejsFunction | any) =>
|
|
87
|
+
scope.node.tryFindChild('SideEffectMarker');
|
|
88
|
+
|
|
89
|
+
assert.ok(
|
|
90
|
+
findMarker(backend1),
|
|
91
|
+
'Stage1 backend should have SideEffectMarker construct from module side effect',
|
|
92
|
+
);
|
|
93
|
+
assert.ok(
|
|
94
|
+
findMarker(backend2),
|
|
95
|
+
'Stage2 backend should have SideEffectMarker construct from re-executed module',
|
|
96
|
+
);
|
|
97
|
+
});
|
|
53
98
|
});
|
|
54
99
|
|
|
55
100
|
describe('synth shape (drop into existing stack)', () => {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
});
|
|
85
|
-
await BlocksBackend.create(parent, 'BackendB', {
|
|
86
|
-
backendHandlerPath: handlerPath,
|
|
87
|
-
backendCDKPath: sideEffectBackendPath,
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
const template = Template.fromStack(parent);
|
|
91
|
-
template.resourceCountIs('AWS::ApiGateway::RestApi', 2);
|
|
92
|
-
});
|
|
101
|
+
test('BlocksBackend lives inside the parent stack and synthesizes Lambda + API Gateway', async () => {
|
|
102
|
+
const app = new cdk.App();
|
|
103
|
+
const parent = new cdk.Stack(app, 'MyExistingStack');
|
|
104
|
+
|
|
105
|
+
const backend = await makeBackend(parent, 'Blocks', sideEffectBackendPath);
|
|
106
|
+
|
|
107
|
+
// Public surface mirrors BlocksStack.
|
|
108
|
+
assert.ok(backend.handler, 'BlocksBackend should expose .handler');
|
|
109
|
+
assert.ok(backend.gateway, 'BlocksBackend should expose .gateway');
|
|
110
|
+
assert.ok(backend.apiUrl, 'BlocksBackend should expose .apiUrl');
|
|
111
|
+
|
|
112
|
+
// Synth produces the expected resources inside the parent stack —
|
|
113
|
+
// no separate stack is created.
|
|
114
|
+
const template = Template.fromStack(parent);
|
|
115
|
+
template.hasResourceProperties('AWS::Lambda::Function', {});
|
|
116
|
+
template.resourceCountIs('AWS::ApiGateway::RestApi', 1);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('multiple BlocksBackends in the same parent stack do not collide', async () => {
|
|
120
|
+
const app = new cdk.App();
|
|
121
|
+
const parent = new cdk.Stack(app, 'MultiBackendStack');
|
|
122
|
+
|
|
123
|
+
await makeBackend(parent, 'BackendA', sideEffectBackendPath);
|
|
124
|
+
await makeBackend(parent, 'BackendB', sideEffectBackendPath);
|
|
125
|
+
|
|
126
|
+
const template = Template.fromStack(parent);
|
|
127
|
+
template.resourceCountIs('AWS::ApiGateway::RestApi', 2);
|
|
128
|
+
});
|
|
93
129
|
});
|
|
94
130
|
|
|
95
131
|
describe('shared execution role', () => {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
132
|
+
test('exposes executionRole on the backend', async () => {
|
|
133
|
+
const app = new cdk.App();
|
|
134
|
+
const parent = new cdk.Stack(app, 'RoleSurfaceStack');
|
|
135
|
+
|
|
136
|
+
const backend = await makeBackend(parent, 'Blocks', sideEffectBackendPath);
|
|
137
|
+
|
|
138
|
+
assert.ok(backend.executionRole, 'BlocksBackend should expose .executionRole');
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test('synth produces a Lambda-assumable role with basic execution, and the handler uses it', async () => {
|
|
142
|
+
const app = new cdk.App();
|
|
143
|
+
const parent = new cdk.Stack(app, 'RoleSynthStack');
|
|
144
|
+
|
|
145
|
+
await makeBackend(parent, 'Blocks', sideEffectBackendPath);
|
|
146
|
+
|
|
147
|
+
const template = Template.fromStack(parent);
|
|
148
|
+
|
|
149
|
+
// The shared role (logical id derived from the 'BlocksRole' construct id)
|
|
150
|
+
// is assumable by Lambda and carries AWSLambdaBasicExecutionRole (so
|
|
151
|
+
// CloudWatch Logs keep working after swapping off the auto-role). Other
|
|
152
|
+
// roles exist (API Gateway CloudWatch role, config BucketDeployment role),
|
|
153
|
+
// so we target ours by logical id.
|
|
154
|
+
const roles = template.findResources('AWS::IAM::Role');
|
|
155
|
+
const blocksRoleId = Object.keys(roles).find((k) => k.includes('BlocksRole'));
|
|
156
|
+
assert.ok(blocksRoleId, 'expected a role from the BlocksRole construct');
|
|
157
|
+
const blocksRole = roles[blocksRoleId];
|
|
158
|
+
assert.deepStrictEqual(blocksRole.Properties.AssumeRolePolicyDocument.Statement[0], {
|
|
159
|
+
Action: 'sts:AssumeRole',
|
|
160
|
+
Effect: 'Allow',
|
|
161
|
+
Principal: { Service: 'lambda.amazonaws.com' },
|
|
162
|
+
});
|
|
163
|
+
assert.ok(
|
|
164
|
+
JSON.stringify(blocksRole.Properties.ManagedPolicyArns ?? []).includes('AWSLambdaBasicExecutionRole'),
|
|
165
|
+
'BlocksRole should attach AWSLambdaBasicExecutionRole',
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
// The Blocks handler references the shared role, not an auto-generated one.
|
|
169
|
+
template.hasResourceProperties('AWS::Lambda::Function', {
|
|
170
|
+
Role: { 'Fn::GetAtt': [blocksRoleId, 'Arn'] },
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test('a nested block resolves executionRole via the construct-tree walk', async () => {
|
|
175
|
+
const app = new cdk.App();
|
|
176
|
+
const parent = new cdk.Stack(app, 'RoleResolveStack');
|
|
177
|
+
|
|
178
|
+
const backend = await makeBackend(parent, 'Blocks', sideEffectBackendPath);
|
|
179
|
+
|
|
180
|
+
// Build nested Scopes under the backend (outer → inner), the same shape a
|
|
181
|
+
// real Building Block tree has, and grant a uniquely-named marker action to
|
|
182
|
+
// `this.executionRole` from the innermost scope. If the getter's tree-walk
|
|
183
|
+
// failed, it would resolve the wrong role (or throw), and the marker would
|
|
184
|
+
// not land on the backend's shared role.
|
|
185
|
+
// `create()` sets globalThis.CURRENT_BLOCKS_STACK = backend, so a parent-less
|
|
186
|
+
// Scope attaches under the backend (the same way a real backend module's
|
|
187
|
+
// top-level blocks do); `inner` is then nested one level deeper.
|
|
188
|
+
const outer = new Scope('outer');
|
|
189
|
+
const inner = new Scope('inner', { parent: outer });
|
|
190
|
+
|
|
191
|
+
// Resolves to the backend's shared role from two levels deep.
|
|
192
|
+
assert.strictEqual(inner.executionRole, backend.executionRole);
|
|
193
|
+
|
|
194
|
+
inner.executionRole.addToPrincipalPolicy(
|
|
195
|
+
new PolicyStatement({ actions: [EXECUTION_ROLE_MARKER_ACTION], resources: ['*'] }),
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
// The grant lands on the shared role's default inline policy (AWS::IAM::Policy).
|
|
199
|
+
const template = Template.fromStack(parent);
|
|
200
|
+
template.hasResourceProperties('AWS::IAM::Policy', {
|
|
201
|
+
PolicyDocument: {
|
|
202
|
+
Statement: Match.arrayWith([Match.objectLike({ Action: EXECUTION_ROLE_MARKER_ACTION })]),
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
});
|
|
143
207
|
|
|
144
|
-
|
|
208
|
+
describe('CJS bundle: import.meta.url in the handler is shimmed (no Lambda-load crash)', () => {
|
|
209
|
+
test('a handler that uses import.meta.url bundles successfully instead of throwing at load', async () => {
|
|
210
|
+
// The handler is bundled to CJS, where `import.meta` is empty. Left unshimmed,
|
|
211
|
+
// `fileURLToPath(import.meta.url)` compiles to `fileURLToPath(undefined)` and
|
|
212
|
+
// throws at Lambda load (esbuild only warns, so the broken bundle would deploy).
|
|
213
|
+
// blocksNodejsBundling shims import.meta.* to CommonJS equivalents, so bundling
|
|
214
|
+
// (which runs synchronously during construction) succeeds. The runtime behaviour
|
|
215
|
+
// of the emitted shim is verified directly in bundling.test.ts.
|
|
145
216
|
const app = new cdk.App();
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
// `this.executionRole` from the innermost scope. If the getter's tree-walk
|
|
156
|
-
// failed, it would resolve the wrong role (or throw), and the marker would
|
|
157
|
-
// not land on the backend's shared role.
|
|
158
|
-
// `create()` sets globalThis.CURRENT_BLOCKS_STACK = backend, so a parent-less
|
|
159
|
-
// Scope attaches under the backend (the same way a real backend module's
|
|
160
|
-
// top-level blocks do); `inner` is then nested one level deeper.
|
|
161
|
-
const outer = new Scope('outer');
|
|
162
|
-
const inner = new Scope('inner', { parent: outer });
|
|
163
|
-
|
|
164
|
-
// Resolves to the backend's shared role from two levels deep.
|
|
165
|
-
assert.strictEqual(inner.executionRole, backend.executionRole);
|
|
166
|
-
|
|
167
|
-
inner.executionRole.addToPrincipalPolicy(
|
|
168
|
-
new PolicyStatement({ actions: [EXECUTION_ROLE_MARKER_ACTION], resources: ['*'] }),
|
|
217
|
+
const stack = new cdk.Stack(app, 'ImportMetaStack');
|
|
218
|
+
|
|
219
|
+
await assert.doesNotReject(() =>
|
|
220
|
+
BlocksBackend.create(stack, 'blocks', {
|
|
221
|
+
backendHandlerPath: importMetaHandlerPath,
|
|
222
|
+
backendCDKPath: sideEffectBackendPath,
|
|
223
|
+
defaults: BlocksPresets.production,
|
|
224
|
+
defaultComputeFactory: stubComputeFactory,
|
|
225
|
+
}),
|
|
169
226
|
);
|
|
170
|
-
|
|
171
|
-
// The grant lands on the shared role's default inline policy (AWS::IAM::Policy).
|
|
172
|
-
const template = Template.fromStack(parent);
|
|
173
|
-
template.hasResourceProperties('AWS::IAM::Policy', {
|
|
174
|
-
PolicyDocument: {
|
|
175
|
-
Statement: Match.arrayWith([Match.objectLike({ Action: EXECUTION_ROLE_MARKER_ACTION })]),
|
|
176
|
-
},
|
|
177
|
-
});
|
|
178
227
|
});
|
|
179
228
|
});
|
|
180
229
|
|
|
181
230
|
describe('factory function support', () => {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
231
|
+
test('BlocksBackend.create() calls default export function with the backend instance', async () => {
|
|
232
|
+
const app = new cdk.App();
|
|
233
|
+
const stack = new cdk.Stack(app, 'FactoryTestStack');
|
|
185
234
|
|
|
186
|
-
|
|
187
|
-
backendHandlerPath: handlerPath,
|
|
188
|
-
backendCDKPath: factoryBackendPath,
|
|
189
|
-
});
|
|
235
|
+
const backend = await makeBackend(stack, 'FactoryStage', factoryBackendPath);
|
|
190
236
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
237
|
+
const marker = backend.node.tryFindChild('FactoryMarker');
|
|
238
|
+
assert.ok(marker, 'Factory function should have created FactoryMarker on the backend');
|
|
239
|
+
});
|
|
194
240
|
});
|
|
195
241
|
|
|
196
242
|
describe('fullId is token-free (construct IDs / env-var keys)', () => {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
243
|
+
test('top-level stack: fullId is {stackName}-{id} and resolvable', async () => {
|
|
244
|
+
const app = new cdk.App();
|
|
245
|
+
const stack = new cdk.Stack(app, 'TopLevelStack');
|
|
246
|
+
|
|
247
|
+
const backend = await makeBackend(stack, 'blocks', sideEffectBackendPath);
|
|
248
|
+
|
|
249
|
+
assert.strictEqual(backend.fullId, 'TopLevelStack-blocks');
|
|
250
|
+
assert.ok(!cdk.Token.isUnresolved(backend.fullId), 'fullId must not contain a token');
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test('nested stack: fullId stays token-free (regression for #714 / Amplify Gen2)', async () => {
|
|
254
|
+
// Amplify Gen2 wires Blocks into a NestedStack via backend.createStack('blocks').
|
|
255
|
+
// A NestedStack has a tokenized stackName that only resolves at deploy time —
|
|
256
|
+
// embedding it in fullId broke construct IDs with
|
|
257
|
+
// "ID components may not include unresolved tokens".
|
|
258
|
+
const app = new cdk.App();
|
|
259
|
+
const parent = new cdk.Stack(app, 'ParentStack');
|
|
260
|
+
const nested = new cdk.NestedStack(parent, 'blocks');
|
|
261
|
+
|
|
262
|
+
// Sanity: the nested stack's own name really is a token.
|
|
263
|
+
assert.ok(
|
|
264
|
+
cdk.Token.isUnresolved(nested.stackName),
|
|
265
|
+
'precondition: NestedStack.stackName should be an unresolved token',
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
const backend = await makeBackend(nested, 'blocks', sideEffectBackendPath);
|
|
269
|
+
|
|
270
|
+
assert.ok(
|
|
271
|
+
!cdk.Token.isUnresolved(backend.fullId),
|
|
272
|
+
`fullId must be token-free inside a nested stack, got: ${backend.fullId}`,
|
|
273
|
+
);
|
|
274
|
+
// Falls back to the top-level (concrete) stack name, keeping uniqueness.
|
|
275
|
+
assert.strictEqual(backend.fullId, 'ParentStack-blocks');
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
test('child Scope can use fullId as a construct ID inside a nested stack', async () => {
|
|
279
|
+
// The fixture mirrors bb-distributed-data: it builds a construct whose ID is
|
|
280
|
+
// `${this.fullId}Marker`. If fullId carried a token, synth would throw.
|
|
281
|
+
const app = new cdk.App();
|
|
282
|
+
const parent = new cdk.Stack(app, 'Gen2ParentStack');
|
|
283
|
+
const nested = new cdk.NestedStack(parent, 'blocks');
|
|
284
|
+
|
|
285
|
+
const backend = await makeBackend(nested, 'blocks', fullIdConstructBackendPath);
|
|
286
|
+
|
|
287
|
+
// The construct ID is `${scope.fullId}Marker` → `ParentStack-blocks-blocks-dbMarker`.
|
|
288
|
+
const expectedId = `${backend.fullId}-dbMarker`;
|
|
289
|
+
assert.ok(nested.node.tryFindChild(expectedId), `expected a child construct with id "${expectedId}"`);
|
|
290
|
+
|
|
291
|
+
// Full synth must not throw on unresolved-token-in-construct-id.
|
|
292
|
+
assert.doesNotThrow(() => app.synth());
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
test('BLOCKS_STACK_NAME env var equals fullId (CDK-time ↔ runtime invariant)', async () => {
|
|
296
|
+
// Physical resource names (DynamoDB tables, DSQL env-var keys, IAM ARNs) are
|
|
297
|
+
// derived from fullId on BOTH sides: at synth via BlocksBackend.fullId, and at
|
|
298
|
+
// runtime via the root parent `{ id: process.env.BLOCKS_STACK_NAME }`. They MUST
|
|
299
|
+
// be byte-for-byte identical, otherwise the runtime looks up names/grants that
|
|
300
|
+
// were never created. BlocksBackend keeps them in sync by writing fullId into the
|
|
301
|
+
// handler's BLOCKS_STACK_NAME env var — assert that contract holds and is token-free.
|
|
302
|
+
const app = new cdk.App();
|
|
303
|
+
const parent = new cdk.Stack(app, 'InvariantParent');
|
|
304
|
+
const nested = new cdk.NestedStack(parent, 'blocks');
|
|
305
|
+
|
|
306
|
+
const backend = await makeBackend(nested, 'blocks', sideEffectBackendPath);
|
|
307
|
+
|
|
308
|
+
const template = Template.fromStack(nested);
|
|
309
|
+
const fns = template.findResources('AWS::Lambda::Function');
|
|
310
|
+
const envValues = Object.values(fns)
|
|
311
|
+
.map((fn: any) => fn.Properties?.Environment?.Variables?.BLOCKS_STACK_NAME)
|
|
312
|
+
.filter((v): v is string => typeof v === 'string');
|
|
313
|
+
|
|
314
|
+
assert.ok(envValues.length > 0, 'expected a Lambda with a BLOCKS_STACK_NAME env var');
|
|
315
|
+
for (const value of envValues) {
|
|
316
|
+
assert.strictEqual(
|
|
317
|
+
value,
|
|
318
|
+
backend.fullId,
|
|
319
|
+
'BLOCKS_STACK_NAME must equal BlocksBackend.fullId so runtime names match synth',
|
|
320
|
+
);
|
|
321
|
+
assert.ok(!cdk.Token.isUnresolved(value), `BLOCKS_STACK_NAME must be token-free, got: ${value}`);
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
});
|
|
209
325
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
// A NestedStack has a tokenized stackName that only resolves at deploy time —
|
|
213
|
-
// embedding it in fullId broke construct IDs with
|
|
214
|
-
// "ID components may not include unresolved tokens".
|
|
326
|
+
describe('infrastructure defaults (backend-anchored)', () => {
|
|
327
|
+
test('each backend exposes its own defaults', async () => {
|
|
215
328
|
const app = new cdk.App();
|
|
216
|
-
const
|
|
217
|
-
const nested = new cdk.NestedStack(parent, 'blocks');
|
|
218
|
-
|
|
219
|
-
// Sanity: the nested stack's own name really is a token.
|
|
220
|
-
assert.ok(
|
|
221
|
-
cdk.Token.isUnresolved(nested.stackName),
|
|
222
|
-
'precondition: NestedStack.stackName should be an unresolved token',
|
|
223
|
-
);
|
|
329
|
+
const stack = new cdk.Stack(app, 'TwoBackendsStack');
|
|
224
330
|
|
|
225
|
-
const
|
|
331
|
+
const a = await BlocksBackend.create(stack, 'A', {
|
|
226
332
|
backendHandlerPath: handlerPath,
|
|
227
333
|
backendCDKPath: sideEffectBackendPath,
|
|
334
|
+
defaults: BlocksPresets.production,
|
|
335
|
+
defaultComputeFactory: stubComputeFactory,
|
|
228
336
|
});
|
|
229
|
-
|
|
230
|
-
assert.ok(
|
|
231
|
-
!cdk.Token.isUnresolved(backend.fullId),
|
|
232
|
-
`fullId must be token-free inside a nested stack, got: ${backend.fullId}`,
|
|
233
|
-
);
|
|
234
|
-
// Falls back to the top-level (concrete) stack name, keeping uniqueness.
|
|
235
|
-
assert.strictEqual(backend.fullId, 'ParentStack-blocks');
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
test('child Scope can use fullId as a construct ID inside a nested stack', async () => {
|
|
239
|
-
// The fixture mirrors bb-distributed-data: it builds a construct whose ID is
|
|
240
|
-
// `${this.fullId}Marker`. If fullId carried a token, synth would throw.
|
|
241
|
-
const app = new cdk.App();
|
|
242
|
-
const parent = new cdk.Stack(app, 'Gen2ParentStack');
|
|
243
|
-
const nested = new cdk.NestedStack(parent, 'blocks');
|
|
244
|
-
|
|
245
|
-
const backend = await BlocksBackend.create(nested, 'blocks', {
|
|
337
|
+
const b = await BlocksBackend.create(stack, 'B', {
|
|
246
338
|
backendHandlerPath: handlerPath,
|
|
247
|
-
backendCDKPath:
|
|
339
|
+
backendCDKPath: sideEffectBackendPath,
|
|
340
|
+
defaults: BlocksPresets.sandbox,
|
|
341
|
+
defaultComputeFactory: stubComputeFactory,
|
|
248
342
|
});
|
|
249
343
|
|
|
250
|
-
//
|
|
251
|
-
|
|
252
|
-
assert.
|
|
253
|
-
|
|
254
|
-
`expected a child construct with id "${expectedId}"`,
|
|
255
|
-
);
|
|
256
|
-
|
|
257
|
-
// Full synth must not throw on unresolved-token-in-construct-id.
|
|
258
|
-
assert.doesNotThrow(() => app.synth());
|
|
344
|
+
// Two backends in one stack must NOT clobber each other — defaults are
|
|
345
|
+
// anchored on the backend, not the shared stack.
|
|
346
|
+
assert.strictEqual(a.defaults, BlocksPresets.production);
|
|
347
|
+
assert.strictEqual(b.defaults, BlocksPresets.sandbox);
|
|
259
348
|
});
|
|
260
349
|
|
|
261
|
-
test('
|
|
262
|
-
// Physical resource names (DynamoDB tables, DSQL env-var keys, IAM ARNs) are
|
|
263
|
-
// derived from fullId on BOTH sides: at synth via BlocksBackend.fullId, and at
|
|
264
|
-
// runtime via the root parent `{ id: process.env.BLOCKS_STACK_NAME }`. They MUST
|
|
265
|
-
// be byte-for-byte identical, otherwise the runtime looks up names/grants that
|
|
266
|
-
// were never created. BlocksBackend keeps them in sync by writing fullId into the
|
|
267
|
-
// handler's BLOCKS_STACK_NAME env var — assert that contract holds and is token-free.
|
|
350
|
+
test('a nested block resolves its owning backend defaults via the tree-walk', async () => {
|
|
268
351
|
const app = new cdk.App();
|
|
269
|
-
const
|
|
270
|
-
const nested = new cdk.NestedStack(parent, 'blocks');
|
|
352
|
+
const stack = new cdk.Stack(app, 'ResolveDefaultsStack');
|
|
271
353
|
|
|
272
|
-
const backend = await BlocksBackend.create(
|
|
354
|
+
const backend = await BlocksBackend.create(stack, 'Blocks', {
|
|
273
355
|
backendHandlerPath: handlerPath,
|
|
274
356
|
backendCDKPath: sideEffectBackendPath,
|
|
357
|
+
defaults: BlocksPresets.sandbox,
|
|
358
|
+
defaultComputeFactory: stubComputeFactory,
|
|
275
359
|
});
|
|
276
360
|
|
|
277
|
-
|
|
278
|
-
const
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
assert.ok(envValues.length > 0, 'expected a Lambda with a BLOCKS_STACK_NAME env var');
|
|
284
|
-
for (const value of envValues) {
|
|
285
|
-
assert.strictEqual(
|
|
286
|
-
value,
|
|
287
|
-
backend.fullId,
|
|
288
|
-
'BLOCKS_STACK_NAME must equal BlocksBackend.fullId so runtime names match synth',
|
|
289
|
-
);
|
|
290
|
-
assert.ok(
|
|
291
|
-
!cdk.Token.isUnresolved(value),
|
|
292
|
-
`BLOCKS_STACK_NAME must be token-free, got: ${value}`,
|
|
293
|
-
);
|
|
294
|
-
}
|
|
361
|
+
// A Scope under the backend resolves scope.defaults by walking up to it.
|
|
362
|
+
const outer = new Scope('outer');
|
|
363
|
+
const inner = new Scope('inner', { parent: outer });
|
|
364
|
+
assert.strictEqual(inner.defaults, backend.defaults);
|
|
365
|
+
assert.strictEqual(inner.defaults, BlocksPresets.sandbox);
|
|
295
366
|
});
|
|
296
367
|
});
|