@wevu/web-apis 1.3.5 → 1.4.0
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/README.md +36 -8
- package/dist/abort.d.mts +2 -4
- package/dist/abort.mjs +2 -3
- package/dist/eventsource-DUCc1iHv.d.mts +82 -0
- package/dist/eventsource-ojngrwBI.mjs +340 -0
- package/dist/eventsource.d.mts +2 -0
- package/dist/eventsource.mjs +2 -0
- package/dist/fetch-CQoQx3mz.mjs +301 -0
- package/dist/fetch-CSESA-oq.d.mts +33 -0
- package/dist/fetch.d.mts +2 -35
- package/dist/fetch.mjs +1 -188
- package/dist/http-DCCqK1QL.mjs +505 -0
- package/dist/{http-CE8-CIHE.d.mts → http-Dd6YCSHK.d.mts} +6 -14
- package/dist/http.d.mts +1 -1
- package/dist/http.mjs +1 -1
- package/dist/index.d.mts +19 -54
- package/dist/index.mjs +35 -104
- package/dist/networkDefaults.d.mts +5 -11
- package/dist/networkDefaults.mjs +1 -2
- package/dist/{shared-85INNxXV.mjs → shared-CARmZrDA.mjs} +5 -9
- package/dist/{shared-Dc42USJC.d.mts → shared-DfU0oEwy.d.mts} +0 -2
- package/dist/shared.d.mts +1 -1
- package/dist/shared.mjs +1 -1
- package/dist/streams-6m8eQfZM.mjs +84 -0
- package/dist/streams.d.mts +31 -0
- package/dist/streams.mjs +1 -0
- package/dist/textCodec-muWByCAW.mjs +132 -0
- package/dist/{url-D0ANYIyk.d.mts → url-CEgyUgm2.d.mts} +0 -2
- package/dist/url-DjMmeEth.mjs +285 -0
- package/dist/url.d.mts +1 -1
- package/dist/url.mjs +1 -229
- package/dist/{web-uZ-hezcd.mjs → web-DDzJNeV3.mjs} +6 -8
- package/dist/{web-DNBBg6y9.d.mts → web-DjLufM4u.d.mts} +1 -7
- package/dist/web.d.mts +1 -1
- package/dist/web.mjs +1 -1
- package/dist/websocket.d.mts +3 -4
- package/dist/websocket.mjs +4 -5
- package/dist/xhr.d.mts +2 -3
- package/dist/xhr.mjs +4 -5
- package/package.json +20 -6
- package/dist/http-mcdF3N9S.mjs +0 -303
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wevu/web-apis",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.4.0",
|
|
5
5
|
"description": "Web API polyfills and global installers for mini-program runtimes",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -67,6 +67,14 @@
|
|
|
67
67
|
"./xhr": {
|
|
68
68
|
"types": "./dist/xhr.d.mts",
|
|
69
69
|
"import": "./dist/xhr.mjs"
|
|
70
|
+
},
|
|
71
|
+
"./streams": {
|
|
72
|
+
"types": "./dist/streams.d.mts",
|
|
73
|
+
"import": "./dist/streams.mjs"
|
|
74
|
+
},
|
|
75
|
+
"./eventsource": {
|
|
76
|
+
"types": "./dist/eventsource.d.mts",
|
|
77
|
+
"import": "./dist/eventsource.mjs"
|
|
70
78
|
}
|
|
71
79
|
},
|
|
72
80
|
"main": "./dist/index.mjs",
|
|
@@ -76,11 +84,13 @@
|
|
|
76
84
|
"dist"
|
|
77
85
|
],
|
|
78
86
|
"engines": {
|
|
79
|
-
"node": "^
|
|
87
|
+
"node": "^22.23.4 || ^24.22.1 || >=26.12.1"
|
|
80
88
|
},
|
|
81
89
|
"dependencies": {
|
|
82
|
-
"@weapp-core/
|
|
83
|
-
"@
|
|
90
|
+
"@weapp-core/api": "0.4.0",
|
|
91
|
+
"@weapp-core/constants": "0.5.0",
|
|
92
|
+
"@wevu/api": "0.4.0",
|
|
93
|
+
"web-streams-polyfill": "4.3.0"
|
|
84
94
|
},
|
|
85
95
|
"publishConfig": {
|
|
86
96
|
"access": "public",
|
|
@@ -93,6 +103,9 @@
|
|
|
93
103
|
"paths": {}
|
|
94
104
|
}
|
|
95
105
|
},
|
|
106
|
+
"devDependencies": {
|
|
107
|
+
"eventsource-parser": "3.1.1"
|
|
108
|
+
},
|
|
96
109
|
"scripts": {
|
|
97
110
|
"dev": "tsdown -w --sourcemap",
|
|
98
111
|
"build": "tsdown",
|
|
@@ -101,7 +114,8 @@
|
|
|
101
114
|
"test:dev": "vitest",
|
|
102
115
|
"pretest:types": "pnpm build",
|
|
103
116
|
"test:types": "pnpm run test:types:check",
|
|
104
|
-
"test:types:check": "tsd --files \"test-d/**/*.test-d.ts\"",
|
|
105
|
-
"typecheck": "tsc --noEmit"
|
|
117
|
+
"test:types:check": "tsd --files \"test-d/**/*.test-d.ts\" && pnpm run test:types:pure",
|
|
118
|
+
"typecheck": "tsc --noEmit",
|
|
119
|
+
"test:types:pure": "cd test-d-pure && tsd --files \"**/*.test-d.ts\""
|
|
106
120
|
}
|
|
107
121
|
}
|
package/dist/http-mcdF3N9S.mjs
DELETED
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
import { d as normalizeHeaderName, h as isUrlSearchParamsInstance, i as decodeText, l as isArrayBufferLike, m as isUrlInstance, n as cloneArrayBuffer, o as encodeText, r as cloneArrayBufferView, u as isBlobLike } from "./shared-85INNxXV.mjs";
|
|
2
|
-
import { r as BlobPolyfill, t as FormDataPolyfill } from "./web-uZ-hezcd.mjs";
|
|
3
|
-
//#region src/http/headers.ts
|
|
4
|
-
const SET_COOKIE_HEADER_NAME = "set-cookie";
|
|
5
|
-
function isIterableHeaders(input) {
|
|
6
|
-
return Boolean(input) && typeof input[Symbol.iterator] === "function";
|
|
7
|
-
}
|
|
8
|
-
function isHeaderObject(input) {
|
|
9
|
-
return typeof input === "object" && input !== null;
|
|
10
|
-
}
|
|
11
|
-
var HeadersPolyfill = class {
|
|
12
|
-
store = /* @__PURE__ */ new Map();
|
|
13
|
-
constructor(init) {
|
|
14
|
-
if (!init) return;
|
|
15
|
-
if (isIterableHeaders(init)) {
|
|
16
|
-
for (const [key, value] of init) this.append(key, value);
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
if (typeof init.forEach === "function") {
|
|
20
|
-
init.forEach((value, key) => {
|
|
21
|
-
this.set(key, value);
|
|
22
|
-
});
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
if (isHeaderObject(init)) for (const [key, value] of Object.entries(init)) this.append(key, Array.isArray(value) ? value.join(", ") : String(value));
|
|
26
|
-
}
|
|
27
|
-
append(key, value) {
|
|
28
|
-
const normalized = normalizeHeaderName(key);
|
|
29
|
-
if (!normalized) return;
|
|
30
|
-
const item = this.store.get(normalized);
|
|
31
|
-
if (item) {
|
|
32
|
-
item.values.push(String(value));
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
this.store.set(normalized, {
|
|
36
|
-
key,
|
|
37
|
-
values: [String(value)]
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
set(key, value) {
|
|
41
|
-
const normalized = normalizeHeaderName(key);
|
|
42
|
-
if (!normalized) return;
|
|
43
|
-
this.store.set(normalized, {
|
|
44
|
-
key,
|
|
45
|
-
values: [String(value)]
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
get(key) {
|
|
49
|
-
return this.store.get(normalizeHeaderName(key))?.values.join(", ") ?? null;
|
|
50
|
-
}
|
|
51
|
-
getSetCookie() {
|
|
52
|
-
return this.store.get(SET_COOKIE_HEADER_NAME)?.values.slice() ?? [];
|
|
53
|
-
}
|
|
54
|
-
has(key) {
|
|
55
|
-
return this.store.has(normalizeHeaderName(key));
|
|
56
|
-
}
|
|
57
|
-
delete(key) {
|
|
58
|
-
this.store.delete(normalizeHeaderName(key));
|
|
59
|
-
}
|
|
60
|
-
forEach(callback, thisArg) {
|
|
61
|
-
for (const { key, values } of this.store.values()) callback.call(thisArg, values.join(", "), key, this);
|
|
62
|
-
}
|
|
63
|
-
entries() {
|
|
64
|
-
return Array.from(this.store.values(), (item) => [item.key, item.values.join(", ")])[Symbol.iterator]();
|
|
65
|
-
}
|
|
66
|
-
keys() {
|
|
67
|
-
return Array.from(this.store.values(), (item) => item.key)[Symbol.iterator]();
|
|
68
|
-
}
|
|
69
|
-
values() {
|
|
70
|
-
return Array.from(this.store.values(), (item) => item.values.join(", "))[Symbol.iterator]();
|
|
71
|
-
}
|
|
72
|
-
[Symbol.iterator]() {
|
|
73
|
-
return this.entries();
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
function headersToObject(headers) {
|
|
77
|
-
const result = {};
|
|
78
|
-
headers.forEach((value, key) => {
|
|
79
|
-
result[key] = value;
|
|
80
|
-
});
|
|
81
|
-
return result;
|
|
82
|
-
}
|
|
83
|
-
//#endregion
|
|
84
|
-
//#region src/multipart.ts
|
|
85
|
-
const CRLF = "\r\n";
|
|
86
|
-
function createMultipartBoundary() {
|
|
87
|
-
return `----weapp-vite-formdata-${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`;
|
|
88
|
-
}
|
|
89
|
-
function escapeMultipartName(value) {
|
|
90
|
-
return String(value).replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\r/g, "%0D").replace(/\n/g, "%0A");
|
|
91
|
-
}
|
|
92
|
-
function concatArrayBuffers(buffers) {
|
|
93
|
-
const totalLength = buffers.reduce((sum, buffer) => sum + buffer.byteLength, 0);
|
|
94
|
-
const merged = new Uint8Array(totalLength);
|
|
95
|
-
let offset = 0;
|
|
96
|
-
for (const buffer of buffers) {
|
|
97
|
-
merged.set(new Uint8Array(buffer), offset);
|
|
98
|
-
offset += buffer.byteLength;
|
|
99
|
-
}
|
|
100
|
-
return merged.buffer;
|
|
101
|
-
}
|
|
102
|
-
function isBlobFormDataValue(value) {
|
|
103
|
-
return typeof value !== "string" && typeof value?.arrayBuffer === "function";
|
|
104
|
-
}
|
|
105
|
-
function getMultipartFilename(value) {
|
|
106
|
-
const name = value.name;
|
|
107
|
-
return typeof name === "string" && name.length > 0 ? name : "blob";
|
|
108
|
-
}
|
|
109
|
-
async function createMultipartPart(name, value) {
|
|
110
|
-
if (!isBlobFormDataValue(value)) return [
|
|
111
|
-
encodeText(`Content-Disposition: form-data; name="${escapeMultipartName(name)}"${CRLF}${CRLF}`),
|
|
112
|
-
encodeText(String(value)),
|
|
113
|
-
encodeText(CRLF)
|
|
114
|
-
];
|
|
115
|
-
const filename = getMultipartFilename(value);
|
|
116
|
-
const headers = [
|
|
117
|
-
`Content-Disposition: form-data; name="${escapeMultipartName(name)}"; filename="${escapeMultipartName(filename)}"`,
|
|
118
|
-
`Content-Type: ${value.type || "application/octet-stream"}`,
|
|
119
|
-
"",
|
|
120
|
-
""
|
|
121
|
-
].join(CRLF);
|
|
122
|
-
return [
|
|
123
|
-
encodeText(headers),
|
|
124
|
-
await value.arrayBuffer(),
|
|
125
|
-
encodeText(CRLF)
|
|
126
|
-
];
|
|
127
|
-
}
|
|
128
|
-
async function encodeMultipartFormData(formData) {
|
|
129
|
-
const boundary = createMultipartBoundary();
|
|
130
|
-
const buffers = [];
|
|
131
|
-
for (const [name, value] of formData) {
|
|
132
|
-
buffers.push(encodeText(`--${boundary}${CRLF}`));
|
|
133
|
-
buffers.push(...await createMultipartPart(name, value));
|
|
134
|
-
}
|
|
135
|
-
buffers.push(encodeText(`--${boundary}--${CRLF}`));
|
|
136
|
-
return {
|
|
137
|
-
body: concatArrayBuffers(buffers),
|
|
138
|
-
contentType: `multipart/form-data; boundary=${boundary}`
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
//#endregion
|
|
142
|
-
//#region src/http/body.ts
|
|
143
|
-
const bodies = /* @__PURE__ */ new WeakMap();
|
|
144
|
-
function isFormData(value) {
|
|
145
|
-
return value instanceof FormDataPolyfill || typeof FormData === "function" && value instanceof FormData;
|
|
146
|
-
}
|
|
147
|
-
function initializeBody(owner, input) {
|
|
148
|
-
let value;
|
|
149
|
-
let contentType = "";
|
|
150
|
-
if (input == null) value = null;
|
|
151
|
-
else if (isArrayBufferLike(input)) value = cloneArrayBuffer(input);
|
|
152
|
-
else if (ArrayBuffer.isView(input)) value = cloneArrayBufferView(input);
|
|
153
|
-
else if (isBlobLike(input)) {
|
|
154
|
-
value = input;
|
|
155
|
-
contentType = input.type ?? "";
|
|
156
|
-
} else if (isFormData(input)) value = input;
|
|
157
|
-
else if (isUrlSearchParamsInstance(input)) {
|
|
158
|
-
value = input.toString();
|
|
159
|
-
contentType = "application/x-www-form-urlencoded;charset=UTF-8";
|
|
160
|
-
} else {
|
|
161
|
-
value = String(input);
|
|
162
|
-
contentType = "text/plain;charset=UTF-8";
|
|
163
|
-
}
|
|
164
|
-
if (contentType && !owner.headers.has("content-type")) owner.headers.set("content-type", contentType);
|
|
165
|
-
bodies.set(owner, {
|
|
166
|
-
value,
|
|
167
|
-
used: false
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
function getBodyValue(owner) {
|
|
171
|
-
return bodies.get(owner)?.value;
|
|
172
|
-
}
|
|
173
|
-
function assertBodyUnused(owner) {
|
|
174
|
-
if (bodies.get(owner)?.used) throw new TypeError("Body has already been consumed");
|
|
175
|
-
}
|
|
176
|
-
/** 在异步转换之前同步占用 body,确保并发读取与请求发送共享消费状态。 */
|
|
177
|
-
function consumeBodyValue(owner) {
|
|
178
|
-
assertBodyUnused(owner);
|
|
179
|
-
const state = bodies.get(owner);
|
|
180
|
-
if (state && state.value != null) state.used = true;
|
|
181
|
-
return state?.value;
|
|
182
|
-
}
|
|
183
|
-
async function bodyBytes(value) {
|
|
184
|
-
if (value == null) return /* @__PURE__ */ new ArrayBuffer(0);
|
|
185
|
-
if (typeof value === "string") return encodeText(value);
|
|
186
|
-
if (isArrayBufferLike(value)) return cloneArrayBuffer(value);
|
|
187
|
-
if (ArrayBuffer.isView(value)) return cloneArrayBufferView(value);
|
|
188
|
-
if (isBlobLike(value)) return cloneArrayBuffer(await value.arrayBuffer());
|
|
189
|
-
return (await encodeMultipartFormData(value)).body;
|
|
190
|
-
}
|
|
191
|
-
/** 仅供 Request/Response 内部复用,不作为包的公开导出。 */
|
|
192
|
-
var HttpBody = class {
|
|
193
|
-
get body() {
|
|
194
|
-
return null;
|
|
195
|
-
}
|
|
196
|
-
get bodyUsed() {
|
|
197
|
-
return bodies.get(this)?.used === true;
|
|
198
|
-
}
|
|
199
|
-
async arrayBuffer() {
|
|
200
|
-
return bodyBytes(consumeBodyValue(this));
|
|
201
|
-
}
|
|
202
|
-
async bytes() {
|
|
203
|
-
return new Uint8Array(await this.arrayBuffer());
|
|
204
|
-
}
|
|
205
|
-
async text() {
|
|
206
|
-
return decodeText(await this.arrayBuffer());
|
|
207
|
-
}
|
|
208
|
-
async json() {
|
|
209
|
-
return JSON.parse(await this.text());
|
|
210
|
-
}
|
|
211
|
-
async blob() {
|
|
212
|
-
const bytes = await this.arrayBuffer();
|
|
213
|
-
const contentType = this.headers.get("content-type") ?? "";
|
|
214
|
-
const type = /^[!#$%&'*+.^`|~\w-]+\/[!#$%&'*+.^`|~\w-]+(?:\s*;[^\r\n]*)?$/.test(contentType) ? contentType : "";
|
|
215
|
-
return new (typeof Blob === "function" ? Blob : BlobPolyfill)([bytes], { type });
|
|
216
|
-
}
|
|
217
|
-
async formData() {
|
|
218
|
-
throw new TypeError("formData is not supported in Body polyfill");
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
//#endregion
|
|
222
|
-
//#region src/http/request.ts
|
|
223
|
-
var RequestPolyfill = class RequestPolyfill extends HttpBody {
|
|
224
|
-
url;
|
|
225
|
-
method;
|
|
226
|
-
headers;
|
|
227
|
-
signal;
|
|
228
|
-
[Symbol.toStringTag] = "Request";
|
|
229
|
-
constructor(input, init = {}) {
|
|
230
|
-
super();
|
|
231
|
-
const request = input instanceof RequestPolyfill ? input : void 0;
|
|
232
|
-
this.url = typeof input === "string" ? input : isUrlInstance(input) ? input.toString() : request?.url ?? "";
|
|
233
|
-
this.method = String(init.method ?? request?.method ?? "GET").toUpperCase();
|
|
234
|
-
this.headers = new HeadersPolyfill(init.headers ?? request?.headers);
|
|
235
|
-
this.signal = init.signal ?? request?.signal ?? null;
|
|
236
|
-
const inherited = init.body == null && request;
|
|
237
|
-
if (inherited) assertBodyUnused(request);
|
|
238
|
-
initializeBody(this, init.body ?? (request ? getBodyValue(request) : void 0));
|
|
239
|
-
if (inherited) consumeBodyValue(request);
|
|
240
|
-
}
|
|
241
|
-
clone() {
|
|
242
|
-
assertBodyUnused(this);
|
|
243
|
-
return new RequestPolyfill(this.url, {
|
|
244
|
-
method: this.method,
|
|
245
|
-
headers: this.headers,
|
|
246
|
-
signal: this.signal,
|
|
247
|
-
body: getBodyValue(this)
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
function getRequestBodyValue(request) {
|
|
252
|
-
return request ? getBodyValue(request) : void 0;
|
|
253
|
-
}
|
|
254
|
-
//#endregion
|
|
255
|
-
//#region src/http/response.ts
|
|
256
|
-
var ResponsePolyfill = class ResponsePolyfill extends HttpBody {
|
|
257
|
-
headers;
|
|
258
|
-
status;
|
|
259
|
-
statusText;
|
|
260
|
-
ok;
|
|
261
|
-
url;
|
|
262
|
-
redirected = false;
|
|
263
|
-
type;
|
|
264
|
-
[Symbol.toStringTag] = "Response";
|
|
265
|
-
constructor(body, init = {}) {
|
|
266
|
-
super();
|
|
267
|
-
this.status = Number.isFinite(init.status) ? init.status : 200;
|
|
268
|
-
this.statusText = init.statusText ?? "";
|
|
269
|
-
this.ok = this.status >= 200 && this.status < 300;
|
|
270
|
-
this.headers = new HeadersPolyfill(init.headers);
|
|
271
|
-
this.url = init.url ?? "";
|
|
272
|
-
this.type = init.type ?? "basic";
|
|
273
|
-
initializeBody(this, body);
|
|
274
|
-
}
|
|
275
|
-
static error() {
|
|
276
|
-
return new ResponsePolyfill(null, {
|
|
277
|
-
status: 0,
|
|
278
|
-
type: "error"
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
static json(data, init = {}) {
|
|
282
|
-
const body = JSON.stringify(data);
|
|
283
|
-
if (body === void 0) throw new TypeError("Failed to execute 'json' on 'Response': data is not JSON serializable");
|
|
284
|
-
const headers = new HeadersPolyfill(init.headers);
|
|
285
|
-
if (!headers.has("content-type")) headers.set("content-type", "application/json");
|
|
286
|
-
return new ResponsePolyfill(body, {
|
|
287
|
-
...init,
|
|
288
|
-
headers
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
clone() {
|
|
292
|
-
assertBodyUnused(this);
|
|
293
|
-
return new ResponsePolyfill(getBodyValue(this), {
|
|
294
|
-
status: this.status,
|
|
295
|
-
statusText: this.statusText,
|
|
296
|
-
headers: this.headers,
|
|
297
|
-
url: this.url,
|
|
298
|
-
type: this.type
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
//#endregion
|
|
303
|
-
export { encodeMultipartFormData as a, consumeBodyValue as i, RequestPolyfill as n, HeadersPolyfill as o, getRequestBodyValue as r, headersToObject as s, ResponsePolyfill as t };
|