async-http 0.33.1 → 0.33.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/fetch/Gemfile +4 -0
- data/examples/fetch/Gemfile.lock +54 -0
- data/examples/fetch/README.md +3 -0
- data/examples/fetch/config.ru +29 -0
- data/examples/fetch/public/index.html +23 -0
- data/examples/fetch/public/stream.js +56 -0
- data/examples/request.rb +38 -0
- data/lib/async/http/body/readable.rb +2 -2
- data/lib/async/http/body/reader.rb +21 -8
- data/lib/async/http/body/streamable.rb +7 -5
- data/lib/async/http/pool.rb +1 -3
- data/lib/async/http/version.rb +1 -1
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2018a5f45e997512850826ead70a696aa7a8cecfff16d4008776f4f11b0d3219
|
4
|
+
data.tar.gz: 9850ab072b612c65c47769eec40ffe12b733b114542fb7be7a7582f0177f9932
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a4b16f09fdf6b9f985cf7bb9bdebce44dd521ff36af1836c756a5a12bcc7ea0240f42432fc4e26da65ce4f5008842c264827bee60ed6ac678f692a306cefd5b
|
7
|
+
data.tar.gz: d4779932899b863951518920756008bb64a16d18536e3db62103c7beb3486e7379ac0d5252fbb579893058a430355151f086635e0e08dd8e59b43a9e4286b824
|
@@ -0,0 +1,54 @@
|
|
1
|
+
GEM
|
2
|
+
specs:
|
3
|
+
async (1.10.3)
|
4
|
+
nio4r (~> 2.3)
|
5
|
+
timers (~> 4.1)
|
6
|
+
async-container (0.6.1)
|
7
|
+
async (~> 1.0)
|
8
|
+
async-io (~> 1.4)
|
9
|
+
async-http (0.33.0)
|
10
|
+
async (~> 1.6)
|
11
|
+
async-io (~> 1.16)
|
12
|
+
http-protocol (~> 0.6.0)
|
13
|
+
async-io (1.16.1)
|
14
|
+
async (~> 1.3)
|
15
|
+
coderay (1.1.2)
|
16
|
+
falcon (0.18.13)
|
17
|
+
async-container (~> 0.6.0)
|
18
|
+
async-http (~> 0.33.0)
|
19
|
+
async-io (~> 1.9)
|
20
|
+
http-protocol (~> 0.6.0)
|
21
|
+
localhost (~> 1.1)
|
22
|
+
rack (>= 1.0)
|
23
|
+
samovar (~> 1.3)
|
24
|
+
hitimes (1.3.0)
|
25
|
+
http-hpack (0.1.1)
|
26
|
+
http-protocol (0.6.0)
|
27
|
+
http-hpack (~> 0.1.0)
|
28
|
+
localhost (1.1.4)
|
29
|
+
mapping (1.1.1)
|
30
|
+
method_source (0.9.0)
|
31
|
+
nio4r (2.3.1)
|
32
|
+
pry (0.11.3)
|
33
|
+
coderay (~> 1.1.0)
|
34
|
+
method_source (~> 0.9.0)
|
35
|
+
rack (2.0.5)
|
36
|
+
rainbow (2.2.2)
|
37
|
+
rake
|
38
|
+
rake (12.3.1)
|
39
|
+
samovar (1.9.0)
|
40
|
+
mapping (~> 1.0)
|
41
|
+
rainbow (~> 2.0)
|
42
|
+
timers (4.1.2)
|
43
|
+
hitimes
|
44
|
+
|
45
|
+
PLATFORMS
|
46
|
+
ruby
|
47
|
+
|
48
|
+
DEPENDENCIES
|
49
|
+
falcon
|
50
|
+
pry
|
51
|
+
rack
|
52
|
+
|
53
|
+
BUNDLED WITH
|
54
|
+
1.16.5
|
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
require 'rack'
|
3
|
+
require 'pry'
|
4
|
+
|
5
|
+
class Echo
|
6
|
+
def initialize(app)
|
7
|
+
@app = app
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
request = Rack::Request.new(env)
|
12
|
+
|
13
|
+
if request.path_info == "/echo"
|
14
|
+
if output = request.body
|
15
|
+
return [200, {}, output.body]
|
16
|
+
else
|
17
|
+
return [200, {}, ["Hello World?"]]
|
18
|
+
end
|
19
|
+
else
|
20
|
+
return @app.call(env)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
use Echo
|
26
|
+
|
27
|
+
use Rack::Static, :urls => [''], :root => 'public', :index => 'index.html'
|
28
|
+
|
29
|
+
run lambda{|env| [404, {}, []]}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Streaming Fetch</title>
|
5
|
+
</head>
|
6
|
+
<body>
|
7
|
+
<h1>Streams</h1>
|
8
|
+
|
9
|
+
<button id="stopButton">Start</button>
|
10
|
+
|
11
|
+
<h2>Sent</h2>
|
12
|
+
|
13
|
+
<ul id="sent">
|
14
|
+
</ul>
|
15
|
+
|
16
|
+
<h2>Received</h2>
|
17
|
+
|
18
|
+
<ul id="received">
|
19
|
+
</ul>
|
20
|
+
|
21
|
+
<script src="stream.js"></script>
|
22
|
+
</body>
|
23
|
+
</html>
|
@@ -0,0 +1,56 @@
|
|
1
|
+
const inputStream = new ReadableStream({
|
2
|
+
start(controller) {
|
3
|
+
interval = setInterval(() => {
|
4
|
+
let string = "Hello World!";
|
5
|
+
|
6
|
+
// Add the string to the stream
|
7
|
+
controller.enqueue(string);
|
8
|
+
|
9
|
+
// show it on the screen
|
10
|
+
let listItem = document.createElement('li');
|
11
|
+
listItem.textContent = string;
|
12
|
+
sent.appendChild(listItem);
|
13
|
+
}, 10000);
|
14
|
+
|
15
|
+
stopButton.addEventListener('click', function() {
|
16
|
+
clearInterval(interval);
|
17
|
+
controller.close();
|
18
|
+
})
|
19
|
+
},
|
20
|
+
pull(controller) {
|
21
|
+
// We don't really need a pull in this example
|
22
|
+
},
|
23
|
+
cancel() {
|
24
|
+
// This is called if the reader cancels,
|
25
|
+
// so we should stop generating strings
|
26
|
+
clearInterval(interval);
|
27
|
+
}
|
28
|
+
});
|
29
|
+
|
30
|
+
fetch("/echo", {method: 'POST', body: inputStream})
|
31
|
+
.then(response => {
|
32
|
+
const reader = response.body.getReader();
|
33
|
+
const decoder = new TextDecoder("utf-8");
|
34
|
+
|
35
|
+
function push() {
|
36
|
+
reader.read().then(({done, value}) => {
|
37
|
+
console.log("done:", done, "value:", value);
|
38
|
+
const string = decoder.decode(value);
|
39
|
+
|
40
|
+
// show it on the screen
|
41
|
+
let listItem = document.createElement('li');
|
42
|
+
|
43
|
+
if (done)
|
44
|
+
listItem.textContent = "<EOF>"
|
45
|
+
else
|
46
|
+
listItem.textContent = string;
|
47
|
+
|
48
|
+
received.appendChild(listItem);
|
49
|
+
|
50
|
+
if (done) return;
|
51
|
+
else push();
|
52
|
+
});
|
53
|
+
};
|
54
|
+
|
55
|
+
push();
|
56
|
+
});
|
data/examples/request.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
|
4
|
+
$LOAD_PATH.unshift(File.expand_path("../../http-protocol/lib", __dir__))
|
5
|
+
|
6
|
+
require 'async'
|
7
|
+
require 'async/logger'
|
8
|
+
require 'async/http/client'
|
9
|
+
require 'async/http/url_endpoint'
|
10
|
+
|
11
|
+
# Async.logger.level = Logger::DEBUG
|
12
|
+
|
13
|
+
Async.run do |task|
|
14
|
+
endpoint = Async::HTTP::URLEndpoint.parse("https://www.google.com")
|
15
|
+
|
16
|
+
client = Async::HTTP::Client.new(endpoint)
|
17
|
+
|
18
|
+
headers = {
|
19
|
+
'accept' => 'text/html',
|
20
|
+
}
|
21
|
+
|
22
|
+
request = Async::HTTP::Request.new("www.google.com", "GET", "/search?q=cats", headers)
|
23
|
+
|
24
|
+
puts "Sending request..."
|
25
|
+
response = client.call(request)
|
26
|
+
|
27
|
+
puts "Reading response status=#{response.status}..."
|
28
|
+
|
29
|
+
if body = response.body
|
30
|
+
while chunk = body.read
|
31
|
+
puts chunk.size
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
response.close
|
36
|
+
|
37
|
+
puts "Finish reading response."
|
38
|
+
end
|
@@ -52,7 +52,7 @@ module Async
|
|
52
52
|
nil
|
53
53
|
end
|
54
54
|
|
55
|
-
# Enumerate all chunks until finished
|
55
|
+
# Enumerate all chunks until finished, then invoke `#close`.
|
56
56
|
def each
|
57
57
|
return to_enum unless block_given?
|
58
58
|
|
@@ -64,7 +64,7 @@ module Async
|
|
64
64
|
self.close($!)
|
65
65
|
end
|
66
66
|
|
67
|
-
# Read all remaining chunks into a single binary string
|
67
|
+
# Read all remaining chunks into a single binary string using `#each`.
|
68
68
|
def join
|
69
69
|
buffer = IO::Buffer.new
|
70
70
|
|
@@ -21,31 +21,44 @@
|
|
21
21
|
module Async
|
22
22
|
module HTTP
|
23
23
|
module Body
|
24
|
+
# General operations for interacting with a request or response body.
|
24
25
|
module Reader
|
25
26
|
# Read chunks from the body.
|
27
|
+
# @yield [String] read chunks from the body.
|
26
28
|
def each(&block)
|
27
|
-
|
29
|
+
if @body
|
30
|
+
@body.each(&block)
|
31
|
+
@body = nil
|
32
|
+
end
|
28
33
|
end
|
29
34
|
|
30
35
|
# Reads the entire request/response body.
|
36
|
+
# @return [String] the entire body as a string.
|
31
37
|
def read
|
32
|
-
if
|
33
|
-
|
38
|
+
if @body
|
39
|
+
buffer = @body.join
|
40
|
+
@body = nil
|
41
|
+
|
42
|
+
return buffer
|
34
43
|
end
|
35
44
|
end
|
36
45
|
|
37
46
|
# Gracefully finish reading the body. This will buffer the remainder of the body.
|
47
|
+
# @return [Buffered] buffers the entire body.
|
38
48
|
def finish
|
39
|
-
if
|
40
|
-
|
49
|
+
if @body
|
50
|
+
body = @body.finish
|
51
|
+
@body = nil
|
52
|
+
|
53
|
+
return body
|
41
54
|
end
|
42
55
|
end
|
43
56
|
|
44
57
|
# Close the connection as quickly as possible. Discards body. May close the underlying connection if necessary to terminate the stream.
|
45
58
|
def close(error = nil)
|
46
|
-
if
|
47
|
-
|
48
|
-
|
59
|
+
if @body
|
60
|
+
@body.close(error)
|
61
|
+
@body = nil
|
49
62
|
end
|
50
63
|
end
|
51
64
|
|
@@ -45,9 +45,13 @@ module Async
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def close(*)
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
if @body
|
49
|
+
super
|
50
|
+
|
51
|
+
@callback.call
|
52
|
+
|
53
|
+
@body = nil
|
54
|
+
end
|
51
55
|
end
|
52
56
|
|
53
57
|
def read
|
@@ -57,8 +61,6 @@ module Async
|
|
57
61
|
if @remaining and @remaining > 0
|
58
62
|
raise EOFError, "Expected #{@remaining} more bytes!"
|
59
63
|
end
|
60
|
-
|
61
|
-
@callback.call
|
62
64
|
end
|
63
65
|
|
64
66
|
return chunk
|
data/lib/async/http/pool.rb
CHANGED
@@ -89,12 +89,10 @@ module Async
|
|
89
89
|
protected
|
90
90
|
|
91
91
|
def availability_string
|
92
|
-
@resources.collect{|resource,usage| "#{usage}/#{resource.multiplex}#{resource.
|
92
|
+
@resources.collect{|resource,usage| "#{usage}/#{resource.multiplex}#{resource.connected? ? '' : '*'}"}.join(";")
|
93
93
|
end
|
94
94
|
|
95
95
|
def reuse(resource)
|
96
|
-
Async.logger.debug(self) {"Reuse #{resource}"}
|
97
|
-
|
98
96
|
@resources[resource] -= 1
|
99
97
|
|
100
98
|
@available.signal
|
data/lib/async/http/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: async-http
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.33.
|
4
|
+
version: 0.33.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: async
|
@@ -136,6 +136,13 @@ files:
|
|
136
136
|
- README.md
|
137
137
|
- Rakefile
|
138
138
|
- async-http.gemspec
|
139
|
+
- examples/fetch/Gemfile
|
140
|
+
- examples/fetch/Gemfile.lock
|
141
|
+
- examples/fetch/README.md
|
142
|
+
- examples/fetch/config.ru
|
143
|
+
- examples/fetch/public/index.html
|
144
|
+
- examples/fetch/public/stream.js
|
145
|
+
- examples/request.rb
|
139
146
|
- lib/async/http.rb
|
140
147
|
- lib/async/http/accept_encoding.rb
|
141
148
|
- lib/async/http/body.rb
|