Slide 48
Slide 48 text
src/rack.js
rack.js
export class RackHandler {
async process(request) {
let vm = await this.vmSetup();
const railsURL = request.url;
const railsHeaders = {};
for (const [key, value] of request.headers.entries()) {
railsHeaders[`HTTP_${key.toUpperCase().replaceAll("-", "_")}`] = value;
}
try {
const command = `
request = Rack::MockRequest.env_for(
"${railsURL}",
JSON.parse(%q[${JSON.stringify(railsHeaders)}]).merge(
method: :${request.method}
)
)
response = Rack::Response[*Rails.application.call(request)]
status, headers, body = *response.finish
{status:, headers:, body:}
`;
let res = vm.eval(command).toJS();
let { status, headers, body } = res;
const resp = new Response(body, {
headers,
status,
});
return resp;
} catch (e) {
this.logger.error(e);
return new Response(`Application Error: ${e.message}`, {
status: 500,
});
}
}
}
1
2
3
4
5
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45