lamby 0.6.0 → 1.0.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
  SHA256:
3
- metadata.gz: 158acb62cde3cf096b2fa436266c972673610f9ae3a50c453861c68c17fc5354
4
- data.tar.gz: 3222b4c37bf6499526edfa9fb7b9bf6368c2792f2ffac2cc58ff5b60dbe9162a
3
+ metadata.gz: cad37e588b82d16978294d8ae866bdb90f67dbd82f8da8ad04f74c04515043d7
4
+ data.tar.gz: f2301b796dfcfcd32ea21dd8dee9666257887e28022266a9e662c17f2a5df204
5
5
  SHA512:
6
- metadata.gz: 06e5cd72373261f7d6197dd85fda6c620d61183a002c80397be5433c61178c7fe632ef1b9264935c799ed5725caa12cf65880343d03cc091ad1feb51322b1613
7
- data.tar.gz: 48f2f83253a4a44b8334fa0117400ff9d99d7d976dfd1b3847f4e64cb98a4c985f6d0cb52577447fc6dd99c253214969717a9821805a23336dafbb97a018ca5c
6
+ metadata.gz: 0062a78076ce56c00ce31ba1813112f4e1341e24fd104df195131f4a5cde00142490d336ceacb91c6431facfaae3dc115feba6e0e9952c81c6e907f3bd32b3c9
7
+ data.tar.gz: d728410d053304a4b6306972d63e054b1d33b45ca57ca8c9499467902c53990523164661399b1098fbd9b27d9cff1e11518250fee0d03c8bc0130bda6670eda4
@@ -2,6 +2,13 @@
2
2
 
3
3
  See this http://keepachangelog.com link for information on how we want this documented formatted.
4
4
 
5
+ ## v1.0.0
6
+
7
+ #### Fixed
8
+
9
+ * ALB query params & binary responses. Fixes #38.
10
+
11
+
5
12
  ## v0.6.0
6
13
 
7
14
  #### Added
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lamby (0.6.0)
4
+ lamby (1.0.0)
5
5
  rack
6
6
  rails
7
7
 
@@ -87,7 +87,7 @@ GEM
87
87
  minitest (5.11.3)
88
88
  mocha (1.8.0)
89
89
  metaclass (~> 0.0.1)
90
- nio4r (2.3.1)
90
+ nio4r (2.4.0)
91
91
  nokogiri (1.10.3)
92
92
  mini_portile2 (~> 2.4.0)
93
93
  pry (0.12.2)
@@ -4,6 +4,7 @@ require 'base64'
4
4
  require 'active_support/all'
5
5
  require 'lamby/version'
6
6
  require 'lamby/sam_helpers'
7
+ require 'lamby/rack'
7
8
  require 'lamby/rack_alb'
8
9
  require 'lamby/rack_api'
9
10
  require 'lamby/debug'
@@ -5,7 +5,7 @@ module Lamby
5
5
  extend self
6
6
 
7
7
  def on?(event)
8
- params = event['queryStringParameters']
8
+ params = event['multiValueQueryStringParameters'] || event['queryStringParameters']
9
9
  (Rails.env.development? || ENV['LAMBY_DEBUG']) && params && params['debug'] == '1'
10
10
  end
11
11
 
@@ -20,7 +20,7 @@ module Lamby
20
20
  def response
21
21
  { statusCode: status,
22
22
  headers: headers,
23
- body: body }
23
+ body: body }.merge(rack_response)
24
24
  end
25
25
 
26
26
  def status
@@ -55,6 +55,10 @@ module Lamby
55
55
  end
56
56
  end
57
57
 
58
+ def rack_response
59
+ rack.response(self)
60
+ end
61
+
58
62
  def call_app
59
63
  if Debug.on?(@event)
60
64
  Debug.call @event, @context, rack.env
@@ -0,0 +1,85 @@
1
+ module Lamby
2
+ class Rack
3
+
4
+ include SamHelpers
5
+
6
+ LAMBDA_EVENT = 'lambda.event'.freeze
7
+ LAMBDA_CONTEXT = 'lambda.context'.freeze
8
+ HTTP_X_REQUESTID = 'HTTP_X_REQUEST_ID'.freeze
9
+
10
+ attr_reader :event, :context
11
+
12
+ def initialize(event, context)
13
+ @event = event
14
+ @context = context
15
+ end
16
+
17
+ def env
18
+ @env ||= env_base.merge!(env_headers)
19
+ end
20
+
21
+ def response(_handler)
22
+ {}
23
+ end
24
+
25
+ def api?
26
+ false
27
+ end
28
+
29
+ def alb?
30
+ false
31
+ end
32
+
33
+ def multi_value?
34
+ false
35
+ end
36
+
37
+ private
38
+
39
+ def env_base
40
+ raise NotImplementedError
41
+ end
42
+
43
+ def env_headers
44
+ headers.transform_keys do |key|
45
+ "HTTP_#{key.to_s.upcase.tr '-', '_'}"
46
+ end.tap do |hdrs|
47
+ hdrs[HTTP_X_REQUESTID] = request_id
48
+ end
49
+ end
50
+
51
+ def content_type
52
+ headers.delete('Content-Type') || headers.delete('content-type') || headers.delete('CONTENT_TYPE')
53
+ end
54
+
55
+ def content_length
56
+ bytesize = body.bytesize.to_s if body
57
+ headers.delete('Content-Length') || headers.delete('content-length') || headers.delete('CONTENT_LENGTH') || bytesize
58
+ end
59
+
60
+ def body
61
+ @body ||= if event['body'] && base64_encoded?
62
+ Base64.decode64 event['body']
63
+ else
64
+ event['body']
65
+ end
66
+ end
67
+
68
+ def headers
69
+ @headers ||= event['headers'] || {}
70
+ end
71
+
72
+ def query_string
73
+ @query_string ||= event['queryStringParameters'].try(:to_query)
74
+ end
75
+
76
+ def base64_encoded?
77
+ event['isBase64Encoded']
78
+ end
79
+
80
+ def request_id
81
+ context.aws_request_id
82
+ end
83
+
84
+ end
85
+ end
@@ -1,21 +1,24 @@
1
1
  module Lamby
2
- class RackAlb
2
+ class RackAlb < Lamby::Rack
3
3
 
4
- include SamHelpers
5
-
6
- LAMBDA_EVENT = 'lambda.event'.freeze
7
- LAMBDA_CONTEXT = 'lambda.context'.freeze
8
- HTTP_X_REQUESTID = 'HTTP_X_REQUEST_ID'.freeze
9
-
10
- attr_reader :event, :context
4
+ def alb?
5
+ true
6
+ end
11
7
 
12
- def initialize(event, context)
13
- @event = event
14
- @context = context
8
+ def multi_value?
9
+ event.key? 'multiValueHeaders'
15
10
  end
16
11
 
17
- def env
18
- @env ||= env_base.merge!(env_headers)
12
+ def response(handler)
13
+ hhdrs = handler.headers
14
+ multivalue_headers = hhdrs.transform_values { |v| Array.wrap(v) } if multi_value?
15
+ status_description = "#{handler.status} #{::Rack::Utils::HTTP_STATUS_CODES[handler.status]}"
16
+ base64_encode = hhdrs['Content-Transfer-Encoding'] == 'binary' || hhdrs['X-Lamby-Base64'] == '1'
17
+ body = Base64.strict_encode64(handler.body) if base64_encode
18
+ { multiValueHeaders: multivalue_headers,
19
+ statusDescription: status_description,
20
+ isBase64Encoded: base64_encode,
21
+ body: body }.compact
19
22
  end
20
23
 
21
24
  private
@@ -45,45 +48,25 @@ module Lamby
45
48
  end
46
49
  end
47
50
 
48
- def env_headers
49
- headers.transform_keys do |key|
50
- "HTTP_#{key.to_s.upcase.tr '-', '_'}"
51
- end.tap do |hdrs|
52
- hdrs[HTTP_X_REQUESTID] = request_id
53
- end
54
- end
55
-
56
- def content_type
57
- headers.delete('Content-Type') || headers.delete('content-type') || headers.delete('CONTENT_TYPE')
58
- end
59
-
60
- def content_length
61
- bytesize = body.bytesize.to_s if body
62
- headers.delete('Content-Length') || headers.delete('content-length') || headers.delete('CONTENT_LENGTH') || bytesize
51
+ def headers
52
+ @headers ||= multi_value? ? headers_multi : super
63
53
  end
64
54
 
65
- def body
66
- @body ||= if event['body'] && base64_encoded?
67
- Base64.decode64 event['body']
68
- else
69
- event['body']
55
+ def headers_multi
56
+ (event['multiValueHeaders'] || {}).transform_values do |v|
57
+ v.is_a?(Array) ? v.first : v
70
58
  end
71
59
  end
72
60
 
73
- def headers
74
- event['headers'] || {}
75
- end
76
-
77
61
  def query_string
78
- @query_string ||= event['queryStringParameters'].try(:to_query)
79
- end
80
-
81
- def base64_encoded?
82
- event['isBase64Encoded']
62
+ @query_string ||= multi_value? ? query_string_multi : super
83
63
  end
84
64
 
