websocket_logger.js (1694B)
1 /** 2 * AUTO-GENERATED - DO NOT EDIT. Source: https://github.com/gpuweb/cts 3 **/import { globalTestConfig } from '../framework/test_config.js'; /** 4 * - 'uninitialized' means we haven't tried to connect yet 5 * - Promise means it's pending 6 * - 'failed' means it failed (this is the most common case, where the logger isn't running) 7 * - WebSocket means it succeeded 8 */ 9 let connection = 10 'uninitialized'; 11 12 /** 13 * If the logToWebSocket option is enabled (?log_to_web_socket=1 in browser, 14 * --log-to-web-socket on command line, or enable it by default in options.ts), 15 * log a string to a websocket at `localhost:59497`. See `tools/websocket-logger`. 16 * 17 * This does nothing if a logToWebSocket is not enabled, or if a connection 18 * couldn't be established on the first call. 19 */ 20 export function logToWebSocket(msg) { 21 if (!globalTestConfig.logToWebSocket || connection === 'failed') { 22 return; 23 } 24 25 if (connection === 'uninitialized') { 26 connection = new Promise((resolve) => { 27 if (typeof WebSocket === 'undefined') { 28 resolve('failed'); 29 return; 30 } 31 32 const ws = new WebSocket('ws://localhost:59497/optional_cts_websocket_logger'); 33 ws.onopen = () => { 34 resolve(ws); 35 }; 36 ws.onerror = () => { 37 connection = 'failed'; 38 resolve('failed'); 39 }; 40 ws.onclose = () => { 41 connection = 'failed'; 42 resolve('failed'); 43 }; 44 }); 45 void connection.then((resolved) => { 46 connection = resolved; 47 }); 48 } 49 50 void (async () => { 51 // connection may be a promise or a value here. Either is OK to await. 52 const ws = await connection; 53 if (ws !== 'failed') { 54 ws.send(msg); 55 } 56 })(); 57 }