forked from oliyg/juejinxiaoce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
100 lines (90 loc) · 2.32 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const https = require('https')
const fs = require('fs')
const path = require('path')
const process = require('process')
const mkdir = dirname => {
const outputdir = path.resolve(process.env.PWD, dirname)
fs.existsSync(outputdir) || fs.mkdirSync(outputdir)
}
const rmfile = ext => {
const outputdir = path.resolve(__dirname, 'dist', ext)
const fileList = fs.readdirSync(outputdir).filter(item => path.extname(item) === '.' + ext)
fileList.forEach(file => {
fs.unlinkSync(path.join(outputdir, file))
})
}
const sleep = async(wait = 1000) => {
return new Promise(res => {
setTimeout(() => {
res(1)
}, wait)
})
}
function sendGet(hostname, path, headers) {
return new Promise((resolve, reject) => {
const req = https.request({
hostname, port: 443, path, method: 'GET'
}, res => {
let data = ''
res.on('data', chunk => { data = data + chunk.toString() })
res.on('error', reject)
res.on('end', () => {
resolve({
headers: res.headers,
data
})
})
})
setHeaders(req, headers)
req.on('error', reject)
req.end()
})
}
function sendPost(hostname, path, data, headers) {
return new Promise((resolve, reject) => {
const req = https.request({
hostname, path, port: 443, method: 'POST'
}, res => {
let data = ''
res.on('data', chunk => { data = data + chunk.toString() })
res.on('end', () => { resolve({
res,
data
}) })
res.on('error', reject)
})
setHeaders(req, headers)
req.on('error', reject)
req.write(data)
req.end()
})
}
function setHeaders(req, obj) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
req.setHeader(key, obj[key])
}
}
}
function getCookieObj(cookie_arr) {
cookie_arr = cookie_arr.map(item => item.split('; path=')[0])
cookie_arr = cookie_arr.map(item => item.split('='))
let result = {}
cookie_arr.forEach(item => {
result[item[0]] = item[1]
})
return result
}
function getCookieArr(cookie_obj) {
const result = []
for (const key in cookie_obj) {
if (cookie_obj.hasOwnProperty(key)) {
const value = cookie_obj[key]
result.push(key + '=' + value)
}
}
return result
}
module.exports = {
setHeaders, getCookieObj, getCookieArr, sendPost, sendGet, sleep, rmfile, mkdir
}