Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const request = require("request");
const rp = require("request-promise-native");
const { StringStream } = require("scramjet");
StringStream.from( // fetch your API to a scramjet stream
request("https://api.example.org/v1/shows/list")
)
.setOptions({maxParallel: 4}) // set your options
.lines() // split the stream by line
.parse(theirShow => { // parse strings to data
return {
id: theirShow.id,
title: theirShow.name,
url: theirShow.url
};
})
.map(async myShow => rp({ // use asynchronous mapping (for example send requests)
method: "POST",
simple: true,
uri: `http://api.local/set/${myShow.id}`,
body: JSON.stringify(myShow)
export const getStream = function (cityName, url, averagingPeriod, source, orgUrl) {
const { metadata } = source;
const match = url.match(/[\w]{2}_([\w.]{2,})_([\d]{4})(?:_gg)?.txt/);
const parameter = match[1].toLowerCase().replace('.', '');
const year = match[2];
const unit = getUnit(parameter);
const dayPosition = averagingPeriod.value === 1 ? 0 : 1;
const fewDaysAgo = +Number(moment.tz(timezone).subtract(4, 'days').format('DDD'));
log.verbose(`Fetching data from ${url}`);
const stations = {};
return StringStream.from(
request(url)
)
.lines(StringStream.SPLIT_LINE)
.map(x => x.replace(/\s+/g, ' ').replace(/^\s+|\s+$/g, ''))
.parse(
row =>
row
.trim()
.split(/\s+/g)
)
.shift(1, ([header]) => {
header
.slice(2)
.forEach((x, i) => {
if (+x) {
stations[i] = Object.assign(metadata[x]);