@@ -408,70 +408,69 @@ void cbm_pipeline_emit_def_route(cbm_gbuf_t *gb, int64_t func_id, const CBMDefin
408408 snprintf (route_qn , sizeof (route_qn ), "__route__%s__%s" , rm ,
409409 cbm_route_canon_path (def -> route_path , cpath , sizeof (cpath )));
410410 char rprops [CBM_SZ_256 ];
411- snprintf (rprops , sizeof (rprops ), "{\"method\":\"%s\",\"source\":\"decorator\"}" , rm );
411+ char esc_m [CBM_SZ_128 ];
412+ cbm_json_escape (esc_m , sizeof (esc_m ), rm );
413+ (void )snprintf (rprops , sizeof (rprops ), "{\"method\":\"%s\",\"source\":\"decorator\"}" , esc_m );
412414 int64_t route_id = cbm_gbuf_upsert_node (gb , "Route" , def -> route_path , route_qn ,
413415 def -> file_path ? def -> file_path : rel , 0 , 0 , rprops );
414- char hprops [CBM_SZ_512 ];
416+ char hprops [CBM_SZ_1K ]; /* must exceed escaped value + wrapper or snprintf cuts the
417+ closing brace */
415418 char esc_h [CBM_SZ_512 ];
416419 cbm_json_escape (esc_h , sizeof (esc_h ), def -> qualified_name );
417- snprintf (hprops , sizeof (hprops ), "{\"handler\":\"%s\"}" , esc_h );
420+ ( void ) snprintf (hprops , sizeof (hprops ), "{\"handler\":\"%s\"}" , esc_h );
418421 cbm_gbuf_insert_edge (gb , func_id , route_id , "HANDLES" , hprops );
419422}
420423
421424/* Phase 2a: Ensure all functions with route_path properties have Route+HANDLES edges.
422425 * During incremental indexing, only changed files get Route nodes from extraction.
423426 * This pass scans ALL Function/Method nodes and creates missing Route+HANDLES. */
424- /* Extract a JSON string property value into buf. Returns true if found. */
425- static bool extract_json_prop (const char * json , const char * key , char * buf , int bufsz ) {
426- if (!json ) {
427+ /* Extract the string property `key` of a parsed properties object into buf,
428+ * with its escapes decoded. Returns true if found: a non-empty string with no
429+ * NUL that fits buf. Nothing is copied otherwise. */
430+ static bool extract_json_prop (yyjson_val * root , const char * key , char * buf , size_t bufsz ) {
431+ yyjson_val * val = yyjson_obj_get (root , key );
432+ const char * str = yyjson_get_str (val );
433+ size_t len = yyjson_get_len (val );
434+ if (!str || len == 0 || len >= bufsz || memchr (str , '\0' , len )) {
427435 return false;
428436 }
429- char pattern [CBM_SZ_64 ];
430- snprintf (pattern , sizeof (pattern ), "\"%s\":\"" , key );
431- const char * p = strstr (json , pattern );
432- if (!p ) {
433- return false;
434- }
435- p += strlen (pattern );
436- const char * end = strchr (p , '"' );
437- if (!end || end <= p ) {
438- return false;
439- }
440- int len = (int )(end - p );
441- if (len >= bufsz ) {
442- return false;
443- }
444- memcpy (buf , p , (size_t )len );
445- buf [len ] = '\0' ;
437+ memcpy (buf , str , len + SKIP_ONE );
446438 return true;
447439}
448440
449441/* Process a single Function/Method node: create Route+HANDLES if it has route_path.
450442 * Returns 1 if a new HANDLES edge was created, 0 otherwise. */
451443static int ensure_one_decorator_route (cbm_gbuf_t * gb , const cbm_gbuf_node_t * func ) {
452- if (!func -> properties_json ) {
444+ /* Most nodes carry no route: skip them before parsing their properties. */
445+ if (!func -> properties_json || !strstr (func -> properties_json , "\"route_path\"" )) {
453446 return 0 ;
454447 }
455448
449+ /* Properties hold source text as its bytes, which need not be UTF-8. */
450+ yyjson_doc * doc = yyjson_read (func -> properties_json , strlen (func -> properties_json ),
451+ YYJSON_READ_ALLOW_INVALID_UNICODE );
452+ yyjson_val * root = yyjson_doc_get_root (doc );
456453 char path [CBM_SZ_256 ];
457- if (!extract_json_prop (func -> properties_json , "route_path" , path , sizeof (path ))) {
458- return 0 ;
454+ char method [CBM_SZ_16 ] = "ANY" ;
455+ bool routed = extract_json_prop (root , "route_path" , path , sizeof (path )) && path [0 ] == '/' ;
456+ if (routed ) {
457+ extract_json_prop (root , "route_method" , method , sizeof (method ));
459458 }
460- if (path [0 ] != '/' ) {
459+ yyjson_doc_free (doc );
460+ if (!routed ) {
461461 return 0 ;
462462 }
463463
464- char method [CBM_SZ_16 ] = "ANY" ;
465- extract_json_prop (func -> properties_json , "route_method" , method , sizeof (method ));
466-
467464 char route_qn [CBM_ROUTE_QN_SIZE ];
468465 char cpath [CBM_SZ_256 ];
469466 snprintf (route_qn , sizeof (route_qn ), "__route__%s__%s" , method ,
470467 cbm_route_canon_path (path , cpath , sizeof (cpath )));
471468 const cbm_gbuf_node_t * existing = cbm_gbuf_find_by_qn (gb , route_qn );
472469
473470 char rprops [CBM_SZ_256 ];
474- snprintf (rprops , sizeof (rprops ), "{\"method\":\"%s\",\"source\":\"decorator\"}" , method );
471+ char esc_m [CBM_SZ_128 ];
472+ cbm_json_escape (esc_m , sizeof (esc_m ), method );
473+ (void )snprintf (rprops , sizeof (rprops ), "{\"method\":\"%s\",\"source\":\"decorator\"}" , esc_m );
475474 int64_t route_id = cbm_gbuf_upsert_node (gb , "Route" , path , route_qn ,
476475 func -> file_path ? func -> file_path : "" , 0 , 0 , rprops );
477476
@@ -486,9 +485,12 @@ static int ensure_one_decorator_route(cbm_gbuf_t *gb, const cbm_gbuf_node_t *fun
486485 }
487486 }
488487
489- char hprops [CBM_SZ_512 ];
490- snprintf (hprops , sizeof (hprops ), "{\"handler\":\"%s\"}" ,
491- func -> qualified_name ? func -> qualified_name : "" );
488+ /* The same properties cbm_pipeline_emit_def_route gives the edge. */
489+ char hprops [CBM_SZ_1K ]; /* must exceed escaped value + wrapper or snprintf cuts the
490+ closing brace */
491+ char esc_h [CBM_SZ_512 ];
492+ cbm_json_escape (esc_h , sizeof (esc_h ), func -> qualified_name ? func -> qualified_name : "" );
493+ (void )snprintf (hprops , sizeof (hprops ), "{\"handler\":\"%s\"}" , esc_h );
492494 cbm_gbuf_insert_edge (gb , func -> id , route_id , "HANDLES" , hprops );
493495 return SKIP_ONE ;
494496}
@@ -1180,6 +1182,17 @@ typedef struct {
11801182 int files_seen ;
11811183} sveltekit_ctx_t ;
11821184
1185+ /* The properties of a SvelteKit handler's HANDLES edge: its QN, escaped as
1186+ * every HANDLES edge's handler is. */
1187+ static void sveltekit_handles_props (char * out , size_t outsz , const cbm_gbuf_node_t * child ,
1188+ bool is_actions ) {
1189+ char esc_h [CBM_SZ_512 ];
1190+ cbm_json_escape (esc_h , sizeof (esc_h ),
1191+ child -> qualified_name ? child -> qualified_name : child -> name );
1192+ (void )snprintf (out , outsz , "{\"handler\":\"%s\",\"framework\":\"sveltekit\"%s}" , esc_h ,
1193+ is_actions ? ",\"via\":\"actions_object\"" : "" );
1194+ }
1195+
11831196/* Process one File node: if it matches a SvelteKit server-side file,
11841197 * synthesise a Route node and HANDLES edges from any handler functions
11851198 * (or actions Variable) defined in the file. */
@@ -1256,10 +1269,9 @@ static void sveltekit_file_visitor(const cbm_gbuf_node_t *node, void *userdata)
12561269 }
12571270 ctx -> routes_created ++ ;
12581271
1259- char hprops [CBM_SZ_256 ];
1260- snprintf (hprops , sizeof (hprops ), "{\"handler\":\"%s\",\"framework\":\"sveltekit\"%s}" ,
1261- child -> qualified_name ? child -> qualified_name : child -> name ,
1262- is_actions ? ",\"via\":\"actions_object\"" : "" );
1272+ char hprops [CBM_SZ_1K ]; /* must exceed escaped value + wrapper or snprintf cuts the
1273+ closing brace */
1274+ sveltekit_handles_props (hprops , sizeof (hprops ), child , is_actions );
12631275 cbm_gbuf_insert_edge (ctx -> gb , child -> id , route_id , "HANDLES" , hprops );
12641276 ctx -> handles_created ++ ;
12651277 }
0 commit comments