MCPcopy Create free account
hub / github.com/nodejs/node / getJsonBuffer

Function getJsonBuffer

deps/npm/lib/utils/display.js:104–138  ·  view source on GitHub ↗
({ [JSON_ERROR_KEY]: metaError }, buffer)

Source from the content-addressed store, hash-verified

102const redactValue = (obj) => JSON.parse(redactLog(JSON.stringify(obj)))
103
104const getJsonBuffer = ({ [JSON_ERROR_KEY]: metaError }, buffer) => {
105 const items = []
106 // meta also contains the meta object passed to flush
107 const errors = metaError ? [metaError] : []
108 // index 1 is the meta, 2 is the logged argument
109 for (const [, { [JSON_ERROR_KEY]: error, redact = true }, obj] of buffer) {
110 if (obj) {
111 items.push(redact ? redactValue(obj) : obj)
112 }
113 if (error) {
114 errors.push(error)
115 }
116 }
117
118 if (!items.length && !errors.length) {
119 return null
120 }
121
122 const res = getArrayOrObject(items)
123
124 // This skips any error checking since we can only set an error property on an object that can be stringified
125 // XXX(BREAKING_CHANGE): remove this in favor of always returning an object with result and error keys
126 if (isPlainObject(res) && errors.length) {
127 // This is not ideal.
128 // JSON output has always been keyed at the root with an `error` key, so we cant change that without it being a breaking change. At the same time some commands output arbitrary keys at the top level of the output, such as package names.
129 // So the output could already have the same key. The choice here is to overwrite it with our error since that is (probably?) more important.
130 // XXX(BREAKING_CHANGE): all json output should be keyed under well known keys, eg `result` and `error`
131 if (res[ERROR_KEY]) {
132 log.warn('', `overwriting existing ${ERROR_KEY} on json output`)
133 }
134 res[ERROR_KEY] = getArrayOrObject(errors)
135 }
136
137 return res
138}
139
140const withMeta = (handler) => (level, ...args) => {
141 let meta = {}

Callers 1

DisplayClass · 0.85

Calls 5

redactValueFunction · 0.85
getArrayOrObjectFunction · 0.85
isPlainObjectFunction · 0.85
warnMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected