feat: browser client for smol mail
This commit is contained in:
commit
9798d38040
19 changed files with 3331 additions and 0 deletions
22
web/js/transport.js
Normal file
22
web/js/transport.js
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// The byte pipe to a smolmaild server. Browsers cannot open TCP sockets, so
|
||||
// bytes travel through the local bridge's WebSocket relay unchanged; the
|
||||
// Noise session lives in this page either way.
|
||||
|
||||
const BRIDGE = `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}`;
|
||||
|
||||
export function connectStream(host, port) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const socket = new WebSocket(`${BRIDGE}/tcp/${host}/${port}`);
|
||||
socket.binaryType = "arraybuffer";
|
||||
const stream = {
|
||||
send: bytes => socket.send(bytes),
|
||||
close: () => socket.close(),
|
||||
onData: null,
|
||||
onClose: null,
|
||||
};
|
||||
socket.onopen = () => resolve(stream);
|
||||
socket.onerror = () => reject(new Error(`cannot reach the bridge at ${BRIDGE}`));
|
||||
socket.onmessage = event => stream.onData?.(new Uint8Array(event.data));
|
||||
socket.onclose = () => stream.onClose?.();
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue