node-client-api icon indicating copy to clipboard operation
node-client-api copied to clipboard

ResultProvider.result() sometimes returns a text node as a Buffer

Open fgeorges opened this issue 9 months ago • 1 comments

In some circumstances, ResultProvider.result() returns a Buffer object for a text node. This does not seem to be documented, and it is not clear how to handle them (e.g. which encoding to use.)

Neither ResultProvider.result() nor DatabaseClient.eval() documents this.

In order to replicate this, create 2 text documents in a database:

  • /test.txt, with the following content: Hello, world!
  • /hello.xqy, with the following content: 'Hello, world!'

If you execute the following script:

"use strict";

const ml = require('marklogic');

async function main(client)
{
    const res = client.eval(`cts.doc('/hello.xqy')`);
    console.dir(await res.result());
    client.release();
}

main(ml.createDatabaseClient({
    host:     'localhost',
    user:     'admin',
    password: 'admin',
    port:     8000,
    authType: 'DIGEST',
    database: 'Documents',
}));

The output is the following:

[
  {
    format: 'text',
    datatype: 'node()',
    value: Buffer(16) [Uint8Array] [
       39,  72, 101, 108, 108,
      111,  44,  32, 119, 111,
      114, 108, 100,  33,  39,
       10
    ]
  }
]

Is this intentional? Is there a switch to enable/disable this behaviour? How are we supposed to deal with it? (using .toString()?, with which encoding?)

fgeorges avatar Feb 27 '25 10:02 fgeorges

Adding some variations as a comment, in order to keep the main description shorter.

If you change the code to client.eval(`cts.doc('/test.txt')`), you get the following result:

[ { format: 'text', datatype: 'node()', value: 'Hello, world!' } ]

This: client.eval(`[cts.doc('/test.txt'), cts.doc('/hello.xqy')]`), gives:

[
  {
    format: 'json',
    datatype: 'node()',
    value: [ 'Hello, world!', "'Hello, world!'\n" ]
  }
]

And finally this one: client.eval(`Sequence.from([cts.doc('/test.txt'), cts.doc('/hello.xqy')])`) gives you this:

[
  { format: 'text', datatype: 'node()', value: 'Hello, world!' },
  {
    format: 'text',
    datatype: 'node()',
    value: Buffer(16) [Uint8Array] [
       39,  72, 101, 108, 108,
      111,  44,  32, 119, 111,
      114, 108, 100,  33,  39,
       10
    ]
  }
]

fgeorges avatar Feb 27 '25 10:02 fgeorges

@fgeorges The fix for this issue will be included in the next release. Thanks.!

anu3990 avatar May 16 '25 19:05 anu3990

Thank you, @anu3990 ! I will test it once it's released.

fgeorges avatar May 23 '25 19:05 fgeorges

Fixed in 3.7.0

rjrudin avatar Jul 30 '25 14:07 rjrudin