Skip to content

Instantly share code, notes, and snippets.

@tsmx
tsmx / git-build-nodejs-coveralls.yml
Last active November 22, 2020 20:14
Building a NodeJS app with GitHub actions and report test results to Coveralls.
# save as ./github/workflows/git-ci-build.yml
# make sure that 'test-coverage' generates the coverage reports (lcov)
name: git-ci-build
on:
[push]
jobs:
build:
@tsmx
tsmx / nodejs-promise-basic-example.js
Last active November 11, 2020 20:10
NodeJS Promise example showing basic usage and control flow with: resolve, reject, then, catch & throw.
function isOdd(x) {
return new Promise((resolve, reject) => {
if (x == 11) throw new Error('11 is not allowed!');
if (x % 2) {
resolve(x.toString() + ' is odd')
}
else {
reject(new Error(x.toString() + ' is even'));
}
});
@tsmx
tsmx / activate-gitignore.sh
Created November 9, 2020 19:31
Activate a changed gitignore file if it is not recognized
git rm -r --cached .
git add .
@tsmx
tsmx / got-download-stream.js
Last active October 6, 2022 13:40
Stream-download a file with filename from response header (content-disposition) via got package.
const got = require('got');
const stream = require('stream');
const fs = require('fs');
const { promisify } = require('util');
const pipeline = promisify(stream.pipeline);
// instantiate the download stream - use options to set authorization header etc. if needed
let downStream = got.stream('https://example.com/download');
downStream.on('response', response => {
@tsmx
tsmx / got-authorization-header.js
Last active August 2, 2022 13:43
Set authorization header with bearer-token in got.
const got = require('got');
// get 'url' and 'token'...
got.get(url), {
responseType: 'json',
hooks: {
beforeRequest: [
options => {
options.headers['Authorization'] = 'Bearer ' + token;
@tsmx
tsmx / apache-benchmark-automation.sh
Created October 25, 2020 20:26
A simple automation script for Apache Benchmark with bearer token authentication. Running multiple scenarios of requests/concurrency.
#!/bin/bash
# set URL to test here
url=YOUR_URL_HERE
# set test iterations here: requests|concurrent
declare -a iterations=("1|1" "10|10" "100|20" "100|100" "1000|50" "1000|100")
# first argument is the bearer token
if [ -z "$1" ]
@tsmx
tsmx / mongoose-encryption-at-rest.js
Last active October 10, 2020 18:55
Encryption-at-rest in MongoDB with Mongoose getters/setters and NodeJS Crypto.
const crypto = require('crypto');
const algorithm = 'aes-256-cbc';
const dbURI = 'mongodb://localhost/encryptiontest';
const dbOptions = {
user: 'encryptiontest',
pass: 'test',
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
@tsmx
tsmx / json-to-html-list.js
Created September 23, 2020 11:02
Create a HTML list from a JSON object. Supports simple nested objects and flat arrays.
function jsonToHtmlList(obj, level = 0) {
if (level == 0) {
console.log('<ul>');
}
else {
console.log((' ').repeat(level) + '<ul class=\"nested\">');
}
Object.entries(obj).forEach(([key, val]) => {
if (val !== null && typeof val == 'object' && !Array.isArray(val)) {
console.log((' ').repeat(level) + ' <li class=\"caret\">Key: ' + key + '</li>')
@tsmx
tsmx / json-traverse.js
Last active September 22, 2020 19:32
Traversing a JSON object in JavaScript for printing it out or applying functions/filters to every key/value - tracks the full path of every element, supports arrays, nested objects, arrays-in-arrays and objects-in-arrays
// the sample simply logs every item with its corresponding path
// replace 'logKeyValue' with custom functions/filters/... that should be applied
function traverse(obj, path = []) {
Object.entries(obj).forEach(([key, val]) => {
if (val !== null && typeof val == 'object') {
if (Array.isArray(val)) {
for (let i = 0; i < val.length; i++) {
let elem = val[i];
let itemKey = createKeyString(i, true);
@tsmx
tsmx / streamutils.js
Last active November 9, 2020 19:14
NodeJS: convert stream to buffer and vice versa
const Readable = require('stream').Readable;
// stream-to-buffer promise variant
module.exports.streamToBufferPromise = function (stream) {
return new Promise((resolve, reject) => {
var bufs = [];
var buffer = null;
stream.on('error', (error) => { reject(error); });
stream.on('data', (data) => { bufs.push(data); });
stream.on('end', () => {