sumomo 0.7.3 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da1b5b6612475c1c796f2fc3e25d24b941e6a37f
4
- data.tar.gz: b67672d8fb1595aa5c373bf694f675440bbe1839
3
+ metadata.gz: 7e272e7159b46a624aad9e1fd27d42206ff0089d
4
+ data.tar.gz: be5daaba9aa97814db24f976f6e0aa7f32496275
5
5
  SHA512:
6
- metadata.gz: 85a1b46df2f52fb00d462c509094cd1b544f1acc5f3b34f29fd28ceaeaf0d88461fbeaac31af43fe537cf81f51c751b1940597543ed8c9ed7146d42daaaf50f3
7
- data.tar.gz: 9a36dbedaa1bbe0a366625255b44a3bb78ad435ded63669294ee5e8401e135ecdae0dfe2fe63e8fd133187c6421849a40ad9a5c170d5fc936f6f465d2c42ae5c
6
+ metadata.gz: 2b526710d8fb56ca84de1b0f7e7d8979534a82f8b7e8608428a804340d52e80bcf54eb7360488fdf1e33f03125b4ff45f9338319012fed37f5cfd168442387c4
7
+ data.tar.gz: 8c2e126517ea84d32df7b2ee77b99a7c5eba3f4d9fbfb8ac41b2cdf8e5102d952234357b22f943f7744ff0da2269856a8b0ea989071b0bb8f69a55d0e3067682
@@ -80,6 +80,24 @@ function Storage()
80
80
 
81
81
  var Store = new Storage();
82
82
 
83
+ function parseQuery(queryString) {
84
+ try
85
+ {
86
+ var res = JSON.parse(queryString)
87
+ return res;
88
+ }
89
+ catch(e) { }
90
+ var query = {};
91
+ var pairs = queryString.split('&');
92
+ for (var i = 0; i < pairs.length; i++) {
93
+ var pair = pairs[i].split('=');
94
+ query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
95
+ }
96
+ return query;
97
+ }
98
+
99
+ {{ SCRIPT }}
100
+
83
101
  function prepare(handler)
84
102
  {
85
103
  return function(request, callback)
@@ -7,28 +7,52 @@ var http = require('http');
7
7
  var url = require('url');
8
8
  var merge = require('utils-merge');
9
9
  var Router = require('router')
10
+ var fs = require('fs')
10
11
 
11
12
  // Simulated store
12
13
  function Storage()
13
14
  {
14
15
  var store = {}
15
16
 
17
+ try
18
+ {
19
+ var store_contents = fs.readFileSync(".store")
20
+ store = JSON.parse(store_contents)
21
+ }
22
+ catch (e)
23
+ {
24
+ console.log("Error reading .store; will start with blank store")
25
+ console.log(e)
26
+ }
27
+
16
28
  this.get = function(key, onComplete, onError)
17
29
  {
18
30
  if (store[key] === undefined)
19
31
  {
20
- onError({err: "no_such_key"});
32
+ if (onError)
33
+ {
34
+ onError({err: "no_such_key"});
35
+ }
21
36
  }
22
37
  else
23
38
  {
24
- onComplete(store[key]);
39
+ if (onComplete)
40
+ {
41
+ onComplete(store[key]);
42
+ }
25
43
  }
26
44
  }
27
45
 
28
46
  this.set = function(key, value, onComplete, onError)
29
47
  {
30
48
  store[key] = value;
31
- onComplete(key);
49
+ fs.writeFile(".store", JSON.stringify(store), function()
50
+ {
51
+ if (onComplete)
52
+ {
53
+ onComplete(key);
54
+ }
55
+ })
32
56
  }
33
57
 
34
58
  return this;
@@ -56,6 +80,24 @@ var server = http.createServer(function(req, res) {
56
80
  });
57
81
  });
58
82
 
83
+ function parseQuery(queryString) {
84
+ try
85
+ {
86
+ var res = JSON.parse(queryString)
87
+ return res;
88
+ }
89
+ catch(e) { }
90
+ var query = {};
91
+ var pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&');
92
+ for (var i = 0; i < pairs.length; i++) {
93
+ var pair = pairs[i].split('=');
94
+ query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
95
+ }
96
+ return query;
97
+ }
98
+
99
+ {{ SCRIPT }}
100
+
59
101
  // Simulate API Gateway Lambda Proxy Event
