Skip to content

Instantly share code, notes, and snippets.

@myobie
myobie / router.ex
Last active Apr 15, 2021
A pipeline for proxying large file uploads
View router.ex
defmodule AppWeb.Router do
use AppWeb, :router
pipeline :browser do
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Phoenix.json_library()
plug Plug.MethodOverride
@myobie
myobie / poller.ex
Last active Apr 8, 2021
GenServer for longpoll requests with backoff in case of errors
View poller.ex
defmodule Example.Poller do
use GenServer
@backoff [
50,
100,
150,
250,
400,
650
@myobie
myobie / application.ex
Last active Apr 1, 2021
Using Finch with ExAws
View application.ex
defmodule Example.Application do
@moduledoc false
use Application
def start(_type, _args) do
children = [
Example.Repo,
ExampleWeb.Telemetry,
{Phoenix.PubSub, name: Example.PubSub},
View identifier.swift
import Foundation
public struct Identifier:
Codable, CustomStringConvertible, ExpressibleByStringLiteral,
Hashable, RawRepresentable
{
public let rawValue: [UInt8]
public let dataValue: Data
private let stringValue: String
View encrypted-previews.md

Previewing encrypted content on the web

This would be an extension to The Open Graph protocol.

As more and more people are using privacy preserving tools for sharing content on the web, more content embedded into web pages will be end-to-end encrypted and decrypted in-browser by javascript or wasm code.

A current problem with previews of web URLs is they require the server to know the exact contents of the URL and be able to provide a preview as a resource either inside a header or as a URL to an image. If the primary content at the URL were encrypted, no preview could be provided directly by the server.

What the server could provide is an encrypted preview which was encrypted with a symmetric key by the original creator using one of the standard algorithms provided by webcrypto. Then the browser/client could find the decryption key in the URL fragment (or prompt the user for it) to then facilitate the decryption of the preview content safely on device. This would provide a standard

@myobie
myobie / example-throws-in-callback-that-works-in-playgrounds.swift
Created Nov 29, 2017
An example of swift async callbacks that either throw or return a result
View example-throws-in-callback-that-works-in-playgrounds.swift
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
// either throws an Error or returns a String result
typealias ThrowsCallback = () throws -> (String)
enum AsyncError: Error {
case kaboom
}
View runner.ts
const worker = new Worker('/tests.js')
worker.onmessage = e => {
if (e.data && e.data._command) {
if (e.data._command === 'console') {
const type = e.data.type || 'log'
const msg = e.data.msg || '<empty>'
switch (type) {
case 'error':
@myobie
myobie / drain.js
Last active Jan 25, 2021
Drain an array of potential promises with a max concurrency factor
View drain.js
/** @typedef {() => Promise<unknown>} Task */
/**
* @param {Task[]} pending
* @param {number} max
* @returns {Promise<unknown[]>}
*/
export function drain (pending, max) {
return new Promise((resolve, reject) => {
let nextIndex = 0
@myobie
myobie / build-utils.cjs
Created Jan 7, 2021
Using estrella to build tests, serve-handler to serve the results, and playwright to execute the tests and stream the logs back to the node console
View build-utils.cjs
const { join, resolve } = require('path')
const { basename, build, file, log, glob, cliopts, stdoutStyle, watch } = require('estrella')
async function serve (cwd, opts = null) {
opts || (opts = {})
let pkgOpts = { devServerPort: null, publicPath: null, rewrites: null, redirects: null }
if (isNodeProject(cwd)) {
const pkg = JSON.parse(await file.read(join(cwd, 'package.json'), { encoding: 'utf8' }))
@myobie
myobie / tests.ts
Created Jan 7, 2021
Build tests with esbuild, serve them with vercel's serve-handler, then use playwright to open a browser and stream the console back to the node console
View tests.ts
// NOTE: we are just assuming _tests is an alright place to render to
export async function buildAndRunTests (cwd: string): Promise<void> {
const [{ once }] = cliopts.parse(
['o, once', 'run the tests and then exit']
)
await file.mkdirs(join(cwd, '_tests'))
await buildBrowserTests(cwd)