Skip to content

Instantly share code, notes, and snippets.

Avatar
👻
A ghost

Nathan myobie

👻
A ghost
View GitHub Profile
@myobie
myobie / expand-all-files-on-github-diff-page.js
Created Oct 6, 2022
I couldn't find an "expand all" button, so I just made my own
View expand-all-files-on-github-diff-page.js
document.querySelectorAll('button[aria-label="Toggle diff contents"]').forEach(b => b.click())
@myobie
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(${
JSON.stringify(Array.from(bytes))
}), { type: 'image/png' })`
)
await Deno.writeFile(new URL('./hardcoded-thumbnail.ts', import.meta.url), contents)
@myobie
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
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 'https://deno.land/std@0.125.0/testing/asserts.ts'
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
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
myobie / task.ts
Created Jul 30, 2021
Very simple deno script to test that deno works
View task.ts
console.log('Fetching...')
const res = await fetch('https://example.com')
const body = new Uint8Array(await res.arrayBuffer())
console.log('---')
await Deno.stdout.write(body)
console.log('---')
console.log('Done.')
@myobie
myobie / server.ts
Created Jul 29, 2021
Very simple deno webserver mostly exactly from the example in the docs at https://deno.land/manual/examples/http_server
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
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
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