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

Returns a new instance of Node.



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
57
58
59
# File 'lib/pacproxy/runtimes/node/node.rb', line 29

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?

      initialize_client
    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.



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

def source
  @source
end

Class Method Details

.runtimeObject



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

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



76
77
78
79
80
# File 'lib/pacproxy/runtimes/node/node.rb', line 76

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

#shutdownObject



61
62
63
64
65
66
67
68
# File 'lib/pacproxy/runtimes/node/node.rb', line 61

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

#update(file_location) ⇒ Object



70
71
72
73
74
# File 'lib/pacproxy/runtimes/node/node.rb', line 70

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