Socket
Socket
Sign inDemoInstall

blob-common

Package Overview
Dependencies
190
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    blob-common

Helper functions for blob-images API, useful in stack with serverless and AWS ** from v2.0.0 for SDK V3 **, but probably not generally useful.


Version published
Weekly downloads
57
increased by2750%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

NPM module for common functions in backend

useful in a setup with

  • serverless stack
  • AWS

Usage

All functions can be imported from their respective modules (results in lowest packages size), e.g.:

import { sleep } from 'blob-common/core/sleep';
import { btoa } from 'blob-common/core/base64';

All functions can be imported directly from the core, e.g.:

import { sleep, btoa } from 'blob-common';

NB: the sanitize function is not included in the core, due to the relatively large size of imported package.

Generic functions

ModuleFunctionDescription
/base64otob(object)takes an object and coverts to base64 string
/base64btoa(base64String)inverse: takes a base64 string and returns an object
/datenow()returns a string of the current date in format yyyy-mm-dd
/datediffDate(dateStr, days)takes a dateStr in standard format and difference, returns a dateStr in the standard format
/dateexpireDate(dateStr)takes a dateStr in standard format and returns a date 30 days later in the standard format
/handlerhandler(lamdba)wrapper function for event handler (to log errors)
/handlergetUserFromEvent(event)returns the userId (from AWS cognito user pool) of authenticated User
/handlerapiCall(promise)async wrapper for promise, returns the a tuple [err, result]
/idsnewPhotoId()returns a newly generated photoId (starts with P)
/idsnewGroupId()returns a newly generated groupId (starts with G)
/idsnewAlbumId()returns a newly generated albumId (starts with A)
/RNDRNDreturns a random number between 1 and 15, usable as an index key in dynamoDb to spread database load on very large queries
/sleepsleep(ms)await sleep(1000) sleeps for 1 second. Used in tests to wait for dynamoDB to have processed updates
/sanitizesanitize(dirty)sanitizes a string from any sequences that may cause security issues (e.g. when using name string in an email message)

DynamoDb functions

ModuleFunctionDescription
/dbdynamoDb.get(params)returns a promise for DynamoDb function using the params. Uses AWS-SDK, refer to official docs for more info. Also has functions .put, .update, .delete, query, transact (for TransactWrite)
/dbdbUpdate(PK, SK, key, newVal)returns a promise for a single update on the database, setting key to new value provided in newVal
/dbdbUpdateMulti(PK, SK, newKV)returns a promise for a single update on the database at multiple keys. newKV is an object containing keys to be updated with new values
/dbCleancleanRecord(object)removes keys RK, datePK, dateSK, cognitoId from the object - useful for storing data as derived items in other items (e.g. owner as part of photo object)
/dbCreatedbItem(object)returns an object with extended keys for database: createdAt, RK, datePK, dateSK
/dbCreatedbCreateItem(object)returns a promise to create an expanded item in database from object (object must have PK and SK keys)

Some caveats on the specific dynamoDB functions:

  • eu-central-1 is the standard region chosen
  • to work, there should be a process.env.photoTable variable for all functions (except dynamoDb)
  • database must have PK (string) as primary key, and SK (string) as sortkey

S3 and SES functions

ModuleFunctionDescription
/s3s3.getMetadata(params)returns a promise for an s3 function using the params.
/s3s3.delete(params)deletes stuff.
/s3s3.get(params)gets an object.
/s3s3.list(params)lists a bucket probably.
/s3s3.getSignedUrl(params)gets a signed url for ADDING a file to s3 (for uploads).
/s3s3.getSignedUrlGet(params)gets a signed url for READING a file from s3.
/sesses.send(params)sends an email from ses using params - returns promise
/sesses.sendEmail(params)sends an email from ses using {toEmail, fromEmail, subject, data, textData} - returns promise

NB:

  • toEmail can be either a string or an array of strings (multiple recipients)

Caveats:

  • s3 function uses s3 bucket specified in process.env.photoBucket or process.env.devBucket

email helper functions

ModuleFunctionDescription
/emailemailBody(rows)returns email body with rows
/emailrow(cells)generic row containing array of cells
/emailheaderRow(logoSrc, frontendUrl)header row with logo, links to frontend
/emailfooterRow
/emailphotoRow(photoSrc, linkUrl)row containing (header) photo
/emailtextCell(subtype)cell containing greeting, paragraph or buttonEscape (small text with link)
/emaildividerCell(dividerSrc)
/emailbuttonCell(text, link)
/emailcodeCell(code)big text block centered with code
/emailsignatureCell(src)left aligned signature under email
/emailgreeting(text)
/emailparagraph(text)
/emailbuttonEscape(url)
/emailmakeEmailSrc(url, width, height)returns publicly accessible url for image in S3 bucket

Caveats:

  • makeEmailSrc uses s3 bucketname from process.env.bucket or process.env.devBucket

Cognito functions

ModuleFunctionDescription
/cognitocognito.disableUser(userId)disables a user in the cognito user pool
/cognitocognito.deleteUser(userId)deletes a user in the cognito user pool

Caveats:

  • uses cognito userpool id from process.env.userPoolId or process.env.devUserPoolId

Keywords

FAQs

Last updated on 18 Dec 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc