From 4a919235c562e6a9972654d51dfb889ca20b2f70 Mon Sep 17 00:00:00 2001 From: Bob den Os Date: Thu, 24 Oct 2024 15:53:54 +0200 Subject: [PATCH 1/2] Enable cesu8 by default for hdb and encode entries streams --- hana/lib/HANAService.js | 1 + hana/lib/drivers/hdb.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/hana/lib/HANAService.js b/hana/lib/HANAService.js index 801a11253..b6aec0f53 100644 --- a/hana/lib/HANAService.js +++ b/hana/lib/HANAService.js @@ -661,6 +661,7 @@ class HANAService extends SQLService { const _stream = entries => { const stream = Readable.from(this.INSERT_entries_stream(entries, 'hex'), { objectMode: false }) + stream.type = 'json' stream._raw = entries return stream } diff --git a/hana/lib/drivers/hdb.js b/hana/lib/drivers/hdb.js index 212115444..41f7f362c 100644 --- a/hana/lib/drivers/hdb.js +++ b/hana/lib/drivers/hdb.js @@ -22,7 +22,6 @@ class HDBDriver extends driver { */ constructor(creds) { creds = { - useCesu8: false, fetchSize: 1 << 16, // V8 default memory page size ...creds, } @@ -138,7 +137,7 @@ class HDBDriver extends driver { _getResultForProcedure(rows, outParameters) { // on hdb, rows already contains results for scalar params const isArray = Array.isArray(rows) - const result = isArray ? {...rows[0]} : {...rows} + const result = isArray ? { ...rows[0] } : { ...rows } // merge table output params into scalar params const args = isArray ? rows.slice(1) : [] @@ -158,6 +157,13 @@ class HDBDriver extends driver { const streams = [] values = values.map((v, i) => { if (v instanceof Stream) { + if (this._creds.useCesu8 !== false && v.type === 'json') { + const encode = iconv.encodeStream('cesu8') + v.setEncoding('utf-8') + v.pipe(encode) + return encode + } + streams[i] = v const iterator = v[Symbol.asyncIterator]() return Readable.from(iterator, { objectMode: false }) From 1944d3e5f8a69ffddd4ed00a88dc1e0f5dd5456f Mon Sep 17 00:00:00 2001 From: Bob den Os Date: Fri, 25 Oct 2024 08:45:31 +0200 Subject: [PATCH 2/2] use pipeline for proper error propegation --- hana/lib/HANAService.js | 3 ++- hana/lib/drivers/hdb.js | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/hana/lib/HANAService.js b/hana/lib/HANAService.js index b6aec0f53..31e123f05 100644 --- a/hana/lib/HANAService.js +++ b/hana/lib/HANAService.js @@ -236,7 +236,7 @@ class HANAService extends SQLService { const row = rows[i] const expands = JSON.parse(row._expands_) const blobs = JSON.parse(row._blobs_) - const data = Object.assign(JSON.parse(row._json_), expands, blobs) + const data = Object.assign(JSON.parse(row._json_ || '{}'), expands, blobs) Object.keys(blobs).forEach(k => (data[k] = row[k] || data[k])) // REVISIT: try to unify with handleLevel from base driver used for streaming @@ -661,6 +661,7 @@ class HANAService extends SQLService { const _stream = entries => { const stream = Readable.from(this.INSERT_entries_stream(entries, 'hex'), { objectMode: false }) + stream.setEncoding('utf-8') stream.type = 'json' stream._raw = entries return stream diff --git a/hana/lib/drivers/hdb.js b/hana/lib/drivers/hdb.js index 41f7f362c..5d8630c7c 100644 --- a/hana/lib/drivers/hdb.js +++ b/hana/lib/drivers/hdb.js @@ -1,4 +1,4 @@ -const { Readable, Stream } = require('stream') +const { Readable, Stream, promises: { pipeline } } = require('stream') const { StringDecoder } = require('string_decoder') const { text } = require('stream/consumers') @@ -160,7 +160,8 @@ class HDBDriver extends driver { if (this._creds.useCesu8 !== false && v.type === 'json') { const encode = iconv.encodeStream('cesu8') v.setEncoding('utf-8') - v.pipe(encode) + // hdb will react to the stream error no need to handle it twice + pipeline(v, encode).catch(() => { }) return encode }