0
votes

I use the code proposed as an example in the documentation for Domino AppDev Pack 1.0.4 , the only difference is the reading of a text file (body.txt) as a buffer, this file containing only simple long text (40Ko).

When it is executed, the document is created in the database and the rest of the code does not return an error. But finally, the rich text field was not added to the document. Here the response returned:

response: {"fields":[{"fieldName":"Body","unid":"8EA69129BEECA6DEC1258554002F5DCD","error":{"name":"ProtonError","code":65577,"id":"RICH_TEXT_STREAM_CORRUPT"}}]}

My goal is to write very long text (more than 64 Ko) in a rich text field. I use in the example a text file for the buffer but it could be later something like const buffer = Buffer.from ('very long text ...')

Is this the right way or does it have to be done differently ?

I'm using a Windows system with IBM Domino (r) Server (64 Bit), Release 10.0.1FP4 and AppDevPack 1.0.4.

Thank you in advance for your help

Here's code :

const write = async (database) => {
  let writable;
  let result;
  try {
    // Create a document with subject write-example-1 to hold rich text
    const unid = await database.createDocument({
      document: {
        Form: 'RichDiscussion',
        Title: 'write-example-1',
      },
    });
    writable = await database.bulkCreateRichTextStream({});
    result = await new Promise((resolve, reject) => {
      // Set up event handlers.
      // Reject the Promise if there is a connection-level error.
      writable.on('error', (e) => {
        reject(e);
      });
      // Return the response from writing when resolving the Promise.
      writable.on('response', (response) => {
        console.log("response: " + JSON.stringify(response));
        resolve(response);
      });
      // Indicates which document and item name to use.
      writable.field({ unid, fieldName: 'Body' });
      let offset = 0;
      // Assume for purposes of this example that we buffer the entire file.
      const buffer = fs.readFileSync('/driver/body.txt');
      // When writing large amounts of data, it is necessary to
      // wait for the client-side to complete the previous write
      // before writing more data.
      const writeData = () => {
        let draining = true;
        while (offset < buffer.length && draining) {
          const remainingBytes = buffer.length - offset;
          let chunkSize = 16 * 1024;
          if (remainingBytes < chunkSize) {
            chunkSize = remainingBytes;
          }
          draining = writable.write(buffer.slice(offset, offset + chunkSize));
          offset += chunkSize;
        }
        if (offset < buffer.length) {
          // Buffer is not draining. Whenever the drain event is emitted
          // call this function again to write more data.
          writable.once('drain', writeData);
        }
      };
      writeData();
      writable = undefined;
    });
  } catch (e) {
    console.log(`Unexpected exception ${e.message}`);
  } finally {
    if (writable) {
      writable.end();
    }
  }
  return result;
};
1

1 Answers

1
votes

As of appdev pack 1.0.4, the rich text stream accepts writing data of valid rich text cd format, in the LMBCS character set. We are currently working on a library to help you write valid rich text data to the stream.

I'd love to hear more about your use cases, and we're excited you're already poking around the feature! If you can join the openntf slack channel, I usually hang out there.