Skip to content

Instantly share code, notes, and snippets.

Avatar

Rodrigo Navarro reu

View GitHub Profile
@reu
reu / as_json_string.rs
Created Mar 13, 2021
Serde double encoding JSON
View as_json_string.rs
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 Feb 15, 2021
Naive http/https proxy
View Cargo.toml
[package]
name = "naive-http-proxy"
version = "0.1.0"
authors = ["Rodrigo Navarro <rnavarro@rnavarro.com.br>"]
edition = "2018"
[[bin]]
name = "proxy"
path = "proxy.rs"
View immutable-example.rb
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 Mar 22, 2019
Simple HTTP proxy
View proxy.js
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 Nov 8, 2018
HTML builder DSL
View example.js
import { h1, div, p } from "./html";
div([
h1({ class: "title" }, "Title"),
p("A nice text"),
]);
View simple-permanent-redirect.js
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)}` });
View hackish-url-parser.js
// @flow
import {
always,
memoize,
pick,
} from "ramda";
type ParsedURL = {
protocol: string,
View batch-map.js
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;
View collapsable.js
import React, {
Component,
PropTypes,
} from "react";
import {
Animated,
View,
} from "react-native";
View mkdirp.js
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);