-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrtc-helpers.php
More file actions
102 lines (92 loc) · 2.94 KB
/
rtc-helpers.php
File metadata and controls
102 lines (92 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* CLI helpers for rtc-test.sh — no WordPress required.
*
* Usage:
* php rtc-helpers.php capture-sanitize <fixture.json>
* php rtc-helpers.php replay-extract <fixture.json>
*/
$command = $argv[1] ?? '';
$file = $argv[2] ?? '';
// Use realpath() + is_file() rather than file_exists() — the latter resolves
// PHP stream wrappers (php://filter, phar://) which would bypass this guard.
$real = '' !== $file ? realpath( $file ) : false;
if ( false === $real || ! is_file( $real ) ) {
fwrite( STDERR, "Usage: php rtc-helpers.php <capture-sanitize|replay-extract> <fixture.json>\n" );
exit( 1 );
}
$file = $real;
switch ( $command ) {
case 'capture-sanitize':
rtctest_capture_sanitize( $file );
break;
case 'replay-extract':
rtctest_replay_extract( $file );
break;
default:
fwrite( STDERR, "Unknown command: {$command}\n" );
exit( 1 );
}
// -----------------------------------------------------------------------------
function rtctest_capture_sanitize( string $file ) {
$data = json_decode( file_get_contents( $file ), true );
if ( null === $data ) {
fwrite( STDERR, 'capture-sanitize: ' . json_last_error_msg() . "\n" );
exit( 1 );
}
$frames_in = $data['frames'] ?? [];
$frames_out = [];
foreach ( $frames_in as $frame ) {
$rooms = $frame['request']['rooms'] ?? [];
$post_room = null;
foreach ( $rooms as $r ) {
if ( strpos( $r['room'] ?? '', 'postType/post:' ) === 0 ) {
$post_room = $r;
break;
}
}
if ( null === $post_room ) {
continue;
}
$frames_out[] = [
'n' => $frame['n'] ?? count( $frames_out ) + 1,
'elapsed_ms' => $frame['elapsed_ms'] ?? 0,
'client_id' => $frame['client_id'] ?? 0,
'request' => [ 'rooms' => [ [
'room' => 'postType/post:0',
'client_id' => $post_room['client_id'] ?? ( $frame['client_id'] ?? 0 ),
'awareness' => new stdClass(),
'after' => 0,
'updates' => $post_room['updates'] ?? [],
] ] ],
];
}
$out = [
'session_id' => $data['session_id'] ?? '',
'frame_count' => count( $frames_out ),
'frames' => $frames_out,
];
echo json_encode( $out, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) . "\n";
}
function rtctest_replay_extract( string $file ) {
$data = json_decode( file_get_contents( $file ), true );
if ( null === $data ) {
fwrite( STDERR, 'replay-extract: ' . json_last_error_msg() . "\n" );
exit( 1 );
}
foreach ( $data['frames'] ?? [] as $frame ) {
$elapsed_ms = (int) ( $frame['elapsed_ms'] ?? 0 );
$client_id = (int) ( $frame['client_id'] ?? 0 );
$rooms = $frame['request']['rooms'] ?? [];
foreach ( $rooms as $r ) {
if ( strpos( $r['room'] ?? '', 'postType/post:' ) === 0 ) {
$updates = $r['updates'] ?? [];
$updates_str = implode( ',', array_map( function ( $u ) {
return json_encode( $u, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
}, $updates ) );
echo $elapsed_ms . "\t" . $client_id . "\t" . $updates_str . "\n";
break;
}
}
}
}