Created
March 27, 2017 00:42
-
-
Save muralisr/6ce88427c2ecf34b30c1a7cd7536dfe8 to your computer and use it in GitHub Desktop.
joy fixed service worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dexecure = { | |
"server": "https://withjoy.dexecure.net/", | |
"firstPartyDomain": [ | |
"dev-media.withjoy.com", | |
"rehearsal-media.withjoy.com", | |
"dev.withjoy.com", | |
"rehearsal.withjoy.com", | |
"ceremony-media.withjoy.com", | |
"withjoy.com" | |
], | |
"confirmedImageDomains": [ | |
"dev-media.withjoy.com", | |
"rehearsal-media.withjoy.com", | |
"ceremony-media.withjoy.com" | |
], | |
"optimisationsEnabled": true, | |
"debugMode": false, | |
"imageMatchRegex": "\\.jpe?g|\\.png" | |
};"use strict";function urlContainsSignature(e){var r=e.substring(e.lastIndexOf("/")+1);return r=r.toLowerCase(),r.includes("se")&&r.includes("st")&&r.includes("sp")&&r.includes("sv")&&r.includes("sr")&&r.includes("sig")} | |
function separateSignatureAndURL(e){ | |
var r=e.substring(e.lastIndexOf("/")+1),t=e.substring(0,e.lastIndexOf("/")); | |
return{potentialURL:t,potentialSignature:r}} | |
function convertToDexecureURL(e){var r={ | |
"rehearsal-media.withjoy.com": "d171kkocp64eig.cloudfront.net", | |
"dev.withjoy.com": "d218ixx1cwqp18.cloudfront.net", | |
"dev-media.withjoy.com":"d2yhjivysbovll.cloudfront.net", | |
"rehearsal.withjoy.com": "d25rpokoeorypg.cloudfront.net", | |
"ceremony-media.withjoy.com": "d31ikebgmilz0f.cloudfront.net", | |
"withjoy.com": "d2ytcdhzgn2xcf.cloudfront.net"}, | |
t=new URL(e);for(i in r)if(i.toLowerCase()==t.host.toLowerCase())return e.replace(i,r[i]);return e}function isDefiniteImageURL(e){for(var r=new URL(e),t=dexecure.confirmedImageDomains.length-1;t>=0;t--)if(dexecure.confirmedImageDomains[t].toLowerCase()==r.host.toLowerCase())return!0;return!1}function isFirstPartyDomain(e){var r=new URL(e),t=dexecure.firstPartyDomain;if(!t)return dexecure.debugMode&&console.log("firstPartyDomain is null"),!1;for(var n=t.length-1;n>=0;n--)if(t[n].toLowerCase()==r.host.toLowerCase())return!0;return dexecure.debugMode&&console.log("firstPartyDomain is false for ",e),!1}dexecure.optimisationsEnabled&&(self.addEventListener("install",function(e){e.waitUntil(self.skipWaiting())}),self.addEventListener("activate",function(e){e.waitUntil(self.clients.claim())}),self.addEventListener("fetch",function(e){var r={};e.request.headers.has("Accept")&&(r.Accept=e.request.headers. | |
("Accept"));var t=new Headers(r),n=new RegExp(dexecure.imageMatchRegex,"i");if((isDefiniteImageURL(e.request.url.toLowerCase())||n.test(e.request.url.toLowerCase()))&&"get"==e.request.method.toLowerCase()&&isFirstPartyDomain(e.request.url)){var o=convertToDexecureURL(e.request.url),i="";if(urlContainsSignature(o)){dexecure.debugMode&&console.log("url contains signature");var s=separateSignatureAndURL(o);dexecure.debugMode&&console.log("separatedComponents is ",s),o=s.potentialURL,i=s.potentialSignature,o=decodeURIComponent(o)}dexecure.debugMode&&(console.log("dexecureURL is ",o),console.log("signatureComponent is ",i),console.log("Modified url is ",o)),i&&i.length>0&&(t.append("signaturepresent","true"),t.append("signatureseparator","/"),t.append("signature",i)),e.respondWith(fetch(o,{mode:"cors",headers:t}).then(function(r){if(r.ok)return r;throw dexecure.debugMode&&console.log("Responding with original image as optimiser was not reachable ",e.request.url),new Error("Unable to fetch optimised image")}).catch(function(r){return dexecure.debugMode&&(console.log("Sending original image as an error occured when trying to optimise ",e.request.url),console.log("The error was ",r)),fetch(e.request)}))}})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment