forked from svrjs/svrjs
Compare commits
2 commits
50465e4a6c
...
79b506a4f0
Author | SHA1 | Date | |
---|---|---|---|
79b506a4f0 | |||
e0536620d6 |
8 changed files with 143 additions and 10 deletions
|
@ -81,7 +81,7 @@ After installing the packages, build SVR.JS with this command:
|
|||
```bash
|
||||
npm run build
|
||||
```
|
||||
After running the command, you will get bundled SVR.JS script, around with built-in utilities and assets in the `dist` directory. You will also get a zip archive in `out` directory, that can be installed using SVR.JS installer
|
||||
After running the command, you will get bundled SVR.JS script, around with built-in utilities and assets in the `dist` directory. You will also get a zip archive in `out` directory, that can be installed using SVR.JS installer. Additionally, you will get the SVR.JS Core package contents in the `core` directory, which you can publish by running `npm publish` in the `core` directory.
|
||||
|
||||
## Installation (built from source)
|
||||
|
||||
|
@ -118,6 +118,8 @@ You can read the [SVR.JS documentation](https://svrjs.org/docs) to get informati
|
|||
The file structure for SVR.JS source code looks like this:
|
||||
- .husky - Git hooks
|
||||
- assets - files to copy into dist folder and to the archive
|
||||
- core - contains SVR.JS Core
|
||||
- coreAssets - files to copy into core folder
|
||||
- dist - contains SVR.JS, assets, and SVR.JS utiltiies
|
||||
- generatedAssets - assets generated by the build script
|
||||
- out - contains SVR.JS zip archive
|
||||
|
|
|
@ -1,3 +1,64 @@
|
|||
# SVR.JS Core
|
||||
|
||||
TODO
|
||||
SVR.JS Core is a library for static file serving, built from SVR.JS source code.
|
||||
|
||||
Example code (with Node.JS "http" module):
|
||||
```javascript
|
||||
var http = require("http");
|
||||
var fs = require("fs");
|
||||
var url = require("url");
|
||||
var svrjsCore = require("svrjs-core").init(); // Initialize SVR.JS Core
|
||||
var server = http.createServer(function (req,res) {
|
||||
if(url.parse(req.url).pathname == "/useragent") {
|
||||
res.writeHead(200, "OK", {"content-type": "text-plain"}); // Output as plain text
|
||||
res.end("Your user agent: " + req.headers["user-agent"]); // Send user agent
|
||||
} else {
|
||||
svrjsCore(req,res); // Serve static content
|
||||
}
|
||||
}).listen(8888);
|
||||
```
|
||||
|
||||
Example code (with Express):
|
||||
```javascript
|
||||
var express = require("express");
|
||||
var svrjsCore = require("svrjs-core");
|
||||
|
||||
var app = express();
|
||||
app.use(svrjsCore());
|
||||
|
||||
app.listen(3000);
|
||||
```
|
||||
|
||||
## Methods
|
||||
|
||||
### *svrjsCore([config])*
|
||||
|
||||
Parameters:
|
||||
|
||||
- *config* - the SVR.JS Core configuration (optional, *Object*)
|
||||
|
||||
Returns: the request handler for use in Node.JS HTTP server or Express, with three parameters (*req*, *res*, and optional *next*)
|
||||
|
||||
The *config* object is almost the same format as SVR.JS configuration. You can read about SVR.JS configuration properties in [the SVR.JS documentation](https://svrjs.org/docs/config/configuration).
|
||||
|
||||
However, only these SVR.JS configuration properties apply to SVR.JS Core:
|
||||
- *users*
|
||||
- *page404*
|
||||
- *enableCompression*
|
||||
- *customHeaders*
|
||||
- *enableDirectoryListing*
|
||||
- *enableDirectoryListingWithDefaultHead*
|
||||
- *serverAdministratorEmail*
|
||||
- *stackHidden*
|
||||
- *exposeServerVersion*
|
||||
- *dontCompress*
|
||||
- *enableIPSpoofing*
|
||||
- *enableETag*
|
||||
- *rewriteDirtyURLs*
|
||||
- *errorPages*
|
||||
- *disableTrailingSlashRedirects*
|
||||
- *allowDoubleSlashes*
|
||||
|
||||
### *svrjsCore.init([config])*
|
||||
|
||||
An alias to the *svrjsCore()* function
|
|
@ -11,7 +11,6 @@ const svrjsCoreInfo = JSON.parse(
|
|||
fs.readFileSync(__dirname + "/svrjs.core.json")
|
||||
);
|
||||
const { externalPackages } = svrjsCoreInfo;
|
||||
const coreVersion = svrjsCoreInfo.version;
|
||||
const corePackageJSON = svrjsCoreInfo.packageJSON;
|
||||
const isDev = process.env.NODE_ENV == "development";
|
||||
|
||||
|
@ -265,7 +264,7 @@ if (!isDev) {
|
|||
const packageJSON = Object.assign({}, corePackageJSON);
|
||||
|
||||
// Add package.json properties
|
||||
packageJSON.version = coreVersion;
|
||||
packageJSON.version = version;
|
||||
packageJSON.main = "./svr.core.js";
|
||||
packageJSON.dependencies = coreDependencyNames.reduce(
|
||||
(previousDependencies, dependency) => {
|
||||
|
|
41
src/core.js
41
src/core.js
|
@ -13,7 +13,6 @@ const statusCodes = require("./res/statusCodes.js");
|
|||
|
||||
const middleware = [
|
||||
require("./middleware/urlSanitizer.js"),
|
||||
require("./middleware/rewriteURL.js"),
|
||||
require("./middleware/redirectTrailingSlashes.js"),
|
||||
require("./middleware/defaultHandlerChecks.js"),
|
||||
require("./middleware/staticFileServingAndDirectoryListings.js")
|
||||
|
@ -591,6 +590,9 @@ function requestHandler(req, res, next) {
|
|||
? config.domain
|
||||
: "unknown.invalid")
|
||||
);
|
||||
|
||||
// req.originalParsedURL fallback
|
||||
req.originalParsedURL = req.parsedURL;
|
||||
} catch (err) {
|
||||
res.error(400, err);
|
||||
return;
|
||||
|
@ -626,6 +628,43 @@ function requestHandler(req, res, next) {
|
|||
|
||||
function init(config) {
|
||||
if (config) coreConfig = config;
|
||||
|
||||
if (coreConfig.users === undefined) coreConfig.users = [];
|
||||
if (coreConfig.page404 === undefined) coreConfig.page404 = "404.html";
|
||||
if (coreConfig.enableCompression === undefined)
|
||||
coreConfig.enableCompression = true;
|
||||
if (coreConfig.customHeaders === undefined) coreConfig.customHeaders = {};
|
||||
if (coreConfig.enableDirectoryListing === undefined)
|
||||
coreConfig.enableDirectoryListing = true;
|
||||
if (coreConfig.enableDirectoryListingWithDefaultHead === undefined)
|
||||
coreConfig.enableDirectoryListingWithDefaultHead = false;
|
||||
if (coreConfig.serverAdministratorEmail === undefined)
|
||||
coreConfig.serverAdministratorEmail = "[no contact information]";
|
||||
if (coreConfig.stackHidden === undefined) coreConfig.stackHidden = false;
|
||||
if (coreConfig.exposeServerVersion === undefined)
|
||||
coreConfig.exposeServerVersion = true;
|
||||
if (coreConfig.dontCompress === undefined)
|
||||
coreConfig.dontCompress = [
|
||||
"/.*\\.ipxe$/",
|
||||
"/.*\\.(?:jpe?g|png|bmp|tiff|jfif|gif|webp)$/",
|
||||
"/.*\\.(?:[id]mg|iso|flp)$/",
|
||||
"/.*\\.(?:zip|rar|bz2|[gb7x]z|lzma|tar)$/",
|
||||
"/.*\\.(?:mp[34]|mov|wm[av]|avi|webm|og[gv]|mk[va])$/"
|
||||
];
|
||||
if (coreConfig.enableIPSpoofing === undefined)
|
||||
coreConfig.enableIPSpoofing = false;
|
||||
if (coreConfig.enableETag === undefined) coreConfig.enableETag = true;
|
||||
if (coreConfig.rewriteDirtyURLs === undefined)
|
||||
coreConfig.rewriteDirtyURLs = false;
|
||||
if (coreConfig.errorPages === undefined) coreConfig.errorPages = [];
|
||||
if (coreConfig.disableTrailingSlashRedirects === undefined)
|
||||
coreConfig.disableTrailingSlashRedirects = false;
|
||||
if (coreConfig.allowDoubleSlashes === undefined)
|
||||
coreConfig.allowDoubleSlashes = false;
|
||||
|
||||
// You wouldn't use SVR.JS mods in SVR.JS Core
|
||||
coreConfig.exposeModsInErrorPages = false;
|
||||
|
||||
return requestHandler;
|
||||
}
|
||||
|
||||
|
|
|
@ -706,6 +706,9 @@ function requestHandler(req, res) {
|
|||
? config.domain
|
||||
: "unknown.invalid")
|
||||
);
|
||||
|
||||
// req.originalParsedURL fallback
|
||||
req.originalParsedURL = req.parsedURL;
|
||||
} catch (err) {
|
||||
res.error(400, err);
|
||||
return;
|
||||
|
|
|
@ -980,7 +980,32 @@ module.exports = (req, res, logFacilities, config, next) => {
|
|||
} else if (dirImagesMissing) {
|
||||
fs.stat(readFrom, (e, s) => {
|
||||
if (e || !s.isFile()) {
|
||||
properDirectoryListingAndStaticFileServe();
|
||||
if (err.code == "ENOENT") {
|
||||
res.error(404);
|
||||
logFacilities.errmessage("Resource not found.");
|
||||
return;
|
||||
} else if (err.code == "ENOTDIR") {
|
||||
res.error(404); // Assume that file doesn't exist.
|
||||
logFacilities.errmessage("Resource not found.");
|
||||
return;
|
||||
} else if (err.code == "EACCES") {
|
||||
res.error(403);
|
||||
logFacilities.errmessage("Access denied.");
|
||||
return;
|
||||
} else if (err.code == "ENAMETOOLONG") {
|
||||
res.error(414);
|
||||
return;
|
||||
} else if (err.code == "EMFILE") {
|
||||
res.error(503);
|
||||
return;
|
||||
} else if (err.code == "ELOOP") {
|
||||
res.error(508); // The symbolic link loop is detected during file system operations.
|
||||
logFacilities.errmessage("Symbolic link loop detected.");
|
||||
return;
|
||||
} else {
|
||||
res.error(500, err);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
stats = s;
|
||||
properDirectoryListingAndStaticFileServe();
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
const svrjsInfo = require("../../svrjs.core.json");
|
||||
const { version, name } = svrjsInfo;
|
||||
const svrjsCoreInfo = require("../../svrjs.core.json");
|
||||
const { name } = svrjsCoreInfo;
|
||||
const svrjsInfo = require("../../svrjs.json");
|
||||
const { version } = svrjsInfo;
|
||||
const getOS = require("./getOS.js");
|
||||
|
||||
function generateServerString(exposeServerVersion) {
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
{
|
||||
"version": "Nightly-GitNext",
|
||||
"name": "SVR.JS Core",
|
||||
"externalPackages": ["mime-types"],
|
||||
"packageJSON": {
|
||||
|
@ -18,6 +17,9 @@
|
|||
"middleware"
|
||||
],
|
||||
"homepage": "https://svrjs.org",
|
||||
"license": "MIT"
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue