Skip to content

Instantly share code, notes, and snippets.

A ghost

Nathan myobie

A ghost
View GitHub Profile
myobie / generate-importable-blob.ts
Created Mar 25, 2022
Generate Blob import from file on disk with deno
View generate-importable-blob.ts
const bytes = await Deno.readFile(new URL('./thumbnail.png', import.meta.url))
const encoder = new TextEncoder()
const contents = encoder.encode(
`export const thumbnail = new Blob(new Uint8Array(${
}), { type: 'image/png' })`
await Deno.writeFile(new URL('./hardcoded-thumbnail.ts', import.meta.url), contents)
myobie / remove-www.js
Created Mar 15, 2022
A cloudflare worker to remove the www subdomain and redirect
View remove-www.js
const status = 301
addEventListener('fetch', event => {
const url = new URL(event.request.url)
url.hostname = url.hostname.replace(/^www\./, '')
event.respondWith(Response.redirect(url.toString(), status))
myobie / async_iterators_throw_catch_test.ts
Last active Feb 10, 2022
I was testing to see if the next() in an async iterator worked how I suspected, where throws are really Promise.reject() which throws at the for await point.
View async_iterators_throw_catch_test.ts
import { assertEquals } from ''
Deno.test('async iterator throw in next should throw in for scope', async () => {
const results: number[] = []
const numbers = (() => {
let counter = 0
const iterator: AsyncIterator<number> = {
// deno-lint-ignore require-await
myobie / deferred.ts
Created Aug 2, 2021
My own promise-like object where I can type the rejection and I can resolve from the outside
View deferred.ts
export class Deferred<T = void, E = Error> {
#promise: Promise<T>
// deno-lint-ignore ban-ts-comment
// @ts-ignore
resolve: (arg: T) => void
// deno-lint-ignore ban-ts-comment
// @ts-ignore
reject: (arg: E) => void
constructor() {
myobie / task.ts
Created Jul 30, 2021
Very simple deno script to test that deno works
View task.ts
const res = await fetch('')
const body = new Uint8Array(await res.arrayBuffer())
await Deno.stdout.write(body)
myobie / server.ts
Created Jul 29, 2021
Very simple deno webserver mostly exactly from the example in the docs at
View server.ts
// Start listening on port 8080 of localhost.
const server = Deno.listen({ port: 8080 })
console.log('HTTP webserver running...')
const headers = Object.freeze({
'Content-type': 'plain/text'
for await (const conn of server) {
// deno-lint-ignore no-extra-semi
myobie / params.ex
Created May 26, 2021
I'm not saying this is a good idea, but I'm not not saying it
View params.ex
defmodule POC.Params do
import Ecto.Changeset
@spec permit(map, map) :: map
def permit(input, types) do
{types, nested} = Enum.reduce(types, {%{}, %{}}, &process_types/2)
{%{}, types}
|> cast(input, Map.keys(types))
|> permit_nested(nested)
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 / 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 [
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