@tryghost/mw-vhost 3.0.2 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +66 -61
- package/lib/vhost.js +5 -8
- package/package.json +16 -18
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013-2026 Ghost Foundation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
|
|
5
5
|
Virtual-host Express middleware (forked from `vhost`) with Ghost-specific trust-proxy behavior.
|
|
6
6
|
|
|
7
|
-
|
|
8
7
|
Forked from https://github.com/expressjs/vhost/ which appears abandoned.
|
|
9
8
|
|
|
10
9
|
## API
|
|
@@ -12,7 +11,7 @@ Forked from https://github.com/expressjs/vhost/ which appears abandoned.
|
|
|
12
11
|
<!-- eslint-disable no-unused-vars -->
|
|
13
12
|
|
|
14
13
|
```js
|
|
15
|
-
var vhost = require('vhost')
|
|
14
|
+
var vhost = require('vhost');
|
|
16
15
|
```
|
|
17
16
|
|
|
18
17
|
### vhost(hostname, handle)
|
|
@@ -36,18 +35,20 @@ If you're running a raw Node.js/connect server, this comes from [`req.headers.ho
|
|
|
36
35
|
If you're running an express v4 server, this comes from [`req.hostname`](http://expressjs.com/en/4x/api.html#req.hostname).
|
|
37
36
|
|
|
38
37
|
```js
|
|
39
|
-
var connect = require('connect')
|
|
40
|
-
var vhost = require('vhost')
|
|
41
|
-
var app = connect()
|
|
42
|
-
|
|
43
|
-
app.use(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
38
|
+
var connect = require('connect');
|
|
39
|
+
var vhost = require('vhost');
|
|
40
|
+
var app = connect();
|
|
41
|
+
|
|
42
|
+
app.use(
|
|
43
|
+
vhost('*.*.example.com', function handle(req, res, next) {
|
|
44
|
+
// for match of "foo.bar.example.com:8080" against "*.*.example.com":
|
|
45
|
+
console.dir(req.vhost.host); // => 'foo.bar.example.com:8080'
|
|
46
|
+
console.dir(req.vhost.hostname); // => 'foo.bar.example.com'
|
|
47
|
+
console.dir(req.vhost.length); // => 2
|
|
48
|
+
console.dir(req.vhost[0]); // => 'foo'
|
|
49
|
+
console.dir(req.vhost[1]); // => 'bar'
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
51
52
|
```
|
|
52
53
|
|
|
53
54
|
## Examples
|
|
@@ -55,98 +56,102 @@ app.use(vhost('*.*.example.com', function handle (req, res, next) {
|
|
|
55
56
|
### using with connect for static serving
|
|
56
57
|
|
|
57
58
|
```js
|
|
58
|
-
var connect = require('connect')
|
|
59
|
-
var serveStatic = require('serve-static')
|
|
60
|
-
var vhost = require('vhost')
|
|
59
|
+
var connect = require('connect');
|
|
60
|
+
var serveStatic = require('serve-static');
|
|
61
|
+
var vhost = require('vhost');
|
|
61
62
|
|
|
62
|
-
var mailapp = connect()
|
|
63
|
+
var mailapp = connect();
|
|
63
64
|
|
|
64
65
|
// add middlewares to mailapp for mail.example.com
|
|
65
66
|
|
|
66
67
|
// create app to serve static files on subdomain
|
|
67
|
-
var staticapp = connect()
|
|
68
|
-
staticapp.use(serveStatic('public'))
|
|
68
|
+
var staticapp = connect();
|
|
69
|
+
staticapp.use(serveStatic('public'));
|
|
69
70
|
|
|
70
71
|
// create main app
|
|
71
|
-
var app = connect()
|
|
72
|
+
var app = connect();
|
|
72
73
|
|
|
73
74
|
// add vhost routing to main app for mail
|
|
74
|
-
app.use(vhost('mail.example.com', mailapp))
|
|
75
|
+
app.use(vhost('mail.example.com', mailapp));
|
|
75
76
|
|
|
76
77
|
// route static assets for "assets-*" subdomain to get
|
|
77
78
|
// around max host connections limit on browsers
|
|
78
|
-
app.use(vhost('assets-*.example.com', staticapp))
|
|
79
|
+
app.use(vhost('assets-*.example.com', staticapp));
|
|
79
80
|
|
|
80
81
|
// add middlewares and main usage to app
|
|
81
82
|
|
|
82
|
-
app.listen(3000)
|
|
83
|
+
app.listen(3000);
|
|
83
84
|
```
|
|
84
85
|
|
|
85
86
|
### using with connect for user subdomains
|
|
86
87
|
|
|
87
88
|
```js
|
|
88
|
-
var connect = require('connect')
|
|
89
|
-
var serveStatic = require('serve-static')
|
|
90
|
-
var vhost = require('vhost')
|
|
89
|
+
var connect = require('connect');
|
|
90
|
+
var serveStatic = require('serve-static');
|
|
91
|
+
var vhost = require('vhost');
|
|
91
92
|
|
|
92
|
-
var mainapp = connect()
|
|
93
|
+
var mainapp = connect();
|
|
93
94
|
|
|
94
95
|
// add middlewares to mainapp for the main web site
|
|
95
96
|
|
|
96
97
|
// create app that will server user content from public/{username}/
|
|
97
|
-
var userapp = connect()
|
|
98
|
+
var userapp = connect();
|
|
98
99
|
|
|
99
100
|
userapp.use(function (req, res, next) {
|
|
100
|
-
|
|
101
|
+
var username = req.vhost[0]; // username is the "*"
|
|
101
102
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
103
|
+
// pretend request was for /{username}/* for file serving
|
|
104
|
+
req.originalUrl = req.url;
|
|
105
|
+
req.url = '/' + username + req.url;
|
|
105
106
|
|
|
106
|
-
|
|
107
|
-
})
|
|
108
|
-
userapp.use(serveStatic('public'))
|
|
107
|
+
next();
|
|
108
|
+
});
|
|
109
|
+
userapp.use(serveStatic('public'));
|
|
109
110
|
|
|
110
111
|
// create main app
|
|
111
|
-
var app = connect()
|
|
112
|
+
var app = connect();
|
|
112
113
|
|
|
113
114
|
// add vhost routing for main app
|
|
114
|
-
app.use(vhost('userpages.local', mainapp))
|
|
115
|
-
app.use(vhost('www.userpages.local', mainapp))
|
|
115
|
+
app.use(vhost('userpages.local', mainapp));
|
|
116
|
+
app.use(vhost('www.userpages.local', mainapp));
|
|
116
117
|
|
|
117
118
|
// listen on all subdomains for user pages
|
|
118
|
-
app.use(vhost('*.userpages.local', userapp))
|
|
119
|
+
app.use(vhost('*.userpages.local', userapp));
|
|
119
120
|
|
|
120
|
-
app.listen(3000)
|
|
121
|
+
app.listen(3000);
|
|
121
122
|
```
|
|
122
123
|
|
|
123
124
|
### using with any generic request handler
|
|
124
125
|
|
|
125
126
|
```js
|
|
126
|
-
var connect = require('connect')
|
|
127
|
-
var http = require('http')
|
|
128
|
-
var vhost = require('vhost')
|
|
127
|
+
var connect = require('connect');
|
|
128
|
+
var http = require('http');
|
|
129
|
+
var vhost = require('vhost');
|
|
129
130
|
|
|
130
131
|
// create main app
|
|
131
|
-
var app = connect()
|
|
132
|
+
var app = connect();
|
|
132
133
|
|
|
133
|
-
app.use(
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
134
|
+
app.use(
|
|
135
|
+
vhost('mail.example.com', function (req, res) {
|
|
136
|
+
// handle req + res belonging to mail.example.com
|
|
137
|
+
res.setHeader('Content-Type', 'text/plain');
|
|
138
|
+
res.end('hello from mail!');
|
|
139
|
+
}),
|
|
140
|
+
);
|
|
138
141
|
|
|
139
142
|
// an external api server in any framework
|
|
140
143
|
var httpServer = http.createServer(function (req, res) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
})
|
|
144
|
-
|
|
145
|
-
app.use(
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
144
|
+
res.setHeader('Content-Type', 'text/plain');
|
|
145
|
+
res.end('hello from the api!');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
app.use(
|
|
149
|
+
vhost('api.example.com', function (req, res) {
|
|
150
|
+
// handle req + res belonging to api.example.com
|
|
151
|
+
// pass the request to a standard Node.js HTTP server
|
|
152
|
+
httpServer.emit('request', req, res);
|
|
153
|
+
}),
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
app.listen(3000);
|
|
152
157
|
```
|
package/lib/vhost.js
CHANGED
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
* MIT Licensed
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
|
|
12
11
|
'use strict';
|
|
13
12
|
|
|
14
13
|
/**
|
|
@@ -87,14 +86,10 @@ function hostnameof(req) {
|
|
|
87
86
|
return;
|
|
88
87
|
}
|
|
89
88
|
|
|
90
|
-
var offset = host[0] === '['
|
|
91
|
-
? host.indexOf(']') + 1
|
|
92
|
-
: 0;
|
|
89
|
+
var offset = host[0] === '[' ? host.indexOf(']') + 1 : 0;
|
|
93
90
|
var index = host.indexOf(':', offset);
|
|
94
91
|
|
|
95
|
-
return index !== -1
|
|
96
|
-
? host.substring(0, index)
|
|
97
|
-
: host;
|
|
92
|
+
return index !== -1 ? host.substring(0, index) : host;
|
|
98
93
|
}
|
|
99
94
|
|
|
100
95
|
/**
|
|
@@ -118,7 +113,9 @@ function isregexp(val) {
|
|
|
118
113
|
|
|
119
114
|
function hostregexp(val) {
|
|
120
115
|
var source = !isregexp(val)
|
|
121
|
-
? String(val)
|
|
116
|
+
? String(val)
|
|
117
|
+
.replace(ESCAPE_REGEXP, ESCAPE_REPLACE)
|
|
118
|
+
.replace(ASTERISK_REGEXP, ASTERISK_REPLACE)
|
|
122
119
|
: val.source;
|
|
123
120
|
|
|
124
121
|
// force leading anchor matching
|
package/package.json
CHANGED
|
@@ -1,30 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/mw-vhost",
|
|
3
|
-
"version": "3.0
|
|
4
|
-
"author": "Ghost Foundation",
|
|
3
|
+
"version": "3.1.0",
|
|
5
4
|
"license": "MIT",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"dev": "echo \"Implement me!\"",
|
|
12
|
-
"test:unit": "NODE_ENV=testing vitest run --coverage --config ../../vitest.config.ts",
|
|
13
|
-
"test": "yarn test:unit",
|
|
14
|
-
"lint:code": "eslint *.js lib/ --ext .js --cache",
|
|
15
|
-
"lint": "yarn lint:code && yarn lint:test",
|
|
16
|
-
"lint:test": "eslint -c test/.eslintrc.js test/ --ext .js --cache"
|
|
5
|
+
"author": "Ghost Foundation",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/TryGhost/framework.git",
|
|
9
|
+
"directory": "packages/mw-vhost"
|
|
17
10
|
},
|
|
18
11
|
"files": [
|
|
19
12
|
"index.js",
|
|
20
13
|
"lib"
|
|
21
14
|
],
|
|
15
|
+
"main": "index.js",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
22
19
|
"devDependencies": {
|
|
23
20
|
"supertest": "7.2.2"
|
|
24
21
|
},
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
22
|
+
"scripts": {
|
|
23
|
+
"dev": "echo \"Implement me!\"",
|
|
24
|
+
"test:unit": "NODE_ENV=testing vitest run --coverage --config ../../vitest.config.ts",
|
|
25
|
+
"test": "pnpm run test:unit",
|
|
26
|
+
"lint": "oxlint -c ../../.oxlintrc.json ."
|
|
29
27
|
}
|
|
30
|
-
}
|
|
28
|
+
}
|