@nohost/router
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -5,3 +5,3 @@ const url = require('url'); | ||
const nohostConn = require('@nohost/connect'); | ||
const { getServers, isFinished, decode, getJSON } = require('./util'); | ||
const { getServers, isFinished, decode, getJSON, addPlugins } = require('./util'); | ||
const { connect, writeError, writeHead, writeBody } = require('./connect'); | ||
@@ -19,9 +19,2 @@ | ||
const CLIENT_ID = 'x-whistle-client-id'; | ||
const AUTH_URL = 'x-whistle-.script-auth-url'; | ||
const REQ_RULES_URL = 'x-whistle-.script-req-rules-url'; | ||
const RES_RULES_URL = 'x-whistle-.script-res-rules-url'; | ||
const STATS_URL = 'x-whistle-.script-stats-url'; | ||
const DATA_URL = 'x-whistle-.script-data-url'; | ||
const POLICY = 'x-whistle-.script-policy'; | ||
const URL_RE = /^http:(?:\/\/|%3A%2F%2F)[\w.-]/; | ||
const CLIENT_ID_FILTER = 'x-whistle-filter-client-id'; | ||
@@ -66,31 +59,7 @@ const ROUTE_RE = /([?&])route=([^?&]+)($|&)/; | ||
addHeader(CLIENT_ID, options.clientId); | ||
let useRemote; | ||
if (URL_RE.test(options.authUrl)) { | ||
addHeader(AUTH_URL, options.authUrl); | ||
useRemote = true; | ||
} | ||
const rulesUrl = options.rulesUrl || options.reqRulesUrl; | ||
if (URL_RE.test(rulesUrl)) { | ||
addHeader(REQ_RULES_URL, rulesUrl); | ||
useRemote = true; | ||
} | ||
if (URL_RE.test(options.resRulesUrl)) { | ||
addHeader(RES_RULES_URL, options.resRulesUrl); | ||
useRemote = true; | ||
} | ||
if (URL_RE.test(options.statsUrl)) { | ||
addHeader(STATS_URL, options.statsUrl); | ||
useRemote = true; | ||
} | ||
if (URL_RE.test(options.dataUrl)) { | ||
addHeader(DATA_URL, options.dataUrl); | ||
useRemote = true; | ||
} | ||
if (useRemote) { | ||
headers[POLICY] = 'remote'; | ||
} | ||
addPlugins(headers, options.plugins || options); | ||
} | ||
addHeader(SPACE_NAME, options.spaceName); | ||
addHeader(GROUP_NAME, options.groupName); | ||
addHeader(ENV_NAME, options.envName); | ||
addHeader(SPACE_NAME, options.spaceName || options.space); | ||
addHeader(GROUP_NAME, options.groupName || options.group); | ||
addHeader(ENV_NAME, options.envName || options.env); | ||
}; | ||
@@ -212,8 +181,12 @@ | ||
} | ||
const { headers, isUIRequest } = req; | ||
if (this._nohostAddress) { | ||
if (req.isUIRequest) { | ||
req.headers['x-whistle-nohost-ui'] = 1; | ||
} | ||
// options?.host 兼容性? | ||
const server = options && options.host ? options : this._nohostAddress; | ||
if (isUIRequest) { | ||
headers['x-whistle-nohost-ui'] = 1; | ||
headers['x-whistle-real-host'] = 'local.whistlejs.com'; | ||
} else { | ||
addPlugins(headers, server.plugins || server); | ||
} | ||
if (callback) { | ||
@@ -232,3 +205,2 @@ callback(server); | ||
addOptions(req, options); | ||
const { headers } = req; | ||
const space = headers[SPACE_NAME]; | ||
@@ -238,3 +210,3 @@ const group = headers[GROUP_NAME]; | ||
if (!space || !group) { | ||
if (req.isUIRequest) { | ||
if (isUIRequest) { | ||
throw new Error('space & group is required.'); | ||
@@ -246,3 +218,3 @@ } | ||
let needRoute; | ||
if (req.isUIRequest && ROUTE_RE.test(req.url)) { | ||
if (isUIRequest && ROUTE_RE.test(req.url)) { | ||
let host = decode(RegExp.$2); | ||
@@ -264,3 +236,3 @@ if (host === '!required!') { | ||
const env = `$${status.index}`; | ||
if (req.isUIRequest) { | ||
if (isUIRequest) { | ||
headers['x-whistle-nohost-ui'] = 1; | ||
@@ -267,0 +239,0 @@ headers['x-whistle-filter-key'] = name ? ENV_NAME : ENV_HEAD; |
@@ -7,3 +7,54 @@ const http = require('http'); | ||
const noop = (_) => _; | ||
const AUTH_URL = 'x-whistle-.script-auth-url'; | ||
const SNI_URL = 'x-whistle-.script-sni-url'; | ||
const REQ_RULES_URL = 'x-whistle-.script-req-rules-url'; | ||
const RES_RULES_URL = 'x-whistle-.script-res-rules-url'; | ||
const STATS_URL = 'x-whistle-.script-stats-url'; | ||
const DATA_URL = 'x-whistle-.script-data-url'; | ||
const POLICY = 'x-whistle-.script-policy'; | ||
const URL_RE = /^http:\/\/[\w.-]/; | ||
const addPlugins = (headers, plugins) => { | ||
if (!plugins) { | ||
return; | ||
} | ||
const { | ||
sniUrl, | ||
authUrl, | ||
resRulesUrl, | ||
statsUrl, | ||
dataUrl, | ||
} = plugins; | ||
const rulesUrl = plugins.rulesUrl || plugins.reqRulesUrl; | ||
let hasSni; | ||
let hasRemote; | ||
if (URL_RE.test(sniUrl)) { | ||
hasSni = true; | ||
headers[SNI_URL] = encodeURIComponent(sniUrl); | ||
} | ||
if (URL_RE.test(authUrl)) { | ||
hasRemote = true; | ||
headers[AUTH_URL] = encodeURIComponent(authUrl); | ||
} | ||
if (URL_RE.test(rulesUrl)) { | ||
hasRemote = true; | ||
headers[REQ_RULES_URL] = encodeURIComponent(rulesUrl); | ||
} | ||
if (URL_RE.test(resRulesUrl)) { | ||
hasRemote = true; | ||
headers[RES_RULES_URL] = encodeURIComponent(resRulesUrl); | ||
} | ||
if (URL_RE.test(dataUrl)) { | ||
hasRemote = true; | ||
headers[DATA_URL] = encodeURIComponent(resRulesUrl); | ||
} | ||
if (URL_RE.test(statsUrl)) { | ||
hasRemote = true; | ||
headers[STATS_URL] = encodeURIComponent(statsUrl); | ||
} | ||
if (hasSni || hasRemote) { | ||
headers[POLICY] = `${hasSni ? 'sni' : ''}${hasRemote ? 'remote' : ''}`; | ||
} | ||
}; | ||
const getBody = (url) => { | ||
@@ -122,1 +173,3 @@ return new Promise((resolve, reject) => { | ||
}; | ||
exports.addPlugins = addPlugins; |
{ | ||
"name": "@nohost/router", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Nohost cluster router", | ||
@@ -35,5 +35,5 @@ "main": "lib/", | ||
"dependencies": { | ||
"@nohost/connect": "^1.3.6", | ||
"@nohost/connect": "^1.4.0", | ||
"hparser": "^0.3.0" | ||
} | ||
} |
18585
487
Updated@nohost/connect@^1.4.0