Skip to content

Commit 9d441d7

Browse files
committed
refactor: native fetch
1 parent 5effac9 commit 9d441d7

12 files changed

+42
-116
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"dependencies": {
2121
"@wasmer/wasi": "^1.2.2",
2222
"https-proxy-agent": "^7.0.5",
23-
"node-fetch": "^3.3.2",
2423
"p-limit": "^6.1.0",
2524
"semver": "^7.6.3",
2625
"timeout-signal": "^2.0.0",

pnpm-lock.yaml

-71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/coupons/gundam.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from '../fetch.js'
1+
import request from '../request.js'
22
import { dateFormat, removePhoneRestriction } from '../util/index.js'
33
import { getTemplateData, matchMoudleData } from '../template.js'
44
import { ECODE } from './const.js'
@@ -26,7 +26,7 @@ function resolveRedMod(text, renderList) {
2626
}
2727

2828
async function getPayload({ gundamId, gdId, appJs, renderList }, guard) {
29-
const jsText = await fetch(appJs).then((res) => res.text())
29+
const jsText = await request(appJs).then((res) => res.text())
3030
const data = resolveRedMod(jsText, renderList)
3131

3232
if (!data) {
@@ -82,7 +82,7 @@ async function grabCoupon(cookie, gundamId, guard) {
8282
const actUrl = getActUrl(gundamId)
8383
const tmplData = await getTemplateData(cookie, gundamId, guard)
8484
const payload = await getPayload(tmplData, guard)
85-
const res = await fetch.post(
85+
const res = await request.post(
8686
'https://mediacps.meituan.com/gundam/gundamGrabV4',
8787
payload,
8888
{

src/coupons/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from '../fetch.js'
1+
import request from '../request.js'
22
import ShadowGuard from '../shadow/index.js'
33
import { createMTCookie, getUserInfo } from '../user.js'
44
import { mainActConf, gundamActConfs, wxfwhActConfs, ECODE } from './const.js'
@@ -44,12 +44,12 @@ async function runTask(cookie, guard) {
4444
code = ECODE.AUTH
4545
msg = '登录过期'
4646
break
47-
case fetch.ECODE.FETCH:
47+
case request.ECODE.FETCH:
4848
code = ECODE.API
4949
msg = '接口异常'
5050
break
51-
case fetch.ECODE.TIMEOUT:
52-
case fetch.ECODE.NETWOEK:
51+
case request.ECODE.TIMEOUT:
52+
case request.ECODE.NETWOEK:
5353
code = ECODE.NETWOEK
5454
msg = '网络异常'
5555
break
@@ -79,7 +79,7 @@ async function grabCoupons(token, { maxRetry = 0, proxy }) {
7979

8080
// 优先设置代理
8181
if (proxy) {
82-
fetch.setProxyAgent(proxy)
82+
request.setProxyAgent(proxy)
8383
}
8484

8585
const cookieJar = createMTCookie(token)
@@ -89,7 +89,7 @@ async function grabCoupons(token, { maxRetry = 0, proxy }) {
8989

9090
async function main(retryTimes = 0) {
9191
const result = await runTask(cookieJar, guard)
92-
const needRetry = [fetch.ECODE.NETWOEK, fetch.ECODE.API].includes(
92+
const needRetry = [request.ECODE.NETWOEK, request.ECODE.API].includes(
9393
result.code
9494
)
9595

src/coupons/lottery.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import fetch from '../fetch.js'
1+
import request from '../request.js'
22
import { getTemplateData } from '../template.js'
3-
import { ECODE } from './const.js'
43

54
const fetchStatus = {
65
CAN_FETCH: 0,
@@ -41,7 +40,7 @@ function resolveMetadata(renderList, jsText) {
4140
}
4241

4342
async function getTicketConfig(appJs, renderList) {
44-
const jsText = await fetch(appJs).then((res) => res.text())
43+
const jsText = await request(appJs).then((res) => res.text())
4544
const data = resolveMetadata(renderList, jsText)
4645
const ticketConfig = data.ticketConfig.makeOptions1.ticketInfo1
4746

@@ -87,7 +86,7 @@ function formatCoupons(coupons, info) {
8786
}
8887

8988
async function getCouponList(cookie, couponIds) {
90-
const res = await fetch.get(
89+
const res = await request.get(
9190
`https://promotion.waimai.meituan.com/lottery/couponcomponent/info/v2`,
9291
{
9392
cookie,
@@ -131,7 +130,7 @@ async function grabCoupon(cookie, gundamId, guard) {
131130
},
132131
guard
133132
)
134-
const res = await fetch.post(
133+
const res = await request.post(
135134
`https://promotion.waimai.meituan.com/lottery/couponcomponent/fetchcomponentcoupon/v2`,
136135
payload.body,
137136
{

src/coupons/wxfwh.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from '../fetch.js'
1+
import request from '../request.js'
22
import { dateFormat, groupBy } from '../util/index.js'
33
import { getTemplateData, matchMoudleData } from '../template.js'
44
import { ECODE } from './const.js'
@@ -30,7 +30,7 @@ function formatCoupons(coupons, actName) {
3030
}
3131

3232
async function getCouponList(cookie, viewId) {
33-
const res = await fetch.get(
33+
const res = await request.get(
3434
'https://promotion.waimai.meituan.com/playcenter/generalcoupon/info',
3535
{
3636
params: {
@@ -66,7 +66,7 @@ async function getPayload(
6666
{ gundamId, gdId, pageId, renderList, appJs },
6767
guard
6868
) {
69-
const jsText = await fetch(appJs).then((res) => res.text())
69+
const jsText = await request(appJs).then((res) => res.text())
7070
let data = null
7171

7272
try {
@@ -119,7 +119,7 @@ async function grabCoupon(cookie, gundamId, guard) {
119119
return []
120120
}
121121

122-
const res = await fetch.post(
122+
const res = await request.post(
123123
'https://promotion.waimai.meituan.com/playcenter/generalcoupon/fetch',
124124
payload,
125125
{

src/notifier/util.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import fetch from 'node-fetch'
1+
import timeoutSignal from 'timeout-signal'
22

33
function doGet(url, data) {
44
const params = new URLSearchParams(data)
55

66
return fetch(`${url}?${params.toString()}`, {
7-
timeout: 10000
7+
signal: timeoutSignal(10000)
88
}).then((res) => res.json())
99
}
1010

@@ -27,7 +27,7 @@ function doPost(url, data, type = 'json') {
2727
headers: {
2828
'Content-Type': cType
2929
},
30-
timeout: 10000
30+
signal: timeoutSignal(10000)
3131
}).then((res) => res.json())
3232
}
3333

src/fetch.js src/request.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import nodeFetch from 'node-fetch'
21
import tough from 'tough-cookie'
32
import timeoutSignal from 'timeout-signal'
43
import HttpsProxyAgent from 'https-proxy-agent'
@@ -13,7 +12,7 @@ const ECODE = {
1312
TIMEOUT: 'TIMEOUT'
1413
}
1514

16-
async function fetch(url, opts = {}) {
15+
async function request(url, opts = {}) {
1716
const cookieJar = opts.cookie
1817
const existCookie = cookieJar?.getCookieStringSync?.(url)
1918
const optCookie = opts.headers?.cookie || ''
@@ -66,7 +65,7 @@ async function fetch(url, opts = {}) {
6665
}
6766

6867
try {
69-
res = await nodeFetch(urlObj, opts)
68+
res = await fetch(urlObj, opts)
7069
} catch (e) {
7170
if (opts.signal?.aborted) {
7271
throw { code: ECODE.TIMEOUT, req: urlObj, msg: e }
@@ -75,7 +74,7 @@ async function fetch(url, opts = {}) {
7574
throw { code: ECODE.FETCH, req: urlObj, msg: res.statusText }
7675
}
7776

78-
const setCookies = res.headers.raw()['set-cookie']
77+
const setCookies = res.headers['set-cookie']
7978

8079
if (setCookies) {
8180
setCookies.map((cookie) =>
@@ -87,7 +86,7 @@ async function fetch(url, opts = {}) {
8786
}
8887

8988
async function doGet(url, opts = {}) {
90-
const res = await fetch(url, {
89+
const res = await request(url, {
9190
...opts,
9291
timeout: opts.timeout ?? 10000
9392
})
@@ -111,7 +110,7 @@ async function doPost(url, data, opts = {}) {
111110
body = data ? JSON.stringify(data) : ''
112111
}
113112

114-
const res = await fetch(url, {
113+
const res = await request(url, {
115114
...opts,
116115
method: 'POST',
117116
body: body,
@@ -126,11 +125,11 @@ async function doPost(url, data, opts = {}) {
126125
throw { code: ECODE.FETCH, url: url, msg: res.statusText }
127126
}
128127

129-
fetch.ECODE = ECODE
130-
fetch.get = doGet
131-
fetch.post = doPost
132-
fetch.setProxyAgent = (url) => {
133-
fetch._proxyAgent = new HttpsProxyAgent(url)
128+
request.ECODE = ECODE
129+
request.get = doGet
130+
request.post = doPost
131+
request.setProxyAgent = (url) => {
132+
request._proxyAgent = new HttpsProxyAgent(url)
134133
}
135134

136135
export function createCookieJar(id) {
@@ -141,4 +140,4 @@ export function createCookieJar(id) {
141140
return cookieJar
142141
}
143142

144-
export default fetch
143+
export default request

src/shadow/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from '../fetch.js'
1+
import request from '../request.js'
22
import {
33
formatUrl,
44
genMetaData,
@@ -40,7 +40,7 @@ class ShadowGuard {
4040
}
4141

4242
async getWebDfpId(fingerprint) {
43-
const res = await fetch.post(
43+
const res = await request.post(
4444
'https://appsec-mobile.meituan.com/v1/webdfpid',
4545
{
4646
data: fingerprint

0 commit comments

Comments
 (0)