Class: Opal::SourceMapServer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SourceMapServer



16
17
18
19
# File 'opal/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 'opal/lib/opal/sprockets/server.rb', line 21

def prefix
  @prefix
end

#sprocketsObject (readonly)

Returns the value of attribute sprockets



21
22
23
# File 'opal/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
53
54
# File 'opal/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$/, '')]
    return not_found(asset) if source.nil?

    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 'opal/lib/opal/sprockets/server.rb', line 23

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

#not_found(*messages) ⇒ Object



56
57
58
# File 'opal/lib/opal/sprockets/server.rb', line 56

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

#registerObject



60
61
62
# File 'opal/lib/opal/sprockets/server.rb', line 60

def register
  Opal::Processor.source_map_register
end