Skip to content

Commit

Permalink
Add AudioWorklet globals (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored May 9, 2024
1 parent f22d19f commit 7d163e3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
7 changes: 7 additions & 0 deletions data/browser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export default {
AudioScheduledSourceNode: false,
AudioSinkInfo: false,
AudioWorklet: false,
AudioWorkletGlobalScope: false,
AudioWorkletNode: false,
AudioWorkletProcessor: false,
AuthenticatorAssertionResponse: false,
AuthenticatorAttestationResponse: false,
AuthenticatorResponse: false,
Expand Down Expand Up @@ -162,6 +164,8 @@ export default {
CSSUnitValue: false,
CSSUnparsedValue: false,
CSSVariableReferenceValue: false,
currentFrame: false,
currentTime: false,
CustomElementRegistry: false,
customElements: false,
CustomEvent: false,
Expand Down Expand Up @@ -729,6 +733,7 @@ export default {
ReadableStreamBYOBRequest: false,
ReadableStreamDefaultController: false,
ReadableStreamDefaultReader: false,
registerProcessor: false,
RelativeOrientationSensor: false,
RemotePlayback: false,
removeEventListener: false,
Expand Down Expand Up @@ -766,6 +771,7 @@ export default {
RTCSessionDescription: false,
RTCStatsReport: false,
RTCTrackEvent: false,
sampleRate: false,
scheduler: false,
Scheduler: false,
Scheduling: false,
Expand Down Expand Up @@ -1037,6 +1043,7 @@ export default {
WindowControlsOverlayGeometryChangeEvent: false,
Worker: false,
Worklet: false,
WorkletGlobalScope: false,
WritableStream: false,
WritableStreamDefaultController: false,
WritableStreamDefaultWriter: false,
Expand Down
7 changes: 7 additions & 0 deletions globals.json
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@
"AudioScheduledSourceNode": false,
"AudioSinkInfo": false,
"AudioWorklet": false,
"AudioWorkletGlobalScope": false,
"AudioWorkletNode": false,
"AudioWorkletProcessor": false,
"AuthenticatorAssertionResponse": false,
"AuthenticatorAttestationResponse": false,
"AuthenticatorResponse": false,
Expand Down Expand Up @@ -484,6 +486,8 @@
"CSSUnitValue": false,
"CSSUnparsedValue": false,
"CSSVariableReferenceValue": false,
"currentFrame": false,
"currentTime": false,
"CustomElementRegistry": false,
"customElements": false,
"CustomEvent": false,
Expand Down Expand Up @@ -1051,6 +1055,7 @@
"ReadableStreamBYOBRequest": false,
"ReadableStreamDefaultController": false,
"ReadableStreamDefaultReader": false,
"registerProcessor": false,
"RelativeOrientationSensor": false,
"RemotePlayback": false,
"removeEventListener": false,
Expand Down Expand Up @@ -1088,6 +1093,7 @@
"RTCSessionDescription": false,
"RTCStatsReport": false,
"RTCTrackEvent": false,
"sampleRate": false,
"scheduler": false,
"Scheduler": false,
"Scheduling": false,
Expand Down Expand Up @@ -1359,6 +1365,7 @@
"WindowControlsOverlayGeometryChangeEvent": false,
"Worker": false,
"Worklet": false,
"WorkletGlobalScope": false,
"WritableStream": false,
"WritableStreamDefaultController": false,
"WritableStreamDefaultWriter": false,
Expand Down
45 changes: 43 additions & 2 deletions scripts/get-browser-globals.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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});
Expand All @@ -150,13 +155,47 @@ 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 => {
// 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');
// eslint-disable-next-line unicorn/prefer-add-event-listener -- not working
node.port.onmessage = ({data}) => {
resolve(data);
};
});
}, {
secureContext: true,
arguments: [workletCode],
});
}

async function runInWebWorker(function_) {
await downloadBrowser();

Expand Down Expand Up @@ -196,11 +235,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,
Expand Down

0 comments on commit 7d163e3

Please sign in to comment.