Snippets

Recent Snippets

Creating a new user with an SSH key on Linux

First, create a new user with useradd: sudo useradd -m -d /home/username -s /bin/bash username Next, set the user’s password: passwd username Then, copy the contents of the user’s public key into /home/username/.ssh/authorized_keys. This is a plain text file where you can paste one public key per line. After that, set up the correct permissions... […]

How to setup and deploy a web application on Dokku

Dokku is a lightweight, Heroku-like utility for deploying web applications. While there is a quickstart, web-based UI for getting started, there are a bunch of steps you’ll need to go through in order to get your application deployed. Create the app The first step is to SSH into the droplet and create the application. I’ll... […]

How to regain Jenkins web access after being locked out

Jenkins is a fantastic continuous integration tool that I’ve used on a variety of projects. One of its problems, however, is that sometimes authentication can get messed up (frequently via an authentication plugin) and you can end up locked out of the web interface. As long as you have access the Jenkins host, though, it’s... […]

Create TypeScript declarations from JavaScript and JSDoc

For a variety of reasons, I prefer to write my code in vanilla JavaScript with JSDoc comments rather than writing in TypeScript. Today’s smart code editors, like Visual Studio Code, are able to read type information from JSDoc and provide Intellisense options as you code. However, those consuming your code may not get the same... […]

How to read environment variables in Deno using JavaScript

The way to read environment variables in Deno has changed from its early incarnation. Originally, there was a Deno.env() function that returned an object containing all of the defined environment variables. For v1.0.0, Deno switched to a Deno.env object with a get() method. First, in order to read environment variables, you must run your code... […]

How to validate the signature of a GitHub webhook using Node.js

When you have GitHub send a webhook to your server, you can optionally specify a secret that is used to generate a signature for the webhook request. It’s highly recommend to use the secret to check the signature of the payload to ensure it’s actually coming from GitHub. You’ll need to generate a signature based... […]

How to generate ID attributes in headings using Eleventy

When switching from Jekyll to Eleventy, I discovered that my headings no longer had id attributes in the rendered HTML. This meant that I couldn’t link directly to headings in my (sometimes very long) pages. After searching through the Eleventy documentation, I discovered that this wasn’t supported in Eleventy and I’d need to use a... […]

How to optionally await a JavaScript function call

Even though async functions and the await operator quickly became popular in JavaScript, many use await solely with function calls, such as: const result = await readSomeFile(); That works fine when you know the function you’re calling returns a promise. Actually, it works fine even without the function returning a promise as await will wrap... […]

Setting up Visual Studio Code intellisense for Jest globals

One of the benefits of Visual Studio Code is it’s ability to automatically detect the type of JavaScript value you’re working with and provide autocomplete (aka intellisense) for properties and methods. This works really well whenever you’re using a module system and explicitly importing values from modules. If a framework happens to add random global... […]

Reading streams with promises in Node.js

If you’ve tried to use the Node.js fs.promises API, you might have noticed that there doesn’t appear to be a cross-platform way of using fs.readFile() to read from process.stdin (such as process.stdin). Using the old, callback-based API, you could pass in a numeric file descriptor, such as: import fs from "fs"; const text = fs.readFileSync(0,... […]

Flattening a JavaScript array with a generator

ECMAScript 2019 will introduce the Array.prototype.flat() method to aid in flattening out multidimensional arrays in JavaScript. However, flattening arrays has been easy in JavaScript every since ECMAScript 6 (2015) introduced generators. Because generators can yield values in any order, not just linearly, it’s possible to use a recursive generator to easily flatten an array. Here’s... […]

Special formatting for SVG images with CSS

I frequently run into a problem displaying SVG images in my blog posts. SVG images tend to have no background color (so the post background bleeds through) and don’t have any space between the artwork and what would be the border. This left the SVG images looking a bit squished and washed out even though... […]

Posting to Medium using Node.js and fetch()

You can use the Medium REST API to post new articles on Medium. They previously had a Node.js SDK to make using the API a bit simpler, but that SDK is no longer supported. Rather than relying on an unsupported SDK, you can call the REST API endpoints directly using the node-fetch package. The following... […]