Skip to content

Commit

Permalink
Revert "fix: do not override .toJSON of buffers (#949)"
Browse files Browse the repository at this point in the history
This reverts commit ed52f72.
  • Loading branch information
BobdenOs authored Dec 18, 2024
1 parent ed52f72 commit 4e0c042
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions db-service/lib/cqn2sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { Readable } = require('stream')

const DEBUG = cds.debug('sql|sqlite')
const LOG_SQL = cds.log('sql')
const LOG_SQLITE = cds.log('sqlite')
const LOG_SQLITE = cds.log('sqlite')

class CQN2SQLRenderer {
/**
Expand All @@ -25,7 +25,7 @@ class CQN2SQLRenderer {
if (cds.env.sql.names === 'quoted') {
this.class.prototype.name = (name, query) => {
const e = name.id || name
return (query?.target || this.model?.definitions[e])?.['@cds.persistence.name'] || e
return (query?.target || this.model?.definitions[e])?.['@cds.persistence.name'] || e
}
this.class.prototype.quote = (s) => `"${String(s).replace(/"/g, '""')}"`
}
Expand Down Expand Up @@ -86,7 +86,7 @@ class CQN2SQLRenderer {
if (vars && Object.keys(vars).length && !this.values?.length) this.values = vars
const sanitize_values = process.env.NODE_ENV === 'production' && cds.env.log.sanitize_values !== false


if (DEBUG && (LOG_SQL._debug || LOG_SQLITE._debug)) {
let values = sanitize_values && (this.entries || this.values?.length > 0) ? ['***'] : this.entries || this.values || []
if (values && !Array.isArray(values)) {
Expand All @@ -95,7 +95,7 @@ class CQN2SQLRenderer {
DEBUG(this.sql, ...values)
}


return this
}

Expand Down Expand Up @@ -528,6 +528,9 @@ class CQN2SQLRenderer {

async *INSERT_entries_stream(entries, binaryEncoding = 'base64') {
const elements = this.cqn.target?.elements || {}
const transformBase64 = binaryEncoding === 'base64'
? a => a
: a => a != null ? Buffer.from(a, 'base64').toString(binaryEncoding) : a
const bufferLimit = 65536 // 1 << 16
let buffer = '['

Expand Down Expand Up @@ -558,8 +561,8 @@ class CQN2SQLRenderer {

buffer += '"'
} else {
if (val != null && elements[key]?.type in this.BINARY_TYPES) {
val = Buffer.from(val, 'base64').toString(binaryEncoding)
if (elements[key]?.type in this.BINARY_TYPES) {
val = transformBase64(val)
}
buffer += `${keyJSON}${JSON.stringify(val)}`
}
Expand Down Expand Up @@ -1146,6 +1149,11 @@ class CQN2SQLRenderer {
}
}

// REVISIT: Workaround for JSON.stringify to work with buffers
Buffer.prototype.toJSON = function () {
return this.toString('base64')
}

Readable.prototype[require('node:util').inspect.custom] = Readable.prototype.toJSON = function () { return this._raw || `[object ${this.constructor.name}]` }

const ObjectKeys = o => (o && [...ObjectKeys(o.__proto__), ...Object.keys(o)]) || []
Expand Down

0 comments on commit 4e0c042

Please sign in to comment.