w-ipproxy 1.0.1 → 1.0.2

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 (52) hide show
  1. package/.eslintrc.js +18 -17
  2. package/.github/workflows/ci-test.yml +2 -2
  3. package/README.md +64 -2
  4. package/babel.config.js +4 -2
  5. package/dist/w-ip-proxy.umd.js +7 -0
  6. package/dist/w-ip-proxy.umd.js.map +1 -0
  7. package/docs/WIpProxy.html +642 -0
  8. package/docs/WIpProxy.mjs.html +465 -0
  9. package/docs/fonts/Montserrat/Montserrat-Bold.eot +0 -0
  10. package/docs/fonts/Montserrat/Montserrat-Bold.ttf +0 -0
  11. package/docs/fonts/Montserrat/Montserrat-Bold.woff +0 -0
  12. package/docs/fonts/Montserrat/Montserrat-Bold.woff2 +0 -0
  13. package/docs/fonts/Montserrat/Montserrat-Regular.eot +0 -0
  14. package/docs/fonts/Montserrat/Montserrat-Regular.ttf +0 -0
  15. package/docs/fonts/Montserrat/Montserrat-Regular.woff +0 -0
  16. package/docs/fonts/Montserrat/Montserrat-Regular.woff2 +0 -0
  17. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.eot +0 -0
  18. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.svg +978 -0
  19. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.ttf +0 -0
  20. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff +0 -0
  21. package/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff2 +0 -0
  22. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.eot +0 -0
  23. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.svg +1049 -0
  24. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.ttf +0 -0
  25. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff +0 -0
  26. package/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff2 +0 -0
  27. package/docs/index.html +84 -0
  28. package/docs/scripts/collapse.js +39 -0
  29. package/docs/scripts/commonNav.js +28 -0
  30. package/docs/scripts/linenumber.js +25 -0
  31. package/docs/scripts/nav.js +12 -0
  32. package/docs/scripts/polyfill.js +4 -0
  33. package/docs/scripts/prettify/Apache-License-2.0.txt +202 -0
  34. package/docs/scripts/prettify/lang-css.js +2 -0
  35. package/docs/scripts/prettify/prettify.js +28 -0
  36. package/docs/scripts/search.js +99 -0
  37. package/docs/styles/jsdoc.css +776 -0
  38. package/docs/styles/prettify.css +80 -0
  39. package/g-getByTimer.mjs +24 -0
  40. package/g-getOnce.mjs +11 -0
  41. package/g-srv.mjs +28 -0
  42. package/package.json +11 -9
  43. package/script.txt +17 -0
  44. package/src/WIpProxy.mjs +384 -14
  45. package/src/provideServer.mjs +65 -0
  46. package/toolg/cleanFolder.mjs +4 -0
  47. package/toolg/gDistRollup.mjs +29 -0
  48. package/toolg/modifyReadme.mjs +4 -0
  49. package/g.mjs +0 -12
  50. package/src/getHtml.mjs +0 -84
  51. package/src/getProxyListOrg.mjs +0 -66
  52. package/src/parseHtml.mjs +0 -19
@@ -0,0 +1,80 @@
1
+ .pln {
2
+ color: #ddd;
3
+ }
4
+
5
+ /* string content */
6
+ .str {
7
+ color: #61ce3c;
8
+ }
9
+
10
+ /* a keyword */
11
+ .kwd {
12
+ color: #fbde2d;
13
+ }
14
+
15
+ /* a comment */
16
+ .com {
17
+ color: #aeaeae;
18
+ }
19
+
20
+ /* a type name */
21
+ .typ {
22
+ color: #8da6ce;
23
+ }
24
+
25
+ /* a literal value */
26
+ .lit {
27
+ color: #fbde2d;
28
+ }
29
+
30
+ /* punctuation */
31
+ .pun {
32
+ color: #ddd;
33
+ }
34
+
35
+ /* lisp open bracket */
36
+ .opn {
37
+ color: #000000;
38
+ }
39
+
40
+ /* lisp close bracket */
41
+ .clo {
42
+ color: #000000;
43
+ }
44
+
45
+ /* a markup tag name */
46
+ .tag {
47
+ color: #8da6ce;
48
+ }
49
+
50
+ /* a markup attribute name */
51
+ .atn {
52
+ color: #fbde2d;
53
+ }
54
+
55
+ /* a markup attribute value */
56
+ .atv {
57
+ color: #ddd;
58
+ }
59
+
60
+ /* a declaration */
61
+ .dec {
62
+ color: #EF5050;
63
+ }
64
+
65
+ /* a variable name */
66
+ .var {
67
+ color: #c82829;
68
+ }
69
+
70
+ /* a function name */
71
+ .fun {
72
+ color: #4271ae;
73
+ }
74
+
75
+ /* Specify class=linenums on a pre to get line numbering */
76
+ ol.linenums {
77
+ margin-top: 0;
78
+ margin-bottom: 0;
79
+ padding-bottom: 2px;
80
+ }
@@ -0,0 +1,24 @@
1
+ import _ from 'lodash-es'
2
+ import wip from './src/WIpProxy.mjs'
3
+
4
+
5
+ let wo = wip({
6
+ // tar: `https://www.google.com`,
7
+ })
8
+ wo.init()
9
+ wo.on('getRawProxies', (prxsRaw) => {
10
+ console.log(`已抓取公開代理共 ${_.size(prxsRaw)} 個...`)
11
+ })
12
+ wo.on('add', (p, proxies) => {
13
+ // console.log('新加入代理', { host: p.host, port: p.port }, _.map(proxies, 'proxy'))
14
+ })
15
+ wo.on('delete', (p, proxies) => {
16
+ // console.log('刪除代理', { host: p.host, port: p.port }, _.map(proxies, 'proxy'))
17
+ })
18
+ wo.on('change', (proxies) => {
19
+ // console.log(`有效代理`, _.map(proxies, 'proxy'))
20
+ console.log(`已檢測有效代理共 ${_.size(proxies)} 個...`)
21
+ })
22
+
23
+
24
+ //node g-getByTimer.mjs
package/g-getOnce.mjs ADDED
@@ -0,0 +1,11 @@
1
+ import wip from './src/WIpProxy.mjs'
2
+
3
+
4
+ let wo = wip({
5
+ // tar: `https://www.google.com`,
6
+ })
7
+ let ps = await wo.getProxiesOnce()
8
+ console.log('ps', ps)
9
+
10
+
11
+ //node g-getOnce.mjs
package/g-srv.mjs ADDED
@@ -0,0 +1,28 @@
1
+ import _ from 'lodash-es'
2
+ import wip from './src/WIpProxy.mjs'
3
+
4
+
5
+ let wo = wip({
6
+ // tar: `https://www.google.com`,
7
+ withServer: true,
8
+ serverPort: 9000,
9
+ serverCorsOrigins: ['*'],
10
+ })
11
+ wo.on('getRawProxies', (prxsRaw) => {
12
+ console.log(`已抓取公開代理共 ${_.size(prxsRaw)} 個...`)
13
+ })
14
+ wo.on('add', (p, proxies) => {
15
+ // console.log('新加入代理', { host: p.host, port: p.port }, _.map(proxies, 'proxy'))
16
+ })
17
+ wo.on('delete', (p, proxies) => {
18
+ // console.log('刪除代理', { host: p.host, port: p.port }, _.map(proxies, 'proxy'))
19
+ })
20
+ wo.on('change', (proxies) => {
21
+ // console.log(`有效代理`, _.map(proxies, 'proxy'))
22
+ console.log(`已檢測有效代理共 ${_.size(proxies)} 個...`)
23
+ })
24
+
25
+ //browser view: http://localhost:9000/getProxies
26
+
27
+
28
+ //node g-srv.mjs
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "w-ipproxy",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "main": "dist/w-ipproxy.umd.js",
5
5
  "dependencies": {
6
- "axios": "^1.2.3",
7
- "cheerio": "^1.0.0-rc.12",
8
- "lodash": "^4.17.21",
9
- "puppeteer": "^19.5.2",
10
- "wsemi": "^1.6.87"
6
+ "@hapi/hapi": "^21.4.7",
7
+ "axios": "^1.13.6",
8
+ "cheerio": "^1.2.0",
9
+ "lodash-es": "^4.17.23",
10
+ "wsemi": "^1.8.43"
11
11
  },
12
12
  "devDependencies": {
13
- "w-package-tools": "^1.0.66"
13
+ "w-package-tools": "^1.1.3"
14
14
  },
15
15
  "scripts": {
16
- "test": "mocha --parallel --timeout 60000 --experimental-modules --es-module-specifier-resolution=node",
16
+ "test": "mocha --parallel --timeout 60000",
17
17
  "deploy": "gh-pages -d docs"
18
18
  },
