Skip to content

Commit

Permalink
fix: do not override .toJSON of buffers (#949)
Browse files Browse the repository at this point in the history
Co-authored-by: Bob den Os <[email protected]>
  • Loading branch information
johannes-vogel and BobdenOs authored Dec 18, 2024
1 parent 6aa7878 commit ed52f72
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 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,9 +528,6 @@ 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 @@ -561,8 +558,8 @@ class CQN2SQLRenderer {

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

Please sign in to comment.