Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qol tweaks #5

Merged
merged 3 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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