first commit
This commit is contained in:
7
node_modules/hexo-server/LICENSE
generated
vendored
Normal file
7
node_modules/hexo-server/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
Copyright (c) 2014 Tommy Chen
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
58
node_modules/hexo-server/README.md
generated
vendored
Normal file
58
node_modules/hexo-server/README.md
generated
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
# hexo-server
|
||||
|
||||
[](https://github.com/hexojs/hexo-server/actions?query=workflow%3ATester)
|
||||
[](https://www.npmjs.com/package/hexo-server)
|
||||
[](https://coveralls.io/r/hexojs/hexo-server?branch=master)
|
||||
|
||||
Server module for [Hexo].
|
||||
|
||||
## Installation
|
||||
|
||||
``` bash
|
||||
$ npm install hexo-server --save
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
``` bash
|
||||
$ hexo server
|
||||
```
|
||||
|
||||
Option | Description | Default
|
||||
--- | --- | ---
|
||||
`-i`, `--ip` | Override the default server IP. | `::` when IPv6 is available, else `0.0.0.0` (note: in most systems, `::` also binds to `0.0.0.0`)
|
||||
`-p`, `--port` | Override the default port. | 4000
|
||||
`-s`, `--static` | Only serve static files. | false
|
||||
`-l`, `--log [format]` | Enable logger. Override log format. | false
|
||||
`-o`, `--open` | Immediately open the server url in your default web browser. | false
|
||||
|
||||
## Options
|
||||
|
||||
``` yaml
|
||||
server:
|
||||
port: 4000
|
||||
log: false
|
||||
ip: 0.0.0.0
|
||||
compress: false
|
||||
cache: false
|
||||
header: true
|
||||
serveStatic:
|
||||
extensions:
|
||||
- html
|
||||
```
|
||||
|
||||
- **port**: Server port
|
||||
- **log**: Display request info on the console. Always enabled in debug mode.
|
||||
- **ip**: Server IP
|
||||
- **compress**: Enable GZIP compression
|
||||
- **cache**: Enable cache for rendered content
|
||||
- This can speed up server response. However, any changes will no longer take effect in real time.
|
||||
- Suitable for production environment only.
|
||||
- **header**: Add `X-Powered-By: Hexo` header
|
||||
- **serveStatic**: Extra options passed to [serve-static](https://github.com/expressjs/serve-static#options)
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
||||
[Hexo]: http://hexo.io/
|
||||
30
node_modules/hexo-server/index.js
generated
vendored
Normal file
30
node_modules/hexo-server/index.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
/* global hexo */
|
||||
|
||||
'use strict';
|
||||
|
||||
hexo.config.server = Object.assign({
|
||||
port: 4000,
|
||||
log: false,
|
||||
// `undefined` uses Node's default (try `::` with fallback to `0.0.0.0`)
|
||||
ip: undefined,
|
||||
compress: false,
|
||||
header: true
|
||||
}, hexo.config.server);
|
||||
|
||||
hexo.extend.console.register('server', 'Start the server.', {
|
||||
desc: 'Start the server and watch for file changes.',
|
||||
options: [
|
||||
{name: '-i, --ip', desc: 'Override the default server IP. Bind to all IP address by default.'},
|
||||
{name: '-p, --port', desc: 'Override the default port.'},
|
||||
{name: '-s, --static', desc: 'Only serve static files.'},
|
||||
{name: '-l, --log [format]', desc: 'Enable logger. Override log format.'},
|
||||
{name: '-o, --open', desc: 'Immediately open the server url in your default web browser.'}
|
||||
]
|
||||
}, require('./lib/server'));
|
||||
|
||||
hexo.extend.filter.register('server_middleware', require('./lib/middlewares/header'));
|
||||
hexo.extend.filter.register('server_middleware', require('./lib/middlewares/gzip'));
|
||||
hexo.extend.filter.register('server_middleware', require('./lib/middlewares/logger'));
|
||||
hexo.extend.filter.register('server_middleware', require('./lib/middlewares/route'));
|
||||
hexo.extend.filter.register('server_middleware', require('./lib/middlewares/static'));
|
||||
hexo.extend.filter.register('server_middleware', require('./lib/middlewares/redirect'));
|
||||
10
node_modules/hexo-server/lib/middlewares/gzip.js
generated
vendored
Normal file
10
node_modules/hexo-server/lib/middlewares/gzip.js
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const compress = require('compression');
|
||||
|
||||
module.exports = function(app) {
|
||||
const config = this.config.server || {};
|
||||
if (!config.compress) return;
|
||||
|
||||
app.use(compress());
|
||||
};
|
||||
11
node_modules/hexo-server/lib/middlewares/header.js
generated
vendored
Normal file
11
node_modules/hexo-server/lib/middlewares/header.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(app) {
|
||||
const config = this.config.server || {};
|
||||
if (!config.header) return;
|
||||
|
||||
app.use((req, res, next) => {
|
||||
res.setHeader('X-Powered-By', 'Hexo');
|
||||
next();
|
||||
});
|
||||
};
|
||||
14
node_modules/hexo-server/lib/middlewares/logger.js
generated
vendored
Normal file
14
node_modules/hexo-server/lib/middlewares/logger.js
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
'use strict';
|
||||
|
||||
const morgan = require('morgan');
|
||||
|
||||
module.exports = function(app) {
|
||||
const { config } = this;
|
||||
const { args = {} } = this.env;
|
||||
let logger = args.l || args.log || config.server.log;
|
||||
|
||||
if (!logger && !args.debug) return;
|
||||
if (typeof logger !== 'string') logger = 'dev';
|
||||
|
||||
app.use(morgan(logger));
|
||||
};
|
||||
15
node_modules/hexo-server/lib/middlewares/redirect.js
generated
vendored
Normal file
15
node_modules/hexo-server/lib/middlewares/redirect.js
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function(app) {
|
||||
const { root } = this.config;
|
||||
if (root === '/') return;
|
||||
|
||||
// If root url is not `/`, redirect to the correct root url
|
||||
app.use((req, res, next) => {
|
||||
if (req.method !== 'GET' || req.url !== '/') return next();
|
||||
|
||||
res.statusCode = 302;
|
||||
res.setHeader('Location', root);
|
||||
res.end('Redirecting');
|
||||
});
|
||||
};
|
||||
87
node_modules/hexo-server/lib/middlewares/route.js
generated
vendored
Normal file
87
node_modules/hexo-server/lib/middlewares/route.js
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
'use strict';
|
||||
|
||||
const pathFn = require('path');
|
||||
const mime = require('mime');
|
||||
|
||||
module.exports = function(app) {
|
||||
const { config, route } = this;
|
||||
const { args = {} } = this.env;
|
||||
const { root, feed } = config;
|
||||
|
||||
if (args.s || args.static) return;
|
||||
|
||||
const { pretty_urls } = config;
|
||||
const { trailing_index, trailing_html } = pretty_urls ? pretty_urls : {};
|
||||
|
||||
app.use(root, (req, res, next) => {
|
||||
const { method, url: requestUrl } = req;
|
||||
if (method !== 'GET' && method !== 'HEAD') return next();
|
||||
|
||||
let url = route.format(decodeURIComponent(requestUrl));
|
||||
let data = route.get(url);
|
||||
let extname = pathFn.extname(url);
|
||||
|
||||
if (!data) {
|
||||
if (route.get(url + '.html')) {
|
||||
// location `foo/bar.html`; request `foo/bar`; proxy to the location
|
||||
extname = '.html';
|
||||
data = route.get(url + extname);
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
req.url = encodeURI('/' + url + extname);
|
||||
data.pipe(res).on('error', next);
|
||||
return;
|
||||
} else if (route.get(url + '/index.html')) {
|
||||
// location `foo/index.html`; request `foo`; redirect to `foo/`
|
||||
url = encodeURI(url);
|
||||
res.statusCode = 301;
|
||||
res.setHeader('Location', `${root + url}/`);
|
||||
res.end('Redirecting');
|
||||
return;
|
||||
} else if (route.get(url.replace(/\/index\.html$/i, '') + '.html')) {
|
||||
// location `foo/bar.html`; request `foo/bar/`; redirect to `foo`
|
||||
// request with trailing slash is appended with index.html by route.format()
|
||||
url = encodeURI(url.replace(/\/index\.html$/i, ''));
|
||||
res.statusCode = 301;
|
||||
res.setHeader('Location', root + url);
|
||||
res.end('Redirecting');
|
||||
return;
|
||||
} return next();
|
||||
}
|
||||
if (trailing_html === false && !requestUrl.endsWith('/index.html') && requestUrl.endsWith('.html')) {
|
||||
// location `foo/bar.html`; request `foo/bar.html`; redirect to `foo/bar`
|
||||
url = encodeURI(url.replace(/\.html$/i, ''));
|
||||
res.statusCode = 301;
|
||||
res.setHeader('Location', root + url);
|
||||
res.end('Redirecting');
|
||||
return;
|
||||
} else if (trailing_index === false && requestUrl.endsWith('/index.html')) {
|
||||
// location `foo/index.html`; request `foo/index.html`; redirect to `foo/`
|
||||
url = encodeURI(url.replace(/index\.html$/i, ''));
|
||||
res.statusCode = 301;
|
||||
res.setHeader('Location', root + url);
|
||||
res.end('Redirecting');
|
||||
return;
|
||||
}
|
||||
|
||||
// Do not overwrite header if already set by another middleware
|
||||
if (!res.hasHeader('Content-Type')) {
|
||||
if (feed && feed.path === url) {
|
||||
if (feed.type === 'atom') {
|
||||
res.setHeader('Content-Type', 'application/atom+xml');
|
||||
} else if (feed.type === 'rss') {
|
||||
res.setHeader('Content-Type', 'application/rss+xml');
|
||||
} else {
|
||||
res.setHeader('Content-Type', extname ? mime.getType(extname) : 'application/octet-stream');
|
||||
}
|
||||
} else {
|
||||
res.setHeader('Content-Type', extname ? mime.getType(extname) : 'application/octet-stream');
|
||||
}
|
||||
}
|
||||
|
||||
if (method === 'GET') {
|
||||
data.pipe(res).on('error', next);
|
||||
} else {
|
||||
res.end();
|
||||
}
|
||||
});
|
||||
};
|
||||
7
node_modules/hexo-server/lib/middlewares/static.js
generated
vendored
Normal file
7
node_modules/hexo-server/lib/middlewares/static.js
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const serveStatic = require('serve-static');
|
||||
|
||||
module.exports = function(app) {
|
||||
app.use(this.config.root, serveStatic(this.public_dir, this.config.server.serveStatic));
|
||||
};
|
||||
80
node_modules/hexo-server/lib/server.js
generated
vendored
Normal file
80
node_modules/hexo-server/lib/server.js
generated
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
'use strict';
|
||||
|
||||
const connect = require('connect');
|
||||
const http = require('http');
|
||||
const { underline } = require('picocolors');
|
||||
const Promise = require('bluebird');
|
||||
const open = require('open');
|
||||
const net = require('net');
|
||||
|
||||
module.exports = function(args) {
|
||||
const app = connect();
|
||||
const { config } = this;
|
||||
const ip = args.i || args.ip || config.server.ip || undefined;
|
||||
const port = parseInt(args.p || args.port || config.server.port || process.env.port, 10) || 4000;
|
||||
const { root } = config;
|
||||
|
||||
return checkPort(ip, port).then(() => this.extend.filter.exec('server_middleware', app, {context: this})).then(() => {
|
||||
if (args.s || args.static) {
|
||||
return this.load();
|
||||
}
|
||||
|
||||
return this.watch();
|
||||
}).then(() => startServer(http.createServer(app), port, ip)).then(server => {
|
||||
const addr = server.address();
|
||||
const addrString = formatAddress(ip || addr.address, addr.port, root);
|
||||
|
||||
this.log.info('Hexo is running at %s . Press Ctrl+C to stop.', underline(addrString));
|
||||
this.emit('server');
|
||||
|
||||
if (args.o || args.open) {
|
||||
open(addrString);
|
||||
}
|
||||
|
||||
return server;
|
||||
}).catch(err => {
|
||||
switch (err.code) {
|
||||
case 'EADDRINUSE':
|
||||
this.log.fatal(`Port ${port} has been used. Try other port instead.`);
|
||||
break;
|
||||
|
||||
case 'EACCES':
|
||||
this.log.fatal(`Permission denied. You can't use port ${port}.`);
|
||||
break;
|
||||
}
|
||||
|
||||
this.unwatch();
|
||||
throw err;
|
||||
});
|
||||
};
|
||||
|
||||
function startServer(server, port, ip) {
|
||||
return new Promise((resolve, reject) => {
|
||||
server.listen(port, ip, resolve);
|
||||
server.on('error', reject);
|
||||
}).then(() => server);
|
||||
}
|
||||
|
||||
function checkPort(ip, port) {
|
||||
if (port > 65535 || port < 1) {
|
||||
return Promise.reject(new RangeError(`Port number ${port} is invalid. Try a number between 1 and 65535.`));
|
||||
}
|
||||
|
||||
const server = net.createServer();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
server.once('error', reject);
|
||||
server.once('listening', resolve);
|
||||
server.listen(port, ip);
|
||||
}).then(() => { server.close(); });
|
||||
}
|
||||
|
||||
function formatAddress(ip, port, root) {
|
||||
let hostname = ip;
|
||||
if (ip === '0.0.0.0' || ip === '::') {
|
||||
hostname = 'localhost';
|
||||
}
|
||||
|
||||
let path = root.startsWith("/") ? root : `/${root}`;
|
||||
return new URL(`http://${hostname}:${port}${path}`).toString();
|
||||
}
|
||||
57
node_modules/hexo-server/package.json
generated
vendored
Normal file
57
node_modules/hexo-server/package.json
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
{
|
||||
"name": "hexo-server",
|
||||
"version": "3.0.0",
|
||||
"description": "Server module of Hexo.",
|
||||
"main": "index",
|
||||
"scripts": {
|
||||
"eslint": "eslint .",
|
||||
"test": "mocha test/index.js",
|
||||
"test-cov": "nyc --reporter=lcovonly npm run test"
|
||||
},
|
||||
"directories": {
|
||||
"lib": "./lib"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"lib/"
|
||||
],
|
||||
"repository": "hexojs/hexo-server",
|
||||
"homepage": "http://hexo.io/",
|
||||
"keywords": [
|
||||
"hexo",
|
||||
"server",
|
||||
"connect"
|
||||
],
|
||||
"author": "Tommy Chen <tommy351@gmail.com> (http://zespia.tw)",
|
||||
"maintainers": [
|
||||
"Abner Chou <hi@abnerchou.me> (http://abnerchou.me)"
|
||||
],
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"bluebird": "^3.5.5",
|
||||
"compression": "^1.7.4",
|
||||
"connect": "^3.7.0",
|
||||
"mime": "^3.0.0",
|
||||
"morgan": "^1.9.1",
|
||||
"picocolors": "^1.0.0",
|
||||
"open": "^8.0.9",
|
||||
"serve-static": "^1.14.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "^4.2.0",
|
||||
"chai-as-promised": "^7.1.1",
|
||||
"eslint": "^8.1.0",
|
||||
"eslint-config-hexo": "^4.0.0",
|
||||
"hexo": "^6.0.0",
|
||||
"hexo-fs": "^3.0.1",
|
||||
"hexo-util": "^2.1.0",
|
||||
"mocha": "^9.1.3",
|
||||
"nyc": "^15.0.0",
|
||||
"sinon": "^12.0.1",
|
||||
"supertest": "^6.1.3",
|
||||
"uuid": "^8.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12.13.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user