qqmusic-api-enhanced 1.0.0 → 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.
package/util/request.js CHANGED
@@ -64,7 +64,21 @@ export async function requestCgi(module, method, param = {}, options = {}) {
64
64
  const responseJson = (await res.json());
65
65
  // 解包 req_0
66
66
  const req0 = responseJson?.req_0;
67
- const resultBody = req0 ? (req0.data !== undefined ? req0.data : req0) : responseJson;
67
+ let resultBody = responseJson;
68
+ if (req0) {
69
+ if (req0.data !== undefined && req0.data !== null && typeof req0.data === "object") {
70
+ resultBody = Array.isArray(req0.data) ? req0.data : { ...req0.data };
71
+ if (!Array.isArray(resultBody) && resultBody.code === undefined && req0.code !== undefined) {
72
+ resultBody.code = req0.code;
73
+ }
74
+ }
75
+ else if (req0.data !== undefined) {
76
+ resultBody = req0.data;
77
+ }
78
+ else {
79
+ resultBody = req0;
80
+ }
81
+ }
68
82
  return {
69
83
  status: res.status,
70
84
  body: resultBody,
@@ -75,6 +89,28 @@ export async function requestCgi(module, method, param = {}, options = {}) {
75
89
  clearTimeout(timeoutTimer);
76
90
  }
77
91
  }
92
+ function isBinaryBuffer(buf, contentType) {
93
+ if (contentType.includes("image/") ||
94
+ contentType.includes("application/octet-stream") ||
95
+ contentType.includes("audio/") ||
96
+ contentType.includes("video/")) {
97
+ return true;
98
+ }
99
+ if (buf.length >= 4) {
100
+ if (buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47)
101
+ return true; // PNG
102
+ if (buf[0] === 0xff && buf[1] === 0xd8)
103
+ return true; // JPEG
104
+ if (buf[0] === 0x47 && buf[1] === 0x49 && buf[2] === 0x46)
105
+ return true; // GIF
106
+ if (buf.length >= 12 &&
107
+ buf.subarray(0, 4).toString("ascii") === "RIFF" &&
108
+ buf.subarray(8, 12).toString("ascii") === "WEBP") {
109
+ return true; // WEBP
110
+ }
111
+ }
112
+ return false;
113
+ }
78
114
  /**
79
115
  * 执行原始 HTTP/HTTPS 请求 (用于扫码、验证码或第三方 OAuth 接口).
80
116
  *
@@ -90,7 +126,19 @@ export async function requestRaw(url, init = {}) {
90
126
  ...init,
91
127
  signal: controller.signal,
92
128
  });
93
- const text = await res.text();
129
+ const cookie = res.headers.getSetCookie?.() || [];
130
+ const contentType = res.headers.get("content-type") || "";
131
+ const buffer = Buffer.from(await res.arrayBuffer());
132
+ if (isBinaryBuffer(buffer, contentType)) {
133
+ return {
134
+ status: res.status,
135
+ data: buffer,
136
+ text: "",
137
+ headers: res.headers,
138
+ cookie,
139
+ };
140
+ }
141
+ const text = buffer.toString("utf-8");
94
142
  let data = text;
95
143
  try {
96
144
  data = JSON.parse(text);
@@ -98,7 +146,6 @@ export async function requestRaw(url, init = {}) {
98
146
  catch {
99
147
  // 保持原始文本
100
148
  }
101
- const cookie = res.headers.getSetCookie?.() || [];
102
149
  return {
103
150
  status: res.status,
104
151
  data,
package/util/request.ts CHANGED
@@ -92,7 +92,19 @@ export async function requestCgi<T = any>(
92
92
 
93
93
  // 解包 req_0
94
94
  const req0 = responseJson?.req_0;
95
- const resultBody = req0 ? (req0.data !== undefined ? req0.data : req0) : responseJson;
95
+ let resultBody: any = responseJson;
96
+ if (req0) {
97
+ if (req0.data !== undefined && req0.data !== null && typeof req0.data === "object") {
98
+ resultBody = Array.isArray(req0.data) ? req0.data : { ...req0.data };
99
+ if (!Array.isArray(resultBody) && resultBody.code === undefined && req0.code !== undefined) {
100
+ resultBody.code = req0.code;
101
+ }
102
+ } else if (req0.data !== undefined) {
103
+ resultBody = req0.data;
104
+ } else {
105
+ resultBody = req0;
106
+ }
107
+ }
96
108
 
97
109
  return {
98
110
  status: res.status,
@@ -104,6 +116,30 @@ export async function requestCgi<T = any>(
104
116
  }
105
117
  }
106
118
 
119
+ function isBinaryBuffer(buf: Buffer, contentType: string): boolean {
120
+ if (
121
+ contentType.includes("image/") ||
122
+ contentType.includes("application/octet-stream") ||
123
+ contentType.includes("audio/") ||
124
+ contentType.includes("video/")
125
+ ) {
126
+ return true;
127
+ }
128
+ if (buf.length >= 4) {
129
+ if (buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47) return true; // PNG
130
+ if (buf[0] === 0xff && buf[1] === 0xd8) return true; // JPEG
131
+ if (buf[0] === 0x47 && buf[1] === 0x49 && buf[2] === 0x46) return true; // GIF
132
+ if (
133
+ buf.length >= 12 &&
134
+ buf.subarray(0, 4).toString("ascii") === "RIFF" &&
135
+ buf.subarray(8, 12).toString("ascii") === "WEBP"
136
+ ) {
137
+ return true; // WEBP
138
+ }
139
+ }
140
+ return false;
141
+ }
142
+
107
143
  /**
108
144
  * 执行原始 HTTP/HTTPS 请求 (用于扫码、验证码或第三方 OAuth 接口).
109
145
  *
@@ -124,7 +160,21 @@ export async function requestRaw(
124
160
  signal: controller.signal,
125
161
  });
126
162
 
127
- const text = await res.text();
163
+ const cookie = res.headers.getSetCookie?.() || [];
164
+ const contentType = res.headers.get("content-type") || "";
165
+ const buffer = Buffer.from(await res.arrayBuffer());
166
+
167
+ if (isBinaryBuffer(buffer, contentType)) {
168
+ return {
169
+ status: res.status,
170
+ data: buffer,
171
+ text: "",
172
+ headers: res.headers,
173
+ cookie,
174
+ };
175
+ }
176
+
177
+ const text = buffer.toString("utf-8");
128
178
  let data: any = text;
129
179
  try {
130
180
  data = JSON.parse(text);
@@ -132,7 +182,6 @@ export async function requestRaw(
132
182
  // 保持原始文本
133
183
  }
134
184
 
135
- const cookie = res.headers.getSetCookie?.() || [];
136
185
  return {
137
186
  status: res.status,
138
187
  data,