5m
is a near real time utility, 5mb or 5min
Installation | Use case | Annotated source | License
Installation
npm i 5m
Use case
I use this package with debug and aws-sdk to upload logs on S3.
Then there is a trigger that feeds every file created on S3 to an AWS Lambda which sends file content to a server that broadcast it using socket.io. Yes I know there are other cool tools like CloudWatch, Kinesis, etc. (but 5m is free as in speach and free as in beer too ;)
The cool part is that it is possible to trigger an AWS Lambda every time a new file is created, and for example load it on a database.
Here is an example code to achieve logging and upload on S3.
// File: log.js//// Prints to STDOUT if DEBUG environment variable is set properly.// Uploads logs on some S3 bucket.//// const log = require('./log')// log('Hello world')// const AWS = const debug = const fiveM = const s3 = // Use package name as namespace for logging, just as an example.const pkg = const namespace = pkgnameconst debug5m = const debugPkg = const writeOnS3 = { const Bucket = 'my-bucket' const tstamp = const Key = `my/path/.log` s3} { const tstamp = const record = ` ` record } moduleexports = log
Annotated source
Store data in a namespaced bucket, as well as its function to flush data.
var bucket = {}var flush = {}
Considering 1 char should be 1 byte, and dates are expressed in milliseconds,
the following constants express 5 MB and 5 minutes.
Aproximation is ok, since we want to achieve a near real time.
The timeout used for flushing data can be set, for testing or other purpouse
via the FIVEM_TIMEOUT_MILLISECONDS
environment variable.
const fiveMb = 1024 * 1024 * 5const fiveMin = 300 * 1000const flushTimeout = processenvFIVEM_TIMEOUT_MILLISECONDS || fiveMin
Make sure no data is lost on exit.
process
Create the 5m function.
Since 5m is allowed as npm package name, but not as JavaScript identifier, using a roman number like Vm can be confusing, so maybe naming the function as fiveM is a good idea.
/** * **@param** `{String}` namespace * **@param** `{Function}` write * * **@returns** `{Function}` logger */ {
Create the namespaced flush function: write data and clean up.
flushnamespace = { if bucketnamespace delete bucketnamespace }
Create the logger function.
/** * @param */ return {
If necessary, initialize data bucket and set timeout to flush it later.
if typeof bucketnamespace === 'undefined' bucketnamespace = ''
Append data to named bucket.
bucketnamespace += data
Check if data is bigger than 5 MB.
const exceededSpace = bucketnamespace && bucketnamespacelength > 0 && bucketnamespacelength > fiveMb
If yes, flush it!
if exceededSpace flushnamespace }}
Export 5m function.
moduleexports = fiveM