Skip to content

Commit

Permalink
Merge pull request #14 from sajera/feature/URL
Browse files Browse the repository at this point in the history
feature/URL
  • Loading branch information
sajera authored Sep 14, 2022
2 parents a6a86da + eb03f9e commit 4169eb6
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 22 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ DEBUG=true
# API local configuration
PORT=3000
HOST=localhost
RENDER_FALLBACK=true

# Redis local configuration
REDIS_URL=redis://127.0.0.1:6379
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ services:
DEBUG: 1
PORT: 3000
HOST: 0.0.0.0
RENDER_FALLBACK: 0
REDIS_URL: redis://cache:6379/0
CHROME_FORWARD_HEADERS: 1
CHROME_DEBUGGING_PORT: 9222
Expand Down
13 changes: 1 addition & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"chrome-remote-interface": "^0.31.3",
"dotenv": "^16.0.1",
"redis": "^4.2.0",
"uuid": "^8.3.2",
"valid-url": "^1.0.9"
"uuid": "^8.3.2"
},
"devDependencies": {
"nodemon": "^2.0.19"
Expand Down
4 changes: 3 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const DEBUG = varBoolean(process.env.DEBUG);
export const API = {
port: varNumber(process.env.PORT) || 80,
host: varString(process.env.HOST) || '0.0.0.0',
renderFallback: varBoolean(process.env.RENDER_FALLBACK),
};
// NOTE for now Redis only
export const CACHE = {
Expand Down Expand Up @@ -59,7 +60,6 @@ export function defaultCleanupHtmlScript () {
}
})(['noscript', 'script', 'style'])`;
}

/******************************************************
* ¯\(ヅ)/¯ helpers ᕦ(ツ)ᕤ
*****************************************************/
Expand All @@ -73,3 +73,5 @@ const logWithTime = (text, data) => console.log(
text,
data === undefined ? '' : DEBUG ? JSON.stringify(data, null, 4) : JSON.stringify(data),
);
const urlRegExp = /(?:https?):\/\/(\w+:?\w*)?(\S+)(:\d+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
export const isUrl = url => urlRegExp.test(url);
18 changes: 11 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

// outsource dependencies
import qs from 'node:querystring';
import { isWebUri } from 'valid-url';

// local dependencies
import api from './api/index.js';
import cache from './cache/index.js';
import prerender from './prerender/index.js';
import { logError, log, API, CACHE, PRERENDER } from './config.js';
import { isUrl, logError, log, API, CACHE, PRERENDER } from './config.js';

// NOTE log unhandled promise exception
process.on('unhandledRejection', error => logError('[service:unhandledRejection]', error && {
Expand Down Expand Up @@ -43,19 +42,24 @@ render.contentType = 'text/html';
async function render (request) {
if (!cache.isReady()) { throw { code: 503, message: 'Service not ready yet' }; }
const url = qs.unescape(qs.parse(request.url.query).url);
if (!isWebUri(url)) { throw { code: 400, message: `Invalid query parameter url "${url}"` }; }
if (!isUrl(url)) { throw { code: 400, message: `Invalid query parameter url "${url}"` }; }
const results = await cache.get(url);
results && log('[api:cache]', url);
return results || refresh(request);
if (results) {
log('[api:cache]', url);
return results;
}
if (API.renderFallback) {
return refresh(request);
}
throw { code: 404, message: `Cache empty for "${url}"` };
}

api.middleware['/refresh'] = refresh;
refresh.contentType = 'text/html';
async function refresh (request) {
console.log(request.url.query);
if (!prerender.isReady() || !cache.isReady()) { throw { code: 503, message: 'Service not ready yet' }; }
const url = qs.unescape(qs.parse(request.url.query).url);
if (!isWebUri(url)) { throw { code: 400, message: `Invalid query parameter url "${url}"` }; }
if (!isUrl(url)) { throw { code: 400, message: `Invalid query parameter url "${url}"` }; }
const results = await prerender.render(url);
log('[api:generate]', url);
await cache.set(url, results);
Expand Down

0 comments on commit 4169eb6

Please sign in to comment.