Skip to content

Instantly share code, notes, and snippets.

const AWS = require('aws-sdk')
// a function that returns a promise
function copyFileToS3() {
// initialise AWS S3
const s3 = new AWS.S3({
accessKeyId: 'whatever',
secretAccessKey: 'whatever',
endpoint: 'http://localhost:4566', // localstack is running on port 4566. Imagine this was the real S3 endpoint
const s3 = require('./s3') // This is the file where we use aws-sdk's S3 CopyObject method
const mockS3Instance = {
copyObject: jest.fn().mockReturnThis(),
promise: jest.fn().mockReturnThis(),
catch: jest.fn(),
}
jest.mock('aws-sdk', () => {
return { S3: jest.fn(() => mockS3Instance) }
@nkhil
nkhil / Calculator.md
Last active June 9, 2021 11:47
Create a simple calculator that given a string of operators (), +, -, *, / and numbers separated by spaces returns the value of that expression
@nkhil
nkhil / for-each-async-await.js
Created June 13, 2021 20:41
Do not use async/await inside forEach functions
async function someAsyncFuncion(num) {
return await new Promise(resolve => resolve(num))
}
function orchestrator() {
[1, 2, 3, 4, 5].forEach(async num => {
console.log('console 1 fired')
const promiseResolvedValue = await someAsyncFuncion(num)
console.log(promiseResolvedValue)
console.log('console 2 fired')

Here's how to create and export an RSA public/private key pair from Node.js

const crypto = require('crypto')
const fs = require('fs')

const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 2048,
})
const crypto = require('crypto')
const NUMBER_OF_BYTES = 32
const randomBytes = crypto.randomBytes(NUMBER_OF_BYTES)
const crypto = require('crypto')
const fs = require('fs')
const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", {
// The standard secure default length for RSA keys is 2048 bits
modulusLength: 2048,
})
// *********************************************************************
//
const fs = require('fs')
const crypto = require('crypto')
const dataToEncrypt = fs.readFileSync('data_to_encrypt.txt', { encoding: 'utf-8' })
const publicKey = Buffer.from(fs.readFileSync('public.pem', { encoding: 'utf-8' }))
const encryptedData = crypto.publicEncrypt(
{
key: publicKey,
const crypto = require('crypto')
const fs = require('fs')
const encryptedData = fs.readFileSync('encrypted_data.txt', { encoding: 'utf-8' })
const privateKey = fs.readFileSync('private.pem', { encoding: 'utf-8' })
const decryptedData = crypto.privateDecrypt(
{
key: privateKey,
// In order to decrypt the data, we need to specify the
function parseLogs(logs) {
const logsArray = logs.split('\n').map(eachLog => eachLog.split(','))
const normalisedArray = normaliseArray(logsArray)
const logsWithSeconds = normalisedArray.map(([timestamp, phoneNumber]) =>
[calculateTotalSeconds(timestamp), phoneNumber])
const grouped = groupByPhoneNumber(logsWithSeconds)
const phoneNumberWithLargestTotal = getPhoneNumberWithLargestTotalSeconds(grouped)
grouped[phoneNumberWithLargestTotal] = 0
return calculateTotalBill(logsWithSeconds, phoneNumberWithLargestTotal)
}