Skip to content

Commit

Permalink
Merge pull request #5 from mwood77/qol-tweaks
Browse files Browse the repository at this point in the history
Qol tweaks
  • Loading branch information
mwood77 authored Feb 23, 2025
2 parents c842d77 + 05e86db commit ccf6668
Show file tree
Hide file tree
Showing 487 changed files with 86,313 additions and 55 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/github-pages.yaml

This file was deleted.

34 changes: 34 additions & 0 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable no-underscore-dangle */
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

// Helpers
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const distDir = path.join(__dirname, 'dist');

// Cleanup old dist folder
if (fs.existsSync(distDir)) fs.rmSync(distDir, { recursive: true, force: true });

// List of files and folders to include in the build
const includePaths = ['cors', 'datagenerators', 'server', 'views', 'public', 'index.js'];

// Function to copy files/folders
const copy = (src, dest) => {
if (!fs.existsSync(src)) return;

const stats = fs.lstatSync(src);
if (stats.isDirectory()) {
fs.mkdirSync(dest, { recursive: true });
fs.readdirSync(src).forEach((file) => copy(path.join(src, file), path.join(dest, file)));
} else {
fs.copyFileSync(src, dest);
}
};

// Copy each included file/folder
includePaths.forEach((item) => copy(path.join(__dirname, item), path.join(distDir, item)));

console.log('✅ Build completed: Files copied to dist/');
43 changes: 43 additions & 0 deletions dist/cors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// pass through api requests

// http(s) modules
const https = require('https');

// url parsing
const queryString = require('querystring');

// return an express router
module.exports = (req, res) => {
// add out-going headers
const headers = {};
headers['user-agent'] = '(WeatherStar 4000+, [email protected])';
headers.accept = req.headers.accept;

// get query paramaters if the exist
const queryParams = Object.keys(req.query).reduce((acc, key) => {
// skip the paramater 'u'
if (key === 'u') return acc;
// add the paramter to the resulting object
acc[key] = req.query[key];
return acc;
}, {});
let query = queryString.encode(queryParams);
if (query.length > 0) query = `?${query}`;

// get the page
https.get(`https://api.weather.gov${req.path}${query}`, {
headers,
}, (getRes) => {
// pull some info
const { statusCode } = getRes;
// pass the status code through
res.status(statusCode);

// set headers
res.header('content-type', getRes.headers['content-type']);
// pipe to response
getRes.pipe(res);
}).on('error', (e) => {
console.error(e);
});
};
44 changes: 44 additions & 0 deletions dist/cors/outlook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// pass through api requests

// http(s) modules
const https = require('https');

// url parsing
const queryString = require('querystring');

// return an express router
module.exports = (req, res) => {
// add out-going headers
const headers = {};
headers['user-agent'] = '(WeatherStar 4000+, [email protected])';
headers.accept = req.headers.accept;

// get query paramaters if the exist
const queryParams = Object.keys(req.query).reduce((acc, key) => {
// skip the paramater 'u'
if (key === 'u') return acc;
// add the paramter to the resulting object
acc[key] = req.query[key];
return acc;
}, {});
let query = queryString.encode(queryParams);
if (query.length > 0) query = `?${query}`;

// get the page
https.get(`https://www.cpc.ncep.noaa.gov/${req.path}${query}`, {
headers,
}, (getRes) => {
// pull some info
const { statusCode } = getRes;
// pass the status code through
res.status(statusCode);

// set headers
res.header('content-type', getRes.headers['content-type']);
res.header('last-modified', getRes.headers['last-modified']);
// pipe to response
getRes.pipe(res);
}).on('error', (e) => {
console.error(e);
});
};
44 changes: 44 additions & 0 deletions dist/cors/radar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// pass through api requests

// http(s) modules
const https = require('https');

// url parsing
const queryString = require('querystring');

// return an express router
module.exports = (req, res) => {
// add out-going headers
const headers = {};
headers['user-agent'] = '(WeatherStar 4000+, [email protected])';
headers.accept = req.headers.accept;

// get query paramaters if the exist
const queryParams = Object.keys(req.query).reduce((acc, key) => {
// skip the paramater 'u'
if (key === 'u') return acc;
// add the paramter to the resulting object
acc[key] = req.query[key];
return acc;
}, {});
let query = queryString.encode(queryParams);
if (query.length > 0) query = `?${query}`;

// get the page
https.get(`https://radar.weather.gov${req.path}${query}`, {
headers,
}, (getRes) => {
// pull some info
const { statusCode } = getRes;
// pass the status code through
res.status(statusCode);

// set headers
res.header('content-type', getRes.headers['content-type']);
res.header('last-modified', getRes.headers['last-modified']);
// pipe to response
getRes.pipe(res);
}).on('error', (e) => {
console.error(e);
});
};
22 changes: 22 additions & 0 deletions dist/datagenerators/chunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// turn a long array into a set of smaller chunks

const chunk = (data, chunkSize = 10) => {
const chunks = [];

let thisChunk = [];

data.forEach((d) => {
if (thisChunk.length >= chunkSize) {
chunks.push(thisChunk);
thisChunk = [];
}
thisChunk.push(d);
});

// final chunk
if (thisChunk.length > 0) chunks.push(thisChunk);

return chunks;
};

module.exports = chunk;
24 changes: 24 additions & 0 deletions dist/datagenerators/https.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// async https wrapper

const https = require('https');

module.exports = (url) => new Promise((resolve, reject) => {
const headers = {};
headers['user-agent'] = '(WeatherStar 4000+ data generator, [email protected])';

https.get(url, {
headers,
}, (res) => {
if (res.statusCode === 200) {
const buffers = [];
res.on('data', (data) => buffers.push(data));
res.on('end', () => resolve(Buffer.concat(buffers).toString()));
} else {
console.log(res);
reject(new Error(`Unable to get: ${url}`));
}
}).on('error', (e) => {
console.log(e);
reject(e);
});
});
Loading

0 comments on commit ccf6668

Please sign in to comment.