Skip to content

Instantly share code, notes, and snippets.

View DavidWells's full-sized avatar
😃

David Wells DavidWells

😃
View GitHub Profile
const fetch = async (...args) => {
console.log(...args)
return {
statusCode: 200,
data: {},
}
}
function httpRequest(url, method, data, opts, cb) {
const init = { method }
@DavidWells
DavidWells / fetch-via-js-proxy.js
Last active February 8, 2022 17:02
Fetch pages via JS proxy with await www
// Via https://twitter.com/RReverser/status/1490873967577640961
function wwwProxy() {
return new Proxy(new URL('https://www/'), {
get: function get(target, prop) {
let orig = Reflect.get(target, prop);
if (typeof orig === 'function') return orig.bind(target);
if (typeof prop !== 'string') return orig;
if (prop === 'then') return Promise.prototype.then.bind(fetch(target));
target = new URL(target);
target.hostname += `.${prop}`;
@DavidWells
DavidWells / alter-function-args-with-proxy.js
Created February 8, 2022 01:04
Alter function args via JS proxy
// via https://blog.sessionstack.com/how-javascript-works-proxy-and-reflect-11748452c695
const p = new Proxy(function() {}, {
apply: function(target, thisArg, argumentsList) {
console.log('called: ' + argumentsList.join(', '));
return argumentsList[0] + argumentsList[1] + argumentsList[2];
}
});
console.log(p(1, 2, 3)); // This will print called: 1, 2, 3 and 6
@DavidWells
DavidWells / set-private-field-via-proxy.js
Created February 8, 2022 01:00
Set private object fields via JS proxy
// https://betterprogramming.pub/everything-you-should-know-about-javascript-proxy-67576f2e069e
function setPrivateField(obj, prefix = "_"){
return new Proxy(obj, {
has: (obj, prop) => {
if(typeof prop === "string" && prop.startsWith(prefix)){
return false
}
return prop in obj
},
ownKeys: obj => {
@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 October 6, 2023 18:39
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',