19
19
  "repository": {
@@ -24,7 +24,9 @@
24
24
  "package",
25
25
  "tool",
26
26
  "ip",
27
- "proxy"
27
+ "proxy",
28
+ "hapi",
29
+ "nodejs"
28
30
  ],
29
31
  "author": "yuda-lyu(semisphere)",
30
32
  "license": "MIT"
package/script.txt ADDED
@@ -0,0 +1,17 @@
1
+ #node toolg/addVersion.mjs
2
+ node toolg/modifyReadme.mjs
3
+
4
+ node toolg/cleanFolder.mjs
5
+ ./node_modules/.bin/jsdoc -c .jsdoc
6
+
7
+ node toolg/gDistRollup.mjs
8
+
9
+ git add . -A
10
+ git commit -m 'modify: '
11
+ git push origin master:master
12
+
13
+ npm run deploy
14
+
15
+ #npm test
16
+
17
+ #npm publish
package/src/WIpProxy.mjs CHANGED
@@ -1,23 +1,393 @@
1
- import getProxyListOrg from './getProxyListOrg.mjs'
1
+ import axios from 'axios'
2
+ import https from 'https'
3
+ import get from 'lodash-es/get.js'
4
+ import size from 'lodash-es/size.js'
5
+ import each from 'lodash-es/each.js'
6
+ import map from 'lodash-es/map.js'
7
+ import trim from 'lodash-es/trim.js'
8
+ import values from 'lodash-es/values.js'
9
+ import evem from 'wsemi/src/evem.mjs'
10
+ import sep from 'wsemi/src/sep.mjs'
11
+ import isestr from 'wsemi/src/isestr.mjs'
12
+ import ispint from 'wsemi/src/ispint.mjs'
13
+ import isbol from 'wsemi/src/isbol.mjs'
14
+ import isearr from 'wsemi/src/isearr.mjs'
15
+ import iseobj from 'wsemi/src/iseobj.mjs'
16
+ import cint from 'wsemi/src/cint.mjs'
17
+ import waitFun from 'wsemi/src/waitFun.mjs'
18
+ import provideServer from './provideServer.mjs'
2
19
 
3
- //freeproxylists
4
- //http://www.freeproxylists.net/zh/?c=&pt=&pr=&a%5B%5D=0&a%5B%5D=1&a%5B%5D=2&u=0
5
20
 
