123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- const fs = require('fs')
- const url = require('url')
- const path = require('path')
- const chalk = require('chalk')
- const address = require('address')
- const defaultConfig = {
- logLevel: 'silent',
- secure: false,
- changeOrigin: true,
- ws: true,
- xfwd: true
- }
- module.exports = function prepareProxy (proxy, appPublicFolder) {
-
-
-
- if (!proxy) {
- return undefined
- }
- if (Array.isArray(proxy) || (typeof proxy !== 'object' && typeof proxy !== 'string')) {
- console.log(
- chalk.red(
- 'When specified, "proxy" in package.json must be a string or an object.'
- )
- )
- console.log(
- chalk.red('Instead, the type of "proxy" was "' + typeof proxy + '".')
- )
- console.log(
- chalk.red(
- 'Either remove "proxy" from package.json, or make it an object.'
- )
- )
- process.exit(1)
- }
-
- function mayProxy (pathname) {
- const maybePublicPath = path.resolve(appPublicFolder, pathname.slice(1))
- return !fs.existsSync(maybePublicPath)
- }
- function createProxyEntry (target, usersOnProxyReq, context) {
-
-
-
- if (typeof target === 'string' && process.platform === 'win32') {
- target = resolveLoopback(target)
- }
- return {
- target,
- context (pathname, req) {
-
- if (!mayProxy(pathname)) {
- return false
- }
- if (context) {
-
- return pathname.match(context)
- } else {
-
- if (req.method !== 'GET') {
- return true
- }
-
-
-
-
- return (
- req.headers.accept &&
- req.headers.accept.indexOf('text/html') === -1
- )
- }
- },
- onProxyReq (proxyReq, req, res) {
- if (usersOnProxyReq) {
- usersOnProxyReq(proxyReq, req, res)
- }
-
-
-
- if (!proxyReq.agent && proxyReq.getHeader('origin')) {
- proxyReq.setHeader('origin', target)
- }
- },
- onError: onProxyError(target)
- }
- }
-
- if (typeof proxy === 'string') {
- if (!/^http(s)?:\/\//.test(proxy)) {
- console.log(
- chalk.red(
- 'When "proxy" is specified in package.json it must start with either http:// or https://'
- )
- )
- process.exit(1)
- }
- return [
- Object.assign({}, defaultConfig, createProxyEntry(proxy))
- ]
- }
-
- return Object.keys(proxy).map(context => {
- const config = proxy[context]
- if (!config.hasOwnProperty('target')) {
- console.log(
- chalk.red(
- 'When `proxy` in package.json is an object, each `context` object must have a ' +
- '`target` property specified as a url string'
- )
- )
- process.exit(1)
- }
- const entry = createProxyEntry(config.target, config.onProxyReq, context)
- return Object.assign({}, defaultConfig, config, entry)
- })
- }
- function resolveLoopback (proxy) {
- const o = url.parse(proxy)
- o.host = undefined
- if (o.hostname !== 'localhost') {
- return proxy
- }
-
-
-
-
-
- try {
-
-
-
- if (!address.ip()) {
- o.hostname = '127.0.0.1'
- }
- } catch (_ignored) {
- o.hostname = '127.0.0.1'
- }
- return url.format(o)
- }
- function onProxyError (proxy) {
- return (err, req, res) => {
- const host = req.headers && req.headers.host
- console.log(
- chalk.red('Proxy error:') +
- ' Could not proxy request ' +
- chalk.cyan(req.url) +
- ' from ' +
- chalk.cyan(host) +
- ' to ' +
- chalk.cyan(proxy) +
- '.'
- )
- console.log(
- 'See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (' +
- chalk.cyan(err.code) +
- ').'
- )
- console.log()
-
-
- if (res.writeHead && !res.headersSent) {
- res.writeHead(500)
- }
- res.end(
- 'Proxy error: Could not proxy request ' +
- req.url +
- ' from ' +
- host +
- ' to ' +
- proxy +
- ' (' +
- err.code +
- ').'
- )
- }
- }
|