Skip to content

Instantly share code, notes, and snippets.

View khalidx's full-sized avatar
💭
Engineering for the ☁️

Khalid Zoabi khalidx

💭
Engineering for the ☁️
View GitHub Profile
@khalidx
khalidx / restart-mac-audio.md
Created October 7, 2022 14:00
Restart the audio process, for MacBook.

There's a bug on the MacBook Air (M1, 2020) where the audio process crashes or something, and no videos or audio will play.

Here's how to restart the audio process when that happens:

sudo pkill coreaudiod

# or, if the audio process is not responding at all
sudo pkill -9 coreaudiod
@khalidx
khalidx / hash-ring.md
Last active August 3, 2022 00:14
An implementation of a "hash ring" for partitioning with consistent hashes.

Usage

const hashRing = createHashRing({
  clients: [
    { address: 'localhost:5001' },
    { address: 'localhost:5002' },
    { address: 'localhost:5003' }
  ]
})
@khalidx
khalidx / required-form-input.md
Last active April 23, 2023 09:28
A simple way to visually indicate that a form input is required using plain HTML and CSS.

Following this guide will get you the red asterisks* beside form inputs, that you are used to seeing on forms across the internet.

Include the following CSS:

<style>
  label.required:after {
    content: " *";
 color: red;
@khalidx
khalidx / log-an-error.md
Created May 12, 2022 16:50
A quick way to log an error object in Node.js.

Obviously, we usually log errors to the console like:

console.error(error)

Problem is, this doesn't always show you all the properties of an error.

If you want to see those properties, use something like:

@khalidx
khalidx / exclude-type-from-array.md
Created March 2, 2022 05:52
Exclude a type from a TypeScript tuple or array.

The following block excludes the type undefined from a TypeScript tuple or array.

You can also replace undefined with something else to exclude another type, but YMMV, depending on the type and how you use it.

export type FilterUndefined<T extends unknown[]> = T extends [] ? [] :
  T extends [infer H, ...infer R] ?
  H extends undefined ? FilterUndefined<R> : [H, ...FilterUndefined<R>] : T
@khalidx
khalidx / ts-union-to-intersection.md
Last active January 15, 2022 08:38
Convert a TypeScript Union of Types to an Intersection of Types.

TypeScript: Union to Intersection

Convert a TypeScript Union of Types (something | somethingElse) to an Intersection of Types (something & somethingElse).

// See "MergO" from https://github.com/microsoft/TypeScript/issues/26058#issuecomment-553212998
export type UnionToIntersection<U extends object> =
  (U extends object ? (k: U) => void : never) extends (k: infer T) => void
    ? (T extends object ? T : object)
 : object
@khalidx
khalidx / dynamodb-item-size-limit.md
Last active January 12, 2022 09:42
Check if a payload you plan to store falls under the 400KB DynamoDB item size limit

DynamoDB Item Size Limit Checker

A quick utility for checking whether you should store an item in DynamoDB.

DynamoDB currently limits each item stored to a maximum of 400KB.

Continue reading to explore how to check if a payload you plan to store falls under the 400KB DynamoDB item size limit.

There are two approaches for this:

@khalidx
khalidx / docker-node.md
Created December 2, 2021 03:35
Quickly execute a node script in a fresh docker container

Read more about using the official Node.js docker image.

running a file

Quickly execute a node script in a fresh docker container using the command below.

The current directory where the command is run will be mounted into the container as a volume.

docker run -it --rm --name my-running-script \
@khalidx
khalidx / cloudfront-edge-basic-auth.md
Last active November 12, 2021 08:24
Restricting access to an AWS CloudFront distribution using HTTP Basic Auth

A simple, low cost, fast, and secure way to restrict access to an AWS CloudFront Distribution using a CloudFront Edge Function and HTTP Basic Auth.

/**
 * This function is for a viewer request event trigger.
 * Choose viewer request for event trigger when you associate this function with a distribution.
 */
function handler(event) {
  if (!event.request.headers.authorization) return respond401();
 var auth = event.request.headers.authorization.value;
@khalidx
khalidx / generate-file.md
Created September 21, 2021 23:28
Generate a file of a specific size

generating a file of a specific size

mac

mkfile -n 2g ~/some/file.txt

linux