Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View bencmbrook's full-sized avatar

Ben Brook bencmbrook

View GitHub Profile
@bencmbrook
bencmbrook / createDefinedPropertyObject.ts
Last active December 18, 2023 23:19
createDefinedPropertyObject.ts
type NonUndefined<T> = T extends undefined ? never : T;
/**
* Create an object with a single property, if the value is not undefined.
* Guarantees you don't have `{ myKey: undefined }` in your object.
*
* @param keyName the name of the key
* @param value the value which may be undefined
* @returns an object which should be spread into a parent object
@bencmbrook
bencmbrook / scrollable.css
Last active January 18, 2023 00:42
hover scroll
/*******************
* Scrollbar
*******************/
/** Scrollbar style override */
.scroller {
overflow-x: hidden;
overflow-y: scroll;
border-color: rgba(0, 0, 0, 0);
transition-timing-function: ease-in-out;
@bencmbrook
bencmbrook / zlookup.gs
Last active July 19, 2022 16:24
Fuzzy lookup Google Sheets (extended from https://github.com/nyanlynntherazi/ZLOOKUP)
const exclusions = [
' Inc.',
', Inc.',
'The ',
'.com',
];
/**
* Exclude words from matching score. See exclusions above.
*/
@bencmbrook
bencmbrook / nativeLanguageNames.ts
Created July 29, 2021 01:08
Languages names, written in their native language
// local
import { LanguageKey } from './enums';
/**
* Language name should be written in own language
*/
export const nativeLanguageNames: Record<LanguageKey, string> = {
/* English */
[LanguageKey.En]: 'English',
/* Arabic */
@bencmbrook
bencmbrook / export-for-browser-and-node.js
Created August 1, 2020 17:28
Export shared code between Browser and Node (ESM/CJS dual export)
/* Export a file to be imported by both Node and Browser.
*
* module.exports = sharedFunction;
* AND
* export default sharedFunction;
*/
// shared-code.js
function sharedFunction(x) {
return x * 2;
@bencmbrook
bencmbrook / xhr-logger.js
Created August 14, 2019 05:26
Log XHR URLs on your site to build a CSP
(function(XHR) {
var open = XHR.prototype.open;
var send = XHR.prototype.send;
XHR.prototype.open = function(method, url, async, user, pass) {
this._url = url;
open.call(this, method, url, async, user, pass);
};
XHR.prototype.send = function(data) {
@bencmbrook
bencmbrook / ECDSA-keystore.js
Last active February 21, 2019 20:17
Generate ECDSA JWKs in JWKS keystore using ES384
// Generates an elliptic curve keypair for the purpose of signing JWTs with ECDSA.
const jose = require('node-jose');
const fs = require('fs');
const keystore = jose.JWK.createKeyStore();
async function addNewECDSAKey(kid, keystore) {
const kty = "EC";
const crv = "P-384";
@bencmbrook
bencmbrook / streamMem.js
Last active February 20, 2019 10:55
Check Node stream memory usage
// https://nodejs.org/api/process.html#process_process_memoryusage
let rssMax = heapTotalMax = heapUsedMax = externalMax = 0;
let rssMin = heapTotalMin = heapUsedMin = externalMin = Number.MAX_SAFE_INTEGER;
readable.on('data', () => {
const x = process.memoryUsage();
if (x.rss > rssMax) rssMax = x.rss;
if (x.heapTotal > heapTotalMax) heapTotalMax = x.heapTotal;
if (x.heapUsed > heapUsedMax) heapUsedMax = x.heapUsed;
@bencmbrook
bencmbrook / keybase.md
Created February 16, 2019 03:25
Keybase Verification

Keybase proof

I hereby claim:

  • I am bencmbrook on github.
  • I am bencmbrook (https://keybase.io/bencmbrook) on keybase.
  • I have a public key whose fingerprint is 29DF 17D4 2988 819D 2EDB FC38 67E3 C53B 8261 BC94

To claim this, I am signing this object:

@bencmbrook
bencmbrook / jwtES384.sh
Last active December 27, 2022 03:13
How to generate JWT ES384 key
# ECDSA using P-384 and SHA-384 (NIST curve, part of CNSA Suite, and approved to protect "top secret" systems)
# https://apps.nsa.gov/iaarchive/library/ia-guidance/ia-solutions-for-classified/algorithm-guidance/commercial-national-security-algorithm-suite-factsheet.cfm
# https://tools.ietf.org/html/rfc7518#section-3.4
# Generate private key
openssl ecparam -name secp384r1 -genkey -noout -out jwtES384key.pem
# Generate public key
openssl ec -in jwtES384key.pem -pubout -out jwtES384pubkey.pem