rack-emstream 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +42 -0
- data/Rakefile +2 -0
- data/lib/rack/emstream.rb +1 -0
- data/lib/rack-emstream/version.rb +5 -0
- data/lib/rack-emstream.rb +33 -0
- data/rack-emstream.gemspec +19 -0
- metadata +71 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 John Bintz
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Super-simple Rack streaming with Thin and other EventMachine-based servers
|
2
|
+
|
3
|
+
This is the absolute simplest way to turn any Rack app into a streaming- and deferrable-capable service using Thin.
|
4
|
+
It handles the necessary async calls to make Thin start streaming, then delivers your
|
5
|
+
response body on each next tick until sent. If you're sending something big, make sure it responds to `each`
|
6
|
+
in chunks:
|
7
|
+
|
8
|
+
``` ruby
|
9
|
+
class FileStreamer
|
10
|
+
def initialize(file)
|
11
|
+
@file = file
|
12
|
+
end
|
13
|
+
|
14
|
+
def each
|
15
|
+
while !@file.eof?
|
16
|
+
yield @file.read(8192)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# then respond with a FileStreamer
|
22
|
+
|
23
|
+
def call(env)
|
24
|
+
# ... do stuff ...
|
25
|
+
|
26
|
+
[ 200, {}, FileStreamer.new(File.open('big-file.mpg')) ]
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
Nothing to configure, just drop it in to your Rack middleware stack and
|
31
|
+
use Thin as your server:
|
32
|
+
|
33
|
+
``` ruby
|
34
|
+
# for Rails:
|
35
|
+
|
36
|
+
config.middleware.insert_before(::Rack::Lock, ::Rack::EMStream)
|
37
|
+
|
38
|
+
# for Rack::Builder and derivatives:
|
39
|
+
|
40
|
+
use Rack::EMStream
|
41
|
+
```
|
42
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rack-emstream'
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'eventmachine'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class EMStream
|
5
|
+
include EventMachine::Deferrable
|
6
|
+
|
7
|
+
def initialize(app)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def each(&b)
|
12
|
+
@callback = b
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
dup._call(env)
|
17
|
+
end
|
18
|
+
|
19
|
+
def _call(env)
|
20
|
+
result = @app.call(env)
|
21
|
+
|
22
|
+
EM.next_tick {
|
23
|
+
env['async.callback'].call [ result[0], result[1], self ]
|
24
|
+
|
25
|
+
result[2].each { |data| EM.next_tick { @callback.call(data) } }
|
26
|
+
EM.next_tick { succeed }
|
27
|
+
}
|
28
|
+
|
29
|
+
throw :async
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rack-emstream/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["John Bintz"]
|
6
|
+
gem.email = ["john@coswellproductions.com"]
|
7
|
+
gem.description = %q{Super-simple Rack streaming with Thin}
|
8
|
+
gem.summary = %q{Super-simple Rack streaming with Thin}
|
9
|
+
gem.homepage = "http://github.com/johnbintz/rack-emstream/"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "rack-emstream"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Rack::EMStream::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'eventmachine'
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-emstream
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- John Bintz
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: eventmachine
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Super-simple Rack streaming with Thin
|
31
|
+
email:
|
32
|
+
- john@coswellproductions.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/rack-emstream.rb
|
43
|
+
- lib/rack-emstream/version.rb
|
44
|
+
- lib/rack/emstream.rb
|
45
|
+
- rack-emstream.gemspec
|
46
|
+
homepage: http://github.com/johnbintz/rack-emstream/
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.23
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Super-simple Rack streaming with Thin
|
70
|
+
test_files: []
|
71
|
+
has_rdoc:
|