build: update reader server for Node 22
This commit is contained in:
+53
-43
@@ -1,54 +1,64 @@
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const port = 33843;
|
||||
const { JSDOM } = require('jsdom');
|
||||
const { Readability } = require('@mozilla/readability');
|
||||
|
||||
const request = require('request');
|
||||
const JSDOM = require('jsdom').JSDOM;
|
||||
const { Readability } = require('readability');
|
||||
const PORT = 33843;
|
||||
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0';
|
||||
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
function createApp(fetchImpl = globalThis.fetch) {
|
||||
const app = express();
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.send('<form method="POST" accept-charset="UTF-8"><input name="url"><button type="submit">SUBMIT</button></form>');
|
||||
});
|
||||
});
|
||||
|
||||
const requestCallback = (url, res) => (error, response, body) => {
|
||||
if (!error && response.statusCode == 200) {
|
||||
console.log('Response OK.');
|
||||
|
||||
const doc = new JSDOM(body, {url: url});
|
||||
const reader = new Readability(doc.window.document);
|
||||
const article = reader.parse();
|
||||
|
||||
if (article && article.content) {
|
||||
res.send(article.content);
|
||||
} else {
|
||||
res.sendStatus(404);
|
||||
}
|
||||
} else {
|
||||
console.log('Response error:', error ? error.toString() : response.statusCode);
|
||||
res.sendStatus(response ? response.statusCode : 404);
|
||||
}
|
||||
};
|
||||
|
||||
app.post('/', (req, res) => {
|
||||
app.post('/', async (req, res) => {
|
||||
const url = req.body.url;
|
||||
const requestOptions = {
|
||||
url: url,
|
||||
gzip: true,
|
||||
//headers: {'User-Agent': 'Googlebot/2.1 (+http://www.google.com/bot.html)'},
|
||||
//headers: {'User-Agent': 'Twitterbot/1.0'},
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0',
|
||||
'X-Forwarded-For': '66.249.66.1',
|
||||
},
|
||||
};
|
||||
if (!url) {
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
|
||||
console.log('Parse request for:', url);
|
||||
|
||||
request(requestOptions, requestCallback(url, res));
|
||||
});
|
||||
try {
|
||||
const response = await fetchImpl(url, {
|
||||
headers: {
|
||||
'User-Agent': USER_AGENT,
|
||||
'X-Forwarded-For': '66.249.66.1',
|
||||
},
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}!`);
|
||||
});
|
||||
if (response.status !== 200) {
|
||||
console.log('Response error:', response.status);
|
||||
if (response.body && response.body.cancel) {
|
||||
await response.body.cancel();
|
||||
}
|
||||
return res.sendStatus(response.status);
|
||||
}
|
||||
|
||||
const body = await response.text();
|
||||
const document = new JSDOM(body, { url }).window.document;
|
||||
const article = new Readability(document).parse();
|
||||
|
||||
if (article && article.content) {
|
||||
return res.send(article.content);
|
||||
}
|
||||
return res.sendStatus(404);
|
||||
} catch (error) {
|
||||
console.log('Response error:', error.toString());
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
createApp().listen(PORT, () => {
|
||||
console.log(`Reader server listening on port ${PORT}!`);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { createApp, PORT };
|
||||
|
||||
@@ -3,11 +3,16 @@
|
||||
"version": "1.0.0",
|
||||
"main": "main.js",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=22.22.2"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "node main.js",
|
||||
"test": "node --test"
|
||||
},
|
||||
"dependencies": {
|
||||
"dompurify": "^1.0.11",
|
||||
"express": "^4.17.1",
|
||||
"jsdom": "^15.1.1",
|
||||
"readability": "https://github.com/mozilla/readability",
|
||||
"request": "^2.88.0"
|
||||
"@mozilla/readability": "^0.6.0",
|
||||
"express": "^5.3.0",
|
||||
"jsdom": "^31.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
const { once } = require('node:events');
|
||||
const test = require('node:test');
|
||||
const { createApp } = require('../main');
|
||||
|
||||
async function startServer(t, fetchImpl) {
|
||||
const server = createApp(fetchImpl).listen(0, '127.0.0.1');
|
||||
await once(server, 'listening');
|
||||
t.after(() => new Promise((resolve, reject) => {
|
||||
server.close((error) => error ? reject(error) : resolve());
|
||||
server.closeAllConnections();
|
||||
}));
|
||||
return `http://127.0.0.1:${server.address().port}`;
|
||||
}
|
||||
|
||||
function postUrl(url, form = { url: 'https://example.test/article' }) {
|
||||
return fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
||||
body: new URLSearchParams(form),
|
||||
});
|
||||
}
|
||||
|
||||
test('GET / renders the submission form', async (t) => {
|
||||
const baseUrl = await startServer(t, async () => {
|
||||
throw new Error('GET should not fetch an article');
|
||||
});
|
||||
const response = await fetch(`${baseUrl}/`);
|
||||
assert.equal(response.status, 200);
|
||||
assert.match(await response.text(), /<form method="POST"/);
|
||||
});
|
||||
|
||||
test('POST / fetches and extracts article content', async (t) => {
|
||||
const articleText = 'Distinctive article text for reader view. '.repeat(40);
|
||||
const html = `<html><head><title>Test article</title></head><body>
|
||||
<nav>Navigation should not appear</nav>
|
||||
<article><h1>Readable title</h1><p>${articleText}</p></article>
|
||||
</body></html>`;
|
||||
let requestedUrl;
|
||||
let requestOptions;
|
||||
const baseUrl = await startServer(t, async (url, options) => {
|
||||
requestedUrl = url;
|
||||
requestOptions = options;
|
||||
return { status: 200, text: async () => html };
|
||||
});
|
||||
|
||||
const response = await postUrl(baseUrl);
|
||||
const content = await response.text();
|
||||
assert.equal(response.status, 200);
|
||||
assert.equal(requestedUrl, 'https://example.test/article');
|
||||
assert.equal(requestOptions.headers['User-Agent'], 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0');
|
||||
assert.equal(requestOptions.headers['X-Forwarded-For'], '66.249.66.1');
|
||||
assert.match(content, /Distinctive article text for reader view/);
|
||||
assert.doesNotMatch(content, /Navigation should not appear/);
|
||||
});
|
||||
|
||||
test('POST / returns 404 when Readability cannot find an article', async (t) => {
|
||||
const baseUrl = await startServer(t, async () => ({
|
||||
status: 200,
|
||||
text: async () => '<html><body></body></html>',
|
||||
}));
|
||||
const response = await postUrl(baseUrl);
|
||||
assert.equal(response.status, 404);
|
||||
});
|
||||
|
||||
test('POST / forwards upstream HTTP error statuses', async (t) => {
|
||||
const baseUrl = await startServer(t, async () => ({ status: 503 }));
|
||||
const response = await postUrl(baseUrl);
|
||||
assert.equal(response.status, 503);
|
||||
});
|
||||
|
||||
test('POST / returns 404 when the upstream request fails', async (t) => {
|
||||
const baseUrl = await startServer(t, async () => {
|
||||
throw new Error('network unavailable');
|
||||
});
|
||||
const response = await postUrl(baseUrl);
|
||||
assert.equal(response.status, 404);
|
||||
});
|
||||
|
||||
test('POST / rejects requests without a URL without fetching', async (t) => {
|
||||
let fetched = false;
|
||||
const baseUrl = await startServer(t, async () => {
|
||||
fetched = true;
|
||||
throw new Error('should not fetch');
|
||||
});
|
||||
const response = await postUrl(baseUrl, {});
|
||||
assert.equal(response.status, 400);
|
||||
assert.equal(fetched, false);
|
||||
});
|
||||
+525
-844
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user