85
- def request_id
86
- context.aws_request_id
65
+ def query_string_multi
66
+ query = event['multiValueQueryStringParameters'] || {}
67
+ string = query.map do |key, value|
68
+ value.map{ |v| "#{key}=#{v}" }.join('&')
69
+ end.flatten.join('&')
87
70
  end
88
71
 
89
72
  end
@@ -1,21 +1,8 @@
1
1
  module Lamby
2
- class RackApi
2
+ class RackApi < Lamby::Rack
3
3
 
4
- include SamHelpers
5
-
6
- LAMBDA_EVENT = 'lambda.event'.freeze
7
- LAMBDA_CONTEXT = 'lambda.context'.freeze
8
- HTTP_X_REQUESTID = 'HTTP_X_REQUEST_ID'.freeze
9
-
10
- attr_reader :event, :context
11
-
12
- def initialize(event, context)
13
- @event = event
14
- @context = context
15
- end
16
-
17
- def env
18
- @env ||= env_base.merge!(env_headers)
4
+ def api?
5
+ true
19
6
  end
20
7
 
21
8
  private
@@ -27,7 +14,7 @@ module Lamby
27
14
  ::Rack::QUERY_STRING => query_string,
28
15
  ::Rack::SERVER_NAME => headers['Host'],
29
16
  ::Rack::SERVER_PORT => headers['X-Forwarded-Port'],
30
- ::Rack::SERVER_PROTOCOL => server_protocol,
17
+ ::Rack::SERVER_PROTOCOL => event.dig('requestContext', 'protocol') || 'HTTP/1.1',
31
18
  ::Rack::RACK_VERSION => ::Rack::VERSION,
32
19
  ::Rack::RACK_URL_SCHEME => 'https',
33
20
  ::Rack::RACK_INPUT => StringIO.new(body || ''),
@@ -45,50 +32,5 @@ module Lamby
45
32
  end
46
33
  end
47
34
 
48
- def env_headers
49
- headers.transform_keys do |key|
50
- "HTTP_#{key.to_s.upcase.tr '-', '_'}"
51
- end.tap do |hdrs|
52
- hdrs[HTTP_X_REQUESTID] = request_id
53
- end
54
- end
55
-
56
- def content_type
57
- headers.delete('Content-Type') || headers.delete('content-type') || headers.delete('CONTENT_TYPE')
58
- end
59
-
60
- def content_length
61
- bytesize = body.bytesize.to_s if body
62
- headers.delete('Content-Length') || headers.delete('content-length') || headers.delete('CONTENT_LENGTH') || bytesize
63
- end
64
-
65
- def body
66
- @body ||= if event['body'] && base64_encoded?
67
- Base64.decode64 event['body']
68
- else
69
- event['body']
70
- end
71
- end
72
-
73
- def headers
74
- event['headers'] || {}
75
- end
76
-
77
- def query_string
78
- @query_string ||= event['queryStringParameters'].try(:to_query)
79
- end
80
-
81
- def base64_encoded?
82
- event['isBase64Encoded']
83
- end
84
-
85
- def server_protocol
86
- event.dig('requestContext', 'protocol') || 'HTTP/1.1'
87
- end
88
-
89
- def request_id
90
- context.aws_request_id
91
- end
92
-
93
35
  end
94
36
  end
@@ -31,6 +31,9 @@ Resources:
31
31
  DependsOn: RailsFunctionInvokePermission
32
32
  Properties:
33
33
  TargetType: lambda
34
+ TargetGroupAttributes:
35
+ - Key: lambda.multi_value_headers.enabled
36
+ Value: true
34
37
  Targets:
35
38
  - Id: !GetAtt RailsFunction.Arn
36
39
 
@@ -79,7 +82,8 @@ Resources:
79
82
  Outputs:
80
83
 
81
84
  RailsLoadBalancerDNSNAme:
82
- Value: !GetAtt RailsLoadBalancer.DNSName
85
+ Description: Application Load Balancer DNS Name
86
+ Value: !GetAtt RailsLoadBalancer.DNSName
83
87
 
84
88
  RailsFunctionArn:
85
89
  Description: Lambda ARN
@@ -1,3 +1,3 @@
1
1
  module Lamby
2
- VERSION = '0.6.0'
2
+ VERSION = '1.0.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lamby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Collins
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-29 00:00:00.000000000 Z
11
+ date: 2019-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -167,6 +167,7 @@ files:
167
167
  - lib/lamby/debug.rb
168
168
  - lib/lamby/handler.rb
169
169
  - lib/lamby/logger.rb
170
+ - lib/lamby/rack.rb
170
171
  - lib/lamby/rack_alb.rb
171
172
  - lib/lamby/rack_api.rb
172
173
  - lib/lamby/railtie.rb