Skip to content

Commit

Permalink
remove EventSource
Browse files Browse the repository at this point in the history
  • Loading branch information
dsh0416 committed Feb 10, 2024
1 parent a8214ca commit 847094a
Show file tree
Hide file tree
Showing 15 changed files with 17 additions and 113 deletions.
1 change: 0 additions & 1 deletion lib/inori.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
require_relative 'inori/route'
require_relative 'inori/sandbox'
require_relative 'inori/websocket'
require_relative 'inori/eventsource'
require_relative 'inori/middleware'
require_relative 'inori/configure'
require_relative 'inori/runner'
Expand Down
4 changes: 1 addition & 3 deletions lib/inori/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def helper(name, &block)
unsubscribe
patch
purge
websocket
eventsource].freeze
websocket].freeze

# Magics to fill DSL methods through dynamically class method definition
METHODS.each do |method|
Expand All @@ -123,7 +122,6 @@ def helper(name, &block)
end

singleton_class.send :alias_method, :ws, :websocket
singleton_class.send :alias_method, :es, :eventsource

private

Expand Down
7 changes: 0 additions & 7 deletions lib/inori/api_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ def receive(request, connection = nil)
connection.websocket.request = request
Inori::Sandbox.run(clean_room, route.function, connection.websocket)
return Inori::Response.new
elsif request.eventsource?
connection.send_data Inori::Response.new(
status: 200,
header: Inori::Const::EVENTSOURCE_HEADER
)
Inori::Sandbox.run(clean_room, route.function, connection.eventsource)
return Inori::Response.new
else
request = middleware_exec(route.middlewares, clean_room, request)
return request if request.is_a? Inori::Response # Early stop
Expand Down
10 changes: 0 additions & 10 deletions lib/inori/api_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,5 @@ def unlink(path, &block) end
# puts 'Hello World'
# end
def websocket(path, &block) end

# Add EVENTSOURCE method as a DSL for route definition
# @param [String] path Accepts as part of path in route definition
# @yield what to run when route matched
# @return [nil] nil
# @example String as router
# eventsource '/' do
# puts 'Hello World'
# end
def eventsource(path, &block) end
end
end
7 changes: 0 additions & 7 deletions lib/inori/const.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,6 @@ module Const
'Server' => "Inori/#{Inori::VERSION}"
}.freeze

# Default header for EventSource response
EVENTSOURCE_HEADER = {
'Content-Type' => 'text-event-stream',
'Cache-Control' => 'no-cache',
'Connection' => 'keep-alive'
}.freeze

