User:Thundercraft5/sw.js

// Add any test code here. // DO NOT SUBMIT TO JSRT! /* global self, Request */ /* jshint undef:true, esversion: 6 */ const map = { "test": "User:Thundercraft5/sw.js/module.js", "test2": "User:Thundercraft5/sw.js/module2.js", "self": "User:${USER}/${PATH}", "wiki": "MediaWiki:/${PATH}", };

function apiFetch(method, params = {}, fetchOptions = {}) { method = method.toUpperCase; const defaults = { format: "json", };	/* jshint ignore:start */ const entries = Object.entries({ ...params, ...defaults }); const toString = => entries.map(([k, v]) => `${k}=${Array.isArray(v) ? v.join("|") : v}`).join("&"); /* jshint ignore:end */

if (method === "POST") { /* jshint ignore:start */ return fetch("/api.php", {			body: toString,			method,			headers: {				["content-type"]: "application/x-www-form-urlencoded; charset=UTF-8"			},			...fetchOptions,		}).then(r => r.json); /* jshint ignore:end */ } else { /* jshint ignore:start */ return fetch(`/api.php?${ toString }`, { method, ...fetchOptions }).then(r => r.json); /* jshint ignore:end */ } }

const userInfo = { id: null, name: null, messages: null, groups: null, groupmemberships: null, implicitgroups: null, rights: null, changeablegroups: null, editcount: null, ratelimits: null, latestcontrib: null, };

const npmProviders = { "unpkg": "https://unpkg.com/${PACKAGE}@${VERSION|latest}/${PATH}", "esm-sh": "https://esm.sh/${PACKAGE}@${VERSION|latest}/${PATH}", "jsdelivr": "https://cdn.jsdelivr.net/npm/${PACKAGE}@${VERSION|latest}/${PATH}", };

function formatString(s, replacements) { return s.replace(/\${(? \w+)(?:\|(?.+?)?)?}/g, (...args) => {		const { name, defaultValue } = args.pop;		return replacements[name] || defaultValue || "";	}); }

async function eventHandler { let { request: { url }, request } = this; url = new URL(url); console.log(this.clientId, url, Object.fromEntries(request.headers)); if (url.pathname.match(/^\/@(?!.*\/wiki\/).*(\.[mc]?js$)?/)) { let normalized = url.pathname.replace(/^\/@/g, ""); console.log(normalized); const isSpecifier = ![".", "/"].some(s => normalized.includes(s)); const { groups: providerMatch } = normalized.match(/^(? [\w\-]+):(? [a-z\-0-9]+)(?:@(? [\d\.]{1,3}\d+|latest))?(? [^\&\?\#]+)?/) || {};		let responseUrl; if (!providerMatch) { if (isSpecifier && !providerMatch) { normalized = map[normalized]; if (!normalized) throw new Error(`Module specifier "${normalized}" could not be resolved`); return Response.redirect("/@" + normalized); }			console.log(normalized); url.searchParams.append("action", "raw"); url.searchParams.append("ctype", "text/javascript"); url.searchParams.set("title", normalized); url.pathname = `/index.php`; responseUrl = new URL(url); responseUrl.pathname = `/@${normalized.split("/").slice(0, -1).join("/")}/index.php`; } else if (npmProviders[providerMatch.provider]) { const { provider, path, version = "latest", package: packageName } = providerMatch; url = new URL(formatString(npmProviders[provider], { PACKAGE: packageName, PATH: path, VERSION: version, }));			responseUrl = new URL(url); }		return fetch(new Request(url, { ...request })) .then(async response => {				const headers = Object.fromEntries(response.headers);				const validTypes = ["application/javascript", "text/javascript"];				const type = headers?.["content-type"]?.split(";").shift;				if (!validTypes.includes(type)) 					throw new Error(`Server responded with a MIME type of "${type}", expected ${validTypes.map(s => `"${s}"`).join(" or ")} instead`);				return [					{ ...response, url }, 					headers,					await response.text,				];			}) .then(([response, headers, text]) => new Response(new Blob([text], { type: headers["content-type"] }), { ...response, url: responseUrl, redirected: true, })); 	} };

self.addEventListener('fetch', event => { 	const result = eventHandler.call(event);	event.respondWith(result.then(r => {		if (r === undefined) return fetch(event.request);		else return r;	})); });

self.addEventListener('activate', event => {	console.log(event);	apiFetch("GET", { "action": "query", "format": "json", "meta": "userinfo", "uiprop": "latestcontrib|ratelimits|editcount|changeablegroups|rights|implicitgroups|groups|groupmemberships|blockinfo|hasmsg" }).then(({ query: { userinfo } }) => Object.assign(userInfo, userinfo)); });

self.addEventListener('install', event => {	console.log(event);	});