updated to node 20 and rewritten as modules

This commit is contained in:
James Tomasino 2023-07-29 11:33:37 +00:00
parent 244665db1c
commit 97c437cdce
3 changed files with 43 additions and 47 deletions

View File

@ -6,6 +6,7 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"type": "module",
"author": "",
"license": "AGPL-3.0",
"dependencies": {

View File

@ -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 = '<a href="/"><span class="dim">&lt;&lt;</span> BACK TO COSMIC VOYAGE</a>'
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 = '<a href="/log"><span class="dim">&lt;&lt;</span> BACK TO RS001 LOG</a>'
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 = '<a href="/"><span class="dim">&lt;&lt;</span> BACK TO COSMIC VOYAGE</a>'
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 = '<a href="/log"><span class="dim">&lt;&lt;</span> BACK TO RS001 LOG</a>'
const content = back + '\n\n' + file
const fullUrl = 'https://cosmic.voyage' + req.originalUrl
res.render('basic', { content: content, canonical: fullUrl})
} else {
error = true
}
})
} else {

View File

@ -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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;')
}
module.exports = {
bufferFile: bufferFile,
head: head,
wc: wc,
escape: escape
}