@awsless/awsless 0.0.44 → 0.0.45
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/README.MD +1 -0
- package/dist/bin.js +1140 -447
- package/dist/features/delete-bucket.js +86 -0
- package/dist/features/delete-hosted-zone.js +127 -0
- package/dist/features/global-exports.js +61 -0
- package/dist/features/upload-bucket-asset.js +28384 -0
- package/dist/index.d.ts +734 -205
- package/package.json +14 -4
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { S3Client, ListObjectsV2Command, DeleteObjectsCommand, ListObjectVersionsCommand } from '@aws-sdk/client-s3';
|
|
2
|
+
|
|
3
|
+
const send = async (event, id, status, data, reason = '')=>{
|
|
4
|
+
const body = JSON.stringify({
|
|
5
|
+
Status: status,
|
|
6
|
+
Reason: reason,
|
|
7
|
+
PhysicalResourceId: id,
|
|
8
|
+
StackId: event.StackId,
|
|
9
|
+
RequestId: event.RequestId,
|
|
10
|
+
LogicalResourceId: event.LogicalResourceId,
|
|
11
|
+
NoEcho: false,
|
|
12
|
+
Data: data
|
|
13
|
+
});
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
await fetch(event.ResponseURL, {
|
|
16
|
+
method: 'PUT',
|
|
17
|
+
port: 443,
|
|
18
|
+
body,
|
|
19
|
+
headers: {
|
|
20
|
+
'content-type': '',
|
|
21
|
+
'content-length': Buffer.from(body).byteLength
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const client = new S3Client({});
|
|
27
|
+
const handler = async (event)=>{
|
|
28
|
+
const type = event.RequestType;
|
|
29
|
+
const bucketName = event.ResourceProperties.bucketName;
|
|
30
|
+
console.log('Type:', type);
|
|
31
|
+
console.log('BucketName:', bucketName);
|
|
32
|
+
try {
|
|
33
|
+
if (type === 'Delete') {
|
|
34
|
+
console.log('Deleting bucket objects...');
|
|
35
|
+
await emptyBucket(bucketName);
|
|
36
|
+
console.log('Done');
|
|
37
|
+
}
|
|
38
|
+
await send(event, bucketName, 'SUCCESS');
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (error instanceof Error) {
|
|
41
|
+
await send(event, bucketName, 'FAILED', {}, error.message);
|
|
42
|
+
} else {
|
|
43
|
+
await send(event, bucketName, 'FAILED', {}, 'Unknown error');
|
|
44
|
+
}
|
|
45
|
+
console.error(error);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const emptyBucket = async (bucket)=>{
|
|
49
|
+
while(true){
|
|
50
|
+
const result = await client.send(new ListObjectsV2Command({
|
|
51
|
+
Bucket: bucket,
|
|
52
|
+
MaxKeys: 1000
|
|
53
|
+
}));
|
|
54
|
+
if (!result.Contents || result.Contents.length === 0) {
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
await client.send(new DeleteObjectsCommand({
|
|
58
|
+
Bucket: bucket,
|
|
59
|
+
Delete: {
|
|
60
|
+
Objects: result.Contents.map((object)=>({
|
|
61
|
+
Key: object.Key
|
|
62
|
+
}))
|
|
63
|
+
}
|
|
64
|
+
}));
|
|
65
|
+
}
|
|
66
|
+
while(true){
|
|
67
|
+
const result = await client.send(new ListObjectVersionsCommand({
|
|
68
|
+
Bucket: bucket,
|
|
69
|
+
MaxKeys: 1000
|
|
70
|
+
}));
|
|
71
|
+
if (!result.Versions || result.Versions.length === 0) {
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
await client.send(new DeleteObjectsCommand({
|
|
75
|
+
Bucket: bucket,
|
|
76
|
+
Delete: {
|
|
77
|
+
Objects: result.Versions.map((object)=>({
|
|
78
|
+
Key: object.Key,
|
|
79
|
+
VersionId: object.VersionId
|
|
80
|
+
}))
|
|
81
|
+
}
|
|
82
|
+
}));
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export { handler };
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { Route53Client, ChangeResourceRecordSetsCommand, ListResourceRecordSetsCommand } from '@aws-sdk/client-route-53';
|
|
2
|
+
|
|
3
|
+
const send = async (event, id, status, data, reason = '')=>{
|
|
4
|
+
const body = JSON.stringify({
|
|
5
|
+
Status: status,
|
|
6
|
+
Reason: reason,
|
|
7
|
+
PhysicalResourceId: id,
|
|
8
|
+
StackId: event.StackId,
|
|
9
|
+
RequestId: event.RequestId,
|
|
10
|
+
LogicalResourceId: event.LogicalResourceId,
|
|
11
|
+
NoEcho: false,
|
|
12
|
+
Data: data
|
|
13
|
+
});
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
await fetch(event.ResponseURL, {
|
|
16
|
+
method: 'PUT',
|
|
17
|
+
port: 443,
|
|
18
|
+
body,
|
|
19
|
+
headers: {
|
|
20
|
+
'content-type': '',
|
|
21
|
+
'content-length': Buffer.from(body).byteLength
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
27
|
+
|
|
28
|
+
var chunkExports = {};
|
|
29
|
+
var chunk$1 = {
|
|
30
|
+
get exports(){ return chunkExports; },
|
|
31
|
+
set exports(v){ chunkExports = v; },
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
(function (module, exports) {
|
|
35
|
+
|
|
36
|
+
(function () {
|
|
37
|
+
function chunk(collection, size) {
|
|
38
|
+
var result = [];
|
|
39
|
+
|
|
40
|
+
// default size to two item
|
|
41
|
+
size = parseInt(size) || 2;
|
|
42
|
+
|
|
43
|
+
// add each chunk to the result
|
|
44
|
+
for (var x = 0; x < Math.ceil(collection.length / size); x++) {
|
|
45
|
+
var start = x * size;
|
|
46
|
+
var end = start + size;
|
|
47
|
+
|
|
48
|
+
result.push(collection.slice(start, end));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// export in node or browser
|
|
55
|
+
{
|
|
56
|
+
if (module.exports) {
|
|
57
|
+
exports = module.exports = chunk;
|
|
58
|
+
}
|
|
59
|
+
exports.chunk = chunk;
|
|
60
|
+
}
|
|
61
|
+
}.call(commonjsGlobal));
|
|
62
|
+
} (chunk$1, chunkExports));
|
|
63
|
+
|
|
64
|
+
var chunk = chunkExports;
|
|
65
|
+
|
|
66
|
+
const client = new Route53Client({});
|
|
67
|
+
const handler = async (event)=>{
|
|
68
|
+
const type = event.RequestType;
|
|
69
|
+
const hostedZoneId = event.ResourceProperties.hostedZoneId;
|
|
70
|
+
console.log('Type:', type);
|
|
71
|
+
console.log('HostedZoneId:', hostedZoneId);
|
|
72
|
+
try {
|
|
73
|
+
if (type === 'Delete') {
|
|
74
|
+
const records = await listHostedZoneRecords(hostedZoneId);
|
|
75
|
+
console.log('Records:', records);
|
|
76
|
+
await deleteHostedZoneRecords(hostedZoneId, records);
|
|
77
|
+
}
|
|
78
|
+
await send(event, hostedZoneId, 'SUCCESS');
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (error instanceof Error) {
|
|
81
|
+
await send(event, hostedZoneId, 'FAILED', {}, error.message);
|
|
82
|
+
} else {
|
|
83
|
+
await send(event, hostedZoneId, 'FAILED', {}, 'Unknown error');
|
|
84
|
+
}
|
|
85
|
+
console.error(error);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const deleteHostedZoneRecords = async (hostedZoneId, records)=>{
|
|
89
|
+
records = records.filter((record)=>![
|
|
90
|
+
'SOA',
|
|
91
|
+
'NS'
|
|
92
|
+
].includes(record.Type));
|
|
93
|
+
if (records.length === 0) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
await Promise.all(chunk(records, 100).map(async (records)=>{
|
|
97
|
+
await client.send(new ChangeResourceRecordSetsCommand({
|
|
98
|
+
HostedZoneId: hostedZoneId,
|
|
99
|
+
ChangeBatch: {
|
|
100
|
+
Changes: records.map((record)=>({
|
|
101
|
+
Action: 'DELETE',
|
|
102
|
+
ResourceRecordSet: record
|
|
103
|
+
}))
|
|
104
|
+
}
|
|
105
|
+
}));
|
|
106
|
+
}));
|
|
107
|
+
};
|
|
108
|
+
const listHostedZoneRecords = async (hostedZoneId)=>{
|
|
109
|
+
const records = [];
|
|
110
|
+
let token;
|
|
111
|
+
while(true){
|
|
112
|
+
const result = await client.send(new ListResourceRecordSetsCommand({
|
|
113
|
+
HostedZoneId: hostedZoneId,
|
|
114
|
+
StartRecordName: token
|
|
115
|
+
}));
|
|
116
|
+
if (result.ResourceRecordSets && result.ResourceRecordSets.length) {
|
|
117
|
+
records.push(...result.ResourceRecordSets);
|
|
118
|
+
}
|
|
119
|
+
if (result.NextRecordName) {
|
|
120
|
+
token = result.NextRecordName;
|
|
121
|
+
} else {
|
|
122
|
+
return records;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export { handler };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { CloudFormationClient, ListExportsCommand } from '@aws-sdk/client-cloudformation';
|
|
2
|
+
|
|
3
|
+
const send = async (event, id, status, data, reason = '')=>{
|
|
4
|
+
const body = JSON.stringify({
|
|
5
|
+
Status: status,
|
|
6
|
+
Reason: reason,
|
|
7
|
+
PhysicalResourceId: id,
|
|
8
|
+
StackId: event.StackId,
|
|
9
|
+
RequestId: event.RequestId,
|
|
10
|
+
LogicalResourceId: event.LogicalResourceId,
|
|
11
|
+
NoEcho: false,
|
|
12
|
+
Data: data
|
|
13
|
+
});
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
await fetch(event.ResponseURL, {
|
|
16
|
+
method: 'PUT',
|
|
17
|
+
port: 443,
|
|
18
|
+
body,
|
|
19
|
+
headers: {
|
|
20
|
+
'content-type': '',
|
|
21
|
+
'content-length': Buffer.from(body).byteLength
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const handler = async (event)=>{
|
|
27
|
+
const region = event.ResourceProperties.region;
|
|
28
|
+
try {
|
|
29
|
+
const data = await listExports(region);
|
|
30
|
+
await send(event, region, 'SUCCESS', data);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (error instanceof Error) {
|
|
33
|
+
await send(event, region, 'FAILED', {}, error.message);
|
|
34
|
+
} else {
|
|
35
|
+
await send(event, region, 'FAILED', {}, 'Unknown error');
|
|
36
|
+
}
|
|
37
|
+
console.error(error);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const listExports = async (region)=>{
|
|
41
|
+
const client = new CloudFormationClient({
|
|
42
|
+
region
|
|
43
|
+
});
|
|
44
|
+
const data = {};
|
|
45
|
+
let token;
|
|
46
|
+
while(true){
|
|
47
|
+
const result = await client.send(new ListExportsCommand({
|
|
48
|
+
NextToken: token
|
|
49
|
+
}));
|
|
50
|
+
result.Exports?.forEach((item)=>{
|
|
51
|
+
data[item.Name] = item.Value;
|
|
52
|
+
});
|
|
53
|
+
if (result.NextToken) {
|
|
54
|
+
token = result.NextToken;
|
|
55
|
+
} else {
|
|
56
|
+
return data;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export { handler };
|