57
58
59
60
61
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
|
# File 'lib/up/bun/server.rb', line 57
def listen
raise "already running" if @server
::Up.instance_variable_set(:@instance, self)
%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
|