@tryghost/mw-vhost 1.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013-2025 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 ADDED
@@ -0,0 +1,147 @@
1
+ # Vhost Middleware
2
+
3
+ Forked from https://github.com/expressjs/vhost/ which appears abandoned.
4
+
5
+ ## API
6
+
7
+ <!-- eslint-disable no-unused-vars -->
8
+
9
+ ```js
10
+ var vhost = require('vhost')
11
+ ```
12
+
13
+ ### vhost(hostname, handle)
14
+
15
+ Create a new middleware function to hand off request to `handle` when the incoming
16
+ host for the request matches `hostname`. The function is called as
17
+ `handle(req, res, next)`, like a standard middleware.
18
+
19
+ `hostname` can be a string or a RegExp object. When `hostname` is a string it can
20
+ contain `*` to match 1 or more characters in that section of the hostname. When
21
+ `hostname` is a RegExp, it will be forced to case-insensitive (since hostnames are)
22
+ and will be forced to match based on the start and end of the hostname.
23
+
24
+ When host is matched and the request is sent down to a vhost handler, the `req.vhost`
25
+ property will be populated with an object. This object will have numeric properties
26
+ corresponding to each wildcard (or capture group if RegExp object provided) and the
27
+ `hostname` that was matched.
28
+
29
+ Where the `hostname` of the request comes from depends on the type of server you're running.
30
+ If you're running a raw Node.js/connect server, this comes from [`req.headers.host`](https://nodejs.org/dist/latest/docs/api/http.html#http_message_headers).
31
+ If you're running an express v4 server, this comes from [`req.hostname`](http://expressjs.com/en/4x/api.html#req.hostname).
32
+
33
+ ```js
34
+ var connect = require('connect')
35
+ var vhost = require('vhost')
36
+ var app = connect()
37
+
38
+ app.use(vhost('*.*.example.com', function handle (req, res, next) {
39
+ // for match of "foo.bar.example.com:8080" against "*.*.example.com":
40
+ console.dir(req.vhost.host) // => 'foo.bar.example.com:8080'
41
+ console.dir(req.vhost.hostname) // => 'foo.bar.example.com'
42
+ console.dir(req.vhost.length) // => 2
43
+ console.dir(req.vhost[0]) // => 'foo'
44
+ console.dir(req.vhost[1]) // => 'bar'
45
+ }))
46
+ ```
47
+
48
+ ## Examples
49
+
50
+ ### using with connect for static serving
51
+
52
+ ```js
53
+ var connect = require('connect')
54
+ var serveStatic = require('serve-static')
55
+ var vhost = require('vhost')
56
+
57
+ var mailapp = connect()
58
+
59
+ // add middlewares to mailapp for mail.example.com
60
+
61
+ // create app to serve static files on subdomain
62
+ var staticapp = connect()
63
+ staticapp.use(serveStatic('public'))
64
+
65
+ // create main app
66
+ var app = connect()
67
+
68
+ // add vhost routing to main app for mail
69
+ app.use(vhost('mail.example.com', mailapp))
70
+
71
+ // route static assets for "assets-*" subdomain to get
72
+ // around max host connections limit on browsers
73
+ app.use(vhost('assets-*.example.com', staticapp))
74
+
75
+ // add middlewares and main usage to app
76
+
77
+ app.listen(3000)
78
+ ```
79
+
80
+ ### using with connect for user subdomains
81
+
82
+ ```js
83
+ var connect = require('connect')
84
+ var serveStatic = require('serve-static')
85
+ var vhost = require('vhost')
86
+
87
+ var mainapp = connect()
88
+
89
+ // add middlewares to mainapp for the main web site
90
+
91
+ // create app that will server user content from public/{username}/
92
+ var userapp = connect()
93
+
94
+ userapp.use(function (req, res, next) {
95
+ var username = req.vhost[0] // username is the "*"
96
+
97
+ // pretend request was for /{username}/* for file serving
98
+ req.originalUrl = req.url
99
+ req.url = '/' + username + req.url
100
+
101
+ next()
102
+ })
103
+ userapp.use(serveStatic('public'))
104
+
105
+ // create main app
106
+ var app = connect()
107
+
108
+ // add vhost routing for main app
109
+ app.use(vhost('userpages.local', mainapp))
110
+ app.use(vhost('www.userpages.local', mainapp))
111
+
112
+ // listen on all subdomains for user pages
113
+ app.use(vhost('*.userpages.local', userapp))
114
+
115
+ app.listen(3000)
116
+ ```
117
+
118
+ ### using with any generic request handler
119
+
120
+ ```js
121
+ var connect = require('connect')
122
+ var http = require('http')
123
+ var vhost = require('vhost')
124
+
125
+ // create main app
126
+ var app = connect()
127
+
128
+ app.use(vhost('mail.example.com', function (req, res) {
129
+ // handle req + res belonging to mail.example.com
130
+ res.setHeader('Content-Type', 'text/plain')
131
+ res.end('hello from mail!')
132
+ }))
133
+
134
+ // an external api server in any framework
135
+ var httpServer = http.createServer(function (req, res) {
136
+ res.setHeader('Content-Type', 'text/plain')
137
+ res.end('hello from the api!')
138
+ })
139
+
140
+ app.use(vhost('api.example.com', function (req, res) {
141
+ // handle req + res belonging to api.example.com
142
+ // pass the request to a standard Node.js HTTP server
143
+ httpServer.emit('request', req, res)
144
+ }))
145
+
146
+ app.listen(3000)
147
+ ```
package/index.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./lib/vhost');
package/lib/vhost.js ADDED
@@ -0,0 +1,171 @@
1
+ // This a fork of expressjs/vhost with trust proxy support
2
+ /* eslint-disable */
3
+
4
+ /*!
5
+ * vhost
6
+ * Copyright(c) 2014 Jonathan Ong
7
+ * Copyright(c) 2014-2015 Douglas Christopher Wilson
8
+ * MIT Licensed
9
+ */
10
+
11
+
12
+ 'use strict';
13
+
14
+ /**
15
+ * Module exports.
16
+ * @public
17
+ */
18
+
19
+ module.exports = vhost;
20
+
21
+ /**
22
+ * Module variables.
23
+ * @private
24
+ */
25
+
26
+ var ASTERISK_REGEXP = /\*/g;
27
+ var ASTERISK_REPLACE = '([^.]+)';
28
+ var END_ANCHORED_REGEXP = /(?:^|[^\\])(?:\\\\)*\$$/;
29
+ var ESCAPE_REGEXP = /([.+?^=!:${}()|[\]/\\])/g;
30
+ var ESCAPE_REPLACE = '\\$1';
31
+
32
+ /**
33
+ * Create a vhost middleware.
34
+ *
35
+ * @param {string|RegExp} hostname
36
+ * @param {function} handle
37
+ * @return {import('express').RequestHandler}
38
+ * @public
39
+ */
40
+
41
+ function vhost(hostname, handle) {
42
+ if (!hostname) {
43
+ throw new TypeError('argument hostname is required');
44
+ }
45
+
46
+ if (!handle) {
47
+ throw new TypeError('argument handle is required');
48
+ }
49
+
50
+ if (typeof handle !== 'function') {
51
+ throw new TypeError('argument handle must be a function');
52
+ }
53
+
54
+ // create regular expression for hostname
55
+ var regexp = hostregexp(hostname);
56
+
57
+ return function vhost(req, res, next) {
58
+ var vhostdata = vhostof(req, regexp);
59
+
60
+ if (!vhostdata) {
61
+ return next();
62
+ }
63
+
64
+ // populate
65
+ req.vhost = vhostdata;
66
+
67
+ // handle
68
+ handle(req, res, next);
69
+ };
70
+ }
71
+
72
+ /**
73
+ * Get hostname of request.
74
+ *
75
+ * @param {object} req
76
+ * @return {string}
77
+ * @private
78
+ */
79
+
80
+ function hostnameof(req) {
81
+ var host =
82
+ req.hostname || // express v4
83
+ req.host || // express v3
84
+ req.headers.host; // http
85
+
86
+ if (!host) {
87
+ return;
88
+ }
89
+
90
+ var offset = host[0] === '['
91
+ ? host.indexOf(']') + 1
92
+ : 0;
93
+ var index = host.indexOf(':', offset);
94
+
95
+ return index !== -1
96
+ ? host.substring(0, index)
97
+ : host;
98
+ }
99
+
100
+ /**
101
+ * Determine if object is RegExp.
102
+ *
103
+ * @param {object} val
104
+ * @return {boolean}
105
+ * @private
106
+ */
107
+
108
+ function isregexp(val) {
109
+ return Object.prototype.toString.call(val) === '[object RegExp]';
110
+ }
111
+
112
+ /**
113
+ * Generate RegExp for given hostname value.
114
+ *
115
+ * @param {string|RegExp} val
116
+ * @private
117
+ */
118
+
119
+ function hostregexp(val) {
120
+ var source = !isregexp(val)
121
+ ? String(val).replace(ESCAPE_REGEXP, ESCAPE_REPLACE).replace(ASTERISK_REGEXP, ASTERISK_REPLACE)
122
+ : val.source;
123
+
124
+ // force leading anchor matching
125
+ if (source[0] !== '^') {
126
+ source = '^' + source;
127
+ }
128
+
129
+ // force trailing anchor matching
130
+ if (!END_ANCHORED_REGEXP.test(source)) {
131
+ source += '$';
132
+ }
133
+
134
+ return new RegExp(source, 'i');
135
+ }
136
+
137
+ /**
138
+ * Get the vhost data of the request for RegExp
139
+ *
140
+ * @param {object} req
141
+ * @param {RegExp} regexp
142
+ * @return {object}
143
+ * @private
144
+ */
145
+
146
+ function vhostof(req, regexp) {
147
+ var host = req.headers.host;
148
+ var hostname = hostnameof(req);
149
+
150
+ if (!hostname) {
151
+ return;
152
+ }
153
+
154
+ var match = regexp.exec(hostname);
155
+
156
+ if (!match) {
157
+ return;
158
+ }
159
+
160
+ var obj = Object.create(null);
161
+
162
+ obj.host = host;
163
+ obj.hostname = hostname;
164
+ obj.length = match.length - 1;
165
+
166
+ for (var i = 1; i < match.length; i++) {
167
+ obj[i - 1] = match[i];
168
+ }
169
+
170
+ return obj;
171
+ }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@tryghost/mw-vhost",
3
+ "version": "1.0.0",
4
+ "author": "Ghost Foundation",
5
+ "license": "MIT",
6
+ "main": "index.js",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "scripts": {
11
+ "dev": "echo \"Implement me!\"",
12
+ "test:unit": "NODE_ENV=testing c8 --all --reporter text --reporter cobertura -- mocha --reporter dot './test/**/*.test.js'",
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"
17
+ },
18
+ "files": [
19
+ "index.js",
20
+ "lib"
21
+ ],
22
+ "devDependencies": {
23
+ "c8": "8.0.1",
24
+ "mocha": "10.8.2",
25
+ "supertest": "6.3.4"
26
+ },
27
+ "gitHead": "feac71f0032c5d92f56a8dfa7ccd5a2d883ad53d"
28
+ }