Skip to content

Instantly share code, notes, and snippets.

Matthew Miner mminer

Block or report user

Report or block mminer

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@mminer
mminer / MultiReadHttpServletRequest.java
Created Aug 21, 2019
Servlet request wrapper that allows reading request body multiple times.
View MultiReadHttpServletRequest.java
package com.matthewminer;
import org.apache.commons.io.IOUtils;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@mminer
mminer / lruCache.ts
Created Jul 4, 2019
Simple least-recently inserted cache.
View lruCache.ts
export default class LeastRecentlyInsertedCache<T> implements Iterable<T> {
private maxSize: number;
// Set objects maintain insertion order, making them appropriate for keeping track of when values
// are added relative to each other.
private set = new Set<T>();
constructor(maxSize: number) {
this.maxSize = maxSize;
}
@mminer
mminer / basicAuthMiddleware.js
Created Dec 12, 2018
Express middleware for HTTP Basic Authentication.
View basicAuthMiddleware.js
function basicAuthMiddleware(request, response, next) {
const { authorization = '' } = request.headers;
const [, credentials] = authorization.split(' ');
const decoded = new Buffer(credentials || '', 'base64').toString();
const splitIndex = decoded.indexOf(':');
const username = decoded.substring(0, splitIndex);
const password = decoded.substring(splitIndex + 1);
// TODO: supply valid values for username and password
if (username !== USERNAME || password !== PASSWORD) {
@mminer
mminer / InactivityReset.js
Created Dec 11, 2018
React component that runs an action after no user activity is detected.
View InactivityReset.js
import PropTypes from 'prop-types';
import { Component } from 'react';
const eventListenerOptions = { capture: true, passive: true };
// Events that signify that the user is still active.
const eventNames = ['keydown', 'mousedown', 'touchstart'];
class InactivityReset extends Component {
constructor(props) {
@mminer
mminer / RelativeTime.js
Created Dec 11, 2018
React component that displays an auto-updating relative time.
View RelativeTime.js
import moment from 'moment';
import PropTypes from 'prop-types';
import { Component, createElement as h } from 'react';
class RelativeTime extends Component {
constructor(props) {
super(props);
this.intervalId = null;
this.state = { output: moment(props.time).fromNow() };
this.updateOutput = this.updateOutput.bind(this);
@mminer
mminer / restore-docker-volumes.sh
Created Nov 26, 2018
Restores Docker volumes from tar archives.
View restore-docker-volumes.sh
#!/usr/bin/env bash
usage() {
cat <<EOF
Usage: $0 VOLUME [VOLUME ...]
Restore Docker volumes from tar archives.
EOF
}
@mminer
mminer / backup-docker-volumes.sh
Last active Nov 26, 2018
Back up Docker volumes to tar archives.
View backup-docker-volumes.sh
#!/usr/bin/env bash
usage() {
cat <<EOF
Usage: $0 VOLUME [VOLUME ...]
Back up Docker volumes to tar archives.
EOF
}
@mminer
mminer / notify_event.sql
Created Sep 18, 2018
PostgreSQL trigger function to send notifications when table changes.
View notify_event.sql
-- Adapted from http://coussej.github.io/2015/09/15/Listening-to-generic-JSON-notifications-from-PostgreSQL-in-Go/
CREATE OR REPLACE FUNCTION notify_event() RETURNS TRIGGER AS $$
DECLARE
data JSON;
notification JSON;
BEGIN
-- Skip notification if row doesn't actually change in UPDATE.
-- We can accomplish the same thing with a WHERE clause in the CREATE TRIGGER statement,
-- but only if the trigger watches exclusively for UPDATE events.
@mminer
mminer / withHashRouter.js
Created Aug 23, 2018
React higher-order component that provides basic hash-based routing.
View withHashRouter.js
// Usage:
//
// const YourComponent = ({ activePageHash }) => ...
// export default withHashRouter(YourComponent, ['#valid-page-1', '#valid-page-2']);
import { Component, createElement as h } from 'react';
// Falls back to the initial page if the hash specifies an unknown one.
function replaceHashWithValidPage(validPageHashes) {
if (!validPageHashes) {
@mminer
mminer / groupBy.js
Created Aug 21, 2018
Groups an array of objects by a common key.
View groupBy.js
function groupBy(values, key) {
return values.reduce((accumulator, value) => {
const id = value[key];
accumulator[id] = accumulator[id] || [];
accumulator[id].push(value);
return accumulator;
}, {});
}
You can’t perform that action at this time.