Class: Opal::SourceMapServer

Inherits:
Object
  • Object
show all
Defined in:
opal/lib/opal/sprockets/source_map_server.rb

Defined Under Namespace

Classes: Cache

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sprockets, prefix = '/') ⇒ SourceMapServer

Returns a new instance of SourceMapServer



69
70
71
72
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 69

def initialize sprockets, prefix = '/'
  @sprockets = sprockets
  @prefix = prefix
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix



74
75
76
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 74

def prefix
  @prefix
end

#sprocketsObject (readonly)

Returns the value of attribute sprockets



74
75
76
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 74

def sprockets
  @sprockets
end

Class Method Details

.cache(sprockets) ⇒ Object



59
60
61
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 59

def self.cache(sprockets)
  @cache ||= sprockets.cache ? sprockets : Cache.new
end

.cache_key_for_path(logical_path) ⇒ Object



63
64
65
66
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 63

def self.cache_key_for_path(logical_path)
  base_name = logical_path.gsub(/\.js#{REGEXP_END}/, '')
  File.join('opal', 'source_maps', base_name)
end

.get_map_cache(sprockets, logical_path) ⇒ Object



47
48
49
50
51
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 47

def self.get_map_cache(sprockets, logical_path)
  logical_path = logical_path.gsub(/\.js#{REGEXP_END}/, '')
  cache_key = cache_key_for_path(logical_path+'.map')
  cache(sprockets).cache_get(cache_key)
end

.set_map_cache(sprockets, logical_path, map_contents) ⇒ Object



53
54
55
56
57
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 53

def self.set_map_cache(sprockets, logical_path, map_contents)
  logical_path = logical_path.gsub(/\.js#{REGEXP_END}/, '')
  cache_key = cache_key_for_path(logical_path+'.map')
  cache(sprockets).cache_set(cache_key, map_contents)
end

Instance Method Details

#call(env) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 80

def call(env)
  prefix_regex = %r{^(?:#{prefix}/|/)}
  path_info = env['PATH_INFO'].to_s.sub(prefix_regex, '')

  case path_info
  when %r{^(.*)\.self\.map$}
    path = $1
    asset  = sprockets[path+'.js']
    return not_found(path) if asset.nil?

    # "logical_name" of a BundledAsset keeps the .js extension
    source = SourceMapServer.get_map_cache(sprockets, asset.logical_path)
    return not_found(asset) if source.nil?

    map = JSON.parse(source)
    map['sources'] = map['sources'].map {|s| "#{prefix}/#{s}"}
    source = map.to_json

    return [200, {"Content-Type" => "text/json"}, [source.to_s]]
  when %r{^(.*)\.rb$}
    begin
      asset = sprockets.resolve(path_info.sub(/\.rb#{REGEXP_END}/,''))
    rescue Sprockets::FileNotFound
      return not_found(path_info)
    end
    return [200, {"Content-Type" => "text/ruby"}, [Pathname(asset).read]]
  else
    not_found(path_info)
  end
end

#inspectObject



76
77
78
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 76

def inspect
  "#<#{self.class}:#{object_id}>"
end

#not_found(*messages) ⇒ Object



111
112
113
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 111

def not_found(*messages)
  not_found = [404, {}, [{not_found: messages}.inspect]]
end