Skip to content

Instantly share code, notes, and snippets.

View terrisgit's full-sized avatar
🎯

Terris Linenbach terrisgit

🎯
View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@terrisgit
terrisgit / Makefile
Last active September 6, 2023 00:44
Python Pipfile-based Makefile
.NOTPARALLEL :
# To make sure the Python virtual environment will be placed in the
# .venv directory:
# export PIPENV_VENV_IN_PROJECT=1
#
# Get program help:
# make run
#
# Run the program:
@terrisgit
terrisgit / .zshrc
Created June 22, 2023 14:50
Git Branch in Prompt
# Prompt example: ~/dir [repo-name] main () %
autoload -Uz vcs_info
zstyle ':vcs_info:git*' formats "[%r] %b (%a) %m%u%c "
precmd() { vcs_info }
setopt prompt_subst
PROMPT='${PWD/#$HOME/~} ${vcs_info_msg_0_}%# '
@terrisgit
terrisgit / aws-upload.js
Last active June 2, 2023 20:19
AWS-SDK v3 Streaming Example
const { S3Client } = require('@aws-sdk/client-s3');
const { Upload } = require('@aws-sdk/lib-storage');
const { PassThrough } = require('stream');
/**
* A custom PassThrough stream for piping any sort of data in a
* backpressure-friendly way using Promises.
*
* Derives from PassThrough instead of Readble; otherwise, piping to
* files etc. won't work; for example, push(null) won't cause finished
@terrisgit
terrisgit / ResumePush.js
Last active June 6, 2023 15:22
A custom ReadableStream using back pressure and Promises for pushing data to NodeJS streams
/**
* Custom ReadableStream stream for piping data in a backpressure-friendly way using Promises
*
* @see {@link https://terrislinenbach.medium.com/obeying-back-pressure-in-javascript-streams-read-stream-pipe-edition-ccd6203596bf|How ReadStream responds to back pressure}
*
* Derives from PassThrough instead of ReadbleStream; otherwise, piping to files etc. won't
* work -- for example, push(null) won't fire 'finished' events.
*
* Usage:
* - Call push(obj) to write obj to the stream
@terrisgit
terrisgit / replace.sh
Last active June 2, 2023 20:04
sed: Find and Replace Regular Expression with Capture Groups Example
#! /bin/sh
# Complex string manipulation in Bash example
#
# Transforms a string in the form of: "archives/A/B C", to: A/B/B C
line='"A/B C",'
# Remove starting "
line="${line#\"}"
@terrisgit
terrisgit / example.js
Created January 27, 2023 16:32
Surround a command-line argument with apostrophes and escape the apostrophes in the value
// See https://stackoverflow.com/questions/1250079/how-to-escape-single-quotes-within-single-quoted-strings
const zipPath = "fo''o.zip";
const escapedZipPath = zipPath.replace(/'/g, "'\\''");
const cmd = `/usr/bin/unzip '${escapedZipPath}' > /dev/null 2> /dev/null`;
@terrisgit
terrisgit / removeCComments.js
Last active January 11, 2023 20:50
Remove C-style comments from a string in JavaScript
// Removes all /* C-style comments */ from a string
// Credit:
// https://blog.ostermiller.org/finding-comments-in-source-code-using-regular-expressions/
// However this fixes the input string /* comment one */ blah /* comment two */ by using nongreedy match
// This intentionally doesn't work with //-style comments because it was written for MySQL comments only
'some string'.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*?\*+\//g, '');
@terrisgit
terrisgit / reverse-truncate.sh
Last active July 29, 2023 16:11
A shell script that removes the beginning blocks of a file efficiently
#!/bin/sh
# Purpose
#
# This is a reverse truncation script intended for but not limited to log
# files. The oldest log entries are discarded.
#
# This script operates on bytes only -- not lines or even characters. It
# alters the sizes of specified files in place efficiently, but with a heavy
# hand. It removes the beginning bytes from a file until it is roughly the
@terrisgit
terrisgit / LoggingPolicy.js
Last active February 2, 2024 05:44
Express Gateway custom Policy for logging requests
const humanizeDuration = require('humanize-duration');
const Loggers = require('@goodware/log');
const { PassThrough } = require('stream');
const { ulid } = require('ulidx');
/**
* See https://github.com/expressjs/body-parser for more options
*/
const jsonParser = require('express').json({ limit: '10mb' });