zipstream 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/zipstream-fiber-compat.rb +35 -0
- data/lib/zipstream/body.rb +32 -0
- metadata +5 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# Credit: http://www.khjk.org/log/2010/jun/fibr.html
|
2
|
+
|
3
|
+
class Fiber
|
4
|
+
@@fibers = [] # a stack of fibers corresponding to calls of 'resume'
|
5
|
+
|
6
|
+
def initialize &block
|
7
|
+
@body = lambda &block # lambda makes 'return' work as expected
|
8
|
+
end
|
9
|
+
|
10
|
+
def resume *args
|
11
|
+
@@fibers.push self
|
12
|
+
jump *args # jumping into fiber
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.current
|
16
|
+
@@fibers.last
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.yield *args
|
20
|
+
if fiber = @@fibers.pop
|
21
|
+
fiber.send :jump, args # jumping out of fiber
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def jump *args
|
28
|
+
callcc do |continuation|
|
29
|
+
destination, @body = @body, continuation
|
30
|
+
destination.call *args
|
31
|
+
@@fibers.pop
|
32
|
+
@body.call # return from the last 'resume'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end if RUBY_VERSION < '1.9'
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'zipstream'
|
2
|
+
require 'zipstream-fiber-compat' unless defined? Fiber
|
3
|
+
|
4
|
+
# We use Fibers to deep-yield data being written to the zip stream
|
5
|
+
# to use as a rack response body (responds to #each)
|
6
|
+
class ZipStream::Body
|
7
|
+
def initialize &block
|
8
|
+
@stream = FiberYieldingStream.new
|
9
|
+
@fiber = Fiber.new do
|
10
|
+
zip = ZipStream.new @stream
|
11
|
+
block.call zip
|
12
|
+
zip.close
|
13
|
+
# Make sure this returns nil as a sentinel
|
14
|
+
nil
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def each
|
19
|
+
while !(data = @fiber.resume).nil?
|
20
|
+
yield data
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# A stream that yields each write to the current fiber
|
25
|
+
class FiberYieldingStream
|
26
|
+
def write data
|
27
|
+
tap { Fiber.yield data }
|
28
|
+
end
|
29
|
+
|
30
|
+
alias << write
|
31
|
+
end
|
32
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zipstream
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-11-04 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70230380021020 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 2.7.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70230380021020
|
25
25
|
description:
|
26
26
|
email: sj26@sj26.com
|
27
27
|
executables: []
|
@@ -33,6 +33,8 @@ files:
|
|
33
33
|
- README.md
|
34
34
|
- LICENSE
|
35
35
|
- VERSION
|
36
|
+
- lib/zipstream/body.rb
|
37
|
+
- lib/zipstream-fiber-compat.rb
|
36
38
|
- lib/zipstream.rb
|
37
39
|
homepage: http://github.com/sj26/zipstream
|
38
40
|
licenses: []
|