From 97c437cdce1ce9a032cd806e3b9281515bdbd65f Mon Sep 17 00:00:00 2001 From: James Tomasino Date: Sat, 29 Jul 2023 11:33:37 +0000 Subject: [PATCH] updated to node 20 and rewritten as modules --- package.json | 1 + server.js | 62 +++++++++++++++++++++++++++------------------------- utils.js | 27 +++++++++-------------- 3 files changed, 43 insertions(+), 47 deletions(-) diff --git a/package.json b/package.json index d011a52..8d70516 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, + "type": "module", "author": "", "license": "AGPL-3.0", "dependencies": { diff --git a/server.js b/server.js index baf4a1d..ec31e98 100644 --- a/server.js +++ b/server.js @@ -1,12 +1,15 @@ -const express = require('express') +import { access, constants, readFile, stat } from 'node:fs'; +import express from 'express'; +import path from 'node:path'; +import { fileURLToPath } from 'url'; +import { bufferFile, wc, head, escape } from './utils.js'; const app = express() const port = 3000 -var fs = require('fs') -var path = require('path') -const { bufferFile, wc, head, escape } = require('./utils') +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); app.engine('cosmic', function (filePath, options, callback) { - fs.readFile(filePath, function (err, content) { + readFile(filePath, function (err, content) { var s if (err) return callback(err) var rendered = content.toString() @@ -126,7 +129,7 @@ app.get('/atom.xml', function (_req, res) { app.get('/sitemap.xml', function (_req, res) { var content = bufferFile('/var/gopher/sitemap.xml') - fs.stat('/var/gopher/rss.xml', (err, stats) => { + stat('/var/gopher/rss.xml', (err, stats) => { if (err) { res(err) } @@ -144,11 +147,16 @@ app.get('/.well-known/webfinger', function(req, res) { if (resources.length) { const user = resources[1].toLowerCase() const path = '/home/' + user + '/.webfinger.json' - if (fs.existsSync(path)) { - const content = bufferFile(path) - res.setHeader('content-type', 'application/jrd+json') - res.render('raw', { content: content }) - } + access(path, constants.R_OK, (err) => { + if (err) { + res.setHeader('content-type', 'application/jrd+json') + res.render('raw', { content: '' }) + } else { + const content = bufferFile(path) + res.setHeader('content-type', 'application/jrd+json') + res.render('raw', { content: content }) + } + }) } } res.setHeader('content-type', 'application/jrd+json') @@ -160,30 +168,24 @@ app.use(express.static(path.join(__dirname, '/static'))) // Override default LICENSE display and format for cosmic styles app.get('*/LICENSE', function(req, res){ - let error = false var file = path.join('/var/gopher/', decodeURIComponent(req.path)); - fs.exists(file, function(exists) { - if (exists) { + access(file, constants.R_OK, (err) => { + if (err) { + const back = '<< BACK TO COSMIC VOYAGE' + const error = 'Message not found. Please try again.' + const content = back + '\n\n' + error + res.status(404) + const fullUrl = 'https://cosmic.voyage' + req.originalUrl + res.render('basic', { content: content, canonical: fullUrl}) + } else { const file = escape(bufferFile('/var/gopher/' + decodeURIComponent(req.path))) const back = '<< BACK TO RS001 LOG' const content = back + '\n\n' + file res.setHeader('content-type', 'text/html') const fullUrl = 'https://cosmic.voyage' + req.originalUrl res.render('basic', { content: content, canonical: fullUrl}) - } else { - error = true } }) - - // If license isn't found, give the 404 - if (error) { - const back = '<< BACK TO COSMIC VOYAGE' - const error = 'Message not found. Please try again.' - const content = back + '\n\n' + error - res.status(404) - const fullUrl = 'https://cosmic.voyage' + req.originalUrl - res.render('basic', { content: content, canonical: fullUrl}) - } }) // Any other gopher content directly linked will show as-is @@ -194,15 +196,15 @@ app.get('*', function(req, res){ let error = false if (req.path.indexOf('.html') !== -1) { const file = path.join('/var/gopher/', decodeURIComponent(req.path).replace(/\.html/, '.txt')); - fs.exists(file, function(exists) { - if (exists) { + access(file, constants.R_OK, (err) => { + if (err) { + error = true + } else { const file = escape(bufferFile('/var/gopher/' + decodeURIComponent(req.path).replace(/\.html/, '.txt'))) const back = '<< BACK TO RS001 LOG' const content = back + '\n\n' + file const fullUrl = 'https://cosmic.voyage' + req.originalUrl res.render('basic', { content: content, canonical: fullUrl}) - } else { - error = true } }) } else { diff --git a/utils.js b/utils.js index 3d030a3..42371cf 100644 --- a/utils.js +++ b/utils.js @@ -1,21 +1,21 @@ -var fs = require('fs') -const readline = require('readline') -const { once } = require('events') -var exec = require('child_process').exec +import { readFileSync, createReadStream } from 'node:fs'; +import readline from 'readline' +import { once } from 'events' +import { exec } from 'node:child_process' -function bufferFile(path) { +export function bufferFile (path) { try { - return fs.readFileSync(path, { encoding: 'utf8' }) + return readFileSync(path, { encoding: 'utf8' }) } catch (_e) { return '' } } -async function head (path, lines) { +export async function head (path, lines) { let x = 0 const acc = [] const readInterface = readline.createInterface({ - input: fs.createReadStream(path), + input: createReadStream(path), terminal: false, crlfDelay: Infinity }) @@ -31,7 +31,7 @@ async function head (path, lines) { return acc } -function wc (path) { +export function wc (path) { return new Promise((resolve, reject) => { exec("sed -n '$=' " + path, function (error, results) { if (error) { @@ -43,13 +43,6 @@ function wc (path) { }) } -function escape (text) { +export function escape (text) { return text.replace(/\&/g, '&').replace(//g, '>').replace(/"/g, '"').replace(/'/g, ''') } - -module.exports = { - bufferFile: bufferFile, - head: head, - wc: wc, - escape: escape -}