Skip to content

Instantly share code, notes, and snippets.

View reu's full-sized avatar

Rodrigo Navarro reu

View GitHub Profile
@reu
reu / as_json_string.rs
Created March 13, 2021 16:31
Serde double encoding JSON
mod as_json_string {
use serde::de::{Deserialize, DeserializeOwned, Deserializer};
use serde::ser::{Serialize, Serializer};
use serde_json;
pub fn serialize<T: Serialize, S: Serializer>(
value: &T,
serializer: S,
) -> Result<S::Ok, S::Error> {
let json = serde_json::to_string(value).map_err(serde::ser::Error::custom)?;
@reu
reu / Cargo.toml
Last active February 15, 2021 22:57
Naive http/https proxy
[package]
name = "naive-http-proxy"
version = "0.1.0"
authors = ["Rodrigo Navarro <rnavarro@rnavarro.com.br>"]
edition = "2018"
[[bin]]
name = "proxy"
path = "proxy.rs"
Person = Immutable.new(:name, :age)
p1 = Person.new(name: "Sasha", age: 30) # <Person @name="Sasha", @age=30>
p2 = p1.with_name("Tori") # <Person @name="Tori", @age=30>
p3 = p2.update(name: "Craudio", age: 10) # <Person @name="Craudio", @age=10>
Person.new(name: "Sasha").with_age(30).with_name { |name| "#{name} Grey" } # <Person @name="Sasha Grey", @age=30>
@reu
reu / proxy.js
Created March 22, 2019 15:01
Simple HTTP proxy
const http = require("http");
const { PORT, PROXY_HOST, PROXY_PORT } = process.env;
http
.createServer((req, res) => {
const proxyRequest = http
.request({
hostname: PROXY_HOST,
port: PROXY_PORT || 80,
@reu
reu / example.js
Last active January 4, 2023 00:34
HTML builder DSL
import { h1, div, p } from "./html";
div([
h1({ class: "title" }, "Title"),
p("A nice text"),
]);
const http = require("http");
const path = require("path");
const target = process.env.TARGET; // ex: google.com
const port = process.env.PORT || 80;
http
.createServer(({ url, headers }, res) => {
const protocol = headers["x-forwarded-proto"] || "http";
res.writeHead(308, { Location: `${protocol}://${path.join(target, url)}` });
// @flow
import {
always,
memoize,
pick,
} from "ramda";
type ParsedURL = {
protocol: string,
const { curry, splitEvery } = require("ramda");
const batchMap = curry((batchSize, fn, list) =>
splitEvery(batchSize, list)
.map(batch => all => Promise.all(batch.map(fn)).then(res => all.concat(res)))
.reduce((results, batch) => results.then(batch), Promise.resolve([]))
);
module.exports = batchMap;
import React, {
Component,
PropTypes,
} from "react";
import {
Animated,
View,
} from "react-native";
@reu
reu / mkdirp.js
Last active April 14, 2017 02:58
const fs = require("fs");
const path = require("path");
const promisify = fn => (...args) =>
new Promise((resolve, reject) =>
fn(...args, (err, res) => err === null ? resolve(res) : reject(err))
);
const mkdir = promisify(fs.mkdir);
const stat = promisify(fs.stat);