Skip to content

Instantly share code, notes, and snippets.

Avatar
😃

David Wells DavidWells

😃
View GitHub Profile
View get-nested-prop-values-in-js-proxy.js
// 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 Aug 7, 2022
Using a javascript proxy as low code REST client
View javascript-proxy-as-rest-client.js
/* 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) {
View resolve-api-path-from-string.js
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 Mar 11, 2022
Simple tiny colored logged for CLIs
View simple-logger.js
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 Jan 17, 2022
Create Gist in browser with github token
View create-gist.js
// 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 {
View cron-utils.js
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',
View no-deps-url-parse.js
// 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 Dec 11, 2021
Async to sync messaging for webworkers
View How.md

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 Dec 2, 2021
Show warning just once via new Set()
View warn-once.js
// 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 Dec 1, 2021
Block confusing usernames
View forbidden-usernames.js
// 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',