Skip to content

Instantly share code, notes, and snippets.

@rpivo
rpivo / index.md
Last active Apr 29, 2021
Creating a JSON Walker Class
View index.md

Creating a JSON Walker Class

class JsonWalker {
  constructor(content) {
    this.content = content;
  }

  get firstChild() {
    return new JsonWalker(this.content.children[0]);
@rpivo
rpivo / index.md
Last active Apr 11, 2021
Creating a Node Event Logger
View index.md

Creating a Node Event Logger Class

Below is an example of a Node Event Logger class implementation that saves each event object that's emitted from an EventEmitter to a JSON array.

import fs from "fs";
import path from "path";

export default class EventLogger {
  constructor(emitter, logFileName) {
@rpivo
rpivo / index.md
Created Apr 11, 2021
Listing All Events Registered On a Node EventEmitter
View index.md

Listing All Events Registered On a Node EventEmitter

import http from "http";

const server = http.createServer((_, res) => {
  res.end();
});

console.log({ eventNames: server.eventNames()});
@rpivo
rpivo / index.md
Last active Apr 3, 2021
Piping Output From a Child Process to the Parent Process in Node
View index.md

Piping Output From a Child Process to the Parent Process in Node

You can pipe the output of a child process by setting the stdio property to "inherit" for any child process in node.

const child_process = require("child_process");

child_process.execSync("ls -a", { stdio: "inherit" });
@rpivo
rpivo / index.md
Last active Apr 1, 2021
Creating a GitHub Action Workflow to Update a Lambda Function on Push
View index.md

Creating a GitHub Action Workflow to Update a Lambda Function on Push

The below workflow will work with a Lambda function written in Node. It's set up to run on every push, but could be updated for other actions.

The workflow is also set up to run on the main branch.

on:
  push:
    branches:
@rpivo
rpivo / index.md
Created Mar 29, 2021
Bumping the Version of a Package with the NPM Version Command
View index.md

Bumping the Version of a Package with the NPM Version Command

Bump major version

npm version major

Bump minor version

npm version minor
@rpivo
rpivo / index.md
Last active Mar 28, 2021
Filtering Out Keys in an Object Using Json.Parse & json.stringify
View index.md

Filtering Out Keys in an Object Using JSON.parse & JSON.stringify

The examples below make use of JSON.parse's second argument (aka the reviver) and JSON.stringify's second argument (aka the replacer).

Keep Only Specific Top-Level Keys

const obj = {
  a: 1,
  b: 2,
@rpivo
rpivo / index.md
Last active Apr 4, 2021
Encrypting & Decrypting Sensitive Data With the Web Crypto API
View index.md

Encrypting & Decrypting Sensitive Data With the Web Crypto API

Chris Veness created a really great gist that shares code to encrypt and decrypt data using the Web Crypto API here.

This gist breaks down the code in that gist step by step, detailing a real-world scenario with actual data.

It might be helpful to have that gist open in another tab and use this gist to walk through each line of code.

One interesting use case for this would be to encrypt on a client, and then decrypt in the cloud with something like a Lambda function. In order to do this, you could use Node's latest version as of this gist, version 15, which includes a webcrypto module that's designed to be a Node implementation of the Web Crypto API. Lambdas can't use v15 by default -- however, you can create a custom Lambda layer that contains v15.

@rpivo
rpivo / index.md
Last active Mar 20, 2021
Using string.FromCharCode() to Read Byte Arrays In JavaScript
View index.md

Using string.FromCharCode() to Read Byte Arrays In JavaScript

string.FromCharCode() can be useful to convert a byte array into a readable string.

console.log(String.fromCharCode(65, 66, 67));
// ABC

Here's what this would look like when encoding a string with TextEncoder, and then converting this string back to readable text using fromCharCode:

@rpivo
rpivo / index.md
Last active Mar 18, 2021
Working With ArrayBuffers & DataViews in Javascript
View index.md

Working With ArrayBuffers & DataViews in Javascript

// creates an ArrayBuffer with a length of 16 bytes, or 128 bits
const buffer = new ArrayBuffer(16)

// this data cannot yet be read or written. We need a DataView to do that

// available type array views: