Uploaded image for project: 'Couchbase Server'
  1. Couchbase Server
  2. MB-49862

[UDF] cannot use // comment style in JS udf

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Not a Bug
    • Neo
    • Neo
    • js-evaluator
    • 7.1.0-1801
    • Untriaged
    • 1
    • Unknown

    Description

      Cannot have // comment in JS

      # cat pi.js 
      function pi() {
        // some comment
        return 3.1415926;
      } 

      # curl -s -k -X POST http://172.23.104.91:8093/evaluator/v1/libraries/use_cases -u Administrator:password -H 'content-type: application/json' -d@pi.js
      Error while compiling library. Cause:
      compilation failed:
      Exception: SyntaxError: Unexpected end of input
      Location: functions/use_cases.js:1 
      Code: function pi() {  // some comment  return 3.1415926;}
      SyntaxError: Unexpected end of input
       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          Sitaram.Vemulapalli Sitaram Vemulapalli added a comment - - edited

          From Donald Haggart

          use "--data-binary" with curl for JS functions and single-line comments:
           
          $ cat -n /tmp/qs.js
               1  function qs()
               2  {
               3    // this is a single line comment
               4    var i = 0;
               5
               6    var query = SELECT ';';
               7    var acc = [];
               8    for (const row of query)
               9    {
              10      row.rownum = i;
              11      acc.push(row);
              12      i++;
              13    }
              14    return acc;
              15  }
           
          $ curl -q -X POST http://localhost:8093/evaluator/v1/libraries/udf2 -u Administrator:password --data-binary @/tmp/qs.js
          {status: "OK"}
           
          $ curl -u Administrator:password -d 'pretty=true&statement=execute function qs()' http://localhost:8093/query/service
          {
              "requestID": "6aa73308-9e54-45a3-bd96-2a9da34fb293",
              "signature": null,
              "results": [
              [
                  {
                      "$1": ";",
                      "rownum": 0
                  }
              ]
              ],
              "status": "success",
              "metrics": {
                  "elapsedTime": "22.265453ms",
                  "executionTime": "22.153302ms",
                  "resultCount": 1,
                  "resultSize": 74,
                  "serviceLoad": 3
              }
          }
          
          

          Also points correct line location of error with "--data-binary"

          $ cat -n /tmp/qs.js
               1  function qs(name)
               2  {
               3    // this is a single line comment
               4    var i = 0;
               5
               6    this is a syntax error on line 6
               7
               8    //var query = SELECT $1; // this is my query
               9    var params = ['a','b','c','d','e']
              10    //var params = {'a':'a','b':'b','c':'c','d':'d','e':'e'}
              11    var query = N1QL('SELECT array_append([],$1,$2,$3,$4,$5);', params, true);
              12    var acc = [];
              13    for (const row of query)
              14    {
              15      row.rownum = i;
              16      acc.push(row);
              17      i++;
              18    }
              19    return acc;
              20  }
           
          $ curl -q -X POST http://localhost:8093/evaluator/v1/libraries/udf3 -u Administrator:password --data-binary @/tmp/qs.js
          Error while compiling library. Cause:
          compilation failed:
          Exception: SyntaxError: Unexpected identifier
          Location: functions/udf3.js:6
          Code:   this is a syntax error on line 6
          SyntaxError: Unexpected identifier
           
          If there is a syntax error in the SQL, then that's a runtime error:
           
          $ cat -n /tmp/qs.js
               1  function qs(name)
               2  {
               3    // this is a single line comment
               4    var i = 0;
               5
               6    //var query = SELECT $1; // this is my query
               7    var params = ['a','b','c','d','e']
               8    //var params = {'a':'a','b':'b','c':'c','d':'d','e':'e'}
               9    var query = N1QL('xSELECT array_append([],$1,$2,$3,$4,$5);', params, true);
              10    var acc = [];
              11    for (const row of query)
              12    {
              13      row.rownum = i;
              14      acc.push(row);
              15      i++;
              16    }
              17    return acc;
              18  }
          $ curl -q -X POST http://localhost:8093/evaluator/v1/libraries/udf3 -u Administrator:password --data-binary @/tmp/qs.js
          {"status": "OK"}
          $ curl -u Administrator:password -d 'pretty=true&statement=execute function qs(1)' http://localhost:8093/query/service
          {
              "requestID": "1e97c8fa-345d-460f-8f59-891e958efe6f",
              "signature": null,
              "results": [
              ],
              "errors": [
                  {
                      "cause": {
                          "details": [
                              "Evaluator error",
                              "Exceptions from JS code : run qs failed:",
                              "Exception: Error: syntax error - line 1, column 9, near 'xSELECT', at: array_append",
                              "Location: functions/udf3.js:9 ",
                              "Code:   var query = N1QL('xSELECT array_append([],$1,$2,$3,$4,$5);', params, true);",
                              "Error at qs (functions/udf3.js:9:15)"
                          ],
                          "name": "qs",
                          "what": "(udf3:qs)"
                      },
                      "code": 10109,
                      "msg": "Error executing function qs (udf3:qs): Evaluator error Exceptions from JS code : run qs failed:\nException: Error: syntax error - line 1, column 9, near 'xSELECT', at: array_append\nLocation: functions/udf3.js:9 \nCode:   var query = N1QL('xSELECT array_append([],$1,$2,$3,$4,$5);', params, true);\nError\n    at qs (functions/udf3.js:9:15)\n"
                  }
              ],
              "status": "errors",
              "metrics": {
                  "elapsedTime": "19.65587ms",
                  "executionTime": "19.509501ms",
                  "resultCount": 0,
                  "resultSize": 0,
                  "serviceLoad": 3,
                  "errorCount": 1
              }
          }
          

          Sitaram.Vemulapalli Sitaram Vemulapalli added a comment - - edited From Donald Haggart use "--data-binary" with curl for JS functions and single-line comments:   $ cat -n /tmp/qs.js 1 function qs() 2 { 3 // this is a single line comment 4 var i = 0 ; 5 6 var query = SELECT ';' ; 7 var acc = []; 8 for ( const row of query) 9 { 10 row.rownum = i; 11 acc.push(row); 12 i++; 13 } 14 return acc; 15 }   $ curl -q -X POST http: //localhost:8093/evaluator/v1/libraries/udf2 -u Administrator:password --data-binary @/tmp/qs.js {status: "OK" }   $ curl -u Administrator:password -d 'pretty=true&statement=execute function qs()' http: //localhost:8093/query/service { "requestID" : "6aa73308-9e54-45a3-bd96-2a9da34fb293" , "signature" : null , "results" : [ [ { "$1" : ";" , "rownum" : 0 } ] ], "status" : "success" , "metrics" : { "elapsedTime" : "22.265453ms" , "executionTime" : "22.153302ms" , "resultCount" : 1 , "resultSize" : 74 , "serviceLoad" : 3 } } Also points correct line location of error with "--data-binary" $ cat -n /tmp/qs.js 1 function qs(name) 2 { 3 // this is a single line comment 4 var i = 0 ; 5 6 this is a syntax error on line 6 7 8 //var query = SELECT $1; // this is my query 9 var params = [ 'a' , 'b' , 'c' , 'd' , 'e' ] 10 //var params = {'a':'a','b':'b','c':'c','d':'d','e':'e'} 11 var query = N1QL( 'SELECT array_append([],$1,$2,$3,$4,$5);' , params, true ); 12 var acc = []; 13 for ( const row of query) 14 { 15 row.rownum = i; 16 acc.push(row); 17 i++; 18 } 19 return acc; 20 }   $ curl -q -X POST http: //localhost:8093/evaluator/v1/libraries/udf3 -u Administrator:password --data-binary @/tmp/qs.js Error while compiling library. Cause: compilation failed: Exception: SyntaxError: Unexpected identifier Location: functions/udf3.js: 6 Code: this is a syntax error on line 6 SyntaxError: Unexpected identifier   If there is a syntax error in the SQL, then that's a runtime error:   $ cat -n /tmp/qs.js 1 function qs(name) 2 { 3 // this is a single line comment 4 var i = 0 ; 5 6 //var query = SELECT $1; // this is my query 7 var params = [ 'a' , 'b' , 'c' , 'd' , 'e' ] 8 //var params = {'a':'a','b':'b','c':'c','d':'d','e':'e'} 9 var query = N1QL( 'xSELECT array_append([],$1,$2,$3,$4,$5);' , params, true ); 10 var acc = []; 11 for ( const row of query) 12 { 13 row.rownum = i; 14 acc.push(row); 15 i++; 16 } 17 return acc; 18 } $ curl -q -X POST http: //localhost:8093/evaluator/v1/libraries/udf3 -u Administrator:password --data-binary @/tmp/qs.js { "status" : "OK" } $ curl -u Administrator:password -d 'pretty=true&statement=execute function qs(1)' http: //localhost:8093/query/service { "requestID" : "1e97c8fa-345d-460f-8f59-891e958efe6f" , "signature" : null , "results" : [ ], "errors" : [ { "cause" : { "details" : [ "Evaluator error" , "Exceptions from JS code : run qs failed:" , "Exception: Error: syntax error - line 1, column 9, near 'xSELECT', at: array_append" , "Location: functions/udf3.js:9 " , "Code: var query = N1QL('xSELECT array_append([],$1,$2,$3,$4,$5);', params, true);" , "Error at qs (functions/udf3.js:9:15)" ], "name" : "qs" , "what" : "(udf3:qs)" }, "code" : 10109 , "msg" : "Error executing function qs (udf3:qs): Evaluator error Exceptions from JS code : run qs failed:\nException: Error: syntax error - line 1, column 9, near 'xSELECT', at: array_append\nLocation: functions/udf3.js:9 \nCode: var query = N1QL('xSELECT array_append([],$1,$2,$3,$4,$5);', params, true);\nError\n at qs (functions/udf3.js:9:15)\n" } ], "status" : "errors" , "metrics" : { "elapsedTime" : "19.65587ms" , "executionTime" : "19.509501ms" , "resultCount" : 0 , "resultSize" : 0 , "serviceLoad" : 3 , "errorCount" : 1 } }

          This is not a bug with js-evaluator. curl removes new line characters from file input: https://stackoverflow.com/questions/17456584/does-curl-remove-new-line-characters/17503683

          To prove this I reproduced the above failure but took a network capture to see what input is js-evaluator receiving:

          Without --data-binary:

          POST /evaluator/v1/libraries/use_cases HTTP/1.1
          Host: 127.0.0.1:9498
          Authorization: Basic QWRtaW5pc3RyYXRvcjphc2Rhc2Q=
          User-Agent: curl/7.64.1
          Accept: */*
          content-type: application/json
          Content-Length: 52
           
          function pi() {  // some comment  return 3.1415926;}
          

          With --data-binary:

          POST /evaluator/v1/libraries/use_cases HTTP/1.1
          Host: 127.0.0.1:9498
          Authorization: Basic QWRtaW5pc3RyYXRvcjphc2Rhc2Q=
          User-Agent: curl/7.64.1
          Accept: */*
          content-type: application/json
          Content-Length: 56
           
          function pi() {
            // some comment
            return 3.1415926;
          }
          

          Note the "Content-Length", we are receiving 4 more characters with --data-binary where the 4 characters are the 4 newline characters.

          With an input as "function pi()

          { // some comment return 3.1415926;}

          " js-evaluator interprets the return statement as being part of the comment and that the function does not have a closing brace.

          abhishek.jindal Abhishek Jindal added a comment - This is not a bug with js-evaluator. curl removes new line characters from file input: https://stackoverflow.com/questions/17456584/does-curl-remove-new-line-characters/17503683 To prove this I reproduced the above failure but took a network capture to see what input is js-evaluator receiving: Without --data-binary: POST /evaluator/v1/libraries/use_cases HTTP/1.1 Host: 127.0.0.1:9498 Authorization: Basic QWRtaW5pc3RyYXRvcjphc2Rhc2Q= User-Agent: curl/7.64.1 Accept: */* content-type: application/json Content-Length: 52   function pi() { // some comment return 3.1415926;} With --data-binary: POST /evaluator/v1/libraries/use_cases HTTP/1.1 Host: 127.0.0.1:9498 Authorization: Basic QWRtaW5pc3RyYXRvcjphc2Rhc2Q= User-Agent: curl/7.64.1 Accept: */* content-type: application/json Content-Length: 56   function pi() { // some comment return 3.1415926; } Note the "Content-Length", we are receiving 4 more characters with --data-binary where the 4 characters are the 4 newline characters. With an input as "function pi() { // some comment return 3.1415926;} " js-evaluator interprets the return statement as being part of the comment and that the function does not have a closing brace.

          Closing all Duplicates, Not a Bug, Incomplete, Duplicate

          ritam.sharma Ritam Sharma added a comment - Closing all Duplicates, Not a Bug, Incomplete, Duplicate

          People

            pierre.regazzoni Pierre Regazzoni
            pierre.regazzoni Pierre Regazzoni
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes

                PagerDuty