Skip to content

Instantly share code, notes, and snippets.

View ydnar's full-sized avatar

Randy Reddig ydnar

View GitHub Profile
@ydnar
ydnar / pushrod.go
Last active August 29, 2015 14:08
V8 JavaScript middleware for Go (golang)
package main
import (
"fmt"
"net/http"
"os"
"reflect"
v8 "github.com/idada/v8.go"
"github.com/nbio/hitch"
@ydnar
ydnar / redirects.js
Last active September 12, 2023 10:14
Nuxt.js middleware to enable redirects to absolute URLs
import url from 'url'
import qs from 'qs'
export default function (ctx) {
fixRedirect(ctx)
const { route, redirect } = ctx
const { path, query } = route
// Redirect trailing slashes, preserving query string: /foo/ -> /foo
if (path.length > 1 && path.slice(-1) === '/') {
@ydnar
ydnar / simultaneous-transitions.vue
Created September 24, 2017 13:35
Simultaneous page transitions in Nuxt.js
<template>
<div class="container">
<h1>Flex Test</h1>
<div class="content" @click="show = (show + 1) % 3">
<transition name="fade">
<div v-if="show == 0" class="foo">
<h2>Foo</h2>
<p>One paragraph.</p>
</div>
</transition>
@ydnar
ydnar / index.es6.js
Last active September 16, 2022 10:52
Versioned documents with Firestore Cloud Functions
import * as admin from 'firebase-admin'
import * as functions from 'firebase-functions'
admin.initializeApp(functions.config().firebase)
const db = admin.firestore()
async function writeIntegerVersion(event) {
const ref = event.data.ref
if (ref.path.indexOf('/_versions/') >= 0) {
return false
@ydnar
ydnar / .circleci-config.yml
Last active January 14, 2021 11:02
Screenshot test artifacts on CircleCI with AVA + Puppeteer
version: 2
jobs:
build:
working_directory: ~/app
docker:
- image: circleci/node:8.7-browsers
environment:
NODE_ENV: production
steps:
- checkout
@ydnar
ydnar / EXPIRENX.txt
Last active January 12, 2018 21:58
Lua one-liner to implement an “EXPIRENX” command in Redis
DEL example.com
HSET example.com status reserved
EVAL "return redis.call('TTL',KEYS[1])>0 or redis.call('EXPIRE',KEYS[1],ARGV[1])" 1 example.com 111111
HGETALL example.com
TTL example.com
HSET example.com comment IANA
EVAL "return redis.call('TTL',KEYS[1])>0 or redis.call('EXPIRE',KEYS[1],ARGV[1])" 1 example.com 222222
@ydnar
ydnar / syncmap.go
Last active June 11, 2018 20:12
sync.Map with LoadOrCreate for idempotent deferred creation of expensive entries
package syncmap
import (
"sync"
)
// Map wraps a sync.Map to enable idempotent deferred creation of new values.
type Map struct {
sync.Map
}
// Thanks https://amorten.com/blog/2017/ios-bluetooth-and-microphone-input/
func configure() {
// ...
let session = AVAudioSession.sharedInstance()
let sampleRate = 48000.0
do {
@ydnar
ydnar / Makefile
Created January 28, 2020 17:30
Recursive make with positional arguments
# To make a sandwich:
# $ make <track> sandwich
# e.g.: make alpha sandwich
#
# This works by rewriting MAKECMDGOALS by stripping the argument from the list
# and recursively calling make with a variable assignment (e.g. track=alpha)
# and the remaining goal(s).
#
# The ifeq below ensures that the “real” goals aren’t executed unless the
# track variable is set. The @: command silences the “Nothing to be done”
@ydnar
ydnar / Makefile
Created March 17, 2020 15:31
Swift tools package
TOOLS := swiftformat protoc-gen-swift protoc-gen-grpc-swift
tools: $(TOOLS)
$(TOOLS): Package.swift Package.resolved
swift build -c release --product $@
cp $$(swift build -c release --show-bin-path)/$@ ~/bin/