Transporter 0.5.1
Request handler
(ctx)=>{
ctx.headers.set('content-type', 'text/event-stream');
const stream = new BroadcastStream();
ctx.request.body?.pipeTo(stream.writable);
return stream.readable;
}
Quick start
async function message(msg) {
return await importTransporter().then(({ stream: { transport } })=>transport.text('/toUpperCase').pipe(transport.text(`/echo`))).then((stream)=>{
stream.read(console.log);
stream.write(msg);
return stream;
});
}
This example
async function example() {
// import
const transporter = await importTransporter();
// make global
// example
const { transport } = transporter.stream;
const stream = transport.text('/echo');
await stream.request.response;
stream.read((chunk)=>{
console.log(chunk);
document.querySelector('body>pre')?.append(chunk);
});
stream.write(`Hello World!\n`);
document.querySelector('textarea')?.addEventListener('input', (e)=>stream.write(e.data || '\n'));
Object.assign(globalThis, {
stream
});
console.log({
example,
transporter
});
}