A CloudFlare Worker for caching SnapLogic API Responses
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.
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
- If running locally, create and set variables in .dev.vars
- If running in production, set the following variables in the Project->Settings->Variables
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 |
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.
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 ECMAScriptstart
Runs wrangler in development modepublish
Publishes the application as a CloudFlare Workerclean
Removes dist/*lint
Runs eslint src/**/*.{ts,tsx}lint:fix
Runslint
with the --fix optiontest
Does nothing, put test plans here
This project is comprised of TypeScript and follows the Google TypeScript Style Guidelines with the exception of using TSDoc for documentation.
See CHANGELOG.md