Skip to content

Commit 7d163e3

Browse files
authoredMay 9, 2024··
Add AudioWorklet globals (#249)
1 parent f22d19f commit 7d163e3

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed
 

‎data/browser.mjs

+7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ export default {
3333
AudioScheduledSourceNode: false,
3434
AudioSinkInfo: false,
3535
AudioWorklet: false,
36+
AudioWorkletGlobalScope: false,
3637
AudioWorkletNode: false,
38+
AudioWorkletProcessor: false,
3739
AuthenticatorAssertionResponse: false,
3840
AuthenticatorAttestationResponse: false,
3941
AuthenticatorResponse: false,
@@ -162,6 +164,8 @@ export default {
162164
CSSUnitValue: false,
163165
CSSUnparsedValue: false,
164166
CSSVariableReferenceValue: false,
167+
currentFrame: false,
168+
currentTime: false,
165169
CustomElementRegistry: false,
166170
customElements: false,
167171
CustomEvent: false,
@@ -729,6 +733,7 @@ export default {
729733
ReadableStreamBYOBRequest: false,
730734
ReadableStreamDefaultController: false,
731735
ReadableStreamDefaultReader: false,
736+
registerProcessor: false,
732737
RelativeOrientationSensor: false,
733738
RemotePlayback: false,
734739
removeEventListener: false,
@@ -766,6 +771,7 @@ export default {
766771
RTCSessionDescription: false,
767772
RTCStatsReport: false,
768773
RTCTrackEvent: false,
774+
sampleRate: false,
769775
scheduler: false,
770776
Scheduler: false,
771777
Scheduling: false,
@@ -1037,6 +1043,7 @@ export default {
10371043
WindowControlsOverlayGeometryChangeEvent: false,
10381044
Worker: false,
10391045
Worklet: false,
1046+
WorkletGlobalScope: false,
10401047
WritableStream: false,
10411048
WritableStreamDefaultController: false,
10421049
WritableStreamDefaultWriter: false,

‎globals.json

+7
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@
355355
"AudioScheduledSourceNode": false,
356356
"AudioSinkInfo": false,
357357
"AudioWorklet": false,
358+
"AudioWorkletGlobalScope": false,
358359
"AudioWorkletNode": false,
360+
"AudioWorkletProcessor": false,
359361
"AuthenticatorAssertionResponse": false,
360362
"AuthenticatorAttestationResponse": false,
361363
"AuthenticatorResponse": false,
@@ -484,6 +486,8 @@
484486
"CSSUnitValue": false,
485487
"CSSUnparsedValue": false,
486488
"CSSVariableReferenceValue": false,
489+
"currentFrame": false,
490+
"currentTime": false,
487491
"CustomElementRegistry": false,
488492
"customElements": false,
489493
"CustomEvent": false,
@@ -1051,6 +1055,7 @@
10511055
"ReadableStreamBYOBRequest": false,
10521056
"ReadableStreamDefaultController": false,
10531057
"ReadableStreamDefaultReader": false,
1058+
"registerProcessor": false,
10541059
"RelativeOrientationSensor": false,
10551060
"RemotePlayback": false,
10561061
"removeEventListener": false,
@@ -1088,6 +1093,7 @@
10881093
"RTCSessionDescription": false,
10891094
"RTCStatsReport": false,
10901095
"RTCTrackEvent": false,
1096+
"sampleRate": false,
10911097
"scheduler": false,
10921098
"Scheduler": false,
10931099
"Scheduling": false,
@@ -1359,6 +1365,7 @@
13591365
"WindowControlsOverlayGeometryChangeEvent": false,
13601366
"Worker": false,
13611367
"Worklet": false,
1368+
"WorkletGlobalScope": false,
13621369
"WritableStream": false,
13631370
"WritableStreamDefaultController": false,
13641371
"WritableStreamDefaultWriter": false,

‎scripts/get-browser-globals.mjs

+43-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import http from 'node:http';
33
import assert from 'node:assert/strict';
44
import puppeteer from 'puppeteer';
55
import getPort from 'get-port';
6+
import {outdent} from 'outdent';
67
import {getGlobalThisProperties, createGlobals} from './utilities.mjs';
78

89
const ignoredGlobals = new Set([
@@ -134,7 +135,11 @@ async function navigateToSecureContext(page, responses) {
134135
};
135136
}
136137

137-
async function runInBrowser(function_, {product, secureContext = false} = {}) {
138+
async function runInBrowser(function_, {
139+
product,
140+
secureContext = false,
141+
arguments: arguments_ = [],
142+
} = {}) {
138143
await downloadBrowser({product});
139144

140145
const browser = await puppeteer.launch({product});
@@ -150,13 +155,47 @@ async function runInBrowser(function_, {product, secureContext = false} = {}) {
150155
);
151156
}
152157

153-
return await page.evaluate(function_);
158+
return await page.evaluate(function_, arguments_);
154159
} finally {
155160
await browser.close();
156161
await server?.close();
157162
}
158163
}
159164

165+
async function runInAudioWorklet(function_) {
166+
const workletCode = outdent`
167+
registerProcessor('execute-processor', class extends AudioWorkletProcessor {
168+
constructor() {
169+
super();
170+
171+
this.port.postMessage(${function_}());
172+
}
173+
process() {
174+
return true;
175+
}
176+
});
177+
`;
178+
179+
return runInBrowser(async workletCode => {
180+
// eslint-disable-next-line no-undef -- execute in browser
181+
const context = new AudioContext();
182+
const workletUrl = URL.createObjectURL(new Blob([workletCode], {type: 'application/javascript'}));
183+
await context.audioWorklet.addModule(workletUrl);
184+
URL.revokeObjectURL(workletUrl);
185+
return new Promise(resolve => {
186+
// eslint-disable-next-line no-undef -- execute in browser
187+
const node = new AudioWorkletNode(context, 'execute-processor');
188+
// eslint-disable-next-line unicorn/prefer-add-event-listener -- not working
189+
node.port.onmessage = ({data}) => {
190+
resolve(data);
191+
};
192+
});
193+
}, {
194+
secureContext: true,
195+
arguments: [workletCode],
196+
});
197+
}
198+
160199
async function runInWebWorker(function_) {
161200
await downloadBrowser();
162201

@@ -196,11 +235,13 @@ async function runInWebWorker(function_) {
196235
async function getBrowserGlobals() {
197236
const chromeGlobals = await runInBrowser(getGlobalThisProperties, {secureContext: true});
198237
const firefoxGlobals = await runInBrowser(getGlobalThisProperties, {product: 'firefox', secureContext: true});
238+
const audioWorkletGlobals = await runInAudioWorklet(getGlobalThisProperties);
199239

200240
return createGlobals(
201241
[
202242
...chromeGlobals,
203243
...firefoxGlobals,
244+
...audioWorkletGlobals,
204245
],
205246
{
206247
shouldExclude,

0 commit comments

Comments
 (0)
Please sign in to comment.