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



71
72
73
74
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 71

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

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix



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

def prefix
  @prefix
end

#sprocketsObject (readonly)

Returns the value of attribute sprockets



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

def sprockets
  @sprockets
end

Class Method Details

.cache(sprockets) ⇒ Object



61
62
63
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 61

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

.cache_key_for_path(logical_path) ⇒ Object



65
66
67
68
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 65

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



49
50
51
52
53
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 49

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



55
56
57
58
59
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 55

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



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
110
111
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 82

def call(env)
  prefix_regex = %r{^(?:#{prefix}/|/)}
  path_info = CGI.unescape(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



78
79
80
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 78

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

#not_found(*messages) ⇒ Object



113
114
115
# File 'opal/lib/opal/sprockets/source_map_server.rb', line 113

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