|
| 1 | +// testharness file with ShadowRealm utilities to be imported in the realm |
| 2 | +// hosting the ShadowRealm |
| 3 | + |
| 4 | +/** |
| 5 | + * Convenience function for evaluating some async code in the ShadowRealm and |
| 6 | + * waiting for the result. |
| 7 | + * |
| 8 | + * @param {ShadowRealm} realm - the ShadowRealm to evaluate the code in |
| 9 | + * @param {string} asyncBody - the code to evaluate; will be put in the body of |
| 10 | + * an async function, and must return a value explicitly if a value is to be |
| 11 | + * returned to the hosting realm. |
| 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 | + * Convenience adaptor function for fetch() that can be passed to |
| 25 | + * setShadowRealmGlobalProperties() (see testharness-shadowrealm-inner.js). |
| 26 | + * Used to adapt the hosting realm's fetch(), if present, to fetch a resource |
| 27 | + * and pass its text through the callable boundary to the ShadowRealm. |
| 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 | + * Used when the hosting realm is a worker. This value is a Promise that |
| 38 | + * resolves to a function that posts a message to the worker's message port, |
| 39 | + * just like postMessage(). The message port is only available asynchronously in |
| 40 | + * SharedWorkers and ServiceWorkers. |
| 41 | + */ |
| 42 | +globalThis.getPostMessageFunc = async function () { |
| 43 | + if (typeof postMessage === "function") { |
| 44 | + return postMessage; // postMessage available directly in dedicated worker |
| 45 | + } |
| 46 | + |
| 47 | + if (typeof clients === "object") { |
| 48 | + // Messages from the ShadowRealm are not in response to any message received |
| 49 | + // from the ServiceWorker's client, so broadcast them to all clients |
| 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 | +// Port available asynchronously in shared worker, but not via an async func |
| 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 | + * Used when the hosting realm does not permit dynamic import, e.g. in |
| 77 | + * ServiceWorkers or AudioWorklets. Requires an adaptor function such as |
| 78 | + * fetchAdaptor() above, or an equivalent if fetch() is not present in the |
| 79 | + * hosting realm. |
| 80 | + * |
| 81 | + * @param {ShadowRealm} realm - the ShadowRealm in which to setup a |
| 82 | + * fakeDynamicImport() global function. |
| 83 | + * @param {function} adaptor - an adaptor function that does what fetchAdaptor() |
| 84 | + * does. |
| 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 | + * Used when the hosting realm does not expose fetch(), i.e. in worklets. The |
| 106 | + * port on the other side of the channel needs to send messages starting with |
| 107 | + * 'fetchRequest::' and listen for messages starting with 'fetchResult::'. See |
| 108 | + * testharness-shadowrealm-audioworkletprocessor.js. |
| 109 | + * |
| 110 | + * @param {port} MessagePort - the message port on which to listen for fetch |
| 111 | + * requests |
| 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