Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enable cesu8 by default for hdb driver and encode entries streams #868

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -667,6 +667,8 @@ 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
}
Expand Down
13 changes: 10 additions & 3 deletions hana/lib/drivers/hdb.js
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -22,7 +22,6 @@ class HDBDriver extends driver {
*/
constructor(creds) {
creds = {
useCesu8: false,
fetchSize: 1 << 16, // V8 default memory page size
...creds,
}
Expand Down Expand Up @@ -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) : []
Expand All @@ -158,6 +157,14 @@ 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')
// hdb will react to the stream error no need to handle it twice
pipeline(v, encode).catch(() => { })
return encode
}

streams[i] = v
const iterator = v[Symbol.asyncIterator]()
return Readable.from(iterator, { objectMode: false })
Expand Down