Skip to content

Instantly share code, notes, and snippets.

View patrickhulce's full-sized avatar
😎
Live, eat, breathe JS

Patrick Hulce patrickhulce

😎
Live, eat, breathe JS
View GitHub Profile
@patrickhulce
patrickhulce / client.js
Created November 1, 2023 15:46
How node request cancellation works.
/* eslint-disable */
const fetch_ = require('node-fetch');
const fetchWithTimeout = async (url, options, timeout) => {
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(url, {
...options,
@patrickhulce
patrickhulce / web-events.test.ts
Created July 12, 2023 02:25
Cheatsheet for web events.
import {ReadableStream} from 'node:stream/web';
// Custom logger that accumulates log statements
const logger = {
logs: [],
log(...messages) {
this.logs.push(messages.join(' '));
},
error(...messages) {
this.logs.push(`ERROR: ${messages.join(' ')}`);
@patrickhulce
patrickhulce / plate-checker.js
Created July 8, 2023 19:42
Checks for a particular license plate combination in Texas.
async function checkAvailability(plate: string): Promise<string> {
const encodedPlate = encodeURIComponent(plate);
const url = `https://www.myplates.com/api/licenseplates/passenger/carbon-fiber/${encodedPlate}?_=${Date.now()}`;
const response = await fetch(url, {
headers: {
accept: 'application/json, text/javascript, */*; q=0.01',
'accept-language': 'en-US,en;q=0.9',
Referer: 'https://www.myplates.com/design/personalized/passenger/carbon-fiber/',
'Referrer-Policy': 'strict-origin-when-cross-origin',
},
@patrickhulce
patrickhulce / typedoc.sh
Created February 15, 2022 22:13
Automatically generate typedoc markdown on multiple platforms.
#!/bin/bash
set -euxo pipefail
./pnpm exec typedoc \
--treatWarningsAsErrors \
--excludePrivate \
--gitRevision master \
--githubPages false \
--plugin typedoc-plugin-markdown \
@patrickhulce
patrickhulce / Dockerfile
Created February 11, 2022 19:47
Chromium hangs on screenshots when `--disable-frame-rate-limit` is used.
FROM node:16-buster
WORKDIR /app
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | tee /etc/apt/sources.list.d/google-chrome.list
RUN apt-get update
RUN apt-get install -y google-chrome-stable
COPY package.json ./
@patrickhulce
patrickhulce / microtasks.mjs
Created February 8, 2022 21:18
Microtask flush example
let x = 0;
new Promise(resolve => resolve()).then(() => {
x = 1
new Promise(resolve => resolve()).then(() => {
x = 2
})
})
await flushAllMicrotasks()
@patrickhulce
patrickhulce / resizer.js
Created April 19, 2021 20:01
Resize Image
function resize(imageDataUrl) {
return new Promise((resolve, reject) => {
const img = new Image()
img.addEventListener('error', reject)
img.addEventListener('load', () => {
try {
canvas.width = img.width
canvas.height = img.height
context.drawImage(img, 0, 0)
const imageData = context!.getImageData(0, 0, img.width, img.height)
@patrickhulce
patrickhulce / run.js
Created December 1, 2020 16:03
Get GitHub logins from commit hashes
const fs = require('fs');
const fetch = require('isomorphic-fetch');
async function go() {
const hashes = fs.readFileSync('hashes.txt', 'utf-8').split('\n');
for (const hash of hashes) {
if (!hash.trim()) continue;
const response = await fetch(`https://api.github.com/repos/GoogleChrome/lighthouse/commits/${hash}`);
const json = await response.json();
@patrickhulce
patrickhulce / mac-setup.sh
Last active December 2, 2022 04:03
GitHub Self-Hosted Runners
#!/bin/bash
# Non-scripted Tasks:
# - Configure device name in Preferences > Sharing
# - Enable Remote Login & Remote Management in Preferences > Sharing
# - Enable automatic login/disable password after sleep in Preferences > Security & Privacy > General
# - Disable screensaver/sleep in Preferences > Energy Saver
# - Disable spotlight indexing of home directory
# - Add a runner in GitHub UI to grab your token https://github.com/<org>/<repo>/settings/actions/runners/new
@patrickhulce
patrickhulce / server.js
Created April 7, 2020 16:49
Movies Interview Server
const _ = require('lodash')
const express = require('express')
const movies = require('./movies.json')
const actors = []
const roles = []
/**
* Documentation
*
* Installation: npm install express lodash morgan