|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +globalThis.shadowRealmEvalAsync = function (realm, asyncBody) { |
| 14 | + return new Promise(realm.evaluate(` |
| 15 | + (resolve, reject) => { |
| 16 | + (async () => { |
| 17 | + ${asyncBody} |
| 18 | + })().then(resolve, (e) => reject(e.toString())); |
| 19 | + } |
| 20 | + `)); |
| 21 | +}; |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +globalThis.fetchAdaptor = (resource) => (resolve, reject) => { |
| 30 | + fetch(resource) |
| 31 | + .then(res => res.text()) |
| 32 | + .then(resolve, (e) => reject(e.toString())); |
| 33 | +}; |
| 34 | + |
| 35 | +let sharedWorkerMessagePortPromise; |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +globalThis.getPostMessageFunc = async function () { |
| 43 | + if (typeof postMessage === "function") { |
| 44 | + return postMessage; |
| 45 | + } |
| 46 | + |
| 47 | + if (typeof clients === "object") { |
| 48 | + |
| 49 | + |
| 50 | + const allClients = await clients.matchAll({ includeUncontrolled: true }); |
| 51 | + return function broadcast(msg) { |
| 52 | + allClients.map(client => client.postMessage(msg)); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (sharedWorkerMessagePortPromise) { |
| 57 | + return await sharedWorkerMessagePortPromise; |
| 58 | + } |
| 59 | + |
| 60 | + throw new Error("getPostMessageFunc is intended for Worker scopes"); |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +let savedResolver; |
| 65 | +if (globalThis.constructor.name === "SharedWorkerGlobalScope") { |
| 66 | + sharedWorkerMessagePortPromise = new Promise((resolve) => { |
| 67 | + savedResolver = resolve; |
| 68 | + }); |
| 69 | + addEventListener("connect", function (event) { |
| 70 | + const port = event.ports[0]; |
| 71 | + savedResolver(port.postMessage.bind(port)); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +globalThis.setupFakeDynamicImportInShadowRealm = function(realm, adaptor) { |
| 87 | + function fetchModuleTextExecutor(url) { |
| 88 | + return (resolve, reject) => { |
| 89 | + new Promise(adaptor(url)) |
| 90 | + .then(text => realm.evaluate(text + ";\nundefined")) |
| 91 | + .then(resolve, (e) => reject(e.toString())); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + realm.evaluate(` |
| 96 | + (fetchModuleTextExecutor) => { |
| 97 | + globalThis.fakeDynamicImport = function (url) { |
| 98 | + return new Promise(fetchModuleTextExecutor(url)); |
| 99 | + } |
| 100 | + } |
| 101 | + `)(fetchModuleTextExecutor); |
| 102 | +}; |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +globalThis.setupFakeFetchOverMessagePort = function (port) { |
| 114 | + port.addEventListener("message", (event) => { |
| 115 | + if (typeof event.data !== "string" || !event.data.startsWith("fetchRequest::")) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + fetch(event.data.slice("fetchRequest::".length)) |
| 120 | + .then(res => res.text()) |
| 121 | + .then( |
| 122 | + text => port.postMessage(`fetchResult::success::${text}`), |
| 123 | + error => port.postMessage(`fetchResult::fail::${error}`), |
| 124 | + ); |
| 125 | + }); |
| 126 | + port.start(); |
| 127 | +} |
0 commit comments