Skip to content

Instantly share code, notes, and snippets.

View benjaminudoh10's full-sized avatar

Benjamin benjaminudoh10

View GitHub Profile
function lastDigit(str1, str2) {
if (str2 === "1") return parseInt(str1[str1.length - 1])
if (str2 === "0") return 1
if (str1 === "0" && str2 === "0") return 1
if (str1.endsWith("0")) return 0
if (str1.endsWith("1")) return 1
if (str1.endsWith("2")) {
return [6, 2, 4, 8][str2 % 4]
}
if (str1.endsWith("3")) {
@benjaminudoh10
benjaminudoh10 / v4_uuid.ts
Last active January 18, 2023 13:40
Naive implementation of version 4 uuid generator
import * as crypto from 'crypto';
function v4_uuid() {
let rand_bytes = crypto.randomBytes(16);
let rand_bytes_int = Uint8Array.from(rand_bytes);
// replace 7th byte with 4 (0100)
// & 0b00001111 replaces the first 4 bits with 0s
// | 0b01000000 replaces the first 4 bits with 0100
rand_bytes_int[6] = (rand_bytes_int[6] & 0b00001111) | 0b01000000;
@benjaminudoh10
benjaminudoh10 / app.ts
Created January 4, 2023 11:33
Bull Board integration
...
import { createBullBoard } from 'bull-board';
import { BullMQAdapter } from 'bull-board/bullMQAdapter';
const app = express();
...
const defaultQueue = new QueueService().getQueue(Queues.DEFAULT);
if (defaultQueue) {
@benjaminudoh10
benjaminudoh10 / app.ts
Last active January 4, 2023 16:21
Bullqueue blog - Add endpoint for processing payment
import express, { Request, Response } from 'express';
const app = express();
...
app.post('/process-payment', (request: Request, response: Response) => {
const { cardNumber, cvv, cardExpiry } = request.body;
const queue = new QueueService().getQueue(Queues.DEFAULT);
@benjaminudoh10
benjaminudoh10 / processors.default.ts
Created January 4, 2023 11:02
Bullqueue blog - Add processor
import { Job } from 'bullmq';
import { JobType, Queues } from '../enum/queue.enum';
import QueueService from '../services/queue.service';
// these enums should be in a separate file and exported
enum Queues {
DEFAULT = 'default',
}
enum JobType {
@benjaminudoh10
benjaminudoh10 / queue.service.ts
Last active January 4, 2023 11:06
Bullqueue blog - Queue Service (add workers)
...
import DefaultProcessor from './processors.default';
enum JobType {
PROCESS_PAYMENT = 'process-payment',
}
export default class QueueService {
...
@benjaminudoh10
benjaminudoh10 / queue.service.ts
Last active January 4, 2023 10:33
Bullqueue blog - Queue Service
import { Queue } from 'bullmq';
enum Queues {
DEFAULT = 'default',
}
export default class QueueService {
private queues: Record<string, Queue>;
private defaultQueue: Queue;
@benjaminudoh10
benjaminudoh10 / app.ts
Last active January 4, 2023 10:15
Bullqueue blog - App instantiation
import express, { Request, Response } from 'express';
const app = express();
app.use(express.json());
app.get('/health', (request: Request, response: Response) => {
response
.status(200)
.json({
@benjaminudoh10
benjaminudoh10 / bootstrap-node-app.sh
Created January 2, 2023 14:16
Bootstrap NodeJS Application
#!/bin/bash
DIR_NAME=node-app-bootstrap;
function bootstrap_node_app() {
if which node > /dev/null
then
mkdir $DIR_NAME;
cd $DIR_NAME;
npm init -y > /dev/null;
@benjaminudoh10
benjaminudoh10 / reactions.service.ts
Last active April 15, 2023 16:55
Add reaction to feed
import Redis from 'ioredis';
export class ReactionService {
private redisClient: Redis
constructor() {
// connect to your redis instance first
this.redisClient = new Redis()
}