Skip to content

Instantly share code, notes, and snippets.

View jasdeepkhalsa's full-sized avatar

Jasdeep Khalsa jasdeepkhalsa

View GitHub Profile
@jasdeepkhalsa
jasdeepkhalsa / horizontal-center-no-width.css
Created May 23, 2022 14:16
Center horizontally without using calc or a fixed width
.center {
postion: absolute;
left: 50%;
transform: translateX(-50%);
}
@jasdeepkhalsa
jasdeepkhalsa / stringifySafely.ts
Last active November 30, 2021 13:51
Stringify JavaScript Objects -> JSON or other parsers safely
// Adapted from the Axios HTTP library
// https://github.com/axios/axios/blob/76f09afc03fbcf392d31ce88448246bcd4f91f8c/lib/defaults.js#L29
/**
* Trim excess whitespace off the beginning and end of a string
*
* @param {String} str The String to trim
* @returns {String} The String freed of excess whitespace
*/
const trim = (str) => {
@jasdeepkhalsa
jasdeepkhalsa / video-pip-bookmarklet.js
Created April 7, 2021 20:45
Video Picture-In-Picture Bookmarklet
javascript:let video=document.getElementsByTagName('video')[0];video.removeAttribute('disablePictureInPicture');video.requestPictureInPicture()
@jasdeepkhalsa
jasdeepkhalsa / sdout.ts
Created March 15, 2021 16:30
Explicit output to SDOUT using JavaScript instead of using console.log
process.stdout.write('hello world')
@jasdeepkhalsa
jasdeepkhalsa / gcp-docker-k8s-jenkins.sh
Last active March 8, 2021 22:31
GCP with Docker, K8s & Jenkins
## This script covers
# * Running Docker containers on a host.
# * Storing Docker images in the Google Container Repository (GCR).
# * Deploying GCR images on Kubernetes.
# * Pushing updates onto Kubernetes.
# * Automating deployments to Kubernetes using Jenkins.
GCP_PROJECT_ID="qwiklabs-gcp-04-17810074a304"
# Clone a repository from the Google Cloud Shell
@jasdeepkhalsa
jasdeepkhalsa / index.html
Created March 5, 2021 12:44 — forked from igrigorik/index.html
XHR streaming example
<p>Hello
<script>
var xhr = new XMLHttpRequest();
xhr.open('GET', '/stream');
xhr.seenBytes = 0;
xhr.onreadystatechange = function() {
console.log("state change.. state: "+ xhr.readyState);
@jasdeepkhalsa
jasdeepkhalsa / docker-cmd.sh
Last active December 8, 2021 21:16
Docker power commands
# Stop all containers
docker stop $(docker ps -q)
# Remove all containers
docker rm $(docker ps -aq)
# Remove all images
docker rmi $(docker images -aq)
# See logs of a container
@jasdeepkhalsa
jasdeepkhalsa / gcp.sh
Last active February 27, 2021 18:13
GCP gcloud provisioning
## General commands
# Get active account name
gcloud auth list
# List the project ID
gcloud config list project
# Find default zone
gcloud compute project-info describe --project <GCP Project ID>
@jasdeepkhalsa
jasdeepkhalsa / aws-lambda-edge.json
Created February 5, 2021 17:33
Role -> Trust Relationship on Principle to enable Lambda@Edge for a given Lambda
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com",
"edgelambda.amazonaws.com"
]
@jasdeepkhalsa
jasdeepkhalsa / rejection-based-retry.ts
Created January 18, 2021 12:47
Exponential backoff retry strategy for async
// General purpose Rejection-based retrying
// Source: https://advancedweb.hu/how-to-implement-an-exponential-backoff-retry-strategy-in-javascript/
const wait = (ms) => new Promise((res) => setTimeout(res, ms));
const maybeFail = (successProbability, result, error) => new Promise((res, rej) => Math.random() < successProbability ? res(result) : rej());
const maybeFailingOperation = async () => {
await wait(10);
return maybeFail(0.1, "result", "error");
}