This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch scanner-improvements
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 06e22fbe4868309c6480d4a44df79b73f0938df0
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Thu Feb 6 12:49:11 2025 -0500

    QuickJS scanner improvements
    
      * In the doc updater, handler mock request body should be string not an
      object [1].
    
      * Expect search indexes to be broken so there may not be any valid fields
      returned. In that case don't crash the scanner with a `{badmatch, []}`
    
    [1] https://docs.couchdb.org/en/stable/json-structure.html#request-object
---
 src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl     | 14 +++++++++-----
 .../test/couch_quickjs_scanner_plugin_tests.erl            |  2 ++
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl 
b/src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl
index 88af9e489..0d7b233de 100644
--- a/src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl
+++ b/src/couch_quickjs/src/couch_quickjs_scanner_plugin.erl
@@ -75,7 +75,7 @@
         <<"sizes">> => #{<<"active">> => 42, <<"disk">> => 42, <<"external">> 
=> 42}
     },
     <<"query">> => #{},
-    <<"body">> => #{},
+    <<"body">> => <<"{}">>,
     <<"method">> => <<"POST">>,
     <<"headers">> => #{},
     <<"form">> => #{},
@@ -814,12 +814,16 @@ nouveau_add_fun(#proc{}, _) ->
     ok.
 
 clouseau_index_doc(#proc{} = Proc, {[_ | _]} = Doc) ->
-    [Fields | _] = prompt(Proc, [<<"index_doc">>, Doc]),
-    lists:sort(Fields).
+    case prompt(Proc, [<<"index_doc">>, Doc]) of
+        [Fields | _] -> lists:sort(Fields);
+        [] -> []
+    end.
 
 nouveau_index_doc(#proc{} = Proc, {[_ | _]} = Doc) ->
-    [Fields | _] = prompt(Proc, [<<"nouveau_index_doc">>, Doc]),
-    lists:sort(Fields).
+    case prompt(Proc, [<<"nouveau_index_doc">>, Doc]) of
+        [Fields | _] -> lists:sort(Fields);
+        [] -> []
+    end.
 
 filter_doc(#proc{} = Proc, DDocId, FName, {[_ | _]} = Doc) ->
     % Add a mock request object so param access doesn't throw a TypeError
diff --git a/src/couch_quickjs/test/couch_quickjs_scanner_plugin_tests.erl 
b/src/couch_quickjs/test/couch_quickjs_scanner_plugin_tests.erl
index 711807c67..df7a1906d 100644
--- a/src/couch_quickjs/test/couch_quickjs_scanner_plugin_tests.erl
+++ b/src/couch_quickjs/test/couch_quickjs_scanner_plugin_tests.erl
@@ -692,6 +692,7 @@ ddoc_update(Doc) ->
         updates => #{
             u1 => <<
                 "function(doc, req) {\n"
+                "  body = JSON.parse(req.body) \n"
                 "  doc.a.search(/(x+)/); \n"
                 "  if (RegExp.$1 === undefined) {\n"
                 "    return [null, 'no_dollar_one']; \n"
@@ -702,6 +703,7 @@ ddoc_update(Doc) ->
             >>,
             u2 => <<
                 "function(doc, req) {\n"
+                "  body = JSON.parse(req.body) \n"
                 "  if (typeof(req.form) === 'object') {\n"
                 "    return [null, 'has_form']; \n"
                 "  } else { \n"

Reply via email to