awslabs / llrt

LLRT (Low Latency Runtime) is an experimental, lightweight JavaScript runtime designed to address the growing demand for fast and efficient Serverless applications.
Apache License 2.0
7.73k stars 341 forks source link

URLSearchParams.set() not behaving like specified #396

Closed chrisdirnberger closed 4 weeks ago

chrisdirnberger commented 4 weeks ago

Hey there, I found a small little something on the URLSearchParams interface:

The URLSearchParams.set() method does not create the parameter if it does not yet exist. Version: v0.1.14-beta

As documented here https://url.spec.whatwg.org/#dom-urlsearchparams-set and here https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/set the set() method should behave like this:

The set() method of the URLSearchParams interface sets the value associated with a given search parameter to the given value. If there were several matching values, this method deletes the others. If the search parameter doesn't exist, this method creates it.

In the browser both variants will return the same result, whilst running on the llrt the behavior differs.

const searchParams = new URLSearchParams('?foo=&bar=baz');
searchParams.set('foo', 'bar');
console.log(searchParams.toString()); // returns foo=bar&bar=baz

const searchParams = new URLSearchParams('?bar=baz');
searchParams.set('foo', 'bar');
console.log(searchParams.toString()); // returns bar=baz

Workaround in the meantime:

if(searchParams.has('foo')) {
  searchParams.set('foo', 'bar');
} else {
  searchParams.append('foo', 'bar');
}
richarddavison commented 4 weeks ago

Thanks for spotting!

richarddavison commented 4 weeks ago

Fixed in https://github.com/awslabs/llrt/pull/397