![Maven Central Adds Sigstore Signature Validation](https://cdn.sanity.io/images/cgdhsj6q/production/7da3bc8a946cfb5df15d7fcf49767faedc72b483-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
feed-fetcher
Advanced tools
fetch API based abstraction to fetch json/text/blob feeds using methods similar to setInterval and setTimeout
This package is meant for the browser context.
In order to make scheduled fetch requests (usually for data feeds) easier and less annoying when it comes to exceptions and errors, feed fetcher handles most of it internally while providing an interface that is very similar to what you are used in the browser: setTimeout/setInterval, it also allows you to feed things once, or use the former methods and clearInterval/clearTimeout as you would normally.
For the error types when fetching look at the error types section.
Import into a webpack project by using
import feedFetcher from 'feed-fetcher'
If you need to support older browsers that don't have Promise, fetch, or requestAnimationFrame make sure you include polyfills before importing anything from FeedFetcher
import './your-polyfills' // Import your own polyfills if needed. ex: promise-polyfill, whatwg-fetch
import feedFetcher from 'feed-fetcher'
NOTE: Reminder to include fetch and Promise polyfills for browsers like internet explorer, BEFORE including the FeedFetcher script
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/whatwg-fetch@2.0.4/fetch.min.js"> </script>
<script src="FeedFetcher.min.js"> </script>
// Access it through window:
window.feedFetcher
// Minimal example, automatically sets mode to json because of filename.
// data on the method onData can be expected to be a json object
// intervalID return value can be used to clearFeedInterval when needed.
// In this case: As soon as we get working data once, we kill the feed fetcher.
let intervalID = feedFetcher.setInterval({
url: 'https://somewebsite.com/file.json',
onData: (data, prevData) => {
console.log('New Data:', data)
feedFetcher.clearInterval(intervalID)
}
}, 10000)
let options = {
// Url that will be fetched.
url: '/file1.json',
// [optional]
// Defines how to parse and deal with data internally. If omitted it will be set automatically to 'json' for json files, 'text' for txt files, and by default to 'text' for all other urls
// Current possible values: 'json', 'text', 'blob'
mode: 'json',
// How often we will attempt to fetch data in milliseconds
// if omitted the default will be used: 10 seconds.
// This refresh rate can be specified as a second parameter also like usual browser setInterval, setTimeout
refreshRate: 5000,
// How long we will wait for request to be completed before failing.
// Default is 15 seconds.
timeout: 15000,
// Runs when we receive data.
// data parameter will contain a string in 'text' mode, and a javascript object if mode is set to 'json'
// prevData parameter will work similarly as data, but with the last cached data, on the first execution it will be null (no previous data)
// Note: if compareWithCache option is set to true, this method will not execute on identical new data.
onData: (data, prevData) => {/* do something... */},
// [optional]
// if not omitted, it will run when data received is equal to the last data received that was cached.
// Works identically to onData otherwise.
onSameData: (data) => {/* do something... */},
// [optional]
// Although errors are already logged, if anything in particular needs to be done depending on the error type, this callback can be specified:
// error parameter is always an object which contains:
// error = {
// errorType: 'FETCH_FAIL' || 'JSON_PARSE_FAIL' || 'JSON_AND_TEXT_PARSE_FAIL', //... more error types, look below.
// errorText: 'Error text caught in the catch() of the fetch',
// feedObject: {'internal feed object being exposed for analysis. Do not modify it'},
// // if errorType == 'FETCH FAIL'
// status: 404,
// statusText: 'Not Found'
// }
onError: (error) => {
}
}
setTimeout and clearTimeout work identically to setInterval and clearInterval where the first parameter is the options, and the second parameter is milliseconds.
Fetch is a shorthand for fetching a feed just once and cancelling the subsequent fetching internally, no matter what the result ends up being.
// Simple one time fetch that times out after 5 seconds
feedFetcher.fetch({
url: '/',
timeout: 5000,
mode: 'text',
onData: (data) => {
console.log('Data received: ', data)
},
onError: (error) => {
console.warn('Error occurred with fetch: ', error)
}
})
You can also do the following shorthand for function parameters: url[, mode], onData [, onError, onSameData], milliseconds
The mandatory parameters are url, onData, and milliseconds. you can specify mode in between url and onData, and specify other method handlers before milliseconds.
// Simple one time fetch.
feedFetcher.fetch('https://somewebsite.com/file.json', (data) => {
console.log(data)
})
feedFetcher.setInterval('/', 'text',
(data) => {
console.log(data)
},
(error) => {
console.error(error)
}
, 7500)
By logging the error object with the onError callback, and reading the errorType property, we can know what went wrong.
error.errorType == 'FETCH_TIMEOUT'
// The fetch timed out, either because you specified a timeout number in specific: ex: 10000 (10 seconds)
// Or the default timeout parameter: 15 seconds.
error.errorType == 'JSON_PARSE_FAIL'
// When fetching in JSON mode, response.json() failed internally, so whatever you fetched could not parse properly.
// error.textContents will contain the response as a string so you can see what might be the problem.
error.errorType == 'JSON_AND_TEXT_PARSE_FAIL'
// When trying to read the text contents of a JSON_PARSE_FAIL error, we also cannot get the text contents. This is not likely.
error.errorType == 'BLOB_PARSE_FAIL'
// Tried to fetch a file/url as a blob but the internal response.blob() failed. Can get text contents of request via error.textContents
error.errorType == 'BLOB_AND_TEXT_PARSE_FAIL'
// Similar as JSON_AND_TEXT_PARSE_FAIL but for blob.
error.errorType == 'FETCH_FAIL'
// General error for failed fetching, use error.errorText, or analyize error.feedObject / error.statusText / error.status to see what went wrong
error.errorType == 'USER_METHOD_ERROR'
// An error got caught within the callback methods supplied: onData, onSameData (Warning: onError will not be caught here.)
// Ex: Trying to JSON parse something that's not a proper JSON string, whether related to the fetched data or not in this method, will be caught internally.
// errorText will display the error.
If you are confident you will have requestAnimationFrame support on your target browser and you do not want the fetches to run when the page is tabbed out; You can set the internal mode of feed fetcher to work via requestAnimationFrame. Note that you can only change this when there are no feeds set up to run at an interval or timeout. So ideally, at the beginning of the app.
feedFetcher.setMode('raf') // Default is 'interval'
FAQs
fetch API based abstraction to fetch json/text/blob feeds using methods similar to setInterval and setTimeout
The npm package feed-fetcher receives a total of 8 weekly downloads. As such, feed-fetcher popularity was classified as not popular.
We found that feed-fetcher demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.