@wevu/web-apis
Version:
Web API polyfills and global installers for mini-program runtimes
187 lines (186 loc) • 5.84 kB
JavaScript
import { f as isUrlInstance, i as decodeText, l as normalizeHeaderName, n as cloneArrayBuffer, o as encodeText, r as cloneArrayBufferView } from "./shared-BB491DgN.mjs";
//#region src/http.ts
function isIterableHeaders(input) {
return Boolean(input) && typeof input[Symbol.iterator] === "function";
}
function isHeaderObject(input) {
return typeof input === "object" && input !== null;
}
var HeadersPolyfill = class {
store = /* @__PURE__ */ new Map();
constructor(init) {
if (!init) return;
if (typeof init.forEach === "function") {
init.forEach((value, key) => {
this.set(key, value);
});
return;
}
if (isIterableHeaders(init)) {
for (const [key, value] of init) this.append(key, value);
return;
}
if (isHeaderObject(init)) for (const [key, value] of Object.entries(init)) this.append(key, Array.isArray(value) ? value.join(", ") : String(value));
}
append(key, value) {
const current = this.get(key);
this.set(key, current ? `${current}, ${value}` : value);
}
set(key, value) {
const normalized = normalizeHeaderName(key);
if (!normalized) return;
this.store.set(normalized, {
key,
value: String(value)
});
}
get(key) {
return this.store.get(normalizeHeaderName(key))?.value ?? null;
}
has(key) {
return this.store.has(normalizeHeaderName(key));
}
delete(key) {
this.store.delete(normalizeHeaderName(key));
}
forEach(callback) {
for (const { key, value } of this.store.values()) callback(value, key);
}
entries() {
return Array.from(this.store.values(), (item) => [item.key, item.value])[Symbol.iterator]();
}
keys() {
return Array.from(this.store.values(), (item) => item.key)[Symbol.iterator]();
}
values() {
return Array.from(this.store.values(), (item) => item.value)[Symbol.iterator]();
}
[Symbol.iterator]() {
return this.entries();
}
};
const requestBodyStore = /* @__PURE__ */ new WeakMap();
const requestBodyUsedStore = /* @__PURE__ */ new WeakMap();
const responseBodyStore = /* @__PURE__ */ new WeakMap();
const responseBodyUsedStore = /* @__PURE__ */ new WeakMap();
function normalizeBody(body) {
if (body == null || typeof body === "string" || body instanceof ArrayBuffer || ArrayBuffer.isView(body)) return body;
if (typeof Blob !== "undefined" && body instanceof Blob) return body;
return String(body);
}
async function readBodyAsArrayBuffer(body) {
if (body == null) return /* @__PURE__ */ new ArrayBuffer(0);
if (typeof body === "string") return encodeText(body);
if (body instanceof ArrayBuffer) return cloneArrayBuffer(body);
if (ArrayBuffer.isView(body)) return cloneArrayBufferView(body);
if (typeof Blob !== "undefined" && body instanceof Blob) return body.arrayBuffer();
return encodeText(String(body));
}
async function readBodyAsText(body) {
if (typeof body === "string") return body;
return decodeText(await readBodyAsArrayBuffer(body));
}
function getRequestBodyValue(request) {
return request ? requestBodyStore.get(request) : void 0;
}
function getResponseBodyValue(response) {
return responseBodyStore.get(response);
}
var RequestPolyfill = class RequestPolyfill {
url;
method;
headers;
signal;
[Symbol.toStringTag] = "Request";
constructor(input, init = {}) {
const request = input instanceof RequestPolyfill ? input : void 0;
this.url = typeof input === "string" ? input : isUrlInstance(input) ? input.toString() : request?.url ?? "";
this.method = String(init.method ?? request?.method ?? "GET").toUpperCase();
this.headers = new HeadersPolyfill(init.headers ?? request?.headers);
this.signal = init.signal ?? request?.signal ?? null;
requestBodyStore.set(this, normalizeBody(init.body ?? getRequestBodyValue(request)));
requestBodyUsedStore.set(this, false);
}
get body() {
return null;
}
get bodyUsed() {
return requestBodyUsedStore.get(this) === true;
}
async arrayBuffer() {
requestBodyUsedStore.set(this, true);
return readBodyAsArrayBuffer(getRequestBodyValue(this));
}
async text() {
requestBodyUsedStore.set(this, true);
return readBodyAsText(getRequestBodyValue(this));
}
clone() {
return new RequestPolyfill(this.url, {
method: this.method,
headers: this.headers,
signal: this.signal,
body: getRequestBodyValue(this)
});
}
};
var ResponsePolyfill = class ResponsePolyfill {
headers;
status;
statusText;
ok;
url;
redirected = false;
type = "basic";
[Symbol.toStringTag] = "Response";
constructor(body, init = {}) {
responseBodyStore.set(this, normalizeBody(body));
responseBodyUsedStore.set(this, false);
this.status = Number.isFinite(init.status) ? init.status : 200;
this.statusText = init.statusText ?? "";
this.ok = this.status >= 200 && this.status < 300;
this.headers = new HeadersPolyfill(init.headers);
this.url = init.url ?? "";
}
get body() {
return null;
}
get bodyUsed() {
return responseBodyUsedStore.get(this) === true;
}
async arrayBuffer() {
responseBodyUsedStore.set(this, true);
return readBodyAsArrayBuffer(getResponseBodyValue(this));
}
async blob() {
if (typeof Blob !== "function") throw new TypeError("Blob is unavailable in current runtime");
return new Blob([await this.arrayBuffer()]);
}
async formData() {
throw new TypeError("formData is not supported in Response polyfill");
}
async json() {
return JSON.parse(await this.text());
}
async text() {
responseBodyUsedStore.set(this, true);
return readBodyAsText(getResponseBodyValue(this));
}
clone() {
return new ResponsePolyfill(getResponseBodyValue(this), {
status: this.status,
statusText: this.statusText,
headers: this.headers,
url: this.url
});
}
};
function headersToObject(headers) {
const result = {};
headers.forEach((value, key) => {
result[key] = value;
});
return result;
}
//#endregion
export { HeadersPolyfill, RequestPolyfill, ResponsePolyfill, headersToObject };