pixelpinch-proxy / src /polyfills.js
Sacredeye's picture
Upload 10 files
8de1fe2 verified
Raw History Blame Contribute Delete
3.52 kB
// Polyfills for Bun runtime (minimal - Bun has most built-in)
import { Blob } from 'buffer';
// Safe global access helper
function safeGlobalAssign(name, value) {
try {
if (typeof global !== 'undefined' && !global[name]) {
global[name] = value;
return true;
}
} catch (error) {
console.warn(`Failed to assign global ${name}:`, error.message);
}
return false;
}
// Bun has native File and FormData support, but add fallbacks for edge cases
if (typeof global.File === 'undefined') {
safeGlobalAssign('File', class File extends Blob {
constructor(bits, name, options = {}) {
// Input validation
if (!bits || !Array.isArray(bits)) {
throw new TypeError('File constructor requires bits array');
}
if (typeof name !== 'string') {
throw new TypeError('File constructor requires name string');
}
super(bits, options);
this.name = name;
this.lastModified = options.lastModified || Date.now();
this.type = options.type || '';
this.size = this.size || 0;
this.webkitRelativePath = '';
}
});
}
if (typeof global.FormData === 'undefined') {
safeGlobalAssign('FormData', class FormData {
constructor() {
this.data = new Map();
}
append(name, value, filename) {
// Input validation
if (typeof name !== 'string') {
throw new TypeError('FormData.append requires name string');
}
if (value === undefined || value === null) {
throw new TypeError('FormData.append requires value');
}
this.data.set(name, { value, filename });
}
get(name) {
if (typeof name !== 'string') {
return null;
}
const entry = this.data.get(name);
return entry ? entry.value : null;
}
getAll(name) {
if (typeof name !== 'string') {
return [];
}
const entry = this.data.get(name);
return entry ? [entry.value] : [];
}
has(name) {
if (typeof name !== 'string') {
return false;
}
return this.data.has(name);
}
delete(name) {
if (typeof name !== 'string') {
return false;
}
return this.data.delete(name);
}
set(name, value, filename) {
if (typeof name !== 'string') {
throw new TypeError('FormData.set requires name string');
}
if (value === undefined || value === null) {
throw new TypeError('FormData.set requires value');
}
this.data.set(name, { value, filename });
}
entries() {
const result = [];
for (const [name, entry] of this.data) {
result.push([name, entry.value]);
}
return result[Symbol.iterator]();
}
keys() {
return this.data.keys();
}
values() {
const result = [];
for (const entry of this.data.values()) {
result.push(entry.value);
}
return result[Symbol.iterator]();
}
forEach(callback, thisArg) {
if (typeof callback !== 'function') {
throw new TypeError('FormData.forEach requires callback function');
}
for (const [name, entry] of this.data) {
callback.call(thisArg, entry.value, name, this);
}
}
[Symbol.iterator]() {
return this.entries();
}
});
}
export default {};