Skip to content

Instantly share code, notes, and snippets.

@ali-master
ali-master / crypto-subtle-hmac.ts
Created April 23, 2025 23:35
Javascript HMAC with Crypto Subtle example
import * as b64ArrBufferConvertor from "base64-arraybuffer";
const SECRET_HMAC_KEY =
"209C5BBE79E2752FD1E4E687AD410778DD098546CC9B15ECAB1AEB4F21A46EF2";
async function importHmacSecretKey(secret: string) {
return crypto.subtle.importKey(
"raw",
new TextEncoder().encode(secret),
{ name: "HMAC", hash: "SHA-256" },
false,
@ali-master
ali-master / README.md
Created April 4, 2025 17:38
setTimeout caps out at ~25 days. If you need a bigger timeout, use setBigTimeout.

Javascript Big Timeout

setTimeout caps out at ~25 days. If you need a bigger timeout, use setBigTimeout.

import { setBigTimeout } from "./mod.mts";

const FORTY_DAYS_IN_MILLISECONDS = 3.456e9;
@ali-master
ali-master / solution.js
Created January 14, 2025 21:06
Disable Right-Click Using JavaScript
document.addEventListener("contextmenu", function(event) {
event.preventDefault();
alert("Right-click is disabled on this website!");
});
// Disable Right-Click for Specific Elements Only
document.getElementById("protected-content").addEventListener("contextmenu", function(event) {
event.preventDefault();
});
@ali-master
ali-master / MinHeap.md
Created December 29, 2024 23:35
Top-K Data Structure in Javascript

1. Min-Heap Implementation in TypeScript

We first define a generic MinHeap class with the following operations:

  • size(): Returns the number of elements in the heap.
  • peek(): Returns the minimum (root) without removing it.
  • push(value): Inserts a value, keeping the heap structure.
  • pop(): Removes and returns the minimum (root).
  • heapifyUp() and heapifyDown(): Internal methods to maintain heap order.
@ali-master
ali-master / server-time-handler.ts
Created December 24, 2024 16:51
Javascript Server Time handler
import ko from 'knockout';
import {getLogger} from 'Util/Logger';
export const serverTimeHandler = {
computeTimeOffset(serverTimeString: string): void {
const timeOffset = Date.now() - new Date(serverTimeString).valueOf();
this.timeOffset(timeOffset);
this.logger.log(`Current backend time is '${serverTimeString}'. Time offset updated to '${this.timeOffset()}' ms`);
},
@ali-master
ali-master / farm.config.ts
Created December 22, 2024 00:47
Run Nestjs with Farm bundler
import { defineConfig } from "@farmfe/core";
export default defineConfig({
plugins: [
{
name: "NestJS",
priority: 0,
config(config) {
const inputFileEntry = Object.values(config?.compilation?.input || {})[0] ?? "src/main.ts";
const mode = config.compilation.mode ?? process.env.NODE_ENV ?? "development";
@ali-master
ali-master / DragAndDrop.jsx
Last active November 26, 2024 06:30
React Drag and Drop with indicator(Swap)
import React, {useState} from "react";
export const DragAndDrop = () => {
const cardData = [
{
id: 1,
image: "https://i.ibb.co.com/XxvZ2Kq/Logo.png"
},
{
id: 2,
@ali-master
ali-master / cluster.service.ts
Created November 4, 2024 20:35
NestJS clustering via NodeJS workers
import cluster from "node:cluster";
import { Injectable } from "@nestjs/common";
import { cpuCountSync } from "node-cpu-count";
const numCPUs = cpuCountSync();
@Injectable()
export class AppClusterService {
static register(callback: () => void): void {
if (cluster.isPrimary) {
@ali-master
ali-master / README.md
Created October 21, 2024 21:00
Macos Operation not permitted issue solution

I find a solution for this, in finder, if the hosts file has a lock on it, use this command to unlock it:

sudo chflags nouchg,noschg /etc/hosts

then it will be editable by root, so you can use sudo nano /etc/hosts.

BTY, you can find hosts file in finder by this command: open /etc.

@ali-master
ali-master / date-timezone-converter.ts
Created October 19, 2024 08:40
Javascript Date Timezone awareness utility
export const convertDate = (date: Date | string) => {
if (typeof date === "string") {
const d = new Date(date);
return d.toISOString().slice(0, 19).replace("T", " ");
}
return date.toISOString().slice(0, 19).replace("T", " ");
};
export const getNow = () => {
return new Date(convertDate(new Date()));