@@ -43,23 +43,43 @@ pub fn do(
4343 s : []const u8 ,
4444 ctx : * Ctx ,
4545) ! []const u8 {
46+
47+ // JSON scanner
4648 var scanner = std .json .Scanner .initCompleteInput (alloc , s );
4749 defer scanner .deinit ();
4850
4951 std .debug .assert (try scanner .next () == .object_begin );
5052
51- try checkKey ("id" , (try scanner .next ()).string );
52- const id = try std .fmt .parseUnsigned (u64 , (try scanner .next ()).number , 10 );
53-
54- try checkKey ("method" , (try scanner .next ()).string );
55- const method_name = (try scanner .next ()).string ;
53+ // handle 2 possible orders:
54+ // - id, method <...>
55+ // - method, id <...>
56+ var id_key = (try scanner .next ()).string ;
57+ var id_token = try scanner .next ();
58+ var method_key = (try scanner .next ()).string ;
59+ var method_token = try scanner .next ();
60+ // check swap order
61+ if (! std .mem .eql (u8 , id_key , "id" )) {
62+ const swap_key = method_key ;
63+ const swap_token = method_token ;
64+ method_key = id_key ;
65+ method_token = id_token ;
66+ id_key = swap_key ;
67+ id_token = swap_token ;
68+ }
69+ try checkKey (id_key , "id" );
70+ try checkKey (method_key , "method" );
5671
72+ // retrieve id and method
73+ const id = try std .fmt .parseUnsigned (u64 , id_token .number , 10 );
74+ const method_name = method_token .string ;
5775 std .log .debug ("cmd: id {any}, method {s}" , .{ id , method_name });
5876
77+ // retrieve domain from method
5978 var iter = std .mem .splitScalar (u8 , method_name , '.' );
6079 const domain = std .meta .stringToEnum (Domains , iter .first ()) orelse
6180 return error .UnknonwDomain ;
6281
82+ // select corresponding domain
6383 return switch (domain ) {
6484 .Browser = > browser (alloc , id , iter .next ().? , & scanner , ctx ),
6585 .Target = > target (alloc , id , iter .next ().? , & scanner , ctx ),
0 commit comments