Skip to content

Instantly share code, notes, and snippets.

View ali-master's full-sized avatar
🎯
Focusing on Something new!

Ali Torki ali-master

🎯
Focusing on Something new!
View GitHub Profile
@mberneti
mberneti / retryDynamicImport.ts
Last active October 17, 2024 09:24
This utility function retryDynamicImport enhances React’s lazy loading mechanism by adding retry logic with a versioned query parameter. It retries importing a component multiple times in case of failure, which can be useful for bypassing browser cache or dealing with intermittent network issues. It can be used as a drop-in replacement for React…
// Usage:
// Replace React.lazy(() => import('x'));
// with retryDynamicImport(() => import('x'));
import { ComponentType, lazy } from 'react';
const MAX_RETRY_COUNT = 15;
const RETRY_DELAY_MS = 500;
// Regex to extract the module URL from the import statement
@myesn
myesn / logto-auth.guard.ts
Created October 20, 2022 08:52
NestJS Logto Auth Guard
import {
Injectable,
CanActivate,
ExecutionContext,
HttpStatus,
BadRequestException,
UnauthorizedException,
} from '@nestjs/common';
import { Request } from 'express';
import { ConfigService } from '@nestjs/config';
@getify
getify / 1-CalendarItem.js
Last active March 21, 2024 09:11
an illustration (non-trivial example) of many newer JS class features
// abstract class, not intended to be instantiated directly
class CalendarItem {
static #UNSET = Symbol("unset")
static #isUnset(v) {
return v === this.#UNSET;
}
static {
for (let [idx,msg] of [
"ID is already set.",
"ID is unset.",
@getify
getify / 1.md
Last active March 2, 2023 21:24
In defense of using blocks to create localized scope for variables... (part 1 of 2)
@kjmph
kjmph / A_UUID_v7_for_Postgres.sql
Last active October 6, 2024 14:54
Postgres PL/pgSQL function for UUID v7 and a bonus custom UUID v8 to support microsecond precision as well. Read more here: https://datatracker.ietf.org/doc/draft-peabody-dispatch-new-uuid-format/
-- Based off IETF draft, https://datatracker.ietf.org/doc/draft-peabody-dispatch-new-uuid-format/
create or replace function uuid_generate_v7()
returns uuid
as $$
begin
-- use random v4 uuid as starting point (which has the same variant we need)
-- then overlay timestamp
-- then set version 7 by flipping the 2 and 1 bit in the version 4 string
return encode(
@kpirliyev
kpirliyev / postgresql_13_replication.md
Last active August 14, 2024 20:33
PostgreSQL 13 Master-Slave Streaming Replication

PostgreSQL 13 Master-Standby Streaming Replication

PostgreSQL has various types of replication available and it could be a little bit confusing to figure out what has to be done to just configure a simple master-standby replicated database setup. Digging my way through documentation I decided to write my own little guide on how to setup simple replication in PostgreSQL 13.

How it works

Streaming replication works by streaming the contents of WAL (Write-Ahead Logging) file from one server to another. Each transaction is first written into the WAL file, so essentially every change on the primary server will be replicated on the standby server. Standby server could be used for read-only operations, but can't be written to. Ofcourse, transferring data over the network takes time and there is a small lag before data written to one server becomes available on the other. To guarrantee data consistency on both servers at the time of read operation we can enable synchronous replication mode. This way, datab

@ErfanMirzapour
ErfanMirzapour / TailwindCSS-CheatSheet.md
Last active September 9, 2024 17:01
Mapping CSS properties to equivalent tailwind classes
@sindresorhus
sindresorhus / esm-package.md
Last active October 18, 2024 07:59
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
function preprocessImage(canvas) {
const ctx = canvas.getContext('2d');
const image = ctx.getImageData(0,0,canvas.width, canvas.height);
blurARGB(image.data, canvas, 1);
dilate(image.data, canvas);
invertColors(image.data);
thresholdFilter(image.data, 0.4);
return image;
}
@mstorus
mstorus / cryptoSign.js
Created February 27, 2021 07:03
use browser subtle crypto to sign some text with hmac-sha1
async function cryptoSign(message) {
const key = await window.crypto.subtle.generateKey(
{
name: "HMAC",
hash: { name: "SHA-512" },
},
true,
["sign", "verify"]
);
const encodedText = new TextEncoder().encode(message);