dassets 0.14.3 → 0.15.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -7
- data/Gemfile +5 -1
- data/README.md +15 -17
- data/dassets.gemspec +14 -9
- data/lib/dassets.rb +51 -13
- data/lib/dassets/asset_file.rb +27 -24
- data/lib/dassets/cache.rb +27 -33
- data/lib/dassets/config.rb +55 -50
- data/lib/dassets/engine.rb +11 -23
- data/lib/dassets/file_store.rb +27 -28
- data/lib/dassets/server.rb +27 -27
- data/lib/dassets/server/request.rb +44 -41
- data/lib/dassets/server/response.rb +101 -80
- data/lib/dassets/source.rb +24 -9
- data/lib/dassets/source_file.rb +110 -81
- data/lib/dassets/source_proxy.rb +36 -20
- data/lib/dassets/version.rb +3 -1
- data/test/helper.rb +31 -25
- data/test/support/app.rb +5 -5
- data/test/support/empty/{.gitkeep → .keep} +0 -0
- data/test/support/factory.rb +3 -2
- data/test/support/linked_source_files/linked_file.txt +0 -0
- data/test/support/source_files/linked +1 -0
- data/test/support/source_files/linked_file2.txt +1 -0
- data/test/system/rack_tests.rb +65 -61
- data/test/unit/asset_file_tests.rb +68 -60
- data/test/unit/cache_tests.rb +15 -34
- data/test/unit/config_tests.rb +58 -51
- data/test/unit/dassets_tests.rb +31 -24
- data/test/unit/engine_tests.rb +9 -43
- data/test/unit/file_store_tests.rb +34 -24
- data/test/unit/server/request_tests.rb +57 -59
- data/test/unit/server/response_tests.rb +82 -82
- data/test/unit/server_tests.rb +5 -9
- data/test/unit/source_file_tests.rb +86 -74
- data/test/unit/source_proxy_tests.rb +84 -90
- data/test/unit/source_tests.rb +89 -50
- data/tmp/.gitkeep +0 -0
- metadata +92 -64
- data/.gitignore +0 -19
data/lib/dassets/engine.rb
CHANGED
@@ -1,31 +1,19 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
module Dassets; end
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
def initialize(opts=nil)
|
8
|
-
@opts = opts || {}
|
9
|
-
end
|
10
|
-
|
11
|
-
def ext(input_ext)
|
12
|
-
raise NotImplementedError
|
13
|
-
end
|
14
|
-
|
15
|
-
def compile(input)
|
16
|
-
raise NotImplementedError
|
17
|
-
end
|
5
|
+
class Dassets::Engine
|
6
|
+
attr_reader :opts
|
18
7
|
|
8
|
+
def initialize(opts = nil)
|
9
|
+
@opts = opts || {}
|
19
10
|
end
|
20
11
|
|
21
|
-
|
22
|
-
|
23
|
-
input_ext
|
24
|
-
end
|
25
|
-
|
26
|
-
def compile(input)
|
27
|
-
input
|
28
|
-
end
|
12
|
+
def ext(input_ext)
|
13
|
+
raise NotImplementedError
|
29
14
|
end
|
30
15
|
|
16
|
+
def compile(input)
|
17
|
+
raise NotImplementedError
|
18
|
+
end
|
31
19
|
end
|
data/lib/dassets/file_store.rb
CHANGED
@@ -1,39 +1,38 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require "thread"
|
4
4
|
|
5
|
-
|
6
|
-
attr_reader :root
|
5
|
+
module Dassets; end
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
@save_mutex = ::Mutex.new
|
11
|
-
end
|
12
|
-
|
13
|
-
def save(url_path, &block)
|
14
|
-
@save_mutex.synchronize do
|
15
|
-
store_path(url_path).tap do |path|
|
16
|
-
FileUtils.mkdir_p(File.dirname(path))
|
17
|
-
File.open(path, "w"){ |f| f.write(block.call) }
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
7
|
+
class Dassets::FileStore
|
8
|
+
attr_reader :root
|
21
9
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
class NullStore < FileStore
|
27
|
-
def initialize
|
28
|
-
super('')
|
29
|
-
end
|
10
|
+
def initialize(root)
|
11
|
+
@root = root
|
12
|
+
@save_mutex = ::Mutex.new
|
13
|
+
end
|
30
14
|
|
31
|
-
|
32
|
-
|
33
|
-
|
15
|
+
def save(url_path, &block)
|
16
|
+
@save_mutex.synchronize do
|
17
|
+
store_path(url_path).tap do |path|
|
18
|
+
FileUtils.mkdir_p(File.dirname(path))
|
19
|
+
File.open(path, "w"){ |f| f.write(block.call) }
|
34
20
|
end
|
35
21
|
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def store_path(url_path)
|
25
|
+
File.join(@root, url_path)
|
26
|
+
end
|
27
|
+
end
|
36
28
|
|
29
|
+
class Dassets::NullFileStore < Dassets::FileStore
|
30
|
+
def initialize
|
31
|
+
super("")
|
37
32
|
end
|
38
33
|
|
34
|
+
def save(url_path)
|
35
|
+
# No-op, just return the store path like the base does.
|
36
|
+
store_path(url_path)
|
37
|
+
end
|
39
38
|
end
|
data/lib/dassets/server.rb
CHANGED
@@ -1,37 +1,37 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "dassets/server/request"
|
4
|
+
require "dassets/server/response"
|
3
5
|
|
4
6
|
# Rack middleware for serving Dassets asset files
|
5
7
|
|
6
|
-
module Dassets
|
7
|
-
class Server
|
8
|
+
module Dassets; end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
class Dassets::Server
|
11
|
+
def initialize(app)
|
12
|
+
@app = app
|
13
|
+
end
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
15
|
+
# The Rack call interface. The receiver acts as a prototype and runs
|
16
|
+
# each request in a clone object unless the +rack.run_once+ variable is
|
17
|
+
# set in the environment. Ripped from:
|
18
|
+
# http://github.com/rtomayko/rack-cache/blob/master/lib/rack/cache/context.rb
|
19
|
+
def call(env)
|
20
|
+
if env["rack.run_once"]
|
21
|
+
call! env
|
22
|
+
else
|
23
|
+
clone.call! env
|
23
24
|
end
|
25
|
+
end
|
24
26
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
27
|
+
# The real Rack call interface.
|
28
|
+
# if an asset file is being requested, this is an endpoint - otherwise, call
|
29
|
+
# on up to the app as normal
|
30
|
+
def call!(env)
|
31
|
+
if (request = Request.new(env)).for_asset_file?
|
32
|
+
Response.new(env, request.asset_file).to_rack
|
33
|
+
else
|
34
|
+
@app.call(env)
|
34
35
|
end
|
35
|
-
|
36
36
|
end
|
37
37
|
end
|
@@ -1,56 +1,59 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
class Dassets::Server
|
5
|
-
|
6
|
-
class Request < Rack::Request
|
3
|
+
require "rack"
|
7
4
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
5
|
+
module Dassets; end
|
6
|
+
class Dassets::Server; end
|
7
|
+
|
8
|
+
class Dassets::Server::Request < Rack::Request
|
9
|
+
# The HTTP request method. This is the standard implementation of this
|
10
|
+
# method but is respecified here due to libraries that attempt to modify
|
11
|
+
# the behavior to respect POST tunnel method specifiers. We always want
|
12
|
+
# the real request method.
|
13
|
+
def request_method
|
14
|
+
@env["REQUEST_METHOD"]
|
15
|
+
end
|
13
16
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
def path_info
|
18
|
+
@env["PATH_INFO"].sub(dassets_base_url, "")
|
19
|
+
end
|
17
20
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
+
def dassets_base_url
|
22
|
+
Dassets.config.base_url.to_s
|
23
|
+
end
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
# Determine if the request is for an asset file
|
26
|
+
# This will be called on every request so speed is an issue
|
27
|
+
# - first check if the request is a GET or HEAD (fast)
|
28
|
+
# - then check if for a digested asset resource (kinda fast)
|
29
|
+
# - then check if source exists for the digested asset (slower)
|
30
|
+
def for_asset_file?
|
31
|
+
!!((get? || head?) && for_digested_asset? && asset_file.exists?)
|
32
|
+
end
|
30
33
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
+
def asset_path
|
35
|
+
@asset_path ||= path_digest_match.captures.select{ |m| !m.empty? }.join
|
36
|
+
end
|
34
37
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
+
def asset_file
|
39
|
+
@asset_file ||= Dassets.asset_file(asset_path)
|
40
|
+
end
|
38
41
|
|
39
|
-
|
42
|
+
private
|
40
43
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
+
def for_digested_asset?
|
45
|
+
!path_digest_match.captures.empty?
|
46
|
+
end
|
44
47
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
48
|
+
def path_digest_match
|
49
|
+
@path_digest_match ||= begin
|
50
|
+
path_info.match(%r{/(.+)-[a-f0-9]{32}(\..+|)$}i) || NullDigestMatch.new
|
49
51
|
end
|
52
|
+
end
|
50
53
|
|
51
|
-
|
52
|
-
|
54
|
+
class NullDigestMatch
|
55
|
+
def captures
|
56
|
+
[]
|
53
57
|
end
|
54
|
-
|
55
58
|
end
|
56
59
|
end
|
@@ -1,108 +1,129 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rack/response"
|
4
|
+
require "rack/utils"
|
5
|
+
require "rack/mime"
|
4
6
|
|
5
7
|
module Dassets; end
|
6
|
-
class Dassets::Server
|
8
|
+
class Dassets::Server; end
|
9
|
+
|
10
|
+
class Dassets::Server::Response
|
11
|
+
attr_reader :asset_file, :status, :headers, :body
|
12
|
+
|
13
|
+
def initialize(env, asset_file)
|
14
|
+
@asset_file = asset_file
|
15
|
+
|
16
|
+
mtime = @asset_file.mtime.to_s
|
17
|
+
@status, @headers, @body = if env["HTTP_IF_MODIFIED_SINCE"] == mtime
|
18
|
+
[
|
19
|
+
304,
|
20
|
+
Rack::Utils::HeaderHash.new("Last-Modified" => mtime),
|
21
|
+
[],
|
22
|
+
]
|
23
|
+
elsif !@asset_file.exists?
|
24
|
+
[
|
25
|
+
404,
|
26
|
+
Rack::Utils::HeaderHash.new,
|
27
|
+
["Not Found"],
|
28
|
+
]
|
29
|
+
else
|
30
|
+
@asset_file.digest!
|
31
|
+
body = Body.new(env, @asset_file)
|
32
|
+
[
|
33
|
+
body.partial? ? 206 : 200,
|
34
|
+
Rack::Utils::HeaderHash
|
35
|
+
.new
|
36
|
+
.merge(@asset_file.response_headers).tap do |h|
|
37
|
+
h["Last-Modified"] = mtime.to_s
|
38
|
+
h["Content-Type"] = @asset_file.mime_type.to_s
|
39
|
+
h["Content-Length"] = body.size.to_s
|
40
|
+
h["Content-Range"] = body.content_range if body.partial?
|
41
|
+
end,
|
42
|
+
env["REQUEST_METHOD"] == "HEAD" ? [] : body,
|
43
|
+
]
|
44
|
+
end
|
45
|
+
end
|
7
46
|
|
8
|
-
|
47
|
+
def to_rack
|
48
|
+
[@status, @headers.to_hash, @body]
|
49
|
+
end
|
50
|
+
|
51
|
+
# This class borrows from the body range handling in Rack::File and adapts
|
52
|
+
# it for use with Dasset's asset files and their generic string content.
|
53
|
+
class Body
|
54
|
+
CHUNK_SIZE = (8 * 1024) # 8k
|
9
55
|
|
10
|
-
attr_reader :asset_file, :
|
56
|
+
attr_reader :asset_file, :size, :content_range
|
11
57
|
|
12
58
|
def initialize(env, asset_file)
|
13
59
|
@asset_file = asset_file
|
14
|
-
|
15
|
-
|
16
|
-
@status, @headers, @body = if env['HTTP_IF_MODIFIED_SINCE'] == mtime
|
17
|
-
[ 304, Rack::Utils::HeaderHash.new('Last-Modified' => mtime), [] ]
|
18
|
-
elsif !@asset_file.exists?
|
19
|
-
[ 404, Rack::Utils::HeaderHash.new, ["Not Found"] ]
|
20
|
-
else
|
21
|
-
@asset_file.digest!
|
22
|
-
body = Body.new(env, @asset_file)
|
23
|
-
[ body.partial? ? 206 : 200,
|
24
|
-
Rack::Utils::HeaderHash.new.merge(@asset_file.response_headers).tap do |h|
|
25
|
-
h['Last-Modified'] = mtime
|
26
|
-
h['Content-Type'] = @asset_file.mime_type.to_s
|
27
|
-
h['Content-Length'] = body.size.to_s
|
28
|
-
h['Content-Range'] = body.content_range if body.partial?
|
29
|
-
end,
|
30
|
-
env["REQUEST_METHOD"] == "HEAD" ? [] : body
|
31
|
-
]
|
32
|
-
end
|
60
|
+
@range, @content_range = get_range_info(env, @asset_file)
|
61
|
+
@size = range_end - range_begin + 1
|
33
62
|
end
|
34
63
|
|
35
|
-
def
|
36
|
-
|
64
|
+
def partial?
|
65
|
+
!@content_range.nil?
|
37
66
|
end
|
38
67
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
# it for use with Dasset's asset files and their generic string content.
|
43
|
-
|
44
|
-
CHUNK_SIZE = (8*1024).freeze # 8k
|
45
|
-
|
46
|
-
attr_reader :asset_file, :size, :content_range
|
47
|
-
|
48
|
-
def initialize(env, asset_file)
|
49
|
-
@asset_file = asset_file
|
50
|
-
@range, @content_range = get_range_info(env, @asset_file)
|
51
|
-
@size = self.range_end - self.range_begin + 1
|
52
|
-
end
|
53
|
-
|
54
|
-
def partial?
|
55
|
-
!@content_range.nil?
|
56
|
-
end
|
68
|
+
def range_begin
|
69
|
+
@range.begin
|
70
|
+
end
|
57
71
|
|
58
|
-
|
59
|
-
|
72
|
+
def range_end
|
73
|
+
@range.end
|
74
|
+
end
|
60
75
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
76
|
+
def each
|
77
|
+
StringIO.open(@asset_file.content, "rb") do |io|
|
78
|
+
io.seek(@range.begin)
|
79
|
+
remaining_len = size
|
80
|
+
while remaining_len > 0
|
81
|
+
part = io.read([CHUNK_SIZE, remaining_len].min)
|
82
|
+
break if part.nil?
|
68
83
|
|
69
|
-
|
70
|
-
|
71
|
-
end
|
84
|
+
remaining_len -= part.length
|
85
|
+
yield part
|
72
86
|
end
|
73
87
|
end
|
88
|
+
end
|
74
89
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
90
|
+
def inspect
|
91
|
+
"#<#{self.class}:#{"0x0%x" % (object_id << 1)} " \
|
92
|
+
"digest_path=#{asset_file.digest_path} " \
|
93
|
+
"range_begin=#{range_begin} range_end=#{range_end}>"
|
94
|
+
end
|
80
95
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
96
|
+
def ==(other)
|
97
|
+
if other.is_a?(self.class)
|
98
|
+
asset_file == other.asset_file &&
|
99
|
+
range_begin == other.range_begin &&
|
100
|
+
range_end == other.range_end
|
101
|
+
else
|
102
|
+
super
|
85
103
|
end
|
104
|
+
end
|
86
105
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
# No ranges or multiple ranges are not supported, just return full size
|
95
|
-
return full_size_range_info(content_size) if ranges.nil? || ranges.empty? || ranges.length > 1
|
96
|
-
# single range
|
97
|
-
[ranges[0], "bytes #{ranges[0].begin}-#{ranges[0].end}/#{content_size}"]
|
106
|
+
private
|
107
|
+
|
108
|
+
def get_range_info(env, asset_file)
|
109
|
+
content_size = asset_file.size
|
110
|
+
# legacy rack version, just return full size
|
111
|
+
unless Rack::Utils.respond_to?(:byte_ranges)
|
112
|
+
return full_size_range_info(content_size)
|
98
113
|
end
|
99
114
|
|
100
|
-
|
101
|
-
|
115
|
+
ranges = Rack::Utils.byte_ranges(env, content_size)
|
116
|
+
# No ranges or multiple ranges are not supported, just return full size
|
117
|
+
if ranges.nil? || ranges.empty? || ranges.length > 1
|
118
|
+
return full_size_range_info(content_size)
|
102
119
|
end
|
103
120
|
|
121
|
+
# single range
|
122
|
+
[ranges[0], "bytes #{ranges[0].begin}-#{ranges[0].end}/#{content_size}"]
|
104
123
|
end
|
105
124
|
|
125
|
+
def full_size_range_info(content_size)
|
126
|
+
[(0..content_size - 1), nil]
|
127
|
+
end
|
106
128
|
end
|
107
|
-
|
108
129
|
end
|