import { STATUS_CODES } from "http" ; import arc from "@architect/functions" ; import { Request } from "./architect-types" ; import util from "util" ; import crypto from "crypto" ; export const STATUSES = Object . fromEntries ( Object . entries ( STATUS_CODES ) . map ( ( entry ) => [ entry [ 1 ] , Number ( entry [ 0 ] ) ] ) ) ; export const CONTENT_TYPES = { html : "text/html; charset=utf8" , json : "application/json" , } ; export function isLoggedIn ( request ) { const session = arc . http . session . read ( request ) ; log ( `Checking isLoggedIn. session is ${ stringify ( session ) }` ) ; if ( session ?. person ?. email ) { return true ; } return false ; } export async function asyncForEach < Generic > ( array , iterator ) { for ( let index = 0 ; index < array . length ; index ++ ) { await iterator ( array [ index ] , index , array ) ; } } export const wait = async ( timeInMs ) : Promise < void > => { return new Promise ( ( resolve ) => setTimeout ( resolve , timeInMs ) ) ; } ; export async function repeat < Generic > ( func , count ) { const results = [ ] ; for ( let index = 0 ; index < count ; index ++ ) { results . push ( func ( ) ) ; } return Promise . all ( results ) ; } export const log = console . log . bind ( console ) ; export const warn = console . warn . bind ( console ) ; export const stringify = ( input ) : string => { return JSON . stringify ( input , null , 2 ) ; } ; export function deepClone ( obj ) { return JSON . parse ( JSON . stringify ( obj ) ) ; } export const dateFromNowNumber = function ( adjustmentMs ) : number { const now = new Date ( ) . valueOf ( ) ; return now + adjustmentMs ; } ; export const dateFromNow = function ( adjustmentMs ) : Date { return new Date ( dateFromNowNumber ( adjustmentMs ) ) ; } ; export const getRandomBytes = util . promisify ( crypto . randomBytes ) ; export function makeStringUrlSafe ( string ) { const URL_UNSAFE_CHARACTERS = /^[\W]/g ; return string . replace ( URL_UNSAFE_CHARACTERS , "" ) ; } export async function getRandomUrlSafeString ( length ) { const buffer = await getRandomBytes ( length ) ; return makeStringUrlSafe ( buffer . toString ( "hex" ) . substring ( 0 , length ) ) ; } export interface ObjectLiteral { [ key ] : any ; } export function getRandomNumberBetween ( minimim , maximum ) { minimim = Math . ceil ( minimim ) ; maximum = Math . floor ( maximum ) ; return Math . floor ( Math . random ( ) * ( maximum - minimim + 1 ) ) + minimim ; } export function getRandomPortNumberForSandbox ( ) { return getRandomNumberBetween ( 3000 , 4000 ) ; }