Class: Opal::SourceMapServer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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


16
17
18
19
# File 'lib/opal/sprockets/server.rb', line 16

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

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix


21
22
23
# File 'lib/opal/sprockets/server.rb', line 21

def prefix
  @prefix
end

#sprocketsObject (readonly)

Returns the value of attribute sprockets


21
22
23
# File 'lib/opal/sprockets/server.rb', line 21

def sprockets
  @sprockets
end

Instance Method Details

#call(env) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/opal/sprockets/server.rb', line 27

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

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

    # "logical_name" of a BundledAsset keeps the .js extension
    source = register[asset.logical_path.sub(/\.js$/, '')]
    map = JSON.parse(source)
    map['sources'] = map['sources'].map {|s| "#{prefix}/#{s}"}
    source = map.to_json
    return not_found(asset) if source.nil?

    return [200, {"Content-Type" => "text/json"}, [source.to_s]]
  when %r{^(.*)\.rb$}
    source = File.read(sprockets.resolve(path_info))
    return not_found(path_info) if source.nil?
    return [200, {"Content-Type" => "text/ruby"}, [source]]
  else
    not_found(path_info)
  end
end

#inspectObject


23
24
25
# File 'lib/opal/sprockets/server.rb', line 23

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

#not_found(*messages) ⇒ Object


54
55
56
# File 'lib/opal/sprockets/server.rb', line 54

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

#registerObject


58
59
60
# File 'lib/opal/sprockets/server.rb', line 58

def register
  Opal::Processor.source_map_register
end