4 ms·
I built an online RSS reader with Rust and WebAssembly
- huydotnet 7y agoI built this as an experiment with Cloudflare's Worker Script. Here's a bit behind this app. - Frontend: Just few lines of code using Svelte - Backend: a Worker Script using Rust, here's how I do the RSS to JSON stuff: #[wasm_bindgen] pub fn parse(content: &str) -> String { let feed: Option<rss::Channel> = match content.parse::<Feed>().unwrap() { Feed::RSS(rss_feed) => Some(rss_feed), _ => None }; if feed.is_some() { let rss_feed = feed.unwrap(); let items: Vec<serde_json::Value> = rss_feed.items.iter().map(|item: &rss::Item| { return json!({ "title": item.title.as_ref(), "link": item.link.as_ref(), "date": item.pub_date.as_ref(), }); }).collect(); return json!({ "feed": items }).to_string(); } return "".to_string(); } The only overhead, I think, is the part that I have to actually write the code to fetch RSS content in JavaScript (worker.js), as fool as you can imagine: const url = request.url.match(/url=(.*)/); if (url && url.length > 1) { const feeds = url[1].split(','); let promises = feeds.map(async url => { const res = await fetch(url); const text = await res.text(); const json = JSON.parse(parse(text)); return json && json.feed || []; }); let result = await Promise.all(promises); let combined = result.reduce((arr, feed) => { arr = arr.concat(feed); return arr; }, []); return new Response(JSON.stringify({ "items": combined }), {status: 200, headers: { 'Content-Type': 'application/json' }}); } If network libraries such as `reqwest`, and some date parser libraries like `chrono` already supported WASM, I'd be able to skip this JS step for good. Deployed on Cloudflare using Wrangler CLI, I'm still on my free plan, and I hope I won't hit a 100k requests/day limit anytime soon.
- rahuldottech 7y agoThis is neat! I'm definitely going to be making use of this :)
- lostmsu 7y agoIt is cool, but it is not really an application in the expected sense. It is just a 2 stage RSS to HTML converter (e.g. RSS->JSON server, JSON->HTML client). Not sure why not make just do RSS->HTML directly on the server. Obviously, does not track read state or sync, or anything else basically.