exe/faastruby-server in faastruby-0.4.4 vs exe/faastruby-server in faastruby-0.4.5

- old
+ new

@@ -3,10 +3,11 @@ require 'sinatra' require 'sinatra/multi_route' require 'yaml' require 'oj' require 'faastruby-rpc' +require 'base64' module FaaStRuby class DoubleRenderError < StandardError; end end @@ -39,19 +40,35 @@ end def rendered? @rendered end - def respond_with(body, status: 200, headers: {}) + def respond_with(body, status: 200, headers: {}, binary: false) raise FaaStRuby::DoubleRenderError.new("You called 'render' or 'respond_with' twice in your handler method") if rendered? - response = FaaStRuby::Response.new(body: body, status: status, headers: headers) + response = FaaStRuby::Response.new(body: body, status: status, headers: headers, binary: binary) rendered! response end - def render(js: nil, body: nil, inline: nil, html: nil, json: nil, yaml: nil, text: nil, status: 200, headers: {}, content_type: nil) + def render( + js: nil, + body: nil, + inline: nil, + html: nil, + json: nil, + yaml: nil, + text: nil, + data: nil, + png: nil, + svg: nil, + jpeg: nil, + gif: nil, + icon: nil, + status: 200, headers: {}, content_type: nil, binary: false + ) headers["Content-Type"] = content_type if content_type + bin = false case when json headers["Content-Type"] ||= "application/json" resp_body = json.is_a?(String) ? json : Oj.dump(json) when html, inline @@ -63,29 +80,58 @@ when yaml headers["Content-Type"] ||= "application/yaml" resp_body = yaml.is_a?(String) ? yaml : YAML.load(yaml) when body headers["Content-Type"] ||= "application/octet-stream" - resp_body = raw + bin = binary + resp_body = bin ? Base64.urlsafe_encode64(body) : body + when data + headers["Content-Type"] ||= "application/octet-stream" + resp_body = Base64.urlsafe_encode64(data) + bin = true when js headers["Content-Type"] ||= "text/javascript" resp_body = js + when png + headers["Content-Type"] ||= "image/png" + resp_body = Base64.urlsafe_encode64(png) + bin = true + when svg + headers["Content-Type"] ||= "image/svg+xml" + resp_body = svg + when jpeg + headers["Content-Type"] ||= "image/jpeg" + resp_body = Base64.urlsafe_encode64(jpeg) + bin = true + when gif + headers["Content-Type"] ||= "image/gif" + resp_body = Base64.urlsafe_encode64(gif) + bin = true + when icon + headers["Content-Type"] ||= "image/x-icon" + resp_body = Base64.urlsafe_encode64(icon) + bin = true end - respond_with(resp_body, status: status, headers: headers) + respond_with(resp_body, status: status, headers: headers, binary: bin) end end class Event < Struct end class Response - attr_accessor :body, :status, :headers - def initialize(body:, status: 200, headers: {}) + attr_accessor :body, :status, :headers, :binary + def initialize(body:, status: 200, headers: {}, binary: false) @body = body @status = status @headers = headers + @binary = binary end + + def binary? + @binary + end end end class FaaStRubyServer < Sinatra::Application set :port, 3000 @@ -115,10 +161,14 @@ context = set_context(params[:workspace_name], params[:function_name]) event = e.new(body, query_params, headers, context) response = FaaStRuby::Runner.new.call(params[:workspace_name], params[:function_name], event, rpc_args) status response.status headers response.headers - body response.body + if response.binary? + body Base64.urlsafe_decode64(response.body) + else + body response.body + end end def parse_body(body, content_type, method) return nil if method == 'GET' return {} if body.nil? && method != 'GET'