Skip to content

Instantly share code, notes, and snippets.

View DavidWells's full-sized avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@DavidWells
DavidWells / get-nested-prop-values-in-js-proxy.js
Created February 8, 2022 00:10
Get nested prop values in JS proxy
// via https://stackoverflow.com/questions/46005706/get-the-path-to-the-accessed-value-in-a-nested-object
function wrap(o, fn, scope = []) {
const handler = {
set(target, prop, value, receiver) {
fn('set value in scope: ', scope.concat(prop))
target[prop] = value
return true
},
get(target, prop, receiver) {
fn('get value in scope: ', scope.concat(prop))
@DavidWells
DavidWells / javascript-proxy-as-rest-client.js
Last active May 12, 2024 14:24
Using a javascript proxy as low code REST client
/* Using a JavaScript proxy for a super low code REST client */
// via https://dev.to/dipsaus9/javascript-lets-create-aproxy-19hg
// also see https://towardsdatascience.com/why-to-use-javascript-proxy-5cdc69d943e3
// also see https://github.com/fastify/manifetch
// also see https://github.com/flash-oss/allserver
// and https://gist.github.com/v1vendi/75d5e5dad7a2d1ef3fcb48234e4528cb
const createApi = (url) => {
return new Proxy({}, {
get(target, key) {
@DavidWells
DavidWells / resolve-api-path-from-string.js
Created January 24, 2022 03:04
Resolve API URL from string
const { URL } = require('url')
const assert = require('assert')
const DEFAULT_BASE = 'https://api.github.com'
/* Zero dependency backward compatible url parser */
function parseUrl(url) {
const match = url.match(/^(https?)?(?:[\:\/]*)([a-z0-9\.-]*)(?:\:(\d+))?(\/[^?#]*)?(?:\?([^#]*))?(?:#(.*))?$/i)
return {
protocol: match[1] || '',
@DavidWells
DavidWells / simple-logger.js
Last active March 11, 2022 21:20
Simple tiny colored logged for CLIs
const process = require('process')
const styles = require('ansi-styles')
// via https://github.com/sindresorhus/is-unicode-supported/blob/main/index.js
function isUnicodeSupported() {
if (process.platform !== 'win32') return process.env.TERM !== 'linux'; // Linux console (kernel)
return Boolean(process.env.CI)
|| Boolean(process.env.WT_SESSION) // Windows Terminal
|| process.env.ConEmuTask === '{cmd::Cmder}' // ConEmu and cmder
|| process.env.TERM_PROGRAM === 'vscode' || process.env.TERM === 'xterm-256color' || process.env.TERM === 'alacritty';
@DavidWells
DavidWells / create-gist.js
Created January 17, 2022 00:46
Create Gist in browser with github token
// via https://github.com/nalgeon/sqlime/blob/3.37.2/js/gister.js
// https://twitter.com/simonw/status/1482845419637903366
// Github Gist API client
const HEADERS = {
Accept: "application/json",
"Content-Type": "application/json",
};
class Gister {
@DavidWells
DavidWells / cron-utils.js
Last active June 7, 2022 11:09
Cron utilities
const cronstrue = require('cronstrue')
// https://github.com/harrisiirak/cron-parser
const MONTHS = [null, 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
const DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
const dayNum = {
sun: '0',
mon: '1',
tue: '2',
wed: '3',
@DavidWells
DavidWells / no-deps-url-parse.js
Created January 2, 2022 07:33
Parse URL fallback
// https://github.com/shm-open/utilities/blob/master/src/url.ts?cool#L52
function parseURL(url, base) {
let rest = url;
const reProtocol = /^([a-z][a-z0-9.+-]*:)?(\/\/)?([\S\s]*)/i;
// extract protocol
const protocolMatch = reProtocol.exec(url);
let protocol = protocolMatch[1]?.toLowerCase() ?? '';
const hasSlashes = !!protocolMatch[2];
// eslint-disable-next-line prefer-destructuring
rest = protocolMatch[3];
@DavidWells
DavidWells / How.md
Created December 11, 2021 23:31
Async to sync messaging for webworkers

How Does It Work?

How Partytown's Sync Communication Works

Partytown relies on Web Workers, Service Workers, JavaScript Proxies, and a communication layer between them all.

  1. Scripts are disabled from running on the main thread by using the type="text/partytown" attribute on the <script/> tag.
  2. Service worker creates an onfetch handler to intercept specific requests.
  3. Web worker is given the scripts to execute within the worker thread.
  4. Web worker creates JavaScript Proxies to replicate and forward calls to the main thread APIs (such as DOM operations).
@DavidWells
DavidWells / warn-once.js
Created December 2, 2021 05:06
Show warning just once via new Set()
// https://github.com/satya164/warn-once/blob/main/index.js
const DEV = process.env.NODE_ENV !== "production";
const warnings = new Set();
function warnOnce(condition, ...rest) {
if (DEV && condition) {
const key = rest.join(" ");
if (warnings.has(key)) {
@DavidWells
DavidWells / forbidden-usernames.js
Created December 1, 2021 05:36
Block confusing usernames
// https://github.com/apify/apify-shared-js/blob/master/packages/utilities/src/utilities.ts#L188
/**
* List of forbidden usernames. Note that usernames can be used as apify.com/username,
* so we need to prohibit any username that might be part of our website or confusing in anyway.
*/
const FORBIDDEN_USERNAMES_REGEXPS = [
// Meteor app routes
'page-not-found', 'docs', 'terms-of-use', 'about', 'pricing', 'privacy-policy', 'customers',
'request-form', 'request-solution', 'release-notes', 'jobs', 'api-reference', 'video-tutorials',
'acts', 'key-value-stores', 'schedules', 'account', 'sign-up', 'sign-in-discourse', 'admin',