This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
import { createBullBoard } from 'bull-board'; | |
import { BullMQAdapter } from 'bull-board/bullMQAdapter'; | |
const app = express(); | |
... | |
const defaultQueue = new QueueService().getQueue(Queues.DEFAULT); | |
if (defaultQueue) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
import DefaultProcessor from './processors.default'; | |
enum JobType { | |
PROCESS_PAYMENT = 'process-payment', | |
} | |
export default class QueueService { | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Queue } from 'bullmq'; | |
enum Queues { | |
DEFAULT = 'default', | |
} | |
export default class QueueService { | |
private queues: Record<string, Queue>; | |
private defaultQueue: Queue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express, { Request, Response } from 'express'; | |
const app = express(); | |
app.use(express.json()); | |
app.get('/health', (request: Request, response: Response) => { | |
response | |
.status(200) | |
.json({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Redis from 'ioredis'; | |
export class ReactionService { | |
private redisClient: Redis | |
constructor() { | |
// connect to your redis instance first | |
this.redisClient = new Redis() | |
} |