Class: Up::Bun::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/up/bun/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(app:, host: 'localhost', port: 3000, scheme: 'http', ca_file: nil, cert_file: nil, key_file: nil, pid_file: nil, logger: Logger.new(STDERR)) ⇒ Server

Returns a new instance of Server.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/up/bun/server.rb', line 17

def initialize(app:, host: 'localhost', port: 3000, scheme: 'http',
               ca_file: nil, cert_file: nil, key_file: nil,
               pid_file: nil,
               logger: Logger.new(STDERR))
  @app = app
  @scheme    = scheme || 'http'
  raise "unsupported scheme #{@scheme}" unless %w[http https].include?(@scheme)
  @host      = host || 'localhost'
  @port      = port&.to_i || 3000
  @config    = { handler: self.class.name, engine: "bun/#{`process.version`}", port: port, scheme: scheme, host: host, logger: logger }.freeze
  @ca_file   = ca_file
  @cert_file = cert_file
  @key_file  = key_file
  @pid_file  = pid_file
  @default_input = StringIO.new('', 'r')
  @server    = nil
  @logger    = logger
end

Instance Method Details

#listenObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/up/bun/server.rb', line 62

def listen
  raise "already running" if @server
  ::Up.instance_variable_set(:@instance, self)
  File.write(@pid_file, `process.pid.toString()`) if @pid_file
  puts "Server PID: #{`process.pid`}"
  %x{
    const oubs = Opal.Up.Bun.Server;
    const ouwc = Opal.Up.Client;
    const deco = new TextDecoder();
    var server_options = {
      port: #@port,
      hostname: #@host,
      development: false,
      async fetch(req, server) {
        const upgrade = req.headers.get('Upgrade');
        const env = new Map();
        env.set('rack.errors',#{STDERR});
        if (req.method === 'POST') {
          let body = await req.text();
          console.log('received: ', body);
          env.set('rack.input', #{StringIO.new(`body`)});
        } else {
          env.set('rack.input', #@default_input);
        }
        env.set('rack.logger', #@logger);
        if (upgrade) {
          env.set('rack.upgrade?', #{:websocket});
        }
        env.set('rack.url_scheme', #@scheme);
        env.set('SCRIPT_NAME', "");
        env.set('SERVER_PROTOCOL', req.httpVersion);
        env.set('HTTP_VERSION', req.httpVersion);
        env.set('SERVER_NAME', #@host);
        env.set('SERVER_PORT', #@port);
        env.set('QUERY_STRING', "");
        env.set('REQUEST_METHOD', req.method);
        env.set('PATH_INFO', req.url);
        req.headers.forEach((k, v) => { 
          let h = k.toUpperCase().replaceAll('-', '_');
          if (h[0] === 'C' && (h === 'CONTENT_TYPE || h === 'CONTENT_LENGTH')) {
            env.set(h, v) ;
          } else {
            env.set('HTTP_' + h, v) ;
          }
        });
        const rack_res = #@app.$call(env);
        if (upgrade) {
          const handler = env.get('rack.upgrade');
          if (rack_res[0] < 300 && handler && handler !== nil) {
            const client = ouwc.$new();
            client.env = env;
            client.open = false;
            client.handler = handler
            client.protocol = #{:websocket};
            client.server = server;
            client.timeout = 120;
            server.upgrade(req, { data: { client: client }});
            return;
          }
        }
        const hdrs = new Headers();
        oubs.handle_headers(rack_res[1], hdrs);
        var body = '';
        body = oubs.handle_response(rack_res[2], body);
        return new Response(body, {status: rack_res[0], statusText: 'OK', headers: hdrs});
      },
      websocket: {
        close: (ws) => {
          const client = ws.data.client;
          if (typeof(client.handler.$on_close) === 'function') {
            client.ws = ws;
            client.open = false;
            client.handler.$on_close(client);
            client.ws = null;
          }
        },
        drain: (ws) => {
          const client = ws.data.client;
          if (typeof(client.handler.$on_drained) === 'function') {
            client.ws = ws;
            client.handler.$on_drained(client);
            client.ws = null;
          }
        },
        message: (ws, message) => {
          const client = ws.data.client;
          if (typeof(client.handler.$on_message) === 'function') {
            if (typeof(message) !== 'string') {
              message = deco.decode(message);
            }
            client.ws = ws;
            client.handler.$on_message(client, message);
            client.ws = null;
          }
        },
        open: (ws) => {
          const client = ws.data.client;
          if (typeof(client.handler.$on_open) === 'function') {
            client.ws = ws;
            client.open = true;
            client.handler.$on_open(client);
            client.ws = null;
          }
        } 
      }
    };
    if (#@scheme === 'https') {
      server_options.tls = {
        key: Bun.file(#@key_file),
        cert: Bun.file(#@cert_file),
        ca: Bun.file(#@ca_file)
      };
    }

    #@server = Bun.serve(server_options);
    console.log(`Server is running on ${#@scheme}://${#@host}:${#@port}`);
  }
end

#publish(channel, message) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/up/bun/server.rb', line 181

def publish(channel, message)
  %x{
    if (!message.$$is_string) {
      message = JSON.stringify(message);
    }
    #@server.publish(channel, message);
  }
end

#stopObject



190
191
192
193
194
195
# File 'lib/up/bun/server.rb', line 190

def stop
  if Up::CLI::stoppable?
    `#@server.stop()`
    @server = nil
  end
end