60
102
  function prepare(handler)
61
103
  {
@@ -183,7 +183,7 @@ module Sumomo
183
183
  instance_eval(&block)
184
184
  end
185
185
 
186
- def make_api(domain_name, name:, script:nil, dns:nil, cert:nil, &block)
186
+ def make_api(domain_name, name:, script:nil, dns:nil, cert:nil, with_statements: [], &block)
187
187
  @apis[name] = block
188
188
  end
189
189
 
@@ -213,6 +213,7 @@ module Sumomo
213
213
 
214
214
  script = File.read(File.join(Gem.loaded_specs['sumomo'].full_gem_path, "data", "sumomo", "api_modules", "test_script.js"))
215
215
  script.sub!("// {{ ROUTES }}", apigen.generate);
216
+ script.gsub!("{{ SCRIPT }}", apigen.init_script);
216
217
 
217
218
  File.write(".test.js", script)
218
219
 
@@ -3,8 +3,33 @@ module Sumomo
3
3
  module Stack
4
4
 
5
5
  class APIGenerator
6
+
7
+ class CorsInfo
8
+ attr_accessor :allowed_origins, :allowed_headers
9
+
10
+ def apply(&block)
11
+ @allowed_origins = []
12
+ @allowed_headers = ['origin', 'content-type', 'accept', 'cache-control', 'x-requested-with', 'if-modified-since']
13
+ instance_eval(&block) if block
14
+ end
15
+
16
+ def AllowOrigin(value)
17
+ @allowed_origins << value
18
+ end
19
+
20
+ def AllowHeaders(value)
21
+ @allowed_headers << value
22
+ end
23
+ end
24
+
6
25
  def initialize(pretty_print: false, &block)
7
26
  @methods = {}
27
+ @cors = CorsInfo.new
28
+ @script = ""
29
+ # defaults
30
+ @cors.apply do
31
+ AllowOrigin "*"
32
+ end
8
33
  @pretty_print = pretty_print
9
34
  instance_eval(&block)
10
35
  end
@@ -23,6 +48,18 @@ module Sumomo
23
48
  end
24
49
  end
25
50
 
51
+ def CORS(&block)
52
+ @cors.apply(&block)
53
+ end
54
+
55
+ def SCRIPT(value)
56
+ @script = value
57
+ end
58
+
59
+ def init_script
60
+ @script
61
+ end
62
+
26
63
  def generate
27
64
 
28
65
  if @pretty_print
@@ -32,7 +69,30 @@ module Sumomo
32
69
  end
33
70
 
34
71
  result = ""
72
+
73
+ all_methods = @methods.clone
74
+
75
+ # Generate appropriate options methods as well
76
+
35
77
  @methods.each do |path, resource|
78
+ meths = resource.map{|meth,info| meth}
79
+
80
+ # Insert OPTIONS method if there isn't already one
81
+ if !all_methods[path].has_key?("OPTIONS")
82
+ all_methods[path]["OPTIONS"] = { script: <<-SCRIPT, params: [] }
83
+ var headers = {}
84
+ headers["Access-Control-Allow-Origin"] = #{@cors.allowed_origins.join(",").inspect}
85
+ headers["Access-Control-Request-Method"] = '*'
86
+ headers["Access-Control-Allow-Methods"] = '#{meths.join(', ')}'
87
+ headers["Access-Control-Allow-Headers"] = #{@cors.allowed_headers.join(",").inspect}
88
+
89
+ respond_with({methods: #{meths.inspect}}, 200, headers)
90
+ SCRIPT
91
+ end
92
+ end
93
+
94
+
95
+ all_methods.each do |path, resource|
36
96
  resource.each do |method, method_info|
37
97
 
38
98
  parameter_list = method_info[:params].join(", ")
@@ -44,15 +104,28 @@ module Sumomo
44
104
 
45
105
  var retval = {};
46
106
 
107
+ var bodyParameters = parseQuery(event.body);
108
+
47
109
  var params = merge(event.queryStringParameters || {}, event.pathParameters || {});
110
+ params = merge(params, bodyParameters);
48
111
 
49
- function respond_with(response_object, response_status)
112
+ function respond_with(response_object, response_status, response_headers)
50
113
  {
114
+ var headers = {}
115
+ headers["Content-Type"] = "application/json; charset=utf-8"
116
+ headers["Access-Control-Allow-Origin"] = #{@cors.allowed_origins.join(",").inspect}
117
+
118
+ if (response_headers)
119
+ {
120
+ for(var key in response_headers)
121
+ {
122
+ headers[key] = response_headers[key];
123
+ }
124
+ }
125
+
51
126
  var response = {
52
127
  statusCode: response_status || 200,
53
- headers: {
54
- "Content-Type" : "application/json; charset=utf-8"
55
- },
128
+ headers: headers,
56
129
  body: JSON.stringify(response_object#{pretty_print})
57
130
  };
58
131
 
@@ -72,6 +145,7 @@ module Sumomo
72
145
  end
73
146
  end
74
147
 
148
+
75
149
  def make_api(domain_name, name:, script:nil, dns:nil, cert:nil, with_statements:[], &block)
76
150
 
77
151
  api = make "AWS::ApiGateway::RestApi", name: name do
@@ -82,7 +156,7 @@ module Sumomo
82
156
 
83
157
  apigen = APIGenerator.new(&block);
84
158
  script.sub!("// {{ ROUTES }}", apigen.generate);
85
-
159
+ script.gsub!("{{ SCRIPT }}", apigen.init_script);
86
160
  script.gsub!("{{ REGION }}", @region);
87
161
  script.gsub!("{{ BUCKET }}", @bucket_name);
88
162
  script.gsub!("{{ STORE_PREFIX }}", "functions/" + name);
@@ -1,7 +1,7 @@
1
1
 
2
2
  module Sumomo
3
3
  module Stack
4
- def make_cdn_from_dir(domain:, dns:nil, name:nil, dir:, low_ttl: [])
4
+ def make_cdn_from_dir(domain:, cert:nil, dns:nil, name:nil, dir:, low_ttl: [])
5
5
 
6
6
  bucket_name = @bucket_name
7
7
 
@@ -32,6 +32,11 @@ module Sumomo
32
32
  })
33
33
  end
34
34
 
35
+ viewer_policy = "allow-all"
36
+ if cert
37
+ viewer_policy = "redirect-to-https"
38
+ end
39
+
35
40
  cdn = make "AWS::CloudFront::Distribution", name: name do
36
41
  DistributionConfig do
37
42
  Origins [{
@@ -51,7 +56,7 @@ module Sumomo
51
56
  Cookies: { Forward: "none" }
52
57
  },
53
58
  TargetOriginId: "originBucket",
54
- ViewerProtocolPolicy: "allow-all",
59
+ ViewerProtocolPolicy: viewer_policy,
55
60
  DefaultTTL: 60,
56
61
  MaxTTL: 60,
57
62
  MinTTL: 60
@@ -61,12 +66,20 @@ module Sumomo
61
66
  Enabled "true"
62
67
  DefaultRootObject "index.html"
63
68
  Aliases [ domain ]
64
- ViewerCertificate { CloudFrontDefaultCertificate "true" }
69
+
70
+ if cert
71
+ ViewerCertificate {
72
+ AcmCertificateArn cert
73
+ SslSupportMethod "sni-only"
74
+ }
75
+ else
76
+ ViewerCertificate { CloudFrontDefaultCertificate "true" }
77
+ end
65
78
 
66
79
  DefaultCacheBehavior do
67
80
  AllowedMethods ["GET", "HEAD", "OPTIONS"]
68
81
  TargetOriginId "originBucket"
69
- ViewerProtocolPolicy "allow-all"
82
+ ViewerProtocolPolicy viewer_policy
70
83
  ForwardedValues {
71
84
  QueryString "false"
72
85
  Cookies { Forward "none" }
@@ -1,3 +1,3 @@
1
1
  module Sumomo
2
- VERSION = "0.7.3"
2
+ VERSION = "0.8.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sumomo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.3
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Siaw
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-13 00:00:00.000000000 Z
11
+ date: 2018-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler