@@ -2,94 +2,106 @@ const aws = require("aws-sdk");
22const fs = require ( "fs" ) ;
33const path = require ( "path" ) ;
44const mime = require ( "mime-types" ) ;
5+ const https = require ( "https" ) ;
6+ const url = require ( "url" ) ;
57
68const s3 = new aws . S3 ( ) ;
79
810const SUCCESS = "SUCCESS" ;
911const FAILED = "FAILED" ;
1012
11- const BUCKET = process . env . BUCKET ;
13+ const { BUCKET } = process . env ;
1214
13- exports . staticHandler = function ( event , context ) {
14- if ( event . RequestType !== "Create" && event . RequestType !== "Update" ) {
15- return respond ( event , context , SUCCESS , { } ) ;
16- }
15+ exports . staticHandler = function ( event , context ) {
16+ if ( event . RequestType !== "Create" && event . RequestType !== "Update" ) {
17+ return respond ( event , context , SUCCESS , { } ) ;
18+ }
19+
20+ Promise . all (
21+ walkSync ( "./" ) . map ( ( file ) => {
22+ const fileType = mime . lookup ( file ) || "application/octet-stream" ;
1723
18- Promise . all ( walkSync ( "./" ) . map ( file => {
19- var fileType = mime . lookup ( file ) || "application/octet-stream" ;
20-
21- console . log ( `${ file } -> ${ fileType } ` ) ;
22-
23- return s3 . upload ( {
24- Body : fs . createReadStream ( file ) ,
25- Bucket : BUCKET ,
26- ContentType : fileType ,
27- Key : file ,
28- ACL : "private" ,
29- } ) . promise ( ) ;
30- } ) ) . then ( ( msg ) => {
31- respond ( event , context , SUCCESS , { } ) ;
32- } ) . catch ( err => {
33- respond ( event , context , FAILED , { Message : err } ) ;
24+ console . log ( `${ file } -> ${ fileType } ` ) ;
25+
26+ return s3
27+ . upload ( {
28+ Body : fs . createReadStream ( file ) ,
29+ Bucket : BUCKET ,
30+ ContentType : fileType ,
31+ Key : file ,
32+ ACL : "private" ,
33+ } )
34+ . promise ( ) ;
35+ } )
36+ )
37+ . then ( ( msg ) => {
38+ respond ( event , context , SUCCESS , { } ) ;
39+ } )
40+ . catch ( ( err ) => {
41+ respond ( event , context , FAILED , { Message : err } ) ;
3442 } ) ;
3543} ;
3644
3745// List all files in a directory in Node.js recursively in a synchronous fashion
38- function walkSync ( dir , filelist ) {
39- var files = fs . readdirSync ( dir ) ;
40- filelist = filelist || [ ] ;
41-
42- files . forEach ( function ( file ) {
43- if ( fs . statSync ( path . join ( dir , file ) ) . isDirectory ( ) ) {
44- filelist = walkSync ( path . join ( dir , file ) , filelist ) ;
45- } else {
46- filelist . push ( path . join ( dir , file ) ) ;
47- }
48- } ) ;
49-
50- return filelist ;
51- } ;
46+ function walkSync ( dir , filelist = [ ] ) {
47+ const files = fs . readdirSync ( dir ) ;
5248
53- function respond ( event , context , responseStatus , responseData , physicalResourceId , noEcho ) {
54- var responseBody = JSON . stringify ( {
55- Status : responseStatus ,
56- Reason : "See the details in CloudWatch Log Stream: " + context . logStreamName ,
57- PhysicalResourceId : physicalResourceId || context . logStreamName ,
58- StackId : event . StackId ,
59- RequestId : event . RequestId ,
60- LogicalResourceId : event . LogicalResourceId ,
61- NoEcho : noEcho || false ,
62- Data : responseData
63- } ) ;
64-
65- console . log ( "Response body:\n" , responseBody ) ;
66-
67- var https = require ( "https" ) ;
68- var url = require ( "url" ) ;
69-
70- var parsedUrl = new url . URL ( event . ResponseURL ) ;
71- var options = {
72- hostname : parsedUrl . hostname ,
73- port : 443 ,
74- path : parsedUrl . pathname + parsedUrl . search ,
75- method : "PUT" ,
76- headers : {
77- "content-type" : "" ,
78- "content-length" : responseBody . length
79- }
80- } ;
81-
82- var request = https . request ( options , function ( response ) {
83- console . log ( "Status code: " + response . statusCode ) ;
84- console . log ( "Status message: " + response . statusMessage ) ;
85- context . done ( ) ;
86- } ) ;
87-
88- request . on ( "error" , function ( error ) {
89- console . log ( "send(..) failed executing https.request(..): " + error ) ;
90- context . done ( ) ;
91- } ) ;
92-
93- request . write ( responseBody ) ;
94- request . end ( ) ;
49+ files . forEach ( function ( file ) {
50+ if ( fs . statSync ( path . join ( dir , file ) ) . isDirectory ( ) ) {
51+ filelist = walkSync ( path . join ( dir , file ) , filelist ) ;
52+ } else {
53+ filelist . push ( path . join ( dir , file ) ) ;
54+ }
55+ } ) ;
56+
57+ return filelist ;
58+ }
59+
60+ function respond (
61+ event ,
62+ context ,
63+ responseStatus ,
64+ responseData ,
65+ physicalResourceId ,
66+ noEcho
67+ ) {
68+ const responseBody = JSON . stringify ( {
69+ Status : responseStatus ,
70+ Reason :
71+ "See the details in CloudWatch Log Stream: " + context . logStreamName ,
72+ PhysicalResourceId : physicalResourceId || context . logStreamName ,
73+ StackId : event . StackId ,
74+ RequestId : event . RequestId ,
75+ LogicalResourceId : event . LogicalResourceId ,
76+ NoEcho : noEcho || false ,
77+ Data : responseData ,
78+ } ) ;
79+
80+ console . log ( "Response body:\n" , responseBody ) ;
81+
82+ const { pathname, hostname, search } = new url . URL ( event . ResponseURL ) ;
83+ const options = {
84+ hostname,
85+ port : 443 ,
86+ path : pathname + search ,
87+ method : "PUT" ,
88+ headers : {
89+ "content-type" : "" ,
90+ "content-length" : responseBody . length ,
91+ } ,
92+ } ;
93+
94+ const request = https . request ( options , function ( response ) {
95+ console . log ( "Status code: " + response . statusCode ) ;
96+ console . log ( "Status message: " + response . statusMessage ) ;
97+ context . done ( ) ;
98+ } ) ;
99+
100+ request . on ( "error" , function ( error ) {
101+ console . log ( "send(..) failed executing https.request(..): " + error ) ;
102+ context . done ( ) ;
103+ } ) ;
104+
105+ request . write ( responseBody ) ;
106+ request . end ( ) ;
95107}
0 commit comments