Skip to content

Commit

Permalink
fix(stega): fallback to original value if invalid JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jan 23, 2024
1 parent 9240509 commit d51963a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/stega/vercelStegaCleanAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import {vercelStegaSplit} from '@vercel/stega'
* @alpha
*/
export function vercelStegaCleanAll<Result = unknown>(result: Result): Result {
return JSON.parse(
JSON.stringify(result, (key, value) => {
if (typeof value !== 'string') return value
return vercelStegaSplit(value).cleaned
}),
)
try {
return JSON.parse(
JSON.stringify(result, (key, value) => {
if (typeof value !== 'string') return value
return vercelStegaSplit(value).cleaned
}),
)
} catch {
return result
}
}
34 changes: 34 additions & 0 deletions test/stega/vercelStegaCleanAll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,39 @@ test('it removes everything', () => {
vercelStegaCombine('stega', editInfo),
vercelStegaCombine('embedded', editInfo),
].join(' ')
expect(encoded).not.toEqual(payload)
expect(vercelStegaCleanAll(encoded)).toEqual(payload)
})

test('it handles strings', () => {
const payload = 'foo'
const editInfo = JSON.stringify({origin: 'sanity.io', href: '/studio'})
const encoded = vercelStegaCombine(payload, editInfo)
expect(encoded).not.toEqual(payload)
expect(vercelStegaCleanAll(encoded)).toEqual(payload)
})

test('it handles values that are not supported by JSON', () => {
expect(vercelStegaCleanAll(undefined)).toMatchInlineSnapshot(`undefined`)
expect(vercelStegaCleanAll(null)).toMatchInlineSnapshot(`null`)
expect(vercelStegaCleanAll(Symbol('foo'))).toMatchInlineSnapshot(`Symbol(foo)`)
expect(vercelStegaCleanAll(new Set([1, 2, 3]))).toMatchInlineSnapshot(`{}`)
expect(
vercelStegaCleanAll(
new Map([
[0, 0],
[1, 1],
[2, 2],
]),
),
).toMatchInlineSnapshot(`{}`)
expect(vercelStegaCleanAll([{foo: undefined, bar: null, baz: new Date('1995-12-17T03:24:00')}]))
.toMatchInlineSnapshot(`

Check failure on line 47 in test/stega/vercelStegaCleanAll.test.ts

View workflow job for this annotation

GitHub Actions / Build, lint and test coverage

Error: Snapshot `it handles values that are not supported by JSON 6` mismatched

at test/stega/vercelStegaCleanAll.test.ts:47:6
[
{
"bar": null,
"baz": "1995-12-17T02:24:00.000Z",
},
]
`)
})

0 comments on commit d51963a

Please sign in to comment.