Skip to content
Open
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
16 changes: 15 additions & 1 deletion lib/utils/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,21 @@ const getArrayOrObject = (items) => {
return Object.assign({}, ...items.filter(o => isPlainObject(o)))
}

const redactValue = (obj) => JSON.parse(redactLog(JSON.stringify(obj)))
const redactStrings = (obj) => {
if (typeof obj === 'string') {
return redactLog(obj)
}
if (Array.isArray(obj)) {
return obj.map(redactStrings)
}
if (isPlainObject(obj)) {
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, redactStrings(v)]))
}
return obj
}

// Redact one string at a time. Redacting the serialized document instead lets a url in one value match across the punctuation around it and corrupt the json
const redactValue = (obj) => redactStrings(JSON.parse(JSON.stringify(obj)))

const getJsonBuffer = ({ [JSON_ERROR_KEY]: metaError }, buffer) => {
const items = []
Expand Down
30 changes: 30 additions & 0 deletions test/lib/utils/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,36 @@ t.test('json output redacts by default', async t => {
'inline redact: false preserves uuid values')
})

t.test('json output with a url inside a value stays valid json', async t => {
const { META } = require('proc-log')
const { output, outputs } = await mockDisplay(t)

output.buffer({
dependencies: {
'@esbuild-kit/esm-loader': {
deprecated: 'Merged into tsx: https://tsx.hirok.io',
dev: true,
_id: '@esbuild-kit/esm-loader@2.6.5',
},
},
registry: 'https://user:hunter2@registry.npmjs.org/',
versions: ['2.6.5'],
before: new Date('2024-01-01'),
})
output.flush({ [META]: true, json: true })

t.equal(outputs.length, 1, 'one output')
const parsed = JSON.parse(outputs[0])
const dep = parsed.dependencies['@esbuild-kit/esm-loader']
t.equal(dep.deprecated, 'Merged into tsx: https://tsx.hirok.io',
'a url in one value does not swallow the values after it')
t.equal(dep._id, '@esbuild-kit/esm-loader@2.6.5', 'the following values are intact')
t.strictSame(parsed.versions, ['2.6.5'], 'arrays are walked too')
t.equal(parsed.registry, 'https://user:***@registry.npmjs.org/',
'url passwords are still redacted')
t.equal(parsed.before, '2024-01-01T00:00:00.000Z', 'toJSON values are still serialized')
})

t.test('prompt functionality', async t => {
t.test('regular prompt completion works', async t => {
const { input } = await mockDisplay(t)
Expand Down
Loading