This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Crypto PseudoRandom Shuffle: one right way to randomize, esp for gaming | |
// https://replit.com/@jeremybradbury/CSPRNG-Fisher-Yeats-Shuffle | |
// NodeJS - CJS version (backward comaptible) | |
const crypto = require("crypto"); | |
// the secret sauce | |
const getRndInt = (bytes) => | |
parseFloat( | |
parseFloat(crypto.randomBytes(bytes).toString("hex"), 16).toString(10), | |
10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// written for NodeJS v12 | |
const https = require("https"); // core node | |
const http = require("http"); // core node | |
const qs = require("querystring"); // core node | |
const isProd = process.env.NODE_ENV === "production"; | |
const debug = process.env.DEBUG; | |
const request = async ( | |
url = "", // string | |
options = {}, // any, or see: https://nodejs.org/api/http.html#new-agentoptions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- source oneliner - all caps letters + 10 digits - base 36 ~2.17B enums | |
SELECT array_to_string(array(select substr('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',((random()*(36-1)+1)::integer),1) from generate_series(1,6)),'') AS "Code"; | |
/* | |
Code | |
-------- | |
EY25QI | |
(1 row) | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a very basic Winston replacement, which combines well with a systemd service, to replace most features | |
const isProd = process.env.NODE_ENV === "production"; | |
const logLog = (level, ...e) => { | |
switch (true) { | |
case e && !!e.find((o) => o.redact): // params contain an object with a key called redact: {redact: true} | |
case isProd && level === "dir": | |
case isProd && level === "log": | |
case isProd && level === "info": |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// blacklist Mozilla/Opera (all browsers) prefix, with optional overrides | |
export const noBrowsersMiddleware = async (req, res, next) => { | |
const client = req.headers["user-agent"]; | |
switch (true) { // yeah it's still faster than if/else | |
// optional overrrides by platform | |
// mobile / tablet | |
//case client.includes('Android') && !client.includes('Windows'): | |
//case client.includes('iPhone'): | |
//case client.includes('Windows Phone'): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | |
'use strict'; | |
const asn1 = exports; | |
asn1.bignum = require('bn.js'); | |
asn1.define = require('./asn1/api').define; | |
asn1.base = require('./asn1/base'); | |
asn1.constants = require('./asn1/constants'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// you'll likely want the pool creation in another file & imported here instead of a new pool | |
const pool = new require("pg").Pool(); // left here for completeness | |
// import { pool } from "../db" | |
// usage example: `upsertRow("profile", {id:"999", bio: "I like to..." image: "https://..."})` | |
const upsertRow = async (table, row = {}) => { | |
try { | |
const fields = Object.keys(row); | |
const values = Object.values(row); | |
let q = `INSERT INTO ${table} (${fields.reduce((x, y) => `"${x}", "${y}"`)}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//// | |
// MIT License | |
// Copyright (c) 2021 Jeremy Bradbury | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getWherePhraseFromObj = (where={}) => { | |
// input {userid: 11, type: 'admin'} | |
// output {wherePhrase: 'WHERE userid = $1 AND type = $2', values:[11,'admin']} | |
const keys = Object.keys(where); | |
const values = Object.values(where); | |
console.info({keys, values}); | |
let wherePhrase=''; | |
for (let i=0; i < keys.length; i++) { | |
if (! wherePhrase) { | |
wherePhrase = 'WHERE '; // first time: i === 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { | |
generateKeyPairSync, | |
privateDecrypt, | |
publicEncrypt, | |
constants: { RSA_PKCS1_OAEP_PADDING }, | |
} = require("crypto"); // core node | |
const { | |
CRYPTIC_PASSPHRASE, // set to enable for your private key (changes require new keys) | |
CRYPTIC_GENERATE_KEYS, // set CRYPTIC_GENERATE_KEYS=1 to write key files | |
} = process.env; |
NewerOlder