1/5
Easy#patchfetchhookssearch-paramsmutation

Isolate tuple searchParams in init hooks

sindresorhus/ky#861 · @atharvasingh7007 · MIT · licence

a slice of a larger PR (2 files) · see it all

description
## Summary - isolate searchParams tuple arrays before init hooks mutate them - prevent tuple edits from leaking into later requests on the same instance - add a regression test covering the supported [[key, value]] search params form ## Root cause cloneInitHookOptions() is supposed to give init hooks a request-local mutable copy of the options, but searchParams in tuple-array form only got a shallow outer array copy. Mutating an existing tuple like options.searchParams[0][1] = ... changed the shared default tuple, so the next request inherited the previous hook mutation. ## Fix - add a cloneSearchParametersForInitHook() helper that deep-copies tuple arrays while preserving the other supported searchParams input forms - keep the existing cloning behavior for strings, objects, and URLSearchParams - add an end-to-end init-hook regression that proves tuple-array defaults no longer leak between requests ## Testing - npm run build - npx ava test/hooks.ts --match "init hook*do not leak across requests" - npx ava test/hooks.ts --match "init hook tuple searchParams mutations do not leak across requests" ## Notes npm test in this Windows checkout still hits upstream xo project-service parsing errors for the repo's test files before it reaches AVA. The targeted hook tests and build above passed locally.
1 commit · 1 file
  • b7ea609fix: isolate tuple searchParams in init hooks

// files changed

Msource/core/Ky.ts+9−1
@@ -84,14 +84,22 @@ const isRequestInstance = (value: unknown): value is Request =>
8484const isResponseInstance = (value: unknown): value is Response =>
8585 value instanceof globalThis.Response || objectToString.call(value) === '[object Response]';
8686
87const cloneSearchParametersForInitHook = (searchParameters: SearchParamsOption | undefined): SearchParamsOption | undefined => {
88 if (Array.isArray(searchParameters)) {
89 return searchParameters.map(parameter => [...parameter]) as SearchParamsOption;
90 }
91
92 return cloneShallow(searchParameters) as SearchParamsOption | undefined;
93};
94
8795// Shallow-clone mutable option properties so init hook mutations don't leak across requests.
8896function cloneInitHookOptions(options: Options): Options {
8997 const clonedOptions: Options = {
9098 ...options,
9199 json: cloneShallow(options.json),
92100 context: cloneShallow(options.context)!,
93101 headers: cloneShallow(options.headers)!,
94 searchParams: cloneShallow(options.searchParams) as SearchParamsOption | undefined,
102 searchParams: cloneSearchParametersForInitHook(options.searchParams),
95103 };
96104
97105 if (options.retry !== undefined) {