Compare commits

..

1 commit

Author SHA1 Message Date
Erin 5104d3b568 apply patch "Rose's hubris" 2023-07-01 18:20:15 -04:00
5 changed files with 1060 additions and 220 deletions

View file

@ -2,20 +2,10 @@
## run it
no external dependencies! no need to `npm i`! woo!
npm i
cp webring.txt.sample webring.txt && $EDITOR webring.txt
npm run start
## configuration
the server listens on port `$PORT` (default 80) and loads the list of
participating sites from the file `$SITES_FILE` (default `webring.txt`).
the sites file is a newline-separated list of URLs; leading and trailing
whitespace, empty lines, and lines starting with `#` are ignored. you can test
out the format using [this regexr link](https://regexr.com/?expression=/^\s*([^%23\s].*)\s*$/gm&text=%23%20sample%20SITES_FILE%0A%0A%23%20my%20friend%20alice%20who%20is%20cool%0Ahttps%3A%2F%2Falices.awesome.website%0A%0A%23%20my%20other%20friend%20bob%20who%20is%20neat%20as%20well%0Ahttp%3A%2F%2Fsome.shared.domain%2F~bob%0A&tool=list&input=your.webring.server/next?from=$1\n).
## link to webring sites
participating sites should link to the endpoints `/prev?from=<site>` and

View file

@ -1,29 +0,0 @@
{
"typescript": {
"lineWidth": 80,
"useTabs": true,
"quoteStyle": "alwaysSingle",
"quoteProps": "consistent",
"useBraces": "always",
"arrowFunction.useParentheses": "preferNone",
"enumDeclaration.memberSpacing": "newLine",
"spaceSurroundingProperties": false,
"exportDeclaration.spaceSurroundingNamedExports": false,
"importDeclaration.spaceSurroundingNamedImports": false,
"constructor.spaceBeforeParentheses": true,
"functionDeclaration.spaceBeforeParentheses": true,
"functionExpression.spaceBeforeParentheses": true,
"getAccessor.spaceBeforeParentheses": true,
"setAccessor.spaceBeforeParentheses": true,
"method.spaceBeforeParentheses": true
},
"markdown": {},
"excludes": [
"**/node_modules",
"**/*-lock.json"
],
"plugins": [
"https://plugins.dprint.dev/typescript-0.86.1.wasm",
"https://plugins.dprint.dev/markdown-0.15.3.wasm"
]
}

View file

@ -1,30 +1,44 @@
import {readFileSync} from 'node:fs';
import {createServer} from 'node:http';
import {URL} from 'node:url';
import * as fs from 'node:fs';
import http from 'node:http';
const data = readFileSync(process.env.SITES_FILE || 'webring.txt', 'utf-8');
const sites = [...data.matchAll(/^\s*([^#\s].*)\s*$/gm)].map(match => match[1]);
const mod = (n, m) => ((n % m) + m) % m;
const stripURL = s => s.replace(/(^https?:\/\/|\/$)/g, '')
const text = (response, body, code = 200) => {
response.writeHead(code, {'Content-Type': 'text/plain'}).write(body);
response.end();
};
const redirect = (res, url) => res.writeHead(302, {Location: url}).end();
const sitesArray = fs.readFileSync('./webring.txt', 'utf-8')
.match(/(?<!#.*)https?:\/\/[^\s]+(?=\s*$)/gmi)
const toSite = (response, from, offset) => {
const index = sites.findIndex(site => site === from);
if (!from || index === -1) {
return text(response, 'the provided site is not in the webring', 400);
const sitesLookup = sitesArray.map(stripURL).reduce((acc, current, index) => ({
...acc, [current]: {
'/next': sitesArray[mod(index + 1, sitesArray.length)],
'/prev': sitesArray[mod(index - 1, sitesArray.length)],
}
redirect(response, sites[(index + offset + sites.length) % sites.length]);
};
}), {})
const pathHandlers = {
'/prev': (response, query) => toSite(response, query.get('from'), -1),
'/next': (response, query) => toSite(response, query.get('from'), +1),
'/list': response => text(response, sites.join('\n')),
};
createServer((request, response) => {
const redirect = (response, url) => {
response.setHeader('Location', url)
response.statusCode = 301
response.end()
}
const handle = (direction, from, response) => {
const destination = sitesLookup[stripURL(from)]?.[direction]
if (destination)
return redirect(response, destination)
response.statusCode = 400
response.end('the provided site is not in the webring')
}
http.createServer((request, response) => {
const url = new URL(request.url, `http://${request.headers.host}`);
pathHandlers[url.pathname]?.(response, url.searchParams);
}).listen(process.env.PORT || 80);
if (url.pathname === '/prev' || url.pathname === '/next') {
return handle(url.pathname, url.searchParams.get('from'), response)
} else if (url.pathname === '/list') {
return response.setHeader('Content-Type', 'text/plain').end(sitesArray.join('\n'))
}
response.statusCode = 404
response.end('not found')
}).listen(process.env.PORT || 8080)

1150
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -4,13 +4,12 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.mjs",
"fmt": "dprint fmt"
"start": "node index.mjs"
},
"keywords": [],
"author": "",
"license": "WTFPL",
"devDependencies": {
"dprint": "^0.40.2"
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
}
}