@@ -24,16 +24,10 @@ const audioProbePageScriptFunctions = [
2424
2525export function buildAudioProbeEvalScript ( options : WebAudioProbeOptions ) : string {
2626 const scriptBody = audioProbePageScriptFunctions . map ( ( fn ) => `${ fn . toString ( ) } ;` ) . join ( '' ) ;
27- const optionsJsonLiteral = escapeUnsafeChars (
28- JSON . stringify (
29- JSON . stringify ( {
30- action : options . action ,
31- durationMs : finiteNumberOrUndefined ( options . durationMs ) ,
32- bucketMs : finiteNumberOrUndefined ( options . bucketMs ) ,
33- } ) ,
34- ) ,
35- ) ;
36- return `(()=>{${ scriptBody } return ${ audioProbeEvalScript . name } (JSON.parse(${ optionsJsonLiteral } ))})()` ;
27+ const action = readAudioProbeEvalAction ( options . action ) ;
28+ const durationMs = finiteNumberLiteralOrUndefined ( options . durationMs ) ;
29+ const bucketMs = finiteNumberLiteralOrUndefined ( options . bucketMs ) ;
30+ return `(()=>{${ scriptBody } return ${ audioProbeEvalScript . name } ({action:${ JSON . stringify ( action ) } ,durationMs:${ durationMs } ,bucketMs:${ bucketMs } })})()` ;
3731}
3832
3933export function normalizeAgentBrowserAudioProbeResult ( data : unknown ) : WebAudioProbeResult {
@@ -64,29 +58,21 @@ type AudioProbePageStats = { rms: number; peak: number };
6458declare const window : AudioProbePageRecord ;
6559declare const document : { querySelectorAll ( selector : string ) : any [ ] } ;
6660
67- const unsafeCodeStringCharacters : Record < string , string > = {
68- '<' : '\\u003C' ,
69- '>' : '\\u003E' ,
70- '\b' : '\\b' ,
71- '\f' : '\\f' ,
72- '\n' : '\\n' ,
73- '\r' : '\\r' ,
74- '\t' : '\\t' ,
75- '\u0000' : '\\0' ,
76- '\u2028' : '\\u2028' ,
77- '\u2029' : '\\u2029' ,
78- } ;
79-
80- function escapeUnsafeChars ( value : string ) : string {
81- return value . replace (
82- // eslint-disable-next-line no-control-regex -- CodeQL js/bad-code-sanitization recommends this sanitizer shape for generated code strings.
83- / [ < > \b \f \n \r \t \0 \u2028 \u2029 ] / g,
84- ( character ) => unsafeCodeStringCharacters [ character ] ?? character ,
85- ) ;
61+ function readAudioProbeEvalAction (
62+ action : WebAudioProbeOptions [ 'action' ] ,
63+ ) : WebAudioProbeOptions [ 'action' ] {
64+ switch ( action ) {
65+ case 'start' :
66+ return 'start' ;
67+ case 'stop' :
68+ return 'stop' ;
69+ default :
70+ return 'status' ;
71+ }
8672}
8773
88- function finiteNumberOrUndefined ( value : number | undefined ) : number | undefined {
89- return Number . isFinite ( value ) ? value : undefined ;
74+ function finiteNumberLiteralOrUndefined ( value : number | undefined ) : string {
75+ return value === undefined || ! Number . isFinite ( value ) ? 'undefined' : String ( Math . trunc ( value ) ) ;
9076}
9177
9278function audioProbeDbfs ( value : number ) : number {
0 commit comments