# Default header for Websocket response
WEBSOCKET_HEADER = {
'Upgrade' => 'websocket',
Expand Down
2 changes: 1 addition & 1 deletion lib/inori/core_ext/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Configurable
# @param [Object] value value to the name
# @param [Boolean] read_only Generate option= method or not
# @return [nil] nil
def set(option, value = (not_set = true), read_only = false)
def set(option, value = (not_set = true), read_only: false)
raise ArgumentError if not_set

setter = proc { |val| set option, val }
Expand Down
26 changes: 0 additions & 26 deletions lib/inori/eventsource.rb

This file was deleted.

2 changes: 0 additions & 2 deletions lib/inori/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class NotMasked < StandardError; end
class FrameEnd < StandardError; end
# Websocket Ping Pong size too large
class PingPongSizeTooLarge < StandardError; end
# Not sending String in EventSource
class EventSourceTypeError < StandardError; end
# Insert a not middleware class to middleware list
class MiddlewareError < StandardError; end
end
Expand Down
14 changes: 0 additions & 14 deletions lib/inori/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def initialize
@parsed = false
@body_parsed = false
@is_websocket = false
@is_eventsource = false
@ip = ''
@port = 0
@parser = Mizu::Parser.new
Expand Down Expand Up @@ -63,7 +62,6 @@ def reset!
@parsed = false
@body_parsed = false
@is_websocket = false
@is_eventsource = false
@parser.reset!
@params = {}
@query_params = Hash.new([])
Expand Down Expand Up @@ -119,12 +117,6 @@ def pre_proceed
@is_websocket = true
end

# Deal with EventSource
if @header['Accept'] == 'text/event-stream'
@method = :EVENTSOURCE
@is_eventsource = true
end

@method = @method.to_sym
nil
end
Expand All @@ -146,11 +138,5 @@ def body_parsed?
def websocket?
@is_websocket
end

# Syntactic sugar for whether a request is an eventsource request
# @return [Boolean] eventsource or not
def eventsource?
@is_eventsource
end
end
end
21 changes: 8 additions & 13 deletions lib/inori/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def initialize(api)
Inori.logger = configure.logger
@bind = configure.bind
@port = configure.port
@api = (api.is_a? Inori::APIEngine) ? api : Inori::APIEngine.new(api, configure.route_type)
@api = api.is_a? Inori::APIEngine ? api : Inori::APIEngine.new(api, configure.route_type)
@before = configure.before
end

Expand All @@ -33,22 +33,15 @@ def running?
def start
return false if running?

@logger.info "Inori #{Inori::VERSION} is now running on #{bind}:#{port}".blue
init_socket
@logger.info 'Inori is serving...'.blue
Fiber.schedule do
@logger.info 'Inori is booting...'.blue
@before.call
@logger.info 'Inori is serving...'.blue
Fiber.schedule do
loop do
socket = @server.accept
connection = Inori::Connection.new(socket)
connection.server_initialize(@api, @logger)
connection.listen
end
loop do
conn = Inori::Connection.new(@server.accept)
conn.server_initialize(@api, @logger)
conn.listen
end
end
nil
end

# Stop the Inori server
Expand All @@ -70,6 +63,8 @@ def stop
private

def init_socket
@logger.info "Inori #{Inori::VERSION} is now running on #{bind}:#{port}".blue

@server = Socket.new Socket::AF_INET, Socket::SOCK_STREAM
@server.reuse_port if Inori::Configure.socket_reuse_port
@server.bind Addrinfo.tcp @bind, @port
Expand Down
7 changes: 2 additions & 5 deletions lib/inori/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ module Server
# @return [Class] inherited from Inori::API
# @!attribute websocket
# @return [Inori::Websocket] defined websocket instance
# @!attribute eventsource
# @return [Inori::EventSource] defined eventsource instance
attr_accessor :request, :api, :websocket, :eventsource
attr_accessor :request, :api, :websocket

# Define server behaviour
# @param [Class] api inherited from Inori::API
Expand All @@ -23,7 +21,6 @@ def server_initialize(api, logger)

@request = Inori::Request.new
@websocket = Inori::Websocket.new(self)
@eventsource = Inori::EventSource.new(self)

# Add keep-alive parameters
@keep_alive_timer = nil
Expand Down Expand Up @@ -75,7 +72,7 @@ def receive_new_request
@logger.warn e.backtrace.join("\n").yellow
end

return if @request.websocket? || @request.eventsource?
return if @request.websocket?

send_data @response
proceed_keep_alive
Expand Down
5 changes: 3 additions & 2 deletions spec/inori/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class RawHello < Inori::API
get '/' do
'Hello'
end

# rubocop:disable Lint/EmptyBlock
post '/' do
end
put '/' do
Expand All @@ -31,8 +33,7 @@ class RawHello < Inori::API
end
websocket '/' do
end
eventsource '/' do
end
# rubocop:enable Lint/EmptyBlock
end

class JSONMiddleware < Inori::Middleware
Expand Down
6 changes: 2 additions & 4 deletions spec/inori/fixtures/example_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class ExampleAPI < Inori::API
end

ws.on :close do
# Do nothing
end
end

Expand Down Expand Up @@ -110,10 +111,7 @@ class ExampleAPI < Inori::API
end

websocket '/websocket/wrong_opcode' do |ws|
end

eventsource '/eventsource' do |es|
es.send("Hello\nWorld")
# Placeholder
end
end

Expand Down
7 changes: 0 additions & 7 deletions spec/inori/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,6 @@
expect(request.method).to eq(:WEBSOCKET)
end

it 'parse eventsource upgrade' do
data = "GET / HTTP/1.1\r\nAccept: text/event-stream\r\n\r\n"
request = described_class.new
request.parse(data)
expect(request.method).to eq(:EVENTSOURCE)
end

it 'parses all methods' do
methods = %w[ delete
get
Expand Down
11 changes: 0 additions & 11 deletions spec/inori/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,4 @@
end
end
end

# TODO: Reimplement the EventSource test
# describe 'EventSource' do
# it 'should pass Hello World test' do
# Timeout::timeout(1) do
# expect(Typhoeus.get("http://127.0.0.1:8080/eventsource", headers: {
# 'Accept' => 'text/event-stream'
# }).body).to eq("data: Hello\ndata: World\n\n")
# end
# end
# end
end

0 comments on commit 847094a

Please sign in to comment.