From 2856ce3217285b1d7c0edfebe2ff46c0b6628c64 Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 9 May 2024 10:28:01 +0800 Subject: [PATCH 1/3] Add audioWorklet globals --- data/browser.mjs | 7 ++++++ globals.json | 7 ++++++ scripts/get-browser-globals.mjs | 42 +++++++++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/data/browser.mjs b/data/browser.mjs index 604015c..24a9279 100644 --- a/data/browser.mjs +++ b/data/browser.mjs @@ -33,7 +33,9 @@ export default { AudioScheduledSourceNode: false, AudioSinkInfo: false, AudioWorklet: false, + AudioWorkletGlobalScope: false, AudioWorkletNode: false, + AudioWorkletProcessor: false, AuthenticatorAssertionResponse: false, AuthenticatorAttestationResponse: false, AuthenticatorResponse: false, @@ -162,6 +164,8 @@ export default { CSSUnitValue: false, CSSUnparsedValue: false, CSSVariableReferenceValue: false, + currentFrame: false, + currentTime: false, CustomElementRegistry: false, customElements: false, CustomEvent: false, @@ -729,6 +733,7 @@ export default { ReadableStreamBYOBRequest: false, ReadableStreamDefaultController: false, ReadableStreamDefaultReader: false, + registerProcessor: false, RelativeOrientationSensor: false, RemotePlayback: false, removeEventListener: false, @@ -766,6 +771,7 @@ export default { RTCSessionDescription: false, RTCStatsReport: false, RTCTrackEvent: false, + sampleRate: false, scheduler: false, Scheduler: false, Scheduling: false, @@ -1037,6 +1043,7 @@ export default { WindowControlsOverlayGeometryChangeEvent: false, Worker: false, Worklet: false, + WorkletGlobalScope: false, WritableStream: false, WritableStreamDefaultController: false, WritableStreamDefaultWriter: false, diff --git a/globals.json b/globals.json index c924228..79213e3 100644 --- a/globals.json +++ b/globals.json @@ -355,7 +355,9 @@ "AudioScheduledSourceNode": false, "AudioSinkInfo": false, "AudioWorklet": false, + "AudioWorkletGlobalScope": false, "AudioWorkletNode": false, + "AudioWorkletProcessor": false, "AuthenticatorAssertionResponse": false, "AuthenticatorAttestationResponse": false, "AuthenticatorResponse": false, @@ -484,6 +486,8 @@ "CSSUnitValue": false, "CSSUnparsedValue": false, "CSSVariableReferenceValue": false, + "currentFrame": false, + "currentTime": false, "CustomElementRegistry": false, "customElements": false, "CustomEvent": false, @@ -1051,6 +1055,7 @@ "ReadableStreamBYOBRequest": false, "ReadableStreamDefaultController": false, "ReadableStreamDefaultReader": false, + "registerProcessor": false, "RelativeOrientationSensor": false, "RemotePlayback": false, "removeEventListener": false, @@ -1088,6 +1093,7 @@ "RTCSessionDescription": false, "RTCStatsReport": false, "RTCTrackEvent": false, + "sampleRate": false, "scheduler": false, "Scheduler": false, "Scheduling": false, @@ -1359,6 +1365,7 @@ "WindowControlsOverlayGeometryChangeEvent": false, "Worker": false, "Worklet": false, + "WorkletGlobalScope": false, "WritableStream": false, "WritableStreamDefaultController": false, "WritableStreamDefaultWriter": false, diff --git a/scripts/get-browser-globals.mjs b/scripts/get-browser-globals.mjs index d9ca47e..28105dc 100644 --- a/scripts/get-browser-globals.mjs +++ b/scripts/get-browser-globals.mjs @@ -3,6 +3,7 @@ import http from 'node:http'; import assert from 'node:assert/strict'; import puppeteer from 'puppeteer'; import getPort from 'get-port'; +import {outdent} from 'outdent'; import {getGlobalThisProperties, createGlobals} from './utilities.mjs'; const ignoredGlobals = new Set([ @@ -134,7 +135,11 @@ async function navigateToSecureContext(page, responses) { }; } -async function runInBrowser(function_, {product, secureContext = false} = {}) { +async function runInBrowser(function_, { + product, + secureContext = false, + arguments: arguments_ = [], +} = {}) { await downloadBrowser({product}); const browser = await puppeteer.launch({product}); @@ -150,13 +155,44 @@ async function runInBrowser(function_, {product, secureContext = false} = {}) { ); } - return await page.evaluate(function_); + return await page.evaluate(function_, arguments_); } finally { await browser.close(); await server?.close(); } } +async function runInAudioWorklet(function_) { + const workletCode = outdent` + registerProcessor('execute-processor', class extends AudioWorkletProcessor { + constructor() { + super(); + + this.port.postMessage(${function_}()); + } + process() { + return true; + } + }); + `; + + return runInBrowser(async workletCode => { + const context = new AudioContext(); + const workletUrl = URL.createObjectURL(new Blob([workletCode], {type: 'application/javascript'})); + await context.audioWorklet.addModule(workletUrl); + URL.revokeObjectURL(workletUrl); + return new Promise(resolve => { + const node = new AudioWorkletNode(context, 'execute-processor'); + node.port.onmessage = ({data}) => { + resolve(data); + }; + }); + }, { + secureContext: true, + arguments: [workletCode], + }); +} + async function runInWebWorker(function_) { await downloadBrowser(); @@ -196,11 +232,13 @@ async function runInWebWorker(function_) { async function getBrowserGlobals() { const chromeGlobals = await runInBrowser(getGlobalThisProperties, {secureContext: true}); const firefoxGlobals = await runInBrowser(getGlobalThisProperties, {product: 'firefox', secureContext: true}); + const audioWorkletGlobals = await runInAudioWorklet(getGlobalThisProperties); return createGlobals( [ ...chromeGlobals, ...firefoxGlobals, + ...audioWorkletGlobals, ], { shouldExclude, From b326e4b31f37cd7291b8a556c46c6e442fd239f7 Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 9 May 2024 10:30:36 +0800 Subject: [PATCH 2/3] Linting --- scripts/get-browser-globals.mjs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/get-browser-globals.mjs b/scripts/get-browser-globals.mjs index 28105dc..5b76a18 100644 --- a/scripts/get-browser-globals.mjs +++ b/scripts/get-browser-globals.mjs @@ -177,11 +177,13 @@ async function runInAudioWorklet(function_) { `; return runInBrowser(async workletCode => { + // eslint-disable-next-line no-undef -- execute in browser const context = new AudioContext(); const workletUrl = URL.createObjectURL(new Blob([workletCode], {type: 'application/javascript'})); await context.audioWorklet.addModule(workletUrl); URL.revokeObjectURL(workletUrl); return new Promise(resolve => { + // eslint-disable-next-line no-undef -- execute in browser const node = new AudioWorkletNode(context, 'execute-processor'); node.port.onmessage = ({data}) => { resolve(data); From 377b98faf87a170d77b5773c622d12fba0006723 Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 9 May 2024 10:37:52 +0800 Subject: [PATCH 3/3] Linting --- scripts/get-browser-globals.mjs | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/get-browser-globals.mjs b/scripts/get-browser-globals.mjs index 5b76a18..c82f7e6 100644 --- a/scripts/get-browser-globals.mjs +++ b/scripts/get-browser-globals.mjs @@ -185,6 +185,7 @@ async function runInAudioWorklet(function_) { return new Promise(resolve => { // eslint-disable-next-line no-undef -- execute in browser const node = new AudioWorkletNode(context, 'execute-processor'); + // eslint-disable-next-line unicorn/prefer-add-event-listener -- not working node.port.onmessage = ({data}) => { resolve(data); };