@chenshunyu/proxy 1.0.0-beta.31 → 1.0.0-beta.41

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.
Files changed (3) hide show
  1. package/.npmigore +2 -0
  2. package/index.js +27 -8
  3. package/package.json +2 -2
package/.npmigore CHANGED
@@ -1 +1,3 @@
1
1
  *.env.*
2
+ node_modules
3
+ package-lock.json
package/index.js CHANGED
@@ -48,12 +48,19 @@ function read() {
48
48
  }
49
49
 
50
50
  function create(allHeaders) {
51
- let {
52
- protocol,
53
- host,
54
- } = new URL(allHeaders.Referer)
51
+ let target = null
52
+ if(allHeaders.Referer) {
53
+ let {
54
+ protocol,
55
+ host,
56
+ } = new URL(allHeaders.Referer)
57
+ target = `${protocol}//${host}`
58
+ } else if(allHeaders.Host) {
59
+ target = `https://${allHeaders.Host}`
60
+ } else {
61
+ throw new Error('Ivalid headers file: NOTARGET')
62
+ }
55
63
 
56
- let target = `${protocol}//${host}`
57
64
  console.table({
58
65
  target,
59
66
  Referer: allHeaders.Referer,
@@ -68,7 +75,7 @@ function create(allHeaders) {
68
75
  target,
69
76
  secure: false,
70
77
  changeOrigin: true,
71
- cookieDomainRewrite: host,
78
+ cookieDomainRewrite: allHeaders.Host,
72
79
  headers: {
73
80
  Referer: allHeaders.Referer,
74
81
  Host: allHeaders.Host,
@@ -103,11 +110,23 @@ function start(server) {
103
110
  function watch() {
104
111
  console.log('Watching...')
105
112
  watching = true
106
- fs.watch(filePath, e => {
113
+ fs.watch(filePath, debounce(e => {
107
114
  if (e === 'change') {
108
115
  console.log('File changes, reload...');
109
116
  close?.()
110
117
  read();
111
118
  }
112
- });
119
+ }, 200));
120
+ }
121
+
122
+ function debounce(func, wait) {
123
+ let timeout;
124
+ return function executedFunction(...args) {
125
+ const later = () => {
126
+ clearTimeout(timeout);
127
+ func(...args);
128
+ };
129
+ clearTimeout(timeout);
130
+ timeout = setTimeout(later, wait);
131
+ };
113
132
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chenshunyu/proxy",
3
- "version": "1.0.0-beta.31",
3
+ "version": "1.0.0-beta.41",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -15,6 +15,6 @@
15
15
  "author": "chenshunyu",
16
16
  "license": "ISC",
17
17
  "dependencies": {
18
- "http-proxy": "^1.18.1"
18
+ "http-proxy": "^1"
19
19
  }
20
20
  }