Skip to content

Instantly share code, notes, and snippets.

View joshnuss's full-sized avatar
🤘

Joshua Nussbaum joshnuss

🤘
View GitHub Profile
/*
It's now a package. You can find it here:
https://github.com/joshnuss/svelte-local-storage-store
*/
// Svelte store backed by window.localStorage
// Persists store's data locally
@joshnuss
joshnuss / mnesia.exs
Last active March 9, 2024 00:32
Elixir example code for accessing mnesia databases
# define a record, first attribute is considered the key
defrecord User, email: "", first: "", last: ""
# encapsulates mnesia calls
defmodule Database do
def create_schema do
create_table User
end
def find(record, id) do
@joshnuss
joshnuss / app.js
Last active March 4, 2024 00:01
Express.js role-based permissions middleware
// the main app file
import express from "express";
import loadDb from "./loadDb"; // dummy middleware to load db (sets request.db)
import authenticate from "./authentication"; // middleware for doing authentication
import permit from "./authorization"; // middleware for checking if user's role is permitted to make request
const app = express(),
api = express.Router();
// first middleware will setup db connection
@joshnuss
joshnuss / setup.js
Last active February 5, 2024 07:01
Vitest matchers for SvelteKit errors and redirections
import { expect } from 'vitest'
expect.extend({
async toError(promise, status, message=null) {
try {
await promise
return { pass: false, message: () => 'Expected an error to be raised.'}
} catch (actual) {
if (actual?.constructor?.name !== 'HttpError') {
@joshnuss
joshnuss / preloader.exs
Last active January 25, 2024 18:59
Preloading & joining with Ecto, simplified.
# Preloading usually required an extra query.
# To do it in one query, a `join` is needed, and the call to `preload` needs to know the name of join
# This macro does both the `join` and `preload` together
defmodule Preloader do
import Ecto, only: [assoc: 2]
alias Ecto.Query.Builder.{Join, Preload}
defmacro preload_join(query, association) do
expr = quote do: assoc(l, unquote(association))
binding = quote do: [l]
@joshnuss
joshnuss / OAuthClient.js
Last active December 10, 2023 22:46
OAuth2 Client
import fetch from 'node-fetch'
// some provider data is copied from github.com/simov/grant
const providers = {
bogus: {
authorize_url: "http://localhost:8282/auth/request/path",
access_url: "http://localhost:8282/access/token/request",
},
google: {
@joshnuss
joshnuss / README.md
Last active November 18, 2023 06:02
Excellon drill format
@joshnuss
joshnuss / httpStore.js
Last active October 11, 2023 11:29
A Svelte store backed by HTTP
import { writable } from 'svelte/store'
// returns a store with HTTP access functions for get, post, patch, delete
// anytime an HTTP request is made, the store is updated and all subscribers are notified.
export default function(initial) {
// create the underlying store
const store = writable(initial)
// define a request function that will do `fetch` and update store when request finishes
store.request = async (method, url, params=null) => {
@joshnuss
joshnuss / udp_server.exs
Last active October 9, 2023 09:11
Fault tolerant UDP Server in Elixir
# to run:
# > elixir --no-halt udp_server.exs
# to test:
# > echo "hello world" | nc -u -w0 localhost 2052
# > echo "quit" | nc -u -w0 localhost 2052
# Let's call our module "UDPServer"
defmodule UDPServer do
# Our module is going to use the DSL (Domain Specific Language) for Gen(eric) Servers
use GenServer
@joshnuss
joshnuss / ble_speedometer_esp32.ino
Last active August 17, 2023 21:46
Bluetooth Speedometer using ESP32 and Hall effect sensor
/*
* Using digital hall effect sensor SENS-M-10 (purchased at Abra)
*
* MCU Board: ESP2-WROOM-32
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>