Created
September 6, 2022 04:06
-
-
Save shivaylamba/4aee08a9df297bdaf8925b1f2536f06c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
import ReactDOMServer from 'react-dom/server'; | |
import * as std from 'std'; | |
import * as http from 'wasi_http'; | |
import * as net from 'wasi_net'; | |
import App from '../src/App.js'; | |
async function handle_client(cs) { | |
print('open:', cs.peer()); | |
let buffer = new http.Buffer(); | |
while (true) { | |
try { | |
let d = await cs.read(); | |
if (d == undefined || d.byteLength <= 0) { | |
return; | |
} | |
buffer.append(d); | |
let req = buffer.parseRequest(); | |
if (req instanceof http.WasiRequest) { | |
handle_req(cs, req); | |
break; | |
} | |
} catch (e) { | |
print(e); | |
} | |
} | |
print('end:', cs.peer()); | |
} | |
function enlargeArray(oldArr, newLength) { | |
let newArr = new Uint8Array(newLength); | |
oldArr && newArr.set(oldArr, 0); | |
return newArr; | |
} | |
async function handle_req(s, req) { | |
print('uri:', req.uri) | |
let resp = new http.WasiResponse(); | |
let content = ''; | |
if (req.uri == '/') { | |
const app = ReactDOMServer.renderToString(<App />); | |
content = std.loadFile('./build/index.html'); | |
content = content.replace('<div id="root"></div>', `<div id="root">${app}</div>`); | |
} else { | |
let chunk = 1000; // Chunk size of each reading | |
let length = 0; // The whole length of the file | |
let byteArray = null; // File content as Uint8Array | |
// Read file into byteArray by chunk | |
let file = std.open('./build' + req.uri, 'r'); | |
while (true) { | |
byteArray = enlargeArray(byteArray, length + chunk); | |
let readLen = file.read(byteArray.buffer, length, chunk); | |
length += readLen; | |
if (readLen < chunk) { | |
break; | |
} | |
} | |
content = byteArray.slice(0, length).buffer; | |
file.close(); | |
} | |
let contentType = 'text/html; charset=utf-8'; | |
if (req.uri.endsWith('.css')) { | |
contentType = 'text/css; charset=utf-8'; | |
} else if (req.uri.endsWith('.js')) { | |
contentType = 'text/javascript; charset=utf-8'; | |
} else if (req.uri.endsWith('.json')) { | |
contentType = 'text/json; charset=utf-8'; | |
} else if (req.uri.endsWith('.ico')) { | |
contentType = 'image/vnd.microsoft.icon'; | |
} else if (req.uri.endsWith('.png')) { | |
contentType = 'image/png'; | |
} | |
resp.headers = { | |
'Content-Type': contentType | |
}; | |
let r = resp.encode(content); | |
s.write(r); | |
} | |
async function server_start() { | |
print('listen 8002...'); | |
try { | |
let s = new net.WasiTcpServer(8002); | |
for (var i = 0; ; i++) { | |
let cs = await s.accept(); | |
handle_client(cs); | |
} | |
} catch (e) { | |
print(e); | |
} | |
} | |
server_start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment