Skip to content

Instantly share code, notes, and snippets.

View timruffles's full-sized avatar

Tim Ruffles timruffles

View GitHub Profile
@timruffles
timruffles / next.md
Last active February 23, 2024 17:04
Next.js page request handling

How Next.js responds to a page request with HTML

We create the next request handler function via app.getRequestHandler(). This returns a standard express handler, so we use it like expressApp.get('*', middlewareA(), middlewareB(), nextApp.getRequestHandler()).

When the handler is invoked:

  • Server#handleRequest (next-server/server/lib/next-server.js)
    • Parses URL + query string if not already done
  • Server#run
  • Searches for matching route
@timruffles
timruffles / svg-html-entities.js
Created May 27, 2015 08:29
svg - supporting HTML entities, to avoid 'entity not defined' when you're exporting SVGs from the browser
// create a doctype that includes definitions for all HTML entities - http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
var doctype = '<?xml version="1.0" standalone="no"?>' +
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [ '
+ HTML_ENTITIES() + ' ]>';
function HTML_ENTITIES() {
return '<!ENTITY quot "&#34;"> <!ENTITY amp "&#38;"> <!ENTITY apos "&#39;"> <!ENTITY lt "&#60;"> <!ENTITY gt "&#62;"> <!ENTITY nbsp "&#160;"> <!ENTITY iexcl "&#161;"> <!ENTITY cent "&#162;"> <!ENTITY pound "&#163;"> <!ENTITY curren "&#164;"> <!ENTITY yen "&#165;"> <!ENTITY brvbar "&#166;"> <!ENTITY sect "&#167;"> <!ENTITY uml "&#168;"> <!ENTITY copy "&#169;"> <!ENTITY ordf "&#170;"> <!ENTITY laquo "&#171;"> <!ENTITY not "&#172;"> <!ENTITY shy "&#173;"> <!ENTITY reg "&#174;"> <!ENTITY macr "&#175;"> <!ENTITY deg "&#176;"> <!ENTITY plusmn "&#177;"> <!ENTITY sup2 "&#178;"> <!ENTITY sup3 "&#179;"> <!ENTITY acute "&#180;"> <!ENT
@timruffles
timruffles / ports.ex
Last active February 8, 2023 14:53
erlang ports bewlider me
defmodule LearningPorts do
def main do
parent = self()
# if we need to control STDIN, the fun begins:
# unfortunately we can't close the port's stdin without closing the port.
# so it'll block waiting forever for an EOF that'll never appear.
#
# so we use a wrapper program that allows us to specify a sentinel line
@timruffles
timruffles / google-sheets-formula.vb
Last active January 25, 2022 11:24
google sheets - uk stamp duty calculator, new rate (2015)
// put this into a cell and then name a range 'housePrice'
=MIN(MAX(0,housePrice-250000),250000-125000)*0.02 + MIN(MAX(0,housePrice - 250000), 925000-250000) * 0.05 + MIN(MAX(0,housePrice - 9250000), 1500000-925000) * 0.1
@timruffles
timruffles / operation.ts
Last active April 23, 2021 17:11
operation - like Result and various other ADTs in Haskell etc
export type Operation<T, E> =
typeof NotStarted |
typeof Waiting |
Complete<T> |
Failed<E>
export enum OperationState {
NotStartedState = 'NotStarted',
WaitingState = 'Waiting',
CompleteState = 'Complete',
@timruffles
timruffles / attack.md
Last active November 21, 2020 17:35
Chrome/Gmail attack received 11/03/2016. Not sure if the Chrome meta refresh + data:text,html technique is novel.

The following attack will display a "you've been signed out" page for GMail, and attempt to steal your account credentials.

DO NOT PUT ANY ACCOUNT CREDENTIALS INTO ANY TABS CREATED AFTER VISITING THESE LINKS :)

I received an email in my GMail inbox with a fake attachment image, styled to look like the real GMail attachment UI:

fake

This linked to a page that ended up displaying a fake "you've been signed out" link, via the data:text/html... URL feature of Chrome:

@timruffles
timruffles / count_functions_loc.sh
Created June 12, 2020 14:09
A bash script that counts go function and method lines of code (including whitespace and comments)
#!/bin/bash
#
# Usage: bash count_functions_loc.sh some/directory
set -euo pipefail
main() {
for f in $(find $1 -name '*.go' -not -name 'test_*.go'); do
count_funcs < "$f" | awk "{ print \"$f\", \$1, \$2 }"
done
@timruffles
timruffles / dyanmic_or_di_elixir.md
Last active June 11, 2020 04:23
Approaches to dependency-injection/dynamic dispatch in elixir

In many production systems you'll want to have one module capable of talking to many potential implementations of a collaborator module (e.g a in memory cache, a redis-based cache etc). While testing it's useful to control which module the module under test is talking to.

Here are the approaches I can see. The two points that seem to divide the approaches are their tool-ability (dialyzer) and their ability to handle stateful implementations (which need a pid).

Passing modules

Modules are first class, so you can pass them in. Used in EEx, where passed module must implement a behaviour.

@timruffles
timruffles / snake_camel_camel_snake.js
Last active May 21, 2020 21:58
snake to camel & visa-versa for JS
(function() {
var _e = {};
_e.isPlainObject = function(obj) {
return obj && obj.constructor === Object;
};
_e.camelToSnakeCase = function(string) {
return string.replace(/\B[A-Z]/g,function(word) {
return "_" + word.toLowerCase();
@timruffles
timruffles / tmux_perms_fix.md
Last active April 17, 2020 03:48
fix for tmux not starting up due to permissions issues

If you try to run tmux and get:

$ tmux
create session failed: : No such file or directory
$ strace -f -e trace=file tmux
...
[pid 15852] open("/dev/ptyp0", O_RDWR)  = -1 EACCES (Permission denied)

You don't have perms for the pseudoterminals. Add your user to the tty group