sharedWorker_sharedWorker.js (2818B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 "use strict"; 6 7 if (!("self" in this)) { 8 throw new Error("No 'self' exists on SharedWorkerGlobalScope!"); 9 } 10 if (this !== self) { 11 throw new Error("'self' not equal to global object!"); 12 } 13 if (!(self instanceof SharedWorkerGlobalScope)) { 14 throw new Error("self not a SharedWorkerGlobalScope instance!"); 15 } 16 17 var propsToCheck = [ 18 "location", 19 "navigator", 20 "close", 21 "importScripts", 22 "setTimeout", 23 "clearTimeout", 24 "setInterval", 25 "clearInterval", 26 "dump", 27 "atob", 28 "btoa", 29 ]; 30 31 for (var index = 0; index < propsToCheck.length; index++) { 32 var prop = propsToCheck[index]; 33 if (!(prop in self)) { 34 throw new Error("SharedWorkerGlobalScope has no '" + prop + "' property!"); 35 } 36 } 37 38 onconnect = function (event) { 39 if (!("SharedWorkerGlobalScope" in self)) { 40 throw new Error("SharedWorkerGlobalScope should be visible!"); 41 } 42 if (!(self instanceof SharedWorkerGlobalScope)) { 43 throw new Error("The global should be a SharedWorkerGlobalScope!"); 44 } 45 if (!(self instanceof WorkerGlobalScope)) { 46 throw new Error("The global should be a WorkerGlobalScope!"); 47 } 48 if ("DedicatedWorkerGlobalScope" in self) { 49 throw new Error("DedicatedWorkerGlobalScope should not be visible!"); 50 } 51 if (!(event instanceof MessageEvent)) { 52 throw new Error("'connect' event is not a MessageEvent!"); 53 } 54 if (!("ports" in event)) { 55 throw new Error("'connect' event doesn't have a 'ports' property!"); 56 } 57 if (event.ports.length != 1) { 58 throw new Error( 59 "'connect' event has a 'ports' property with length '" + 60 event.ports.length + 61 "'!" 62 ); 63 } 64 if (!event.ports[0]) { 65 throw new Error("'connect' event has a null 'ports[0]' property!"); 66 } 67 if (!(event.ports[0] instanceof MessagePort)) { 68 throw new Error( 69 "'connect' event has a 'ports[0]' property that isn't a " + "MessagePort!" 70 ); 71 } 72 if (!(event.ports[0] == event.source)) { 73 throw new Error("'connect' event source property is incorrect!"); 74 } 75 if (event.data) { 76 throw new Error("'connect' event has data: " + event.data); 77 } 78 79 // Statement after return should trigger a warning, but NOT fire error events 80 // at us. 81 (function () { 82 return; 83 // eslint-disable-next-line no-unreachable 84 1; 85 }); 86 87 event.ports[0].onmessage = function (msg) { 88 if (!(msg instanceof MessageEvent)) { 89 throw new Error("'message' event is not a MessageEvent!"); 90 } 91 if (!("ports" in msg)) { 92 throw new Error("'message' event doesn't have a 'ports' property!"); 93 } 94 if (msg.ports === null) { 95 throw new Error("'message' event has a null 'ports' property!"); 96 } 97 msg.target.postMessage(msg.data); 98 throw new Error(msg.data); 99 }; 100 };