Skip to content

Instantly share code, notes, and snippets.

View clshortfuse's full-sized avatar
😀
Reinventing the internet...

Carlos Lopez clshortfuse

😀
Reinventing the internet...
  • 12:18 (UTC -04:00)
View GitHub Profile
@clshortfuse
clshortfuse / builder.js
Created March 6, 2023 21:05
JS XML Parser and Builder
/**
* @template T
* @typedef {import('./index.js').TupleTree<T>} TupleTree<T>
*/
/**
* @template T
* @typedef {import('./index.js').TupleTreeEntry<T>} TupleTreeEntry<T>
*/
@clshortfuse
clshortfuse / JSONEventSource.js
Created January 14, 2023 06:15
EventSource extension with JSON parsing and FireFox workaround
/**
* @implements {EventSource}
*/
export default class JSONEventSource {
#closeCalled;
#url;
#initArgs;
@clshortfuse
clshortfuse / worker.js
Created November 8, 2022 18:03
runAsync for Web
import AsyncObject from './AsyncObject.js';
/** @type {AsyncObject<Worker>} */
const workerConstructor = new AsyncObject();
/** @return {Promise<Worker>} */
async function getConstructor() {
if (workerConstructor.hasValue()) {
return workerConstructor.value;
}
@clshortfuse
clshortfuse / tediouspool.js
Last active August 2, 2022 15:19
Tedious Connection Pool
import Connection from 'tedious/lib/connection.js';
/** @typedef {import('tedious').ConnectionConfig} ConnectionConfig */
/** @typedef {import('tedious').ConnectionError} ConnectionError */
/** @typedef {import('tedious').RequestError} RequestError */
const MIN_CONNECTION_POOL = 2;
const MAX_CONNECTION_POOL = 10;
const MAX_CONNECTION_IDLE = 5000;
const MAX_CONNECTION_RETRY_TIME = 30000;
@clshortfuse
clshortfuse / base64.js
Created July 11, 2022 00:35
es6 base64 encoder and decoder
const BIT_MASK_6 = 0b11_1111;
const BIT_MASK_8 = 0b1111_1111;
const CODEPOINT_SINGLE_LIMIT = 0x00_80;
const CODEPOINT_DOUBLE_LIMIT = 0x08_00;
const CODEPOINT_TRIPLE_LIMIT = 0x1_00_00;
const CODEPOINT_QUAD_LIMIT = 0x11_00_00;
const CODEPOINT_EXTRA_BYTE = 0b10 << 6;
const BASE64_CHAR_62 = '+';
@clshortfuse
clshortfuse / network.js
Created March 2, 2021 17:22
fetch() operations
export class NetworkError extends Error {
/**
* @param {string} message
* @param {number} status
*/
constructor(message, status) {
super();
this.status = status;
}
}
@clshortfuse
clshortfuse / customevent.js
Created October 26, 2020 17:22
EventTarget & CustomEvent Shims
/**
* @template T
* @typedef {Object} CustomEventShimPrivateFields<T>
* @prop {T} detail
* @prop {string} type
* @prop {any} target
* @prop {any} currentTarget
* @prop {number} eventPhase
* @prop {boolean} bubbles
* @prop {boolean} cancelable
@clshortfuse
clshortfuse / script.js
Created August 1, 2020 13:11
Dynamically load script as a Promise
/**
* @param {string} url
* @param {boolean} [useCredentials=false]
* @param {string} [integrity]
* @return {Promise<void>}
*/
export function loadScript(url, useCredentials, integrity) {
return new Promise((resolve, reject) => {
const scripts = document.head.getElementsByTagName('script');
for (let i = 0; i < scripts.length; i += 1) {
@clshortfuse
clshortfuse / verifier.js
Last active May 13, 2020 19:11
CSS Color 4 Verifier
const RGB_FUNCTION = 'rgba?';
const HSL_FUNCTION = 'hsla?';
const HWB_FUNCTION = 'hwb';
const CMYK_FUNCTION = 'device-cmyk';
const LAB_FUNCTION = 'lab';
const LCH_FUNCTION = 'lch';
const GRAY_FUNCTION = 'gray';
const COLOR_FUNCTION = 'color';
const FUNCTION_START = '\\(';
/** https://www.w3.org/TR/css-syntax-3/#consume-function */
@clshortfuse
clshortfuse / expressJWT-sample.js
Last active November 12, 2019 17:30
Express-Controlled JWT
import express from 'express';
import cors from 'cors';
import { sign, verify } from 'jsonwebtoken';
/**
* @typedef AuthToken
* @prop {string=} sub UserID
* @prop {number} iat Issuance date in seconds
* @prop {number=} exp Expiration date in seconds
*/