-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
232 additions
and
79 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
app/www/assets/polyfills-legacy.5229e370.js → app/www/assets/polyfills-legacy.a3a493e7.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,145 @@ | ||
const https = require('https') | ||
const http = require('http') | ||
const zlib = require('zlib') | ||
const { URL } = require('url') | ||
const qs = require('qs') | ||
|
||
function request(options: any) { | ||
const url = new URL(options.url) | ||
return new Promise((resolve, reject) => { | ||
const req = https.request( | ||
{ | ||
hostname: url.hostname, | ||
port: url.port, | ||
path: `${url.pathname}${url.search}`, | ||
method: 'GET', | ||
...options | ||
}, | ||
(res: any) => { | ||
res.on('data', (buf: any) => { | ||
const data = Buffer.from(buf).toString() | ||
let result = null | ||
try { | ||
result = JSON.parse(data) | ||
} catch { | ||
// | ||
} | ||
if (result.code === 0) { | ||
resolve(result) | ||
} else { | ||
reject(result) | ||
} | ||
}) | ||
class Request { | ||
cookies: string[] = [] | ||
constructor() { | ||
// if (cookie) { | ||
// this.setCookie(cookie) | ||
// } | ||
} | ||
/** | ||
* 获取请求头 | ||
* | ||
* @param host - 请求的域名 | ||
* @param propsData - 请求数据 | ||
* @returns headers - 请求头 | ||
*/ | ||
public getHeaders(host: string, postData?: string) { | ||
const headers: { [prop: string]: string | number | boolean } = { | ||
Host: host, | ||
rejectUnauthorized: false, | ||
Pragma: 'no-cache', | ||
Connection: 'keep-alive', | ||
'Cache-Control': 'no-cache', | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,es;q=0.2', | ||
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', | ||
'User-Agent': | ||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36' | ||
} | ||
if (this.cookies.length) { | ||
headers.Cookie = this.cookies.join('; ') | ||
} | ||
if (postData) { | ||
headers['Content-Length'] = Buffer.byteLength(postData) | ||
} | ||
return headers | ||
} | ||
/** | ||
* 设置cookie | ||
* | ||
* @param cookie - cookie | ||
* @returns this - 当前类 | ||
*/ | ||
public setCookie(cookie: string) { | ||
const cookies = cookie.split(';') | ||
for (let item of cookies) { | ||
item = item.replace(/^\s/, '') | ||
this.cookies.push(item) | ||
} | ||
return this | ||
} | ||
/** | ||
* 发起请求 | ||
* | ||
* @param method - 请求方法:'GET' | 'POST' | 'DELETE' | 'OPTIONS' | ||
* @param url - 请求链接 | ||
* @param params - 请求参数 | ||
* @returns Promise - 请求结果 | ||
*/ | ||
public request( | ||
method: 'GET' | 'POST' | 'DELETE' | 'OPTIONS', | ||
url: string, | ||
params?: object | ||
) { | ||
const postData = qs.stringify(params || {}, { | ||
arrayFormat: 'indices', | ||
allowDots: true | ||
}) | ||
const urlObj = new URL(url) | ||
const options = { | ||
hostname: urlObj.hostname, | ||
port: urlObj.port, | ||
path: urlObj.pathname + urlObj.search, | ||
method, | ||
headers: this.getHeaders(urlObj.host, postData) | ||
} | ||
return new Promise((resolve, reject) => { | ||
const req = (urlObj.protocol == 'http:' ? http : https).request( | ||
options, | ||
(res: any) => { | ||
const chunks: any[] = [] | ||
res.on('data', (buf: any) => { | ||
// const data = JSON.parse(Buffer.from(buf).toString()) | ||
chunks.push(buf) | ||
}) | ||
res.on('end', () => { | ||
const buffer = Buffer.concat(chunks) | ||
const encoding = res.headers['content-encoding'] | ||
if (encoding === 'gzip') { | ||
zlib.gunzip( | ||
buffer, | ||
function (err: Error, decoded: any) { | ||
resolve(decoded.toString()) | ||
} | ||
) | ||
} else if (encoding === 'deflate') { | ||
zlib.inflate( | ||
buffer, | ||
function (err: Error, decoded: any) { | ||
resolve(decoded.toString()) | ||
} | ||
) | ||
} else { | ||
resolve(JSON.parse(buffer.toString())) | ||
} | ||
}) | ||
} | ||
) | ||
req.on('error', (err: Error) => { | ||
reject(err) | ||
}) | ||
if (postData) { | ||
req.write(postData) | ||
} | ||
) | ||
req.on('error', reject) | ||
req.end() | ||
}) | ||
req.end() | ||
}) | ||
} | ||
/** | ||
* get方法 | ||
* | ||
* @param option - 参数 | ||
* @param option.url - 请求链接 | ||
* @returns Promise - 请求结果 | ||
*/ | ||
public async get({ url }: any) { | ||
return await this.request('GET', url) | ||
} | ||
/** | ||
* post方法 | ||
* | ||
* @param option - 参数 | ||
* @param option.url - 请求链接 | ||
* @param option.params - 请求参数 | ||
* @returns Promise - 请求结果 | ||
*/ | ||
public async post({ url, params }: any) { | ||
return await this.request('POST', url, params) | ||
} | ||
} | ||
|
||
module.exports = request | ||
module.exports = new Request() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,91 @@ | ||
'use strict'; | ||
|
||
var __defProp = Object.defineProperty; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
const https = require("https"); | ||
const http = require("http"); | ||
const zlib = require("zlib"); | ||
const { URL } = require("url"); | ||
function request(options) { | ||
const url = new URL(options.url); | ||
return new Promise((resolve, reject) => { | ||
const req = https.request(__spreadValues({ | ||
hostname: url.hostname, | ||
port: url.port, | ||
path: `${url.pathname}${url.search}`, | ||
method: "GET" | ||
}, options), (res) => { | ||
res.on("data", (buf) => { | ||
const data = Buffer.from(buf).toString(); | ||
let result = null; | ||
try { | ||
result = JSON.parse(data); | ||
} catch (e) { | ||
} | ||
if (result.code === 0) { | ||
resolve(result); | ||
} else { | ||
reject(result); | ||
} | ||
const qs = require("qs"); | ||
class Request { | ||
constructor() { | ||
this.cookies = []; | ||
} | ||
getHeaders(host, postData) { | ||
const headers = { | ||
Host: host, | ||
rejectUnauthorized: false, | ||
Pragma: "no-cache", | ||
Connection: "keep-alive", | ||
"Cache-Control": "no-cache", | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Accept-Language": "zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,es;q=0.2", | ||
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", | ||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36" | ||
}; | ||
if (this.cookies.length) { | ||
headers.Cookie = this.cookies.join("; "); | ||
} | ||
if (postData) { | ||
headers["Content-Length"] = Buffer.byteLength(postData); | ||
} | ||
return headers; | ||
} | ||
setCookie(cookie) { | ||
const cookies = cookie.split(";"); | ||
for (let item of cookies) { | ||
item = item.replace(/^\s/, ""); | ||
this.cookies.push(item); | ||
} | ||
return this; | ||
} | ||
request(method, url, params) { | ||
const postData = qs.stringify(params || {}, { | ||
arrayFormat: "indices", | ||
allowDots: true | ||
}); | ||
const urlObj = new URL(url); | ||
const options = { | ||
hostname: urlObj.hostname, | ||
port: urlObj.port, | ||
path: urlObj.pathname + urlObj.search, | ||
method, | ||
headers: this.getHeaders(urlObj.host, postData) | ||
}; | ||
return new Promise((resolve, reject) => { | ||
const req = (urlObj.protocol == "http:" ? http : https).request(options, (res) => { | ||
const chunks = []; | ||
res.on("data", (buf) => { | ||
chunks.push(buf); | ||
}); | ||
res.on("end", () => { | ||
const buffer = Buffer.concat(chunks); | ||
const encoding = res.headers["content-encoding"]; | ||
if (encoding === "gzip") { | ||
zlib.gunzip(buffer, function(err, decoded) { | ||
resolve(decoded.toString()); | ||
}); | ||
} else if (encoding === "deflate") { | ||
zlib.inflate(buffer, function(err, decoded) { | ||
resolve(decoded.toString()); | ||
}); | ||
} else { | ||
resolve(JSON.parse(buffer.toString())); | ||
} | ||
}); | ||
}); | ||
req.on("error", (err) => { | ||
reject(err); | ||
}); | ||
if (postData) { | ||
req.write(postData); | ||
} | ||
req.end(); | ||
}); | ||
req.on("error", reject); | ||
req.end(); | ||
}); | ||
} | ||
async get({ url }) { | ||
return await this.request("GET", url); | ||
} | ||
async post({ url, params }) { | ||
return await this.request("POST", url, params); | ||
} | ||
} | ||
module.exports = request; | ||
module.exports = new Request(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4bff905
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: