Skip to content

Use ESM syntax throughout #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ Specify a port using `-p <port number>`
angular-http-server -p 9000
```

Open in a default browser automatically by using `--open` alias `-o`
### Open browser automatically

```sh
angular-http-server --open
angular-http-server -o
```

HTTPS can be enabled (using a generated self-signed certificate) with `--https` or `--ssl`
Expand Down Expand Up @@ -163,6 +164,7 @@ The `--https` or `--ssl` flags are intended for **development and/or testing pur

## Changelog

- 1.13.0 - Use ESM syntax (thanks Claude ;-))
- 1.12.0 - adds host support (thanks jpwerka)
- 1.11.0 - adds proxy support (thanks AVierwind)
- 1.10.0 - adds --rootPath (thanks Aakash)
Expand All @@ -179,6 +181,12 @@ Contributions are welcome, but do create an issue first to discuss.

Use prettier for formatting

## Dev

```sh
npm run dev
```

## Testing

Run unit tests with
Expand Down
42 changes: 24 additions & 18 deletions lib/angular-http-server.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#!/usr/bin/env node

var fs = require("fs");
var path = require("path");
var mime = require("mime");
var pem = require("pem");
var https = require("https");
var http = require("http");
var opn = require("opn");

var argv = require("minimist")(process.argv.slice(2));
const getFilePathFromUrl = require("./get-file-path-from-url");
const proxyHandler = require("./proxy");
import fs from "fs";
import path from "path";
import mime from "mime";
import pem from "pem";
import https from "https";
import http from "http";
import open from "open";
import minimist from "minimist";
import { fileURLToPath } from "url";
import getFilePathFromUrl from "./get-file-path-from-url.js";
import proxyHandler from "./proxy.js";

// Get current file directory equivalent to __dirname in CJS
const __filename = fileURLToPath(import.meta.url);

const argv = minimist(process.argv.slice(2));

// if using config file, load that first
if (argv.config) {
Expand All @@ -20,7 +25,8 @@ if (argv.config) {
} else {
configPath = path.join(process.cwd(), argv.config);
}
const getConfig = require(configPath);
const configModule = await import(configPath);
const getConfig = configModule.default || configModule;
let config;
if (typeof getConfig === "function") {
config = getConfig(argv);
Expand All @@ -40,7 +46,7 @@ const port = getPort(argv.p);

const NO_PATH_FILE_ERROR_MESSAGE = `Error: rootFile ${rootFile} could not be found in the specified path `;

// As a part of the startup - check to make sure we can accessrootFile
// As a part of the startup - check to make sure we can access rootFile
returnDistFile(true);

// Start with/without https
Expand Down Expand Up @@ -74,10 +80,10 @@ if (useHttps) {

function start() {
server.listen(port, function () {
if (argv.open == true || argv.o) {
if (argv.open || argv.o) {
// open a browser tab
const host = argv.host || "localhost"
opn((useHttps ? "https" : "http") + "://" + host + ":" + port);
const host = argv.host || "localhost";
open((useHttps ? "https" : "http") + "://" + host + ":" + port);
}
return console.log("Listening on " + port);
});
Expand Down Expand Up @@ -138,15 +144,15 @@ function getPort(portNo) {
if (!isNaN(portNum)) {
return portNum;
} else {
throw new Exception("Provided port number is not a number!");
throw new Error("Provided port is not a number!");
}
} else {
return 8080;
}
}

function returnDistFile(displayFileMessages = false) {
var distPath;
let distPath;

try {
if (displayFileMessages) {
Expand Down
64 changes: 33 additions & 31 deletions lib/get-file-path-from-url.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require('path');
import path from "path";

/**
* Safely get the path for a file in the project directory, or reject by returning "dummy"
Expand All @@ -8,40 +8,42 @@ const path = require('path');
* @param {object} [pathLib=path] - path library, override for testing
* @return {string} - will return 'dummy' if the path is bad
*/
function getFilePathFromUrl(url, basePath, { pathLib = path, baseHref = '' } = {}) {
if (!basePath) {
throw new Error('basePath must be specified');
}
if (!pathLib.isAbsolute(basePath)) {
throw new Error(`${basePath} is invalid - must be absolute`);
}
export default function getFilePathFromUrl(
url,
basePath,
{ pathLib = path, baseHref = "" } = {}
) {
if (!basePath) {
throw new Error("basePath must be specified");
}
if (!pathLib.isAbsolute(basePath)) {
throw new Error(`${basePath} is invalid - must be absolute`);
}

// we are not interested in query params
// TODO also filter out hash routes
let relativePath = url.split('?')[0];
// we are not interested in query params
// TODO also filter out hash routes
let relativePath = url.split("?")[0];

if (relativePath.indexOf('../') > -1) {
// any path attempting to traverse up the directory should be rejected
return 'dummy';
}
if (relativePath.indexOf("../") > -1) {
// any path attempting to traverse up the directory should be rejected
return "dummy";
}

if (baseHref) {
if (relativePath.startsWith('/' + baseHref)) {
relativePath = relativePath.substr(baseHref.length + 1)
} else {
return 'dummy'
if (baseHref) {
if (relativePath.startsWith("/" + baseHref)) {
relativePath = relativePath.substr(baseHref.length + 1);
} else {
return "dummy";
}
}
}

const absolutePath = pathLib.join(basePath, relativePath);
if (
!absolutePath.startsWith(basePath) || // if the path has broken out of the basePath, it should be rejected
absolutePath.endsWith(pathLib.sep) // only files (not folders) can be served
) {
return 'dummy';
}
const absolutePath = pathLib.join(basePath, relativePath);
if (
!absolutePath.startsWith(basePath) || // if the path has broken out of the basePath, it should be rejected
absolutePath.endsWith(pathLib.sep) // only files (not folders) can be served
) {
return "dummy";
}

return absolutePath;
return absolutePath;
}

module.exports = getFilePathFromUrl;
Loading