Skip to content

Instantly share code, notes, and snippets.

@craigphicks
craigphicks / extending-error.js
Last active August 12, 2019 23:46
How to extend error when prepending a message to an existing error which may or may not be instance of Error
function showDeep(t){
let o=t
do {
console.log(`--- ${o.constructor.name}`)
for (let k of Reflect.ownKeys(o)) {
let d = Reflect.getOwnPropertyDescriptor(o,k)
if (JSON.stringify(d.value)!==undefined) {
console.log(`${k} : ${JSON.stringify(d.value)}`)
if (k==='stack')
console.log("") // funky
@craigphicks
craigphicks / README.md
Last active August 16, 2019 22:52
Create a non-singleton class from a singleton - yargs

A singleton makes sense when it prevent multiple access to a resource than must not be accessed in that way. But sometimes a singleton design is used because it is sufficient for expected usage. But that may block expanded usage unecessarily.

ES6 dynamic import can import a singelton module into a closure allowing a singleton to become a non-singleton class. See the file yargs-class.mjs below.

The result of test-yargs0class.mjs -

argv[0]= {
@craigphicks
craigphicks / makeNonCryptoRandomAlphaNumString.js
Created September 14, 2019 18:26
A non-crypto js function for creating random alphanum strings - no dependencies
function makeNonCryptoRandomAlphaNumString(length) {
function tf(n) {
if (n < 26) return 65 + n
else if (n < 52) return 97 + n - 26
else return 48 + n - 52
}
var result = ''
for (var i = 0; i < length; i++) {
let idx = Math.floor(Math.random() * 62)
result += String.fromCharCode(tf(idx))
#!/bin/bash
# copyright craigphicks 2020 - free to use with due attribution
confirm_args(){
logger -t "confirm_args" -- "#=${#}"
for index in `seq 0 ${#}` ; do
eval item=\$$index
logger -t "confirm_args" -- "[$index] ${item}"
done
}
async function readStreamToEnd(rstream){
rstream.resume();
let prom=new Promise((resolve,reject)=>{
var buf = Buffer('');
rstream.on('data', function(newbuf) { buf=Buffer.concat([buf,newbuf]); });
rstream.on('end', function() { resolve(buf.toString()); });
rstream.on('error', function(e) { reject(e); });
});
return await prom;
}
@craigphicks
craigphicks / openssh-descrption-of-X11-forwarding.md
Created July 7, 2020 22:11
openssh descrption of X11 forwarding

https://www.openssh.com/features.html

X11 forwarding (which also encrypts X Window System traffic)

X11 forwarding allows the encryption of remote X windows traffic, so that nobody can snoop on your remote xterms or insert malicious commands. The program automatically sets DISPLAY on the server machine, and forwards any X11 connections over the secure channel. Fake Xauthority information is automatically generated and forwarded to the remote machine; the local client automatically examines incoming X11 connections and replaces the fake authorization

@craigphicks
craigphicks / chromium-bug-report-X11-wrong-auth.md
Last active January 10, 2022 01:04
chromium bug report: "X11 connection rejected because of wrong authentication."

Chrome Version : Chromium 83.0.4103.116 snap Other browsers tested: Add OK or FAIL, along with the version, after other browsers where you have tested this issue:

Firefox: OK, "Mozilla Firefox 78.0.1" Chromium (non-span): OK, "Chromium 83.0.4103.61 Built on Ubuntu , running on Ubuntu 18.04"

What steps will reproduce the problem?

@craigphicks
craigphicks / cli.js
Last active November 21, 2020 04:36
js server-client pair - echo text enter into client terminal
var net = require('net');
var jsesc = require('jsesc');
var ctrlC=false;
//var qwrite=[];
async function Socketing(sock){
return await new Promise((res,rej)=>{
// Event: 'data'
sock.on('data', (d) => {
console.log(`sock data: ${jsesc(Buffer.from(d).toString(),{es6:true})}`);
@craigphicks
craigphicks / custom-tsc.ts
Last active January 6, 2021 13:21
Typescript - How to compile only one (or more) files from a project using all the project settings from the tsconfig file(s)
import * as ts from 'typescript';
import * as fs from 'fs';
import * as path from 'path';
import * as cp from 'child_process';
import * as dm from '../src-ts/deep-merge-objects';
function compile(fileNames: string[], options: ts.CompilerOptions): void {
const program = ts.createProgram(fileNames, options);
const emitResult = program.emit();
const allDiagnostics = ts
@craigphicks
craigphicks / commonjs-index.js
Created July 26, 2021 06:52
comparison of index.js produced with module:CommonJs vs. module:es2020 under yamato_daiwa-es_extensions
(() => {
"use strict";
var e, t = {
5272: (e, t) => {
Object.defineProperty(t, "__esModule", {
value: !0
}), t.default = function(e, t) {
for (const [a, n] of e.entries())
if (t(n)) return a;
return null