Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 29 additions & 2 deletions packages/router-core/src/searchParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ export const defaultStringifySearch = stringifySearchWith(
JSON.parse,
)

function canStringBeJsonParsed(value: string) {
if (!value) {
return false
}

const firstCharCode = value.charCodeAt(0)

if (firstCharCode <= 32) {
return true
}

return (
firstCharCode === 34 ||
firstCharCode === 45 ||
firstCharCode === 91 ||
firstCharCode === 123 ||
(firstCharCode >= 48 && firstCharCode <= 57) ||
value === 'true' ||
value === 'false' ||
value === 'null'
)
}

/**
* Build a `parseSearch` function using a provided JSON-like parser.
*
Expand All @@ -30,7 +53,7 @@ export function parseSearchWith(parser: (str: string) => any) {
// Try to parse any query params that might be json
for (const key in query) {
const value = query[key]
if (typeof value === 'string') {
if (typeof value === 'string' && canStringBeJsonParsed(value)) {
try {
query[key] = parser(value)
} catch (_err) {
Expand Down Expand Up @@ -67,7 +90,11 @@ export function stringifySearchWith(
} catch (_err) {
// silent
}
} else if (hasParser && typeof val === 'string') {
} else if (
hasParser &&
typeof val === 'string' &&
canStringBeJsonParsed(val)
) {
try {
// Check if it's a valid parseable string.
// If it is, then stringify it again.
Expand Down
44 changes: 42 additions & 2 deletions packages/router-core/tests/searchParams.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { describe, expect, test } from 'vitest'
import { defaultParseSearch, defaultStringifySearch } from '../src'
import { describe, expect, test, vi } from 'vitest'
import {
defaultParseSearch,
defaultStringifySearch,
parseSearchWith,
stringifySearchWith,
} from '../src'

describe('Search Params serialization and deserialization', () => {
/*
Expand Down Expand Up @@ -98,4 +103,39 @@ describe('Search Params serialization and deserialization', () => {
'?foo=%222024-11-18T00%3A00%3A00.000Z%22',
)
})

test('skips parser work for obviously non-json strings', () => {
const parser = vi.fn(JSON.parse)
const parseSearch = parseSearchWith(parser)
const stringifySearch = stringifySearchWith(JSON.stringify, parser)

expect(parseSearch('?plain=value&other=abc123')).toEqual({
plain: 'value',
other: 'abc123',
})
expect(stringifySearch({ plain: 'value', other: 'abc123' })).toEqual(
'?plain=value&other=abc123',
)

expect(parser).not.toHaveBeenCalled()
})

test('still parses json-like strings', () => {
const parser = vi.fn(JSON.parse)
const parseSearch = parseSearchWith(parser)
const stringifySearch = stringifySearchWith(JSON.stringify, parser)

expect(
parseSearch('?quoted=%22value%22&object=%7B%22ok%22%3Atrue%7D&num=123'),
).toEqual({
quoted: 'value',
object: { ok: true },
num: 123,
})
expect(
stringifySearch({ quoted: '123', object: { ok: true }, num: '42' }),
).toEqual('?quoted=%22123%22&object=%7B%22ok%22%3Atrue%7D&num=%2242%22')

expect(parser).toHaveBeenCalled()
})
})
Loading