-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
171 lines (152 loc) · 4.16 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
86
87
88
89
90
91
92
93
94
95
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { parse as cfnParse } from "./cfn.ts";
import { Options } from "./opts.ts";
import type * as cfn from "./cfn.ts";
async function readAsCfn(
input: ReadableStream<Uint8Array>,
): Promise<cfn.CfnSchema> {
const reader = input.getReader();
try {
const chunks = [];
while (true) {
const { value, done } = await reader.read();
if (done) {
break;
}
chunks.push(value);
}
const blobUrl = URL.createObjectURL(
new Blob(chunks, { type: "text/plain" }),
);
try {
const response = await fetch(blobUrl);
if (!response.ok) {
throw new Error(response.statusText);
}
const text = await response.text();
return cfnParse(text);
} finally {
URL.revokeObjectURL(blobUrl);
}
} finally {
reader.releaseLock();
}
}
function filterStateMachine(
schema: cfn.CfnSchema,
target: string | null,
): cfn.StateMachineResource {
if (
typeof schema.Resources === "undefined" ||
Object.entries(schema.Resources).length === 0
) {
throw new Error("No Resources exists.");
}
if (target === null) {
const [candidate, ...rest] = Object.entries(schema.Resources).flatMap((
[_, v],
) => v.Type === "AWS::Serverless::StateMachine" ? [v] : []);
if (typeof candidate === "undefined") {
throw new Error("No StateMachine exists in Resources.");
}
if (rest.length > 0) {
throw new Error(
`Please specify StateMachine in ${candidate}, ${rest.join(", ")}`,
);
}
return candidate;
}
const candidate = Object.entries(schema.Resources).find(([k, v]) =>
k === target && v.Type === "AWS::Serverless::StateMachine"
);
if (typeof candidate === "undefined") {
throw new Error(`No \`${target}\` exists in Resources.`);
}
const [_, result] = candidate;
if (result.Type !== "AWS::Serverless::StateMachine") {
throw new Error(); // bug
}
return result;
}
async function readAsl(
base: URL,
stateMachine: cfn.StateMachineResource,
): Promise<unknown> {
const uri = new URL(stateMachine.Properties.DefinitionUri, base);
const asl = await Deno.readTextFile(uri);
return JSON.parse(asl);
}
function expandString(
val: string,
stateMachine: cfn.StateMachineResource,
template: cfn.CfnSchema,
): string {
return val.replaceAll(/\$\{([^\}]*)\}/gm, (m, x) => {
const sub = stateMachine.Properties.DefinitionSubstitutions;
if (typeof sub === "undefined") {
return m;
}
const val = sub[x];
if (typeof val === "undefined") {
return m;
}
if (typeof val === "string") {
return val;
}
const [res, attr] = val["Fn::GetAtt"];
if (attr !== "Arn") {
return m;
}
const item = (template.Resources ?? {})[res];
if (typeof item === "undefined") {
return m;
}
const region = "us-east-1";
const account = "123456789012";
return `arn:aws:lambda:${region}:${account}:function:${x}`;
});
}
function expand(
asl: unknown,
stateMachine: cfn.StateMachineResource,
template: cfn.CfnSchema,
): unknown {
if (typeof asl === "number" || typeof asl === "boolean" || asl === null) {
return asl;
}
if (typeof asl === "string") {
return expandString(asl, stateMachine, template);
}
if (Array.isArray(asl)) {
const result = [];
for (const item of asl) {
result.push(expand(item, stateMachine, template));
}
return result;
}
const result: Record<string, unknown> = {};
for (
const [key, val] of Object.entries(asl as Record<keyof unknown, unknown>)
) {
if (typeof key !== "string") {
throw new Error();
}
result[key] = expand(val, stateMachine, template);
}
return result;
}
async function main() {
const opts = Options.from(Deno.args);
const template = await (async () => {
const fp = (await Deno.open(opts.template)).readable;
try {
return await readAsCfn(fp);
} finally {
await fp.cancel();
}
})();
const stateMachine = filterStateMachine(template, opts.target);
const asl = await readAsl(opts.template, stateMachine);
const expanded = expand(asl, stateMachine, template);
console.log(JSON.stringify(expanded, null, 2));
}
main().catch(console.error);