Skip to content

Instantly share code, notes, and snippets.

View wmertens's full-sized avatar

Wout Mertens wmertens

View GitHub Profile
@wmertens
wmertens / wrap-sqlite-backup.sh
Last active October 6, 2022 09:52
Wrap a command with an SQLite read lock on given files so they can safely be backed up
#!/usr/bin/env bash
#
# This script wraps a command with a read lock on given SQLite databases
# Example: wrap-sqlite-backup.sh *.sqlite3 -- tar cvzf backup.tgz *.sqlite3
#
# Note that it doesn't check for errors, so it really only works in WAL mode.
# Updates that read from ${COPROC[0]} to check errors welcome.
#
# Wout.Mertens@gmail.com
# https://gist.github.com/wmertens/4df207197074f9cb93f003c1cb723b4c
@wmertens
wmertens / node-require-debug.js
Created February 8, 2022 22:36
spy on NodeJS require stack
// Require this in your program, or load it with node's -r flag
const Mod = require('module')
const req = Mod.prototype.require
let indent = ''
const rootRE = new RegExp(process.cwd(), 'g')
const stack = []
Mod.prototype.require = function() {
try {
const request = arguments[0]
@wmertens
wmertens / ssh-crypt.bash
Last active February 2, 2024 10:01
ssh-crypt: bash function to encrypt using ssh-agent and openssl
#!/usr/bin/env bash
#
# ssh-crypt
#
# Bash function to encrypt/decrypt with your ssh-agent private key.
# Requires the commands gzip, ssh-add, ssh-keygen and openssl.
#
# Uses bash-specific extensions like <<<$var to securely pass data.
#
# Wout.Mertens@gmail.com 2021-11-11 - MIT Licensed
@wmertens
wmertens / nix-store-in-git.md
Last active September 7, 2022 19:57
Store Nix store in git

Store Nix store in git

The Nix store is an immutable file tree store, addressed by output hashes ($outhash-$name or $out), but could also be tweaked to be addressed by the content hash of the file tree ($cas).

Git is a content-addressed immutable object store. It is optimized for storing files as blobs and directories as tree objects.

Here we consider using a git repository for deduplicating, compressing and distributing the Nix store. We want to achieve maximum deduplication so we extract small changes from files before storing, so they become more similar.

Method

@wmertens
wmertens / BoundRoute.jsx
Created July 28, 2019 13:04
BoundRoute treats a URL like a React bound input
// This behaves like an input field with value and onChange, except that onChange can be called on mount
// Pass defaults so it knows which value keys to encode/decode
// Steps:
// Initial mount, or location changes: start at 1
// Value changes: start at 4
// 1. parse match into parsed
// 2. parse search, merge into parsed
// 3. post-process parsed => value; onChange(value)
// 4. pre-process value => toStore
// 5. create new pathname from toStore or keep current. This can mutate toStore
@wmertens
wmertens / use-id.js
Created July 22, 2019 22:04
Hook for consistent auto-incrementing ids between SSR and browser. To use, wrap your app with <IdProvider> and call useId or useGetId
import React, {createContext, useContext, useRef} from 'react'
import PropTypes from 'prop-types'
const Id = createContext()
const useIdGetter = (prefix = 'id') => {
const ref = useRef()
if (!ref.current) {
const me = {id: 0, get: () => `${prefix}-${me.id++}`}
ref.current = me
@wmertens
wmertens / makeSchema.js
Created November 14, 2018 09:43
Wrap graphql schema with enforced admin-only for mutations, timing etc
/* eslint-disable max-depth, no-console */
// TODO create adminSchema, leave adminQueries and mutations out of schema
// In the graphqlhandler, pass the right schema depending on isAdmin
import debug from 'debug'
import {get} from 'lodash'
import {GraphQLSchema, GraphQLObjectType} from 'graphql'
import ssrCache from 'stratokit/prerender/cache'
import {adminOnly} from './utils'
import {maskErrors} from 'graphql-errors'
@wmertens
wmertens / slateToReact.js
Created July 6, 2018 05:17
Convert Slate JSON object to React element tree
@@ -0,0 +1,62 @@
import React from 'react'
import RULES from './somewhere' // What you normally feed slate-html-serializer
const rules = [
...RULES,
// from slate-html-serializer
{
serialize(obj, children) {
if (obj.object === 'string') {
@wmertens
wmertens / App.jsx
Created January 27, 2018 16:05
Use svg symbols in React, works without changes with SSR
import arrow from 'symbol-loader!arrow.svg'
const App = () => (
<SymbolProvider>
<svg><Use symbol={arrow} /></svg>
</SymbolProvider>
)
@wmertens
wmertens / graphqlExpress.js
Created September 7, 2017 13:59
Add dataloaders to graphql context
import DataLoader from 'dataloader'
import {graphqlExpress} from 'graphql-server-express'
import schema from './schema'
import db from './database'
const contextFromReq = req => {
const {session} = req
const loaders = {}
const getLoader = (id, makeGetter) => {