Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View myobie's full-sized avatar

Nathan myobie

View GitHub Profile
@myobie
myobie / generate-importable-blob.ts
Created March 25, 2022 11:04
Generate Blob import from file on disk with deno
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 March 15, 2022 13:32
A cloudflare worker to remove the www subdomain and redirect
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 February 10, 2022 11:40
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.
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 August 2, 2021 15:09
My own promise-like object where I can type the rejection and I can resolve from the outside
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 July 30, 2021 10:10
Very simple deno script to test that deno works
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 July 29, 2021 18:25
Very simple deno webserver mostly exactly from the example in the docs at https://deno.land/manual/examples/http_server
// 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 18:32
I'm not saying this is a good idea, but I'm not not saying it
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 April 15, 2021 09:48
A pipeline for proxying large file uploads
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 April 8, 2021 11:42
GenServer for longpoll requests with backoff in case of errors
defmodule Example.Poller do
use GenServer
@backoff [
50,
100,
150,
250,
400,
650
import Foundation
public struct Identifier:
Codable, CustomStringConvertible, ExpressibleByStringLiteral,
Hashable, RawRepresentable
{
public let rawValue: [UInt8]
public let dataValue: Data
private let stringValue: String