@clappcodes/transporter
v0.5.1
Published
Transport Web Stream
Downloads
21
Readme
________ _____
___ __/______________ ________ ________________ ______ __________ /______ ________
__ / __ ___/_ __ `/__ __ \__ ___/___ __ \_ __ \__ ___/_ __/_ _ \__ ___/
_ / _ / / /_/ / _ / / /_(__ ) __ /_/ // /_/ /_ / / /_ / __/_ /
/_/ /_/ \__,_/ /_/ /_/ /____/ _ .___/ \____/ /_/ \__/ \___/ /_/
/_/
@clappcodes/transporter
Request handler
import route from '@clappcodes/transporter/route';
import { BroadcastStream, transform } from '@clappcodes/transporter/stream';
const toUpperCase = route.post('/toUpperCase', ({ request }) =>
request.body
?.pipeThrough(new TextDecoderStream())
.pipeThrough(transform.toUpperCase())
.pipeThrough(new TextEncoderStream()));
const echo = route.post('/echo', (ctx) => {
const stream = new BroadcastStream();
ctx.request.body?.pipeTo(stream.writable);
ctx.headers.set('content-type', 'text/event-stream');
return stream.readable;
});
route(route.cors(), echo, toUpperCase)
.serve({ port: 3000 });
Quick start
- Just paste in console
import('https://esm.sh/@clappcodes/transporter/stream')
.then(({ transport, transform }) =>
transport.pipe(
transport.text(`https://transporter.deno.dev/toUpperCase`),
transport.text(`https://transporter.deno.dev/echo`),
transform.toUpperCase(),
)
)
.then((stream) => {
stream.read(console.log);
stream.write('Hello World');
});
import { transport } from "@clappcodes/transporter/stream";
const stream = transport.pipe(
transport.text("/toUpperCase", /** toUpperCase */)
transport.text("/echo", /** echo */)
);
stream.read(console.log);
stream.write("Hello"); // => HELLO