Skip to content

Instantly share code, notes, and snippets.

View ali-habibzadeh's full-sized avatar

Ali Habibzadeh ali-habibzadeh

View GitHub Profile
@ali-habibzadeh
ali-habibzadeh / nest-api-gateway-roles.ts
Created September 7, 2023 16:43
The TypeScript code sets up a role-based access control in a NestJS application using a custom guard. It defines a RolesGuard class that leverages metadata reflection to determine the necessary roles for accessing particular routes. It retrieves the user roles from the AWS API Gateway request context, specified in the headers, and checks if the …
import { Injectable, CanActivate, ExecutionContext } from "@nestjs/common";
import { SetMetadata } from "@nestjs/common";
import { Reflector } from "@nestjs/core";
export enum Role {
User = "user",
Admin = "admin",
}
export const Roles = (...roles: Role[]) => SetMetadata("roles", roles);
@ali-habibzadeh
ali-habibzadeh / api-cdk.ts
Created September 7, 2023 16:41
The TypeScript code uses AWS CDK to create an AWS API Gateway with a set of predefined REST API resources (like "signup", "login") each supporting specific HTTP methods. It sets up CORS, associates the API with a custom domain using a DNS certificate, and creates a DNS record in Route 53. The API integrates with a Lambda function handler and opt…
import {
AuthorizationType,
CognitoUserPoolsAuthorizer,
Cors,
CorsOptions,
EndpointType,
LambdaRestApi,
} from "aws-cdk-lib/aws-apigateway";
import { IFunction } from "aws-cdk-lib/aws-lambda";
import { Certificate, CertificateValidation } from "aws-cdk-lib/aws-certificatemanager";
@ali-habibzadeh
ali-habibzadeh / QA-Chat-Pinecone.ts
Last active September 7, 2023 16:35
Setup up a chat system using the GPT-3.5-turbo-16k model to respond to user queries. It initializes necessary components like a vector store and a QA chain, and includes a query method to fetch generated responses to questions.
import { OpenAI } from "langchain/llms/openai";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { BufferMemory } from "langchain/memory";
import { PineconeStore } from "langchain/vectorstores/pinecone";
import { ConversationalRetrievalQAChain } from "langchain/chains";
import { getPineconeClient } from "../utils/pinecone.utils";
import { PromptTemplate } from "langchain/prompts";
import { getConversationPromot, getQuestionPrompt } from "./prompt";
export class ConversationalRetrievalChat {
@ali-habibzadeh
ali-habibzadeh / Crawl-Query-GPT.ts
Last active September 7, 2023 16:36
The code defines three classes: PineconeCrawler, DiskCrawler, and Neo4jCrawler, each utilizing the PuppeteerCrawler to scrape web pages from specified URLs. They process webpage content differently: storing embeddings in a Pinecone database, saving them locally with HNSWLib, and adding page metadata to a Neo4j graph database, respectively.
import { GlobInput, PuppeteerCrawler } from "crawlee";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { PineconeStore } from "langchain/vectorstores/pinecone";
import { getPineconeClient } from "../utils/pinecone.utils";
import { GraphDbService } from "../graph/GraphDbService";
import { HNSWLib } from "langchain/vectorstores/hnswlib";
export class PineconeCrawler {
constructor(private indexName: string, private urls: string[], private globs?: string[]) {}
@ali-habibzadeh
ali-habibzadeh / zap-alerts.ts
Last active September 7, 2023 16:21
This Node.js script uses Puppeteer and OWASP ZAP API to scan a list of URLs (from "urls.json") for security vulnerabilities. It visits each URL, triggers ZAP scans, and appends alerts along with the URL found to a newline-delimited JSON file ("zap-report.jsonl").
import express from "express";
import { launch } from "puppeteer";
const proxyChain = require("proxy-chain");
import { URLSearchParams } from "node:url";
import fetch from "node-fetch";
import { appendFile } from "fs/promises";
import urls from "./urls.json";
import { ScanResults } from "./zap.type";
express().listen(process.env.PORT || 4000);
@ali-habibzadeh
ali-habibzadeh / isFibonacci.ts
Last active September 7, 2023 16:26
The JavaScript code defines two utility functions to identify special numeric properties. The isPerfectSquare function determines whether an input number is a perfect square, while the isFibonacci function leverages the former to identify if a number is part of the Fibonacci sequence, following a known mathematical property that relates Fibonacc…
const isPerfectSquare = (n: number): boolean => Number.isInteger(Math.sqrt(n));
const isFibonacci = (n: number): boolean => isPerfectSquare(5 * n * n + 4) || isPerfectSquare(5 * n * n - 4);
@ali-habibzadeh
ali-habibzadeh / to-tuple-type.ts
Last active September 7, 2023 16:26
This TypeScript code defines a type alias ToTupleType that takes two type parameters: B, an object type, and F, a tuple of key strings representing keys of B. It maps over the tuple F, creating a new tuple type where each element is the type of the corresponding property in B. It essentially helps in transforming an object into a tuple type with…
type ToTupleType<B, F extends (keyof B)[]> = {
[K in keyof F]: F[K] extends F[number] ? B[F[K]] : never;
};
@ali-habibzadeh
ali-habibzadeh / anomaly-detector.ts
Last active September 7, 2023 16:27
This TypeScript code defines an AnomalyDetector class that identifies anomalies in a dataset based on the specified sensitivity level. It uses a specified numeric key to extract values from dataset objects and compute the average and standard deviation of these values to identify anomalies, which are then filtered and can be retrieved using the …
interface AnomalyDetectorOptions<T> {
dataset: T[];
numericKey: keyof T;
sensitivity: number;
}
class AnomalyDetector<T extends Record<string, any>> {
constructor(private options: AnomalyDetectorOptions<T>) {}
private getValues(): number[] {
@ali-habibzadeh
ali-habibzadeh / Collatz-Conjecture.ts
Last active September 7, 2023 16:28
This JavaScript code defines a CollatzConjecture class that implements the iterable protocol to generate a sequence according to the Collatz conjecture rules starting from a given number. When instantiated with a start number and iterated over, it produces a series of numbers according to the conjecture, which ends when the series reaches the nu…
class CollatzConjecture implements Iterable<number> {
private next = this.startFrom;
constructor(private startFrom: number) {}
*[Symbol.iterator](): Generator<number> {
while (this.next !== 1) {
this.next = this.next % 2 === 0 ? this.next / 2 : this.next * 3 + 1;
yield this.next;
}
}
@ali-habibzadeh
ali-habibzadeh / deduplicate.ts
Last active September 7, 2023 16:29
The TypeScript code defines a type alias OccurrenceMap and a function getDuplicates. The function takes a readonly array, calculates the occurrence of each element, and returns an object that maps each element (that appears more than once) to its frequency. It leverages TypeScript's utility types to work with both number and string arrays.
type OccurrenceMap<T extends readonly string[] | readonly number[]> = Record<T[number], number>;
function getDuplicates(arr: readonly any[]): OccurrenceMap<typeof arr> {
const reducer = (a: OccurrenceMap<typeof arr>, b: string) => ({
...a,
[`${b}`]: a[`${b}`] + 1 || 1,
});
const res: OccurrenceMap<typeof arr> = arr.reduce(reducer);
return Object.fromEntries(Object.entries(res).filter((e) => e[1] > 1));
}