Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"dependencies": {
"bestzip": "^2.2.1",
"mkcert": "^3.2.0",
"read": "^3.0.1"
}
}
76 changes: 76 additions & 0 deletions src/commands/serve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import path from "node:path";
import https from "node:https";
import fs from "fs";
import { createCA, createCert } from "mkcert";
/**
*
* @param args
*/
export default async function serve(args) {
const PORT = 443;

const MIME_TYPES = {
default: "application/octet-stream",
html: "text/html; charset=UTF-8",
js: "application/javascript",
css: "text/css",
png: "image/png",
jpg: "image/jpg",
gif: "image/gif",
ico: "image/x-icon",
svg: "image/svg+xml",
};

const STATIC_PATH = path.join(process.cwd(), args[0]);

const toBool = [() => true, () => false];

const prepareFile = async (url) => {
const paths = [STATIC_PATH, url];
if (url.endsWith("/")) paths.push("index.html");
const filePath = path.join(...paths);
const pathTraversal = !filePath.startsWith(STATIC_PATH);
const exists = await fs.promises.access(filePath).then(...toBool);
const found = !pathTraversal && exists;
const streamPath = found ? filePath : STATIC_PATH + "/index.html";
const ext = path.extname(streamPath).substring(1).toLowerCase();
const stream = fs.createReadStream(streamPath);
return { found, ext, stream };
};

const ca = await createCA({
organization: "Hello CA",
countryCode: "UA",
state: "Bagmati",
locality: "Kathmandu",
validity: 365
});

const cert = await createCert({
ca: { key: ca.key, cert: ca.cert },
domains: ["127.0.0.1", "local.applura.app"],
validity: 365
});

const options = {
cert: cert.cert,
key: cert.key
}
const server = https
.createServer(options, async (req, res) => {
const file = await prepareFile(req.url);
const statusCode = file.found ? 200 : 404;
const mimeType = MIME_TYPES[file.ext] || MIME_TYPES.default;
res.writeHead(statusCode, { "Content-Type": mimeType });
file.stream.pipe(res);
console.log(`${req.method} ${req.url} ${statusCode}`);
});

server.listen(PORT, () => {
console.log(`Server available at: \n https://127.0.0.1/ \n https://local.applura.app/`);
});

return new Promise((res) => {
server.on('close', res);
})
}
2 changes: 2 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import {
} from "./routines/setup.js";
import deployKey from "./commands/deploy-key.js";
import deploy from "./commands/deploy.js";
import serve from "./commands/serve.js";
import { getContext } from "./lib/context.js";

const commands = {
// init is a noop since the main function prepares any missing config.
init: () => {},
"deploy-key": deployKey,
deploy: deploy,
serve: serve,
};

export default async function main(cwd, argv, stdStreams, env) {
Expand Down