diff options
author | Alexis Hovorka <[email protected]> | 2024-02-17 00:42:20 -0700 |
---|---|---|
committer | Alexis Hovorka <[email protected]> | 2024-02-17 00:42:20 -0700 |
commit | 8f44593d55c82197854d9e29174fe66c4e50ebde (patch) | |
tree | a43a6d763517d1304076897952b1111886303158 /app/lib | |
parent | c5aa364e0e372d0e29063a27bd17811446db8b6a (diff) |
[refactor] Switch to ES Modules
Diffstat (limited to 'app/lib')
-rw-r--r-- | app/lib/otp.js | 8 | ||||
-rw-r--r-- | app/lib/pipe.js | 6 | ||||
-rw-r--r-- | app/lib/router.js | 10 | ||||
-rw-r--r-- | app/lib/socket.js | 6 | ||||
-rw-r--r-- | app/lib/static.js | 18 |
5 files changed, 19 insertions, 29 deletions
diff --git a/app/lib/otp.js b/app/lib/otp.js index 751edf4..14763a8 100644 --- a/app/lib/otp.js +++ b/app/lib/otp.js @@ -1,6 +1,4 @@ -"use strict"; - -const { createHmac } = require("crypto"); +import { createHmac } from "node:crypto"; const getHMAC = (k,c) => { const h = createHmac("sha1", k); h.update(c, "hex"); return h.digest("hex"); } @@ -12,7 +10,7 @@ const b32u = s => Uint8Array.from(s.split("").reduce((a,c) => a+b32a.indexOf(c.toUpperCase()).toString(2).padStart(5,0), "") .match(/.{8}/g).map(b => parseInt(b,2))); -module.exports = function totp(secret, { expiry=30, +export default function totp(secret, { expiry=30, now=Math.round(new Date().getTime()/1000), length=6 }={}) { const time = d2h(now/expiry).padStart(16,0); const hmac = getHMAC(b32u(secret), time); @@ -21,7 +19,7 @@ module.exports = function totp(secret, { expiry=30, return otp.padStart(length,0).substr(-length); } -module.exports.check = function check(token, secret, { expiry=30, +export function check(token, secret, { expiry=30, now=Math.round(new Date().getTime()/1000), length=6, window=0 }={}) { const i = Array(window*2+1).fill().map((e,i) => now+(expiry*(i-window))) .findIndex(n => token === this(secret, {now:n, expiry, length})); diff --git a/app/lib/pipe.js b/app/lib/pipe.js index e329b2b..db58e5f 100644 --- a/app/lib/pipe.js +++ b/app/lib/pipe.js @@ -1,8 +1,6 @@ -"use strict"; +import { spawn } from "node:child_process"; -const { spawn } = require("child_process"); - -module.exports = function pipe({ command, flags, stdin="", buffer } = {}) { +export default function pipe({ command, flags, stdin="", buffer } = {}) { return new Promise((resolve, reject) => { const child = spawn(command, flags); let stdout = (buffer?[]:""); diff --git a/app/lib/router.js b/app/lib/router.js index fd24693..186e6ad 100644 --- a/app/lib/router.js +++ b/app/lib/router.js @@ -1,15 +1,13 @@ -"use strict"; // https://github.com/mixu/minimal - -const url = require("url"); +import {parse as parseURL} from "node:url"; const degroup = path => Object.assign(path, path.groups); -class Router { +export default class Router { constructor() { this.routes = []; } route(req, res) { - const pathname = url.parse(req.url).pathname; + const pathname = parseURL(req.url).pathname; return this.routes.some(route => { const isMatch = route.method === req.method && route.re.test(pathname); if (isMatch) route.cb(req, res, degroup(route.re.exec(pathname))); @@ -49,5 +47,3 @@ class Router { Router.prototype[method] = function(re, cb) { this.routes.push({method: method.toUpperCase(), cb, re: (re instanceof RegExp)? re : new RegExp(`^${re}$`)})}); - -module.exports = Router; diff --git a/app/lib/socket.js b/app/lib/socket.js index 88fdd49..9222d00 100644 --- a/app/lib/socket.js +++ b/app/lib/socket.js @@ -1,6 +1,4 @@ -"use strict"; - -const WebSocket = require("ws"); +import WebSocket from "ws"; class Client { constructor(ws) { @@ -27,7 +25,7 @@ class Client { } } -module.exports = class Socket { +export default class Socket { constructor(server) { this.wss = new WebSocket.Server({server}); this.handlers = {}; diff --git a/app/lib/static.js b/app/lib/static.js index ef48ae6..26fc231 100644 --- a/app/lib/static.js +++ b/app/lib/static.js @@ -1,13 +1,13 @@ -"use strict"; - -const Path = require("path"); -const url = require("url"); -const fs = require("fs"); +import {normalize, extname} from "node:path"; +import {parse as parseURL} from "node:url"; +import fs from "node:fs"; const mimeTypes = { ".html": "text/html", ".css": "text/css", ".js": "text/javascript", + ".cjs": "text/javascript", + ".mjs": "text/javascript", ".json": "application/json", ".wasm": "application/wasm", ".pdf": "application/pdf", @@ -32,7 +32,7 @@ const mimeTypes = { ".glb": "model/gltf-binary", }; -module.exports = class Static { +export default class Static { constructor(root) { this.root = `${root||"."}`; this.e404 = fs.readFileSync(`${this.root}/404.html`); @@ -46,8 +46,8 @@ module.exports = class Static { return; } - const pathname = url.parse(req.url).pathname; - const sane = Path.normalize(pathname).replace(/^(\.\.\/)+/, ""); + const pathname = parseURL(req.url).pathname; + const sane = normalize(pathname).replace(/^(\.\.\/)+/, ""); let path = `${this.root}${sane}`; //Path.join(__dirname, sane); fs.stat(path, (err, stats) => { @@ -60,7 +60,7 @@ module.exports = class Static { }} if (stats.isDirectory()) path += "/index.html"; - const ext = `${Path.extname(path)}`.toLowerCase(); + const ext = `${extname(path)}`.toLowerCase(); const stream = fs.createReadStream(path); stream.on("error", e => { console.log(e); |