Skip to content

Instantly share code, notes, and snippets.

View rpivo's full-sized avatar

Ryan Pivovar rpivo

  • New York City, NY
View GitHub Profile
@rpivo
rpivo / index.md
Created March 18, 2021 01:41
Encoding & Decoding a String Using the TextEncoder & TextDecoder APIs

Encoding & Decoding a String Using the TextEncoder & TextDecoder APIs

const encoded = new TextEncoder().encode('foo')
const decoded = new TextDecoder().decode(encoded)
console.log({
  encoded,
  decoded,
})
@rpivo
rpivo / index.md
Last active March 18, 2021 23:52
Working With ArrayBuffers & DataViews in Javascript

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:
@rpivo
rpivo / index.md
Last active March 20, 2021 14:47
Using string.FromCharCode() to Read Byte Arrays In JavaScript

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 April 17, 2025 14:37
Encrypting & Decrypting Sensitive Data With the Web Crypto API

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 March 28, 2021 01:58
Filtering Out Keys in an Object Using Json.Parse & json.stringify

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
Created March 29, 2021 03:35
Bumping the Version of a Package with the NPM Version Command

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 April 1, 2021 09:51
Creating a GitHub Action Workflow to Update a Lambda Function on Push

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
Last active April 3, 2021 17:01
Piping Output From a Child Process to the Parent Process in Node

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
Created April 11, 2021 17:59
Listing All Events Registered On a Node EventEmitter

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 April 11, 2021 20:43
Creating a Node Event Logger

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) {