Skip to content

Instantly share code, notes, and snippets.

View davidgilbertson's full-sized avatar

David Gilbertson davidgilbertson

  • Sydney, Australia
View GitHub Profile
const hasLocalStorage = typeof window !== 'undefined' && !!window.localStorage;
export const set = (key: string, data: any) => {
if (!hasLocalStorage) return undefined;
try {
const string = typeof data === 'string' ? data : JSON.stringify(data);
return localStorage.setItem(key, string);
} catch (err) {
console.clear();
(async () => {
const list = ['one', 'two', 'three'];
const doSlowly = msg => new Promise(resolve => {
setTimeout(() => {
resolve(`Processed ${msg}`);
}, 100);
});
const http = require('http');
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const PORT = 3001;
const STATIC_DIRECTORY = path.resolve(__dirname, '../build/');
const cache = {};
const getStaticFile = fileName => {
/*
Adapted from https://github.com/sindresorhus/github-markdown-css
The MIT License (MIT)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
@davidgilbertson
davidgilbertson / http2.js
Last active October 9, 2023 06:09
HTTP2 server with compression and caching
const http2 = require('http2');
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const brotli = require('brotli'); // npm package
const PORT = 3032;
const BROTLI_QUALITY = 11; // slow, but we're caching so who cares
const STATIC_DIRECTORY = path.resolve(__dirname, '../dist/');
const cache = {};
@jhaddix
jhaddix / cloud_metadata.txt
Last active April 30, 2024 09:38 — forked from BuffaloWill/cloud_metadata.txt
Cloud Metadata Dictionary useful for SSRF Testing
## AWS
# from http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html#instancedata-data-categories
http://169.254.169.254/latest/user-data
http://169.254.169.254/latest/user-data/iam/security-credentials/[ROLE NAME]
http://169.254.169.254/latest/meta-data/iam/security-credentials/[ROLE NAME]
http://169.254.169.254/latest/meta-data/ami-id
http://169.254.169.254/latest/meta-data/reservation-id
http://169.254.169.254/latest/meta-data/hostname
http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key
function logColor(color, args) {
console.log(`%c ${args.join(' ')}`, `color: ${color}`);
}
const log = {
aliceblue: (...args) => { logColor('aliceblue', args)},
antiquewhite: (...args) => { logColor('antiquewhite', args)},
aqua: (...args) => { logColor('aqua', args)},
aquamarine: (...args) => { logColor('aquamarine', args)},
azure: (...args) => { logColor('azure', args)},
@davidgilbertson
davidgilbertson / gzipped-size.js
Last active October 8, 2018 00:53
Get the size a file will be when gzipped
const fs = require('fs');
const zlib = require('zlib');
const file = fs.readFileSync('./some_file.js');
const rawSize = file.length;
const gzippedSize = zlib.gzipSync(file).length;
console.log('Raw size: ', (rawSize / 1000), 'KB');
console.log('Gzipped size: ', (gzippedSize / 1000), 'KB');
console.log('Compression: ', `${100 - Math.round(gzippedSize / rawSize * 1000) / 10}%`);
@davidgilbertson
davidgilbertson / Neato npx commands
Created November 22, 2017 21:06
npx is installed with npm 5.2. Here are some useful commands
# list lines of code by file type:
`npx sloc --format cli-table src/`
# print out update/version info about your project's packages
`npx npm-check`
# display a breakdown of package contents by size (for a CRA page)
`npx source-map-explorer build/static/js/main.*`
// Copy/paste this into your Dev Tools console while on https://medium.com/me/stats
// You can save it as a snippet in Chrome if you like: https://developers.google.com/web/tools/chrome-devtools/snippets
console.clear();
(() => {
console.info('Remember to scroll down first to load all your articles');
const formatPercent = rawValue => `${(rawValue * 100).toFixed(1)}%`;
const tableHeaderCell = document.querySelector('.sortableTableHeaders tr').insertCell();
tableHeaderCell.textContent = 'Fans/views';