Skip to content

Commit

Permalink
refactor: native fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
vv314 committed Oct 19, 2024
1 parent 5effac9 commit 9d441d7
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 116 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"dependencies": {
"@wasmer/wasi": "^1.2.2",
"https-proxy-agent": "^7.0.5",
"node-fetch": "^3.3.2",
"p-limit": "^6.1.0",
"semver": "^7.6.3",
"timeout-signal": "^2.0.0",
Expand Down
71 changes: 0 additions & 71 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/coupons/gundam.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from '../fetch.js'
import request from '../request.js'
import { dateFormat, removePhoneRestriction } from '../util/index.js'
import { getTemplateData, matchMoudleData } from '../template.js'
import { ECODE } from './const.js'
Expand Down Expand Up @@ -26,7 +26,7 @@ function resolveRedMod(text, renderList) {
}

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

if (!data) {
Expand Down Expand Up @@ -82,7 +82,7 @@ async function grabCoupon(cookie, gundamId, guard) {
const actUrl = getActUrl(gundamId)
const tmplData = await getTemplateData(cookie, gundamId, guard)
const payload = await getPayload(tmplData, guard)
const res = await fetch.post(
const res = await request.post(
'https://mediacps.meituan.com/gundam/gundamGrabV4',
payload,
{
Expand Down
12 changes: 6 additions & 6 deletions src/coupons/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from '../fetch.js'
import request from '../request.js'
import ShadowGuard from '../shadow/index.js'
import { createMTCookie, getUserInfo } from '../user.js'
import { mainActConf, gundamActConfs, wxfwhActConfs, ECODE } from './const.js'
Expand Down Expand Up @@ -44,12 +44,12 @@ async function runTask(cookie, guard) {
code = ECODE.AUTH
msg = '登录过期'
break
case fetch.ECODE.FETCH:
case request.ECODE.FETCH:
code = ECODE.API
msg = '接口异常'
break
case fetch.ECODE.TIMEOUT:
case fetch.ECODE.NETWOEK:
case request.ECODE.TIMEOUT:
case request.ECODE.NETWOEK:
code = ECODE.NETWOEK
msg = '网络异常'
break
Expand Down Expand Up @@ -79,7 +79,7 @@ async function grabCoupons(token, { maxRetry = 0, proxy }) {

// 优先设置代理
if (proxy) {
fetch.setProxyAgent(proxy)
request.setProxyAgent(proxy)
}

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

async function main(retryTimes = 0) {
const result = await runTask(cookieJar, guard)
const needRetry = [fetch.ECODE.NETWOEK, fetch.ECODE.API].includes(
const needRetry = [request.ECODE.NETWOEK, request.ECODE.API].includes(
result.code
)

Expand Down
9 changes: 4 additions & 5 deletions src/coupons/lottery.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fetch from '../fetch.js'
import request from '../request.js'
import { getTemplateData } from '../template.js'
import { ECODE } from './const.js'

const fetchStatus = {
CAN_FETCH: 0,
Expand Down Expand Up @@ -41,7 +40,7 @@ function resolveMetadata(renderList, jsText) {
}

async function getTicketConfig(appJs, renderList) {
const jsText = await fetch(appJs).then((res) => res.text())
const jsText = await request(appJs).then((res) => res.text())
const data = resolveMetadata(renderList, jsText)
const ticketConfig = data.ticketConfig.makeOptions1.ticketInfo1

Expand Down Expand Up @@ -87,7 +86,7 @@ function formatCoupons(coupons, info) {
}

async function getCouponList(cookie, couponIds) {
const res = await fetch.get(
const res = await request.get(
`https://promotion.waimai.meituan.com/lottery/couponcomponent/info/v2`,
{
cookie,
Expand Down Expand Up @@ -131,7 +130,7 @@ async function grabCoupon(cookie, gundamId, guard) {
},
guard
)
const res = await fetch.post(
const res = await request.post(
`https://promotion.waimai.meituan.com/lottery/couponcomponent/fetchcomponentcoupon/v2`,
payload.body,
{
Expand Down
8 changes: 4 additions & 4 deletions src/coupons/wxfwh.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from '../fetch.js'
import request from '../request.js'
import { dateFormat, groupBy } from '../util/index.js'
import { getTemplateData, matchMoudleData } from '../template.js'
import { ECODE } from './const.js'
Expand Down Expand Up @@ -30,7 +30,7 @@ function formatCoupons(coupons, actName) {
}

async function getCouponList(cookie, viewId) {
const res = await fetch.get(
const res = await request.get(
'https://promotion.waimai.meituan.com/playcenter/generalcoupon/info',
{
params: {
Expand Down Expand Up @@ -66,7 +66,7 @@ async function getPayload(
{ gundamId, gdId, pageId, renderList, appJs },
guard
) {
const jsText = await fetch(appJs).then((res) => res.text())
const jsText = await request(appJs).then((res) => res.text())
let data = null

try {
Expand Down Expand Up @@ -119,7 +119,7 @@ async function grabCoupon(cookie, gundamId, guard) {
return []
}

const res = await fetch.post(
const res = await request.post(
'https://promotion.waimai.meituan.com/playcenter/generalcoupon/fetch',
payload,
{
Expand Down
6 changes: 3 additions & 3 deletions src/notifier/util.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import fetch from 'node-fetch'
import timeoutSignal from 'timeout-signal'

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

return fetch(`${url}?${params.toString()}`, {
timeout: 10000
signal: timeoutSignal(10000)
}).then((res) => res.json())
}

Expand All @@ -27,7 +27,7 @@ function doPost(url, data, type = 'json') {
headers: {
'Content-Type': cType
},
timeout: 10000
signal: timeoutSignal(10000)
}).then((res) => res.json())
}

Expand Down
23 changes: 11 additions & 12 deletions src/fetch.js → src/request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import nodeFetch from 'node-fetch'
import tough from 'tough-cookie'
import timeoutSignal from 'timeout-signal'
import HttpsProxyAgent from 'https-proxy-agent'
Expand All @@ -13,7 +12,7 @@ const ECODE = {
TIMEOUT: 'TIMEOUT'
}

async function fetch(url, opts = {}) {
async function request(url, opts = {}) {
const cookieJar = opts.cookie
const existCookie = cookieJar?.getCookieStringSync?.(url)
const optCookie = opts.headers?.cookie || ''
Expand Down Expand Up @@ -66,7 +65,7 @@ async function fetch(url, opts = {}) {
}

try {
res = await nodeFetch(urlObj, opts)
res = await fetch(urlObj, opts)
} catch (e) {
if (opts.signal?.aborted) {
throw { code: ECODE.TIMEOUT, req: urlObj, msg: e }
Expand All @@ -75,7 +74,7 @@ async function fetch(url, opts = {}) {
throw { code: ECODE.FETCH, req: urlObj, msg: res.statusText }
}

const setCookies = res.headers.raw()['set-cookie']
const setCookies = res.headers['set-cookie']

if (setCookies) {
setCookies.map((cookie) =>
Expand All @@ -87,7 +86,7 @@ async function fetch(url, opts = {}) {
}

async function doGet(url, opts = {}) {
const res = await fetch(url, {
const res = await request(url, {
...opts,
timeout: opts.timeout ?? 10000
})
Expand All @@ -111,7 +110,7 @@ async function doPost(url, data, opts = {}) {
body = data ? JSON.stringify(data) : ''
}

const res = await fetch(url, {
const res = await request(url, {
...opts,
method: 'POST',
body: body,
Expand All @@ -126,11 +125,11 @@ async function doPost(url, data, opts = {}) {
throw { code: ECODE.FETCH, url: url, msg: res.statusText }
}

fetch.ECODE = ECODE
fetch.get = doGet
fetch.post = doPost
fetch.setProxyAgent = (url) => {
fetch._proxyAgent = new HttpsProxyAgent(url)
request.ECODE = ECODE
request.get = doGet
request.post = doPost
request.setProxyAgent = (url) => {
request._proxyAgent = new HttpsProxyAgent(url)
}

export function createCookieJar(id) {
Expand All @@ -141,4 +140,4 @@ export function createCookieJar(id) {
return cookieJar
}

export default fetch
export default request
4 changes: 2 additions & 2 deletions src/shadow/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from '../fetch.js'
import request from '../request.js'
import {
formatUrl,
genMetaData,
Expand Down Expand Up @@ -40,7 +40,7 @@ class ShadowGuard {
}

async getWebDfpId(fingerprint) {
const res = await fetch.post(
const res = await request.post(
'https://appsec-mobile.meituan.com/v1/webdfpid',
{
data: fingerprint
Expand Down
Loading

0 comments on commit 9d441d7

Please sign in to comment.