UNPKG

@wevu/web-apis

Version:

Web API polyfills and global installers for mini-program runtimes

84 lines (83 loc) 2.75 kB
import { i as decodeText, n as cloneArrayBuffer, o as encodeText, r as cloneArrayBufferView } from "./shared-pgiOoBm7.mjs"; //#region src/web.ts function normalizeBlobPart(part) { if (typeof part === "string") return Promise.resolve(encodeText(part)); if (part && typeof part === "object" && typeof part.arrayBuffer === "function") return part.arrayBuffer(); if (part instanceof ArrayBuffer) return Promise.resolve(cloneArrayBuffer(part)); if (ArrayBuffer.isView(part)) return Promise.resolve(cloneArrayBufferView(part)); return Promise.resolve(encodeText(String(part))); } var BlobPolyfill = class BlobPolyfill { size; type; parts; constructor(parts = [], options) { this.parts = [...parts]; this.type = options?.type ?? ""; this.size = parts.reduce((total, part) => { if (typeof part === "string") return total + String(part).length; if (part instanceof BlobPolyfill) return total + part.size; if (part instanceof ArrayBuffer) return total + part.byteLength; if (ArrayBuffer.isView(part)) return total + part.byteLength; return total; }, 0); } async arrayBuffer() { const buffers = await Promise.all(this.parts.map((part) => normalizeBlobPart(part))); const totalLength = buffers.reduce((sum, buffer) => sum + buffer.byteLength, 0); const merged = new Uint8Array(totalLength); let offset = 0; for (const buffer of buffers) { merged.set(new Uint8Array(buffer), offset); offset += buffer.byteLength; } return merged.buffer; } async text() { return decodeText(await this.arrayBuffer()); } }; var FormDataPolyfill = class { entriesList = []; append(name, value) { this.entriesList.push([String(name), value instanceof BlobPolyfill ? value : String(value)]); } delete(name) { const normalizedName = String(name); let index = this.entriesList.length; while (index-- > 0) if (this.entriesList[index]?.[0] === normalizedName) this.entriesList.splice(index, 1); } get(name) { return this.entriesList.find((entry) => entry[0] === String(name))?.[1] ?? null; } getAll(name) { return this.entriesList.filter((entry) => entry[0] === String(name)).map((entry) => entry[1]); } has(name) { return this.entriesList.some((entry) => entry[0] === String(name)); } set(name, value) { this.delete(name); this.append(name, value); } forEach(callback) { for (const [key, value] of this.entriesList) callback(value, key, this); } *entries() { yield* this.entriesList; } *keys() { for (const [key] of this.entriesList) yield key; } *values() { for (const [, value] of this.entriesList) yield value; } [Symbol.iterator]() { return this.entries(); } get [Symbol.toStringTag]() { return "FormData"; } }; //#endregion export { BlobPolyfill, FormDataPolyfill };