Skip to content

CloudFlare Worker for caching SnapLogic API Responses

License

Notifications You must be signed in to change notification settings

ATXadam/snaplogic-cache

Repository files navigation

SnapLogic Cache

A CloudFlare Worker for caching SnapLogic API Responses

TypeScript Code Style: Google License: ISC

Yarn Cloudflare

Deploy to Cloudflare Workers

About

This CloudFlare Workers application is designed for a caching proxy with SnapLogic to save on requests to API endpoints if the data is frequently accessed and infrequently changed. Additionally, it fixes up bad or broken responses from the HTTP Trigger event ensuring a more consistent response when requests fail.

All requests will be cached (POST, GET, etc) if the response from SnapLogic is a 200 OK. To bypass or refresh the cache set a Cache-Control: no-cache request header. This will force the cache to expire for the request data and return and cache a new hit to the target endpoint.

Getting Started

To setup the project locally:

npx wrangler generate snaplogic-cache https://github.com/atxadam/snaplogic-cache
cd snaplogic-cache
yarn install

To start the project with wrangler in default online development mode:

yarn start

To run local-only (no CloudFlare sign-in required):

yarn start --local=true

To run local-only and disable local HTTPS support:

yarn start --local=true --local-protocol=http --var=requireHTTPS:false

More parameters may be found in the CloudFlare Wrangler Commands documentation, or by running yarn start --help

To deploy to CloudFlare using wrangler:

yarn publish

Configuration

  • If running locally, create and set variables in .dev.vars
  • If running in production, set the following variables in the Project->Settings->Variables

Required configuration parameters

Parameter Type Description
ttl int Time To Live in cache, must be greater than 0, defaults to 60
targetProtocol string Target server protocol http[s], defaults to https
targetHostname string Target server hostname, defaults to elastic.snaplogic.com
targetPort int Target server port number, defaults to 443

Optional configuration parameters

Parameter Type Description
targetPathPrefix string Prefixes a path to the target URL for URL shortening
allowBinaryData bool Allows binary POST, PUT, PATCH data, defaults to false
requireHTTPS bool Require all client requests to be HTTPS, or fail with a 426 status code, defaults to true
requestTimeout int Maximum lifetime of request session in seconds, defaults to 100

NOTE: A change in configuration parameters will require a restart of a development mode instance.

Timeouts

SnapLogic has a maximum execution time of 900 seconds while CloudFlare has the following:

  • Free Plan: 100 seconds
  • Enterprise Plan: Up to 6000 seconds

The application parameter requestTimeout controls how long a fetch can take, with a default of 100 seconds to accommodate for the free plan. You may change this parameter to match your use case.

Basic yarn Scripts

  • build Compiles the TypeScript to ECMAScript
  • start Runs wrangler in development mode
  • publish Publishes the application as a CloudFlare Worker
  • clean Removes dist/*
  • lint Runs eslint src/**/*.{ts,tsx}
  • lint:fix Runs lint with the --fix option
  • test Does nothing, put test plans here

Code Styling

This project is comprised of TypeScript and follows the Google TypeScript Style Guidelines with the exception of using TSDoc for documentation.

History

See CHANGELOG.md

License

ISC

About

CloudFlare Worker for caching SnapLogic API Responses

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published