Class: Pacproxy::Runtimes::Node

Inherits:
Base
  • Object
show all
Includes:
Loggable
Defined in:
lib/pacproxy/runtimes/node/node.rb

Overview

Pacproxy::Runtimes::Node represet node js runtime

Constant Summary collapse

TIMEOUT_JS_CALL =
0.5
TIMEOUT_JS_SERVER =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#access_logger, #accesslog, #debug, #error, #fatal, #general_logger, #info, #lwarn

Constructor Details

#initializeNode



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
55
56
# File 'lib/pacproxy/runtimes/node/node.rb', line 28

def initialize
  js = File.join(File.dirname(__FILE__), 'find.js')

  retries = 3
  begin
    Timeout.timeout(TIMEOUT_JS_SERVER) do
      server = TCPServer.new('127.0.0.1', 0)
      @port = server.addr[1]
      server.close
      if OS.windows?
        @server_pid = start_server
      else
        @server_pid = fork { exec('node', js, @port.to_s) }
        Process.detach(@server_pid)
      end
      sleep 0.01 until port_open?
    end
  rescue Timeout::Error
    shutdown
    if retries > 0
      retries -= 1
      lwarn('Timeout. Initialize Node.js server.')
      retry
    else
      error('Gave up to retry Initialize Node.js server.')
      raise 'Gave up to retry Initialize Node.js server.'
    end
  end
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



17
18
19
# File 'lib/pacproxy/runtimes/node/node.rb', line 17

def source
  @source
end

Class Method Details

.runtimeObject



19
20
21
22
23
24
25
26
# File 'lib/pacproxy/runtimes/node/node.rb', line 19

def self.runtime
  if Util.which('node').nil?
    error('No PAC supported runtime')
    fail(RuntimeUnavailable,
         'No PAC supported runtime')
  end
  new
end

Instance Method Details

#find(url) ⇒ Object



72
73
74
75
76
# File 'lib/pacproxy/runtimes/node/node.rb', line 72

def find(url)
  return 'DIRECT' unless @source
  uri = URI.parse(url)
  call_find(uri)
end

#shutdownObject



58
59
60
61
62
63
64
# File 'lib/pacproxy/runtimes/node/node.rb', line 58

def shutdown
  if OS.windows?
    stop_server(@server_pid)
  else
    Process.kill(:INT, @server_pid)
  end
end

#update(file_location) ⇒ Object



66
67
68
69
70
# File 'lib/pacproxy/runtimes/node/node.rb', line 66

def update(file_location)
  @source = open(file_location, proxy: false).read
rescue
  @source = nil
end