fix(client): better inspection of buffers
This commit is contained in:
parent
4b417afd81
commit
0525a59ab3
1 changed files with 15 additions and 5 deletions
|
@ -29,6 +29,9 @@ function getAllGettersNames(obj: object): string[] {
|
||||||
return getters
|
return getters
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const bufferToJsonOriginal = (Buffer as any).toJSON
|
||||||
|
const bufferToJsonInspect = function () { return this.toString('base64') }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Small helper function that adds `toJSON` and `util.custom.inspect`
|
* Small helper function that adds `toJSON` and `util.custom.inspect`
|
||||||
* methods to a given class based on its getters
|
* methods to a given class based on its getters
|
||||||
|
@ -51,25 +54,32 @@ export function makeInspectable(
|
||||||
// dirty hack to set name for inspect result
|
// dirty hack to set name for inspect result
|
||||||
const proto = new Function(`return function ${obj.name}(){}`)().prototype
|
const proto = new Function(`return function ${obj.name}(){}`)().prototype
|
||||||
|
|
||||||
obj.prototype.toJSON = function () {
|
obj.prototype.toJSON = function (nested = false) {
|
||||||
|
if (!nested) {
|
||||||
|
(Buffer as any).toJSON = bufferToJsonInspect
|
||||||
|
}
|
||||||
|
|
||||||
const ret: any = Object.create(proto)
|
const ret: any = Object.create(proto)
|
||||||
getters.forEach((it) => {
|
getters.forEach((it) => {
|
||||||
try {
|
try {
|
||||||
let val = this[it]
|
let val = this[it]
|
||||||
if (Buffer.isBuffer(val)) {
|
if (
|
||||||
val = val.toString('base64')
|
|
||||||
} else if (
|
|
||||||
val &&
|
val &&
|
||||||
typeof val === 'object' &&
|
typeof val === 'object' &&
|
||||||
typeof val.toJSON === 'function'
|
typeof val.toJSON === 'function'
|
||||||
) {
|
) {
|
||||||
val = val.toJSON()
|
val = val.toJSON(true)
|
||||||
}
|
}
|
||||||
ret[it] = val
|
ret[it] = val
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
ret[it] = "Error: " + e.message
|
ret[it] = "Error: " + e.message
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (!nested) {
|
||||||
|
(Buffer as any).toJSON = bufferToJsonOriginal
|
||||||
|
}
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
if (util) {
|
if (util) {
|
||||||
|
|
Loading…
Reference in a new issue