npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fake-cloudwatch-logs

v2.1.2

Published

Setup a fake Cloudwatch logs server for testing purposes

Downloads

3

Readme

fake-cloudwatch-logs

Setup a fake Cloudwatch logs server for testing purposes

Example

const AWS = require('aws-sdk')
const path = require('path')
const { FakeCloudwatchLogs } = require('fake-cloudwatch-logs')

const ACCESS_KEY = '123'

async function test() {
  const region = 'us-east-1'
  const server = new FakeCloudwatchLogs({
    port: 0,
    cachePath: path.join(__dirname, 'cw-fixtures')
  })

  server.populateGroups(ACCESS_KEY, region, [...])
  server.populateStreams(ACCESS_KEY, region, 'my-group', [...])
  server.populateLogEvents(
    ACCESS_KEY, region, 'my-group', 'my-stream', [...]
  )

  await server.populateFromCache()
  await server.bootstrap()

  const cw = new AWS.CloudwatchLogs({
    region: region,
    endpoint: `http://${server.hostPort}`,
    sslEnabled: false,
    accessKey: ACCESS_KEY,
    secretAccessKey: 'abc'
  })

  const groups = await cw.describeLogGroups().promise()

  // Should be groups you populated or loaded from disk cache
  console.log('the groups', groups.logGroups)

  const events = await cw.getLogEvents({
    logGroupName: 'my-group',
    logStreamName: 'my-stream'
  }).promise()

  // Should be events you populated.
  console.log('the events', events.events)

  await server.close()
}

process.on('unhandledRejection', (err) => { throw err })
test()

Features

Currently this fake-cloudwatch-logs module supports various read APIs like describe log groups, describe streams and fetching log events.

It also supports APIs designed for making a read-only copy of production data cached on disks. This allows for using fixture data for local development and integration tests.

The other functionality can be added in the future, as needed.

The API that are supported are :

  • DescribeLogGroups
  • DescribeLogStreams
  • GetLogEvents

Recommended testing approach

Create the FakeCloudwatchLogs server in your test harness. Then configure your aws client to point to the endpoint.

You can call populate methods to populate mock data into the fake cloudwatch server.

Recommended local approach

Create the FakeCloudwatchLogs server on some HTTP port of your choice.

I recommend copying the scripts/cache-from-prod.js into your application, this will cache production data into a fixtures directory.

You can configure the FakeCloudwatchLogs to fetch that fixtures data into memory and then configure your website or application or server to point to the FakeCloudwatchLogs on whatever port you choose.

Here is an example snippet from the script

'use strict'

const path = require('path')
const AWS = require('aws-sdk')
const FakeCloudWatchLogs =
  require('fake-cloudwatch-logs').FakeCloudwatchLogs

async function main () {
  const fakeCW = new FakeCloudWatchLogs({
    cachePath: path.join(__dirname, '..', 'fixtures')
  })
  await fakeCW.populateFromCache()

  // 'all' regions or ['us-east-1']
  // await fakeCW.fetchAndCache(AWS, ['us-east-1'])
  await fakeCW.fetchAndCache(AWS, 'all')
}

main().then(null, (err) => {
  process.nextTick(() => { throw err })
})

Docs :

const server = new FakeCloudwatchLogs(options)

Creates a fake Cloudwatch logs server listening on the port your specified.

  • options.port; port to lsiten on, defaults to 0
  • options.cachePath; the location to read/write fixtures to.

await server.bootstrap()

Starts the server. After this method completes the field server.hostPort is available and can be used to access the actual listening port of the server if you choose to listen on port 0.

await server.close()

Closes the http server.

server.populateGroups(accessKey, region, groups)

Adds groups to the in-memory server. The group must be a valid LogGroup

const group = server.makeLogGroup(name)

server.populateStreams(accessKey, region, groupName, streams)

Adds streams to the in-memory server that belong to the groupName. The streams must be a valid LogStream

const stream = server.makeLogStream(name)

server.populateEvents(accessKey, region, groupName, streamName, events)

Adds events to the in-memory server that belong to the groupName and the streamName. The events must be a valid OutputLogEvent

const event = server.makeLogEvent()

await server.populateFromCache()

This will have the server fetch groups, streams & events from a cache on disk. This can be useful for writing tests with fixtures or for starting a local server that loads fixtures from disk.

It's recommende you use the cacheXToDisk() methods to create the fixtures.

await server.fetchAndCache(AWS, regions)

If you want to fetch and cache data from production into a fixtures directory you can call fetchAndCache() with the AWS sdk and with the regions you want to cache.

You can pass ['us-east-1'] etc as a the regions or the string 'all' if you want to fetch all regions.

await server.cacheGroupsToDisk(cacheDir, groups)

This will write groups to disk in the cache directory. The groups must be valid LogGroup ;

await server.cacheStreamsToDisk(cacheDir, groupName, streams)

This will write streams to disk in the cache directory for the groupName you specify. The streams must be valid LogStream

await server.cacheEventsToDisk(cacheDir, groupName, streamName, events)

This will write events to disk in the cache directory for the groupName and streamName you specify. The streams must be valid OutputLogEvent ;

install

% npm install fake-cloudwatch-logs

MIT Licensed