6
- //aliveproxy
7
- //http://www.aliveproxy.com/proxy-list/proxies.aspx/Taiwan-tw
21
+ /**
22
+ * 抓取代理伺服器
23
+ *
24
+ * @class
25
+ * @param {Object} [opt={}] 輸入設定物件,預設{}
26
+ * @param {String} [opt.src='https://api.proxyscrape.com/v2/?request=displayproxies&protocol=http&timeout=5000&anonymity=all'] 輸入取得代理伺服器API字串,預設'https://api.proxyscrape.com/v2/?request=displayproxies&protocol=http&timeout=5000&anonymity=all'
27
+ * @param {String} [opt.tar='https://httpbin.org/ip'] 輸入檢測目標網址字串,預設'https://httpbin.org/ip'
28
+ * @param {Integer} [opt.timeGetProxies=30*1000] 輸入輪循抓取代理伺服器時間間隔整數,單位ms,預設30*1000
29
+ * @param {Integer} [opt.timeTestProxies=60*1000] 輸入輪循測試代理伺服器時間間隔整數,單位ms,預設60*1000
30
+ * @param {Boolean} [opt.withServer=false] 輸入是否創建供數據供給伺服器布林值,預設false
31
+ * @param {Integer} [opt.serverPort=8080] 輸入創建伺服器所用port整數,預設8080
32
+ * @param {String} [opt.serverApiName='getProxies'] 輸入供給數據API名稱字串,預設'getProxies'
33
+ * @param {Array} [opt.serverCorsOrigins=['*']] 輸入允許跨域網域陣列,若給予['*']代表允許全部,預設['*']
34
+ * @returns {Object} 回傳事件物件,提供函數getProxies,可監聽事件getRawProxies、add、delete、change
35
+ * @example
36
+ *
37
+ * import _ from 'lodash-es'
38
+ * import wip from './src/WIpProxy.mjs'
39
+ *
40
+ * let wo = wip({
41
+ * // tar: `https://www.google.com`,
42
+ * withServer: true,
43
+ * serverPort: 9000,
44
+ * serverCorsOrigins: ['*'],
45
+ * })
46
+ * wo.on('getRawProxies', (prxsRaw) => {
47
+ * console.log(`已抓取公開代理共 ${_.size(prxsRaw)} 個...`)
48
+ * })
49
+ * wo.on('add', (p, proxies) => {
50
+ * // console.log('新加入代理', { host: p.host, port: p.port }, _.map(proxies, 'proxy'))
51
+ * })
52
+ * wo.on('delete', (p, proxies) => {
53
+ * // console.log('刪除代理', { host: p.host, port: p.port }, _.map(proxies, 'proxy'))
54
+ * })
55
+ * wo.on('change', (proxies) => {
56
+ * // console.log(`有效代理`, _.map(proxies, 'proxy'))
57
+ * console.log(`已檢測有效代理共 ${_.size(proxies)} 個...`)
58
+ * })
59
+ *
60
+ * //browser view: http://localhost:9000/getProxies
61
+ *
62
+ */
63
+ function WIpProxy(opt = {}) {
8
64
 
9
- //cnproxy
10
- //http://www.cnproxy.com/proxy1.html
65
+ //src
66
+ let src = get(opt, 'src', '')
67
+ if (!isestr(src)) {
68
+ src = 'https://api.proxyscrape.com/v2/?request=displayproxies' +
69
+ `&protocol=http` +
70
+ // `&protocol=https` +
71
+ `&timeout=5000` +
72
+ // `&country=${join(['tw', 'cn', 'hk', 'jp', 'kr', 'sg', 'my', 'in', 'id', 'th', 'vn'])}` +
73
+ // `&ssl=yes` +
74
+ `&anonymity=all`
75
+ }
11
76
 
12
- async function WIpProxy() {
13
- let rs = []
14
- let r
77
+ //tar
78
+ let tar = get(opt, 'tar', '')
79
+ if (!isestr(tar)) {
80
+ tar = 'https://httpbin.org/ip' //'https://www.google.com'
81
+ }
15
82
 
16
- //getProxyListOrg
17
- r = await getProxyListOrg()
18
- rs = [...rs, ...r]
83
+ //timeGetProxies
84
+ let timeGetProxies = get(opt, 'timeGetProxies')
85
+ if (!ispint(timeGetProxies)) {
86
+ timeGetProxies = 30 * 1000 //30s
87
+ }
88
+ timeGetProxies = cint(timeGetProxies)
19
89
 
20
- return rs
90
+ //timeTestProxies
91
+ let timeTestProxies = get(opt, 'timeTestProxies')
92
+ if (!ispint(timeTestProxies)) {
93
+ timeTestProxies = 60 * 1000 //1min
94
+ }
95
+ timeTestProxies = cint(timeTestProxies)
96
+
97
+ //withServer
98
+ let withServer = get(opt, 'withServer')
99
+ if (!isbol(withServer)) {
100
+ withServer = false
101
+ }
102
+
103
+ //serverPort
104
+ let serverPort = get(opt, 'serverPort')
105
+ if (!ispint(serverPort)) {
106
+ serverPort = 8080
107
+ }
108
+ serverPort = cint(serverPort)
109
+
110
+ //serverApiName
111
+ let serverApiName = get(opt, 'serverApiName')
112
+ if (!isestr(serverApiName)) {
113
+ serverApiName = 'getProxies'
114
+ }
115
+
116
+ //serverCorsOrigins
117
+ let serverCorsOrigins = get(opt, 'serverCorsOrigins', [])
118
+ if (!isearr(serverCorsOrigins)) {
119
+ serverCorsOrigins = ['*']
120
+ }
121
+
122
+ //prxsRaw, kpPrx
123
+ let prxsRaw = []
124
+ let kpPrx = {}
125
+
126
+ async function _getProxiesCore(src) {
127
+ let ps = []
128
+
129
+ try {
130
+
131
+ //res
132
+ let res = await axios.get(src)
133
+
134
+ //c
135
+ let c = res.data
136
+ // console.log('c', c)
137
+
138
+ //sep
139
+ let ss = sep(c, '\n')
140
+ // console.log('ss', ss)
141
+
142
+ //ps
143
+ ps = map(ss, (v) => {
144
+ let [host, port] = v.split(':')
145
+ port = cint(port)
146
+ return {
147
+ proxy: v,
148
+ host,
149
+ port,
150
+ }
151
+ })
152
+ // console.log('ps', ps)
153
+
154
+ }
155
+ catch (err) {
156
+ console.log('_getProxiesCore catch', err.message)
157
+ }
158
+ // console.log('ps', ps)
159
+
160
+ return ps
161
+ }
162
+
163
+ let b_getProxies = false
164
+ async function _getProxies() {
165
+ // console.log('call getProxies...')
166
+
167
+ let core = async() => {
168
+
169
+ //get
170
+ prxsRaw = await _getProxiesCore(src)
171
+
172
+ //emit
173
+ ev.emit('getRawProxies', prxsRaw)
174
+
175
+ }
176
+
177
+ //core
178
+ if (b_getProxies) {
179
+ return
180
+ }
181
+ b_getProxies = true
182
+ await core()
183
+ .catch(() => {}) //吃掉錯誤
184
+ .finally(() => {
185
+ b_getProxies = false
186
+ })
187
+
188
+ }
189
+
190
+ async function _testProxyCore(host, port, url) {
191
+
192
+ //ret
193
+ let ret = (state, msg) => {
194
+ return {
195
+ state,
196
+ msg,
197
+ p: {
198
+ proxy: `${host}:${port}`,
199
+ host,
200
+ port,
201
+ },
202
+ }
203
+ }
204
+
205
+ //r
206
+ let r = {}
207
+ try {
208
+
209
+ //agent
210
+ let agent = new https.Agent({ rejectUnauthorized: false }) //避免證書錯誤中止
211
+
212
+ //get
213
+ let res = await axios.get(url, {
214
+ proxy: {
215
+ host,
216
+ port,
217
+ protocol: 'http',
218
+ },
219
+ httpsAgent: agent,
220
+ timeout: 5000,
221
+ headers: {
222
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/122.0.0.0 Safari/537.36',
223
+ 'Accept': 'application/json',
224
+ },
225
+ })
226
+
227
+ if (res.status === 200) {
228
+ r = ret('success', res.data)
229
+ }
230
+ else {
231
+ r = ret('error', `status[${res.status}] error`)
232
+ }
233
+
234
+ }
235
+ catch (err) {
236
+ r = ret('error', trim(err.message))
237
+ // if (r.msg.indexOf(`ssl3_get_record`) >= 0) {
238
+ // r.msg = `ssl3_get_record error`
239
+ // }
240
+ }
241
+ // console.log(r)
242
+
243
+ return r
244
+ }
245
+
246
+ async function _testProxiesCore(prxs) {
247
+
248
+ //pms
249
+ let pms = map(prxs, (p) => {
250
+ let pm = _testProxyCore(p.host, p.port, tar)
251
+ return pm
252
+ })
253
+
254
+ //rrs, allSettled
255
+ let rrs = await Promise.allSettled(pms)
256
+ // console.log('rrs', rrs)
257
+
258
+ //psValid, psInvalid
259
+ let psValid = []
260
+ let psInvalid = []
261
+ each(rrs, (v) => {
262
+
263
+ //be
264
+ let p = get(v, 'value.p', null)
265
+ let be = iseobj(p) && isestr(get(p, 'proxy'))
266
+
267
+ //check
268
+ if (!be) {
269
+ return true //跳出換下一個
270
+ }
271
+
272
+ //b
273
+ let status = get(v, 'status', '')
274
+ let b1 = status === 'fulfilled'
275
+ let state = get(v, 'value.state', '')
276
+ let b2 = state === 'success'
277
+ let b = b1 && b2
278
+
279
+ //push
280
+ if (b) {
281
+ psValid.push(p)
282
+ }
283
+ else {
284
+ psInvalid.push(p)
285
+ }
286
+
287
+ })
288
+
289
+ return {
290
+ psValid,
291
+ psInvalid,
292
+ }
293
+ }
294
+
295
+ let b_testProxies = false
296
+ async function _testProxies() {
297
+ // console.log('call testProxies...')
298
+
299
+ let core = async() => {
300
+
301
+ //get
302
+ let r = await _testProxiesCore(prxsRaw)
303
+
304
+ //emit add
305
+ each(r.psValid, (p) => {
306
+ kpPrx[p.proxy] = p
307
+ ev.emit('add', p, values(kpPrx))
308
+ })
309
+
310
+ //emit delete
311
+ each(r.psInvalid, (p) => {
312
+ delete kpPrx[p.proxy]
313
+ ev.emit('delete', p, values(kpPrx))
314
+ })
315
+
316
+ //emit change
317
+ ev.emit('change', values(kpPrx))
318
+
319
+ }
320
+
321
+ //core
322
+ if (b_testProxies) {
323
+ return
324
+ }
325
+ b_testProxies = true
326
+ await core()
327
+ .catch(() => {}) //吃掉錯誤
328
+ .finally(() => {
329
+ b_testProxies = false
330
+ })
331
+
332
+ }
333
+
334
+ function init() {
335
+
336
+ //取得代理清單
337
+ if (true) {
338
+ _getProxies()
339
+ setInterval(() => {
340
+ _getProxies()
341
+ }, timeGetProxies)
342
+ }
343
+
344
+ //過濾出有效代理清單, 延遲3s觸發, 給時間抓取
345
+ if (true) {
346
+ setTimeout(() => {
347
+ _testProxies()
348
+ setInterval(() => {
349
+ _testProxies()
350
+ }, timeTestProxies)
351
+ }, 3000)
352
+ }
353
+
354
+ }
355
+
356
+ async function getProxies() {
357
+ let vs
358
+ await waitFun(() => {
359
+ vs = values(kpPrx)
360
+ return size(vs) > 0
361
+ })
362
+ return vs
363
+ }
364
+
365
+ //ev
366
+ let ev = evem()
367
+
368
+ //withServer
369
+ if (withServer) {
370
+ provideServer(getProxies, {
371
+ port: serverPort,
372
+ apiName: serverApiName,
373
+ corsOrigins: serverCorsOrigins,
374
+ })
375
+ }
376
+
377
+ async function getProxiesOnce() {
378
+ await _getProxies()
379
+ await _testProxies()
380
+ let ps = await getProxies()
381
+ return ps
382
+ }
383
+
384
+ //save
385
+ ev.init = init
386
+ ev.getProxies = getProxies
387
+ ev.getProxiesOnce = getProxiesOnce
388
+
389
+ return ev
21
390
  }
22
391
 
392
+
23
393
  export default WIpProxy
@@ -0,0 +1,65 @@
1
+ import Hapi from '@hapi/hapi'
2
+ import get from 'lodash-es/get.js'
3
+ import ispint from 'wsemi/src/ispint.mjs'
4
+ import isearr from 'wsemi/src/isearr.mjs'
5
+ import isestr from 'wsemi/src/isestr.mjs'
6
+ import cint from 'wsemi/src/cint.mjs'
7
+
8
+
9
+ function provideServer(fun, opt = {}) {
10
+
11
+ //port
12
+ let port = get(opt, 'port')
13
+ if (!ispint(port)) {
14
+ port = 8080
15
+ }
16
+ port = cint(port)
17
+
18
+ //corsOrigins
19
+ let corsOrigins = get(opt, 'corsOrigins', [])
20
+ if (!isearr(corsOrigins)) {
21
+ corsOrigins = ['*']
22
+ }
23
+
24
+ //apiName
25
+ let apiName = get(opt, 'apiName')
26
+ if (!isestr(apiName)) {
27
+ apiName = 'getProxies'
28
+ }
29
+
30
+ //startServer
31
+ let startServer = async () => {
32
+ let server = Hapi.server({
33
+ // host: 'localhost',
34
+ port,
35
+ routes: {
36
+ cors: {
37
+ origin: corsOrigins, //Access-Control-Allow-Origin
38
+ credentials: false, //Access-Control-Allow-Credentials
39
+ },
40
+ },
41
+ })
42
+
43
+ //route
44
+ server.route({
45
+ method: 'GET',
46
+ path: `/${apiName}`,
47
+ handler: async () => {
48
+ let proxies = await fun()
49
+ return proxies
50
+ },
51
+ })
52
+
53
+ //start
54
+ await server.start()
55
+
56
+ console.log(`Server running at: ${server.info.uri}`)
57
+ }
58
+
59
+ //startServer
60
+ startServer()
61
+
62
+ }
63
+
64
+
65
+ export default provideServer
@@ -0,0 +1,4 @@
1
+ import cleanFolder from 'w-package-tools/src/cleanFolder.mjs'
2
+
3
+
4
+ cleanFolder('docs')