I made a compromise, as running a typescript project with node would take me more time to configure:
const { PORT, JWT_SECRET } = process.env;
const JWT_PORT = +PORT + 999;
const URL = `http://127.0.0.1:${JWT_PORT}`;
export const sign = async (data) => {
const json = JSON.stringify(data);
console.log('signing', json);
const res = await fetch(`${URL}/sign/${json}`);
const token = await res.text();
return token;
};
export const verify = async (token) => {
const res = await fetch(`${URL}/verify/${token}`);
const data = await res.json();
return data;
};
let jwtProcess;
const connect = async () => {
try {
const isAlive = await fetch(URL);
} catch (ex) {
if (jwtProcess) jwtProcess.kill();
console.log('connecting to jwtProcess');
jwtProcess = Bun.spawn(['node', './nodejs/jwt.js', JWT_PORT, JWT_SECRET]);
console.log('connected to jwtProcess');
}
};
connect();
setInterval(connect, 10000);
and my nodejs/jwt.js file:
import express from 'express';
import jwt from 'jsonwebtoken';
const [, , PORT, SECRET] = process.argv;
console.log(PORT, SECRET);
const app = express();
app.get('/', (req, res) => res.send('ok'));
app.get('/sign/:json', (req, res) => {
try {
res.send(jwt.sign(req.params.json, SECRET));
} catch (ex) {
res.send(null);
}
});
app.get('/verify/:token', (req, res) => {
try {
res.send(jwt.verify(req.params.token, SECRET));
} catch (ex) {
res.send(null);
}
});
app.listen(PORT);