Skip to content

Instantly share code, notes, and snippets.

View jpillora's full-sized avatar
👶

Jaime Pillora jpillora

👶
View GitHub Profile
@jpillora
jpillora / test.js
Last active April 6, 2024 01:27
async javascript test
const MAX_INFLIGHT = 4;
const TOTAL = 100;
// the given dummy api supports a maximum of 4 of inflight requests.
// the given code is correct, but it is slow because it processes elements serially.
// your task is to process 100 elements as fast as possible.
// run this code with "node/bun test.js".
// it should print "pass".
// no external dependencies are allowed.
async function run(elements) {
// ============
@jpillora
jpillora / install.sh
Last active January 24, 2024 13:56
Install Microsoft's VS code-server
#!/bin/bash
set -euf -o pipefail
if [ ! -f code-server ]; then
ARCH=$(uname -m)
OS="unknown-linux-gnu"
echo "downloading code-server ($ARCH-$OS)"
URL="https://vscodeserverlauncher.blob.core.windows.net/builds/latestbin/$ARCH-$OS/$ARCH-$OS"
curl -L "$URL" >code-server
chmod +x code-server
@jpillora
jpillora / README.md
Created February 27, 2023 01:13
tailscale from source
go install -v tailscale.com/cmd/tailscale{,d}@latest

sudo tailscaled install-system-daemon

tailscale up

# login, approve

tailscale status
@jpillora
jpillora / README.md
Last active February 6, 2023 01:01
Mininet demo on M1 Macs
@jpillora
jpillora / README.md
Last active February 5, 2023 23:27
Vite + TypeScript + Reveal.js
  1. Setup

    npx create-vite
    # vanilla
    # typescript
    # enter name
    cd name/
    npm install reveal.js
func typeCache[T any](compute func(t reflect.Type) T) func(t reflect.Type) T {
cache := map[reflect.Type]T{}
return func(t reflect.Type) T {
if v, ok := cache[t]; ok {
return v
}
v := compute(t) // but compute cant recurse
cache[t] = v
return v
}
@jpillora
jpillora / README.md
Created November 1, 2022 12:14
kardianos/service MacOS example

Usage on MacOS

go build -o myservice

# installs and starts
./myservice

# plist file should be created
ls ~/Library/LaunchAgents/
@jpillora
jpillora / useAsync.ts
Created January 6, 2022 05:55
useAsync.ts
import { useCallback, useEffect, useState } from 'react'
// borrowed from https://usehooks.com/useAsync/
export function useAsync<T>(asyncFunction: () => Promise<T>, immediate = true) {
const [loading, setLoading] = useState(true)
const [value, setValue] = useState<T | null>(null)
const [error, setError] = useState<any>(null)
// The execute function wraps asyncFunction and
// handles setting state for pending, value, and error.
// useCallback ensures the below useEffect is not called
@jpillora
jpillora / icloud-dl.sh
Created February 7, 2021 06:47
Download iCloud link from Terminal
#!/bin/bash
# given "https://www.icloud.com/iclouddrive/<ID>#<Filename>
ID="...."
URL=$(curl 'https://ckdatabasews.icloud.com/database/1/com.apple.cloudkit/production/public/records/resolve' \
--data-raw '{"shortGUIDs":[{"value":"$ID"}]}' --compressed \
jq -r '.results[0].rootRecord.fields.fileContent.value.downloadURL')
curl "$URL" -o myfile.ext
@jpillora
jpillora / scale.js
Last active October 7, 2019 11:40
Scale
//scale returns the number scaled to its corresponding SI suffix.
// scale(1234) => "1.2 K"
//MIT License, Copyright jpillora © 2019
function scale(n, d) {
// set default number
if (typeof n !== "number" || isNaN(n)) n = 0;
if (n === 0) return "0";
// set default digit count
if (typeof d !== "number" || isNaN(d)) d = 1;
// find scale index 1000,100000,... becomes 1,2,...