Skip to content

Instantly share code, notes, and snippets.

View robertknight's full-sized avatar

Robert Knight robertknight

View GitHub Profile
@robertknight
robertknight / private-memory-stats.py
Last active July 20, 2021 11:45
Estimate private memory usage of a list of processes
import os
import sys
def process_private_memory(pid):
smaps_path = f"/proc/{pid}/smaps"
total = 0
for line in open(smaps_path, 'r'):
field, val = [val.strip() for val in line.split(':')]
if field != "Private_Dirty":
@robertknight
robertknight / generate-svg-components.js
Last active July 19, 2021 08:22
Generate Preact components that render SVG icons from SVG markup
/* global process */
'use strict';
const fs = require('fs');
const { basename } = require('path');
const escapeHtml = require('escape-html');
const htmlParser = require('htmlparser2');
const prettier = require('prettier');
@robertknight
robertknight / hypothesis-client-wcag-a11y.md
Last active February 25, 2020 13:16
Hypothesis client remaining a11y work

This is a brain-dump of remaining work to achieve a fully accessible [1] Hypothesis client.

[1] fully accessible means that you can use all the functionality with a keyboard (or keyboard-emulating input device) and screen reader (or other assistive tech) conveniently and that it meets (both in letter and spirit) all the WCAG criteria. That doesn't necessarily mean that everything is as fully optimized as it could be (eg. in terms of what keyboard shortcuts are available to speed up various workflows).

Development tasks:

  1. Make it possible to activate highlights with the keyboard
  2. Make it easier to execute Annotate / Highlight commands using the keyboard
@robertknight
robertknight / reviewing-dependency-updates.md
Last active February 3, 2020 20:42
How I review dependency updates

How I review dependency updates

These are some notes on my process when I review dependency updates for both JavaScript and Python projects at Hypothesis.

Before you add a dependency

Before adding a dependency to a project, consider the impact on future maintenance. If what the dependency does for the project can be implemented with only a few lines of code, or can be implemented in an alternative way using dependencies the project already has, it may be a better choice to do that and avoid the dependency.

Dependency update process

@robertknight
robertknight / gen_charmap.py
Last active November 1, 2019 14:01
Optimized charmap generation
import sys
import time
import unicodedata
def gen_charmap_new():
current_cat = None
current_cat_start = None
ranges = []
@robertknight
robertknight / remove-future-imports.py
Created October 3, 2019 09:37
Remove __future__ imports from Python files
import sys
files = sys.argv[1:]
for path in files:
print(f"Updating {path}")
out_lines = []
prev_line_was_future_import = False
with open(path) as file:
@robertknight
robertknight / README.md
Last active September 12, 2019 04:40
Chrome range request + HTTP cache interaction issue

Steps to reproduce:

  1. Save range-request.html locally and open it in Chrome
  2. Clear your browser cache by going to chrome://settings/clearBrowserData, checking "Cached images and files" and clicking "Clear data"
  3. Open the devtools and make sure that the "Disable cache" option is not checked in the Network tab.
  4. Click the "Fetch file with Range requests" button. Observe the console logs and wait for the complete file to be fetched.
  5. Click the "Fetch file with one request" button and observe the console logs.
  6. Click the "Fetch file with one request" button again and observe the console logs.

Expected result:

@robertknight
robertknight / google-drive-test.diff
Created August 22, 2019 10:21
Cross-site file fetching test in the Hypothesis LMS app
diff --git a/lms/static/scripts/frontend_apps/components/FilePickerApp.js b/lms/static/scripts/frontend_apps/components/FilePickerApp.js
index 1e32c81..a040a75 100644
--- a/lms/static/scripts/frontend_apps/components/FilePickerApp.js
+++ b/lms/static/scripts/frontend_apps/components/FilePickerApp.js
@@ -91,9 +91,11 @@ export default function FilePickerApp({
try {
setLoadingIndicatorVisible(true);
const { id, url } = await googlePicker.showPicker();
+ await googlePicker.fetchFile(id);
await googlePicker.enablePublicViewing(id);
@robertknight
robertknight / delay.diff
Created July 29, 2019 11:32
Add a short delay to action dispatches
diff --git a/src/sidebar/store/index.js b/src/sidebar/store/index.js
index 8051a738..1e725db5 100644
--- a/src/sidebar/store/index.js
+++ b/src/sidebar/store/index.js
@@ -71,6 +71,33 @@ function angularDigestMiddleware($rootScope) {
};
}
+/**
+ * Middleware which adds an artificial delay to all actions going through
@robertknight
robertknight / rate_limit_test.py
Last active November 16, 2018 17:44
Rate limiting test script
#!/usr/bin/env python3
from binascii import hexlify
import time
from random import randint
import requests
regular_url = "http://localhost:5000/_status"