@worker-tools/location-polyfill
Version:
A Location polyfill for Cloudflare Workers.
56 lines • 3.02 kB
JavaScript
// deno-lint-ignore-file no-explicit-any no-explicit-any
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _a, _b, _c;
var _WorkerLocationPolyfill_url;
// @ts-ignore: Deno doesn't know about WorkerLocation
class WorkerLocationPolyfill {
constructor(href) {
_WorkerLocationPolyfill_url.set(this, void 0);
__classPrivateFieldSet(this, _WorkerLocationPolyfill_url, new URL(href), "f");
}
get hash() { return ''; }
get host() { return __classPrivateFieldGet(this, _WorkerLocationPolyfill_url, "f").host; }
get hostname() { return __classPrivateFieldGet(this, _WorkerLocationPolyfill_url, "f").hostname; }
get href() { return __classPrivateFieldGet(this, _WorkerLocationPolyfill_url, "f").href; }
get origin() { return __classPrivateFieldGet(this, _WorkerLocationPolyfill_url, "f").origin; }
get pathname() { return '/'; }
get port() { return __classPrivateFieldGet(this, _WorkerLocationPolyfill_url, "f").port; }
get protocol() { return __classPrivateFieldGet(this, _WorkerLocationPolyfill_url, "f").protocol; }
get search() { return ''; }
toString() { return this.href; }
}
_WorkerLocationPolyfill_url = new WeakMap();
function defineProperty(url, writable = false) {
Object.defineProperty(self, 'location', {
configurable: false,
enumerable: true,
writable,
value: new WorkerLocationPolyfill(url),
});
}
function polyfillLocation(event) {
// @ts-ignore: Deno doesn't know about FetchEvent
const _event = event;
defineProperty(_event.request.url, true);
}
if (!('location' in self)) {
const envLoc = (_a = (self.WORKER_LOCATION)) !== null && _a !== void 0 ? _a : ((_c = (_b = self.process) === null || _b === void 0 ? void 0 : _b.env) === null || _c === void 0 ? void 0 : _c.WORKER_LOCATION);
if (envLoc) {
defineProperty(envLoc);
}
else {
self.addEventListener('fetch', polyfillLocation);
}
}
//# sourceMappingURL=index.js.map
;